php-upb.c 267 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132
  1. /* Amalgamated source file */
  2. #include "php-upb.h"
  3. /*
  4. * This is where we define macros used across upb.
  5. *
  6. * All of these macros are undef'd in port_undef.inc to avoid leaking them to
  7. * users.
  8. *
  9. * The correct usage is:
  10. *
  11. * #include "upb/foobar.h"
  12. * #include "upb/baz.h"
  13. *
  14. * // MUST be last included header.
  15. * #include "upb/port_def.inc"
  16. *
  17. * // Code for this file.
  18. * // <...>
  19. *
  20. * // Can be omitted for .c files, required for .h.
  21. * #include "upb/port_undef.inc"
  22. *
  23. * This file is private and must not be included by users!
  24. */
  25. #include <stdint.h>
  26. #include <stddef.h>
  27. #if UINTPTR_MAX == 0xffffffff
  28. #define UPB_SIZE(size32, size64) size32
  29. #else
  30. #define UPB_SIZE(size32, size64) size64
  31. #endif
  32. /* If we always read/write as a consistent type to each address, this shouldn't
  33. * violate aliasing.
  34. */
  35. #define UPB_PTR_AT(msg, ofs, type) ((type*)((char*)(msg) + (ofs)))
  36. #define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \
  37. *UPB_PTR_AT(msg, case_offset, int) == case_val \
  38. ? *UPB_PTR_AT(msg, offset, fieldtype) \
  39. : default
  40. #define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \
  41. *UPB_PTR_AT(msg, case_offset, int) = case_val; \
  42. *UPB_PTR_AT(msg, offset, fieldtype) = value;
  43. #define UPB_MAPTYPE_STRING 0
  44. /* UPB_INLINE: inline if possible, emit standalone code if required. */
  45. #ifdef __cplusplus
  46. #define UPB_INLINE inline
  47. #elif defined (__GNUC__) || defined(__clang__)
  48. #define UPB_INLINE static __inline__
  49. #else
  50. #define UPB_INLINE static
  51. #endif
  52. #define UPB_ALIGN_UP(size, align) (((size) + (align) - 1) / (align) * (align))
  53. #define UPB_ALIGN_DOWN(size, align) ((size) / (align) * (align))
  54. #define UPB_ALIGN_MALLOC(size) UPB_ALIGN_UP(size, 16)
  55. #define UPB_ALIGN_OF(type) offsetof (struct { char c; type member; }, member)
  56. /* Hints to the compiler about likely/unlikely branches. */
  57. #if defined (__GNUC__) || defined(__clang__)
  58. #define UPB_LIKELY(x) __builtin_expect((x),1)
  59. #define UPB_UNLIKELY(x) __builtin_expect((x),0)
  60. #else
  61. #define UPB_LIKELY(x) (x)
  62. #define UPB_UNLIKELY(x) (x)
  63. #endif
  64. /* Define UPB_BIG_ENDIAN manually if you're on big endian and your compiler
  65. * doesn't provide these preprocessor symbols. */
  66. #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  67. #define UPB_BIG_ENDIAN
  68. #endif
  69. /* Macros for function attributes on compilers that support them. */
  70. #ifdef __GNUC__
  71. #define UPB_FORCEINLINE __inline__ __attribute__((always_inline))
  72. #define UPB_NOINLINE __attribute__((noinline))
  73. #define UPB_NORETURN __attribute__((__noreturn__))
  74. #else /* !defined(__GNUC__) */
  75. #define UPB_FORCEINLINE
  76. #define UPB_NOINLINE
  77. #define UPB_NORETURN
  78. #endif
  79. #if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L
  80. /* C99/C++11 versions. */
  81. #include <stdio.h>
  82. #define _upb_snprintf snprintf
  83. #define _upb_vsnprintf vsnprintf
  84. #define _upb_va_copy(a, b) va_copy(a, b)
  85. #elif defined(_MSC_VER)
  86. /* Microsoft C/C++ versions. */
  87. #include <stdarg.h>
  88. #include <stdio.h>
  89. #if _MSC_VER < 1900
  90. int msvc_snprintf(char* s, size_t n, const char* format, ...);
  91. int msvc_vsnprintf(char* s, size_t n, const char* format, va_list arg);
  92. #define UPB_MSVC_VSNPRINTF
  93. #define _upb_snprintf msvc_snprintf
  94. #define _upb_vsnprintf msvc_vsnprintf
  95. #else
  96. #define _upb_snprintf snprintf
  97. #define _upb_vsnprintf vsnprintf
  98. #endif
  99. #define _upb_va_copy(a, b) va_copy(a, b)
  100. #elif defined __GNUC__
  101. /* A few hacky workarounds for functions not in C89.
  102. * For internal use only!
  103. * TODO(haberman): fix these by including our own implementations, or finding
  104. * another workaround.
  105. */
  106. #define _upb_snprintf __builtin_snprintf
  107. #define _upb_vsnprintf __builtin_vsnprintf
  108. #define _upb_va_copy(a, b) __va_copy(a, b)
  109. #else
  110. #error Need implementations of [v]snprintf and va_copy
  111. #endif
  112. #ifdef __cplusplus
  113. #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || \
  114. (defined(_MSC_VER) && _MSC_VER >= 1900)
  115. /* C++11 is present */
  116. #else
  117. #error upb requires C++11 for C++ support
  118. #endif
  119. #endif
  120. #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
  121. #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
  122. #define UPB_UNUSED(var) (void)var
  123. /* UPB_ASSUME(): in release mode, we tell the compiler to assume this is true.
  124. */
  125. #ifdef NDEBUG
  126. #ifdef __GNUC__
  127. #define UPB_ASSUME(expr) if (!(expr)) __builtin_unreachable()
  128. #elif defined _MSC_VER
  129. #define UPB_ASSUME(expr) if (!(expr)) __assume(0)
  130. #else
  131. #define UPB_ASSUME(expr) do {} if (false && (expr))
  132. #endif
  133. #else
  134. #define UPB_ASSUME(expr) assert(expr)
  135. #endif
  136. /* UPB_ASSERT(): in release mode, we use the expression without letting it be
  137. * evaluated. This prevents "unused variable" warnings. */
  138. #ifdef NDEBUG
  139. #define UPB_ASSERT(expr) do {} while (false && (expr))
  140. #else
  141. #define UPB_ASSERT(expr) assert(expr)
  142. #endif
  143. /* UPB_ASSERT_DEBUGVAR(): assert that uses functions or variables that only
  144. * exist in debug mode. This turns into regular assert. */
  145. #define UPB_ASSERT_DEBUGVAR(expr) assert(expr)
  146. #if defined(__GNUC__) || defined(__clang__)
  147. #define UPB_UNREACHABLE() do { assert(0); __builtin_unreachable(); } while(0)
  148. #else
  149. #define UPB_UNREACHABLE() do { assert(0); } while(0)
  150. #endif
  151. /* UPB_INFINITY representing floating-point positive infinity. */
  152. #include <math.h>
  153. #ifdef INFINITY
  154. #define UPB_INFINITY INFINITY
  155. #else
  156. #define UPB_INFINITY (1.0 / 0.0)
  157. #endif
  158. #ifdef NAN
  159. #define UPB_NAN NAN
  160. #else
  161. #define UPB_NAN (0.0 / 0.0)
  162. #endif
  163. #include <setjmp.h>
  164. #include <string.h>
  165. /* Maps descriptor type -> upb field type. */
  166. static const uint8_t desctype_to_fieldtype[] = {
  167. -1, /* invalid descriptor type */
  168. UPB_TYPE_DOUBLE, /* DOUBLE */
  169. UPB_TYPE_FLOAT, /* FLOAT */
  170. UPB_TYPE_INT64, /* INT64 */
  171. UPB_TYPE_UINT64, /* UINT64 */
  172. UPB_TYPE_INT32, /* INT32 */
  173. UPB_TYPE_UINT64, /* FIXED64 */
  174. UPB_TYPE_UINT32, /* FIXED32 */
  175. UPB_TYPE_BOOL, /* BOOL */
  176. UPB_TYPE_STRING, /* STRING */
  177. UPB_TYPE_MESSAGE, /* GROUP */
  178. UPB_TYPE_MESSAGE, /* MESSAGE */
  179. UPB_TYPE_BYTES, /* BYTES */
  180. UPB_TYPE_UINT32, /* UINT32 */
  181. UPB_TYPE_ENUM, /* ENUM */
  182. UPB_TYPE_INT32, /* SFIXED32 */
  183. UPB_TYPE_INT64, /* SFIXED64 */
  184. UPB_TYPE_INT32, /* SINT32 */
  185. UPB_TYPE_INT64, /* SINT64 */
  186. };
  187. /* Maps descriptor type -> upb map size. */
  188. static const uint8_t desctype_to_mapsize[] = {
  189. -1, /* invalid descriptor type */
  190. 8, /* DOUBLE */
  191. 4, /* FLOAT */
  192. 8, /* INT64 */
  193. 8, /* UINT64 */
  194. 4, /* INT32 */
  195. 8, /* FIXED64 */
  196. 4, /* FIXED32 */
  197. 1, /* BOOL */
  198. UPB_MAPTYPE_STRING, /* STRING */
  199. sizeof(void *), /* GROUP */
  200. sizeof(void *), /* MESSAGE */
  201. UPB_MAPTYPE_STRING, /* BYTES */
  202. 4, /* UINT32 */
  203. 4, /* ENUM */
  204. 4, /* SFIXED32 */
  205. 8, /* SFIXED64 */
  206. 4, /* SINT32 */
  207. 8, /* SINT64 */
  208. };
  209. static const unsigned fixed32_ok = (1 << UPB_DTYPE_FLOAT) |
  210. (1 << UPB_DTYPE_FIXED32) |
  211. (1 << UPB_DTYPE_SFIXED32);
  212. static const unsigned fixed64_ok = (1 << UPB_DTYPE_DOUBLE) |
  213. (1 << UPB_DTYPE_FIXED64) |
  214. (1 << UPB_DTYPE_SFIXED64);
  215. /* Op: an action to be performed for a wire-type/field-type combination. */
  216. #define OP_SCALAR_LG2(n) (n) /* n in [0, 2, 3] => op in [0, 2, 3] */
  217. #define OP_STRING 4
  218. #define OP_BYTES 5
  219. #define OP_SUBMSG 6
  220. /* Ops above are scalar-only. Repeated fields can use any op. */
  221. #define OP_FIXPCK_LG2(n) (n + 5) /* n in [2, 3] => op in [7, 8] */
  222. #define OP_VARPCK_LG2(n) (n + 9) /* n in [0, 2, 3] => op in [9, 11, 12] */
  223. static const int8_t varint_ops[19] = {
  224. -1, /* field not found */
  225. -1, /* DOUBLE */
  226. -1, /* FLOAT */
  227. OP_SCALAR_LG2(3), /* INT64 */
  228. OP_SCALAR_LG2(3), /* UINT64 */
  229. OP_SCALAR_LG2(2), /* INT32 */
  230. -1, /* FIXED64 */
  231. -1, /* FIXED32 */
  232. OP_SCALAR_LG2(0), /* BOOL */
  233. -1, /* STRING */
  234. -1, /* GROUP */
  235. -1, /* MESSAGE */
  236. -1, /* BYTES */
  237. OP_SCALAR_LG2(2), /* UINT32 */
  238. OP_SCALAR_LG2(2), /* ENUM */
  239. -1, /* SFIXED32 */
  240. -1, /* SFIXED64 */
  241. OP_SCALAR_LG2(2), /* SINT32 */
  242. OP_SCALAR_LG2(3), /* SINT64 */
  243. };
  244. static const int8_t delim_ops[37] = {
  245. /* For non-repeated field type. */
  246. -1, /* field not found */
  247. -1, /* DOUBLE */
  248. -1, /* FLOAT */
  249. -1, /* INT64 */
  250. -1, /* UINT64 */
  251. -1, /* INT32 */
  252. -1, /* FIXED64 */
  253. -1, /* FIXED32 */
  254. -1, /* BOOL */
  255. OP_STRING, /* STRING */
  256. -1, /* GROUP */
  257. OP_SUBMSG, /* MESSAGE */
  258. OP_BYTES, /* BYTES */
  259. -1, /* UINT32 */
  260. -1, /* ENUM */
  261. -1, /* SFIXED32 */
  262. -1, /* SFIXED64 */
  263. -1, /* SINT32 */
  264. -1, /* SINT64 */
  265. /* For repeated field type. */
  266. OP_FIXPCK_LG2(3), /* REPEATED DOUBLE */
  267. OP_FIXPCK_LG2(2), /* REPEATED FLOAT */
  268. OP_VARPCK_LG2(3), /* REPEATED INT64 */
  269. OP_VARPCK_LG2(3), /* REPEATED UINT64 */
  270. OP_VARPCK_LG2(2), /* REPEATED INT32 */
  271. OP_FIXPCK_LG2(3), /* REPEATED FIXED64 */
  272. OP_FIXPCK_LG2(2), /* REPEATED FIXED32 */
  273. OP_VARPCK_LG2(0), /* REPEATED BOOL */
  274. OP_STRING, /* REPEATED STRING */
  275. OP_SUBMSG, /* REPEATED GROUP */
  276. OP_SUBMSG, /* REPEATED MESSAGE */
  277. OP_BYTES, /* REPEATED BYTES */
  278. OP_VARPCK_LG2(2), /* REPEATED UINT32 */
  279. OP_VARPCK_LG2(2), /* REPEATED ENUM */
  280. OP_FIXPCK_LG2(2), /* REPEATED SFIXED32 */
  281. OP_FIXPCK_LG2(3), /* REPEATED SFIXED64 */
  282. OP_VARPCK_LG2(2), /* REPEATED SINT32 */
  283. OP_VARPCK_LG2(3), /* REPEATED SINT64 */
  284. };
  285. /* Data pertaining to the parse. */
  286. typedef struct {
  287. const char *limit; /* End of delimited region or end of buffer. */
  288. upb_arena *arena;
  289. int depth;
  290. uint32_t end_group; /* Set to field number of END_GROUP tag, if any. */
  291. jmp_buf err;
  292. } upb_decstate;
  293. typedef union {
  294. bool bool_val;
  295. int32_t int32_val;
  296. int64_t int64_val;
  297. uint32_t uint32_val;
  298. uint64_t uint64_val;
  299. upb_strview str_val;
  300. } wireval;
  301. static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
  302. const upb_msglayout *layout);
  303. UPB_NORETURN static void decode_err(upb_decstate *d) { longjmp(d->err, 1); }
  304. void decode_verifyutf8(upb_decstate *d, const char *buf, int len) {
  305. static const uint8_t utf8_offset[] = {
  306. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  307. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  308. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  309. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  310. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  311. 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  312. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  313. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  314. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  315. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  316. 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0,
  317. };
  318. int i, j;
  319. uint8_t offset;
  320. i = 0;
  321. while (i < len) {
  322. offset = utf8_offset[(uint8_t)buf[i]];
  323. if (offset == 0 || i + offset > len) {
  324. decode_err(d);
  325. }
  326. for (j = i + 1; j < i + offset; j++) {
  327. if ((buf[j] & 0xc0) != 0x80) {
  328. decode_err(d);
  329. }
  330. }
  331. i += offset;
  332. }
  333. if (i != len) decode_err(d);
  334. }
  335. static bool decode_reserve(upb_decstate *d, upb_array *arr, size_t elem) {
  336. bool need_realloc = arr->size - arr->len < elem;
  337. if (need_realloc && !_upb_array_realloc(arr, arr->len + elem, d->arena)) {
  338. decode_err(d);
  339. }
  340. return need_realloc;
  341. }
  342. UPB_NOINLINE
  343. static const char *decode_longvarint64(upb_decstate *d, const char *ptr,
  344. const char *limit, uint64_t *val) {
  345. uint8_t byte;
  346. int bitpos = 0;
  347. uint64_t out = 0;
  348. do {
  349. if (bitpos >= 70 || ptr == limit) decode_err(d);
  350. byte = *ptr;
  351. out |= (uint64_t)(byte & 0x7F) << bitpos;
  352. ptr++;
  353. bitpos += 7;
  354. } while (byte & 0x80);
  355. *val = out;
  356. return ptr;
  357. }
  358. UPB_FORCEINLINE
  359. static const char *decode_varint64(upb_decstate *d, const char *ptr,
  360. const char *limit, uint64_t *val) {
  361. if (UPB_LIKELY(ptr < limit && (*ptr & 0x80) == 0)) {
  362. *val = (uint8_t)*ptr;
  363. return ptr + 1;
  364. } else {
  365. return decode_longvarint64(d, ptr, limit, val);
  366. }
  367. }
  368. static const char *decode_varint32(upb_decstate *d, const char *ptr,
  369. const char *limit, uint32_t *val) {
  370. uint64_t u64;
  371. ptr = decode_varint64(d, ptr, limit, &u64);
  372. if (u64 > UINT32_MAX) decode_err(d);
  373. *val = (uint32_t)u64;
  374. return ptr;
  375. }
  376. static void decode_munge(int type, wireval *val) {
  377. switch (type) {
  378. case UPB_DESCRIPTOR_TYPE_BOOL:
  379. val->bool_val = val->uint64_val != 0;
  380. break;
  381. case UPB_DESCRIPTOR_TYPE_SINT32: {
  382. uint32_t n = val->uint32_val;
  383. val->int32_val = (n >> 1) ^ -(int32_t)(n & 1);
  384. break;
  385. }
  386. case UPB_DESCRIPTOR_TYPE_SINT64: {
  387. uint64_t n = val->uint64_val;
  388. val->int64_val = (n >> 1) ^ -(int64_t)(n & 1);
  389. break;
  390. }
  391. }
  392. }
  393. static const upb_msglayout_field *upb_find_field(const upb_msglayout *l,
  394. uint32_t field_number) {
  395. static upb_msglayout_field none = {0, 0, 0, 0, 0, 0};
  396. /* Lots of optimization opportunities here. */
  397. int i;
  398. if (l == NULL) return &none;
  399. for (i = 0; i < l->field_count; i++) {
  400. if (l->fields[i].number == field_number) {
  401. return &l->fields[i];
  402. }
  403. }
  404. return &none; /* Unknown field. */
  405. }
  406. static upb_msg *decode_newsubmsg(upb_decstate *d, const upb_msglayout *layout,
  407. const upb_msglayout_field *field) {
  408. const upb_msglayout *subl = layout->submsgs[field->submsg_index];
  409. return _upb_msg_new(subl, d->arena);
  410. }
  411. static void decode_tosubmsg(upb_decstate *d, upb_msg *submsg,
  412. const upb_msglayout *layout,
  413. const upb_msglayout_field *field, upb_strview val) {
  414. const upb_msglayout *subl = layout->submsgs[field->submsg_index];
  415. const char *saved_limit = d->limit;
  416. if (--d->depth < 0) decode_err(d);
  417. d->limit = val.data + val.size;
  418. decode_msg(d, val.data, submsg, subl);
  419. d->limit = saved_limit;
  420. if (d->end_group != 0) decode_err(d);
  421. d->depth++;
  422. }
  423. static const char *decode_group(upb_decstate *d, const char *ptr,
  424. upb_msg *submsg, const upb_msglayout *subl,
  425. uint32_t number) {
  426. if (--d->depth < 0) decode_err(d);
  427. ptr = decode_msg(d, ptr, submsg, subl);
  428. if (d->end_group != number) decode_err(d);
  429. d->end_group = 0;
  430. d->depth++;
  431. return ptr;
  432. }
  433. static const char *decode_togroup(upb_decstate *d, const char *ptr,
  434. upb_msg *submsg, const upb_msglayout *layout,
  435. const upb_msglayout_field *field) {
  436. const upb_msglayout *subl = layout->submsgs[field->submsg_index];
  437. return decode_group(d, ptr, submsg, subl, field->number);
  438. }
  439. static const char *decode_toarray(upb_decstate *d, const char *ptr,
  440. upb_msg *msg, const upb_msglayout *layout,
  441. const upb_msglayout_field *field, wireval val,
  442. int op) {
  443. upb_array **arrp = UPB_PTR_AT(msg, field->offset, void);
  444. upb_array *arr = *arrp;
  445. void *mem;
  446. if (!arr) {
  447. upb_fieldtype_t type = desctype_to_fieldtype[field->descriptortype];
  448. arr = _upb_array_new(d->arena, type);
  449. if (!arr) decode_err(d);
  450. *arrp = arr;
  451. }
  452. decode_reserve(d, arr, 1);
  453. switch (op) {
  454. case OP_SCALAR_LG2(0):
  455. case OP_SCALAR_LG2(2):
  456. case OP_SCALAR_LG2(3):
  457. /* Append scalar value. */
  458. mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << op, void);
  459. arr->len++;
  460. memcpy(mem, &val, 1 << op);
  461. return ptr;
  462. case OP_STRING:
  463. decode_verifyutf8(d, val.str_val.data, val.str_val.size);
  464. /* Fallthrough. */
  465. case OP_BYTES:
  466. /* Append bytes. */
  467. mem =
  468. UPB_PTR_AT(_upb_array_ptr(arr), arr->len * sizeof(upb_strview), void);
  469. arr->len++;
  470. memcpy(mem, &val, sizeof(upb_strview));
  471. return ptr;
  472. case OP_SUBMSG: {
  473. /* Append submessage / group. */
  474. upb_msg *submsg = decode_newsubmsg(d, layout, field);
  475. *UPB_PTR_AT(_upb_array_ptr(arr), arr->len * sizeof(void *), upb_msg *) =
  476. submsg;
  477. arr->len++;
  478. if (UPB_UNLIKELY(field->descriptortype == UPB_DTYPE_GROUP)) {
  479. ptr = decode_togroup(d, ptr, submsg, layout, field);
  480. } else {
  481. decode_tosubmsg(d, submsg, layout, field, val.str_val);
  482. }
  483. return ptr;
  484. }
  485. case OP_FIXPCK_LG2(2):
  486. case OP_FIXPCK_LG2(3): {
  487. /* Fixed packed. */
  488. int lg2 = op - OP_FIXPCK_LG2(0);
  489. int mask = (1 << lg2) - 1;
  490. size_t count = val.str_val.size >> lg2;
  491. if ((val.str_val.size & mask) != 0) {
  492. decode_err(d); /* Length isn't a round multiple of elem size. */
  493. }
  494. decode_reserve(d, arr, count);
  495. mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
  496. arr->len += count;
  497. memcpy(mem, val.str_val.data, val.str_val.size);
  498. return ptr;
  499. }
  500. case OP_VARPCK_LG2(0):
  501. case OP_VARPCK_LG2(2):
  502. case OP_VARPCK_LG2(3): {
  503. /* Varint packed. */
  504. int lg2 = op - OP_VARPCK_LG2(0);
  505. int scale = 1 << lg2;
  506. const char *ptr = val.str_val.data;
  507. const char *end = ptr + val.str_val.size;
  508. char *out = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
  509. while (ptr < end) {
  510. wireval elem;
  511. ptr = decode_varint64(d, ptr, end, &elem.uint64_val);
  512. decode_munge(field->descriptortype, &elem);
  513. if (decode_reserve(d, arr, 1)) {
  514. out = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
  515. }
  516. arr->len++;
  517. memcpy(out, &elem, scale);
  518. out += scale;
  519. }
  520. if (ptr != end) decode_err(d);
  521. return ptr;
  522. }
  523. default:
  524. UPB_UNREACHABLE();
  525. }
  526. }
  527. static void decode_tomap(upb_decstate *d, upb_msg *msg,
  528. const upb_msglayout *layout,
  529. const upb_msglayout_field *field, wireval val) {
  530. upb_map **map_p = UPB_PTR_AT(msg, field->offset, upb_map *);
  531. upb_map *map = *map_p;
  532. upb_map_entry ent;
  533. const upb_msglayout *entry = layout->submsgs[field->submsg_index];
  534. if (!map) {
  535. /* Lazily create map. */
  536. const upb_msglayout *entry = layout->submsgs[field->submsg_index];
  537. const upb_msglayout_field *key_field = &entry->fields[0];
  538. const upb_msglayout_field *val_field = &entry->fields[1];
  539. char key_size = desctype_to_mapsize[key_field->descriptortype];
  540. char val_size = desctype_to_mapsize[val_field->descriptortype];
  541. UPB_ASSERT(key_field->offset == 0);
  542. UPB_ASSERT(val_field->offset == sizeof(upb_strview));
  543. map = _upb_map_new(d->arena, key_size, val_size);
  544. *map_p = map;
  545. }
  546. /* Parse map entry. */
  547. memset(&ent, 0, sizeof(ent));
  548. if (entry->fields[1].descriptortype == UPB_DESCRIPTOR_TYPE_MESSAGE ||
  549. entry->fields[1].descriptortype == UPB_DESCRIPTOR_TYPE_GROUP) {
  550. /* Create proactively to handle the case where it doesn't appear. */
  551. ent.v.val.val = (uint64_t)_upb_msg_new(entry->submsgs[0], d->arena);
  552. }
  553. decode_tosubmsg(d, &ent.k, layout, field, val.str_val);
  554. /* Insert into map. */
  555. _upb_map_set(map, &ent.k, map->key_size, &ent.v, map->val_size, d->arena);
  556. }
  557. static const char *decode_tomsg(upb_decstate *d, const char *ptr, upb_msg *msg,
  558. const upb_msglayout *layout,
  559. const upb_msglayout_field *field, wireval val,
  560. int op) {
  561. void *mem = UPB_PTR_AT(msg, field->offset, void);
  562. int type = field->descriptortype;
  563. /* Set presence if necessary. */
  564. if (field->presence < 0) {
  565. /* Oneof case */
  566. uint32_t *oneof_case = _upb_oneofcase_field(msg, field);
  567. if (op == OP_SUBMSG && *oneof_case != field->number) {
  568. memset(mem, 0, sizeof(void*));
  569. }
  570. *oneof_case = field->number;
  571. } else if (field->presence > 0) {
  572. _upb_sethas_field(msg, field);
  573. }
  574. /* Store into message. */
  575. switch (op) {
  576. case OP_SUBMSG: {
  577. upb_msg **submsgp = mem;
  578. upb_msg *submsg = *submsgp;
  579. if (!submsg) {
  580. submsg = decode_newsubmsg(d, layout, field);
  581. *submsgp = submsg;
  582. }
  583. if (UPB_UNLIKELY(type == UPB_DTYPE_GROUP)) {
  584. ptr = decode_togroup(d, ptr, submsg, layout, field);
  585. } else {
  586. decode_tosubmsg(d, submsg, layout, field, val.str_val);
  587. }
  588. break;
  589. }
  590. case OP_STRING:
  591. decode_verifyutf8(d, val.str_val.data, val.str_val.size);
  592. /* Fallthrough. */
  593. case OP_BYTES:
  594. memcpy(mem, &val, sizeof(upb_strview));
  595. break;
  596. case OP_SCALAR_LG2(3):
  597. memcpy(mem, &val, 8);
  598. break;
  599. case OP_SCALAR_LG2(2):
  600. memcpy(mem, &val, 4);
  601. break;
  602. case OP_SCALAR_LG2(0):
  603. memcpy(mem, &val, 1);
  604. break;
  605. default:
  606. UPB_UNREACHABLE();
  607. }
  608. return ptr;
  609. }
  610. static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
  611. const upb_msglayout *layout) {
  612. while (ptr < d->limit) {
  613. uint32_t tag;
  614. const upb_msglayout_field *field;
  615. int field_number;
  616. int wire_type;
  617. const char *field_start = ptr;
  618. wireval val;
  619. int op;
  620. ptr = decode_varint32(d, ptr, d->limit, &tag);
  621. field_number = tag >> 3;
  622. wire_type = tag & 7;
  623. field = upb_find_field(layout, field_number);
  624. switch (wire_type) {
  625. case UPB_WIRE_TYPE_VARINT:
  626. ptr = decode_varint64(d, ptr, d->limit, &val.uint64_val);
  627. op = varint_ops[field->descriptortype];
  628. decode_munge(field->descriptortype, &val);
  629. break;
  630. case UPB_WIRE_TYPE_32BIT:
  631. if (d->limit - ptr < 4) decode_err(d);
  632. memcpy(&val, ptr, 4);
  633. ptr += 4;
  634. op = OP_SCALAR_LG2(2);
  635. if (((1 << field->descriptortype) & fixed32_ok) == 0) goto unknown;
  636. break;
  637. case UPB_WIRE_TYPE_64BIT:
  638. if (d->limit - ptr < 8) decode_err(d);
  639. memcpy(&val, ptr, 8);
  640. ptr += 8;
  641. op = OP_SCALAR_LG2(3);
  642. if (((1 << field->descriptortype) & fixed64_ok) == 0) goto unknown;
  643. break;
  644. case UPB_WIRE_TYPE_DELIMITED: {
  645. uint32_t size;
  646. int ndx = field->descriptortype;
  647. if (_upb_isrepeated(field)) ndx += 18;
  648. ptr = decode_varint32(d, ptr, d->limit, &size);
  649. if (size >= INT32_MAX || (size_t)(d->limit - ptr) < size) {
  650. decode_err(d); /* Length overflow. */
  651. }
  652. val.str_val.data = ptr;
  653. val.str_val.size = size;
  654. ptr += size;
  655. op = delim_ops[ndx];
  656. break;
  657. }
  658. case UPB_WIRE_TYPE_START_GROUP:
  659. val.int32_val = field_number;
  660. op = OP_SUBMSG;
  661. if (field->descriptortype != UPB_DTYPE_GROUP) goto unknown;
  662. break;
  663. case UPB_WIRE_TYPE_END_GROUP:
  664. d->end_group = field_number;
  665. return ptr;
  666. default:
  667. decode_err(d);
  668. }
  669. if (op >= 0) {
  670. /* Parse, using op for dispatch. */
  671. switch (field->label) {
  672. case UPB_LABEL_REPEATED:
  673. case _UPB_LABEL_PACKED:
  674. ptr = decode_toarray(d, ptr, msg, layout, field, val, op);
  675. break;
  676. case _UPB_LABEL_MAP:
  677. decode_tomap(d, msg, layout, field, val);
  678. break;
  679. default:
  680. ptr = decode_tomsg(d, ptr, msg, layout, field, val, op);
  681. break;
  682. }
  683. } else {
  684. unknown:
  685. /* Skip unknown field. */
  686. if (field_number == 0) decode_err(d);
  687. if (wire_type == UPB_WIRE_TYPE_START_GROUP) {
  688. ptr = decode_group(d, ptr, NULL, NULL, field_number);
  689. }
  690. if (msg) {
  691. if (!_upb_msg_addunknown(msg, field_start, ptr - field_start,
  692. d->arena)) {
  693. decode_err(d);
  694. }
  695. }
  696. }
  697. }
  698. if (ptr != d->limit) decode_err(d);
  699. return ptr;
  700. }
  701. bool upb_decode(const char *buf, size_t size, void *msg, const upb_msglayout *l,
  702. upb_arena *arena) {
  703. upb_decstate state;
  704. state.limit = buf + size;
  705. state.arena = arena;
  706. state.depth = 64;
  707. state.end_group = 0;
  708. if (setjmp(state.err)) return false;
  709. if (size == 0) return true;
  710. decode_msg(&state, buf, msg, l);
  711. return state.end_group == 0;
  712. }
  713. #undef OP_SCALAR_LG2
  714. #undef OP_FIXPCK_LG2
  715. #undef OP_VARPCK_LG2
  716. #undef OP_STRING
  717. #undef OP_SUBMSG
  718. /* We encode backwards, to avoid pre-computing lengths (one-pass encode). */
  719. #include <string.h>
  720. #define UPB_PB_VARINT_MAX_LEN 10
  721. #define CHK(x) do { if (!(x)) { return false; } } while(0)
  722. static size_t upb_encode_varint(uint64_t val, char *buf) {
  723. size_t i;
  724. if (val < 128) { buf[0] = val; return 1; }
  725. i = 0;
  726. while (val) {
  727. uint8_t byte = val & 0x7fU;
  728. val >>= 7;
  729. if (val) byte |= 0x80U;
  730. buf[i++] = byte;
  731. }
  732. return i;
  733. }
  734. static uint32_t upb_zzencode_32(int32_t n) { return ((uint32_t)n << 1) ^ (n >> 31); }
  735. static uint64_t upb_zzencode_64(int64_t n) { return ((uint64_t)n << 1) ^ (n >> 63); }
  736. typedef struct {
  737. upb_alloc *alloc;
  738. char *buf, *ptr, *limit;
  739. } upb_encstate;
  740. static size_t upb_roundup_pow2(size_t bytes) {
  741. size_t ret = 128;
  742. while (ret < bytes) {
  743. ret *= 2;
  744. }
  745. return ret;
  746. }
  747. static bool upb_encode_growbuffer(upb_encstate *e, size_t bytes) {
  748. size_t old_size = e->limit - e->buf;
  749. size_t new_size = upb_roundup_pow2(bytes + (e->limit - e->ptr));
  750. char *new_buf = upb_realloc(e->alloc, e->buf, old_size, new_size);
  751. CHK(new_buf);
  752. /* We want previous data at the end, realloc() put it at the beginning. */
  753. if (old_size > 0) {
  754. memmove(new_buf + new_size - old_size, e->buf, old_size);
  755. }
  756. e->ptr = new_buf + new_size - (e->limit - e->ptr);
  757. e->limit = new_buf + new_size;
  758. e->buf = new_buf;
  759. return true;
  760. }
  761. /* Call to ensure that at least "bytes" bytes are available for writing at
  762. * e->ptr. Returns false if the bytes could not be allocated. */
  763. static bool upb_encode_reserve(upb_encstate *e, size_t bytes) {
  764. CHK(UPB_LIKELY((size_t)(e->ptr - e->buf) >= bytes) ||
  765. upb_encode_growbuffer(e, bytes));
  766. e->ptr -= bytes;
  767. return true;
  768. }
  769. /* Writes the given bytes to the buffer, handling reserve/advance. */
  770. static bool upb_put_bytes(upb_encstate *e, const void *data, size_t len) {
  771. if (len == 0) return true;
  772. CHK(upb_encode_reserve(e, len));
  773. memcpy(e->ptr, data, len);
  774. return true;
  775. }
  776. static bool upb_put_fixed64(upb_encstate *e, uint64_t val) {
  777. /* TODO(haberman): byte-swap for big endian. */
  778. return upb_put_bytes(e, &val, sizeof(uint64_t));
  779. }
  780. static bool upb_put_fixed32(upb_encstate *e, uint32_t val) {
  781. /* TODO(haberman): byte-swap for big endian. */
  782. return upb_put_bytes(e, &val, sizeof(uint32_t));
  783. }
  784. static bool upb_put_varint(upb_encstate *e, uint64_t val) {
  785. size_t len;
  786. char *start;
  787. CHK(upb_encode_reserve(e, UPB_PB_VARINT_MAX_LEN));
  788. len = upb_encode_varint(val, e->ptr);
  789. start = e->ptr + UPB_PB_VARINT_MAX_LEN - len;
  790. memmove(start, e->ptr, len);
  791. e->ptr = start;
  792. return true;
  793. }
  794. static bool upb_put_double(upb_encstate *e, double d) {
  795. uint64_t u64;
  796. UPB_ASSERT(sizeof(double) == sizeof(uint64_t));
  797. memcpy(&u64, &d, sizeof(uint64_t));
  798. return upb_put_fixed64(e, u64);
  799. }
  800. static bool upb_put_float(upb_encstate *e, float d) {
  801. uint32_t u32;
  802. UPB_ASSERT(sizeof(float) == sizeof(uint32_t));
  803. memcpy(&u32, &d, sizeof(uint32_t));
  804. return upb_put_fixed32(e, u32);
  805. }
  806. static bool upb_put_tag(upb_encstate *e, int field_number, int wire_type) {
  807. return upb_put_varint(e, (field_number << 3) | wire_type);
  808. }
  809. static bool upb_put_fixedarray(upb_encstate *e, const upb_array *arr,
  810. size_t elem_size, uint32_t tag) {
  811. size_t bytes = arr->len * elem_size;
  812. const char* data = _upb_array_constptr(arr);
  813. const char* ptr = data + bytes - elem_size;
  814. if (tag) {
  815. while (true) {
  816. CHK(upb_put_bytes(e, ptr, elem_size) && upb_put_varint(e, tag));
  817. if (ptr == data) break;
  818. ptr -= elem_size;
  819. }
  820. return true;
  821. } else {
  822. return upb_put_bytes(e, data, bytes) && upb_put_varint(e, bytes);
  823. }
  824. }
  825. bool upb_encode_message(upb_encstate *e, const char *msg,
  826. const upb_msglayout *m, size_t *size);
  827. static bool upb_encode_scalarfield(upb_encstate *e, const void *_field_mem,
  828. const upb_msglayout *m,
  829. const upb_msglayout_field *f,
  830. bool skip_zero_value) {
  831. const char *field_mem = _field_mem;
  832. #define CASE(ctype, type, wire_type, encodeval) do { \
  833. ctype val = *(ctype*)field_mem; \
  834. if (skip_zero_value && val == 0) { \
  835. return true; \
  836. } \
  837. return upb_put_ ## type(e, encodeval) && \
  838. upb_put_tag(e, f->number, wire_type); \
  839. } while(0)
  840. switch (f->descriptortype) {
  841. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  842. CASE(double, double, UPB_WIRE_TYPE_64BIT, val);
  843. case UPB_DESCRIPTOR_TYPE_FLOAT:
  844. CASE(float, float, UPB_WIRE_TYPE_32BIT, val);
  845. case UPB_DESCRIPTOR_TYPE_INT64:
  846. case UPB_DESCRIPTOR_TYPE_UINT64:
  847. CASE(uint64_t, varint, UPB_WIRE_TYPE_VARINT, val);
  848. case UPB_DESCRIPTOR_TYPE_UINT32:
  849. CASE(uint32_t, varint, UPB_WIRE_TYPE_VARINT, val);
  850. case UPB_DESCRIPTOR_TYPE_INT32:
  851. case UPB_DESCRIPTOR_TYPE_ENUM:
  852. CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, (int64_t)val);
  853. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  854. case UPB_DESCRIPTOR_TYPE_FIXED64:
  855. CASE(uint64_t, fixed64, UPB_WIRE_TYPE_64BIT, val);
  856. case UPB_DESCRIPTOR_TYPE_FIXED32:
  857. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  858. CASE(uint32_t, fixed32, UPB_WIRE_TYPE_32BIT, val);
  859. case UPB_DESCRIPTOR_TYPE_BOOL:
  860. CASE(bool, varint, UPB_WIRE_TYPE_VARINT, val);
  861. case UPB_DESCRIPTOR_TYPE_SINT32:
  862. CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_32(val));
  863. case UPB_DESCRIPTOR_TYPE_SINT64:
  864. CASE(int64_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_64(val));
  865. case UPB_DESCRIPTOR_TYPE_STRING:
  866. case UPB_DESCRIPTOR_TYPE_BYTES: {
  867. upb_strview view = *(upb_strview*)field_mem;
  868. if (skip_zero_value && view.size == 0) {
  869. return true;
  870. }
  871. return upb_put_bytes(e, view.data, view.size) &&
  872. upb_put_varint(e, view.size) &&
  873. upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
  874. }
  875. case UPB_DESCRIPTOR_TYPE_GROUP: {
  876. size_t size;
  877. void *submsg = *(void **)field_mem;
  878. const upb_msglayout *subm = m->submsgs[f->submsg_index];
  879. if (submsg == NULL) {
  880. return true;
  881. }
  882. return upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
  883. upb_encode_message(e, submsg, subm, &size) &&
  884. upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP);
  885. }
  886. case UPB_DESCRIPTOR_TYPE_MESSAGE: {
  887. size_t size;
  888. void *submsg = *(void **)field_mem;
  889. const upb_msglayout *subm = m->submsgs[f->submsg_index];
  890. if (submsg == NULL) {
  891. return true;
  892. }
  893. return upb_encode_message(e, submsg, subm, &size) &&
  894. upb_put_varint(e, size) &&
  895. upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
  896. }
  897. }
  898. #undef CASE
  899. UPB_UNREACHABLE();
  900. }
  901. static bool upb_encode_array(upb_encstate *e, const char *field_mem,
  902. const upb_msglayout *m,
  903. const upb_msglayout_field *f) {
  904. const upb_array *arr = *(const upb_array**)field_mem;
  905. bool packed = f->label == _UPB_LABEL_PACKED;
  906. if (arr == NULL || arr->len == 0) {
  907. return true;
  908. }
  909. #define VARINT_CASE(ctype, encode) \
  910. { \
  911. const ctype *start = _upb_array_constptr(arr); \
  912. const ctype *ptr = start + arr->len; \
  913. size_t pre_len = e->limit - e->ptr; \
  914. uint32_t tag = packed ? 0 : (f->number << 3) | UPB_WIRE_TYPE_VARINT; \
  915. do { \
  916. ptr--; \
  917. CHK(upb_put_varint(e, encode)); \
  918. if (tag) CHK(upb_put_varint(e, tag)); \
  919. } while (ptr != start); \
  920. if (!tag) CHK(upb_put_varint(e, e->limit - e->ptr - pre_len)); \
  921. } \
  922. break; \
  923. do { \
  924. ; \
  925. } while (0)
  926. #define TAG(wire_type) (packed ? 0 : (f->number << 3 | wire_type))
  927. switch (f->descriptortype) {
  928. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  929. CHK(upb_put_fixedarray(e, arr, sizeof(double), TAG(UPB_WIRE_TYPE_64BIT)));
  930. break;
  931. case UPB_DESCRIPTOR_TYPE_FLOAT:
  932. CHK(upb_put_fixedarray(e, arr, sizeof(float), TAG(UPB_WIRE_TYPE_32BIT)));
  933. break;
  934. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  935. case UPB_DESCRIPTOR_TYPE_FIXED64:
  936. CHK(upb_put_fixedarray(e, arr, sizeof(uint64_t), TAG(UPB_WIRE_TYPE_64BIT)));
  937. break;
  938. case UPB_DESCRIPTOR_TYPE_FIXED32:
  939. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  940. CHK(upb_put_fixedarray(e, arr, sizeof(uint32_t), TAG(UPB_WIRE_TYPE_32BIT)));
  941. break;
  942. case UPB_DESCRIPTOR_TYPE_INT64:
  943. case UPB_DESCRIPTOR_TYPE_UINT64:
  944. VARINT_CASE(uint64_t, *ptr);
  945. case UPB_DESCRIPTOR_TYPE_UINT32:
  946. VARINT_CASE(uint32_t, *ptr);
  947. case UPB_DESCRIPTOR_TYPE_INT32:
  948. case UPB_DESCRIPTOR_TYPE_ENUM:
  949. VARINT_CASE(int32_t, (int64_t)*ptr);
  950. case UPB_DESCRIPTOR_TYPE_BOOL:
  951. VARINT_CASE(bool, *ptr);
  952. case UPB_DESCRIPTOR_TYPE_SINT32:
  953. VARINT_CASE(int32_t, upb_zzencode_32(*ptr));
  954. case UPB_DESCRIPTOR_TYPE_SINT64:
  955. VARINT_CASE(int64_t, upb_zzencode_64(*ptr));
  956. case UPB_DESCRIPTOR_TYPE_STRING:
  957. case UPB_DESCRIPTOR_TYPE_BYTES: {
  958. const upb_strview *start = _upb_array_constptr(arr);
  959. const upb_strview *ptr = start + arr->len;
  960. do {
  961. ptr--;
  962. CHK(upb_put_bytes(e, ptr->data, ptr->size) &&
  963. upb_put_varint(e, ptr->size) &&
  964. upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
  965. } while (ptr != start);
  966. return true;
  967. }
  968. case UPB_DESCRIPTOR_TYPE_GROUP: {
  969. const void *const*start = _upb_array_constptr(arr);
  970. const void *const*ptr = start + arr->len;
  971. const upb_msglayout *subm = m->submsgs[f->submsg_index];
  972. do {
  973. size_t size;
  974. ptr--;
  975. CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
  976. upb_encode_message(e, *ptr, subm, &size) &&
  977. upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP));
  978. } while (ptr != start);
  979. return true;
  980. }
  981. case UPB_DESCRIPTOR_TYPE_MESSAGE: {
  982. const void *const*start = _upb_array_constptr(arr);
  983. const void *const*ptr = start + arr->len;
  984. const upb_msglayout *subm = m->submsgs[f->submsg_index];
  985. do {
  986. size_t size;
  987. ptr--;
  988. CHK(upb_encode_message(e, *ptr, subm, &size) &&
  989. upb_put_varint(e, size) &&
  990. upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
  991. } while (ptr != start);
  992. return true;
  993. }
  994. }
  995. #undef VARINT_CASE
  996. if (packed) {
  997. CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
  998. }
  999. return true;
  1000. }
  1001. static bool upb_encode_map(upb_encstate *e, const char *field_mem,
  1002. const upb_msglayout *m,
  1003. const upb_msglayout_field *f) {
  1004. const upb_map *map = *(const upb_map**)field_mem;
  1005. const upb_msglayout *entry = m->submsgs[f->submsg_index];
  1006. const upb_msglayout_field *key_field = &entry->fields[0];
  1007. const upb_msglayout_field *val_field = &entry->fields[1];
  1008. upb_strtable_iter i;
  1009. if (map == NULL) {
  1010. return true;
  1011. }
  1012. upb_strtable_begin(&i, &map->table);
  1013. for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
  1014. size_t pre_len = e->limit - e->ptr;
  1015. size_t size;
  1016. upb_strview key = upb_strtable_iter_key(&i);
  1017. const upb_value val = upb_strtable_iter_value(&i);
  1018. upb_map_entry ent;
  1019. _upb_map_fromkey(key, &ent.k, map->key_size);
  1020. _upb_map_fromvalue(val, &ent.v, map->val_size);
  1021. CHK(upb_encode_scalarfield(e, &ent.v, entry, val_field, false));
  1022. CHK(upb_encode_scalarfield(e, &ent.k, entry, key_field, false));
  1023. size = (e->limit - e->ptr) - pre_len;
  1024. CHK(upb_put_varint(e, size));
  1025. CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
  1026. }
  1027. return true;
  1028. }
  1029. bool upb_encode_message(upb_encstate *e, const char *msg,
  1030. const upb_msglayout *m, size_t *size) {
  1031. int i;
  1032. size_t pre_len = e->limit - e->ptr;
  1033. const char *unknown;
  1034. size_t unknown_size;
  1035. unknown = upb_msg_getunknown(msg, &unknown_size);
  1036. if (unknown) {
  1037. upb_put_bytes(e, unknown, unknown_size);
  1038. }
  1039. for (i = m->field_count - 1; i >= 0; i--) {
  1040. const upb_msglayout_field *f = &m->fields[i];
  1041. if (_upb_isrepeated(f)) {
  1042. CHK(upb_encode_array(e, msg + f->offset, m, f));
  1043. } else if (f->label == _UPB_LABEL_MAP) {
  1044. CHK(upb_encode_map(e, msg + f->offset, m, f));
  1045. } else {
  1046. bool skip_empty = false;
  1047. if (f->presence == 0) {
  1048. /* Proto3 presence. */
  1049. skip_empty = true;
  1050. } else if (f->presence > 0) {
  1051. /* Proto2 presence: hasbit. */
  1052. if (!_upb_hasbit_field(msg, f)) {
  1053. continue;
  1054. }
  1055. } else {
  1056. /* Field is in a oneof. */
  1057. if (_upb_getoneofcase_field(msg, f) != f->number) {
  1058. continue;
  1059. }
  1060. }
  1061. CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, skip_empty));
  1062. }
  1063. }
  1064. *size = (e->limit - e->ptr) - pre_len;
  1065. return true;
  1066. }
  1067. char *upb_encode(const void *msg, const upb_msglayout *m, upb_arena *arena,
  1068. size_t *size) {
  1069. upb_encstate e;
  1070. e.alloc = upb_arena_alloc(arena);
  1071. e.buf = NULL;
  1072. e.limit = NULL;
  1073. e.ptr = NULL;
  1074. if (!upb_encode_message(&e, msg, m, size)) {
  1075. *size = 0;
  1076. return NULL;
  1077. }
  1078. *size = e.limit - e.ptr;
  1079. if (*size == 0) {
  1080. static char ch;
  1081. return &ch;
  1082. } else {
  1083. UPB_ASSERT(e.ptr);
  1084. return e.ptr;
  1085. }
  1086. }
  1087. #undef CHK
  1088. /** upb_msg *******************************************************************/
  1089. static const char _upb_fieldtype_to_sizelg2[12] = {
  1090. 0,
  1091. 0, /* UPB_TYPE_BOOL */
  1092. 2, /* UPB_TYPE_FLOAT */
  1093. 2, /* UPB_TYPE_INT32 */
  1094. 2, /* UPB_TYPE_UINT32 */
  1095. 2, /* UPB_TYPE_ENUM */
  1096. UPB_SIZE(2, 3), /* UPB_TYPE_MESSAGE */
  1097. 3, /* UPB_TYPE_DOUBLE */
  1098. 3, /* UPB_TYPE_INT64 */
  1099. 3, /* UPB_TYPE_UINT64 */
  1100. UPB_SIZE(3, 4), /* UPB_TYPE_STRING */
  1101. UPB_SIZE(3, 4), /* UPB_TYPE_BYTES */
  1102. };
  1103. static uintptr_t tag_arrptr(void* ptr, int elem_size_lg2) {
  1104. UPB_ASSERT(elem_size_lg2 <= 4);
  1105. return (uintptr_t)ptr | elem_size_lg2;
  1106. }
  1107. static int upb_msg_internalsize(const upb_msglayout *l) {
  1108. return sizeof(upb_msg_internal) - l->extendable * sizeof(void *);
  1109. }
  1110. static size_t upb_msg_sizeof(const upb_msglayout *l) {
  1111. return l->size + upb_msg_internalsize(l);
  1112. }
  1113. static const upb_msg_internal *upb_msg_getinternal_const(const upb_msg *msg) {
  1114. ptrdiff_t size = sizeof(upb_msg_internal);
  1115. return UPB_PTR_AT(msg, -size, upb_msg_internal);
  1116. }
  1117. static upb_msg_internal *upb_msg_getinternal(upb_msg *msg) {
  1118. return (upb_msg_internal*)upb_msg_getinternal_const(msg);
  1119. }
  1120. void _upb_msg_clear(upb_msg *msg, const upb_msglayout *l) {
  1121. ptrdiff_t internal = upb_msg_internalsize(l);
  1122. void *mem = UPB_PTR_AT(msg, -internal, char);
  1123. memset(mem, 0, l->size + internal);
  1124. }
  1125. upb_msg *_upb_msg_new(const upb_msglayout *l, upb_arena *a) {
  1126. void *mem = upb_arena_malloc(a, upb_msg_sizeof(l));
  1127. upb_msg *msg;
  1128. if (!mem) {
  1129. return NULL;
  1130. }
  1131. msg = UPB_PTR_AT(mem, upb_msg_internalsize(l), upb_msg);
  1132. _upb_msg_clear(msg, l);
  1133. return msg;
  1134. }
  1135. bool _upb_msg_addunknown(upb_msg *msg, const char *data, size_t len,
  1136. upb_arena *arena) {
  1137. upb_msg_internal *in = upb_msg_getinternal(msg);
  1138. if (len > in->unknown_size - in->unknown_len) {
  1139. upb_alloc *alloc = upb_arena_alloc(arena);
  1140. size_t need = in->unknown_size + len;
  1141. size_t newsize = UPB_MAX(in->unknown_size * 2, need);
  1142. void *mem = upb_realloc(alloc, in->unknown, in->unknown_size, newsize);
  1143. if (!mem) return false;
  1144. in->unknown = mem;
  1145. in->unknown_size = newsize;
  1146. }
  1147. memcpy(in->unknown + in->unknown_len, data, len);
  1148. in->unknown_len += len;
  1149. return true;
  1150. }
  1151. void _upb_msg_discardunknown_shallow(upb_msg *msg) {
  1152. upb_msg_internal *in = upb_msg_getinternal(msg);
  1153. in->unknown_len = 0;
  1154. }
  1155. const char *upb_msg_getunknown(const upb_msg *msg, size_t *len) {
  1156. const upb_msg_internal *in = upb_msg_getinternal_const(msg);
  1157. *len = in->unknown_len;
  1158. return in->unknown;
  1159. }
  1160. /** upb_array *****************************************************************/
  1161. upb_array *_upb_array_new(upb_arena *a, upb_fieldtype_t type) {
  1162. upb_array *arr = upb_arena_malloc(a, sizeof(upb_array));
  1163. if (!arr) {
  1164. return NULL;
  1165. }
  1166. arr->data = tag_arrptr(NULL, _upb_fieldtype_to_sizelg2[type]);
  1167. arr->len = 0;
  1168. arr->size = 0;
  1169. return arr;
  1170. }
  1171. bool _upb_array_realloc(upb_array *arr, size_t min_size, upb_arena *arena) {
  1172. size_t new_size = UPB_MAX(arr->size, 4);
  1173. int elem_size_lg2 = arr->data & 7;
  1174. size_t old_bytes = arr->size << elem_size_lg2;
  1175. size_t new_bytes;
  1176. void* ptr = _upb_array_ptr(arr);
  1177. /* Log2 ceiling of size. */
  1178. while (new_size < min_size) new_size *= 2;
  1179. new_bytes = new_size << elem_size_lg2;
  1180. ptr = upb_arena_realloc(arena, ptr, old_bytes, new_bytes);
  1181. if (!ptr) {
  1182. return false;
  1183. }
  1184. arr->data = tag_arrptr(ptr, elem_size_lg2);
  1185. arr->size = new_size;
  1186. return true;
  1187. }
  1188. static upb_array *getorcreate_array(upb_array **arr_ptr, upb_fieldtype_t type,
  1189. upb_arena *arena) {
  1190. upb_array *arr = *arr_ptr;
  1191. if (!arr) {
  1192. arr = _upb_array_new(arena, type);
  1193. if (!arr) return NULL;
  1194. *arr_ptr = arr;
  1195. }
  1196. return arr;
  1197. }
  1198. void *_upb_array_resize_fallback(upb_array **arr_ptr, size_t size,
  1199. upb_fieldtype_t type, upb_arena *arena) {
  1200. upb_array *arr = getorcreate_array(arr_ptr, type, arena);
  1201. return arr && _upb_array_resize(arr, size, arena) ? _upb_array_ptr(arr) : NULL;
  1202. }
  1203. bool _upb_array_append_fallback(upb_array **arr_ptr, const void *value,
  1204. upb_fieldtype_t type, upb_arena *arena) {
  1205. upb_array *arr = getorcreate_array(arr_ptr, type, arena);
  1206. size_t elem = arr->len;
  1207. int lg2 = _upb_fieldtype_to_sizelg2[type];
  1208. char *data;
  1209. if (!arr || !_upb_array_resize(arr, elem + 1, arena)) return false;
  1210. data = _upb_array_ptr(arr);
  1211. memcpy(data + (elem << lg2), value, 1 << lg2);
  1212. return true;
  1213. }
  1214. /** upb_map *******************************************************************/
  1215. upb_map *_upb_map_new(upb_arena *a, size_t key_size, size_t value_size) {
  1216. upb_map *map = upb_arena_malloc(a, sizeof(upb_map));
  1217. if (!map) {
  1218. return NULL;
  1219. }
  1220. upb_strtable_init2(&map->table, UPB_CTYPE_INT32, upb_arena_alloc(a));
  1221. map->key_size = key_size;
  1222. map->val_size = value_size;
  1223. return map;
  1224. }
  1225. /*
  1226. ** upb_table Implementation
  1227. **
  1228. ** Implementation is heavily inspired by Lua's ltable.c.
  1229. */
  1230. #include <string.h>
  1231. #define UPB_MAXARRSIZE 16 /* 64k. */
  1232. /* From Chromium. */
  1233. #define ARRAY_SIZE(x) \
  1234. ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
  1235. static const double MAX_LOAD = 0.85;
  1236. /* The minimum utilization of the array part of a mixed hash/array table. This
  1237. * is a speed/memory-usage tradeoff (though it's not straightforward because of
  1238. * cache effects). The lower this is, the more memory we'll use. */
  1239. static const double MIN_DENSITY = 0.1;
  1240. bool is_pow2(uint64_t v) { return v == 0 || (v & (v - 1)) == 0; }
  1241. int log2ceil(uint64_t v) {
  1242. int ret = 0;
  1243. bool pow2 = is_pow2(v);
  1244. while (v >>= 1) ret++;
  1245. ret = pow2 ? ret : ret + 1; /* Ceiling. */
  1246. return UPB_MIN(UPB_MAXARRSIZE, ret);
  1247. }
  1248. char *upb_strdup(const char *s, upb_alloc *a) {
  1249. return upb_strdup2(s, strlen(s), a);
  1250. }
  1251. char *upb_strdup2(const char *s, size_t len, upb_alloc *a) {
  1252. size_t n;
  1253. char *p;
  1254. /* Prevent overflow errors. */
  1255. if (len == SIZE_MAX) return NULL;
  1256. /* Always null-terminate, even if binary data; but don't rely on the input to
  1257. * have a null-terminating byte since it may be a raw binary buffer. */
  1258. n = len + 1;
  1259. p = upb_malloc(a, n);
  1260. if (p) {
  1261. memcpy(p, s, len);
  1262. p[len] = 0;
  1263. }
  1264. return p;
  1265. }
  1266. /* A type to represent the lookup key of either a strtable or an inttable. */
  1267. typedef union {
  1268. uintptr_t num;
  1269. struct {
  1270. const char *str;
  1271. size_t len;
  1272. } str;
  1273. } lookupkey_t;
  1274. static lookupkey_t strkey2(const char *str, size_t len) {
  1275. lookupkey_t k;
  1276. k.str.str = str;
  1277. k.str.len = len;
  1278. return k;
  1279. }
  1280. static lookupkey_t intkey(uintptr_t key) {
  1281. lookupkey_t k;
  1282. k.num = key;
  1283. return k;
  1284. }
  1285. typedef uint32_t hashfunc_t(upb_tabkey key);
  1286. typedef bool eqlfunc_t(upb_tabkey k1, lookupkey_t k2);
  1287. /* Base table (shared code) ***************************************************/
  1288. /* For when we need to cast away const. */
  1289. static upb_tabent *mutable_entries(upb_table *t) {
  1290. return (upb_tabent*)t->entries;
  1291. }
  1292. static bool isfull(upb_table *t) {
  1293. if (upb_table_size(t) == 0) {
  1294. return true;
  1295. } else {
  1296. return ((double)(t->count + 1) / upb_table_size(t)) > MAX_LOAD;
  1297. }
  1298. }
  1299. static bool init(upb_table *t, uint8_t size_lg2, upb_alloc *a) {
  1300. size_t bytes;
  1301. t->count = 0;
  1302. t->size_lg2 = size_lg2;
  1303. t->mask = upb_table_size(t) ? upb_table_size(t) - 1 : 0;
  1304. bytes = upb_table_size(t) * sizeof(upb_tabent);
  1305. if (bytes > 0) {
  1306. t->entries = upb_malloc(a, bytes);
  1307. if (!t->entries) return false;
  1308. memset(mutable_entries(t), 0, bytes);
  1309. } else {
  1310. t->entries = NULL;
  1311. }
  1312. return true;
  1313. }
  1314. static void uninit(upb_table *t, upb_alloc *a) {
  1315. upb_free(a, mutable_entries(t));
  1316. }
  1317. static upb_tabent *emptyent(upb_table *t) {
  1318. upb_tabent *e = mutable_entries(t) + upb_table_size(t);
  1319. while (1) { if (upb_tabent_isempty(--e)) return e; UPB_ASSERT(e > t->entries); }
  1320. }
  1321. static upb_tabent *getentry_mutable(upb_table *t, uint32_t hash) {
  1322. return (upb_tabent*)upb_getentry(t, hash);
  1323. }
  1324. static const upb_tabent *findentry(const upb_table *t, lookupkey_t key,
  1325. uint32_t hash, eqlfunc_t *eql) {
  1326. const upb_tabent *e;
  1327. if (t->size_lg2 == 0) return NULL;
  1328. e = upb_getentry(t, hash);
  1329. if (upb_tabent_isempty(e)) return NULL;
  1330. while (1) {
  1331. if (eql(e->key, key)) return e;
  1332. if ((e = e->next) == NULL) return NULL;
  1333. }
  1334. }
  1335. static upb_tabent *findentry_mutable(upb_table *t, lookupkey_t key,
  1336. uint32_t hash, eqlfunc_t *eql) {
  1337. return (upb_tabent*)findentry(t, key, hash, eql);
  1338. }
  1339. static bool lookup(const upb_table *t, lookupkey_t key, upb_value *v,
  1340. uint32_t hash, eqlfunc_t *eql) {
  1341. const upb_tabent *e = findentry(t, key, hash, eql);
  1342. if (e) {
  1343. if (v) {
  1344. _upb_value_setval(v, e->val.val);
  1345. }
  1346. return true;
  1347. } else {
  1348. return false;
  1349. }
  1350. }
  1351. /* The given key must not already exist in the table. */
  1352. static void insert(upb_table *t, lookupkey_t key, upb_tabkey tabkey,
  1353. upb_value val, uint32_t hash,
  1354. hashfunc_t *hashfunc, eqlfunc_t *eql) {
  1355. upb_tabent *mainpos_e;
  1356. upb_tabent *our_e;
  1357. UPB_ASSERT(findentry(t, key, hash, eql) == NULL);
  1358. t->count++;
  1359. mainpos_e = getentry_mutable(t, hash);
  1360. our_e = mainpos_e;
  1361. if (upb_tabent_isempty(mainpos_e)) {
  1362. /* Our main position is empty; use it. */
  1363. our_e->next = NULL;
  1364. } else {
  1365. /* Collision. */
  1366. upb_tabent *new_e = emptyent(t);
  1367. /* Head of collider's chain. */
  1368. upb_tabent *chain = getentry_mutable(t, hashfunc(mainpos_e->key));
  1369. if (chain == mainpos_e) {
  1370. /* Existing ent is in its main posisiton (it has the same hash as us, and
  1371. * is the head of our chain). Insert to new ent and append to this chain. */
  1372. new_e->next = mainpos_e->next;
  1373. mainpos_e->next = new_e;
  1374. our_e = new_e;
  1375. } else {
  1376. /* Existing ent is not in its main position (it is a node in some other
  1377. * chain). This implies that no existing ent in the table has our hash.
  1378. * Evict it (updating its chain) and use its ent for head of our chain. */
  1379. *new_e = *mainpos_e; /* copies next. */
  1380. while (chain->next != mainpos_e) {
  1381. chain = (upb_tabent*)chain->next;
  1382. UPB_ASSERT(chain);
  1383. }
  1384. chain->next = new_e;
  1385. our_e = mainpos_e;
  1386. our_e->next = NULL;
  1387. }
  1388. }
  1389. our_e->key = tabkey;
  1390. our_e->val.val = val.val;
  1391. UPB_ASSERT(findentry(t, key, hash, eql) == our_e);
  1392. }
  1393. static bool rm(upb_table *t, lookupkey_t key, upb_value *val,
  1394. upb_tabkey *removed, uint32_t hash, eqlfunc_t *eql) {
  1395. upb_tabent *chain = getentry_mutable(t, hash);
  1396. if (upb_tabent_isempty(chain)) return false;
  1397. if (eql(chain->key, key)) {
  1398. /* Element to remove is at the head of its chain. */
  1399. t->count--;
  1400. if (val) _upb_value_setval(val, chain->val.val);
  1401. if (removed) *removed = chain->key;
  1402. if (chain->next) {
  1403. upb_tabent *move = (upb_tabent*)chain->next;
  1404. *chain = *move;
  1405. move->key = 0; /* Make the slot empty. */
  1406. } else {
  1407. chain->key = 0; /* Make the slot empty. */
  1408. }
  1409. return true;
  1410. } else {
  1411. /* Element to remove is either in a non-head position or not in the
  1412. * table. */
  1413. while (chain->next && !eql(chain->next->key, key)) {
  1414. chain = (upb_tabent*)chain->next;
  1415. }
  1416. if (chain->next) {
  1417. /* Found element to remove. */
  1418. upb_tabent *rm = (upb_tabent*)chain->next;
  1419. t->count--;
  1420. if (val) _upb_value_setval(val, chain->next->val.val);
  1421. if (removed) *removed = rm->key;
  1422. rm->key = 0; /* Make the slot empty. */
  1423. chain->next = rm->next;
  1424. return true;
  1425. } else {
  1426. /* Element to remove is not in the table. */
  1427. return false;
  1428. }
  1429. }
  1430. }
  1431. static size_t next(const upb_table *t, size_t i) {
  1432. do {
  1433. if (++i >= upb_table_size(t))
  1434. return SIZE_MAX - 1; /* Distinct from -1. */
  1435. } while(upb_tabent_isempty(&t->entries[i]));
  1436. return i;
  1437. }
  1438. static size_t begin(const upb_table *t) {
  1439. return next(t, -1);
  1440. }
  1441. /* upb_strtable ***************************************************************/
  1442. /* A simple "subclass" of upb_table that only adds a hash function for strings. */
  1443. static upb_tabkey strcopy(lookupkey_t k2, upb_alloc *a) {
  1444. uint32_t len = (uint32_t) k2.str.len;
  1445. char *str = upb_malloc(a, k2.str.len + sizeof(uint32_t) + 1);
  1446. if (str == NULL) return 0;
  1447. memcpy(str, &len, sizeof(uint32_t));
  1448. if (k2.str.len) memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len);
  1449. str[sizeof(uint32_t) + k2.str.len] = '\0';
  1450. return (uintptr_t)str;
  1451. }
  1452. static uint32_t strhash(upb_tabkey key) {
  1453. uint32_t len;
  1454. char *str = upb_tabstr(key, &len);
  1455. return upb_murmur_hash2(str, len, 0);
  1456. }
  1457. static bool streql(upb_tabkey k1, lookupkey_t k2) {
  1458. uint32_t len;
  1459. char *str = upb_tabstr(k1, &len);
  1460. return len == k2.str.len && (len == 0 || memcmp(str, k2.str.str, len) == 0);
  1461. }
  1462. bool upb_strtable_init2(upb_strtable *t, upb_ctype_t ctype, upb_alloc *a) {
  1463. UPB_UNUSED(ctype); /* TODO(haberman): rm */
  1464. return init(&t->t, 2, a);
  1465. }
  1466. void upb_strtable_clear(upb_strtable *t) {
  1467. size_t bytes = upb_table_size(&t->t) * sizeof(upb_tabent);
  1468. t->t.count = 0;
  1469. memset((char*)t->t.entries, 0, bytes);
  1470. }
  1471. void upb_strtable_uninit2(upb_strtable *t, upb_alloc *a) {
  1472. size_t i;
  1473. for (i = 0; i < upb_table_size(&t->t); i++)
  1474. upb_free(a, (void*)t->t.entries[i].key);
  1475. uninit(&t->t, a);
  1476. }
  1477. bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_alloc *a) {
  1478. upb_strtable new_table;
  1479. upb_strtable_iter i;
  1480. if (!init(&new_table.t, size_lg2, a))
  1481. return false;
  1482. upb_strtable_begin(&i, t);
  1483. for ( ; !upb_strtable_done(&i); upb_strtable_next(&i)) {
  1484. upb_strview key = upb_strtable_iter_key(&i);
  1485. upb_strtable_insert3(
  1486. &new_table, key.data, key.size,
  1487. upb_strtable_iter_value(&i), a);
  1488. }
  1489. upb_strtable_uninit2(t, a);
  1490. *t = new_table;
  1491. return true;
  1492. }
  1493. bool upb_strtable_insert3(upb_strtable *t, const char *k, size_t len,
  1494. upb_value v, upb_alloc *a) {
  1495. lookupkey_t key;
  1496. upb_tabkey tabkey;
  1497. uint32_t hash;
  1498. if (isfull(&t->t)) {
  1499. /* Need to resize. New table of double the size, add old elements to it. */
  1500. if (!upb_strtable_resize(t, t->t.size_lg2 + 1, a)) {
  1501. return false;
  1502. }
  1503. }
  1504. key = strkey2(k, len);
  1505. tabkey = strcopy(key, a);
  1506. if (tabkey == 0) return false;
  1507. hash = upb_murmur_hash2(key.str.str, key.str.len, 0);
  1508. insert(&t->t, key, tabkey, v, hash, &strhash, &streql);
  1509. return true;
  1510. }
  1511. bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
  1512. upb_value *v) {
  1513. uint32_t hash = upb_murmur_hash2(key, len, 0);
  1514. return lookup(&t->t, strkey2(key, len), v, hash, &streql);
  1515. }
  1516. bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len,
  1517. upb_value *val, upb_alloc *alloc) {
  1518. uint32_t hash = upb_murmur_hash2(key, len, 0);
  1519. upb_tabkey tabkey;
  1520. if (rm(&t->t, strkey2(key, len), val, &tabkey, hash, &streql)) {
  1521. if (alloc) {
  1522. /* Arena-based allocs don't need to free and won't pass this. */
  1523. upb_free(alloc, (void*)tabkey);
  1524. }
  1525. return true;
  1526. } else {
  1527. return false;
  1528. }
  1529. }
  1530. /* Iteration */
  1531. void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t) {
  1532. i->t = t;
  1533. i->index = begin(&t->t);
  1534. }
  1535. void upb_strtable_next(upb_strtable_iter *i) {
  1536. i->index = next(&i->t->t, i->index);
  1537. }
  1538. bool upb_strtable_done(const upb_strtable_iter *i) {
  1539. if (!i->t) return true;
  1540. return i->index >= upb_table_size(&i->t->t) ||
  1541. upb_tabent_isempty(str_tabent(i));
  1542. }
  1543. upb_strview upb_strtable_iter_key(const upb_strtable_iter *i) {
  1544. upb_strview key;
  1545. uint32_t len;
  1546. UPB_ASSERT(!upb_strtable_done(i));
  1547. key.data = upb_tabstr(str_tabent(i)->key, &len);
  1548. key.size = len;
  1549. return key;
  1550. }
  1551. upb_value upb_strtable_iter_value(const upb_strtable_iter *i) {
  1552. UPB_ASSERT(!upb_strtable_done(i));
  1553. return _upb_value_val(str_tabent(i)->val.val);
  1554. }
  1555. void upb_strtable_iter_setdone(upb_strtable_iter *i) {
  1556. i->t = NULL;
  1557. i->index = SIZE_MAX;
  1558. }
  1559. bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
  1560. const upb_strtable_iter *i2) {
  1561. if (upb_strtable_done(i1) && upb_strtable_done(i2))
  1562. return true;
  1563. return i1->t == i2->t && i1->index == i2->index;
  1564. }
  1565. /* upb_inttable ***************************************************************/
  1566. /* For inttables we use a hybrid structure where small keys are kept in an
  1567. * array and large keys are put in the hash table. */
  1568. static uint32_t inthash(upb_tabkey key) { return upb_inthash(key); }
  1569. static bool inteql(upb_tabkey k1, lookupkey_t k2) {
  1570. return k1 == k2.num;
  1571. }
  1572. static upb_tabval *mutable_array(upb_inttable *t) {
  1573. return (upb_tabval*)t->array;
  1574. }
  1575. static upb_tabval *inttable_val(upb_inttable *t, uintptr_t key) {
  1576. if (key < t->array_size) {
  1577. return upb_arrhas(t->array[key]) ? &(mutable_array(t)[key]) : NULL;
  1578. } else {
  1579. upb_tabent *e =
  1580. findentry_mutable(&t->t, intkey(key), upb_inthash(key), &inteql);
  1581. return e ? &e->val : NULL;
  1582. }
  1583. }
  1584. static const upb_tabval *inttable_val_const(const upb_inttable *t,
  1585. uintptr_t key) {
  1586. return inttable_val((upb_inttable*)t, key);
  1587. }
  1588. size_t upb_inttable_count(const upb_inttable *t) {
  1589. return t->t.count + t->array_count;
  1590. }
  1591. static void check(upb_inttable *t) {
  1592. UPB_UNUSED(t);
  1593. #if defined(UPB_DEBUG_TABLE) && !defined(NDEBUG)
  1594. {
  1595. /* This check is very expensive (makes inserts/deletes O(N)). */
  1596. size_t count = 0;
  1597. upb_inttable_iter i;
  1598. upb_inttable_begin(&i, t);
  1599. for(; !upb_inttable_done(&i); upb_inttable_next(&i), count++) {
  1600. UPB_ASSERT(upb_inttable_lookup(t, upb_inttable_iter_key(&i), NULL));
  1601. }
  1602. UPB_ASSERT(count == upb_inttable_count(t));
  1603. }
  1604. #endif
  1605. }
  1606. bool upb_inttable_sizedinit(upb_inttable *t, size_t asize, int hsize_lg2,
  1607. upb_alloc *a) {
  1608. size_t array_bytes;
  1609. if (!init(&t->t, hsize_lg2, a)) return false;
  1610. /* Always make the array part at least 1 long, so that we know key 0
  1611. * won't be in the hash part, which simplifies things. */
  1612. t->array_size = UPB_MAX(1, asize);
  1613. t->array_count = 0;
  1614. array_bytes = t->array_size * sizeof(upb_value);
  1615. t->array = upb_malloc(a, array_bytes);
  1616. if (!t->array) {
  1617. uninit(&t->t, a);
  1618. return false;
  1619. }
  1620. memset(mutable_array(t), 0xff, array_bytes);
  1621. check(t);
  1622. return true;
  1623. }
  1624. bool upb_inttable_init2(upb_inttable *t, upb_ctype_t ctype, upb_alloc *a) {
  1625. UPB_UNUSED(ctype); /* TODO(haberman): rm */
  1626. return upb_inttable_sizedinit(t, 0, 4, a);
  1627. }
  1628. void upb_inttable_uninit2(upb_inttable *t, upb_alloc *a) {
  1629. uninit(&t->t, a);
  1630. upb_free(a, mutable_array(t));
  1631. }
  1632. bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val,
  1633. upb_alloc *a) {
  1634. upb_tabval tabval;
  1635. tabval.val = val.val;
  1636. UPB_ASSERT(upb_arrhas(tabval)); /* This will reject (uint64_t)-1. Fix this. */
  1637. if (key < t->array_size) {
  1638. UPB_ASSERT(!upb_arrhas(t->array[key]));
  1639. t->array_count++;
  1640. mutable_array(t)[key].val = val.val;
  1641. } else {
  1642. if (isfull(&t->t)) {
  1643. /* Need to resize the hash part, but we re-use the array part. */
  1644. size_t i;
  1645. upb_table new_table;
  1646. if (!init(&new_table, t->t.size_lg2 + 1, a)) {
  1647. return false;
  1648. }
  1649. for (i = begin(&t->t); i < upb_table_size(&t->t); i = next(&t->t, i)) {
  1650. const upb_tabent *e = &t->t.entries[i];
  1651. uint32_t hash;
  1652. upb_value v;
  1653. _upb_value_setval(&v, e->val.val);
  1654. hash = upb_inthash(e->key);
  1655. insert(&new_table, intkey(e->key), e->key, v, hash, &inthash, &inteql);
  1656. }
  1657. UPB_ASSERT(t->t.count == new_table.count);
  1658. uninit(&t->t, a);
  1659. t->t = new_table;
  1660. }
  1661. insert(&t->t, intkey(key), key, val, upb_inthash(key), &inthash, &inteql);
  1662. }
  1663. check(t);
  1664. return true;
  1665. }
  1666. bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v) {
  1667. const upb_tabval *table_v = inttable_val_const(t, key);
  1668. if (!table_v) return false;
  1669. if (v) _upb_value_setval(v, table_v->val);
  1670. return true;
  1671. }
  1672. bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val) {
  1673. upb_tabval *table_v = inttable_val(t, key);
  1674. if (!table_v) return false;
  1675. table_v->val = val.val;
  1676. return true;
  1677. }
  1678. bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val) {
  1679. bool success;
  1680. if (key < t->array_size) {
  1681. if (upb_arrhas(t->array[key])) {
  1682. upb_tabval empty = UPB_TABVALUE_EMPTY_INIT;
  1683. t->array_count--;
  1684. if (val) {
  1685. _upb_value_setval(val, t->array[key].val);
  1686. }
  1687. mutable_array(t)[key] = empty;
  1688. success = true;
  1689. } else {
  1690. success = false;
  1691. }
  1692. } else {
  1693. success = rm(&t->t, intkey(key), val, NULL, upb_inthash(key), &inteql);
  1694. }
  1695. check(t);
  1696. return success;
  1697. }
  1698. bool upb_inttable_insertptr2(upb_inttable *t, const void *key, upb_value val,
  1699. upb_alloc *a) {
  1700. return upb_inttable_insert2(t, (uintptr_t)key, val, a);
  1701. }
  1702. bool upb_inttable_lookupptr(const upb_inttable *t, const void *key,
  1703. upb_value *v) {
  1704. return upb_inttable_lookup(t, (uintptr_t)key, v);
  1705. }
  1706. bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val) {
  1707. return upb_inttable_remove(t, (uintptr_t)key, val);
  1708. }
  1709. void upb_inttable_compact2(upb_inttable *t, upb_alloc *a) {
  1710. /* A power-of-two histogram of the table keys. */
  1711. size_t counts[UPB_MAXARRSIZE + 1] = {0};
  1712. /* The max key in each bucket. */
  1713. uintptr_t max[UPB_MAXARRSIZE + 1] = {0};
  1714. upb_inttable_iter i;
  1715. size_t arr_count;
  1716. int size_lg2;
  1717. upb_inttable new_t;
  1718. upb_inttable_begin(&i, t);
  1719. for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  1720. uintptr_t key = upb_inttable_iter_key(&i);
  1721. int bucket = log2ceil(key);
  1722. max[bucket] = UPB_MAX(max[bucket], key);
  1723. counts[bucket]++;
  1724. }
  1725. /* Find the largest power of two that satisfies the MIN_DENSITY
  1726. * definition (while actually having some keys). */
  1727. arr_count = upb_inttable_count(t);
  1728. for (size_lg2 = ARRAY_SIZE(counts) - 1; size_lg2 > 0; size_lg2--) {
  1729. if (counts[size_lg2] == 0) {
  1730. /* We can halve again without losing any entries. */
  1731. continue;
  1732. } else if (arr_count >= (1 << size_lg2) * MIN_DENSITY) {
  1733. break;
  1734. }
  1735. arr_count -= counts[size_lg2];
  1736. }
  1737. UPB_ASSERT(arr_count <= upb_inttable_count(t));
  1738. {
  1739. /* Insert all elements into new, perfectly-sized table. */
  1740. size_t arr_size = max[size_lg2] + 1; /* +1 so arr[max] will fit. */
  1741. size_t hash_count = upb_inttable_count(t) - arr_count;
  1742. size_t hash_size = hash_count ? (hash_count / MAX_LOAD) + 1 : 0;
  1743. int hashsize_lg2 = log2ceil(hash_size);
  1744. upb_inttable_sizedinit(&new_t, arr_size, hashsize_lg2, a);
  1745. upb_inttable_begin(&i, t);
  1746. for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  1747. uintptr_t k = upb_inttable_iter_key(&i);
  1748. upb_inttable_insert2(&new_t, k, upb_inttable_iter_value(&i), a);
  1749. }
  1750. UPB_ASSERT(new_t.array_size == arr_size);
  1751. UPB_ASSERT(new_t.t.size_lg2 == hashsize_lg2);
  1752. }
  1753. upb_inttable_uninit2(t, a);
  1754. *t = new_t;
  1755. }
  1756. /* Iteration. */
  1757. static const upb_tabent *int_tabent(const upb_inttable_iter *i) {
  1758. UPB_ASSERT(!i->array_part);
  1759. return &i->t->t.entries[i->index];
  1760. }
  1761. static upb_tabval int_arrent(const upb_inttable_iter *i) {
  1762. UPB_ASSERT(i->array_part);
  1763. return i->t->array[i->index];
  1764. }
  1765. void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t) {
  1766. i->t = t;
  1767. i->index = -1;
  1768. i->array_part = true;
  1769. upb_inttable_next(i);
  1770. }
  1771. void upb_inttable_next(upb_inttable_iter *iter) {
  1772. const upb_inttable *t = iter->t;
  1773. if (iter->array_part) {
  1774. while (++iter->index < t->array_size) {
  1775. if (upb_arrhas(int_arrent(iter))) {
  1776. return;
  1777. }
  1778. }
  1779. iter->array_part = false;
  1780. iter->index = begin(&t->t);
  1781. } else {
  1782. iter->index = next(&t->t, iter->index);
  1783. }
  1784. }
  1785. bool upb_inttable_done(const upb_inttable_iter *i) {
  1786. if (!i->t) return true;
  1787. if (i->array_part) {
  1788. return i->index >= i->t->array_size ||
  1789. !upb_arrhas(int_arrent(i));
  1790. } else {
  1791. return i->index >= upb_table_size(&i->t->t) ||
  1792. upb_tabent_isempty(int_tabent(i));
  1793. }
  1794. }
  1795. uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i) {
  1796. UPB_ASSERT(!upb_inttable_done(i));
  1797. return i->array_part ? i->index : int_tabent(i)->key;
  1798. }
  1799. upb_value upb_inttable_iter_value(const upb_inttable_iter *i) {
  1800. UPB_ASSERT(!upb_inttable_done(i));
  1801. return _upb_value_val(
  1802. i->array_part ? i->t->array[i->index].val : int_tabent(i)->val.val);
  1803. }
  1804. void upb_inttable_iter_setdone(upb_inttable_iter *i) {
  1805. i->t = NULL;
  1806. i->index = SIZE_MAX;
  1807. i->array_part = false;
  1808. }
  1809. bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
  1810. const upb_inttable_iter *i2) {
  1811. if (upb_inttable_done(i1) && upb_inttable_done(i2))
  1812. return true;
  1813. return i1->t == i2->t && i1->index == i2->index &&
  1814. i1->array_part == i2->array_part;
  1815. }
  1816. #if defined(UPB_UNALIGNED_READS_OK) || defined(__s390x__)
  1817. /* -----------------------------------------------------------------------------
  1818. * MurmurHash2, by Austin Appleby (released as public domain).
  1819. * Reformatted and C99-ified by Joshua Haberman.
  1820. * Note - This code makes a few assumptions about how your machine behaves -
  1821. * 1. We can read a 4-byte value from any address without crashing
  1822. * 2. sizeof(int) == 4 (in upb this limitation is removed by using uint32_t
  1823. * And it has a few limitations -
  1824. * 1. It will not work incrementally.
  1825. * 2. It will not produce the same results on little-endian and big-endian
  1826. * machines. */
  1827. uint32_t upb_murmur_hash2(const void *key, size_t len, uint32_t seed) {
  1828. /* 'm' and 'r' are mixing constants generated offline.
  1829. * They're not really 'magic', they just happen to work well. */
  1830. const uint32_t m = 0x5bd1e995;
  1831. const int32_t r = 24;
  1832. /* Initialize the hash to a 'random' value */
  1833. uint32_t h = seed ^ len;
  1834. /* Mix 4 bytes at a time into the hash */
  1835. const uint8_t * data = (const uint8_t *)key;
  1836. while(len >= 4) {
  1837. uint32_t k;
  1838. memcpy(&k, data, sizeof(k));
  1839. k *= m;
  1840. k ^= k >> r;
  1841. k *= m;
  1842. h *= m;
  1843. h ^= k;
  1844. data += 4;
  1845. len -= 4;
  1846. }
  1847. /* Handle the last few bytes of the input array */
  1848. switch(len) {
  1849. case 3: h ^= data[2] << 16;
  1850. case 2: h ^= data[1] << 8;
  1851. case 1: h ^= data[0]; h *= m;
  1852. };
  1853. /* Do a few final mixes of the hash to ensure the last few
  1854. * bytes are well-incorporated. */
  1855. h ^= h >> 13;
  1856. h *= m;
  1857. h ^= h >> 15;
  1858. return h;
  1859. }
  1860. #else /* !UPB_UNALIGNED_READS_OK */
  1861. /* -----------------------------------------------------------------------------
  1862. * MurmurHashAligned2, by Austin Appleby
  1863. * Same algorithm as MurmurHash2, but only does aligned reads - should be safer
  1864. * on certain platforms.
  1865. * Performance will be lower than MurmurHash2 */
  1866. #define MIX(h,k,m) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }
  1867. uint32_t upb_murmur_hash2(const void * key, size_t len, uint32_t seed) {
  1868. const uint32_t m = 0x5bd1e995;
  1869. const int32_t r = 24;
  1870. const uint8_t * data = (const uint8_t *)key;
  1871. uint32_t h = (uint32_t)(seed ^ len);
  1872. uint8_t align = (uintptr_t)data & 3;
  1873. if(align && (len >= 4)) {
  1874. /* Pre-load the temp registers */
  1875. uint32_t t = 0, d = 0;
  1876. int32_t sl;
  1877. int32_t sr;
  1878. switch(align) {
  1879. case 1: t |= data[2] << 16; /* fallthrough */
  1880. case 2: t |= data[1] << 8; /* fallthrough */
  1881. case 3: t |= data[0];
  1882. }
  1883. t <<= (8 * align);
  1884. data += 4-align;
  1885. len -= 4-align;
  1886. sl = 8 * (4-align);
  1887. sr = 8 * align;
  1888. /* Mix */
  1889. while(len >= 4) {
  1890. uint32_t k;
  1891. d = *(uint32_t *)data;
  1892. t = (t >> sr) | (d << sl);
  1893. k = t;
  1894. MIX(h,k,m);
  1895. t = d;
  1896. data += 4;
  1897. len -= 4;
  1898. }
  1899. /* Handle leftover data in temp registers */
  1900. d = 0;
  1901. if(len >= align) {
  1902. uint32_t k;
  1903. switch(align) {
  1904. case 3: d |= data[2] << 16; /* fallthrough */
  1905. case 2: d |= data[1] << 8; /* fallthrough */
  1906. case 1: d |= data[0]; /* fallthrough */
  1907. }
  1908. k = (t >> sr) | (d << sl);
  1909. MIX(h,k,m);
  1910. data += align;
  1911. len -= align;
  1912. /* ----------
  1913. * Handle tail bytes */
  1914. switch(len) {
  1915. case 3: h ^= data[2] << 16; /* fallthrough */
  1916. case 2: h ^= data[1] << 8; /* fallthrough */
  1917. case 1: h ^= data[0]; h *= m; /* fallthrough */
  1918. };
  1919. } else {
  1920. switch(len) {
  1921. case 3: d |= data[2] << 16; /* fallthrough */
  1922. case 2: d |= data[1] << 8; /* fallthrough */
  1923. case 1: d |= data[0]; /* fallthrough */
  1924. case 0: h ^= (t >> sr) | (d << sl); h *= m;
  1925. }
  1926. }
  1927. h ^= h >> 13;
  1928. h *= m;
  1929. h ^= h >> 15;
  1930. return h;
  1931. } else {
  1932. while(len >= 4) {
  1933. uint32_t k = *(uint32_t *)data;
  1934. MIX(h,k,m);
  1935. data += 4;
  1936. len -= 4;
  1937. }
  1938. /* ----------
  1939. * Handle tail bytes */
  1940. switch(len) {
  1941. case 3: h ^= data[2] << 16; /* fallthrough */
  1942. case 2: h ^= data[1] << 8; /* fallthrough */
  1943. case 1: h ^= data[0]; h *= m;
  1944. };
  1945. h ^= h >> 13;
  1946. h *= m;
  1947. h ^= h >> 15;
  1948. return h;
  1949. }
  1950. }
  1951. #undef MIX
  1952. #endif /* UPB_UNALIGNED_READS_OK */
  1953. #include <errno.h>
  1954. #include <stdarg.h>
  1955. #include <stddef.h>
  1956. #include <stdint.h>
  1957. #include <stdio.h>
  1958. #include <stdlib.h>
  1959. #include <string.h>
  1960. /* upb_status *****************************************************************/
  1961. void upb_status_clear(upb_status *status) {
  1962. if (!status) return;
  1963. status->ok = true;
  1964. status->msg[0] = '\0';
  1965. }
  1966. bool upb_ok(const upb_status *status) { return status->ok; }
  1967. const char *upb_status_errmsg(const upb_status *status) { return status->msg; }
  1968. void upb_status_seterrmsg(upb_status *status, const char *msg) {
  1969. if (!status) return;
  1970. status->ok = false;
  1971. strncpy(status->msg, msg, UPB_STATUS_MAX_MESSAGE - 1);
  1972. status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0';
  1973. }
  1974. void upb_status_seterrf(upb_status *status, const char *fmt, ...) {
  1975. va_list args;
  1976. va_start(args, fmt);
  1977. upb_status_vseterrf(status, fmt, args);
  1978. va_end(args);
  1979. }
  1980. void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args) {
  1981. if (!status) return;
  1982. status->ok = false;
  1983. _upb_vsnprintf(status->msg, sizeof(status->msg), fmt, args);
  1984. status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0';
  1985. }
  1986. void upb_status_vappenderrf(upb_status *status, const char *fmt, va_list args) {
  1987. size_t len;
  1988. if (!status) return;
  1989. status->ok = false;
  1990. len = strlen(status->msg);
  1991. _upb_vsnprintf(status->msg + len, sizeof(status->msg) - len, fmt, args);
  1992. status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0';
  1993. }
  1994. /* upb_alloc ******************************************************************/
  1995. static void *upb_global_allocfunc(upb_alloc *alloc, void *ptr, size_t oldsize,
  1996. size_t size) {
  1997. UPB_UNUSED(alloc);
  1998. UPB_UNUSED(oldsize);
  1999. if (size == 0) {
  2000. free(ptr);
  2001. return NULL;
  2002. } else {
  2003. return realloc(ptr, size);
  2004. }
  2005. }
  2006. upb_alloc upb_alloc_global = {&upb_global_allocfunc};
  2007. /* upb_arena ******************************************************************/
  2008. /* Be conservative and choose 16 in case anyone is using SSE. */
  2009. typedef struct mem_block {
  2010. struct mem_block *next;
  2011. uint32_t size;
  2012. uint32_t cleanups;
  2013. /* Data follows. */
  2014. } mem_block;
  2015. typedef struct cleanup_ent {
  2016. upb_cleanup_func *cleanup;
  2017. void *ud;
  2018. } cleanup_ent;
  2019. struct upb_arena {
  2020. _upb_arena_head head;
  2021. uint32_t *cleanups;
  2022. /* Allocator to allocate arena blocks. We are responsible for freeing these
  2023. * when we are destroyed. */
  2024. upb_alloc *block_alloc;
  2025. uint32_t last_size;
  2026. /* When multiple arenas are fused together, each arena points to a parent
  2027. * arena (root points to itself). The root tracks how many live arenas
  2028. * reference it. */
  2029. uint32_t refcount; /* Only used when a->parent == a */
  2030. struct upb_arena *parent;
  2031. /* Linked list of blocks to free/cleanup. */
  2032. mem_block *freelist, *freelist_tail;
  2033. };
  2034. static const size_t memblock_reserve = UPB_ALIGN_UP(sizeof(mem_block), 16);
  2035. static upb_arena *arena_findroot(upb_arena *a) {
  2036. /* Path splitting keeps time complexity down, see:
  2037. * https://en.wikipedia.org/wiki/Disjoint-set_data_structure */
  2038. while (a->parent != a) {
  2039. upb_arena *next = a->parent;
  2040. a->parent = next->parent;
  2041. a = next;
  2042. }
  2043. return a;
  2044. }
  2045. static void upb_arena_addblock(upb_arena *a, void *ptr, size_t size) {
  2046. mem_block *block = ptr;
  2047. upb_arena *root = arena_findroot(a);
  2048. /* The block is for arena |a|, but should appear in the freelist of |root|. */
  2049. block->next = root->freelist;
  2050. block->size = (uint32_t)size;
  2051. block->cleanups = 0;
  2052. root->freelist = block;
  2053. a->last_size = block->size;
  2054. if (!root->freelist_tail) root->freelist_tail = block;
  2055. a->head.ptr = UPB_PTR_AT(block, memblock_reserve, char);
  2056. a->head.end = UPB_PTR_AT(block, size, char);
  2057. a->cleanups = &block->cleanups;
  2058. /* TODO(haberman): ASAN poison. */
  2059. }
  2060. static bool upb_arena_allocblock(upb_arena *a, size_t size) {
  2061. size_t block_size = UPB_MAX(size, a->last_size * 2) + memblock_reserve;
  2062. mem_block *block = upb_malloc(a->block_alloc, block_size);
  2063. if (!block) return false;
  2064. upb_arena_addblock(a, block, block_size);
  2065. return true;
  2066. }
  2067. static bool arena_has(upb_arena *a, size_t size) {
  2068. _upb_arena_head *h = (_upb_arena_head*)a;
  2069. return (size_t)(h->end - h->ptr) >= size;
  2070. }
  2071. void *_upb_arena_slowmalloc(upb_arena *a, size_t size) {
  2072. if (!upb_arena_allocblock(a, size)) return NULL; /* Out of memory. */
  2073. UPB_ASSERT(arena_has(a, size));
  2074. return upb_arena_malloc(a, size);
  2075. }
  2076. static void *upb_arena_doalloc(upb_alloc *alloc, void *ptr, size_t oldsize,
  2077. size_t size) {
  2078. upb_arena *a = (upb_arena*)alloc; /* upb_alloc is initial member. */
  2079. return upb_arena_realloc(a, ptr, oldsize, size);
  2080. }
  2081. /* Public Arena API ***********************************************************/
  2082. upb_arena *arena_initslow(void *mem, size_t n, upb_alloc *alloc) {
  2083. const size_t first_block_overhead = sizeof(upb_arena) + memblock_reserve;
  2084. upb_arena *a;
  2085. /* We need to malloc the initial block. */
  2086. n = first_block_overhead + 256;
  2087. if (!alloc || !(mem = upb_malloc(alloc, n))) {
  2088. return NULL;
  2089. }
  2090. a = UPB_PTR_AT(mem, n - sizeof(*a), upb_arena);
  2091. n -= sizeof(*a);
  2092. a->head.alloc.func = &upb_arena_doalloc;
  2093. a->block_alloc = alloc;
  2094. a->parent = a;
  2095. a->refcount = 1;
  2096. a->freelist = NULL;
  2097. a->freelist_tail = NULL;
  2098. upb_arena_addblock(a, mem, n);
  2099. return a;
  2100. }
  2101. upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc) {
  2102. upb_arena *a;
  2103. /* Round block size down to alignof(*a) since we will allocate the arena
  2104. * itself at the end. */
  2105. n = UPB_ALIGN_DOWN(n, UPB_ALIGN_OF(upb_arena));
  2106. if (UPB_UNLIKELY(n < sizeof(upb_arena))) {
  2107. return arena_initslow(mem, n, alloc);
  2108. }
  2109. a = UPB_PTR_AT(mem, n - sizeof(*a), upb_arena);
  2110. n -= sizeof(*a);
  2111. a->head.alloc.func = &upb_arena_doalloc;
  2112. a->block_alloc = alloc;
  2113. a->parent = a;
  2114. a->refcount = 1;
  2115. a->last_size = 128;
  2116. a->head.ptr = mem;
  2117. a->head.end = UPB_PTR_AT(mem, n, char);
  2118. a->freelist = NULL;
  2119. a->cleanups = NULL;
  2120. return a;
  2121. }
  2122. static void arena_dofree(upb_arena *a) {
  2123. mem_block *block = a->freelist;
  2124. UPB_ASSERT(a->parent == a);
  2125. UPB_ASSERT(a->refcount == 0);
  2126. while (block) {
  2127. /* Load first since we are deleting block. */
  2128. mem_block *next = block->next;
  2129. if (block->cleanups > 0) {
  2130. cleanup_ent *end = UPB_PTR_AT(block, block->size, void);
  2131. cleanup_ent *ptr = end - block->cleanups;
  2132. for (; ptr < end; ptr++) {
  2133. ptr->cleanup(ptr->ud);
  2134. }
  2135. }
  2136. upb_free(a->block_alloc, block);
  2137. block = next;
  2138. }
  2139. }
  2140. void upb_arena_free(upb_arena *a) {
  2141. a = arena_findroot(a);
  2142. if (--a->refcount == 0) arena_dofree(a);
  2143. }
  2144. bool upb_arena_addcleanup(upb_arena *a, void *ud, upb_cleanup_func *func) {
  2145. cleanup_ent *ent;
  2146. if (!a->cleanups || !arena_has(a, sizeof(cleanup_ent))) {
  2147. if (!upb_arena_allocblock(a, 128)) return false; /* Out of memory. */
  2148. UPB_ASSERT(arena_has(a, sizeof(cleanup_ent)));
  2149. }
  2150. a->head.end -= sizeof(cleanup_ent);
  2151. ent = (cleanup_ent*)a->head.end;
  2152. (*a->cleanups)++;
  2153. ent->cleanup = func;
  2154. ent->ud = ud;
  2155. return true;
  2156. }
  2157. void upb_arena_fuse(upb_arena *a1, upb_arena *a2) {
  2158. upb_arena *r1 = arena_findroot(a1);
  2159. upb_arena *r2 = arena_findroot(a2);
  2160. if (r1 == r2) return; /* Already fused. */
  2161. /* We want to join the smaller tree to the larger tree.
  2162. * So swap first if they are backwards. */
  2163. if (r1->refcount < r2->refcount) {
  2164. upb_arena *tmp = r1;
  2165. r1 = r2;
  2166. r2 = tmp;
  2167. }
  2168. /* r1 takes over r2's freelist and refcount. */
  2169. r1->refcount += r2->refcount;
  2170. if (r2->freelist_tail) {
  2171. UPB_ASSERT(r2->freelist_tail->next == NULL);
  2172. r2->freelist_tail->next = r1->freelist;
  2173. r1->freelist = r2->freelist;
  2174. }
  2175. r2->parent = r1;
  2176. }
  2177. /* This file was generated by upbc (the upb compiler) from the input
  2178. * file:
  2179. *
  2180. * google/protobuf/descriptor.proto
  2181. *
  2182. * Do not edit -- your changes will be discarded when the file is
  2183. * regenerated. */
  2184. #include <stddef.h>
  2185. static const upb_msglayout *const google_protobuf_FileDescriptorSet_submsgs[1] = {
  2186. &google_protobuf_FileDescriptorProto_msginit,
  2187. };
  2188. static const upb_msglayout_field google_protobuf_FileDescriptorSet__fields[1] = {
  2189. {1, UPB_SIZE(0, 0), 0, 0, 11, 3},
  2190. };
  2191. const upb_msglayout google_protobuf_FileDescriptorSet_msginit = {
  2192. &google_protobuf_FileDescriptorSet_submsgs[0],
  2193. &google_protobuf_FileDescriptorSet__fields[0],
  2194. UPB_SIZE(4, 8), 1, false,
  2195. };
  2196. static const upb_msglayout *const google_protobuf_FileDescriptorProto_submsgs[6] = {
  2197. &google_protobuf_DescriptorProto_msginit,
  2198. &google_protobuf_EnumDescriptorProto_msginit,
  2199. &google_protobuf_FieldDescriptorProto_msginit,
  2200. &google_protobuf_FileOptions_msginit,
  2201. &google_protobuf_ServiceDescriptorProto_msginit,
  2202. &google_protobuf_SourceCodeInfo_msginit,
  2203. };
  2204. static const upb_msglayout_field google_protobuf_FileDescriptorProto__fields[12] = {
  2205. {1, UPB_SIZE(4, 8), 1, 0, 12, 1},
  2206. {2, UPB_SIZE(12, 24), 2, 0, 12, 1},
  2207. {3, UPB_SIZE(36, 72), 0, 0, 12, 3},
  2208. {4, UPB_SIZE(40, 80), 0, 0, 11, 3},
  2209. {5, UPB_SIZE(44, 88), 0, 1, 11, 3},
  2210. {6, UPB_SIZE(48, 96), 0, 4, 11, 3},
  2211. {7, UPB_SIZE(52, 104), 0, 2, 11, 3},
  2212. {8, UPB_SIZE(28, 56), 4, 3, 11, 1},
  2213. {9, UPB_SIZE(32, 64), 5, 5, 11, 1},
  2214. {10, UPB_SIZE(56, 112), 0, 0, 5, 3},
  2215. {11, UPB_SIZE(60, 120), 0, 0, 5, 3},
  2216. {12, UPB_SIZE(20, 40), 3, 0, 12, 1},
  2217. };
  2218. const upb_msglayout google_protobuf_FileDescriptorProto_msginit = {
  2219. &google_protobuf_FileDescriptorProto_submsgs[0],
  2220. &google_protobuf_FileDescriptorProto__fields[0],
  2221. UPB_SIZE(64, 128), 12, false,
  2222. };
  2223. static const upb_msglayout *const google_protobuf_DescriptorProto_submsgs[8] = {
  2224. &google_protobuf_DescriptorProto_msginit,
  2225. &google_protobuf_DescriptorProto_ExtensionRange_msginit,
  2226. &google_protobuf_DescriptorProto_ReservedRange_msginit,
  2227. &google_protobuf_EnumDescriptorProto_msginit,
  2228. &google_protobuf_FieldDescriptorProto_msginit,
  2229. &google_protobuf_MessageOptions_msginit,
  2230. &google_protobuf_OneofDescriptorProto_msginit,
  2231. };
  2232. static const upb_msglayout_field google_protobuf_DescriptorProto__fields[10] = {
  2233. {1, UPB_SIZE(4, 8), 1, 0, 12, 1},
  2234. {2, UPB_SIZE(16, 32), 0, 4, 11, 3},
  2235. {3, UPB_SIZE(20, 40), 0, 0, 11, 3},
  2236. {4, UPB_SIZE(24, 48), 0, 3, 11, 3},
  2237. {5, UPB_SIZE(28, 56), 0, 1, 11, 3},
  2238. {6, UPB_SIZE(32, 64), 0, 4, 11, 3},
  2239. {7, UPB_SIZE(12, 24), 2, 5, 11, 1},
  2240. {8, UPB_SIZE(36, 72), 0, 6, 11, 3},
  2241. {9, UPB_SIZE(40, 80), 0, 2, 11, 3},
  2242. {10, UPB_SIZE(44, 88), 0, 0, 12, 3},
  2243. };
  2244. const upb_msglayout google_protobuf_DescriptorProto_msginit = {
  2245. &google_protobuf_DescriptorProto_submsgs[0],
  2246. &google_protobuf_DescriptorProto__fields[0],
  2247. UPB_SIZE(48, 96), 10, false,
  2248. };
  2249. static const upb_msglayout *const google_protobuf_DescriptorProto_ExtensionRange_submsgs[1] = {
  2250. &google_protobuf_ExtensionRangeOptions_msginit,
  2251. };
  2252. static const upb_msglayout_field google_protobuf_DescriptorProto_ExtensionRange__fields[3] = {
  2253. {1, UPB_SIZE(4, 4), 1, 0, 5, 1},
  2254. {2, UPB_SIZE(8, 8), 2, 0, 5, 1},
  2255. {3, UPB_SIZE(12, 16), 3, 0, 11, 1},
  2256. };
  2257. const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit = {
  2258. &google_protobuf_DescriptorProto_ExtensionRange_submsgs[0],
  2259. &google_protobuf_DescriptorProto_ExtensionRange__fields[0],
  2260. UPB_SIZE(16, 24), 3, false,
  2261. };
  2262. static const upb_msglayout_field google_protobuf_DescriptorProto_ReservedRange__fields[2] = {
  2263. {1, UPB_SIZE(4, 4), 1, 0, 5, 1},
  2264. {2, UPB_SIZE(8, 8), 2, 0, 5, 1},
  2265. };
  2266. const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit = {
  2267. NULL,
  2268. &google_protobuf_DescriptorProto_ReservedRange__fields[0],
  2269. UPB_SIZE(12, 12), 2, false,
  2270. };
  2271. static const upb_msglayout *const google_protobuf_ExtensionRangeOptions_submsgs[1] = {
  2272. &google_protobuf_UninterpretedOption_msginit,
  2273. };
  2274. static const upb_msglayout_field google_protobuf_ExtensionRangeOptions__fields[1] = {
  2275. {999, UPB_SIZE(0, 0), 0, 0, 11, 3},
  2276. };
  2277. const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit = {
  2278. &google_protobuf_ExtensionRangeOptions_submsgs[0],
  2279. &google_protobuf_ExtensionRangeOptions__fields[0],
  2280. UPB_SIZE(4, 8), 1, false,
  2281. };
  2282. static const upb_msglayout *const google_protobuf_FieldDescriptorProto_submsgs[1] = {
  2283. &google_protobuf_FieldOptions_msginit,
  2284. };
  2285. static const upb_msglayout_field google_protobuf_FieldDescriptorProto__fields[11] = {
  2286. {1, UPB_SIZE(36, 40), 6, 0, 12, 1},
  2287. {2, UPB_SIZE(44, 56), 7, 0, 12, 1},
  2288. {3, UPB_SIZE(24, 24), 3, 0, 5, 1},
  2289. {4, UPB_SIZE(8, 8), 1, 0, 14, 1},
  2290. {5, UPB_SIZE(16, 16), 2, 0, 14, 1},
  2291. {6, UPB_SIZE(52, 72), 8, 0, 12, 1},
  2292. {7, UPB_SIZE(60, 88), 9, 0, 12, 1},
  2293. {8, UPB_SIZE(76, 120), 11, 0, 11, 1},
  2294. {9, UPB_SIZE(28, 28), 4, 0, 5, 1},
  2295. {10, UPB_SIZE(68, 104), 10, 0, 12, 1},
  2296. {17, UPB_SIZE(32, 32), 5, 0, 8, 1},
  2297. };
  2298. const upb_msglayout google_protobuf_FieldDescriptorProto_msginit = {
  2299. &google_protobuf_FieldDescriptorProto_submsgs[0],
  2300. &google_protobuf_FieldDescriptorProto__fields[0],
  2301. UPB_SIZE(80, 128), 11, false,
  2302. };
  2303. static const upb_msglayout *const google_protobuf_OneofDescriptorProto_submsgs[1] = {
  2304. &google_protobuf_OneofOptions_msginit,
  2305. };
  2306. static const upb_msglayout_field google_protobuf_OneofDescriptorProto__fields[2] = {
  2307. {1, UPB_SIZE(4, 8), 1, 0, 12, 1},
  2308. {2, UPB_SIZE(12, 24), 2, 0, 11, 1},
  2309. };
  2310. const upb_msglayout google_protobuf_OneofDescriptorProto_msginit = {
  2311. &google_protobuf_OneofDescriptorProto_submsgs[0],
  2312. &google_protobuf_OneofDescriptorProto__fields[0],
  2313. UPB_SIZE(16, 32), 2, false,
  2314. };
  2315. static const upb_msglayout *const google_protobuf_EnumDescriptorProto_submsgs[3] = {
  2316. &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit,
  2317. &google_protobuf_EnumOptions_msginit,
  2318. &google_protobuf_EnumValueDescriptorProto_msginit,
  2319. };
  2320. static const upb_msglayout_field google_protobuf_EnumDescriptorProto__fields[5] = {
  2321. {1, UPB_SIZE(4, 8), 1, 0, 12, 1},
  2322. {2, UPB_SIZE(16, 32), 0, 2, 11, 3},
  2323. {3, UPB_SIZE(12, 24), 2, 1, 11, 1},
  2324. {4, UPB_SIZE(20, 40), 0, 0, 11, 3},
  2325. {5, UPB_SIZE(24, 48), 0, 0, 12, 3},
  2326. };
  2327. const upb_msglayout google_protobuf_EnumDescriptorProto_msginit = {
  2328. &google_protobuf_EnumDescriptorProto_submsgs[0],
  2329. &google_protobuf_EnumDescriptorProto__fields[0],
  2330. UPB_SIZE(32, 64), 5, false,
  2331. };
  2332. static const upb_msglayout_field google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[2] = {
  2333. {1, UPB_SIZE(4, 4), 1, 0, 5, 1},
  2334. {2, UPB_SIZE(8, 8), 2, 0, 5, 1},
  2335. };
  2336. const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit = {
  2337. NULL,
  2338. &google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[0],
  2339. UPB_SIZE(12, 12), 2, false,
  2340. };
  2341. static const upb_msglayout *const google_protobuf_EnumValueDescriptorProto_submsgs[1] = {
  2342. &google_protobuf_EnumValueOptions_msginit,
  2343. };
  2344. static const upb_msglayout_field google_protobuf_EnumValueDescriptorProto__fields[3] = {
  2345. {1, UPB_SIZE(8, 8), 2, 0, 12, 1},
  2346. {2, UPB_SIZE(4, 4), 1, 0, 5, 1},
  2347. {3, UPB_SIZE(16, 24), 3, 0, 11, 1},
  2348. };
  2349. const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit = {
  2350. &google_protobuf_EnumValueDescriptorProto_submsgs[0],
  2351. &google_protobuf_EnumValueDescriptorProto__fields[0],
  2352. UPB_SIZE(24, 32), 3, false,
  2353. };
  2354. static const upb_msglayout *const google_protobuf_ServiceDescriptorProto_submsgs[2] = {
  2355. &google_protobuf_MethodDescriptorProto_msginit,
  2356. &google_protobuf_ServiceOptions_msginit,
  2357. };
  2358. static const upb_msglayout_field google_protobuf_ServiceDescriptorProto__fields[3] = {
  2359. {1, UPB_SIZE(4, 8), 1, 0, 12, 1},
  2360. {2, UPB_SIZE(16, 32), 0, 0, 11, 3},
  2361. {3, UPB_SIZE(12, 24), 2, 1, 11, 1},
  2362. };
  2363. const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit = {
  2364. &google_protobuf_ServiceDescriptorProto_submsgs[0],
  2365. &google_protobuf_ServiceDescriptorProto__fields[0],
  2366. UPB_SIZE(24, 48), 3, false,
  2367. };
  2368. static const upb_msglayout *const google_protobuf_MethodDescriptorProto_submsgs[1] = {
  2369. &google_protobuf_MethodOptions_msginit,
  2370. };
  2371. static const upb_msglayout_field google_protobuf_MethodDescriptorProto__fields[6] = {
  2372. {1, UPB_SIZE(4, 8), 3, 0, 12, 1},
  2373. {2, UPB_SIZE(12, 24), 4, 0, 12, 1},
  2374. {3, UPB_SIZE(20, 40), 5, 0, 12, 1},
  2375. {4, UPB_SIZE(28, 56), 6, 0, 11, 1},
  2376. {5, UPB_SIZE(1, 1), 1, 0, 8, 1},
  2377. {6, UPB_SIZE(2, 2), 2, 0, 8, 1},
  2378. };
  2379. const upb_msglayout google_protobuf_MethodDescriptorProto_msginit = {
  2380. &google_protobuf_MethodDescriptorProto_submsgs[0],
  2381. &google_protobuf_MethodDescriptorProto__fields[0],
  2382. UPB_SIZE(32, 64), 6, false,
  2383. };
  2384. static const upb_msglayout *const google_protobuf_FileOptions_submsgs[1] = {
  2385. &google_protobuf_UninterpretedOption_msginit,
  2386. };
  2387. static const upb_msglayout_field google_protobuf_FileOptions__fields[21] = {
  2388. {1, UPB_SIZE(28, 32), 11, 0, 12, 1},
  2389. {8, UPB_SIZE(36, 48), 12, 0, 12, 1},
  2390. {9, UPB_SIZE(8, 8), 1, 0, 14, 1},
  2391. {10, UPB_SIZE(16, 16), 2, 0, 8, 1},
  2392. {11, UPB_SIZE(44, 64), 13, 0, 12, 1},
  2393. {16, UPB_SIZE(17, 17), 3, 0, 8, 1},
  2394. {17, UPB_SIZE(18, 18), 4, 0, 8, 1},
  2395. {18, UPB_SIZE(19, 19), 5, 0, 8, 1},
  2396. {20, UPB_SIZE(20, 20), 6, 0, 8, 1},
  2397. {23, UPB_SIZE(21, 21), 7, 0, 8, 1},
  2398. {27, UPB_SIZE(22, 22), 8, 0, 8, 1},
  2399. {31, UPB_SIZE(23, 23), 9, 0, 8, 1},
  2400. {36, UPB_SIZE(52, 80), 14, 0, 12, 1},
  2401. {37, UPB_SIZE(60, 96), 15, 0, 12, 1},
  2402. {39, UPB_SIZE(68, 112), 16, 0, 12, 1},
  2403. {40, UPB_SIZE(76, 128), 17, 0, 12, 1},
  2404. {41, UPB_SIZE(84, 144), 18, 0, 12, 1},
  2405. {42, UPB_SIZE(24, 24), 10, 0, 8, 1},
  2406. {44, UPB_SIZE(92, 160), 19, 0, 12, 1},
  2407. {45, UPB_SIZE(100, 176), 20, 0, 12, 1},
  2408. {999, UPB_SIZE(108, 192), 0, 0, 11, 3},
  2409. };
  2410. const upb_msglayout google_protobuf_FileOptions_msginit = {
  2411. &google_protobuf_FileOptions_submsgs[0],
  2412. &google_protobuf_FileOptions__fields[0],
  2413. UPB_SIZE(112, 208), 21, false,
  2414. };
  2415. static const upb_msglayout *const google_protobuf_MessageOptions_submsgs[1] = {
  2416. &google_protobuf_UninterpretedOption_msginit,
  2417. };
  2418. static const upb_msglayout_field google_protobuf_MessageOptions__fields[5] = {
  2419. {1, UPB_SIZE(1, 1), 1, 0, 8, 1},
  2420. {2, UPB_SIZE(2, 2), 2, 0, 8, 1},
  2421. {3, UPB_SIZE(3, 3), 3, 0, 8, 1},
  2422. {7, UPB_SIZE(4, 4), 4, 0, 8, 1},
  2423. {999, UPB_SIZE(8, 8), 0, 0, 11, 3},
  2424. };
  2425. const upb_msglayout google_protobuf_MessageOptions_msginit = {
  2426. &google_protobuf_MessageOptions_submsgs[0],
  2427. &google_protobuf_MessageOptions__fields[0],
  2428. UPB_SIZE(12, 16), 5, false,
  2429. };
  2430. static const upb_msglayout *const google_protobuf_FieldOptions_submsgs[1] = {
  2431. &google_protobuf_UninterpretedOption_msginit,
  2432. };
  2433. static const upb_msglayout_field google_protobuf_FieldOptions__fields[7] = {
  2434. {1, UPB_SIZE(8, 8), 1, 0, 14, 1},
  2435. {2, UPB_SIZE(24, 24), 3, 0, 8, 1},
  2436. {3, UPB_SIZE(25, 25), 4, 0, 8, 1},
  2437. {5, UPB_SIZE(26, 26), 5, 0, 8, 1},
  2438. {6, UPB_SIZE(16, 16), 2, 0, 14, 1},
  2439. {10, UPB_SIZE(27, 27), 6, 0, 8, 1},
  2440. {999, UPB_SIZE(28, 32), 0, 0, 11, 3},
  2441. };
  2442. const upb_msglayout google_protobuf_FieldOptions_msginit = {
  2443. &google_protobuf_FieldOptions_submsgs[0],
  2444. &google_protobuf_FieldOptions__fields[0],
  2445. UPB_SIZE(32, 40), 7, false,
  2446. };
  2447. static const upb_msglayout *const google_protobuf_OneofOptions_submsgs[1] = {
  2448. &google_protobuf_UninterpretedOption_msginit,
  2449. };
  2450. static const upb_msglayout_field google_protobuf_OneofOptions__fields[1] = {
  2451. {999, UPB_SIZE(0, 0), 0, 0, 11, 3},
  2452. };
  2453. const upb_msglayout google_protobuf_OneofOptions_msginit = {
  2454. &google_protobuf_OneofOptions_submsgs[0],
  2455. &google_protobuf_OneofOptions__fields[0],
  2456. UPB_SIZE(4, 8), 1, false,
  2457. };
  2458. static const upb_msglayout *const google_protobuf_EnumOptions_submsgs[1] = {
  2459. &google_protobuf_UninterpretedOption_msginit,
  2460. };
  2461. static const upb_msglayout_field google_protobuf_EnumOptions__fields[3] = {
  2462. {2, UPB_SIZE(1, 1), 1, 0, 8, 1},
  2463. {3, UPB_SIZE(2, 2), 2, 0, 8, 1},
  2464. {999, UPB_SIZE(4, 8), 0, 0, 11, 3},
  2465. };
  2466. const upb_msglayout google_protobuf_EnumOptions_msginit = {
  2467. &google_protobuf_EnumOptions_submsgs[0],
  2468. &google_protobuf_EnumOptions__fields[0],
  2469. UPB_SIZE(8, 16), 3, false,
  2470. };
  2471. static const upb_msglayout *const google_protobuf_EnumValueOptions_submsgs[1] = {
  2472. &google_protobuf_UninterpretedOption_msginit,
  2473. };
  2474. static const upb_msglayout_field google_protobuf_EnumValueOptions__fields[2] = {
  2475. {1, UPB_SIZE(1, 1), 1, 0, 8, 1},
  2476. {999, UPB_SIZE(4, 8), 0, 0, 11, 3},
  2477. };
  2478. const upb_msglayout google_protobuf_EnumValueOptions_msginit = {
  2479. &google_protobuf_EnumValueOptions_submsgs[0],
  2480. &google_protobuf_EnumValueOptions__fields[0],
  2481. UPB_SIZE(8, 16), 2, false,
  2482. };
  2483. static const upb_msglayout *const google_protobuf_ServiceOptions_submsgs[1] = {
  2484. &google_protobuf_UninterpretedOption_msginit,
  2485. };
  2486. static const upb_msglayout_field google_protobuf_ServiceOptions__fields[2] = {
  2487. {33, UPB_SIZE(1, 1), 1, 0, 8, 1},
  2488. {999, UPB_SIZE(4, 8), 0, 0, 11, 3},
  2489. };
  2490. const upb_msglayout google_protobuf_ServiceOptions_msginit = {
  2491. &google_protobuf_ServiceOptions_submsgs[0],
  2492. &google_protobuf_ServiceOptions__fields[0],
  2493. UPB_SIZE(8, 16), 2, false,
  2494. };
  2495. static const upb_msglayout *const google_protobuf_MethodOptions_submsgs[1] = {
  2496. &google_protobuf_UninterpretedOption_msginit,
  2497. };
  2498. static const upb_msglayout_field google_protobuf_MethodOptions__fields[3] = {
  2499. {33, UPB_SIZE(16, 16), 2, 0, 8, 1},
  2500. {34, UPB_SIZE(8, 8), 1, 0, 14, 1},
  2501. {999, UPB_SIZE(20, 24), 0, 0, 11, 3},
  2502. };
  2503. const upb_msglayout google_protobuf_MethodOptions_msginit = {
  2504. &google_protobuf_MethodOptions_submsgs[0],
  2505. &google_protobuf_MethodOptions__fields[0],
  2506. UPB_SIZE(24, 32), 3, false,
  2507. };
  2508. static const upb_msglayout *const google_protobuf_UninterpretedOption_submsgs[1] = {
  2509. &google_protobuf_UninterpretedOption_NamePart_msginit,
  2510. };
  2511. static const upb_msglayout_field google_protobuf_UninterpretedOption__fields[7] = {
  2512. {2, UPB_SIZE(56, 80), 0, 0, 11, 3},
  2513. {3, UPB_SIZE(32, 32), 4, 0, 12, 1},
  2514. {4, UPB_SIZE(8, 8), 1, 0, 4, 1},
  2515. {5, UPB_SIZE(16, 16), 2, 0, 3, 1},
  2516. {6, UPB_SIZE(24, 24), 3, 0, 1, 1},
  2517. {7, UPB_SIZE(40, 48), 5, 0, 12, 1},
  2518. {8, UPB_SIZE(48, 64), 6, 0, 12, 1},
  2519. };
  2520. const upb_msglayout google_protobuf_UninterpretedOption_msginit = {
  2521. &google_protobuf_UninterpretedOption_submsgs[0],
  2522. &google_protobuf_UninterpretedOption__fields[0],
  2523. UPB_SIZE(64, 96), 7, false,
  2524. };
  2525. static const upb_msglayout_field google_protobuf_UninterpretedOption_NamePart__fields[2] = {
  2526. {1, UPB_SIZE(4, 8), 2, 0, 12, 2},
  2527. {2, UPB_SIZE(1, 1), 1, 0, 8, 2},
  2528. };
  2529. const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit = {
  2530. NULL,
  2531. &google_protobuf_UninterpretedOption_NamePart__fields[0],
  2532. UPB_SIZE(16, 32), 2, false,
  2533. };
  2534. static const upb_msglayout *const google_protobuf_SourceCodeInfo_submsgs[1] = {
  2535. &google_protobuf_SourceCodeInfo_Location_msginit,
  2536. };
  2537. static const upb_msglayout_field google_protobuf_SourceCodeInfo__fields[1] = {
  2538. {1, UPB_SIZE(0, 0), 0, 0, 11, 3},
  2539. };
  2540. const upb_msglayout google_protobuf_SourceCodeInfo_msginit = {
  2541. &google_protobuf_SourceCodeInfo_submsgs[0],
  2542. &google_protobuf_SourceCodeInfo__fields[0],
  2543. UPB_SIZE(4, 8), 1, false,
  2544. };
  2545. static const upb_msglayout_field google_protobuf_SourceCodeInfo_Location__fields[5] = {
  2546. {1, UPB_SIZE(20, 40), 0, 0, 5, _UPB_LABEL_PACKED},
  2547. {2, UPB_SIZE(24, 48), 0, 0, 5, _UPB_LABEL_PACKED},
  2548. {3, UPB_SIZE(4, 8), 1, 0, 12, 1},
  2549. {4, UPB_SIZE(12, 24), 2, 0, 12, 1},
  2550. {6, UPB_SIZE(28, 56), 0, 0, 12, 3},
  2551. };
  2552. const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit = {
  2553. NULL,
  2554. &google_protobuf_SourceCodeInfo_Location__fields[0],
  2555. UPB_SIZE(32, 64), 5, false,
  2556. };
  2557. static const upb_msglayout *const google_protobuf_GeneratedCodeInfo_submsgs[1] = {
  2558. &google_protobuf_GeneratedCodeInfo_Annotation_msginit,
  2559. };
  2560. static const upb_msglayout_field google_protobuf_GeneratedCodeInfo__fields[1] = {
  2561. {1, UPB_SIZE(0, 0), 0, 0, 11, 3},
  2562. };
  2563. const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit = {
  2564. &google_protobuf_GeneratedCodeInfo_submsgs[0],
  2565. &google_protobuf_GeneratedCodeInfo__fields[0],
  2566. UPB_SIZE(4, 8), 1, false,
  2567. };
  2568. static const upb_msglayout_field google_protobuf_GeneratedCodeInfo_Annotation__fields[4] = {
  2569. {1, UPB_SIZE(20, 32), 0, 0, 5, _UPB_LABEL_PACKED},
  2570. {2, UPB_SIZE(12, 16), 3, 0, 12, 1},
  2571. {3, UPB_SIZE(4, 4), 1, 0, 5, 1},
  2572. {4, UPB_SIZE(8, 8), 2, 0, 5, 1},
  2573. };
  2574. const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit = {
  2575. NULL,
  2576. &google_protobuf_GeneratedCodeInfo_Annotation__fields[0],
  2577. UPB_SIZE(24, 48), 4, false,
  2578. };
  2579. /* This file was generated by upbc (the upb compiler) from the input
  2580. * file:
  2581. *
  2582. * google/protobuf/descriptor.proto
  2583. *
  2584. * Do not edit -- your changes will be discarded when the file is
  2585. * regenerated. */
  2586. extern const upb_msglayout google_protobuf_FileDescriptorSet_msginit;
  2587. extern const upb_msglayout google_protobuf_FileDescriptorProto_msginit;
  2588. extern const upb_msglayout google_protobuf_DescriptorProto_msginit;
  2589. extern const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit;
  2590. extern const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit;
  2591. extern const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit;
  2592. extern const upb_msglayout google_protobuf_FieldDescriptorProto_msginit;
  2593. extern const upb_msglayout google_protobuf_OneofDescriptorProto_msginit;
  2594. extern const upb_msglayout google_protobuf_EnumDescriptorProto_msginit;
  2595. extern const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit;
  2596. extern const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit;
  2597. extern const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit;
  2598. extern const upb_msglayout google_protobuf_MethodDescriptorProto_msginit;
  2599. extern const upb_msglayout google_protobuf_FileOptions_msginit;
  2600. extern const upb_msglayout google_protobuf_MessageOptions_msginit;
  2601. extern const upb_msglayout google_protobuf_FieldOptions_msginit;
  2602. extern const upb_msglayout google_protobuf_OneofOptions_msginit;
  2603. extern const upb_msglayout google_protobuf_EnumOptions_msginit;
  2604. extern const upb_msglayout google_protobuf_EnumValueOptions_msginit;
  2605. extern const upb_msglayout google_protobuf_ServiceOptions_msginit;
  2606. extern const upb_msglayout google_protobuf_MethodOptions_msginit;
  2607. extern const upb_msglayout google_protobuf_UninterpretedOption_msginit;
  2608. extern const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit;
  2609. extern const upb_msglayout google_protobuf_SourceCodeInfo_msginit;
  2610. extern const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit;
  2611. extern const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit;
  2612. extern const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit;
  2613. static const upb_msglayout *layouts[27] = {
  2614. &google_protobuf_FileDescriptorSet_msginit,
  2615. &google_protobuf_FileDescriptorProto_msginit,
  2616. &google_protobuf_DescriptorProto_msginit,
  2617. &google_protobuf_DescriptorProto_ExtensionRange_msginit,
  2618. &google_protobuf_DescriptorProto_ReservedRange_msginit,
  2619. &google_protobuf_ExtensionRangeOptions_msginit,
  2620. &google_protobuf_FieldDescriptorProto_msginit,
  2621. &google_protobuf_OneofDescriptorProto_msginit,
  2622. &google_protobuf_EnumDescriptorProto_msginit,
  2623. &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit,
  2624. &google_protobuf_EnumValueDescriptorProto_msginit,
  2625. &google_protobuf_ServiceDescriptorProto_msginit,
  2626. &google_protobuf_MethodDescriptorProto_msginit,
  2627. &google_protobuf_FileOptions_msginit,
  2628. &google_protobuf_MessageOptions_msginit,
  2629. &google_protobuf_FieldOptions_msginit,
  2630. &google_protobuf_OneofOptions_msginit,
  2631. &google_protobuf_EnumOptions_msginit,
  2632. &google_protobuf_EnumValueOptions_msginit,
  2633. &google_protobuf_ServiceOptions_msginit,
  2634. &google_protobuf_MethodOptions_msginit,
  2635. &google_protobuf_UninterpretedOption_msginit,
  2636. &google_protobuf_UninterpretedOption_NamePart_msginit,
  2637. &google_protobuf_SourceCodeInfo_msginit,
  2638. &google_protobuf_SourceCodeInfo_Location_msginit,
  2639. &google_protobuf_GeneratedCodeInfo_msginit,
  2640. &google_protobuf_GeneratedCodeInfo_Annotation_msginit,
  2641. };
  2642. static const char descriptor[7619] = {'\n', ' ', 'g', 'o', 'o', 'g', 'l', 'e', '/', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '/', 'd', 'e', 's', 'c', 'r', 'i', 'p',
  2643. 't', 'o', 'r', '.', 'p', 'r', 'o', 't', 'o', '\022', '\017', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u',
  2644. 'f', '\"', 'M', '\n', '\021', 'F', 'i', 'l', 'e', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'S', 'e', 't', '\022', '8', '\n',
  2645. '\004', 'f', 'i', 'l', 'e', '\030', '\001', ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't',
  2646. 'o', 'b', 'u', 'f', '.', 'F', 'i', 'l', 'e', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R',
  2647. '\004', 'f', 'i', 'l', 'e', '\"', '\344', '\004', '\n', '\023', 'F', 'i', 'l', 'e', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P',
  2648. 'r', 'o', 't', 'o', '\022', '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e', '\022',
  2649. '\030', '\n', '\007', 'p', 'a', 'c', 'k', 'a', 'g', 'e', '\030', '\002', ' ', '\001', '(', '\t', 'R', '\007', 'p', 'a', 'c', 'k', 'a', 'g', 'e',
  2650. '\022', '\036', '\n', '\n', 'd', 'e', 'p', 'e', 'n', 'd', 'e', 'n', 'c', 'y', '\030', '\003', ' ', '\003', '(', '\t', 'R', '\n', 'd', 'e', 'p',
  2651. 'e', 'n', 'd', 'e', 'n', 'c', 'y', '\022', '+', '\n', '\021', 'p', 'u', 'b', 'l', 'i', 'c', '_', 'd', 'e', 'p', 'e', 'n', 'd', 'e',
  2652. 'n', 'c', 'y', '\030', '\n', ' ', '\003', '(', '\005', 'R', '\020', 'p', 'u', 'b', 'l', 'i', 'c', 'D', 'e', 'p', 'e', 'n', 'd', 'e', 'n',
  2653. 'c', 'y', '\022', '\'', '\n', '\017', 'w', 'e', 'a', 'k', '_', 'd', 'e', 'p', 'e', 'n', 'd', 'e', 'n', 'c', 'y', '\030', '\013', ' ', '\003',
  2654. '(', '\005', 'R', '\016', 'w', 'e', 'a', 'k', 'D', 'e', 'p', 'e', 'n', 'd', 'e', 'n', 'c', 'y', '\022', 'C', '\n', '\014', 'm', 'e', 's',
  2655. 's', 'a', 'g', 'e', '_', 't', 'y', 'p', 'e', '\030', '\004', ' ', '\003', '(', '\013', '2', ' ', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.',
  2656. 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R',
  2657. '\013', 'm', 'e', 's', 's', 'a', 'g', 'e', 'T', 'y', 'p', 'e', '\022', 'A', '\n', '\t', 'e', 'n', 'u', 'm', '_', 't', 'y', 'p', 'e',
  2658. '\030', '\005', ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.',
  2659. 'E', 'n', 'u', 'm', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\010', 'e', 'n', 'u', 'm',
  2660. 'T', 'y', 'p', 'e', '\022', 'A', '\n', '\007', 's', 'e', 'r', 'v', 'i', 'c', 'e', '\030', '\006', ' ', '\003', '(', '\013', '2', '\'', '.', 'g',
  2661. 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 'D', 'e', 's',
  2662. 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\007', 's', 'e', 'r', 'v', 'i', 'c', 'e', '\022', 'C', '\n', '\t',
  2663. 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\030', '\007', ' ', '\003', '(', '\013', '2', '%', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.',
  2664. 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P',
  2665. 'r', 'o', 't', 'o', 'R', '\t', 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\022', '6', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n',
  2666. 's', '\030', '\010', ' ', '\001', '(', '\013', '2', '\034', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f',
  2667. '.', 'F', 'i', 'l', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\022', 'I', '\n', '\020',
  2668. 's', 'o', 'u', 'r', 'c', 'e', '_', 'c', 'o', 'd', 'e', '_', 'i', 'n', 'f', 'o', '\030', '\t', ' ', '\001', '(', '\013', '2', '\037', '.',
  2669. 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'S', 'o', 'u', 'r', 'c', 'e', 'C', 'o', 'd',
  2670. 'e', 'I', 'n', 'f', 'o', 'R', '\016', 's', 'o', 'u', 'r', 'c', 'e', 'C', 'o', 'd', 'e', 'I', 'n', 'f', 'o', '\022', '\026', '\n', '\006',
  2671. 's', 'y', 'n', 't', 'a', 'x', '\030', '\014', ' ', '\001', '(', '\t', 'R', '\006', 's', 'y', 'n', 't', 'a', 'x', '\"', '\271', '\006', '\n', '\017',
  2672. 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022', '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001',
  2673. ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e', '\022', ';', '\n', '\005', 'f', 'i', 'e', 'l', 'd', '\030', '\002', ' ', '\003', '(', '\013',
  2674. '2', '%', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'D',
  2675. 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\005', 'f', 'i', 'e', 'l', 'd', '\022', 'C', '\n', '\t',
  2676. 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\030', '\006', ' ', '\003', '(', '\013', '2', '%', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.',
  2677. 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P',
  2678. 'r', 'o', 't', 'o', 'R', '\t', 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\022', 'A', '\n', '\013', 'n', 'e', 's', 't', 'e', 'd',
  2679. '_', 't', 'y', 'p', 'e', '\030', '\003', ' ', '\003', '(', '\013', '2', ' ', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't',
  2680. 'o', 'b', 'u', 'f', '.', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\n', 'n', 'e', 's',
  2681. 't', 'e', 'd', 'T', 'y', 'p', 'e', '\022', 'A', '\n', '\t', 'e', 'n', 'u', 'm', '_', 't', 'y', 'p', 'e', '\030', '\004', ' ', '\003', '(',
  2682. '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'E', 'n', 'u', 'm', 'D',
  2683. 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\010', 'e', 'n', 'u', 'm', 'T', 'y', 'p', 'e', '\022',
  2684. 'X', '\n', '\017', 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '_', 'r', 'a', 'n', 'g', 'e', '\030', '\005', ' ', '\003', '(', '\013', '2',
  2685. '/', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'D', 'e', 's', 'c', 'r', 'i', 'p',
  2686. 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '.', 'E', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', 'R', 'a', 'n', 'g', 'e', 'R', '\016',
  2687. 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', 'R', 'a', 'n', 'g', 'e', '\022', 'D', '\n', '\n', 'o', 'n', 'e', 'o', 'f', '_', 'd',
  2688. 'e', 'c', 'l', '\030', '\010', ' ', '\003', '(', '\013', '2', '%', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b',
  2689. 'u', 'f', '.', 'O', 'n', 'e', 'o', 'f', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\t',
  2690. 'o', 'n', 'e', 'o', 'f', 'D', 'e', 'c', 'l', '\022', '9', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\030', '\007', ' ', '\001', '(',
  2691. '\013', '2', '\037', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'M', 'e', 's', 's', 'a',
  2692. 'g', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\022', 'U', '\n', '\016', 'r', 'e', 's',
  2693. 'e', 'r', 'v', 'e', 'd', '_', 'r', 'a', 'n', 'g', 'e', '\030', '\t', ' ', '\003', '(', '\013', '2', '.', '.', 'g', 'o', 'o', 'g', 'l',
  2694. 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't',
  2695. 'o', '.', 'R', 'e', 's', 'e', 'r', 'v', 'e', 'd', 'R', 'a', 'n', 'g', 'e', 'R', '\r', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd',
  2696. 'R', 'a', 'n', 'g', 'e', '\022', '#', '\n', '\r', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd', '_', 'n', 'a', 'm', 'e', '\030', '\n', ' ',
  2697. '\003', '(', '\t', 'R', '\014', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd', 'N', 'a', 'm', 'e', '\032', 'z', '\n', '\016', 'E', 'x', 't', 'e',
  2698. 'n', 's', 'i', 'o', 'n', 'R', 'a', 'n', 'g', 'e', '\022', '\024', '\n', '\005', 's', 't', 'a', 'r', 't', '\030', '\001', ' ', '\001', '(', '\005',
  2699. 'R', '\005', 's', 't', 'a', 'r', 't', '\022', '\020', '\n', '\003', 'e', 'n', 'd', '\030', '\002', ' ', '\001', '(', '\005', 'R', '\003', 'e', 'n', 'd',
  2700. '\022', '@', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\030', '\003', ' ', '\001', '(', '\013', '2', '&', '.', 'g', 'o', 'o', 'g', 'l',
  2701. 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'E', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', 'R', 'a', 'n', 'g', 'e',
  2702. 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\032', '7', '\n', '\r', 'R', 'e', 's', 'e', 'r',
  2703. 'v', 'e', 'd', 'R', 'a', 'n', 'g', 'e', '\022', '\024', '\n', '\005', 's', 't', 'a', 'r', 't', '\030', '\001', ' ', '\001', '(', '\005', 'R', '\005',
  2704. 's', 't', 'a', 'r', 't', '\022', '\020', '\n', '\003', 'e', 'n', 'd', '\030', '\002', ' ', '\001', '(', '\005', 'R', '\003', 'e', 'n', 'd', '\"', '|',
  2705. '\n', '\025', 'E', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', 'R', 'a', 'n', 'g', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', 'X',
  2706. '\n', '\024', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007',
  2707. ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n',
  2708. 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e',
  2709. 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002', '\"',
  2710. '\301', '\006', '\n', '\024', 'F', 'i', 'e', 'l', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022',
  2711. '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e', '\022', '\026', '\n', '\006', 'n', 'u',
  2712. 'm', 'b', 'e', 'r', '\030', '\003', ' ', '\001', '(', '\005', 'R', '\006', 'n', 'u', 'm', 'b', 'e', 'r', '\022', 'A', '\n', '\005', 'l', 'a', 'b',
  2713. 'e', 'l', '\030', '\004', ' ', '\001', '(', '\016', '2', '+', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u',
  2714. 'f', '.', 'F', 'i', 'e', 'l', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '.', 'L', 'a',
  2715. 'b', 'e', 'l', 'R', '\005', 'l', 'a', 'b', 'e', 'l', '\022', '>', '\n', '\004', 't', 'y', 'p', 'e', '\030', '\005', ' ', '\001', '(', '\016', '2',
  2716. '*', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'D', 'e',
  2717. 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '.', 'T', 'y', 'p', 'e', 'R', '\004', 't', 'y', 'p', 'e', '\022',
  2718. '\033', '\n', '\t', 't', 'y', 'p', 'e', '_', 'n', 'a', 'm', 'e', '\030', '\006', ' ', '\001', '(', '\t', 'R', '\010', 't', 'y', 'p', 'e', 'N',
  2719. 'a', 'm', 'e', '\022', '\032', '\n', '\010', 'e', 'x', 't', 'e', 'n', 'd', 'e', 'e', '\030', '\002', ' ', '\001', '(', '\t', 'R', '\010', 'e', 'x',
  2720. 't', 'e', 'n', 'd', 'e', 'e', '\022', '#', '\n', '\r', 'd', 'e', 'f', 'a', 'u', 'l', 't', '_', 'v', 'a', 'l', 'u', 'e', '\030', '\007',
  2721. ' ', '\001', '(', '\t', 'R', '\014', 'd', 'e', 'f', 'a', 'u', 'l', 't', 'V', 'a', 'l', 'u', 'e', '\022', '\037', '\n', '\013', 'o', 'n', 'e',
  2722. 'o', 'f', '_', 'i', 'n', 'd', 'e', 'x', '\030', '\t', ' ', '\001', '(', '\005', 'R', '\n', 'o', 'n', 'e', 'o', 'f', 'I', 'n', 'd', 'e',
  2723. 'x', '\022', '\033', '\n', '\t', 'j', 's', 'o', 'n', '_', 'n', 'a', 'm', 'e', '\030', '\n', ' ', '\001', '(', '\t', 'R', '\010', 'j', 's', 'o',
  2724. 'n', 'N', 'a', 'm', 'e', '\022', '7', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\030', '\010', ' ', '\001', '(', '\013', '2', '\035', '.',
  2725. 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'O', 'p', 't', 'i',
  2726. 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\022', '\'', '\n', '\017', 'p', 'r', 'o', 't', 'o', '3', '_', 'o', 'p',
  2727. 't', 'i', 'o', 'n', 'a', 'l', '\030', '\021', ' ', '\001', '(', '\010', 'R', '\016', 'p', 'r', 'o', 't', 'o', '3', 'O', 'p', 't', 'i', 'o',
  2728. 'n', 'a', 'l', '\"', '\266', '\002', '\n', '\004', 'T', 'y', 'p', 'e', '\022', '\017', '\n', '\013', 'T', 'Y', 'P', 'E', '_', 'D', 'O', 'U', 'B',
  2729. 'L', 'E', '\020', '\001', '\022', '\016', '\n', '\n', 'T', 'Y', 'P', 'E', '_', 'F', 'L', 'O', 'A', 'T', '\020', '\002', '\022', '\016', '\n', '\n', 'T',
  2730. 'Y', 'P', 'E', '_', 'I', 'N', 'T', '6', '4', '\020', '\003', '\022', '\017', '\n', '\013', 'T', 'Y', 'P', 'E', '_', 'U', 'I', 'N', 'T', '6',
  2731. '4', '\020', '\004', '\022', '\016', '\n', '\n', 'T', 'Y', 'P', 'E', '_', 'I', 'N', 'T', '3', '2', '\020', '\005', '\022', '\020', '\n', '\014', 'T', 'Y',
  2732. 'P', 'E', '_', 'F', 'I', 'X', 'E', 'D', '6', '4', '\020', '\006', '\022', '\020', '\n', '\014', 'T', 'Y', 'P', 'E', '_', 'F', 'I', 'X', 'E',
  2733. 'D', '3', '2', '\020', '\007', '\022', '\r', '\n', '\t', 'T', 'Y', 'P', 'E', '_', 'B', 'O', 'O', 'L', '\020', '\010', '\022', '\017', '\n', '\013', 'T',
  2734. 'Y', 'P', 'E', '_', 'S', 'T', 'R', 'I', 'N', 'G', '\020', '\t', '\022', '\016', '\n', '\n', 'T', 'Y', 'P', 'E', '_', 'G', 'R', 'O', 'U',
  2735. 'P', '\020', '\n', '\022', '\020', '\n', '\014', 'T', 'Y', 'P', 'E', '_', 'M', 'E', 'S', 'S', 'A', 'G', 'E', '\020', '\013', '\022', '\016', '\n', '\n',
  2736. 'T', 'Y', 'P', 'E', '_', 'B', 'Y', 'T', 'E', 'S', '\020', '\014', '\022', '\017', '\n', '\013', 'T', 'Y', 'P', 'E', '_', 'U', 'I', 'N', 'T',
  2737. '3', '2', '\020', '\r', '\022', '\r', '\n', '\t', 'T', 'Y', 'P', 'E', '_', 'E', 'N', 'U', 'M', '\020', '\016', '\022', '\021', '\n', '\r', 'T', 'Y',
  2738. 'P', 'E', '_', 'S', 'F', 'I', 'X', 'E', 'D', '3', '2', '\020', '\017', '\022', '\021', '\n', '\r', 'T', 'Y', 'P', 'E', '_', 'S', 'F', 'I',
  2739. 'X', 'E', 'D', '6', '4', '\020', '\020', '\022', '\017', '\n', '\013', 'T', 'Y', 'P', 'E', '_', 'S', 'I', 'N', 'T', '3', '2', '\020', '\021', '\022',
  2740. '\017', '\n', '\013', 'T', 'Y', 'P', 'E', '_', 'S', 'I', 'N', 'T', '6', '4', '\020', '\022', '\"', 'C', '\n', '\005', 'L', 'a', 'b', 'e', 'l',
  2741. '\022', '\022', '\n', '\016', 'L', 'A', 'B', 'E', 'L', '_', 'O', 'P', 'T', 'I', 'O', 'N', 'A', 'L', '\020', '\001', '\022', '\022', '\n', '\016', 'L',
  2742. 'A', 'B', 'E', 'L', '_', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D', '\020', '\002', '\022', '\022', '\n', '\016', 'L', 'A', 'B', 'E', 'L', '_',
  2743. 'R', 'E', 'P', 'E', 'A', 'T', 'E', 'D', '\020', '\003', '\"', 'c', '\n', '\024', 'O', 'n', 'e', 'o', 'f', 'D', 'e', 's', 'c', 'r', 'i',
  2744. 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022', '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004',
  2745. 'n', 'a', 'm', 'e', '\022', '7', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\030', '\002', ' ', '\001', '(', '\013', '2', '\035', '.', 'g',
  2746. 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'O', 'n', 'e', 'o', 'f', 'O', 'p', 't', 'i', 'o',
  2747. 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\"', '\343', '\002', '\n', '\023', 'E', 'n', 'u', 'm', 'D', 'e', 's', 'c', 'r',
  2748. 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022', '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R',
  2749. '\004', 'n', 'a', 'm', 'e', '\022', '?', '\n', '\005', 'v', 'a', 'l', 'u', 'e', '\030', '\002', ' ', '\003', '(', '\013', '2', ')', '.', 'g', 'o',
  2750. 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'E', 'n', 'u', 'm', 'V', 'a', 'l', 'u', 'e', 'D', 'e',
  2751. 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\005', 'v', 'a', 'l', 'u', 'e', '\022', '6', '\n', '\007', 'o',
  2752. 'p', 't', 'i', 'o', 'n', 's', '\030', '\003', ' ', '\001', '(', '\013', '2', '\034', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o',
  2753. 't', 'o', 'b', 'u', 'f', '.', 'E', 'n', 'u', 'm', 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n',
  2754. 's', '\022', ']', '\n', '\016', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd', '_', 'r', 'a', 'n', 'g', 'e', '\030', '\004', ' ', '\003', '(', '\013',
  2755. '2', '6', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'E', 'n', 'u', 'm', 'D', 'e',
  2756. 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '.', 'E', 'n', 'u', 'm', 'R', 'e', 's', 'e', 'r', 'v', 'e',
  2757. 'd', 'R', 'a', 'n', 'g', 'e', 'R', '\r', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd', 'R', 'a', 'n', 'g', 'e', '\022', '#', '\n', '\r',
  2758. 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd', '_', 'n', 'a', 'm', 'e', '\030', '\005', ' ', '\003', '(', '\t', 'R', '\014', 'r', 'e', 's', 'e',
  2759. 'r', 'v', 'e', 'd', 'N', 'a', 'm', 'e', '\032', ';', '\n', '\021', 'E', 'n', 'u', 'm', 'R', 'e', 's', 'e', 'r', 'v', 'e', 'd', 'R',
  2760. 'a', 'n', 'g', 'e', '\022', '\024', '\n', '\005', 's', 't', 'a', 'r', 't', '\030', '\001', ' ', '\001', '(', '\005', 'R', '\005', 's', 't', 'a', 'r',
  2761. 't', '\022', '\020', '\n', '\003', 'e', 'n', 'd', '\030', '\002', ' ', '\001', '(', '\005', 'R', '\003', 'e', 'n', 'd', '\"', '\203', '\001', '\n', '\030', 'E',
  2762. 'n', 'u', 'm', 'V', 'a', 'l', 'u', 'e', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022', '\022',
  2763. '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e', '\022', '\026', '\n', '\006', 'n', 'u', 'm',
  2764. 'b', 'e', 'r', '\030', '\002', ' ', '\001', '(', '\005', 'R', '\006', 'n', 'u', 'm', 'b', 'e', 'r', '\022', ';', '\n', '\007', 'o', 'p', 't', 'i',
  2765. 'o', 'n', 's', '\030', '\003', ' ', '\001', '(', '\013', '2', '!', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b',
  2766. 'u', 'f', '.', 'E', 'n', 'u', 'm', 'V', 'a', 'l', 'u', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i',
  2767. 'o', 'n', 's', '\"', '\247', '\001', '\n', '\026', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r',
  2768. 'P', 'r', 'o', 't', 'o', '\022', '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e',
  2769. '\022', '>', '\n', '\006', 'm', 'e', 't', 'h', 'o', 'd', '\030', '\002', ' ', '\003', '(', '\013', '2', '&', '.', 'g', 'o', 'o', 'g', 'l', 'e',
  2770. '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'M', 'e', 't', 'h', 'o', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o',
  2771. 'r', 'P', 'r', 'o', 't', 'o', 'R', '\006', 'm', 'e', 't', 'h', 'o', 'd', '\022', '9', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's',
  2772. '\030', '\003', ' ', '\001', '(', '\013', '2', '\037', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.',
  2773. 'S', 'e', 'r', 'v', 'i', 'c', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\"', '\211',
  2774. '\002', '\n', '\025', 'M', 'e', 't', 'h', 'o', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022',
  2775. '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e', '\022', '\035', '\n', '\n', 'i', 'n',
  2776. 'p', 'u', 't', '_', 't', 'y', 'p', 'e', '\030', '\002', ' ', '\001', '(', '\t', 'R', '\t', 'i', 'n', 'p', 'u', 't', 'T', 'y', 'p', 'e',
  2777. '\022', '\037', '\n', '\013', 'o', 'u', 't', 'p', 'u', 't', '_', 't', 'y', 'p', 'e', '\030', '\003', ' ', '\001', '(', '\t', 'R', '\n', 'o', 'u',
  2778. 't', 'p', 'u', 't', 'T', 'y', 'p', 'e', '\022', '8', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\030', '\004', ' ', '\001', '(', '\013',
  2779. '2', '\036', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'M', 'e', 't', 'h', 'o', 'd',
  2780. 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\022', '0', '\n', '\020', 'c', 'l', 'i', 'e', 'n',
  2781. 't', '_', 's', 't', 'r', 'e', 'a', 'm', 'i', 'n', 'g', '\030', '\005', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R',
  2782. '\017', 'c', 'l', 'i', 'e', 'n', 't', 'S', 't', 'r', 'e', 'a', 'm', 'i', 'n', 'g', '\022', '0', '\n', '\020', 's', 'e', 'r', 'v', 'e',
  2783. 'r', '_', 's', 't', 'r', 'e', 'a', 'm', 'i', 'n', 'g', '\030', '\006', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R',
  2784. '\017', 's', 'e', 'r', 'v', 'e', 'r', 'S', 't', 'r', 'e', 'a', 'm', 'i', 'n', 'g', '\"', '\221', '\t', '\n', '\013', 'F', 'i', 'l', 'e',
  2785. 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', '!', '\n', '\014', 'j', 'a', 'v', 'a', '_', 'p', 'a', 'c', 'k', 'a', 'g', 'e', '\030', '\001',
  2786. ' ', '\001', '(', '\t', 'R', '\013', 'j', 'a', 'v', 'a', 'P', 'a', 'c', 'k', 'a', 'g', 'e', '\022', '0', '\n', '\024', 'j', 'a', 'v', 'a',
  2787. '_', 'o', 'u', 't', 'e', 'r', '_', 'c', 'l', 'a', 's', 's', 'n', 'a', 'm', 'e', '\030', '\010', ' ', '\001', '(', '\t', 'R', '\022', 'j',
  2788. 'a', 'v', 'a', 'O', 'u', 't', 'e', 'r', 'C', 'l', 'a', 's', 's', 'n', 'a', 'm', 'e', '\022', '5', '\n', '\023', 'j', 'a', 'v', 'a',
  2789. '_', 'm', 'u', 'l', 't', 'i', 'p', 'l', 'e', '_', 'f', 'i', 'l', 'e', 's', '\030', '\n', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a',
  2790. 'l', 's', 'e', 'R', '\021', 'j', 'a', 'v', 'a', 'M', 'u', 'l', 't', 'i', 'p', 'l', 'e', 'F', 'i', 'l', 'e', 's', '\022', 'D', '\n',
  2791. '\035', 'j', 'a', 'v', 'a', '_', 'g', 'e', 'n', 'e', 'r', 'a', 't', 'e', '_', 'e', 'q', 'u', 'a', 'l', 's', '_', 'a', 'n', 'd',
  2792. '_', 'h', 'a', 's', 'h', '\030', '\024', ' ', '\001', '(', '\010', 'B', '\002', '\030', '\001', 'R', '\031', 'j', 'a', 'v', 'a', 'G', 'e', 'n', 'e',
  2793. 'r', 'a', 't', 'e', 'E', 'q', 'u', 'a', 'l', 's', 'A', 'n', 'd', 'H', 'a', 's', 'h', '\022', ':', '\n', '\026', 'j', 'a', 'v', 'a',
  2794. '_', 's', 't', 'r', 'i', 'n', 'g', '_', 'c', 'h', 'e', 'c', 'k', '_', 'u', 't', 'f', '8', '\030', '\033', ' ', '\001', '(', '\010', ':',
  2795. '\005', 'f', 'a', 'l', 's', 'e', 'R', '\023', 'j', 'a', 'v', 'a', 'S', 't', 'r', 'i', 'n', 'g', 'C', 'h', 'e', 'c', 'k', 'U', 't',
  2796. 'f', '8', '\022', 'S', '\n', '\014', 'o', 'p', 't', 'i', 'm', 'i', 'z', 'e', '_', 'f', 'o', 'r', '\030', '\t', ' ', '\001', '(', '\016', '2',
  2797. ')', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'l', 'e', 'O', 'p', 't',
  2798. 'i', 'o', 'n', 's', '.', 'O', 'p', 't', 'i', 'm', 'i', 'z', 'e', 'M', 'o', 'd', 'e', ':', '\005', 'S', 'P', 'E', 'E', 'D', 'R',
  2799. '\013', 'o', 'p', 't', 'i', 'm', 'i', 'z', 'e', 'F', 'o', 'r', '\022', '\035', '\n', '\n', 'g', 'o', '_', 'p', 'a', 'c', 'k', 'a', 'g',
  2800. 'e', '\030', '\013', ' ', '\001', '(', '\t', 'R', '\t', 'g', 'o', 'P', 'a', 'c', 'k', 'a', 'g', 'e', '\022', '5', '\n', '\023', 'c', 'c', '_',
  2801. 'g', 'e', 'n', 'e', 'r', 'i', 'c', '_', 's', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\030', '\020', ' ', '\001', '(', '\010', ':', '\005', 'f',
  2802. 'a', 'l', 's', 'e', 'R', '\021', 'c', 'c', 'G', 'e', 'n', 'e', 'r', 'i', 'c', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\022', '9',
  2803. '\n', '\025', 'j', 'a', 'v', 'a', '_', 'g', 'e', 'n', 'e', 'r', 'i', 'c', '_', 's', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\030', '\021',
  2804. ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\023', 'j', 'a', 'v', 'a', 'G', 'e', 'n', 'e', 'r', 'i', 'c', 'S',
  2805. 'e', 'r', 'v', 'i', 'c', 'e', 's', '\022', '5', '\n', '\023', 'p', 'y', '_', 'g', 'e', 'n', 'e', 'r', 'i', 'c', '_', 's', 'e', 'r',
  2806. 'v', 'i', 'c', 'e', 's', '\030', '\022', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\021', 'p', 'y', 'G', 'e', 'n',
  2807. 'e', 'r', 'i', 'c', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\022', '7', '\n', '\024', 'p', 'h', 'p', '_', 'g', 'e', 'n', 'e', 'r',
  2808. 'i', 'c', '_', 's', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\030', '*', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R',
  2809. '\022', 'p', 'h', 'p', 'G', 'e', 'n', 'e', 'r', 'i', 'c', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\022', '%', '\n', '\n', 'd', 'e',
  2810. 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\030', '\027', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n', 'd', 'e',
  2811. 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\022', '.', '\n', '\020', 'c', 'c', '_', 'e', 'n', 'a', 'b', 'l', 'e', '_', 'a', 'r', 'e',
  2812. 'n', 'a', 's', '\030', '\037', ' ', '\001', '(', '\010', ':', '\004', 't', 'r', 'u', 'e', 'R', '\016', 'c', 'c', 'E', 'n', 'a', 'b', 'l', 'e',
  2813. 'A', 'r', 'e', 'n', 'a', 's', '\022', '*', '\n', '\021', 'o', 'b', 'j', 'c', '_', 'c', 'l', 'a', 's', 's', '_', 'p', 'r', 'e', 'f',
  2814. 'i', 'x', '\030', '$', ' ', '\001', '(', '\t', 'R', '\017', 'o', 'b', 'j', 'c', 'C', 'l', 'a', 's', 's', 'P', 'r', 'e', 'f', 'i', 'x',
  2815. '\022', ')', '\n', '\020', 'c', 's', 'h', 'a', 'r', 'p', '_', 'n', 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e', '\030', '%', ' ', '\001', '(',
  2816. '\t', 'R', '\017', 'c', 's', 'h', 'a', 'r', 'p', 'N', 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e', '\022', '!', '\n', '\014', 's', 'w', 'i',
  2817. 'f', 't', '_', 'p', 'r', 'e', 'f', 'i', 'x', '\030', '\'', ' ', '\001', '(', '\t', 'R', '\013', 's', 'w', 'i', 'f', 't', 'P', 'r', 'e',
  2818. 'f', 'i', 'x', '\022', '(', '\n', '\020', 'p', 'h', 'p', '_', 'c', 'l', 'a', 's', 's', '_', 'p', 'r', 'e', 'f', 'i', 'x', '\030', '(',
  2819. ' ', '\001', '(', '\t', 'R', '\016', 'p', 'h', 'p', 'C', 'l', 'a', 's', 's', 'P', 'r', 'e', 'f', 'i', 'x', '\022', '#', '\n', '\r', 'p',
  2820. 'h', 'p', '_', 'n', 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e', '\030', ')', ' ', '\001', '(', '\t', 'R', '\014', 'p', 'h', 'p', 'N', 'a',
  2821. 'm', 'e', 's', 'p', 'a', 'c', 'e', '\022', '4', '\n', '\026', 'p', 'h', 'p', '_', 'm', 'e', 't', 'a', 'd', 'a', 't', 'a', '_', 'n',
  2822. 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e', '\030', ',', ' ', '\001', '(', '\t', 'R', '\024', 'p', 'h', 'p', 'M', 'e', 't', 'a', 'd', 'a',
  2823. 't', 'a', 'N', 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e', '\022', '!', '\n', '\014', 'r', 'u', 'b', 'y', '_', 'p', 'a', 'c', 'k', 'a',
  2824. 'g', 'e', '\030', '-', ' ', '\001', '(', '\t', 'R', '\013', 'r', 'u', 'b', 'y', 'P', 'a', 'c', 'k', 'a', 'g', 'e', '\022', 'X', '\n', '\024',
  2825. 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003',
  2826. '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n',
  2827. 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p',
  2828. 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '\"', ':', '\n', '\014', 'O', 'p', 't', 'i', 'm', 'i', 'z', 'e', 'M', 'o',
  2829. 'd', 'e', '\022', '\t', '\n', '\005', 'S', 'P', 'E', 'E', 'D', '\020', '\001', '\022', '\r', '\n', '\t', 'C', 'O', 'D', 'E', '_', 'S', 'I', 'Z',
  2830. 'E', '\020', '\002', '\022', '\020', '\n', '\014', 'L', 'I', 'T', 'E', '_', 'R', 'U', 'N', 'T', 'I', 'M', 'E', '\020', '\003', '*', '\t', '\010', '\350',
  2831. '\007', '\020', '\200', '\200', '\200', '\200', '\002', 'J', '\004', '\010', '&', '\020', '\'', '\"', '\321', '\002', '\n', '\016', 'M', 'e', 's', 's', 'a', 'g', 'e',
  2832. 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', '<', '\n', '\027', 'm', 'e', 's', 's', 'a', 'g', 'e', '_', 's', 'e', 't', '_', 'w', 'i',
  2833. 'r', 'e', '_', 'f', 'o', 'r', 'm', 'a', 't', '\030', '\001', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\024', 'm',
  2834. 'e', 's', 's', 'a', 'g', 'e', 'S', 'e', 't', 'W', 'i', 'r', 'e', 'F', 'o', 'r', 'm', 'a', 't', '\022', 'L', '\n', '\037', 'n', 'o',
  2835. '_', 's', 't', 'a', 'n', 'd', 'a', 'r', 'd', '_', 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', '_', 'a', 'c', 'c', 'e',
  2836. 's', 's', 'o', 'r', '\030', '\002', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\034', 'n', 'o', 'S', 't', 'a', 'n',
  2837. 'd', 'a', 'r', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'A', 'c', 'c', 'e', 's', 's', 'o', 'r', '\022', '%', '\n',
  2838. '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\030', '\003', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R',
  2839. '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\022', '\033', '\n', '\t', 'm', 'a', 'p', '_', 'e', 'n', 't', 'r', 'y', '\030',
  2840. '\007', ' ', '\001', '(', '\010', 'R', '\010', 'm', 'a', 'p', 'E', 'n', 't', 'r', 'y', '\022', 'X', '\n', '\024', 'u', 'n', 'i', 'n', 't', 'e',
  2841. 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2', '$', '.', 'g',
  2842. 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e',
  2843. 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O',
  2844. 'p', 't', 'i', 'o', 'n', '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002', 'J', '\004', '\010', '\010', '\020', '\t', 'J', '\004', '\010',
  2845. '\t', '\020', '\n', '\"', '\342', '\003', '\n', '\014', 'F', 'i', 'e', 'l', 'd', 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', 'A', '\n', '\005', 'c',
  2846. 't', 'y', 'p', 'e', '\030', '\001', ' ', '\001', '(', '\016', '2', '#', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o',
  2847. 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'O', 'p', 't', 'i', 'o', 'n', 's', '.', 'C', 'T', 'y', 'p', 'e', ':', '\006', 'S',
  2848. 'T', 'R', 'I', 'N', 'G', 'R', '\005', 'c', 't', 'y', 'p', 'e', '\022', '\026', '\n', '\006', 'p', 'a', 'c', 'k', 'e', 'd', '\030', '\002', ' ',
  2849. '\001', '(', '\010', 'R', '\006', 'p', 'a', 'c', 'k', 'e', 'd', '\022', 'G', '\n', '\006', 'j', 's', 't', 'y', 'p', 'e', '\030', '\006', ' ', '\001',
  2850. '(', '\016', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l',
  2851. 'd', 'O', 'p', 't', 'i', 'o', 'n', 's', '.', 'J', 'S', 'T', 'y', 'p', 'e', ':', '\t', 'J', 'S', '_', 'N', 'O', 'R', 'M', 'A',
  2852. 'L', 'R', '\006', 'j', 's', 't', 'y', 'p', 'e', '\022', '\031', '\n', '\004', 'l', 'a', 'z', 'y', '\030', '\005', ' ', '\001', '(', '\010', ':', '\005',
  2853. 'f', 'a', 'l', 's', 'e', 'R', '\004', 'l', 'a', 'z', 'y', '\022', '%', '\n', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd',
  2854. '\030', '\003', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd',
  2855. '\022', '\031', '\n', '\004', 'w', 'e', 'a', 'k', '\030', '\n', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\004', 'w', 'e',
  2856. 'a', 'k', '\022', 'X', '\n', '\024', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o',
  2857. 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u',
  2858. 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n',
  2859. 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '\"', '/', '\n', '\005', 'C', 'T', 'y', 'p',
  2860. 'e', '\022', '\n', '\n', '\006', 'S', 'T', 'R', 'I', 'N', 'G', '\020', '\000', '\022', '\010', '\n', '\004', 'C', 'O', 'R', 'D', '\020', '\001', '\022', '\020',
  2861. '\n', '\014', 'S', 'T', 'R', 'I', 'N', 'G', '_', 'P', 'I', 'E', 'C', 'E', '\020', '\002', '\"', '5', '\n', '\006', 'J', 'S', 'T', 'y', 'p',
  2862. 'e', '\022', '\r', '\n', '\t', 'J', 'S', '_', 'N', 'O', 'R', 'M', 'A', 'L', '\020', '\000', '\022', '\r', '\n', '\t', 'J', 'S', '_', 'S', 'T',
  2863. 'R', 'I', 'N', 'G', '\020', '\001', '\022', '\r', '\n', '\t', 'J', 'S', '_', 'N', 'U', 'M', 'B', 'E', 'R', '\020', '\002', '*', '\t', '\010', '\350',
  2864. '\007', '\020', '\200', '\200', '\200', '\200', '\002', 'J', '\004', '\010', '\004', '\020', '\005', '\"', 's', '\n', '\014', 'O', 'n', 'e', 'o', 'f', 'O', 'p', 't',
  2865. 'i', 'o', 'n', 's', '\022', 'X', '\n', '\024', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't',
  2866. 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o',
  2867. 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023',
  2868. 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '*', '\t', '\010', '\350', '\007', '\020',
  2869. '\200', '\200', '\200', '\200', '\002', '\"', '\300', '\001', '\n', '\013', 'E', 'n', 'u', 'm', 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', '\037', '\n', '\013',
  2870. 'a', 'l', 'l', 'o', 'w', '_', 'a', 'l', 'i', 'a', 's', '\030', '\002', ' ', '\001', '(', '\010', 'R', '\n', 'a', 'l', 'l', 'o', 'w', 'A',
  2871. 'l', 'i', 'a', 's', '\022', '%', '\n', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\030', '\003', ' ', '\001', '(', '\010', ':',
  2872. '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\022', 'X', '\n', '\024', 'u', 'n', 'i',
  2873. 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2',
  2874. '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r',
  2875. 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't',
  2876. 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002', 'J', '\004', '\010', '\005', '\020', '\006',
  2877. '\"', '\236', '\001', '\n', '\020', 'E', 'n', 'u', 'm', 'V', 'a', 'l', 'u', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', '%', '\n', '\n',
  2878. 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\030', '\001', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n',
  2879. 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\022', 'X', '\n', '\024', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't',
  2880. 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e',
  2881. '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p',
  2882. 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n',
  2883. '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002', '\"', '\234', '\001', '\n', '\016', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 'O', 'p',
  2884. 't', 'i', 'o', 'n', 's', '\022', '%', '\n', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\030', '!', ' ', '\001', '(', '\010',
  2885. ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\022', 'X', '\n', '\024', 'u', 'n',
  2886. 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013',
  2887. '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e',
  2888. 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e',
  2889. 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002', '\"', '\340', '\002', '\n', '\r',
  2890. 'M', 'e', 't', 'h', 'o', 'd', 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', '%', '\n', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't',
  2891. 'e', 'd', '\030', '!', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't',
  2892. 'e', 'd', '\022', 'q', '\n', '\021', 'i', 'd', 'e', 'm', 'p', 'o', 't', 'e', 'n', 'c', 'y', '_', 'l', 'e', 'v', 'e', 'l', '\030', '\"',
  2893. ' ', '\001', '(', '\016', '2', '/', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'M', 'e',
  2894. 't', 'h', 'o', 'd', 'O', 'p', 't', 'i', 'o', 'n', 's', '.', 'I', 'd', 'e', 'm', 'p', 'o', 't', 'e', 'n', 'c', 'y', 'L', 'e',
  2895. 'v', 'e', 'l', ':', '\023', 'I', 'D', 'E', 'M', 'P', 'O', 'T', 'E', 'N', 'C', 'Y', '_', 'U', 'N', 'K', 'N', 'O', 'W', 'N', 'R',
  2896. '\020', 'i', 'd', 'e', 'm', 'p', 'o', 't', 'e', 'n', 'c', 'y', 'L', 'e', 'v', 'e', 'l', '\022', 'X', '\n', '\024', 'u', 'n', 'i', 'n',
  2897. 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2', '$',
  2898. '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p',
  2899. 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e',
  2900. 'd', 'O', 'p', 't', 'i', 'o', 'n', '\"', 'P', '\n', '\020', 'I', 'd', 'e', 'm', 'p', 'o', 't', 'e', 'n', 'c', 'y', 'L', 'e', 'v',
  2901. 'e', 'l', '\022', '\027', '\n', '\023', 'I', 'D', 'E', 'M', 'P', 'O', 'T', 'E', 'N', 'C', 'Y', '_', 'U', 'N', 'K', 'N', 'O', 'W', 'N',
  2902. '\020', '\000', '\022', '\023', '\n', '\017', 'N', 'O', '_', 'S', 'I', 'D', 'E', '_', 'E', 'F', 'F', 'E', 'C', 'T', 'S', '\020', '\001', '\022', '\016',
  2903. '\n', '\n', 'I', 'D', 'E', 'M', 'P', 'O', 'T', 'E', 'N', 'T', '\020', '\002', '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002',
  2904. '\"', '\232', '\003', '\n', '\023', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '\022',
  2905. 'A', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\002', ' ', '\003', '(', '\013', '2', '-', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r',
  2906. 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o',
  2907. 'n', '.', 'N', 'a', 'm', 'e', 'P', 'a', 'r', 't', 'R', '\004', 'n', 'a', 'm', 'e', '\022', ')', '\n', '\020', 'i', 'd', 'e', 'n', 't',
  2908. 'i', 'f', 'i', 'e', 'r', '_', 'v', 'a', 'l', 'u', 'e', '\030', '\003', ' ', '\001', '(', '\t', 'R', '\017', 'i', 'd', 'e', 'n', 't', 'i',
  2909. 'f', 'i', 'e', 'r', 'V', 'a', 'l', 'u', 'e', '\022', ',', '\n', '\022', 'p', 'o', 's', 'i', 't', 'i', 'v', 'e', '_', 'i', 'n', 't',
  2910. '_', 'v', 'a', 'l', 'u', 'e', '\030', '\004', ' ', '\001', '(', '\004', 'R', '\020', 'p', 'o', 's', 'i', 't', 'i', 'v', 'e', 'I', 'n', 't',
  2911. 'V', 'a', 'l', 'u', 'e', '\022', ',', '\n', '\022', 'n', 'e', 'g', 'a', 't', 'i', 'v', 'e', '_', 'i', 'n', 't', '_', 'v', 'a', 'l',
  2912. 'u', 'e', '\030', '\005', ' ', '\001', '(', '\003', 'R', '\020', 'n', 'e', 'g', 'a', 't', 'i', 'v', 'e', 'I', 'n', 't', 'V', 'a', 'l', 'u',
  2913. 'e', '\022', '!', '\n', '\014', 'd', 'o', 'u', 'b', 'l', 'e', '_', 'v', 'a', 'l', 'u', 'e', '\030', '\006', ' ', '\001', '(', '\001', 'R', '\013',
  2914. 'd', 'o', 'u', 'b', 'l', 'e', 'V', 'a', 'l', 'u', 'e', '\022', '!', '\n', '\014', 's', 't', 'r', 'i', 'n', 'g', '_', 'v', 'a', 'l',
  2915. 'u', 'e', '\030', '\007', ' ', '\001', '(', '\014', 'R', '\013', 's', 't', 'r', 'i', 'n', 'g', 'V', 'a', 'l', 'u', 'e', '\022', '\'', '\n', '\017',
  2916. 'a', 'g', 'g', 'r', 'e', 'g', 'a', 't', 'e', '_', 'v', 'a', 'l', 'u', 'e', '\030', '\010', ' ', '\001', '(', '\t', 'R', '\016', 'a', 'g',
  2917. 'g', 'r', 'e', 'g', 'a', 't', 'e', 'V', 'a', 'l', 'u', 'e', '\032', 'J', '\n', '\010', 'N', 'a', 'm', 'e', 'P', 'a', 'r', 't', '\022',
  2918. '\033', '\n', '\t', 'n', 'a', 'm', 'e', '_', 'p', 'a', 'r', 't', '\030', '\001', ' ', '\002', '(', '\t', 'R', '\010', 'n', 'a', 'm', 'e', 'P',
  2919. 'a', 'r', 't', '\022', '!', '\n', '\014', 'i', 's', '_', 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\030', '\002', ' ', '\002', '(', '\010',
  2920. 'R', '\013', 'i', 's', 'E', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\"', '\247', '\002', '\n', '\016', 'S', 'o', 'u', 'r', 'c', 'e', 'C',
  2921. 'o', 'd', 'e', 'I', 'n', 'f', 'o', '\022', 'D', '\n', '\010', 'l', 'o', 'c', 'a', 't', 'i', 'o', 'n', '\030', '\001', ' ', '\003', '(', '\013',
  2922. '2', '(', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'S', 'o', 'u', 'r', 'c', 'e',
  2923. 'C', 'o', 'd', 'e', 'I', 'n', 'f', 'o', '.', 'L', 'o', 'c', 'a', 't', 'i', 'o', 'n', 'R', '\010', 'l', 'o', 'c', 'a', 't', 'i',
  2924. 'o', 'n', '\032', '\316', '\001', '\n', '\010', 'L', 'o', 'c', 'a', 't', 'i', 'o', 'n', '\022', '\026', '\n', '\004', 'p', 'a', 't', 'h', '\030', '\001',
  2925. ' ', '\003', '(', '\005', 'B', '\002', '\020', '\001', 'R', '\004', 'p', 'a', 't', 'h', '\022', '\026', '\n', '\004', 's', 'p', 'a', 'n', '\030', '\002', ' ',
  2926. '\003', '(', '\005', 'B', '\002', '\020', '\001', 'R', '\004', 's', 'p', 'a', 'n', '\022', ')', '\n', '\020', 'l', 'e', 'a', 'd', 'i', 'n', 'g', '_',
  2927. 'c', 'o', 'm', 'm', 'e', 'n', 't', 's', '\030', '\003', ' ', '\001', '(', '\t', 'R', '\017', 'l', 'e', 'a', 'd', 'i', 'n', 'g', 'C', 'o',
  2928. 'm', 'm', 'e', 'n', 't', 's', '\022', '+', '\n', '\021', 't', 'r', 'a', 'i', 'l', 'i', 'n', 'g', '_', 'c', 'o', 'm', 'm', 'e', 'n',
  2929. 't', 's', '\030', '\004', ' ', '\001', '(', '\t', 'R', '\020', 't', 'r', 'a', 'i', 'l', 'i', 'n', 'g', 'C', 'o', 'm', 'm', 'e', 'n', 't',
  2930. 's', '\022', ':', '\n', '\031', 'l', 'e', 'a', 'd', 'i', 'n', 'g', '_', 'd', 'e', 't', 'a', 'c', 'h', 'e', 'd', '_', 'c', 'o', 'm',
  2931. 'm', 'e', 'n', 't', 's', '\030', '\006', ' ', '\003', '(', '\t', 'R', '\027', 'l', 'e', 'a', 'd', 'i', 'n', 'g', 'D', 'e', 't', 'a', 'c',
  2932. 'h', 'e', 'd', 'C', 'o', 'm', 'm', 'e', 'n', 't', 's', '\"', '\321', '\001', '\n', '\021', 'G', 'e', 'n', 'e', 'r', 'a', 't', 'e', 'd',
  2933. 'C', 'o', 'd', 'e', 'I', 'n', 'f', 'o', '\022', 'M', '\n', '\n', 'a', 'n', 'n', 'o', 't', 'a', 't', 'i', 'o', 'n', '\030', '\001', ' ',
  2934. '\003', '(', '\013', '2', '-', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'G', 'e', 'n',
  2935. 'e', 'r', 'a', 't', 'e', 'd', 'C', 'o', 'd', 'e', 'I', 'n', 'f', 'o', '.', 'A', 'n', 'n', 'o', 't', 'a', 't', 'i', 'o', 'n',
  2936. 'R', '\n', 'a', 'n', 'n', 'o', 't', 'a', 't', 'i', 'o', 'n', '\032', 'm', '\n', '\n', 'A', 'n', 'n', 'o', 't', 'a', 't', 'i', 'o',
  2937. 'n', '\022', '\026', '\n', '\004', 'p', 'a', 't', 'h', '\030', '\001', ' ', '\003', '(', '\005', 'B', '\002', '\020', '\001', 'R', '\004', 'p', 'a', 't', 'h',
  2938. '\022', '\037', '\n', '\013', 's', 'o', 'u', 'r', 'c', 'e', '_', 'f', 'i', 'l', 'e', '\030', '\002', ' ', '\001', '(', '\t', 'R', '\n', 's', 'o',
  2939. 'u', 'r', 'c', 'e', 'F', 'i', 'l', 'e', '\022', '\024', '\n', '\005', 'b', 'e', 'g', 'i', 'n', '\030', '\003', ' ', '\001', '(', '\005', 'R', '\005',
  2940. 'b', 'e', 'g', 'i', 'n', '\022', '\020', '\n', '\003', 'e', 'n', 'd', '\030', '\004', ' ', '\001', '(', '\005', 'R', '\003', 'e', 'n', 'd', 'B', '\217',
  2941. '\001', '\n', '\023', 'c', 'o', 'm', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', 'B', '\020', 'D',
  2942. 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 's', 'H', '\001', 'Z', '>', 'g', 'i', 't', 'h', 'u', 'b',
  2943. '.', 'c', 'o', 'm', '/', 'g', 'o', 'l', 'a', 'n', 'g', '/', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '/', 'p', 'r', 'o', 't',
  2944. 'o', 'c', '-', 'g', 'e', 'n', '-', 'g', 'o', '/', 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', ';', 'd', 'e', 's', 'c',
  2945. 'r', 'i', 'p', 't', 'o', 'r', '\370', '\001', '\001', '\242', '\002', '\003', 'G', 'P', 'B', '\252', '\002', '\032', 'G', 'o', 'o', 'g', 'l', 'e', '.',
  2946. 'P', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'R', 'e', 'f', 'l', 'e', 'c', 't', 'i', 'o', 'n',
  2947. };
  2948. static upb_def_init *deps[1] = {
  2949. NULL
  2950. };
  2951. upb_def_init google_protobuf_descriptor_proto_upbdefinit = {
  2952. deps,
  2953. layouts,
  2954. "google/protobuf/descriptor.proto",
  2955. UPB_STRVIEW_INIT(descriptor, 7619)
  2956. };
  2957. #include <ctype.h>
  2958. #include <errno.h>
  2959. #include <stdlib.h>
  2960. #include <string.h>
  2961. typedef struct {
  2962. size_t len;
  2963. char str[1]; /* Null-terminated string data follows. */
  2964. } str_t;
  2965. static str_t *newstr(upb_alloc *alloc, const char *data, size_t len) {
  2966. str_t *ret = upb_malloc(alloc, sizeof(*ret) + len);
  2967. if (!ret) return NULL;
  2968. ret->len = len;
  2969. if (len) memcpy(ret->str, data, len);
  2970. ret->str[len] = '\0';
  2971. return ret;
  2972. }
  2973. struct upb_fielddef {
  2974. const upb_filedef *file;
  2975. const upb_msgdef *msgdef;
  2976. const char *full_name;
  2977. const char *json_name;
  2978. union {
  2979. int64_t sint;
  2980. uint64_t uint;
  2981. double dbl;
  2982. float flt;
  2983. bool boolean;
  2984. str_t *str;
  2985. } defaultval;
  2986. const upb_oneofdef *oneof;
  2987. union {
  2988. const upb_msgdef *msgdef;
  2989. const upb_enumdef *enumdef;
  2990. const google_protobuf_FieldDescriptorProto *unresolved;
  2991. } sub;
  2992. uint32_t number_;
  2993. uint16_t index_;
  2994. uint16_t layout_index;
  2995. uint32_t selector_base; /* Used to index into a upb::Handlers table. */
  2996. bool is_extension_;
  2997. bool lazy_;
  2998. bool packed_;
  2999. bool proto3_optional_;
  3000. upb_descriptortype_t type_;
  3001. upb_label_t label_;
  3002. };
  3003. struct upb_msgdef {
  3004. const upb_msglayout *layout;
  3005. const upb_filedef *file;
  3006. const char *full_name;
  3007. uint32_t selector_count;
  3008. uint32_t submsg_field_count;
  3009. /* Tables for looking up fields by number and name. */
  3010. upb_inttable itof;
  3011. upb_strtable ntof;
  3012. const upb_fielddef *fields;
  3013. const upb_oneofdef *oneofs;
  3014. int field_count;
  3015. int oneof_count;
  3016. int real_oneof_count;
  3017. /* Is this a map-entry message? */
  3018. bool map_entry;
  3019. upb_wellknowntype_t well_known_type;
  3020. /* TODO(haberman): proper extension ranges (there can be multiple). */
  3021. };
  3022. struct upb_enumdef {
  3023. const upb_filedef *file;
  3024. const char *full_name;
  3025. upb_strtable ntoi;
  3026. upb_inttable iton;
  3027. int32_t defaultval;
  3028. };
  3029. struct upb_oneofdef {
  3030. const upb_msgdef *parent;
  3031. const char *full_name;
  3032. uint32_t index;
  3033. upb_strtable ntof;
  3034. upb_inttable itof;
  3035. };
  3036. struct upb_filedef {
  3037. const char *name;
  3038. const char *package;
  3039. const char *phpprefix;
  3040. const char *phpnamespace;
  3041. upb_syntax_t syntax;
  3042. const upb_filedef **deps;
  3043. const upb_msgdef *msgs;
  3044. const upb_enumdef *enums;
  3045. const upb_fielddef *exts;
  3046. int dep_count;
  3047. int msg_count;
  3048. int enum_count;
  3049. int ext_count;
  3050. };
  3051. struct upb_symtab {
  3052. upb_arena *arena;
  3053. upb_strtable syms; /* full_name -> packed def ptr */
  3054. upb_strtable files; /* file_name -> upb_filedef* */
  3055. };
  3056. /* Inside a symtab we store tagged pointers to specific def types. */
  3057. typedef enum {
  3058. UPB_DEFTYPE_FIELD = 0,
  3059. /* Only inside symtab table. */
  3060. UPB_DEFTYPE_MSG = 1,
  3061. UPB_DEFTYPE_ENUM = 2,
  3062. /* Only inside message table. */
  3063. UPB_DEFTYPE_ONEOF = 1,
  3064. UPB_DEFTYPE_FIELD_JSONNAME = 2
  3065. } upb_deftype_t;
  3066. static const void *unpack_def(upb_value v, upb_deftype_t type) {
  3067. uintptr_t num = (uintptr_t)upb_value_getconstptr(v);
  3068. return (num & 3) == type ? (const void*)(num & ~3) : NULL;
  3069. }
  3070. static upb_value pack_def(const void *ptr, upb_deftype_t type) {
  3071. uintptr_t num = (uintptr_t)ptr | type;
  3072. return upb_value_constptr((const void*)num);
  3073. }
  3074. /* isalpha() etc. from <ctype.h> are locale-dependent, which we don't want. */
  3075. static bool upb_isbetween(char c, char low, char high) {
  3076. return c >= low && c <= high;
  3077. }
  3078. static bool upb_isletter(char c) {
  3079. return upb_isbetween(c, 'A', 'Z') || upb_isbetween(c, 'a', 'z') || c == '_';
  3080. }
  3081. static bool upb_isalphanum(char c) {
  3082. return upb_isletter(c) || upb_isbetween(c, '0', '9');
  3083. }
  3084. static bool upb_isident(upb_strview name, bool full, upb_status *s) {
  3085. const char *str = name.data;
  3086. size_t len = name.size;
  3087. bool start = true;
  3088. size_t i;
  3089. for (i = 0; i < len; i++) {
  3090. char c = str[i];
  3091. if (c == '.') {
  3092. if (start || !full) {
  3093. upb_status_seterrf(s, "invalid name: unexpected '.' (%s)", str);
  3094. return false;
  3095. }
  3096. start = true;
  3097. } else if (start) {
  3098. if (!upb_isletter(c)) {
  3099. upb_status_seterrf(
  3100. s, "invalid name: path components must start with a letter (%s)",
  3101. str);
  3102. return false;
  3103. }
  3104. start = false;
  3105. } else {
  3106. if (!upb_isalphanum(c)) {
  3107. upb_status_seterrf(s, "invalid name: non-alphanumeric character (%s)",
  3108. str);
  3109. return false;
  3110. }
  3111. }
  3112. }
  3113. return !start;
  3114. }
  3115. static const char *shortdefname(const char *fullname) {
  3116. const char *p;
  3117. if (fullname == NULL) {
  3118. return NULL;
  3119. } else if ((p = strrchr(fullname, '.')) == NULL) {
  3120. /* No '.' in the name, return the full string. */
  3121. return fullname;
  3122. } else {
  3123. /* Return one past the last '.'. */
  3124. return p + 1;
  3125. }
  3126. }
  3127. /* All submessage fields are lower than all other fields.
  3128. * Secondly, fields are increasing in order. */
  3129. uint32_t field_rank(const upb_fielddef *f) {
  3130. uint32_t ret = upb_fielddef_number(f);
  3131. const uint32_t high_bit = 1 << 30;
  3132. UPB_ASSERT(ret < high_bit);
  3133. if (!upb_fielddef_issubmsg(f))
  3134. ret |= high_bit;
  3135. return ret;
  3136. }
  3137. int cmp_fields(const void *p1, const void *p2) {
  3138. const upb_fielddef *f1 = *(upb_fielddef*const*)p1;
  3139. const upb_fielddef *f2 = *(upb_fielddef*const*)p2;
  3140. return field_rank(f1) - field_rank(f2);
  3141. }
  3142. /* A few implementation details of handlers. We put these here to avoid
  3143. * a def -> handlers dependency. */
  3144. #define UPB_STATIC_SELECTOR_COUNT 3 /* Warning: also in upb/handlers.h. */
  3145. static uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f) {
  3146. return upb_fielddef_isseq(f) ? 2 : 0;
  3147. }
  3148. static uint32_t upb_handlers_selectorcount(const upb_fielddef *f) {
  3149. uint32_t ret = 1;
  3150. if (upb_fielddef_isseq(f)) ret += 2; /* STARTSEQ/ENDSEQ */
  3151. if (upb_fielddef_isstring(f)) ret += 2; /* [STRING]/STARTSTR/ENDSTR */
  3152. if (upb_fielddef_issubmsg(f)) {
  3153. /* ENDSUBMSG (STARTSUBMSG is at table beginning) */
  3154. ret += 0;
  3155. if (upb_fielddef_lazy(f)) {
  3156. /* STARTSTR/ENDSTR/STRING (for lazy) */
  3157. ret += 3;
  3158. }
  3159. }
  3160. return ret;
  3161. }
  3162. static void upb_status_setoom(upb_status *status) {
  3163. upb_status_seterrmsg(status, "out of memory");
  3164. }
  3165. static bool assign_msg_indices(upb_msgdef *m, upb_status *s) {
  3166. /* Sort fields. upb internally relies on UPB_TYPE_MESSAGE fields having the
  3167. * lowest indexes, but we do not publicly guarantee this. */
  3168. upb_msg_field_iter j;
  3169. int i;
  3170. uint32_t selector;
  3171. int n = upb_msgdef_numfields(m);
  3172. upb_fielddef **fields;
  3173. if (n == 0) {
  3174. m->selector_count = UPB_STATIC_SELECTOR_COUNT;
  3175. m->submsg_field_count = 0;
  3176. return true;
  3177. }
  3178. fields = upb_gmalloc(n * sizeof(*fields));
  3179. if (!fields) {
  3180. upb_status_setoom(s);
  3181. return false;
  3182. }
  3183. m->submsg_field_count = 0;
  3184. for(i = 0, upb_msg_field_begin(&j, m);
  3185. !upb_msg_field_done(&j);
  3186. upb_msg_field_next(&j), i++) {
  3187. upb_fielddef *f = upb_msg_iter_field(&j);
  3188. UPB_ASSERT(f->msgdef == m);
  3189. if (upb_fielddef_issubmsg(f)) {
  3190. m->submsg_field_count++;
  3191. }
  3192. fields[i] = f;
  3193. }
  3194. qsort(fields, n, sizeof(*fields), cmp_fields);
  3195. selector = UPB_STATIC_SELECTOR_COUNT + m->submsg_field_count;
  3196. for (i = 0; i < n; i++) {
  3197. upb_fielddef *f = fields[i];
  3198. f->index_ = i;
  3199. f->selector_base = selector + upb_handlers_selectorbaseoffset(f);
  3200. selector += upb_handlers_selectorcount(f);
  3201. }
  3202. m->selector_count = selector;
  3203. upb_gfree(fields);
  3204. return true;
  3205. }
  3206. static bool check_oneofs(upb_msgdef *m, upb_status *s) {
  3207. int i;
  3208. int first_synthetic = -1;
  3209. upb_oneofdef *mutable_oneofs = (upb_oneofdef*)m->oneofs;
  3210. for (i = 0; i < m->oneof_count; i++) {
  3211. mutable_oneofs[i].index = i;
  3212. if (upb_oneofdef_issynthetic(&mutable_oneofs[i])) {
  3213. if (first_synthetic == -1) {
  3214. first_synthetic = i;
  3215. }
  3216. } else {
  3217. if (first_synthetic != -1) {
  3218. upb_status_seterrf(
  3219. s, "Synthetic oneofs must be after all other oneofs: %s",
  3220. upb_oneofdef_name(&mutable_oneofs[i]));
  3221. return false;
  3222. }
  3223. }
  3224. }
  3225. if (first_synthetic == -1) {
  3226. m->real_oneof_count = m->oneof_count;
  3227. } else {
  3228. m->real_oneof_count = first_synthetic;
  3229. }
  3230. return true;
  3231. }
  3232. static void assign_msg_wellknowntype(upb_msgdef *m) {
  3233. const char *name = upb_msgdef_fullname(m);
  3234. if (name == NULL) {
  3235. m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED;
  3236. return;
  3237. }
  3238. if (!strcmp(name, "google.protobuf.Any")) {
  3239. m->well_known_type = UPB_WELLKNOWN_ANY;
  3240. } else if (!strcmp(name, "google.protobuf.FieldMask")) {
  3241. m->well_known_type = UPB_WELLKNOWN_FIELDMASK;
  3242. } else if (!strcmp(name, "google.protobuf.Duration")) {
  3243. m->well_known_type = UPB_WELLKNOWN_DURATION;
  3244. } else if (!strcmp(name, "google.protobuf.Timestamp")) {
  3245. m->well_known_type = UPB_WELLKNOWN_TIMESTAMP;
  3246. } else if (!strcmp(name, "google.protobuf.DoubleValue")) {
  3247. m->well_known_type = UPB_WELLKNOWN_DOUBLEVALUE;
  3248. } else if (!strcmp(name, "google.protobuf.FloatValue")) {
  3249. m->well_known_type = UPB_WELLKNOWN_FLOATVALUE;
  3250. } else if (!strcmp(name, "google.protobuf.Int64Value")) {
  3251. m->well_known_type = UPB_WELLKNOWN_INT64VALUE;
  3252. } else if (!strcmp(name, "google.protobuf.UInt64Value")) {
  3253. m->well_known_type = UPB_WELLKNOWN_UINT64VALUE;
  3254. } else if (!strcmp(name, "google.protobuf.Int32Value")) {
  3255. m->well_known_type = UPB_WELLKNOWN_INT32VALUE;
  3256. } else if (!strcmp(name, "google.protobuf.UInt32Value")) {
  3257. m->well_known_type = UPB_WELLKNOWN_UINT32VALUE;
  3258. } else if (!strcmp(name, "google.protobuf.BoolValue")) {
  3259. m->well_known_type = UPB_WELLKNOWN_BOOLVALUE;
  3260. } else if (!strcmp(name, "google.protobuf.StringValue")) {
  3261. m->well_known_type = UPB_WELLKNOWN_STRINGVALUE;
  3262. } else if (!strcmp(name, "google.protobuf.BytesValue")) {
  3263. m->well_known_type = UPB_WELLKNOWN_BYTESVALUE;
  3264. } else if (!strcmp(name, "google.protobuf.Value")) {
  3265. m->well_known_type = UPB_WELLKNOWN_VALUE;
  3266. } else if (!strcmp(name, "google.protobuf.ListValue")) {
  3267. m->well_known_type = UPB_WELLKNOWN_LISTVALUE;
  3268. } else if (!strcmp(name, "google.protobuf.Struct")) {
  3269. m->well_known_type = UPB_WELLKNOWN_STRUCT;
  3270. } else {
  3271. m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED;
  3272. }
  3273. }
  3274. /* upb_enumdef ****************************************************************/
  3275. const char *upb_enumdef_fullname(const upb_enumdef *e) {
  3276. return e->full_name;
  3277. }
  3278. const char *upb_enumdef_name(const upb_enumdef *e) {
  3279. return shortdefname(e->full_name);
  3280. }
  3281. const upb_filedef *upb_enumdef_file(const upb_enumdef *e) {
  3282. return e->file;
  3283. }
  3284. int32_t upb_enumdef_default(const upb_enumdef *e) {
  3285. UPB_ASSERT(upb_enumdef_iton(e, e->defaultval));
  3286. return e->defaultval;
  3287. }
  3288. int upb_enumdef_numvals(const upb_enumdef *e) {
  3289. return (int)upb_strtable_count(&e->ntoi);
  3290. }
  3291. void upb_enum_begin(upb_enum_iter *i, const upb_enumdef *e) {
  3292. /* We iterate over the ntoi table, to account for duplicate numbers. */
  3293. upb_strtable_begin(i, &e->ntoi);
  3294. }
  3295. void upb_enum_next(upb_enum_iter *iter) { upb_strtable_next(iter); }
  3296. bool upb_enum_done(upb_enum_iter *iter) { return upb_strtable_done(iter); }
  3297. bool upb_enumdef_ntoi(const upb_enumdef *def, const char *name,
  3298. size_t len, int32_t *num) {
  3299. upb_value v;
  3300. if (!upb_strtable_lookup2(&def->ntoi, name, len, &v)) {
  3301. return false;
  3302. }
  3303. if (num) *num = upb_value_getint32(v);
  3304. return true;
  3305. }
  3306. const char *upb_enumdef_iton(const upb_enumdef *def, int32_t num) {
  3307. upb_value v;
  3308. return upb_inttable_lookup32(&def->iton, num, &v) ?
  3309. upb_value_getcstr(v) : NULL;
  3310. }
  3311. const char *upb_enum_iter_name(upb_enum_iter *iter) {
  3312. return upb_strtable_iter_key(iter).data;
  3313. }
  3314. int32_t upb_enum_iter_number(upb_enum_iter *iter) {
  3315. return upb_value_getint32(upb_strtable_iter_value(iter));
  3316. }
  3317. /* upb_fielddef ***************************************************************/
  3318. const char *upb_fielddef_fullname(const upb_fielddef *f) {
  3319. return f->full_name;
  3320. }
  3321. upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f) {
  3322. switch (f->type_) {
  3323. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  3324. return UPB_TYPE_DOUBLE;
  3325. case UPB_DESCRIPTOR_TYPE_FLOAT:
  3326. return UPB_TYPE_FLOAT;
  3327. case UPB_DESCRIPTOR_TYPE_INT64:
  3328. case UPB_DESCRIPTOR_TYPE_SINT64:
  3329. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  3330. return UPB_TYPE_INT64;
  3331. case UPB_DESCRIPTOR_TYPE_INT32:
  3332. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  3333. case UPB_DESCRIPTOR_TYPE_SINT32:
  3334. return UPB_TYPE_INT32;
  3335. case UPB_DESCRIPTOR_TYPE_UINT64:
  3336. case UPB_DESCRIPTOR_TYPE_FIXED64:
  3337. return UPB_TYPE_UINT64;
  3338. case UPB_DESCRIPTOR_TYPE_UINT32:
  3339. case UPB_DESCRIPTOR_TYPE_FIXED32:
  3340. return UPB_TYPE_UINT32;
  3341. case UPB_DESCRIPTOR_TYPE_ENUM:
  3342. return UPB_TYPE_ENUM;
  3343. case UPB_DESCRIPTOR_TYPE_BOOL:
  3344. return UPB_TYPE_BOOL;
  3345. case UPB_DESCRIPTOR_TYPE_STRING:
  3346. return UPB_TYPE_STRING;
  3347. case UPB_DESCRIPTOR_TYPE_BYTES:
  3348. return UPB_TYPE_BYTES;
  3349. case UPB_DESCRIPTOR_TYPE_GROUP:
  3350. case UPB_DESCRIPTOR_TYPE_MESSAGE:
  3351. return UPB_TYPE_MESSAGE;
  3352. }
  3353. UPB_UNREACHABLE();
  3354. }
  3355. upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f) {
  3356. return f->type_;
  3357. }
  3358. uint32_t upb_fielddef_index(const upb_fielddef *f) {
  3359. return f->index_;
  3360. }
  3361. upb_label_t upb_fielddef_label(const upb_fielddef *f) {
  3362. return f->label_;
  3363. }
  3364. uint32_t upb_fielddef_number(const upb_fielddef *f) {
  3365. return f->number_;
  3366. }
  3367. bool upb_fielddef_isextension(const upb_fielddef *f) {
  3368. return f->is_extension_;
  3369. }
  3370. bool upb_fielddef_lazy(const upb_fielddef *f) {
  3371. return f->lazy_;
  3372. }
  3373. bool upb_fielddef_packed(const upb_fielddef *f) {
  3374. return f->packed_;
  3375. }
  3376. const char *upb_fielddef_name(const upb_fielddef *f) {
  3377. return shortdefname(f->full_name);
  3378. }
  3379. const char *upb_fielddef_jsonname(const upb_fielddef *f) {
  3380. return f->json_name;
  3381. }
  3382. uint32_t upb_fielddef_selectorbase(const upb_fielddef *f) {
  3383. return f->selector_base;
  3384. }
  3385. const upb_filedef *upb_fielddef_file(const upb_fielddef *f) {
  3386. return f->file;
  3387. }
  3388. const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f) {
  3389. return f->msgdef;
  3390. }
  3391. const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f) {
  3392. return f->oneof;
  3393. }
  3394. const upb_oneofdef *upb_fielddef_realcontainingoneof(const upb_fielddef *f) {
  3395. if (!f->oneof || upb_oneofdef_issynthetic(f->oneof)) return NULL;
  3396. return f->oneof;
  3397. }
  3398. static void chkdefaulttype(const upb_fielddef *f, int ctype) {
  3399. UPB_UNUSED(f);
  3400. UPB_UNUSED(ctype);
  3401. }
  3402. int64_t upb_fielddef_defaultint64(const upb_fielddef *f) {
  3403. chkdefaulttype(f, UPB_TYPE_INT64);
  3404. return f->defaultval.sint;
  3405. }
  3406. int32_t upb_fielddef_defaultint32(const upb_fielddef *f) {
  3407. chkdefaulttype(f, UPB_TYPE_INT32);
  3408. return (int32_t)f->defaultval.sint;
  3409. }
  3410. uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f) {
  3411. chkdefaulttype(f, UPB_TYPE_UINT64);
  3412. return f->defaultval.uint;
  3413. }
  3414. uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f) {
  3415. chkdefaulttype(f, UPB_TYPE_UINT32);
  3416. return (uint32_t)f->defaultval.uint;
  3417. }
  3418. bool upb_fielddef_defaultbool(const upb_fielddef *f) {
  3419. chkdefaulttype(f, UPB_TYPE_BOOL);
  3420. return f->defaultval.boolean;
  3421. }
  3422. float upb_fielddef_defaultfloat(const upb_fielddef *f) {
  3423. chkdefaulttype(f, UPB_TYPE_FLOAT);
  3424. return f->defaultval.flt;
  3425. }
  3426. double upb_fielddef_defaultdouble(const upb_fielddef *f) {
  3427. chkdefaulttype(f, UPB_TYPE_DOUBLE);
  3428. return f->defaultval.dbl;
  3429. }
  3430. const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len) {
  3431. str_t *str = f->defaultval.str;
  3432. UPB_ASSERT(upb_fielddef_type(f) == UPB_TYPE_STRING ||
  3433. upb_fielddef_type(f) == UPB_TYPE_BYTES ||
  3434. upb_fielddef_type(f) == UPB_TYPE_ENUM);
  3435. if (str) {
  3436. if (len) *len = str->len;
  3437. return str->str;
  3438. } else {
  3439. if (len) *len = 0;
  3440. return NULL;
  3441. }
  3442. }
  3443. const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f) {
  3444. return upb_fielddef_type(f) == UPB_TYPE_MESSAGE ? f->sub.msgdef : NULL;
  3445. }
  3446. const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f) {
  3447. return upb_fielddef_type(f) == UPB_TYPE_ENUM ? f->sub.enumdef : NULL;
  3448. }
  3449. const upb_msglayout_field *upb_fielddef_layout(const upb_fielddef *f) {
  3450. return &f->msgdef->layout->fields[f->layout_index];
  3451. }
  3452. bool upb_fielddef_issubmsg(const upb_fielddef *f) {
  3453. return upb_fielddef_type(f) == UPB_TYPE_MESSAGE;
  3454. }
  3455. bool upb_fielddef_isstring(const upb_fielddef *f) {
  3456. return upb_fielddef_type(f) == UPB_TYPE_STRING ||
  3457. upb_fielddef_type(f) == UPB_TYPE_BYTES;
  3458. }
  3459. bool upb_fielddef_isseq(const upb_fielddef *f) {
  3460. return upb_fielddef_label(f) == UPB_LABEL_REPEATED;
  3461. }
  3462. bool upb_fielddef_isprimitive(const upb_fielddef *f) {
  3463. return !upb_fielddef_isstring(f) && !upb_fielddef_issubmsg(f);
  3464. }
  3465. bool upb_fielddef_ismap(const upb_fielddef *f) {
  3466. return upb_fielddef_isseq(f) && upb_fielddef_issubmsg(f) &&
  3467. upb_msgdef_mapentry(upb_fielddef_msgsubdef(f));
  3468. }
  3469. bool upb_fielddef_hassubdef(const upb_fielddef *f) {
  3470. return upb_fielddef_issubmsg(f) || upb_fielddef_type(f) == UPB_TYPE_ENUM;
  3471. }
  3472. bool upb_fielddef_haspresence(const upb_fielddef *f) {
  3473. if (upb_fielddef_isseq(f)) return false;
  3474. return upb_fielddef_issubmsg(f) || upb_fielddef_containingoneof(f) ||
  3475. f->file->syntax == UPB_SYNTAX_PROTO2;
  3476. }
  3477. static bool between(int32_t x, int32_t low, int32_t high) {
  3478. return x >= low && x <= high;
  3479. }
  3480. bool upb_fielddef_checklabel(int32_t label) { return between(label, 1, 3); }
  3481. bool upb_fielddef_checktype(int32_t type) { return between(type, 1, 11); }
  3482. bool upb_fielddef_checkintfmt(int32_t fmt) { return between(fmt, 1, 3); }
  3483. bool upb_fielddef_checkdescriptortype(int32_t type) {
  3484. return between(type, 1, 18);
  3485. }
  3486. /* upb_msgdef *****************************************************************/
  3487. const char *upb_msgdef_fullname(const upb_msgdef *m) {
  3488. return m->full_name;
  3489. }
  3490. const upb_filedef *upb_msgdef_file(const upb_msgdef *m) {
  3491. return m->file;
  3492. }
  3493. const char *upb_msgdef_name(const upb_msgdef *m) {
  3494. return shortdefname(m->full_name);
  3495. }
  3496. upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m) {
  3497. return m->file->syntax;
  3498. }
  3499. size_t upb_msgdef_selectorcount(const upb_msgdef *m) {
  3500. return m->selector_count;
  3501. }
  3502. uint32_t upb_msgdef_submsgfieldcount(const upb_msgdef *m) {
  3503. return m->submsg_field_count;
  3504. }
  3505. const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i) {
  3506. upb_value val;
  3507. return upb_inttable_lookup32(&m->itof, i, &val) ?
  3508. upb_value_getconstptr(val) : NULL;
  3509. }
  3510. const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name,
  3511. size_t len) {
  3512. upb_value val;
  3513. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  3514. return NULL;
  3515. }
  3516. return unpack_def(val, UPB_DEFTYPE_FIELD);
  3517. }
  3518. const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
  3519. size_t len) {
  3520. upb_value val;
  3521. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  3522. return NULL;
  3523. }
  3524. return unpack_def(val, UPB_DEFTYPE_ONEOF);
  3525. }
  3526. bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len,
  3527. const upb_fielddef **f, const upb_oneofdef **o) {
  3528. upb_value val;
  3529. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  3530. return false;
  3531. }
  3532. *o = unpack_def(val, UPB_DEFTYPE_ONEOF);
  3533. *f = unpack_def(val, UPB_DEFTYPE_FIELD);
  3534. return *o || *f; /* False if this was a JSON name. */
  3535. }
  3536. const upb_fielddef *upb_msgdef_lookupjsonname(const upb_msgdef *m,
  3537. const char *name, size_t len) {
  3538. upb_value val;
  3539. const upb_fielddef* f;
  3540. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  3541. return NULL;
  3542. }
  3543. f = unpack_def(val, UPB_DEFTYPE_FIELD);
  3544. if (!f) f = unpack_def(val, UPB_DEFTYPE_FIELD_JSONNAME);
  3545. return f;
  3546. }
  3547. int upb_msgdef_numfields(const upb_msgdef *m) {
  3548. return m->field_count;
  3549. }
  3550. int upb_msgdef_numoneofs(const upb_msgdef *m) {
  3551. return m->oneof_count;
  3552. }
  3553. int upb_msgdef_numrealoneofs(const upb_msgdef *m) {
  3554. return m->real_oneof_count;
  3555. }
  3556. const upb_msglayout *upb_msgdef_layout(const upb_msgdef *m) {
  3557. return m->layout;
  3558. }
  3559. const upb_fielddef *_upb_msgdef_field(const upb_msgdef *m, int i) {
  3560. if (i >= m->field_count) return NULL;
  3561. return &m->fields[i];
  3562. }
  3563. bool upb_msgdef_mapentry(const upb_msgdef *m) {
  3564. return m->map_entry;
  3565. }
  3566. upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m) {
  3567. return m->well_known_type;
  3568. }
  3569. bool upb_msgdef_isnumberwrapper(const upb_msgdef *m) {
  3570. upb_wellknowntype_t type = upb_msgdef_wellknowntype(m);
  3571. return type >= UPB_WELLKNOWN_DOUBLEVALUE &&
  3572. type <= UPB_WELLKNOWN_UINT32VALUE;
  3573. }
  3574. bool upb_msgdef_iswrapper(const upb_msgdef *m) {
  3575. upb_wellknowntype_t type = upb_msgdef_wellknowntype(m);
  3576. return type >= UPB_WELLKNOWN_DOUBLEVALUE &&
  3577. type <= UPB_WELLKNOWN_BOOLVALUE;
  3578. }
  3579. void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m) {
  3580. upb_inttable_begin(iter, &m->itof);
  3581. }
  3582. void upb_msg_field_next(upb_msg_field_iter *iter) { upb_inttable_next(iter); }
  3583. bool upb_msg_field_done(const upb_msg_field_iter *iter) {
  3584. return upb_inttable_done(iter);
  3585. }
  3586. upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter) {
  3587. return (upb_fielddef *)upb_value_getconstptr(upb_inttable_iter_value(iter));
  3588. }
  3589. void upb_msg_field_iter_setdone(upb_msg_field_iter *iter) {
  3590. upb_inttable_iter_setdone(iter);
  3591. }
  3592. bool upb_msg_field_iter_isequal(const upb_msg_field_iter * iter1,
  3593. const upb_msg_field_iter * iter2) {
  3594. return upb_inttable_iter_isequal(iter1, iter2);
  3595. }
  3596. void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m) {
  3597. upb_strtable_begin(iter, &m->ntof);
  3598. /* We need to skip past any initial fields. */
  3599. while (!upb_strtable_done(iter) &&
  3600. !unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF)) {
  3601. upb_strtable_next(iter);
  3602. }
  3603. }
  3604. void upb_msg_oneof_next(upb_msg_oneof_iter *iter) {
  3605. /* We need to skip past fields to return only oneofs. */
  3606. do {
  3607. upb_strtable_next(iter);
  3608. } while (!upb_strtable_done(iter) &&
  3609. !unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF));
  3610. }
  3611. bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter) {
  3612. return upb_strtable_done(iter);
  3613. }
  3614. const upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter) {
  3615. return unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF);
  3616. }
  3617. void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter) {
  3618. upb_strtable_iter_setdone(iter);
  3619. }
  3620. bool upb_msg_oneof_iter_isequal(const upb_msg_oneof_iter *iter1,
  3621. const upb_msg_oneof_iter *iter2) {
  3622. return upb_strtable_iter_isequal(iter1, iter2);
  3623. }
  3624. /* upb_oneofdef ***************************************************************/
  3625. const char *upb_oneofdef_name(const upb_oneofdef *o) {
  3626. return shortdefname(o->full_name);
  3627. }
  3628. const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o) {
  3629. return o->parent;
  3630. }
  3631. int upb_oneofdef_numfields(const upb_oneofdef *o) {
  3632. return (int)upb_strtable_count(&o->ntof);
  3633. }
  3634. uint32_t upb_oneofdef_index(const upb_oneofdef *o) {
  3635. return o->index;
  3636. }
  3637. bool upb_oneofdef_issynthetic(const upb_oneofdef *o) {
  3638. upb_inttable_iter iter;
  3639. const upb_fielddef *f;
  3640. upb_inttable_begin(&iter, &o->itof);
  3641. if (upb_oneofdef_numfields(o) != 1) return false;
  3642. f = upb_value_getptr(upb_inttable_iter_value(&iter));
  3643. UPB_ASSERT(f);
  3644. return f->proto3_optional_;
  3645. }
  3646. const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o,
  3647. const char *name, size_t length) {
  3648. upb_value val;
  3649. return upb_strtable_lookup2(&o->ntof, name, length, &val) ?
  3650. upb_value_getptr(val) : NULL;
  3651. }
  3652. const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num) {
  3653. upb_value val;
  3654. return upb_inttable_lookup32(&o->itof, num, &val) ?
  3655. upb_value_getptr(val) : NULL;
  3656. }
  3657. void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o) {
  3658. upb_inttable_begin(iter, &o->itof);
  3659. }
  3660. void upb_oneof_next(upb_oneof_iter *iter) {
  3661. upb_inttable_next(iter);
  3662. }
  3663. bool upb_oneof_done(upb_oneof_iter *iter) {
  3664. return upb_inttable_done(iter);
  3665. }
  3666. upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter) {
  3667. return (upb_fielddef *)upb_value_getconstptr(upb_inttable_iter_value(iter));
  3668. }
  3669. void upb_oneof_iter_setdone(upb_oneof_iter *iter) {
  3670. upb_inttable_iter_setdone(iter);
  3671. }
  3672. /* Dynamic Layout Generation. *************************************************/
  3673. static size_t div_round_up(size_t n, size_t d) {
  3674. return (n + d - 1) / d;
  3675. }
  3676. static size_t upb_msgval_sizeof(upb_fieldtype_t type) {
  3677. switch (type) {
  3678. case UPB_TYPE_DOUBLE:
  3679. case UPB_TYPE_INT64:
  3680. case UPB_TYPE_UINT64:
  3681. return 8;
  3682. case UPB_TYPE_ENUM:
  3683. case UPB_TYPE_INT32:
  3684. case UPB_TYPE_UINT32:
  3685. case UPB_TYPE_FLOAT:
  3686. return 4;
  3687. case UPB_TYPE_BOOL:
  3688. return 1;
  3689. case UPB_TYPE_MESSAGE:
  3690. return sizeof(void*);
  3691. case UPB_TYPE_BYTES:
  3692. case UPB_TYPE_STRING:
  3693. return sizeof(upb_strview);
  3694. }
  3695. UPB_UNREACHABLE();
  3696. }
  3697. static uint8_t upb_msg_fielddefsize(const upb_fielddef *f) {
  3698. if (upb_msgdef_mapentry(upb_fielddef_containingtype(f))) {
  3699. upb_map_entry ent;
  3700. UPB_ASSERT(sizeof(ent.k) == sizeof(ent.v));
  3701. return sizeof(ent.k);
  3702. } else if (upb_fielddef_isseq(f)) {
  3703. return sizeof(void*);
  3704. } else {
  3705. return upb_msgval_sizeof(upb_fielddef_type(f));
  3706. }
  3707. }
  3708. static uint32_t upb_msglayout_place(upb_msglayout *l, size_t size) {
  3709. uint32_t ret;
  3710. l->size = UPB_ALIGN_UP(l->size, size);
  3711. ret = l->size;
  3712. l->size += size;
  3713. return ret;
  3714. }
  3715. static int field_number_cmp(const void *p1, const void *p2) {
  3716. const upb_msglayout_field *f1 = p1;
  3717. const upb_msglayout_field *f2 = p2;
  3718. return f1->number - f2->number;
  3719. }
  3720. static void assign_layout_indices(const upb_msgdef *m, upb_msglayout_field *fields) {
  3721. int i;
  3722. int n = upb_msgdef_numfields(m);
  3723. for (i = 0; i < n; i++) {
  3724. upb_fielddef *f = (upb_fielddef*)upb_msgdef_itof(m, fields[i].number);
  3725. UPB_ASSERT(f);
  3726. f->layout_index = i;
  3727. }
  3728. }
  3729. /* This function is the dynamic equivalent of message_layout.{cc,h} in upbc.
  3730. * It computes a dynamic layout for all of the fields in |m|. */
  3731. static bool make_layout(const upb_symtab *symtab, const upb_msgdef *m) {
  3732. upb_msglayout *l = (upb_msglayout*)m->layout;
  3733. upb_msg_field_iter it;
  3734. upb_msg_oneof_iter oit;
  3735. size_t hasbit;
  3736. size_t submsg_count = m->submsg_field_count;
  3737. const upb_msglayout **submsgs;
  3738. upb_msglayout_field *fields;
  3739. upb_alloc *alloc = upb_arena_alloc(symtab->arena);
  3740. memset(l, 0, sizeof(*l));
  3741. fields = upb_malloc(alloc, upb_msgdef_numfields(m) * sizeof(*fields));
  3742. submsgs = upb_malloc(alloc, submsg_count * sizeof(*submsgs));
  3743. if ((!fields && upb_msgdef_numfields(m)) ||
  3744. (!submsgs && submsg_count)) {
  3745. /* OOM. */
  3746. return false;
  3747. }
  3748. l->field_count = upb_msgdef_numfields(m);
  3749. l->fields = fields;
  3750. l->submsgs = submsgs;
  3751. if (upb_msgdef_mapentry(m)) {
  3752. /* TODO(haberman): refactor this method so this special case is more
  3753. * elegant. */
  3754. const upb_fielddef *key = upb_msgdef_itof(m, 1);
  3755. const upb_fielddef *val = upb_msgdef_itof(m, 2);
  3756. fields[0].number = 1;
  3757. fields[1].number = 2;
  3758. fields[0].label = UPB_LABEL_OPTIONAL;
  3759. fields[1].label = UPB_LABEL_OPTIONAL;
  3760. fields[0].presence = 0;
  3761. fields[1].presence = 0;
  3762. fields[0].descriptortype = upb_fielddef_descriptortype(key);
  3763. fields[1].descriptortype = upb_fielddef_descriptortype(val);
  3764. fields[0].offset = 0;
  3765. fields[1].offset = sizeof(upb_strview);
  3766. fields[1].submsg_index = 0;
  3767. if (upb_fielddef_type(val) == UPB_TYPE_MESSAGE) {
  3768. submsgs[0] = upb_fielddef_msgsubdef(val)->layout;
  3769. }
  3770. l->field_count = 2;
  3771. l->size = 2 * sizeof(upb_strview);
  3772. l->size = UPB_ALIGN_UP(l->size, 8);
  3773. return true;
  3774. }
  3775. /* Allocate data offsets in three stages:
  3776. *
  3777. * 1. hasbits.
  3778. * 2. regular fields.
  3779. * 3. oneof fields.
  3780. *
  3781. * OPT: There is a lot of room for optimization here to minimize the size.
  3782. */
  3783. /* Allocate hasbits and set basic field attributes. */
  3784. submsg_count = 0;
  3785. for (upb_msg_field_begin(&it, m), hasbit = 0;
  3786. !upb_msg_field_done(&it);
  3787. upb_msg_field_next(&it)) {
  3788. upb_fielddef* f = upb_msg_iter_field(&it);
  3789. upb_msglayout_field *field = &fields[upb_fielddef_index(f)];
  3790. field->number = upb_fielddef_number(f);
  3791. field->descriptortype = upb_fielddef_descriptortype(f);
  3792. field->label = upb_fielddef_label(f);
  3793. if (field->descriptortype == UPB_DTYPE_STRING &&
  3794. f->file->syntax == UPB_SYNTAX_PROTO2) {
  3795. /* See TableDescriptorType() in upbc/generator.cc for details and
  3796. * rationale. */
  3797. field->descriptortype = UPB_DTYPE_BYTES;
  3798. }
  3799. if (upb_fielddef_ismap(f)) {
  3800. field->label = _UPB_LABEL_MAP;
  3801. } else if (upb_fielddef_packed(f)) {
  3802. field->label = _UPB_LABEL_PACKED;
  3803. }
  3804. if (upb_fielddef_issubmsg(f)) {
  3805. const upb_msgdef *subm = upb_fielddef_msgsubdef(f);
  3806. field->submsg_index = submsg_count++;
  3807. submsgs[field->submsg_index] = subm->layout;
  3808. }
  3809. if (upb_fielddef_haspresence(f) && !upb_fielddef_realcontainingoneof(f)) {
  3810. /* We don't use hasbit 0, so that 0 can indicate "no presence" in the
  3811. * table. This wastes one hasbit, but we don't worry about it for now. */
  3812. field->presence = ++hasbit;
  3813. } else {
  3814. field->presence = 0;
  3815. }
  3816. }
  3817. /* Account for space used by hasbits. */
  3818. l->size = div_round_up(hasbit, 8);
  3819. /* Allocate non-oneof fields. */
  3820. for (upb_msg_field_begin(&it, m); !upb_msg_field_done(&it);
  3821. upb_msg_field_next(&it)) {
  3822. const upb_fielddef* f = upb_msg_iter_field(&it);
  3823. size_t field_size = upb_msg_fielddefsize(f);
  3824. size_t index = upb_fielddef_index(f);
  3825. if (upb_fielddef_realcontainingoneof(f)) {
  3826. /* Oneofs are handled separately below. */
  3827. continue;
  3828. }
  3829. fields[index].offset = upb_msglayout_place(l, field_size);
  3830. }
  3831. /* Allocate oneof fields. Each oneof field consists of a uint32 for the case
  3832. * and space for the actual data. */
  3833. for (upb_msg_oneof_begin(&oit, m); !upb_msg_oneof_done(&oit);
  3834. upb_msg_oneof_next(&oit)) {
  3835. const upb_oneofdef* o = upb_msg_iter_oneof(&oit);
  3836. upb_oneof_iter fit;
  3837. size_t case_size = sizeof(uint32_t); /* Could potentially optimize this. */
  3838. size_t field_size = 0;
  3839. uint32_t case_offset;
  3840. uint32_t data_offset;
  3841. if (upb_oneofdef_issynthetic(o)) continue;
  3842. /* Calculate field size: the max of all field sizes. */
  3843. for (upb_oneof_begin(&fit, o);
  3844. !upb_oneof_done(&fit);
  3845. upb_oneof_next(&fit)) {
  3846. const upb_fielddef* f = upb_oneof_iter_field(&fit);
  3847. field_size = UPB_MAX(field_size, upb_msg_fielddefsize(f));
  3848. }
  3849. /* Align and allocate case offset. */
  3850. case_offset = upb_msglayout_place(l, case_size);
  3851. data_offset = upb_msglayout_place(l, field_size);
  3852. for (upb_oneof_begin(&fit, o);
  3853. !upb_oneof_done(&fit);
  3854. upb_oneof_next(&fit)) {
  3855. const upb_fielddef* f = upb_oneof_iter_field(&fit);
  3856. fields[upb_fielddef_index(f)].offset = data_offset;
  3857. fields[upb_fielddef_index(f)].presence = ~case_offset;
  3858. }
  3859. }
  3860. /* Size of the entire structure should be a multiple of its greatest
  3861. * alignment. TODO: track overall alignment for real? */
  3862. l->size = UPB_ALIGN_UP(l->size, 8);
  3863. /* Sort fields by number. */
  3864. qsort(fields, upb_msgdef_numfields(m), sizeof(*fields), field_number_cmp);
  3865. assign_layout_indices(m, fields);
  3866. return true;
  3867. }
  3868. /* Code to build defs from descriptor protos. *********************************/
  3869. /* There is a question of how much validation to do here. It will be difficult
  3870. * to perfectly match the amount of validation performed by proto2. But since
  3871. * this code is used to directly build defs from Ruby (for example) we do need
  3872. * to validate important constraints like uniqueness of names and numbers. */
  3873. #define CHK(x) if (!(x)) { return false; }
  3874. #define CHK_OOM(x) if (!(x)) { upb_status_setoom(ctx->status); return false; }
  3875. typedef struct {
  3876. const upb_symtab *symtab;
  3877. upb_filedef *file; /* File we are building. */
  3878. upb_alloc *alloc; /* Allocate defs here. */
  3879. upb_alloc *tmp; /* Alloc for addtab and any other tmp data. */
  3880. upb_strtable *addtab; /* full_name -> packed def ptr for new defs */
  3881. const upb_msglayout **layouts; /* NULL if we should build layouts. */
  3882. upb_status *status; /* Record errors here. */
  3883. } symtab_addctx;
  3884. static char* strviewdup(const symtab_addctx *ctx, upb_strview view) {
  3885. return upb_strdup2(view.data, view.size, ctx->alloc);
  3886. }
  3887. static bool streql2(const char *a, size_t n, const char *b) {
  3888. return n == strlen(b) && memcmp(a, b, n) == 0;
  3889. }
  3890. static bool streql_view(upb_strview view, const char *b) {
  3891. return streql2(view.data, view.size, b);
  3892. }
  3893. static const char *makefullname(const symtab_addctx *ctx, const char *prefix,
  3894. upb_strview name) {
  3895. if (prefix) {
  3896. /* ret = prefix + '.' + name; */
  3897. size_t n = strlen(prefix);
  3898. char *ret = upb_malloc(ctx->alloc, n + name.size + 2);
  3899. CHK_OOM(ret);
  3900. strcpy(ret, prefix);
  3901. ret[n] = '.';
  3902. memcpy(&ret[n + 1], name.data, name.size);
  3903. ret[n + 1 + name.size] = '\0';
  3904. return ret;
  3905. } else {
  3906. return strviewdup(ctx, name);
  3907. }
  3908. }
  3909. size_t getjsonname(const char *name, char *buf, size_t len) {
  3910. size_t src, dst = 0;
  3911. bool ucase_next = false;
  3912. #define WRITE(byte) \
  3913. ++dst; \
  3914. if (dst < len) buf[dst - 1] = byte; \
  3915. else if (dst == len) buf[dst - 1] = '\0'
  3916. if (!name) {
  3917. WRITE('\0');
  3918. return 0;
  3919. }
  3920. /* Implement the transformation as described in the spec:
  3921. * 1. upper case all letters after an underscore.
  3922. * 2. remove all underscores.
  3923. */
  3924. for (src = 0; name[src]; src++) {
  3925. if (name[src] == '_') {
  3926. ucase_next = true;
  3927. continue;
  3928. }
  3929. if (ucase_next) {
  3930. WRITE(toupper(name[src]));
  3931. ucase_next = false;
  3932. } else {
  3933. WRITE(name[src]);
  3934. }
  3935. }
  3936. WRITE('\0');
  3937. return dst;
  3938. #undef WRITE
  3939. }
  3940. static char* makejsonname(const char* name, upb_alloc *alloc) {
  3941. size_t size = getjsonname(name, NULL, 0);
  3942. char* json_name = upb_malloc(alloc, size);
  3943. getjsonname(name, json_name, size);
  3944. return json_name;
  3945. }
  3946. static bool symtab_add(const symtab_addctx *ctx, const char *name,
  3947. upb_value v) {
  3948. upb_value tmp;
  3949. if (upb_strtable_lookup(ctx->addtab, name, &tmp) ||
  3950. upb_strtable_lookup(&ctx->symtab->syms, name, &tmp)) {
  3951. upb_status_seterrf(ctx->status, "duplicate symbol '%s'", name);
  3952. return false;
  3953. }
  3954. CHK_OOM(upb_strtable_insert3(ctx->addtab, name, strlen(name), v, ctx->tmp));
  3955. return true;
  3956. }
  3957. /* Given a symbol and the base symbol inside which it is defined, find the
  3958. * symbol's definition in t. */
  3959. static bool resolvename(const upb_strtable *t, const upb_fielddef *f,
  3960. const char *base, upb_strview sym,
  3961. upb_deftype_t type, upb_status *status,
  3962. const void **def) {
  3963. if(sym.size == 0) return false;
  3964. if(sym.data[0] == '.') {
  3965. /* Symbols starting with '.' are absolute, so we do a single lookup.
  3966. * Slice to omit the leading '.' */
  3967. upb_value v;
  3968. if (!upb_strtable_lookup2(t, sym.data + 1, sym.size - 1, &v)) {
  3969. return false;
  3970. }
  3971. *def = unpack_def(v, type);
  3972. if (!*def) {
  3973. upb_status_seterrf(status,
  3974. "type mismatch when resolving field %s, name %s",
  3975. f->full_name, sym.data);
  3976. return false;
  3977. }
  3978. return true;
  3979. } else {
  3980. /* Remove components from base until we find an entry or run out.
  3981. * TODO: This branch is totally broken, but currently not used. */
  3982. (void)base;
  3983. UPB_ASSERT(false);
  3984. return false;
  3985. }
  3986. }
  3987. const void *symtab_resolve(const symtab_addctx *ctx, const upb_fielddef *f,
  3988. const char *base, upb_strview sym,
  3989. upb_deftype_t type) {
  3990. const void *ret;
  3991. if (!resolvename(ctx->addtab, f, base, sym, type, ctx->status, &ret) &&
  3992. !resolvename(&ctx->symtab->syms, f, base, sym, type, ctx->status, &ret)) {
  3993. if (upb_ok(ctx->status)) {
  3994. upb_status_seterrf(ctx->status, "couldn't resolve name '%s'", sym.data);
  3995. }
  3996. return false;
  3997. }
  3998. return ret;
  3999. }
  4000. static bool create_oneofdef(
  4001. const symtab_addctx *ctx, upb_msgdef *m,
  4002. const google_protobuf_OneofDescriptorProto *oneof_proto) {
  4003. upb_oneofdef *o;
  4004. upb_strview name = google_protobuf_OneofDescriptorProto_name(oneof_proto);
  4005. upb_value v;
  4006. o = (upb_oneofdef*)&m->oneofs[m->oneof_count++];
  4007. o->parent = m;
  4008. o->full_name = makefullname(ctx, m->full_name, name);
  4009. v = pack_def(o, UPB_DEFTYPE_ONEOF);
  4010. CHK_OOM(symtab_add(ctx, o->full_name, v));
  4011. CHK_OOM(upb_strtable_insert3(&m->ntof, name.data, name.size, v, ctx->alloc));
  4012. CHK_OOM(upb_inttable_init2(&o->itof, UPB_CTYPE_CONSTPTR, ctx->alloc));
  4013. CHK_OOM(upb_strtable_init2(&o->ntof, UPB_CTYPE_CONSTPTR, ctx->alloc));
  4014. return true;
  4015. }
  4016. static bool parse_default(const symtab_addctx *ctx, const char *str, size_t len,
  4017. upb_fielddef *f) {
  4018. char *end;
  4019. char nullz[64];
  4020. errno = 0;
  4021. switch (upb_fielddef_type(f)) {
  4022. case UPB_TYPE_INT32:
  4023. case UPB_TYPE_INT64:
  4024. case UPB_TYPE_UINT32:
  4025. case UPB_TYPE_UINT64:
  4026. case UPB_TYPE_DOUBLE:
  4027. case UPB_TYPE_FLOAT:
  4028. /* Standard C number parsing functions expect null-terminated strings. */
  4029. if (len >= sizeof(nullz) - 1) {
  4030. return false;
  4031. }
  4032. memcpy(nullz, str, len);
  4033. nullz[len] = '\0';
  4034. str = nullz;
  4035. break;
  4036. default:
  4037. break;
  4038. }
  4039. switch (upb_fielddef_type(f)) {
  4040. case UPB_TYPE_INT32: {
  4041. long val = strtol(str, &end, 0);
  4042. CHK(val <= INT32_MAX && val >= INT32_MIN && errno != ERANGE && !*end);
  4043. f->defaultval.sint = val;
  4044. break;
  4045. }
  4046. case UPB_TYPE_ENUM: {
  4047. const upb_enumdef *e = f->sub.enumdef;
  4048. int32_t val;
  4049. CHK(upb_enumdef_ntoi(e, str, len, &val));
  4050. f->defaultval.sint = val;
  4051. break;
  4052. }
  4053. case UPB_TYPE_INT64: {
  4054. /* XXX: Need to write our own strtoll, since it's not available in c89. */
  4055. int64_t val = strtol(str, &end, 0);
  4056. CHK(val <= INT64_MAX && val >= INT64_MIN && errno != ERANGE && !*end);
  4057. f->defaultval.sint = val;
  4058. break;
  4059. }
  4060. case UPB_TYPE_UINT32: {
  4061. unsigned long val = strtoul(str, &end, 0);
  4062. CHK(val <= UINT32_MAX && errno != ERANGE && !*end);
  4063. f->defaultval.uint = val;
  4064. break;
  4065. }
  4066. case UPB_TYPE_UINT64: {
  4067. /* XXX: Need to write our own strtoull, since it's not available in c89. */
  4068. uint64_t val = strtoul(str, &end, 0);
  4069. CHK(val <= UINT64_MAX && errno != ERANGE && !*end);
  4070. f->defaultval.uint = val;
  4071. break;
  4072. }
  4073. case UPB_TYPE_DOUBLE: {
  4074. double val = strtod(str, &end);
  4075. CHK(errno != ERANGE && !*end);
  4076. f->defaultval.dbl = val;
  4077. break;
  4078. }
  4079. case UPB_TYPE_FLOAT: {
  4080. /* XXX: Need to write our own strtof, since it's not available in c89. */
  4081. float val = strtod(str, &end);
  4082. CHK(errno != ERANGE && !*end);
  4083. f->defaultval.flt = val;
  4084. break;
  4085. }
  4086. case UPB_TYPE_BOOL: {
  4087. if (streql2(str, len, "false")) {
  4088. f->defaultval.boolean = false;
  4089. } else if (streql2(str, len, "true")) {
  4090. f->defaultval.boolean = true;
  4091. } else {
  4092. return false;
  4093. }
  4094. break;
  4095. }
  4096. case UPB_TYPE_STRING:
  4097. f->defaultval.str = newstr(ctx->alloc, str, len);
  4098. break;
  4099. case UPB_TYPE_BYTES:
  4100. /* XXX: need to interpret the C-escaped value. */
  4101. f->defaultval.str = newstr(ctx->alloc, str, len);
  4102. break;
  4103. case UPB_TYPE_MESSAGE:
  4104. /* Should not have a default value. */
  4105. return false;
  4106. }
  4107. return true;
  4108. }
  4109. static void set_default_default(const symtab_addctx *ctx, upb_fielddef *f) {
  4110. switch (upb_fielddef_type(f)) {
  4111. case UPB_TYPE_INT32:
  4112. case UPB_TYPE_INT64:
  4113. case UPB_TYPE_ENUM:
  4114. f->defaultval.sint = 0;
  4115. break;
  4116. case UPB_TYPE_UINT64:
  4117. case UPB_TYPE_UINT32:
  4118. f->defaultval.uint = 0;
  4119. break;
  4120. case UPB_TYPE_DOUBLE:
  4121. case UPB_TYPE_FLOAT:
  4122. f->defaultval.dbl = 0;
  4123. break;
  4124. case UPB_TYPE_STRING:
  4125. case UPB_TYPE_BYTES:
  4126. f->defaultval.str = newstr(ctx->alloc, NULL, 0);
  4127. break;
  4128. case UPB_TYPE_BOOL:
  4129. f->defaultval.boolean = false;
  4130. break;
  4131. case UPB_TYPE_MESSAGE:
  4132. break;
  4133. }
  4134. }
  4135. static bool create_fielddef(
  4136. const symtab_addctx *ctx, const char *prefix, upb_msgdef *m,
  4137. const google_protobuf_FieldDescriptorProto *field_proto) {
  4138. upb_alloc *alloc = ctx->alloc;
  4139. upb_fielddef *f;
  4140. const google_protobuf_FieldOptions *options;
  4141. upb_strview name;
  4142. const char *full_name;
  4143. const char *json_name;
  4144. const char *shortname;
  4145. uint32_t field_number;
  4146. if (!google_protobuf_FieldDescriptorProto_has_name(field_proto)) {
  4147. upb_status_seterrmsg(ctx->status, "field has no name");
  4148. return false;
  4149. }
  4150. name = google_protobuf_FieldDescriptorProto_name(field_proto);
  4151. CHK(upb_isident(name, false, ctx->status));
  4152. full_name = makefullname(ctx, prefix, name);
  4153. shortname = shortdefname(full_name);
  4154. if (google_protobuf_FieldDescriptorProto_has_json_name(field_proto)) {
  4155. json_name = strviewdup(
  4156. ctx, google_protobuf_FieldDescriptorProto_json_name(field_proto));
  4157. } else {
  4158. json_name = makejsonname(shortname, ctx->alloc);
  4159. }
  4160. field_number = google_protobuf_FieldDescriptorProto_number(field_proto);
  4161. if (field_number == 0 || field_number > UPB_MAX_FIELDNUMBER) {
  4162. upb_status_seterrf(ctx->status, "invalid field number (%u)", field_number);
  4163. return false;
  4164. }
  4165. if (m) {
  4166. /* direct message field. */
  4167. upb_value v, field_v, json_v;
  4168. size_t json_size;
  4169. f = (upb_fielddef*)&m->fields[m->field_count++];
  4170. f->msgdef = m;
  4171. f->is_extension_ = false;
  4172. if (upb_strtable_lookup(&m->ntof, shortname, NULL)) {
  4173. upb_status_seterrf(ctx->status, "duplicate field name (%s)", shortname);
  4174. return false;
  4175. }
  4176. if (upb_strtable_lookup(&m->ntof, json_name, NULL)) {
  4177. upb_status_seterrf(ctx->status, "duplicate json_name (%s)", json_name);
  4178. return false;
  4179. }
  4180. if (upb_inttable_lookup(&m->itof, field_number, NULL)) {
  4181. upb_status_seterrf(ctx->status, "duplicate field number (%u)",
  4182. field_number);
  4183. return false;
  4184. }
  4185. field_v = pack_def(f, UPB_DEFTYPE_FIELD);
  4186. json_v = pack_def(f, UPB_DEFTYPE_FIELD_JSONNAME);
  4187. v = upb_value_constptr(f);
  4188. json_size = strlen(json_name);
  4189. CHK_OOM(
  4190. upb_strtable_insert3(&m->ntof, name.data, name.size, field_v, alloc));
  4191. CHK_OOM(upb_inttable_insert2(&m->itof, field_number, v, alloc));
  4192. if (strcmp(shortname, json_name) != 0) {
  4193. upb_strtable_insert3(&m->ntof, json_name, json_size, json_v, alloc);
  4194. }
  4195. if (ctx->layouts) {
  4196. const upb_msglayout_field *fields = m->layout->fields;
  4197. int count = m->layout->field_count;
  4198. bool found = false;
  4199. int i;
  4200. for (i = 0; i < count; i++) {
  4201. if (fields[i].number == field_number) {
  4202. f->layout_index = i;
  4203. found = true;
  4204. break;
  4205. }
  4206. }
  4207. UPB_ASSERT(found);
  4208. }
  4209. } else {
  4210. /* extension field. */
  4211. f = (upb_fielddef*)&ctx->file->exts[ctx->file->ext_count++];
  4212. f->is_extension_ = true;
  4213. CHK_OOM(symtab_add(ctx, full_name, pack_def(f, UPB_DEFTYPE_FIELD)));
  4214. }
  4215. f->full_name = full_name;
  4216. f->json_name = json_name;
  4217. f->file = ctx->file;
  4218. f->type_ = (int)google_protobuf_FieldDescriptorProto_type(field_proto);
  4219. f->label_ = (int)google_protobuf_FieldDescriptorProto_label(field_proto);
  4220. f->number_ = field_number;
  4221. f->oneof = NULL;
  4222. f->proto3_optional_ =
  4223. google_protobuf_FieldDescriptorProto_proto3_optional(field_proto);
  4224. /* We can't resolve the subdef or (in the case of extensions) the containing
  4225. * message yet, because it may not have been defined yet. We stash a pointer
  4226. * to the field_proto until later when we can properly resolve it. */
  4227. f->sub.unresolved = field_proto;
  4228. if (f->label_ == UPB_LABEL_REQUIRED && f->file->syntax == UPB_SYNTAX_PROTO3) {
  4229. upb_status_seterrf(ctx->status, "proto3 fields cannot be required (%s)",
  4230. f->full_name);
  4231. return false;
  4232. }
  4233. if (google_protobuf_FieldDescriptorProto_has_oneof_index(field_proto)) {
  4234. int oneof_index =
  4235. google_protobuf_FieldDescriptorProto_oneof_index(field_proto);
  4236. upb_oneofdef *oneof;
  4237. upb_value v = upb_value_constptr(f);
  4238. if (upb_fielddef_label(f) != UPB_LABEL_OPTIONAL) {
  4239. upb_status_seterrf(ctx->status,
  4240. "fields in oneof must have OPTIONAL label (%s)",
  4241. f->full_name);
  4242. return false;
  4243. }
  4244. if (!m) {
  4245. upb_status_seterrf(ctx->status,
  4246. "oneof_index provided for extension field (%s)",
  4247. f->full_name);
  4248. return false;
  4249. }
  4250. if (oneof_index >= m->oneof_count) {
  4251. upb_status_seterrf(ctx->status, "oneof_index out of range (%s)",
  4252. f->full_name);
  4253. return false;
  4254. }
  4255. oneof = (upb_oneofdef*)&m->oneofs[oneof_index];
  4256. f->oneof = oneof;
  4257. CHK(upb_inttable_insert2(&oneof->itof, f->number_, v, alloc));
  4258. CHK(upb_strtable_insert3(&oneof->ntof, name.data, name.size, v, alloc));
  4259. } else {
  4260. f->oneof = NULL;
  4261. }
  4262. options = google_protobuf_FieldDescriptorProto_has_options(field_proto) ?
  4263. google_protobuf_FieldDescriptorProto_options(field_proto) : NULL;
  4264. if (options && google_protobuf_FieldOptions_has_packed(options)) {
  4265. f->packed_ = google_protobuf_FieldOptions_packed(options);
  4266. } else {
  4267. /* Repeated fields default to packed for proto3 only. */
  4268. f->packed_ = upb_fielddef_isprimitive(f) &&
  4269. f->label_ == UPB_LABEL_REPEATED && f->file->syntax == UPB_SYNTAX_PROTO3;
  4270. }
  4271. if (options) {
  4272. f->lazy_ = google_protobuf_FieldOptions_lazy(options);
  4273. } else {
  4274. f->lazy_ = false;
  4275. }
  4276. return true;
  4277. }
  4278. static bool create_enumdef(
  4279. const symtab_addctx *ctx, const char *prefix,
  4280. const google_protobuf_EnumDescriptorProto *enum_proto) {
  4281. upb_enumdef *e;
  4282. const google_protobuf_EnumValueDescriptorProto *const *values;
  4283. upb_strview name;
  4284. size_t i, n;
  4285. name = google_protobuf_EnumDescriptorProto_name(enum_proto);
  4286. CHK(upb_isident(name, false, ctx->status));
  4287. e = (upb_enumdef*)&ctx->file->enums[ctx->file->enum_count++];
  4288. e->full_name = makefullname(ctx, prefix, name);
  4289. CHK_OOM(symtab_add(ctx, e->full_name, pack_def(e, UPB_DEFTYPE_ENUM)));
  4290. CHK_OOM(upb_strtable_init2(&e->ntoi, UPB_CTYPE_INT32, ctx->alloc));
  4291. CHK_OOM(upb_inttable_init2(&e->iton, UPB_CTYPE_CSTR, ctx->alloc));
  4292. e->file = ctx->file;
  4293. e->defaultval = 0;
  4294. values = google_protobuf_EnumDescriptorProto_value(enum_proto, &n);
  4295. if (n == 0) {
  4296. upb_status_seterrf(ctx->status,
  4297. "enums must contain at least one value (%s)",
  4298. e->full_name);
  4299. return false;
  4300. }
  4301. for (i = 0; i < n; i++) {
  4302. const google_protobuf_EnumValueDescriptorProto *value = values[i];
  4303. upb_strview name = google_protobuf_EnumValueDescriptorProto_name(value);
  4304. char *name2 = strviewdup(ctx, name);
  4305. int32_t num = google_protobuf_EnumValueDescriptorProto_number(value);
  4306. upb_value v = upb_value_int32(num);
  4307. if (i == 0 && e->file->syntax == UPB_SYNTAX_PROTO3 && num != 0) {
  4308. upb_status_seterrf(ctx->status,
  4309. "for proto3, the first enum value must be zero (%s)",
  4310. e->full_name);
  4311. return false;
  4312. }
  4313. if (upb_strtable_lookup(&e->ntoi, name2, NULL)) {
  4314. upb_status_seterrf(ctx->status, "duplicate enum label '%s'", name2);
  4315. return false;
  4316. }
  4317. CHK_OOM(name2)
  4318. CHK_OOM(
  4319. upb_strtable_insert3(&e->ntoi, name2, strlen(name2), v, ctx->alloc));
  4320. if (!upb_inttable_lookup(&e->iton, num, NULL)) {
  4321. upb_value v = upb_value_cstr(name2);
  4322. CHK_OOM(upb_inttable_insert2(&e->iton, num, v, ctx->alloc));
  4323. }
  4324. }
  4325. upb_inttable_compact2(&e->iton, ctx->alloc);
  4326. return true;
  4327. }
  4328. static bool create_msgdef(symtab_addctx *ctx, const char *prefix,
  4329. const google_protobuf_DescriptorProto *msg_proto) {
  4330. upb_msgdef *m;
  4331. const google_protobuf_MessageOptions *options;
  4332. const google_protobuf_OneofDescriptorProto *const *oneofs;
  4333. const google_protobuf_FieldDescriptorProto *const *fields;
  4334. const google_protobuf_EnumDescriptorProto *const *enums;
  4335. const google_protobuf_DescriptorProto *const *msgs;
  4336. size_t i, n;
  4337. upb_strview name;
  4338. name = google_protobuf_DescriptorProto_name(msg_proto);
  4339. CHK(upb_isident(name, false, ctx->status));
  4340. m = (upb_msgdef*)&ctx->file->msgs[ctx->file->msg_count++];
  4341. m->full_name = makefullname(ctx, prefix, name);
  4342. CHK_OOM(symtab_add(ctx, m->full_name, pack_def(m, UPB_DEFTYPE_MSG)));
  4343. CHK_OOM(upb_inttable_init2(&m->itof, UPB_CTYPE_CONSTPTR, ctx->alloc));
  4344. CHK_OOM(upb_strtable_init2(&m->ntof, UPB_CTYPE_CONSTPTR, ctx->alloc));
  4345. m->file = ctx->file;
  4346. m->map_entry = false;
  4347. options = google_protobuf_DescriptorProto_options(msg_proto);
  4348. if (options) {
  4349. m->map_entry = google_protobuf_MessageOptions_map_entry(options);
  4350. }
  4351. if (ctx->layouts) {
  4352. m->layout = *ctx->layouts;
  4353. ctx->layouts++;
  4354. } else {
  4355. /* Allocate now (to allow cross-linking), populate later. */
  4356. m->layout = upb_malloc(ctx->alloc, sizeof(*m->layout));
  4357. }
  4358. oneofs = google_protobuf_DescriptorProto_oneof_decl(msg_proto, &n);
  4359. m->oneof_count = 0;
  4360. m->oneofs = upb_malloc(ctx->alloc, sizeof(*m->oneofs) * n);
  4361. for (i = 0; i < n; i++) {
  4362. CHK(create_oneofdef(ctx, m, oneofs[i]));
  4363. }
  4364. fields = google_protobuf_DescriptorProto_field(msg_proto, &n);
  4365. m->field_count = 0;
  4366. m->fields = upb_malloc(ctx->alloc, sizeof(*m->fields) * n);
  4367. for (i = 0; i < n; i++) {
  4368. CHK(create_fielddef(ctx, m->full_name, m, fields[i]));
  4369. }
  4370. CHK(assign_msg_indices(m, ctx->status));
  4371. CHK(check_oneofs(m, ctx->status));
  4372. assign_msg_wellknowntype(m);
  4373. upb_inttable_compact2(&m->itof, ctx->alloc);
  4374. /* This message is built. Now build nested messages and enums. */
  4375. enums = google_protobuf_DescriptorProto_enum_type(msg_proto, &n);
  4376. for (i = 0; i < n; i++) {
  4377. CHK(create_enumdef(ctx, m->full_name, enums[i]));
  4378. }
  4379. msgs = google_protobuf_DescriptorProto_nested_type(msg_proto, &n);
  4380. for (i = 0; i < n; i++) {
  4381. CHK(create_msgdef(ctx, m->full_name, msgs[i]));
  4382. }
  4383. return true;
  4384. }
  4385. typedef struct {
  4386. int msg_count;
  4387. int enum_count;
  4388. int ext_count;
  4389. } decl_counts;
  4390. static void count_types_in_msg(const google_protobuf_DescriptorProto *msg_proto,
  4391. decl_counts *counts) {
  4392. const google_protobuf_DescriptorProto *const *msgs;
  4393. size_t i, n;
  4394. counts->msg_count++;
  4395. msgs = google_protobuf_DescriptorProto_nested_type(msg_proto, &n);
  4396. for (i = 0; i < n; i++) {
  4397. count_types_in_msg(msgs[i], counts);
  4398. }
  4399. google_protobuf_DescriptorProto_enum_type(msg_proto, &n);
  4400. counts->enum_count += n;
  4401. google_protobuf_DescriptorProto_extension(msg_proto, &n);
  4402. counts->ext_count += n;
  4403. }
  4404. static void count_types_in_file(
  4405. const google_protobuf_FileDescriptorProto *file_proto,
  4406. decl_counts *counts) {
  4407. const google_protobuf_DescriptorProto *const *msgs;
  4408. size_t i, n;
  4409. msgs = google_protobuf_FileDescriptorProto_message_type(file_proto, &n);
  4410. for (i = 0; i < n; i++) {
  4411. count_types_in_msg(msgs[i], counts);
  4412. }
  4413. google_protobuf_FileDescriptorProto_enum_type(file_proto, &n);
  4414. counts->enum_count += n;
  4415. google_protobuf_FileDescriptorProto_extension(file_proto, &n);
  4416. counts->ext_count += n;
  4417. }
  4418. static bool resolve_fielddef(const symtab_addctx *ctx, const char *prefix,
  4419. upb_fielddef *f) {
  4420. upb_strview name;
  4421. const google_protobuf_FieldDescriptorProto *field_proto = f->sub.unresolved;
  4422. if (f->is_extension_) {
  4423. if (!google_protobuf_FieldDescriptorProto_has_extendee(field_proto)) {
  4424. upb_status_seterrf(ctx->status,
  4425. "extension for field '%s' had no extendee",
  4426. f->full_name);
  4427. return false;
  4428. }
  4429. name = google_protobuf_FieldDescriptorProto_extendee(field_proto);
  4430. f->msgdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_MSG);
  4431. CHK(f->msgdef);
  4432. }
  4433. if ((upb_fielddef_issubmsg(f) || f->type_ == UPB_DESCRIPTOR_TYPE_ENUM) &&
  4434. !google_protobuf_FieldDescriptorProto_has_type_name(field_proto)) {
  4435. upb_status_seterrf(ctx->status, "field '%s' is missing type name",
  4436. f->full_name);
  4437. return false;
  4438. }
  4439. name = google_protobuf_FieldDescriptorProto_type_name(field_proto);
  4440. if (upb_fielddef_issubmsg(f)) {
  4441. f->sub.msgdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_MSG);
  4442. CHK(f->sub.msgdef);
  4443. } else if (f->type_ == UPB_DESCRIPTOR_TYPE_ENUM) {
  4444. f->sub.enumdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_ENUM);
  4445. CHK(f->sub.enumdef);
  4446. }
  4447. /* Have to delay resolving of the default value until now because of the enum
  4448. * case, since enum defaults are specified with a label. */
  4449. if (google_protobuf_FieldDescriptorProto_has_default_value(field_proto)) {
  4450. upb_strview defaultval =
  4451. google_protobuf_FieldDescriptorProto_default_value(field_proto);
  4452. if (f->file->syntax == UPB_SYNTAX_PROTO3) {
  4453. upb_status_seterrf(ctx->status,
  4454. "proto3 fields cannot have explicit defaults (%s)",
  4455. f->full_name);
  4456. return false;
  4457. }
  4458. if (upb_fielddef_issubmsg(f)) {
  4459. upb_status_seterrf(ctx->status,
  4460. "message fields cannot have explicit defaults (%s)",
  4461. f->full_name);
  4462. return false;
  4463. }
  4464. if (!parse_default(ctx, defaultval.data, defaultval.size, f)) {
  4465. upb_status_seterrf(ctx->status,
  4466. "couldn't parse default '" UPB_STRVIEW_FORMAT
  4467. "' for field (%s)",
  4468. UPB_STRVIEW_ARGS(defaultval), f->full_name);
  4469. return false;
  4470. }
  4471. } else {
  4472. set_default_default(ctx, f);
  4473. }
  4474. return true;
  4475. }
  4476. static bool build_filedef(
  4477. symtab_addctx *ctx, upb_filedef *file,
  4478. const google_protobuf_FileDescriptorProto *file_proto) {
  4479. upb_alloc *alloc = ctx->alloc;
  4480. const google_protobuf_FileOptions *file_options_proto;
  4481. const google_protobuf_DescriptorProto *const *msgs;
  4482. const google_protobuf_EnumDescriptorProto *const *enums;
  4483. const google_protobuf_FieldDescriptorProto *const *exts;
  4484. const upb_strview* strs;
  4485. size_t i, n;
  4486. decl_counts counts = {0, 0, 0};
  4487. count_types_in_file(file_proto, &counts);
  4488. file->msgs = upb_malloc(alloc, sizeof(*file->msgs) * counts.msg_count);
  4489. file->enums = upb_malloc(alloc, sizeof(*file->enums) * counts.enum_count);
  4490. file->exts = upb_malloc(alloc, sizeof(*file->exts) * counts.ext_count);
  4491. CHK_OOM(counts.msg_count == 0 || file->msgs);
  4492. CHK_OOM(counts.enum_count == 0 || file->enums);
  4493. CHK_OOM(counts.ext_count == 0 || file->exts);
  4494. /* We increment these as defs are added. */
  4495. file->msg_count = 0;
  4496. file->enum_count = 0;
  4497. file->ext_count = 0;
  4498. if (!google_protobuf_FileDescriptorProto_has_name(file_proto)) {
  4499. upb_status_seterrmsg(ctx->status, "File has no name");
  4500. return false;
  4501. }
  4502. file->name =
  4503. strviewdup(ctx, google_protobuf_FileDescriptorProto_name(file_proto));
  4504. file->phpprefix = NULL;
  4505. file->phpnamespace = NULL;
  4506. if (google_protobuf_FileDescriptorProto_has_package(file_proto)) {
  4507. upb_strview package =
  4508. google_protobuf_FileDescriptorProto_package(file_proto);
  4509. CHK(upb_isident(package, true, ctx->status));
  4510. file->package = strviewdup(ctx, package);
  4511. } else {
  4512. file->package = NULL;
  4513. }
  4514. if (google_protobuf_FileDescriptorProto_has_syntax(file_proto)) {
  4515. upb_strview syntax =
  4516. google_protobuf_FileDescriptorProto_syntax(file_proto);
  4517. if (streql_view(syntax, "proto2")) {
  4518. file->syntax = UPB_SYNTAX_PROTO2;
  4519. } else if (streql_view(syntax, "proto3")) {
  4520. file->syntax = UPB_SYNTAX_PROTO3;
  4521. } else {
  4522. upb_status_seterrf(ctx->status, "Invalid syntax '" UPB_STRVIEW_FORMAT "'",
  4523. UPB_STRVIEW_ARGS(syntax));
  4524. return false;
  4525. }
  4526. } else {
  4527. file->syntax = UPB_SYNTAX_PROTO2;
  4528. }
  4529. /* Read options. */
  4530. file_options_proto = google_protobuf_FileDescriptorProto_options(file_proto);
  4531. if (file_options_proto) {
  4532. if (google_protobuf_FileOptions_has_php_class_prefix(file_options_proto)) {
  4533. file->phpprefix = strviewdup(
  4534. ctx,
  4535. google_protobuf_FileOptions_php_class_prefix(file_options_proto));
  4536. }
  4537. if (google_protobuf_FileOptions_has_php_namespace(file_options_proto)) {
  4538. file->phpnamespace = strviewdup(
  4539. ctx, google_protobuf_FileOptions_php_namespace(file_options_proto));
  4540. }
  4541. }
  4542. /* Verify dependencies. */
  4543. strs = google_protobuf_FileDescriptorProto_dependency(file_proto, &n);
  4544. file->deps = upb_malloc(alloc, sizeof(*file->deps) * n) ;
  4545. CHK_OOM(n == 0 || file->deps);
  4546. for (i = 0; i < n; i++) {
  4547. upb_strview dep_name = strs[i];
  4548. upb_value v;
  4549. if (!upb_strtable_lookup2(&ctx->symtab->files, dep_name.data,
  4550. dep_name.size, &v)) {
  4551. upb_status_seterrf(ctx->status,
  4552. "Depends on file '" UPB_STRVIEW_FORMAT
  4553. "', but it has not been loaded",
  4554. UPB_STRVIEW_ARGS(dep_name));
  4555. return false;
  4556. }
  4557. file->deps[i] = upb_value_getconstptr(v);
  4558. }
  4559. /* Create messages. */
  4560. msgs = google_protobuf_FileDescriptorProto_message_type(file_proto, &n);
  4561. for (i = 0; i < n; i++) {
  4562. CHK(create_msgdef(ctx, file->package, msgs[i]));
  4563. }
  4564. /* Create enums. */
  4565. enums = google_protobuf_FileDescriptorProto_enum_type(file_proto, &n);
  4566. for (i = 0; i < n; i++) {
  4567. CHK(create_enumdef(ctx, file->package, enums[i]));
  4568. }
  4569. /* Create extensions. */
  4570. exts = google_protobuf_FileDescriptorProto_extension(file_proto, &n);
  4571. file->exts = upb_malloc(alloc, sizeof(*file->exts) * n);
  4572. CHK_OOM(n == 0 || file->exts);
  4573. for (i = 0; i < n; i++) {
  4574. CHK(create_fielddef(ctx, file->package, NULL, exts[i]));
  4575. }
  4576. /* Now that all names are in the table, build layouts and resolve refs. */
  4577. for (i = 0; i < (size_t)file->ext_count; i++) {
  4578. CHK(resolve_fielddef(ctx, file->package, (upb_fielddef*)&file->exts[i]));
  4579. }
  4580. for (i = 0; i < (size_t)file->msg_count; i++) {
  4581. const upb_msgdef *m = &file->msgs[i];
  4582. int j;
  4583. for (j = 0; j < m->field_count; j++) {
  4584. CHK(resolve_fielddef(ctx, m->full_name, (upb_fielddef*)&m->fields[j]));
  4585. }
  4586. }
  4587. if (!ctx->layouts) {
  4588. for (i = 0; i < (size_t)file->msg_count; i++) {
  4589. const upb_msgdef *m = &file->msgs[i];
  4590. make_layout(ctx->symtab, m);
  4591. }
  4592. }
  4593. return true;
  4594. }
  4595. static bool upb_symtab_addtotabs(upb_symtab *s, symtab_addctx *ctx) {
  4596. const upb_filedef *file = ctx->file;
  4597. upb_alloc *alloc = upb_arena_alloc(s->arena);
  4598. upb_strtable_iter iter;
  4599. CHK_OOM(upb_strtable_insert3(&s->files, file->name, strlen(file->name),
  4600. upb_value_constptr(file), alloc));
  4601. upb_strtable_begin(&iter, ctx->addtab);
  4602. for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) {
  4603. upb_strview key = upb_strtable_iter_key(&iter);
  4604. upb_value value = upb_strtable_iter_value(&iter);
  4605. CHK_OOM(upb_strtable_insert3(&s->syms, key.data, key.size, value, alloc));
  4606. }
  4607. return true;
  4608. }
  4609. /* upb_filedef ****************************************************************/
  4610. const char *upb_filedef_name(const upb_filedef *f) {
  4611. return f->name;
  4612. }
  4613. const char *upb_filedef_package(const upb_filedef *f) {
  4614. return f->package;
  4615. }
  4616. const char *upb_filedef_phpprefix(const upb_filedef *f) {
  4617. return f->phpprefix;
  4618. }
  4619. const char *upb_filedef_phpnamespace(const upb_filedef *f) {
  4620. return f->phpnamespace;
  4621. }
  4622. upb_syntax_t upb_filedef_syntax(const upb_filedef *f) {
  4623. return f->syntax;
  4624. }
  4625. int upb_filedef_msgcount(const upb_filedef *f) {
  4626. return f->msg_count;
  4627. }
  4628. int upb_filedef_depcount(const upb_filedef *f) {
  4629. return f->dep_count;
  4630. }
  4631. int upb_filedef_enumcount(const upb_filedef *f) {
  4632. return f->enum_count;
  4633. }
  4634. const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i) {
  4635. return i < 0 || i >= f->dep_count ? NULL : f->deps[i];
  4636. }
  4637. const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i) {
  4638. return i < 0 || i >= f->msg_count ? NULL : &f->msgs[i];
  4639. }
  4640. const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i) {
  4641. return i < 0 || i >= f->enum_count ? NULL : &f->enums[i];
  4642. }
  4643. void upb_symtab_free(upb_symtab *s) {
  4644. upb_arena_free(s->arena);
  4645. upb_gfree(s);
  4646. }
  4647. upb_symtab *upb_symtab_new(void) {
  4648. upb_symtab *s = upb_gmalloc(sizeof(*s));
  4649. upb_alloc *alloc;
  4650. if (!s) {
  4651. return NULL;
  4652. }
  4653. s->arena = upb_arena_new();
  4654. alloc = upb_arena_alloc(s->arena);
  4655. if (!upb_strtable_init2(&s->syms, UPB_CTYPE_CONSTPTR, alloc) ||
  4656. !upb_strtable_init2(&s->files, UPB_CTYPE_CONSTPTR, alloc)) {
  4657. upb_arena_free(s->arena);
  4658. upb_gfree(s);
  4659. s = NULL;
  4660. }
  4661. return s;
  4662. }
  4663. const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym) {
  4664. upb_value v;
  4665. return upb_strtable_lookup(&s->syms, sym, &v) ?
  4666. unpack_def(v, UPB_DEFTYPE_MSG) : NULL;
  4667. }
  4668. const upb_msgdef *upb_symtab_lookupmsg2(const upb_symtab *s, const char *sym,
  4669. size_t len) {
  4670. upb_value v;
  4671. return upb_strtable_lookup2(&s->syms, sym, len, &v) ?
  4672. unpack_def(v, UPB_DEFTYPE_MSG) : NULL;
  4673. }
  4674. const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) {
  4675. upb_value v;
  4676. return upb_strtable_lookup(&s->syms, sym, &v) ?
  4677. unpack_def(v, UPB_DEFTYPE_ENUM) : NULL;
  4678. }
  4679. const upb_filedef *upb_symtab_lookupfile(const upb_symtab *s, const char *name) {
  4680. upb_value v;
  4681. return upb_strtable_lookup(&s->files, name, &v) ? upb_value_getconstptr(v)
  4682. : NULL;
  4683. }
  4684. const upb_filedef *upb_symtab_lookupfile2(
  4685. const upb_symtab *s, const char *name, size_t len) {
  4686. upb_value v;
  4687. return upb_strtable_lookup2(&s->files, name, len, &v) ?
  4688. upb_value_getconstptr(v) : NULL;
  4689. }
  4690. int upb_symtab_filecount(const upb_symtab *s) {
  4691. return (int)upb_strtable_count(&s->files);
  4692. }
  4693. static const upb_filedef *_upb_symtab_addfile(
  4694. upb_symtab *s, const google_protobuf_FileDescriptorProto *file_proto,
  4695. const upb_msglayout **layouts, upb_status *status) {
  4696. upb_arena *tmparena = upb_arena_new();
  4697. upb_strtable addtab;
  4698. upb_alloc *alloc = upb_arena_alloc(s->arena);
  4699. upb_filedef *file = upb_malloc(alloc, sizeof(*file));
  4700. bool ok;
  4701. symtab_addctx ctx;
  4702. ctx.file = file;
  4703. ctx.symtab = s;
  4704. ctx.alloc = alloc;
  4705. ctx.tmp = upb_arena_alloc(tmparena);
  4706. ctx.addtab = &addtab;
  4707. ctx.layouts = layouts;
  4708. ctx.status = status;
  4709. ok = file && upb_strtable_init2(&addtab, UPB_CTYPE_CONSTPTR, ctx.tmp) &&
  4710. build_filedef(&ctx, file, file_proto) && upb_symtab_addtotabs(s, &ctx);
  4711. upb_arena_free(tmparena);
  4712. return ok ? file : NULL;
  4713. }
  4714. const upb_filedef *upb_symtab_addfile(
  4715. upb_symtab *s, const google_protobuf_FileDescriptorProto *file_proto,
  4716. upb_status *status) {
  4717. return _upb_symtab_addfile(s, file_proto, NULL, status);
  4718. }
  4719. /* Include here since we want most of this file to be stdio-free. */
  4720. #include <stdio.h>
  4721. bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init) {
  4722. /* Since this function should never fail (it would indicate a bug in upb) we
  4723. * print errors to stderr instead of returning error status to the user. */
  4724. upb_def_init **deps = init->deps;
  4725. google_protobuf_FileDescriptorProto *file;
  4726. upb_arena *arena;
  4727. upb_status status;
  4728. upb_status_clear(&status);
  4729. if (upb_strtable_lookup(&s->files, init->filename, NULL)) {
  4730. return true;
  4731. }
  4732. arena = upb_arena_new();
  4733. for (; *deps; deps++) {
  4734. if (!_upb_symtab_loaddefinit(s, *deps)) goto err;
  4735. }
  4736. file = google_protobuf_FileDescriptorProto_parse(
  4737. init->descriptor.data, init->descriptor.size, arena);
  4738. if (!file) {
  4739. upb_status_seterrf(
  4740. &status,
  4741. "Failed to parse compiled-in descriptor for file '%s'. This should "
  4742. "never happen.",
  4743. init->filename);
  4744. goto err;
  4745. }
  4746. if (!_upb_symtab_addfile(s, file, init->layouts, &status)) goto err;
  4747. upb_arena_free(arena);
  4748. return true;
  4749. err:
  4750. fprintf(stderr, "Error loading compiled-in descriptor: %s\n",
  4751. upb_status_errmsg(&status));
  4752. upb_arena_free(arena);
  4753. return false;
  4754. }
  4755. #undef CHK
  4756. #undef CHK_OOM
  4757. #include <string.h>
  4758. static size_t get_field_size(const upb_msglayout_field *f) {
  4759. static unsigned char sizes[] = {
  4760. 0,/* 0 */
  4761. 8, /* UPB_DESCRIPTOR_TYPE_DOUBLE */
  4762. 4, /* UPB_DESCRIPTOR_TYPE_FLOAT */
  4763. 8, /* UPB_DESCRIPTOR_TYPE_INT64 */
  4764. 8, /* UPB_DESCRIPTOR_TYPE_UINT64 */
  4765. 4, /* UPB_DESCRIPTOR_TYPE_INT32 */
  4766. 8, /* UPB_DESCRIPTOR_TYPE_FIXED64 */
  4767. 4, /* UPB_DESCRIPTOR_TYPE_FIXED32 */
  4768. 1, /* UPB_DESCRIPTOR_TYPE_BOOL */
  4769. sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_STRING */
  4770. sizeof(void*), /* UPB_DESCRIPTOR_TYPE_GROUP */
  4771. sizeof(void*), /* UPB_DESCRIPTOR_TYPE_MESSAGE */
  4772. sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_BYTES */
  4773. 4, /* UPB_DESCRIPTOR_TYPE_UINT32 */
  4774. 4, /* UPB_DESCRIPTOR_TYPE_ENUM */
  4775. 4, /* UPB_DESCRIPTOR_TYPE_SFIXED32 */
  4776. 8, /* UPB_DESCRIPTOR_TYPE_SFIXED64 */
  4777. 4, /* UPB_DESCRIPTOR_TYPE_SINT32 */
  4778. 8, /* UPB_DESCRIPTOR_TYPE_SINT64 */
  4779. };
  4780. return _upb_repeated_or_map(f) ? sizeof(void *) : sizes[f->descriptortype];
  4781. }
  4782. /* Strings/bytes are special-cased in maps. */
  4783. static char _upb_fieldtype_to_mapsize[12] = {
  4784. 0,
  4785. 1, /* UPB_TYPE_BOOL */
  4786. 4, /* UPB_TYPE_FLOAT */
  4787. 4, /* UPB_TYPE_INT32 */
  4788. 4, /* UPB_TYPE_UINT32 */
  4789. 4, /* UPB_TYPE_ENUM */
  4790. sizeof(void*), /* UPB_TYPE_MESSAGE */
  4791. 8, /* UPB_TYPE_DOUBLE */
  4792. 8, /* UPB_TYPE_INT64 */
  4793. 8, /* UPB_TYPE_UINT64 */
  4794. 0, /* UPB_TYPE_STRING */
  4795. 0, /* UPB_TYPE_BYTES */
  4796. };
  4797. /** upb_msg *******************************************************************/
  4798. upb_msg *upb_msg_new(const upb_msgdef *m, upb_arena *a) {
  4799. return _upb_msg_new(upb_msgdef_layout(m), a);
  4800. }
  4801. static bool in_oneof(const upb_msglayout_field *field) {
  4802. return field->presence < 0;
  4803. }
  4804. static upb_msgval _upb_msg_getraw(const upb_msg *msg, const upb_fielddef *f) {
  4805. const upb_msglayout_field *field = upb_fielddef_layout(f);
  4806. const char *mem = UPB_PTR_AT(msg, field->offset, char);
  4807. upb_msgval val = {0};
  4808. memcpy(&val, mem, get_field_size(field));
  4809. return val;
  4810. }
  4811. bool upb_msg_has(const upb_msg *msg, const upb_fielddef *f) {
  4812. const upb_msglayout_field *field = upb_fielddef_layout(f);
  4813. if (in_oneof(field)) {
  4814. return _upb_getoneofcase_field(msg, field) == field->number;
  4815. } else if (field->presence > 0) {
  4816. return _upb_hasbit_field(msg, field);
  4817. } else {
  4818. UPB_ASSERT(field->descriptortype == UPB_DESCRIPTOR_TYPE_MESSAGE ||
  4819. field->descriptortype == UPB_DESCRIPTOR_TYPE_GROUP);
  4820. return _upb_msg_getraw(msg, f).msg_val != NULL;
  4821. }
  4822. }
  4823. const upb_fielddef *upb_msg_whichoneof(const upb_msg *msg,
  4824. const upb_oneofdef *o) {
  4825. upb_oneof_iter i;
  4826. const upb_fielddef *f;
  4827. const upb_msglayout_field *field;
  4828. const upb_msgdef *m = upb_oneofdef_containingtype(o);
  4829. uint32_t oneof_case;
  4830. /* This is far from optimal. */
  4831. upb_oneof_begin(&i, o);
  4832. if (upb_oneof_done(&i)) return false;
  4833. f = upb_oneof_iter_field(&i);
  4834. field = upb_fielddef_layout(f);
  4835. oneof_case = _upb_getoneofcase_field(msg, field);
  4836. return oneof_case ? upb_msgdef_itof(m, oneof_case) : NULL;
  4837. }
  4838. upb_msgval upb_msg_get(const upb_msg *msg, const upb_fielddef *f) {
  4839. if (!upb_fielddef_haspresence(f) || upb_msg_has(msg, f)) {
  4840. return _upb_msg_getraw(msg, f);
  4841. } else {
  4842. /* TODO(haberman): change upb_fielddef to not require this switch(). */
  4843. upb_msgval val = {0};
  4844. switch (upb_fielddef_type(f)) {
  4845. case UPB_TYPE_INT32:
  4846. case UPB_TYPE_ENUM:
  4847. val.int32_val = upb_fielddef_defaultint32(f);
  4848. break;
  4849. case UPB_TYPE_INT64:
  4850. val.int64_val = upb_fielddef_defaultint64(f);
  4851. break;
  4852. case UPB_TYPE_UINT32:
  4853. val.uint32_val = upb_fielddef_defaultuint32(f);
  4854. break;
  4855. case UPB_TYPE_UINT64:
  4856. val.uint64_val = upb_fielddef_defaultuint64(f);
  4857. break;
  4858. case UPB_TYPE_FLOAT:
  4859. val.float_val = upb_fielddef_defaultfloat(f);
  4860. break;
  4861. case UPB_TYPE_DOUBLE:
  4862. val.double_val = upb_fielddef_defaultdouble(f);
  4863. break;
  4864. case UPB_TYPE_BOOL:
  4865. val.double_val = upb_fielddef_defaultbool(f);
  4866. break;
  4867. case UPB_TYPE_STRING:
  4868. case UPB_TYPE_BYTES:
  4869. val.str_val.data = upb_fielddef_defaultstr(f, &val.str_val.size);
  4870. break;
  4871. case UPB_TYPE_MESSAGE:
  4872. val.msg_val = NULL;
  4873. break;
  4874. }
  4875. return val;
  4876. }
  4877. }
  4878. upb_mutmsgval upb_msg_mutable(upb_msg *msg, const upb_fielddef *f,
  4879. upb_arena *a) {
  4880. const upb_msglayout_field *field = upb_fielddef_layout(f);
  4881. upb_mutmsgval ret;
  4882. char *mem = UPB_PTR_AT(msg, field->offset, char);
  4883. bool wrong_oneof =
  4884. in_oneof(field) && _upb_getoneofcase_field(msg, field) != field->number;
  4885. memcpy(&ret, mem, sizeof(void*));
  4886. if (a && (!ret.msg || wrong_oneof)) {
  4887. if (upb_fielddef_ismap(f)) {
  4888. const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
  4889. const upb_fielddef *key = upb_msgdef_itof(entry, UPB_MAPENTRY_KEY);
  4890. const upb_fielddef *value = upb_msgdef_itof(entry, UPB_MAPENTRY_VALUE);
  4891. ret.map = upb_map_new(a, upb_fielddef_type(key), upb_fielddef_type(value));
  4892. } else if (upb_fielddef_isseq(f)) {
  4893. ret.array = upb_array_new(a, upb_fielddef_type(f));
  4894. } else {
  4895. UPB_ASSERT(upb_fielddef_issubmsg(f));
  4896. ret.msg = upb_msg_new(upb_fielddef_msgsubdef(f), a);
  4897. }
  4898. memcpy(mem, &ret, sizeof(void*));
  4899. if (wrong_oneof) {
  4900. *_upb_oneofcase_field(msg, field) = field->number;
  4901. } else if (field->presence > 0) {
  4902. _upb_sethas_field(msg, field);
  4903. }
  4904. }
  4905. return ret;
  4906. }
  4907. void upb_msg_set(upb_msg *msg, const upb_fielddef *f, upb_msgval val,
  4908. upb_arena *a) {
  4909. const upb_msglayout_field *field = upb_fielddef_layout(f);
  4910. char *mem = UPB_PTR_AT(msg, field->offset, char);
  4911. UPB_UNUSED(a); /* We reserve the right to make set insert into a map. */
  4912. memcpy(mem, &val, get_field_size(field));
  4913. if (field->presence > 0) {
  4914. _upb_sethas_field(msg, field);
  4915. } else if (in_oneof(field)) {
  4916. *_upb_oneofcase_field(msg, field) = field->number;
  4917. }
  4918. }
  4919. void upb_msg_clearfield(upb_msg *msg, const upb_fielddef *f) {
  4920. const upb_msglayout_field *field = upb_fielddef_layout(f);
  4921. char *mem = UPB_PTR_AT(msg, field->offset, char);
  4922. if (field->presence > 0) {
  4923. _upb_clearhas_field(msg, field);
  4924. } else if (in_oneof(field)) {
  4925. uint32_t *oneof_case = _upb_oneofcase_field(msg, field);
  4926. if (*oneof_case != field->number) return;
  4927. *oneof_case = 0;
  4928. }
  4929. memset(mem, 0, get_field_size(field));
  4930. }
  4931. void upb_msg_clear(upb_msg *msg, const upb_msgdef *m) {
  4932. _upb_msg_clear(msg, upb_msgdef_layout(m));
  4933. }
  4934. bool upb_msg_next(const upb_msg *msg, const upb_msgdef *m,
  4935. const upb_symtab *ext_pool, const upb_fielddef **out_f,
  4936. upb_msgval *out_val, size_t *iter) {
  4937. size_t i = *iter;
  4938. const upb_msgval zero = {0};
  4939. const upb_fielddef *f;
  4940. UPB_UNUSED(ext_pool);
  4941. while ((f = _upb_msgdef_field(m, (int)++i)) != NULL) {
  4942. upb_msgval val = _upb_msg_getraw(msg, f);
  4943. /* Skip field if unset or empty. */
  4944. if (upb_fielddef_haspresence(f)) {
  4945. if (!upb_msg_has(msg, f)) continue;
  4946. } else {
  4947. upb_msgval test = val;
  4948. if (upb_fielddef_isstring(f) && !upb_fielddef_isseq(f)) {
  4949. /* Clear string pointer, only size matters (ptr could be non-NULL). */
  4950. test.str_val.data = NULL;
  4951. }
  4952. /* Continue if NULL or 0. */
  4953. if (memcmp(&test, &zero, sizeof(test)) == 0) continue;
  4954. /* Continue on empty array or map. */
  4955. if (upb_fielddef_ismap(f)) {
  4956. if (upb_map_size(test.map_val) == 0) continue;
  4957. } else if (upb_fielddef_isseq(f)) {
  4958. if (upb_array_size(test.array_val) == 0) continue;
  4959. }
  4960. }
  4961. *out_val = val;
  4962. *out_f = f;
  4963. *iter = i;
  4964. return true;
  4965. }
  4966. *iter = i;
  4967. return false;
  4968. }
  4969. bool _upb_msg_discardunknown(upb_msg *msg, const upb_msgdef *m, int depth) {
  4970. size_t iter = UPB_MSG_BEGIN;
  4971. const upb_fielddef *f;
  4972. upb_msgval val;
  4973. bool ret = true;
  4974. if (--depth == 0) return false;
  4975. _upb_msg_discardunknown_shallow(msg);
  4976. while (upb_msg_next(msg, m, NULL /*ext_pool*/, &f, &val, &iter)) {
  4977. const upb_msgdef *subm = upb_fielddef_msgsubdef(f);
  4978. if (!subm) continue;
  4979. if (upb_fielddef_ismap(f)) {
  4980. const upb_fielddef *val_f = upb_msgdef_itof(subm, 2);
  4981. const upb_msgdef *val_m = upb_fielddef_msgsubdef(val_f);
  4982. upb_map *map = (upb_map*)val.map_val;
  4983. size_t iter = UPB_MAP_BEGIN;
  4984. if (!val_m) continue;
  4985. while (upb_mapiter_next(map, &iter)) {
  4986. upb_msgval map_val = upb_mapiter_value(map, iter);
  4987. if (!_upb_msg_discardunknown((upb_msg*)map_val.msg_val, val_m, depth)) {
  4988. ret = false;
  4989. }
  4990. }
  4991. } else if (upb_fielddef_isseq(f)) {
  4992. const upb_array *arr = val.array_val;
  4993. size_t i, n = upb_array_size(arr);
  4994. for (i = 0; i < n; i++) {
  4995. upb_msgval elem = upb_array_get(arr, i);
  4996. if (!_upb_msg_discardunknown((upb_msg*)elem.msg_val, subm, depth)) {
  4997. ret = false;
  4998. }
  4999. }
  5000. } else {
  5001. if (!_upb_msg_discardunknown((upb_msg*)val.msg_val, subm, depth)) {
  5002. ret = false;
  5003. }
  5004. }
  5005. }
  5006. return ret;
  5007. }
  5008. bool upb_msg_discardunknown(upb_msg *msg, const upb_msgdef *m, int maxdepth) {
  5009. return _upb_msg_discardunknown(msg, m, maxdepth);
  5010. }
  5011. /** upb_array *****************************************************************/
  5012. upb_array *upb_array_new(upb_arena *a, upb_fieldtype_t type) {
  5013. return _upb_array_new(a, type);
  5014. }
  5015. size_t upb_array_size(const upb_array *arr) {
  5016. return arr->len;
  5017. }
  5018. upb_msgval upb_array_get(const upb_array *arr, size_t i) {
  5019. upb_msgval ret;
  5020. const char* data = _upb_array_constptr(arr);
  5021. int lg2 = arr->data & 7;
  5022. UPB_ASSERT(i < arr->len);
  5023. memcpy(&ret, data + (i << lg2), 1 << lg2);
  5024. return ret;
  5025. }
  5026. void upb_array_set(upb_array *arr, size_t i, upb_msgval val) {
  5027. char* data = _upb_array_ptr(arr);
  5028. int lg2 = arr->data & 7;
  5029. UPB_ASSERT(i < arr->len);
  5030. memcpy(data + (i << lg2), &val, 1 << lg2);
  5031. }
  5032. bool upb_array_append(upb_array *arr, upb_msgval val, upb_arena *arena) {
  5033. if (!_upb_array_realloc(arr, arr->len + 1, arena)) {
  5034. return false;
  5035. }
  5036. arr->len++;
  5037. upb_array_set(arr, arr->len - 1, val);
  5038. return true;
  5039. }
  5040. bool upb_array_resize(upb_array *arr, size_t size, upb_arena *arena) {
  5041. return _upb_array_resize(arr, size, arena);
  5042. }
  5043. /** upb_map *******************************************************************/
  5044. upb_map *upb_map_new(upb_arena *a, upb_fieldtype_t key_type,
  5045. upb_fieldtype_t value_type) {
  5046. return _upb_map_new(a, _upb_fieldtype_to_mapsize[key_type],
  5047. _upb_fieldtype_to_mapsize[value_type]);
  5048. }
  5049. size_t upb_map_size(const upb_map *map) {
  5050. return _upb_map_size(map);
  5051. }
  5052. bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val) {
  5053. return _upb_map_get(map, &key, map->key_size, val, map->val_size);
  5054. }
  5055. bool upb_map_set(upb_map *map, upb_msgval key, upb_msgval val,
  5056. upb_arena *arena) {
  5057. return _upb_map_set(map, &key, map->key_size, &val, map->val_size, arena);
  5058. }
  5059. bool upb_map_delete(upb_map *map, upb_msgval key) {
  5060. return _upb_map_delete(map, &key, map->key_size);
  5061. }
  5062. bool upb_mapiter_next(const upb_map *map, size_t *iter) {
  5063. return _upb_map_next(map, iter);
  5064. }
  5065. bool upb_mapiter_done(const upb_map *map, size_t iter) {
  5066. upb_strtable_iter i;
  5067. UPB_ASSERT(iter != UPB_MAP_BEGIN);
  5068. i.t = &map->table;
  5069. i.index = iter;
  5070. return upb_strtable_done(&i);
  5071. }
  5072. /* Returns the key and value for this entry of the map. */
  5073. upb_msgval upb_mapiter_key(const upb_map *map, size_t iter) {
  5074. upb_strtable_iter i;
  5075. upb_msgval ret;
  5076. i.t = &map->table;
  5077. i.index = iter;
  5078. _upb_map_fromkey(upb_strtable_iter_key(&i), &ret, map->key_size);
  5079. return ret;
  5080. }
  5081. upb_msgval upb_mapiter_value(const upb_map *map, size_t iter) {
  5082. upb_strtable_iter i;
  5083. upb_msgval ret;
  5084. i.t = &map->table;
  5085. i.index = iter;
  5086. _upb_map_fromvalue(upb_strtable_iter_value(&i), &ret, map->val_size);
  5087. return ret;
  5088. }
  5089. /* void upb_mapiter_setvalue(upb_map *map, size_t iter, upb_msgval value); */
  5090. #ifdef UPB_MSVC_VSNPRINTF
  5091. /* Visual C++ earlier than 2015 doesn't have standard C99 snprintf and
  5092. * vsnprintf. To support them, missing functions are manually implemented
  5093. * using the existing secure functions. */
  5094. int msvc_vsnprintf(char* s, size_t n, const char* format, va_list arg) {
  5095. if (!s) {
  5096. return _vscprintf(format, arg);
  5097. }
  5098. int ret = _vsnprintf_s(s, n, _TRUNCATE, format, arg);
  5099. if (ret < 0) {
  5100. ret = _vscprintf(format, arg);
  5101. }
  5102. return ret;
  5103. }
  5104. int msvc_snprintf(char* s, size_t n, const char* format, ...) {
  5105. va_list arg;
  5106. va_start(arg, format);
  5107. int ret = msvc_vsnprintf(s, n, format, arg);
  5108. va_end(arg);
  5109. return ret;
  5110. }
  5111. #endif
  5112. #include <errno.h>
  5113. #include <float.h>
  5114. #include <inttypes.h>
  5115. #include <limits.h>
  5116. #include <setjmp.h>
  5117. #include <stdlib.h>
  5118. #include <string.h>
  5119. /* Special header, must be included last. */
  5120. typedef struct {
  5121. const char *ptr, *end;
  5122. upb_arena *arena; /* TODO: should we have a tmp arena for tmp data? */
  5123. const upb_symtab *any_pool;
  5124. int depth;
  5125. upb_status *status;
  5126. jmp_buf err;
  5127. int line;
  5128. const char *line_begin;
  5129. bool is_first;
  5130. int options;
  5131. const upb_fielddef *debug_field;
  5132. } jsondec;
  5133. enum { JD_OBJECT, JD_ARRAY, JD_STRING, JD_NUMBER, JD_TRUE, JD_FALSE, JD_NULL };
  5134. /* Forward declarations of mutually-recursive functions. */
  5135. static void jsondec_wellknown(jsondec *d, upb_msg *msg, const upb_msgdef *m);
  5136. static upb_msgval jsondec_value(jsondec *d, const upb_fielddef *f);
  5137. static void jsondec_wellknownvalue(jsondec *d, upb_msg *msg,
  5138. const upb_msgdef *m);
  5139. static void jsondec_object(jsondec *d, upb_msg *msg, const upb_msgdef *m);
  5140. static bool jsondec_streql(upb_strview str, const char *lit) {
  5141. return str.size == strlen(lit) && memcmp(str.data, lit, str.size) == 0;
  5142. }
  5143. UPB_NORETURN static void jsondec_err(jsondec *d, const char *msg) {
  5144. upb_status_seterrf(d->status, "Error parsing JSON @%d:%d: %s", d->line,
  5145. (int)(d->ptr - d->line_begin), msg);
  5146. longjmp(d->err, 1);
  5147. }
  5148. UPB_NORETURN static void jsondec_errf(jsondec *d, const char *fmt, ...) {
  5149. va_list argp;
  5150. upb_status_seterrf(d->status, "Error parsing JSON @%d:%d: ", d->line,
  5151. (int)(d->ptr - d->line_begin));
  5152. va_start(argp, fmt);
  5153. upb_status_vappenderrf(d->status, fmt, argp);
  5154. va_end(argp);
  5155. longjmp(d->err, 1);
  5156. }
  5157. static void jsondec_skipws(jsondec *d) {
  5158. while (d->ptr != d->end) {
  5159. switch (*d->ptr) {
  5160. case '\n':
  5161. d->line++;
  5162. d->line_begin = d->ptr;
  5163. /* Fallthrough. */
  5164. case '\r':
  5165. case '\t':
  5166. case ' ':
  5167. d->ptr++;
  5168. break;
  5169. default:
  5170. return;
  5171. }
  5172. }
  5173. jsondec_err(d, "Unexpected EOF");
  5174. }
  5175. static bool jsondec_tryparsech(jsondec *d, char ch) {
  5176. if (d->ptr == d->end || *d->ptr != ch) return false;
  5177. d->ptr++;
  5178. return true;
  5179. }
  5180. static void jsondec_parselit(jsondec *d, const char *lit) {
  5181. size_t avail = d->end - d->ptr;
  5182. size_t len = strlen(lit);
  5183. if (avail < len || memcmp(d->ptr, lit, len) != 0) {
  5184. jsondec_errf(d, "Expected: '%s'", lit);
  5185. }
  5186. d->ptr += len;
  5187. }
  5188. static void jsondec_wsch(jsondec *d, char ch) {
  5189. jsondec_skipws(d);
  5190. if (!jsondec_tryparsech(d, ch)) {
  5191. jsondec_errf(d, "Expected: '%c'", ch);
  5192. }
  5193. }
  5194. static void jsondec_true(jsondec *d) { jsondec_parselit(d, "true"); }
  5195. static void jsondec_false(jsondec *d) { jsondec_parselit(d, "false"); }
  5196. static void jsondec_null(jsondec *d) { jsondec_parselit(d, "null"); }
  5197. static void jsondec_entrysep(jsondec *d) {
  5198. jsondec_skipws(d);
  5199. jsondec_parselit(d, ":");
  5200. }
  5201. static int jsondec_rawpeek(jsondec *d) {
  5202. switch (*d->ptr) {
  5203. case '{':
  5204. return JD_OBJECT;
  5205. case '[':
  5206. return JD_ARRAY;
  5207. case '"':
  5208. return JD_STRING;
  5209. case '-':
  5210. case '0':
  5211. case '1':
  5212. case '2':
  5213. case '3':
  5214. case '4':
  5215. case '5':
  5216. case '6':
  5217. case '7':
  5218. case '8':
  5219. case '9':
  5220. return JD_NUMBER;
  5221. case 't':
  5222. return JD_TRUE;
  5223. case 'f':
  5224. return JD_FALSE;
  5225. case 'n':
  5226. return JD_NULL;
  5227. default:
  5228. jsondec_errf(d, "Unexpected character: '%c'", *d->ptr);
  5229. }
  5230. }
  5231. /* JSON object/array **********************************************************/
  5232. /* These are used like so:
  5233. *
  5234. * jsondec_objstart(d);
  5235. * while (jsondec_objnext(d)) {
  5236. * ...
  5237. * }
  5238. * jsondec_objend(d) */
  5239. static int jsondec_peek(jsondec *d) {
  5240. jsondec_skipws(d);
  5241. return jsondec_rawpeek(d);
  5242. }
  5243. static void jsondec_push(jsondec *d) {
  5244. if (--d->depth < 0) {
  5245. jsondec_err(d, "Recursion limit exceeded");
  5246. }
  5247. d->is_first = true;
  5248. }
  5249. static bool jsondec_seqnext(jsondec *d, char end_ch) {
  5250. bool is_first = d->is_first;
  5251. d->is_first = false;
  5252. jsondec_skipws(d);
  5253. if (*d->ptr == end_ch) return false;
  5254. if (!is_first) jsondec_parselit(d, ",");
  5255. return true;
  5256. }
  5257. static void jsondec_arrstart(jsondec *d) {
  5258. jsondec_push(d);
  5259. jsondec_wsch(d, '[');
  5260. }
  5261. static void jsondec_arrend(jsondec *d) {
  5262. d->depth++;
  5263. jsondec_wsch(d, ']');
  5264. }
  5265. static bool jsondec_arrnext(jsondec *d) {
  5266. return jsondec_seqnext(d, ']');
  5267. }
  5268. static void jsondec_objstart(jsondec *d) {
  5269. jsondec_push(d);
  5270. jsondec_wsch(d, '{');
  5271. }
  5272. static void jsondec_objend(jsondec *d) {
  5273. d->depth++;
  5274. jsondec_wsch(d, '}');
  5275. }
  5276. static bool jsondec_objnext(jsondec *d) {
  5277. if (!jsondec_seqnext(d, '}')) return false;
  5278. if (jsondec_peek(d) != JD_STRING) {
  5279. jsondec_err(d, "Object must start with string");
  5280. }
  5281. return true;
  5282. }
  5283. /* JSON number ****************************************************************/
  5284. static bool jsondec_tryskipdigits(jsondec *d) {
  5285. const char *start = d->ptr;
  5286. while (d->ptr < d->end) {
  5287. if (*d->ptr < '0' || *d->ptr > '9') {
  5288. break;
  5289. }
  5290. d->ptr++;
  5291. }
  5292. return d->ptr != start;
  5293. }
  5294. static void jsondec_skipdigits(jsondec *d) {
  5295. if (!jsondec_tryskipdigits(d)) {
  5296. jsondec_err(d, "Expected one or more digits");
  5297. }
  5298. }
  5299. static double jsondec_number(jsondec *d) {
  5300. const char *start = d->ptr;
  5301. assert(jsondec_rawpeek(d) == JD_NUMBER);
  5302. /* Skip over the syntax of a number, as specified by JSON. */
  5303. if (*d->ptr == '-') d->ptr++;
  5304. if (jsondec_tryparsech(d, '0')) {
  5305. if (jsondec_tryskipdigits(d)) {
  5306. jsondec_err(d, "number cannot have leading zero");
  5307. }
  5308. } else {
  5309. jsondec_skipdigits(d);
  5310. }
  5311. if (d->ptr == d->end) goto parse;
  5312. if (jsondec_tryparsech(d, '.')) {
  5313. jsondec_skipdigits(d);
  5314. }
  5315. if (d->ptr == d->end) goto parse;
  5316. if (*d->ptr == 'e' || *d->ptr == 'E') {
  5317. d->ptr++;
  5318. if (d->ptr == d->end) {
  5319. jsondec_err(d, "Unexpected EOF in number");
  5320. }
  5321. if (*d->ptr == '+' || *d->ptr == '-') {
  5322. d->ptr++;
  5323. }
  5324. jsondec_skipdigits(d);
  5325. }
  5326. parse:
  5327. /* Having verified the syntax of a JSON number, use strtod() to parse
  5328. * (strtod() accepts a superset of JSON syntax). */
  5329. errno = 0;
  5330. {
  5331. char* end;
  5332. double val = strtod(start, &end);
  5333. assert(end == d->ptr);
  5334. /* Currently the min/max-val conformance tests fail if we check this. Does
  5335. * this mean the conformance tests are wrong or strtod() is wrong, or
  5336. * something else? Investigate further. */
  5337. /*
  5338. if (errno == ERANGE) {
  5339. jsondec_err(d, "Number out of range");
  5340. }
  5341. */
  5342. if (val > DBL_MAX || val < -DBL_MAX) {
  5343. jsondec_err(d, "Number out of range");
  5344. }
  5345. return val;
  5346. }
  5347. }
  5348. /* JSON string ****************************************************************/
  5349. static char jsondec_escape(jsondec *d) {
  5350. switch (*d->ptr++) {
  5351. case '"':
  5352. return '\"';
  5353. case '\\':
  5354. return '\\';
  5355. case '/':
  5356. return '/';
  5357. case 'b':
  5358. return '\b';
  5359. case 'f':
  5360. return '\f';
  5361. case 'n':
  5362. return '\n';
  5363. case 'r':
  5364. return '\r';
  5365. case 't':
  5366. return '\t';
  5367. default:
  5368. jsondec_err(d, "Invalid escape char");
  5369. }
  5370. }
  5371. static uint32_t jsondec_codepoint(jsondec *d) {
  5372. uint32_t cp = 0;
  5373. const char *end;
  5374. if (d->end - d->ptr < 4) {
  5375. jsondec_err(d, "EOF inside string");
  5376. }
  5377. end = d->ptr + 4;
  5378. while (d->ptr < end) {
  5379. char ch = *d->ptr++;
  5380. if (ch >= '0' && ch <= '9') {
  5381. ch -= '0';
  5382. } else if (ch >= 'a' && ch <= 'f') {
  5383. ch = ch - 'a' + 10;
  5384. } else if (ch >= 'A' && ch <= 'F') {
  5385. ch = ch - 'A' + 10;
  5386. } else {
  5387. jsondec_err(d, "Invalid hex digit");
  5388. }
  5389. cp = (cp << 4) | ch;
  5390. }
  5391. return cp;
  5392. }
  5393. /* Parses a \uXXXX unicode escape (possibly a surrogate pair). */
  5394. static size_t jsondec_unicode(jsondec *d, char* out) {
  5395. uint32_t cp = jsondec_codepoint(d);
  5396. if (cp >= 0xd800 && cp <= 0xdbff) {
  5397. /* Surrogate pair: two 16-bit codepoints become a 32-bit codepoint. */
  5398. uint32_t high = cp;
  5399. uint32_t low;
  5400. jsondec_parselit(d, "\\u");
  5401. low = jsondec_codepoint(d);
  5402. if (low < 0xdc00 || low > 0xdfff) {
  5403. jsondec_err(d, "Invalid low surrogate");
  5404. }
  5405. cp = (high & 0x3ff) << 10;
  5406. cp |= (low & 0x3ff);
  5407. cp += 0x10000;
  5408. } else if (cp >= 0xdc00 && cp <= 0xdfff) {
  5409. jsondec_err(d, "Unpaired low surrogate");
  5410. }
  5411. /* Write to UTF-8 */
  5412. if (cp <= 0x7f) {
  5413. out[0] = cp;
  5414. return 1;
  5415. } else if (cp <= 0x07FF) {
  5416. out[0] = ((cp >> 6) & 0x1F) | 0xC0;
  5417. out[1] = ((cp >> 0) & 0x3F) | 0x80;
  5418. return 2;
  5419. } else if (cp <= 0xFFFF) {
  5420. out[0] = ((cp >> 12) & 0x0F) | 0xE0;
  5421. out[1] = ((cp >> 6) & 0x3F) | 0x80;
  5422. out[2] = ((cp >> 0) & 0x3F) | 0x80;
  5423. return 3;
  5424. } else if (cp < 0x10FFFF) {
  5425. out[0] = ((cp >> 18) & 0x07) | 0xF0;
  5426. out[1] = ((cp >> 12) & 0x3f) | 0x80;
  5427. out[2] = ((cp >> 6) & 0x3f) | 0x80;
  5428. out[3] = ((cp >> 0) & 0x3f) | 0x80;
  5429. return 4;
  5430. } else {
  5431. jsondec_err(d, "Invalid codepoint");
  5432. }
  5433. }
  5434. static void jsondec_resize(jsondec *d, char **buf, char **end, char **buf_end) {
  5435. size_t oldsize = *buf_end - *buf;
  5436. size_t len = *end - *buf;
  5437. size_t size = UPB_MAX(8, 2 * oldsize);
  5438. *buf = upb_arena_realloc(d->arena, *buf, len, size);
  5439. *end = *buf + len;
  5440. *buf_end = *buf + size;
  5441. }
  5442. static upb_strview jsondec_string(jsondec *d) {
  5443. char *buf = NULL;
  5444. char *end = NULL;
  5445. char *buf_end = NULL;
  5446. jsondec_skipws(d);
  5447. if (*d->ptr++ != '"') {
  5448. jsondec_err(d, "Expected string");
  5449. }
  5450. while (d->ptr < d->end) {
  5451. char ch = *d->ptr++;
  5452. if (end == buf_end) {
  5453. jsondec_resize(d, &buf, &end, &buf_end);
  5454. }
  5455. switch (ch) {
  5456. case '"': {
  5457. upb_strview ret;
  5458. ret.data = buf;
  5459. ret.size = end - buf;
  5460. *end = '\0'; /* Needed for possible strtod(). */
  5461. return ret;
  5462. }
  5463. case '\\':
  5464. if (d->ptr == d->end) goto eof;
  5465. if (*d->ptr == 'u') {
  5466. d->ptr++;
  5467. if (buf_end - end < 4) {
  5468. /* Allow space for maximum-sized code point (4 bytes). */
  5469. jsondec_resize(d, &buf, &end, &buf_end);
  5470. }
  5471. end += jsondec_unicode(d, end);
  5472. } else {
  5473. *end++ = jsondec_escape(d);
  5474. }
  5475. break;
  5476. default:
  5477. if ((unsigned char)*d->ptr < 0x20) {
  5478. jsondec_err(d, "Invalid char in JSON string");
  5479. }
  5480. *end++ = ch;
  5481. break;
  5482. }
  5483. }
  5484. eof:
  5485. jsondec_err(d, "EOF inside string");
  5486. }
  5487. static void jsondec_skipval(jsondec *d) {
  5488. switch (jsondec_peek(d)) {
  5489. case JD_OBJECT:
  5490. jsondec_objstart(d);
  5491. while (jsondec_objnext(d)) {
  5492. jsondec_string(d);
  5493. jsondec_entrysep(d);
  5494. jsondec_skipval(d);
  5495. }
  5496. jsondec_objend(d);
  5497. break;
  5498. case JD_ARRAY:
  5499. jsondec_arrstart(d);
  5500. while (jsondec_arrnext(d)) {
  5501. jsondec_skipval(d);
  5502. }
  5503. jsondec_arrend(d);
  5504. break;
  5505. case JD_TRUE:
  5506. jsondec_true(d);
  5507. break;
  5508. case JD_FALSE:
  5509. jsondec_false(d);
  5510. break;
  5511. case JD_NULL:
  5512. jsondec_null(d);
  5513. break;
  5514. case JD_STRING:
  5515. jsondec_string(d);
  5516. break;
  5517. case JD_NUMBER:
  5518. jsondec_number(d);
  5519. break;
  5520. }
  5521. }
  5522. /* Base64 decoding for bytes fields. ******************************************/
  5523. static unsigned int jsondec_base64_tablelookup(const char ch) {
  5524. /* Table includes the normal base64 chars plus the URL-safe variant. */
  5525. const signed char table[256] = {
  5526. -1, -1, -1, -1, -1, -1, -1,
  5527. -1, -1, -1, -1, -1, -1, -1,
  5528. -1, -1, -1, -1, -1, -1, -1,
  5529. -1, -1, -1, -1, -1, -1, -1,
  5530. -1, -1, -1, -1, -1, -1, -1,
  5531. -1, -1, -1, -1, -1, -1, -1,
  5532. -1, 62 /*+*/, -1, 62 /*-*/, -1, 63 /*/ */, 52 /*0*/,
  5533. 53 /*1*/, 54 /*2*/, 55 /*3*/, 56 /*4*/, 57 /*5*/, 58 /*6*/, 59 /*7*/,
  5534. 60 /*8*/, 61 /*9*/, -1, -1, -1, -1, -1,
  5535. -1, -1, 0 /*A*/, 1 /*B*/, 2 /*C*/, 3 /*D*/, 4 /*E*/,
  5536. 5 /*F*/, 6 /*G*/, 07 /*H*/, 8 /*I*/, 9 /*J*/, 10 /*K*/, 11 /*L*/,
  5537. 12 /*M*/, 13 /*N*/, 14 /*O*/, 15 /*P*/, 16 /*Q*/, 17 /*R*/, 18 /*S*/,
  5538. 19 /*T*/, 20 /*U*/, 21 /*V*/, 22 /*W*/, 23 /*X*/, 24 /*Y*/, 25 /*Z*/,
  5539. -1, -1, -1, -1, 63 /*_*/, -1, 26 /*a*/,
  5540. 27 /*b*/, 28 /*c*/, 29 /*d*/, 30 /*e*/, 31 /*f*/, 32 /*g*/, 33 /*h*/,
  5541. 34 /*i*/, 35 /*j*/, 36 /*k*/, 37 /*l*/, 38 /*m*/, 39 /*n*/, 40 /*o*/,
  5542. 41 /*p*/, 42 /*q*/, 43 /*r*/, 44 /*s*/, 45 /*t*/, 46 /*u*/, 47 /*v*/,
  5543. 48 /*w*/, 49 /*x*/, 50 /*y*/, 51 /*z*/, -1, -1, -1,
  5544. -1, -1, -1, -1, -1, -1, -1,
  5545. -1, -1, -1, -1, -1, -1, -1,
  5546. -1, -1, -1, -1, -1, -1, -1,
  5547. -1, -1, -1, -1, -1, -1, -1,
  5548. -1, -1, -1, -1, -1, -1, -1,
  5549. -1, -1, -1, -1, -1, -1, -1,
  5550. -1, -1, -1, -1, -1, -1, -1,
  5551. -1, -1, -1, -1, -1, -1, -1,
  5552. -1, -1, -1, -1, -1, -1, -1,
  5553. -1, -1, -1, -1, -1, -1, -1,
  5554. -1, -1, -1, -1, -1, -1, -1,
  5555. -1, -1, -1, -1, -1, -1, -1,
  5556. -1, -1, -1, -1, -1, -1, -1,
  5557. -1, -1, -1, -1, -1, -1, -1,
  5558. -1, -1, -1, -1, -1, -1, -1,
  5559. -1, -1, -1, -1, -1, -1, -1,
  5560. -1, -1, -1, -1, -1, -1, -1,
  5561. -1, -1, -1, -1, -1, -1, -1,
  5562. -1, -1, -1, -1};
  5563. /* Sign-extend return value so high bit will be set on any unexpected char. */
  5564. return table[(unsigned)ch];
  5565. }
  5566. static char *jsondec_partialbase64(jsondec *d, const char *ptr, const char *end,
  5567. char *out) {
  5568. int32_t val = -1;
  5569. switch (end - ptr) {
  5570. case 2:
  5571. val = jsondec_base64_tablelookup(ptr[0]) << 18 |
  5572. jsondec_base64_tablelookup(ptr[1]) << 12;
  5573. out[0] = val >> 16;
  5574. out += 1;
  5575. break;
  5576. case 3:
  5577. val = jsondec_base64_tablelookup(ptr[0]) << 18 |
  5578. jsondec_base64_tablelookup(ptr[1]) << 12 |
  5579. jsondec_base64_tablelookup(ptr[2]) << 6;
  5580. out[0] = val >> 16;
  5581. out[1] = (val >> 8) & 0xff;
  5582. out += 2;
  5583. break;
  5584. }
  5585. if (val < 0) {
  5586. jsondec_err(d, "Corrupt base64");
  5587. }
  5588. return out;
  5589. }
  5590. static size_t jsondec_base64(jsondec *d, upb_strview str) {
  5591. /* We decode in place. This is safe because this is a new buffer (not
  5592. * aliasing the input) and because base64 decoding shrinks 4 bytes into 3. */
  5593. char *out = (char*)str.data;
  5594. const char *ptr = str.data;
  5595. const char *end = ptr + str.size;
  5596. const char *end4 = ptr + (str.size & -4); /* Round down to multiple of 4. */
  5597. for (; ptr < end4; ptr += 4, out += 3) {
  5598. int val = jsondec_base64_tablelookup(ptr[0]) << 18 |
  5599. jsondec_base64_tablelookup(ptr[1]) << 12 |
  5600. jsondec_base64_tablelookup(ptr[2]) << 6 |
  5601. jsondec_base64_tablelookup(ptr[3]) << 0;
  5602. if (val < 0) {
  5603. /* Junk chars or padding. Remove trailing padding, if any. */
  5604. if (end - ptr == 4 && ptr[3] == '=') {
  5605. if (ptr[2] == '=') {
  5606. end -= 2;
  5607. } else {
  5608. end -= 1;
  5609. }
  5610. }
  5611. break;
  5612. }
  5613. out[0] = val >> 16;
  5614. out[1] = (val >> 8) & 0xff;
  5615. out[2] = val & 0xff;
  5616. }
  5617. if (ptr < end) {
  5618. /* Process remaining chars. We do not require padding. */
  5619. out = jsondec_partialbase64(d, ptr, end, out);
  5620. }
  5621. return out - str.data;
  5622. }
  5623. /* Low-level integer parsing **************************************************/
  5624. /* We use these hand-written routines instead of strto[u]l() because the "long
  5625. * long" variants aren't in c89. Also our version allows setting a ptr limit. */
  5626. static const char *jsondec_buftouint64(jsondec *d, const char *ptr,
  5627. const char *end, uint64_t *val) {
  5628. uint64_t u64 = 0;
  5629. while (ptr < end) {
  5630. unsigned ch = *ptr - '0';
  5631. if (ch >= 10) break;
  5632. if (u64 > UINT64_MAX / 10 || u64 * 10 > UINT64_MAX - ch) {
  5633. jsondec_err(d, "Integer overflow");
  5634. }
  5635. u64 *= 10;
  5636. u64 += ch;
  5637. ptr++;
  5638. }
  5639. *val = u64;
  5640. return ptr;
  5641. }
  5642. static const char *jsondec_buftoint64(jsondec *d, const char *ptr,
  5643. const char *end, int64_t *val) {
  5644. bool neg = false;
  5645. uint64_t u64;
  5646. if (ptr != end && *ptr == '-') {
  5647. ptr++;
  5648. neg = true;
  5649. }
  5650. ptr = jsondec_buftouint64(d, ptr, end, &u64);
  5651. if (u64 > (uint64_t)INT64_MAX + neg) {
  5652. jsondec_err(d, "Integer overflow");
  5653. }
  5654. *val = neg ? -u64 : u64;
  5655. return ptr;
  5656. }
  5657. static uint64_t jsondec_strtouint64(jsondec *d, upb_strview str) {
  5658. const char *end = str.data + str.size;
  5659. uint64_t ret;
  5660. if (jsondec_buftouint64(d, str.data, end, &ret) != end) {
  5661. jsondec_err(d, "Non-number characters in quoted integer");
  5662. }
  5663. return ret;
  5664. }
  5665. static int64_t jsondec_strtoint64(jsondec *d, upb_strview str) {
  5666. const char *end = str.data + str.size;
  5667. int64_t ret;
  5668. if (jsondec_buftoint64(d, str.data, end, &ret) != end) {
  5669. jsondec_err(d, "Non-number characters in quoted integer");
  5670. }
  5671. return ret;
  5672. }
  5673. /* Primitive value types ******************************************************/
  5674. /* Parse INT32 or INT64 value. */
  5675. static upb_msgval jsondec_int(jsondec *d, const upb_fielddef *f) {
  5676. upb_msgval val;
  5677. switch (jsondec_peek(d)) {
  5678. case JD_NUMBER: {
  5679. double dbl = jsondec_number(d);
  5680. if (dbl > 9223372036854774784.0 || dbl < -9223372036854775808.0) {
  5681. jsondec_err(d, "JSON number is out of range.");
  5682. }
  5683. val.int64_val = dbl; /* must be guarded, overflow here is UB */
  5684. if (val.int64_val != dbl) {
  5685. jsondec_errf(d, "JSON number was not integral (%d != %" PRId64 ")", dbl,
  5686. val.int64_val);
  5687. }
  5688. break;
  5689. }
  5690. case JD_STRING: {
  5691. upb_strview str = jsondec_string(d);
  5692. val.int64_val = jsondec_strtoint64(d, str);
  5693. break;
  5694. }
  5695. default:
  5696. jsondec_err(d, "Expected number or string");
  5697. }
  5698. if (upb_fielddef_type(f) == UPB_TYPE_INT32) {
  5699. if (val.int64_val > INT32_MAX || val.int64_val < INT32_MIN) {
  5700. jsondec_err(d, "Integer out of range.");
  5701. }
  5702. val.int32_val = (int32_t)val.int64_val;
  5703. }
  5704. return val;
  5705. }
  5706. /* Parse UINT32 or UINT64 value. */
  5707. static upb_msgval jsondec_uint(jsondec *d, const upb_fielddef *f) {
  5708. upb_msgval val;
  5709. switch (jsondec_peek(d)) {
  5710. case JD_NUMBER: {
  5711. double dbl = jsondec_number(d);
  5712. if (dbl > 18446744073709549568.0 || dbl < 0) {
  5713. jsondec_err(d, "JSON number is out of range.");
  5714. }
  5715. val.uint64_val = dbl; /* must be guarded, overflow here is UB */
  5716. if (val.uint64_val != dbl) {
  5717. jsondec_errf(d, "JSON number was not integral (%d != %" PRIu64 ")", dbl,
  5718. val.uint64_val);
  5719. }
  5720. break;
  5721. }
  5722. case JD_STRING: {
  5723. upb_strview str = jsondec_string(d);
  5724. val.uint64_val = jsondec_strtouint64(d, str);
  5725. break;
  5726. }
  5727. default:
  5728. jsondec_err(d, "Expected number or string");
  5729. }
  5730. if (upb_fielddef_type(f) == UPB_TYPE_UINT32) {
  5731. if (val.uint64_val > UINT32_MAX) {
  5732. jsondec_err(d, "Integer out of range.");
  5733. }
  5734. val.uint32_val = (uint32_t)val.uint64_val;
  5735. }
  5736. return val;
  5737. }
  5738. /* Parse DOUBLE or FLOAT value. */
  5739. static upb_msgval jsondec_double(jsondec *d, const upb_fielddef *f) {
  5740. upb_strview str;
  5741. upb_msgval val;
  5742. switch (jsondec_peek(d)) {
  5743. case JD_NUMBER:
  5744. val.double_val = jsondec_number(d);
  5745. break;
  5746. case JD_STRING:
  5747. str = jsondec_string(d);
  5748. if (jsondec_streql(str, "NaN")) {
  5749. val.double_val = UPB_NAN;
  5750. } else if (jsondec_streql(str, "Infinity")) {
  5751. val.double_val = UPB_INFINITY;
  5752. } else if (jsondec_streql(str, "-Infinity")) {
  5753. val.double_val = -UPB_INFINITY;
  5754. } else {
  5755. val.double_val = strtod(str.data, NULL);
  5756. }
  5757. break;
  5758. default:
  5759. jsondec_err(d, "Expected number or string");
  5760. }
  5761. if (upb_fielddef_type(f) == UPB_TYPE_FLOAT) {
  5762. if (val.double_val != UPB_INFINITY && val.double_val != -UPB_INFINITY &&
  5763. (val.double_val > FLT_MAX || val.double_val < -FLT_MAX)) {
  5764. jsondec_err(d, "Float out of range");
  5765. }
  5766. val.float_val = val.double_val;
  5767. }
  5768. return val;
  5769. }
  5770. /* Parse STRING or BYTES value. */
  5771. static upb_msgval jsondec_strfield(jsondec *d, const upb_fielddef *f) {
  5772. upb_msgval val;
  5773. val.str_val = jsondec_string(d);
  5774. if (upb_fielddef_type(f) == UPB_TYPE_BYTES) {
  5775. val.str_val.size = jsondec_base64(d, val.str_val);
  5776. }
  5777. return val;
  5778. }
  5779. static upb_msgval jsondec_enum(jsondec *d, const upb_fielddef *f) {
  5780. if (jsondec_peek(d) == JD_STRING) {
  5781. const upb_enumdef *e = upb_fielddef_enumsubdef(f);
  5782. upb_strview str = jsondec_string(d);
  5783. upb_msgval val;
  5784. if (!upb_enumdef_ntoi(e, str.data, str.size, &val.int32_val)) {
  5785. if (d->options & UPB_JSONDEC_IGNOREUNKNOWN) {
  5786. val.int32_val = 0;
  5787. } else {
  5788. jsondec_errf(d, "Unknown enumerator: '" UPB_STRVIEW_FORMAT "'",
  5789. UPB_STRVIEW_ARGS(str));
  5790. }
  5791. }
  5792. return val;
  5793. } else {
  5794. return jsondec_int(d, f);
  5795. }
  5796. }
  5797. static upb_msgval jsondec_bool(jsondec *d, const upb_fielddef *f) {
  5798. bool is_map_key = upb_fielddef_number(f) == 1 &&
  5799. upb_msgdef_mapentry(upb_fielddef_containingtype(f));
  5800. upb_msgval val;
  5801. if (is_map_key) {
  5802. upb_strview str = jsondec_string(d);
  5803. if (jsondec_streql(str, "true")) {
  5804. val.bool_val = true;
  5805. } else if (jsondec_streql(str, "false")) {
  5806. val.bool_val = false;
  5807. } else {
  5808. jsondec_err(d, "Invalid boolean map key");
  5809. }
  5810. } else {
  5811. switch (jsondec_peek(d)) {
  5812. case JD_TRUE:
  5813. val.bool_val = true;
  5814. jsondec_true(d);
  5815. break;
  5816. case JD_FALSE:
  5817. val.bool_val = false;
  5818. jsondec_false(d);
  5819. break;
  5820. default:
  5821. jsondec_err(d, "Expected true or false");
  5822. }
  5823. }
  5824. return val;
  5825. }
  5826. /* Composite types (array/message/map) ****************************************/
  5827. static void jsondec_array(jsondec *d, upb_msg *msg, const upb_fielddef *f) {
  5828. upb_array *arr = upb_msg_mutable(msg, f, d->arena).array;
  5829. jsondec_arrstart(d);
  5830. while (jsondec_arrnext(d)) {
  5831. upb_msgval elem = jsondec_value(d, f);
  5832. upb_array_append(arr, elem, d->arena);
  5833. }
  5834. jsondec_arrend(d);
  5835. }
  5836. static void jsondec_map(jsondec *d, upb_msg *msg, const upb_fielddef *f) {
  5837. upb_map *map = upb_msg_mutable(msg, f, d->arena).map;
  5838. const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
  5839. const upb_fielddef *key_f = upb_msgdef_itof(entry, 1);
  5840. const upb_fielddef *val_f = upb_msgdef_itof(entry, 2);
  5841. jsondec_objstart(d);
  5842. while (jsondec_objnext(d)) {
  5843. upb_msgval key, val;
  5844. key = jsondec_value(d, key_f);
  5845. jsondec_entrysep(d);
  5846. val = jsondec_value(d, val_f);
  5847. upb_map_set(map, key, val, d->arena);
  5848. }
  5849. jsondec_objend(d);
  5850. }
  5851. static void jsondec_tomsg(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  5852. if (upb_msgdef_wellknowntype(m) == UPB_WELLKNOWN_UNSPECIFIED) {
  5853. jsondec_object(d, msg, m);
  5854. } else {
  5855. jsondec_wellknown(d, msg, m);
  5856. }
  5857. }
  5858. static upb_msgval jsondec_msg(jsondec *d, const upb_fielddef *f) {
  5859. const upb_msgdef *m = upb_fielddef_msgsubdef(f);
  5860. upb_msg *msg = upb_msg_new(m, d->arena);
  5861. upb_msgval val;
  5862. jsondec_tomsg(d, msg, m);
  5863. val.msg_val = msg;
  5864. return val;
  5865. }
  5866. static bool jsondec_isvalue(const upb_fielddef *f) {
  5867. return upb_fielddef_type(f) == UPB_TYPE_MESSAGE &&
  5868. upb_msgdef_wellknowntype(upb_fielddef_msgsubdef(f)) ==
  5869. UPB_WELLKNOWN_VALUE;
  5870. }
  5871. static void jsondec_field(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  5872. upb_strview name;
  5873. const upb_fielddef *f;
  5874. const upb_fielddef *preserved;
  5875. name = jsondec_string(d);
  5876. jsondec_entrysep(d);
  5877. f = upb_msgdef_lookupjsonname(m, name.data, name.size);
  5878. if (!f) {
  5879. if ((d->options & UPB_JSONDEC_IGNOREUNKNOWN) == 0) {
  5880. jsondec_errf(d, "Unknown field: '" UPB_STRVIEW_FORMAT "'",
  5881. UPB_STRVIEW_ARGS(name));
  5882. }
  5883. jsondec_skipval(d);
  5884. return;
  5885. }
  5886. if (upb_fielddef_containingoneof(f) &&
  5887. upb_msg_whichoneof(msg, upb_fielddef_containingoneof(f))) {
  5888. jsondec_err(d, "More than one field for this oneof.");
  5889. }
  5890. if (jsondec_peek(d) == JD_NULL && !jsondec_isvalue(f)) {
  5891. /* JSON "null" indicates a default value, so no need to set anything. */
  5892. jsondec_null(d);
  5893. return;
  5894. }
  5895. preserved = d->debug_field;
  5896. d->debug_field = f;
  5897. if (upb_fielddef_ismap(f)) {
  5898. jsondec_map(d, msg, f);
  5899. } else if (upb_fielddef_isseq(f)) {
  5900. jsondec_array(d, msg, f);
  5901. } else if (upb_fielddef_issubmsg(f)) {
  5902. upb_msg *submsg = upb_msg_mutable(msg, f, d->arena).msg;
  5903. const upb_msgdef *subm = upb_fielddef_msgsubdef(f);
  5904. jsondec_tomsg(d, submsg, subm);
  5905. } else {
  5906. upb_msgval val = jsondec_value(d, f);
  5907. upb_msg_set(msg, f, val, d->arena);
  5908. }
  5909. d->debug_field = preserved;
  5910. }
  5911. static void jsondec_object(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  5912. jsondec_objstart(d);
  5913. while (jsondec_objnext(d)) {
  5914. jsondec_field(d, msg, m);
  5915. }
  5916. jsondec_objend(d);
  5917. }
  5918. static upb_msgval jsondec_value(jsondec *d, const upb_fielddef *f) {
  5919. switch (upb_fielddef_type(f)) {
  5920. case UPB_TYPE_BOOL:
  5921. return jsondec_bool(d, f);
  5922. case UPB_TYPE_FLOAT:
  5923. case UPB_TYPE_DOUBLE:
  5924. return jsondec_double(d, f);
  5925. case UPB_TYPE_UINT32:
  5926. case UPB_TYPE_UINT64:
  5927. return jsondec_uint(d, f);
  5928. case UPB_TYPE_INT32:
  5929. case UPB_TYPE_INT64:
  5930. return jsondec_int(d, f);
  5931. case UPB_TYPE_STRING:
  5932. case UPB_TYPE_BYTES:
  5933. return jsondec_strfield(d, f);
  5934. case UPB_TYPE_ENUM:
  5935. return jsondec_enum(d, f);
  5936. case UPB_TYPE_MESSAGE:
  5937. return jsondec_msg(d, f);
  5938. default:
  5939. UPB_UNREACHABLE();
  5940. }
  5941. }
  5942. /* Well-known types ***********************************************************/
  5943. static int jsondec_tsdigits(jsondec *d, const char **ptr, size_t digits,
  5944. const char *after) {
  5945. uint64_t val;
  5946. const char *p = *ptr;
  5947. const char *end = p + digits;
  5948. size_t after_len = after ? strlen(after) : 0;
  5949. UPB_ASSERT(digits <= 9); /* int can't overflow. */
  5950. if (jsondec_buftouint64(d, p, end, &val) != end ||
  5951. (after_len && memcmp(end, after, after_len) != 0)) {
  5952. jsondec_err(d, "Malformed timestamp");
  5953. }
  5954. UPB_ASSERT(val < INT_MAX);
  5955. *ptr = end + after_len;
  5956. return (int)val;
  5957. }
  5958. static int jsondec_nanos(jsondec *d, const char **ptr, const char *end) {
  5959. uint64_t nanos = 0;
  5960. const char *p = *ptr;
  5961. if (p != end && *p == '.') {
  5962. const char *nano_end = jsondec_buftouint64(d, p + 1, end, &nanos);
  5963. int digits = (int)(nano_end - p - 1);
  5964. int exp_lg10 = 9 - digits;
  5965. if (digits > 9) {
  5966. jsondec_err(d, "Too many digits for partial seconds");
  5967. }
  5968. while (exp_lg10--) nanos *= 10;
  5969. *ptr = nano_end;
  5970. }
  5971. UPB_ASSERT(nanos < INT_MAX);
  5972. return (int)nanos;
  5973. }
  5974. /* jsondec_epochdays(1970, 1, 1) == 1970-01-01 == 0. */
  5975. int jsondec_epochdays(int y, int m, int d) {
  5976. const uint32_t year_base = 4800; /* Before min year, multiple of 400. */
  5977. const uint32_t m_adj = m - 3; /* March-based month. */
  5978. const uint32_t carry = m_adj > (uint32_t)m ? 1 : 0;
  5979. const uint32_t adjust = carry ? 12 : 0;
  5980. const uint32_t y_adj = y + year_base - carry;
  5981. const uint32_t month_days = ((m_adj + adjust) * 62719 + 769) / 2048;
  5982. const uint32_t leap_days = y_adj / 4 - y_adj / 100 + y_adj / 400;
  5983. return y_adj * 365 + leap_days + month_days + (d - 1) - 2472632;
  5984. }
  5985. static int64_t jsondec_unixtime(int y, int m, int d, int h, int min, int s) {
  5986. return (int64_t)jsondec_epochdays(y, m, d) * 86400 + h * 3600 + min * 60 + s;
  5987. }
  5988. static void jsondec_timestamp(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  5989. upb_msgval seconds;
  5990. upb_msgval nanos;
  5991. upb_strview str = jsondec_string(d);
  5992. const char *ptr = str.data;
  5993. const char *end = ptr + str.size;
  5994. if (str.size < 20) goto malformed;
  5995. {
  5996. /* 1972-01-01T01:00:00 */
  5997. int year = jsondec_tsdigits(d, &ptr, 4, "-");
  5998. int mon = jsondec_tsdigits(d, &ptr, 2, "-");
  5999. int day = jsondec_tsdigits(d, &ptr, 2, "T");
  6000. int hour = jsondec_tsdigits(d, &ptr, 2, ":");
  6001. int min = jsondec_tsdigits(d, &ptr, 2, ":");
  6002. int sec = jsondec_tsdigits(d, &ptr, 2, NULL);
  6003. seconds.int64_val = jsondec_unixtime(year, mon, day, hour, min, sec);
  6004. }
  6005. nanos.int32_val = jsondec_nanos(d, &ptr, end);
  6006. {
  6007. /* [+-]08:00 or Z */
  6008. int ofs = 0;
  6009. bool neg = false;
  6010. if (ptr == end) goto malformed;
  6011. switch (*ptr++) {
  6012. case '-':
  6013. neg = true;
  6014. /* fallthrough */
  6015. case '+':
  6016. if ((end - ptr) != 5) goto malformed;
  6017. ofs = jsondec_tsdigits(d, &ptr, 2, ":00");
  6018. ofs *= 60 * 60;
  6019. seconds.int64_val += (neg ? ofs : -ofs);
  6020. break;
  6021. case 'Z':
  6022. if (ptr != end) goto malformed;
  6023. break;
  6024. default:
  6025. goto malformed;
  6026. }
  6027. }
  6028. if (seconds.int64_val < -62135596800) {
  6029. jsondec_err(d, "Timestamp out of range");
  6030. }
  6031. upb_msg_set(msg, upb_msgdef_itof(m, 1), seconds, d->arena);
  6032. upb_msg_set(msg, upb_msgdef_itof(m, 2), nanos, d->arena);
  6033. return;
  6034. malformed:
  6035. jsondec_err(d, "Malformed timestamp");
  6036. }
  6037. static void jsondec_duration(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6038. upb_msgval seconds;
  6039. upb_msgval nanos;
  6040. upb_strview str = jsondec_string(d);
  6041. const char *ptr = str.data;
  6042. const char *end = ptr + str.size;
  6043. /* "3.000000001s", "3s", etc. */
  6044. ptr = jsondec_buftoint64(d, ptr, end, &seconds.int64_val);
  6045. nanos.int32_val = jsondec_nanos(d, &ptr, end);
  6046. if (end - ptr != 1 || *ptr != 's') {
  6047. jsondec_err(d, "Malformed duration");
  6048. }
  6049. if (seconds.int64_val < -315576000000LL || seconds.int64_val > 315576000000LL) {
  6050. jsondec_err(d, "Duration out of range");
  6051. }
  6052. if (seconds.int64_val < 0) {
  6053. nanos.int32_val = - nanos.int32_val;
  6054. }
  6055. upb_msg_set(msg, upb_msgdef_itof(m, 1), seconds, d->arena);
  6056. upb_msg_set(msg, upb_msgdef_itof(m, 2), nanos, d->arena);
  6057. }
  6058. static void jsondec_listvalue(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6059. const upb_fielddef *values_f = upb_msgdef_itof(m, 1);
  6060. const upb_msgdef *value_m = upb_fielddef_msgsubdef(values_f);
  6061. upb_array *values = upb_msg_mutable(msg, values_f, d->arena).array;
  6062. jsondec_arrstart(d);
  6063. while (jsondec_arrnext(d)) {
  6064. upb_msg *value_msg = upb_msg_new(value_m, d->arena);
  6065. upb_msgval value;
  6066. value.msg_val = value_msg;
  6067. upb_array_append(values, value, d->arena);
  6068. jsondec_wellknownvalue(d, value_msg, value_m);
  6069. }
  6070. jsondec_arrend(d);
  6071. }
  6072. static void jsondec_struct(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6073. const upb_fielddef *fields_f = upb_msgdef_itof(m, 1);
  6074. const upb_msgdef *entry_m = upb_fielddef_msgsubdef(fields_f);
  6075. const upb_fielddef *value_f = upb_msgdef_itof(entry_m, 2);
  6076. const upb_msgdef *value_m = upb_fielddef_msgsubdef(value_f);
  6077. upb_map *fields = upb_msg_mutable(msg, fields_f, d->arena).map;
  6078. jsondec_objstart(d);
  6079. while (jsondec_objnext(d)) {
  6080. upb_msgval key, value;
  6081. upb_msg *value_msg = upb_msg_new(value_m, d->arena);
  6082. key.str_val = jsondec_string(d);
  6083. value.msg_val = value_msg;
  6084. upb_map_set(fields, key, value, d->arena);
  6085. jsondec_entrysep(d);
  6086. jsondec_wellknownvalue(d, value_msg, value_m);
  6087. }
  6088. jsondec_objend(d);
  6089. }
  6090. static void jsondec_wellknownvalue(jsondec *d, upb_msg *msg,
  6091. const upb_msgdef *m) {
  6092. upb_msgval val;
  6093. const upb_fielddef *f;
  6094. upb_msg *submsg;
  6095. switch (jsondec_peek(d)) {
  6096. case JD_NUMBER:
  6097. /* double number_value = 2; */
  6098. f = upb_msgdef_itof(m, 2);
  6099. val.double_val = jsondec_number(d);
  6100. break;
  6101. case JD_STRING:
  6102. /* string string_value = 3; */
  6103. f = upb_msgdef_itof(m, 3);
  6104. val.str_val = jsondec_string(d);
  6105. break;
  6106. case JD_FALSE:
  6107. /* bool bool_value = 4; */
  6108. f = upb_msgdef_itof(m, 4);
  6109. val.bool_val = false;
  6110. jsondec_false(d);
  6111. break;
  6112. case JD_TRUE:
  6113. /* bool bool_value = 4; */
  6114. f = upb_msgdef_itof(m, 4);
  6115. val.bool_val = true;
  6116. jsondec_true(d);
  6117. break;
  6118. case JD_NULL:
  6119. /* NullValue null_value = 1; */
  6120. f = upb_msgdef_itof(m, 1);
  6121. val.int32_val = 0;
  6122. jsondec_null(d);
  6123. break;
  6124. /* Note: these cases return, because upb_msg_mutable() is enough. */
  6125. case JD_OBJECT:
  6126. /* Struct struct_value = 5; */
  6127. f = upb_msgdef_itof(m, 5);
  6128. submsg = upb_msg_mutable(msg, f, d->arena).msg;
  6129. jsondec_struct(d, submsg, upb_fielddef_msgsubdef(f));
  6130. return;
  6131. case JD_ARRAY:
  6132. /* ListValue list_value = 6; */
  6133. f = upb_msgdef_itof(m, 6);
  6134. submsg = upb_msg_mutable(msg, f, d->arena).msg;
  6135. jsondec_listvalue(d, submsg, upb_fielddef_msgsubdef(f));
  6136. return;
  6137. default:
  6138. UPB_UNREACHABLE();
  6139. }
  6140. upb_msg_set(msg, f, val, d->arena);
  6141. }
  6142. static upb_strview jsondec_mask(jsondec *d, const char *buf, const char *end) {
  6143. /* FieldMask fields grow due to inserted '_' characters, so we can't do the
  6144. * transform in place. */
  6145. const char *ptr = buf;
  6146. upb_strview ret;
  6147. char *out;
  6148. ret.size = end - ptr;
  6149. while (ptr < end) {
  6150. ret.size += (*ptr >= 'A' && *ptr <= 'Z');
  6151. ptr++;
  6152. }
  6153. out = upb_arena_malloc(d->arena, ret.size);
  6154. ptr = buf;
  6155. ret.data = out;
  6156. while (ptr < end) {
  6157. char ch = *ptr++;
  6158. if (ch >= 'A' && ch <= 'Z') {
  6159. *out++ = '_';
  6160. *out++ = ch + 32;
  6161. } else if (ch == '_') {
  6162. jsondec_err(d, "field mask may not contain '_'");
  6163. } else {
  6164. *out++ = ch;
  6165. }
  6166. }
  6167. return ret;
  6168. }
  6169. static void jsondec_fieldmask(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6170. /* repeated string paths = 1; */
  6171. const upb_fielddef *paths_f = upb_msgdef_itof(m, 1);
  6172. upb_array *arr = upb_msg_mutable(msg, paths_f, d->arena).array;
  6173. upb_strview str = jsondec_string(d);
  6174. const char *ptr = str.data;
  6175. const char *end = ptr + str.size;
  6176. upb_msgval val;
  6177. while (ptr < end) {
  6178. const char *elem_end = memchr(ptr, ',', end - ptr);
  6179. if (elem_end) {
  6180. val.str_val = jsondec_mask(d, ptr, elem_end);
  6181. ptr = elem_end + 1;
  6182. } else {
  6183. val.str_val = jsondec_mask(d, ptr, end);
  6184. ptr = end;
  6185. }
  6186. upb_array_append(arr, val, d->arena);
  6187. }
  6188. }
  6189. static void jsondec_anyfield(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6190. if (upb_msgdef_wellknowntype(m) == UPB_WELLKNOWN_UNSPECIFIED) {
  6191. /* For regular types: {"@type": "[user type]", "f1": <V1>, "f2": <V2>}
  6192. * where f1, f2, etc. are the normal fields of this type. */
  6193. jsondec_field(d, msg, m);
  6194. } else {
  6195. /* For well-known types: {"@type": "[well-known type]", "value": <X>}
  6196. * where <X> is whatever encoding the WKT normally uses. */
  6197. upb_strview str = jsondec_string(d);
  6198. jsondec_entrysep(d);
  6199. if (!jsondec_streql(str, "value")) {
  6200. jsondec_err(d, "Key for well-known type must be 'value'");
  6201. }
  6202. jsondec_wellknown(d, msg, m);
  6203. }
  6204. }
  6205. static const upb_msgdef *jsondec_typeurl(jsondec *d, upb_msg *msg,
  6206. const upb_msgdef *m) {
  6207. const upb_fielddef *type_url_f = upb_msgdef_itof(m, 1);
  6208. const upb_msgdef *type_m;
  6209. upb_strview type_url = jsondec_string(d);
  6210. const char *end = type_url.data + type_url.size;
  6211. const char *ptr = end;
  6212. upb_msgval val;
  6213. val.str_val = type_url;
  6214. upb_msg_set(msg, type_url_f, val, d->arena);
  6215. /* Find message name after the last '/' */
  6216. while (ptr > type_url.data && *--ptr != '/') {}
  6217. if (ptr == type_url.data || ptr == end) {
  6218. jsondec_err(d, "Type url must have at least one '/' and non-empty host");
  6219. }
  6220. ptr++;
  6221. type_m = upb_symtab_lookupmsg2(d->any_pool, ptr, end - ptr);
  6222. if (!type_m) {
  6223. jsondec_err(d, "Type was not found");
  6224. }
  6225. return type_m;
  6226. }
  6227. static void jsondec_any(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6228. /* string type_url = 1;
  6229. * bytes value = 2; */
  6230. const upb_fielddef *value_f = upb_msgdef_itof(m, 2);
  6231. upb_msg *any_msg;
  6232. const upb_msgdef *any_m = NULL;
  6233. const char *pre_type_data = NULL;
  6234. const char *pre_type_end = NULL;
  6235. upb_msgval encoded;
  6236. jsondec_objstart(d);
  6237. /* Scan looking for "@type", which is not necessarily first. */
  6238. while (!any_m && jsondec_objnext(d)) {
  6239. const char *start = d->ptr;
  6240. upb_strview name = jsondec_string(d);
  6241. jsondec_entrysep(d);
  6242. if (jsondec_streql(name, "@type")) {
  6243. any_m = jsondec_typeurl(d, msg, m);
  6244. if (pre_type_data) {
  6245. pre_type_end = start;
  6246. while (*pre_type_end != ',') pre_type_end--;
  6247. }
  6248. } else {
  6249. if (!pre_type_data) pre_type_data = start;
  6250. jsondec_skipval(d);
  6251. }
  6252. }
  6253. if (!any_m) {
  6254. jsondec_err(d, "Any object didn't contain a '@type' field");
  6255. }
  6256. any_msg = upb_msg_new(any_m, d->arena);
  6257. if (pre_type_data) {
  6258. size_t len = pre_type_end - pre_type_data + 1;
  6259. char *tmp = upb_arena_malloc(d->arena, len);
  6260. const char *saved_ptr = d->ptr;
  6261. const char *saved_end = d->end;
  6262. memcpy(tmp, pre_type_data, len - 1);
  6263. tmp[len - 1] = '}';
  6264. d->ptr = tmp;
  6265. d->end = tmp + len;
  6266. d->is_first = true;
  6267. while (jsondec_objnext(d)) {
  6268. jsondec_anyfield(d, any_msg, any_m);
  6269. }
  6270. d->ptr = saved_ptr;
  6271. d->end = saved_end;
  6272. }
  6273. while (jsondec_objnext(d)) {
  6274. jsondec_anyfield(d, any_msg, any_m);
  6275. }
  6276. jsondec_objend(d);
  6277. encoded.str_val.data = upb_encode(any_msg, upb_msgdef_layout(any_m), d->arena,
  6278. &encoded.str_val.size);
  6279. upb_msg_set(msg, value_f, encoded, d->arena);
  6280. }
  6281. static void jsondec_wrapper(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6282. const upb_fielddef *value_f = upb_msgdef_itof(m, 1);
  6283. upb_msgval val = jsondec_value(d, value_f);
  6284. upb_msg_set(msg, value_f, val, d->arena);
  6285. }
  6286. static void jsondec_wellknown(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6287. switch (upb_msgdef_wellknowntype(m)) {
  6288. case UPB_WELLKNOWN_ANY:
  6289. jsondec_any(d, msg, m);
  6290. break;
  6291. case UPB_WELLKNOWN_FIELDMASK:
  6292. jsondec_fieldmask(d, msg, m);
  6293. break;
  6294. case UPB_WELLKNOWN_DURATION:
  6295. jsondec_duration(d, msg, m);
  6296. break;
  6297. case UPB_WELLKNOWN_TIMESTAMP:
  6298. jsondec_timestamp(d, msg, m);
  6299. break;
  6300. case UPB_WELLKNOWN_VALUE:
  6301. jsondec_wellknownvalue(d, msg, m);
  6302. break;
  6303. case UPB_WELLKNOWN_LISTVALUE:
  6304. jsondec_listvalue(d, msg, m);
  6305. break;
  6306. case UPB_WELLKNOWN_STRUCT:
  6307. jsondec_struct(d, msg, m);
  6308. break;
  6309. case UPB_WELLKNOWN_DOUBLEVALUE:
  6310. case UPB_WELLKNOWN_FLOATVALUE:
  6311. case UPB_WELLKNOWN_INT64VALUE:
  6312. case UPB_WELLKNOWN_UINT64VALUE:
  6313. case UPB_WELLKNOWN_INT32VALUE:
  6314. case UPB_WELLKNOWN_UINT32VALUE:
  6315. case UPB_WELLKNOWN_STRINGVALUE:
  6316. case UPB_WELLKNOWN_BYTESVALUE:
  6317. case UPB_WELLKNOWN_BOOLVALUE:
  6318. jsondec_wrapper(d, msg, m);
  6319. break;
  6320. default:
  6321. UPB_UNREACHABLE();
  6322. }
  6323. }
  6324. bool upb_json_decode(const char *buf, size_t size, upb_msg *msg,
  6325. const upb_msgdef *m, const upb_symtab *any_pool,
  6326. int options, upb_arena *arena, upb_status *status) {
  6327. jsondec d;
  6328. d.ptr = buf;
  6329. d.end = buf + size;
  6330. d.arena = arena;
  6331. d.any_pool = any_pool;
  6332. d.status = status;
  6333. d.options = options;
  6334. d.depth = 64;
  6335. d.line = 1;
  6336. d.line_begin = d.ptr;
  6337. d.debug_field = NULL;
  6338. d.is_first = false;
  6339. if (setjmp(d.err)) return false;
  6340. jsondec_tomsg(&d, msg, m);
  6341. return true;
  6342. }
  6343. #include <ctype.h>
  6344. #include <float.h>
  6345. #include <inttypes.h>
  6346. #include <stdarg.h>
  6347. #include <stdio.h>
  6348. #include <string.h>
  6349. #include <setjmp.h>
  6350. typedef struct {
  6351. char *buf, *ptr, *end;
  6352. size_t overflow;
  6353. int indent_depth;
  6354. int options;
  6355. const upb_symtab *ext_pool;
  6356. jmp_buf err;
  6357. upb_status *status;
  6358. upb_arena *arena;
  6359. } jsonenc;
  6360. static void jsonenc_msg(jsonenc *e, const upb_msg *msg, const upb_msgdef *m);
  6361. static void jsonenc_scalar(jsonenc *e, upb_msgval val, const upb_fielddef *f);
  6362. static void jsonenc_msgfield(jsonenc *e, const upb_msg *msg,
  6363. const upb_msgdef *m);
  6364. static void jsonenc_msgfields(jsonenc *e, const upb_msg *msg,
  6365. const upb_msgdef *m);
  6366. static void jsonenc_value(jsonenc *e, const upb_msg *msg, const upb_msgdef *m);
  6367. UPB_NORETURN static void jsonenc_err(jsonenc *e, const char *msg) {
  6368. upb_status_seterrmsg(e->status, msg);
  6369. longjmp(e->err, 1);
  6370. }
  6371. UPB_NORETURN static void jsonenc_errf(jsonenc *e, const char *fmt, ...) {
  6372. va_list argp;
  6373. va_start(argp, fmt);
  6374. upb_status_vseterrf(e->status, fmt, argp);
  6375. va_end(argp);
  6376. longjmp(e->err, 1);
  6377. }
  6378. static upb_arena *jsonenc_arena(jsonenc *e) {
  6379. /* Create lazily, since it's only needed for Any */
  6380. if (!e->arena) {
  6381. e->arena = upb_arena_new();
  6382. }
  6383. return e->arena;
  6384. }
  6385. static void jsonenc_putbytes(jsonenc *e, const void *data, size_t len) {
  6386. size_t have = e->end - e->ptr;
  6387. if (UPB_LIKELY(have >= len)) {
  6388. memcpy(e->ptr, data, len);
  6389. e->ptr += len;
  6390. } else {
  6391. if (have) memcpy(e->ptr, data, have);
  6392. e->ptr += have;
  6393. e->overflow += (len - have);
  6394. }
  6395. }
  6396. static void jsonenc_putstr(jsonenc *e, const char *str) {
  6397. jsonenc_putbytes(e, str, strlen(str));
  6398. }
  6399. static void jsonenc_printf(jsonenc *e, const char *fmt, ...) {
  6400. size_t n;
  6401. size_t have = e->end - e->ptr;
  6402. va_list args;
  6403. va_start(args, fmt);
  6404. n = _upb_vsnprintf(e->ptr, have, fmt, args);
  6405. va_end(args);
  6406. if (UPB_LIKELY(have > n)) {
  6407. e->ptr += n;
  6408. } else {
  6409. e->ptr += have;
  6410. e->overflow += (n - have);
  6411. }
  6412. }
  6413. static void jsonenc_nanos(jsonenc *e, int32_t nanos) {
  6414. int digits = 9;
  6415. if (nanos == 0) return;
  6416. if (nanos < 0 || nanos >= 1000000000) {
  6417. jsonenc_err(e, "error formatting timestamp as JSON: invalid nanos");
  6418. }
  6419. while (nanos % 1000 == 0) {
  6420. nanos /= 1000;
  6421. digits -= 3;
  6422. }
  6423. jsonenc_printf(e, ".%0.*" PRId32, digits, nanos);
  6424. }
  6425. static void jsonenc_timestamp(jsonenc *e, const upb_msg *msg,
  6426. const upb_msgdef *m) {
  6427. const upb_fielddef *seconds_f = upb_msgdef_itof(m, 1);
  6428. const upb_fielddef *nanos_f = upb_msgdef_itof(m, 2);
  6429. int64_t seconds = upb_msg_get(msg, seconds_f).int64_val;
  6430. int32_t nanos = upb_msg_get(msg, nanos_f).int32_val;
  6431. int L, N, I, J, K, hour, min, sec;
  6432. if (seconds < -62135596800) {
  6433. jsonenc_err(e,
  6434. "error formatting timestamp as JSON: minimum acceptable value "
  6435. "is 0001-01-01T00:00:00Z");
  6436. } else if (seconds > 253402300799) {
  6437. jsonenc_err(e,
  6438. "error formatting timestamp as JSON: maximum acceptable value "
  6439. "is 9999-12-31T23:59:59Z");
  6440. }
  6441. /* Julian Day -> Y/M/D, Algorithm from:
  6442. * Fliegel, H. F., and Van Flandern, T. C., "A Machine Algorithm for
  6443. * Processing Calendar Dates," Communications of the Association of
  6444. * Computing Machines, vol. 11 (1968), p. 657. */
  6445. L = (int)(seconds / 86400) + 68569 + 2440588;
  6446. N = 4 * L / 146097;
  6447. L = L - (146097 * N + 3) / 4;
  6448. I = 4000 * (L + 1) / 1461001;
  6449. L = L - 1461 * I / 4 + 31;
  6450. J = 80 * L / 2447;
  6451. K = L - 2447 * J / 80;
  6452. L = J / 11;
  6453. J = J + 2 - 12 * L;
  6454. I = 100 * (N - 49) + I + L;
  6455. sec = seconds % 60;
  6456. min = (seconds / 60) % 60;
  6457. hour = (seconds / 3600) % 24;
  6458. jsonenc_printf(e, "\"%04d-%02d-%02dT%02d:%02d:%02d", I, J, K, hour, min, sec);
  6459. jsonenc_nanos(e, nanos);
  6460. jsonenc_putstr(e, "Z\"");
  6461. }
  6462. static void jsonenc_duration(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
  6463. const upb_fielddef *seconds_f = upb_msgdef_itof(m, 1);
  6464. const upb_fielddef *nanos_f = upb_msgdef_itof(m, 2);
  6465. int64_t seconds = upb_msg_get(msg, seconds_f).int64_val;
  6466. int32_t nanos = upb_msg_get(msg, nanos_f).int32_val;
  6467. if (seconds > 315576000000 || seconds < -315576000000 ||
  6468. (seconds < 0) != (nanos < 0)) {
  6469. jsonenc_err(e, "bad duration");
  6470. }
  6471. if (nanos < 0) {
  6472. nanos = -nanos;
  6473. }
  6474. jsonenc_printf(e, "\"%" PRId64, seconds);
  6475. jsonenc_nanos(e, nanos);
  6476. jsonenc_putstr(e, "s\"");
  6477. }
  6478. static void jsonenc_enum(int32_t val, const upb_fielddef *f, jsonenc *e) {
  6479. const upb_enumdef *e_def = upb_fielddef_enumsubdef(f);
  6480. const char *name = upb_enumdef_iton(e_def, val);
  6481. if (name) {
  6482. jsonenc_printf(e, "\"%s\"", name);
  6483. } else {
  6484. jsonenc_printf(e, "%" PRId32, val);
  6485. }
  6486. }
  6487. static void jsonenc_bytes(jsonenc *e, upb_strview str) {
  6488. /* This is the regular base64, not the "web-safe" version. */
  6489. static const char base64[] =
  6490. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  6491. const unsigned char *ptr = (unsigned char*)str.data;
  6492. const unsigned char *end = ptr + str.size;
  6493. char buf[4];
  6494. jsonenc_putstr(e, "\"");
  6495. while (end - ptr >= 3) {
  6496. buf[0] = base64[ptr[0] >> 2];
  6497. buf[1] = base64[((ptr[0] & 0x3) << 4) | (ptr[1] >> 4)];
  6498. buf[2] = base64[((ptr[1] & 0xf) << 2) | (ptr[2] >> 6)];
  6499. buf[3] = base64[ptr[2] & 0x3f];
  6500. jsonenc_putbytes(e, buf, 4);
  6501. ptr += 3;
  6502. }
  6503. switch (end - ptr) {
  6504. case 2:
  6505. buf[0] = base64[ptr[0] >> 2];
  6506. buf[1] = base64[((ptr[0] & 0x3) << 4) | (ptr[1] >> 4)];
  6507. buf[2] = base64[(ptr[1] & 0xf) << 2];
  6508. buf[3] = '=';
  6509. jsonenc_putbytes(e, buf, 4);
  6510. break;
  6511. case 1:
  6512. buf[0] = base64[ptr[0] >> 2];
  6513. buf[1] = base64[((ptr[0] & 0x3) << 4)];
  6514. buf[2] = '=';
  6515. buf[3] = '=';
  6516. jsonenc_putbytes(e, buf, 4);
  6517. break;
  6518. }
  6519. jsonenc_putstr(e, "\"");
  6520. }
  6521. static void jsonenc_stringbody(jsonenc *e, upb_strview str) {
  6522. const char *ptr = str.data;
  6523. const char *end = ptr + str.size;
  6524. while (ptr < end) {
  6525. switch (*ptr) {
  6526. case '\n':
  6527. jsonenc_putstr(e, "\\n");
  6528. break;
  6529. case '\r':
  6530. jsonenc_putstr(e, "\\r");
  6531. break;
  6532. case '\t':
  6533. jsonenc_putstr(e, "\\t");
  6534. break;
  6535. case '\"':
  6536. jsonenc_putstr(e, "\\\"");
  6537. break;
  6538. case '\f':
  6539. jsonenc_putstr(e, "\\f");
  6540. break;
  6541. case '\b':
  6542. jsonenc_putstr(e, "\\b");
  6543. break;
  6544. case '\\':
  6545. jsonenc_putstr(e, "\\\\");
  6546. break;
  6547. default:
  6548. if ((uint8_t)*ptr < 0x20) {
  6549. jsonenc_printf(e, "\\u%04x", (int)(uint8_t)*ptr);
  6550. } else {
  6551. /* This could be a non-ASCII byte. We rely on the string being valid
  6552. * UTF-8. */
  6553. jsonenc_putbytes(e, ptr, 1);
  6554. }
  6555. break;
  6556. }
  6557. ptr++;
  6558. }
  6559. }
  6560. static void jsonenc_string(jsonenc *e, upb_strview str) {
  6561. jsonenc_putstr(e, "\"");
  6562. jsonenc_stringbody(e, str);
  6563. jsonenc_putstr(e, "\"");
  6564. }
  6565. static void jsonenc_double(jsonenc *e, const char *fmt, double val) {
  6566. if (val == UPB_INFINITY) {
  6567. jsonenc_putstr(e, "\"Infinity\"");
  6568. } else if (val == -UPB_INFINITY) {
  6569. jsonenc_putstr(e, "\"-Infinity\"");
  6570. } else if (val != val) {
  6571. jsonenc_putstr(e, "\"NaN\"");
  6572. } else {
  6573. jsonenc_printf(e, fmt, val);
  6574. }
  6575. }
  6576. static void jsonenc_wrapper(jsonenc *e, const upb_msg *msg,
  6577. const upb_msgdef *m) {
  6578. const upb_fielddef *val_f = upb_msgdef_itof(m, 1);
  6579. upb_msgval val = upb_msg_get(msg, val_f);
  6580. jsonenc_scalar(e, val, val_f);
  6581. }
  6582. static const upb_msgdef *jsonenc_getanymsg(jsonenc *e, upb_strview type_url) {
  6583. /* Find last '/', if any. */
  6584. const char *end = type_url.data + type_url.size;
  6585. const char *ptr = end;
  6586. const upb_msgdef *ret;
  6587. if (!e->ext_pool) {
  6588. jsonenc_err(e, "Tried to encode Any, but no symtab was provided");
  6589. }
  6590. if (type_url.size == 0) goto badurl;
  6591. while (true) {
  6592. if (--ptr == type_url.data) {
  6593. /* Type URL must contain at least one '/', with host before. */
  6594. goto badurl;
  6595. }
  6596. if (*ptr == '/') {
  6597. ptr++;
  6598. break;
  6599. }
  6600. }
  6601. ret = upb_symtab_lookupmsg2(e->ext_pool, ptr, end - ptr);
  6602. if (!ret) {
  6603. jsonenc_errf(e, "Couldn't find Any type: %.*s", (int)(end - ptr), ptr);
  6604. }
  6605. return ret;
  6606. badurl:
  6607. jsonenc_errf(
  6608. e, "Bad type URL: " UPB_STRVIEW_FORMAT, UPB_STRVIEW_ARGS(type_url));
  6609. }
  6610. static void jsonenc_any(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
  6611. const upb_fielddef *type_url_f = upb_msgdef_itof(m, 1);
  6612. const upb_fielddef *value_f = upb_msgdef_itof(m, 2);
  6613. upb_strview type_url = upb_msg_get(msg, type_url_f).str_val;
  6614. upb_strview value = upb_msg_get(msg, value_f).str_val;
  6615. const upb_msgdef *any_m = jsonenc_getanymsg(e, type_url);
  6616. const upb_msglayout *any_layout = upb_msgdef_layout(any_m);
  6617. upb_arena *arena = jsonenc_arena(e);
  6618. upb_msg *any = upb_msg_new(any_m, arena);
  6619. if (!upb_decode(value.data, value.size, any, any_layout, arena)) {
  6620. jsonenc_err(e, "Error decoding message in Any");
  6621. }
  6622. jsonenc_putstr(e, "{\"@type\":");
  6623. jsonenc_string(e, type_url);
  6624. jsonenc_putstr(e, ",");
  6625. if (upb_msgdef_wellknowntype(any_m) == UPB_WELLKNOWN_UNSPECIFIED) {
  6626. /* Regular messages: {"@type": "...","foo": 1, "bar": 2} */
  6627. jsonenc_msgfields(e, any, any_m);
  6628. } else {
  6629. /* Well-known type: {"@type": "...","value": <well-known encoding>} */
  6630. jsonenc_putstr(e, "\"value\":");
  6631. jsonenc_msgfield(e, any, any_m);
  6632. }
  6633. jsonenc_putstr(e, "}");
  6634. }
  6635. static void jsonenc_putsep(jsonenc *e, const char *str, bool *first) {
  6636. if (*first) {
  6637. *first = false;
  6638. } else {
  6639. jsonenc_putstr(e, str);
  6640. }
  6641. }
  6642. static void jsonenc_fieldpath(jsonenc *e, upb_strview path) {
  6643. const char *ptr = path.data;
  6644. const char *end = ptr + path.size;
  6645. while (ptr < end) {
  6646. char ch = *ptr;
  6647. if (ch >= 'A' && ch <= 'Z') {
  6648. jsonenc_err(e, "Field mask element may not have upper-case letter.");
  6649. } else if (ch == '_') {
  6650. if (ptr == end - 1 || *(ptr + 1) < 'a' || *(ptr + 1) > 'z') {
  6651. jsonenc_err(e, "Underscore must be followed by a lowercase letter.");
  6652. }
  6653. ch = *++ptr - 32;
  6654. }
  6655. jsonenc_putbytes(e, &ch, 1);
  6656. ptr++;
  6657. }
  6658. }
  6659. static void jsonenc_fieldmask(jsonenc *e, const upb_msg *msg,
  6660. const upb_msgdef *m) {
  6661. const upb_fielddef *paths_f = upb_msgdef_itof(m, 1);
  6662. const upb_array *paths = upb_msg_get(msg, paths_f).array_val;
  6663. bool first = true;
  6664. size_t i, n = 0;
  6665. if (paths) n = upb_array_size(paths);
  6666. jsonenc_putstr(e, "\"");
  6667. for (i = 0; i < n; i++) {
  6668. jsonenc_putsep(e, ",", &first);
  6669. jsonenc_fieldpath(e, upb_array_get(paths, i).str_val);
  6670. }
  6671. jsonenc_putstr(e, "\"");
  6672. }
  6673. static void jsonenc_struct(jsonenc *e, const upb_msg *msg,
  6674. const upb_msgdef *m) {
  6675. const upb_fielddef *fields_f = upb_msgdef_itof(m, 1);
  6676. const upb_map *fields = upb_msg_get(msg, fields_f).map_val;
  6677. const upb_msgdef *entry_m = upb_fielddef_msgsubdef(fields_f);
  6678. const upb_fielddef *value_f = upb_msgdef_itof(entry_m, 2);
  6679. size_t iter = UPB_MAP_BEGIN;
  6680. bool first = true;
  6681. jsonenc_putstr(e, "{");
  6682. if (fields) {
  6683. while (upb_mapiter_next(fields, &iter)) {
  6684. upb_msgval key = upb_mapiter_key(fields, iter);
  6685. upb_msgval val = upb_mapiter_value(fields, iter);
  6686. jsonenc_putsep(e, ",", &first);
  6687. jsonenc_string(e, key.str_val);
  6688. jsonenc_putstr(e, ":");
  6689. jsonenc_value(e, val.msg_val, upb_fielddef_msgsubdef(value_f));
  6690. }
  6691. }
  6692. jsonenc_putstr(e, "}");
  6693. }
  6694. static void jsonenc_listvalue(jsonenc *e, const upb_msg *msg,
  6695. const upb_msgdef *m) {
  6696. const upb_fielddef *values_f = upb_msgdef_itof(m, 1);
  6697. const upb_msgdef *values_m = upb_fielddef_msgsubdef(values_f);
  6698. const upb_array *values = upb_msg_get(msg, values_f).array_val;
  6699. size_t i;
  6700. bool first = true;
  6701. jsonenc_putstr(e, "[");
  6702. if (values) {
  6703. const size_t size = upb_array_size(values);
  6704. for (i = 0; i < size; i++) {
  6705. upb_msgval elem = upb_array_get(values, i);
  6706. jsonenc_putsep(e, ",", &first);
  6707. jsonenc_value(e, elem.msg_val, values_m);
  6708. }
  6709. }
  6710. jsonenc_putstr(e, "]");
  6711. }
  6712. static void jsonenc_value(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
  6713. /* TODO(haberman): do we want a reflection method to get oneof case? */
  6714. size_t iter = UPB_MSG_BEGIN;
  6715. const upb_fielddef *f;
  6716. upb_msgval val;
  6717. if (!upb_msg_next(msg, m, NULL, &f, &val, &iter)) {
  6718. jsonenc_err(e, "No value set in Value proto");
  6719. }
  6720. switch (upb_fielddef_number(f)) {
  6721. case 1:
  6722. jsonenc_putstr(e, "null");
  6723. break;
  6724. case 2:
  6725. jsonenc_double(e, "%.17g", val.double_val);
  6726. break;
  6727. case 3:
  6728. jsonenc_string(e, val.str_val);
  6729. break;
  6730. case 4:
  6731. jsonenc_putstr(e, val.bool_val ? "true" : "false");
  6732. break;
  6733. case 5:
  6734. jsonenc_struct(e, val.msg_val, upb_fielddef_msgsubdef(f));
  6735. break;
  6736. case 6:
  6737. jsonenc_listvalue(e, val.msg_val, upb_fielddef_msgsubdef(f));
  6738. break;
  6739. }
  6740. }
  6741. static void jsonenc_msgfield(jsonenc *e, const upb_msg *msg,
  6742. const upb_msgdef *m) {
  6743. switch (upb_msgdef_wellknowntype(m)) {
  6744. case UPB_WELLKNOWN_UNSPECIFIED:
  6745. jsonenc_msg(e, msg, m);
  6746. break;
  6747. case UPB_WELLKNOWN_ANY:
  6748. jsonenc_any(e, msg, m);
  6749. break;
  6750. case UPB_WELLKNOWN_FIELDMASK:
  6751. jsonenc_fieldmask(e, msg, m);
  6752. break;
  6753. case UPB_WELLKNOWN_DURATION:
  6754. jsonenc_duration(e, msg, m);
  6755. break;
  6756. case UPB_WELLKNOWN_TIMESTAMP:
  6757. jsonenc_timestamp(e, msg, m);
  6758. break;
  6759. case UPB_WELLKNOWN_DOUBLEVALUE:
  6760. case UPB_WELLKNOWN_FLOATVALUE:
  6761. case UPB_WELLKNOWN_INT64VALUE:
  6762. case UPB_WELLKNOWN_UINT64VALUE:
  6763. case UPB_WELLKNOWN_INT32VALUE:
  6764. case UPB_WELLKNOWN_UINT32VALUE:
  6765. case UPB_WELLKNOWN_STRINGVALUE:
  6766. case UPB_WELLKNOWN_BYTESVALUE:
  6767. case UPB_WELLKNOWN_BOOLVALUE:
  6768. jsonenc_wrapper(e, msg, m);
  6769. break;
  6770. case UPB_WELLKNOWN_VALUE:
  6771. jsonenc_value(e, msg, m);
  6772. break;
  6773. case UPB_WELLKNOWN_LISTVALUE:
  6774. jsonenc_listvalue(e, msg, m);
  6775. break;
  6776. case UPB_WELLKNOWN_STRUCT:
  6777. jsonenc_struct(e, msg, m);
  6778. break;
  6779. }
  6780. }
  6781. static void jsonenc_scalar(jsonenc *e, upb_msgval val, const upb_fielddef *f) {
  6782. switch (upb_fielddef_type(f)) {
  6783. case UPB_TYPE_BOOL:
  6784. jsonenc_putstr(e, val.bool_val ? "true" : "false");
  6785. break;
  6786. case UPB_TYPE_FLOAT:
  6787. jsonenc_double(e, "%.9g", val.float_val);
  6788. break;
  6789. case UPB_TYPE_DOUBLE:
  6790. jsonenc_double(e, "%.17g", val.double_val);
  6791. break;
  6792. case UPB_TYPE_INT32:
  6793. jsonenc_printf(e, "%" PRId32, val.int32_val);
  6794. break;
  6795. case UPB_TYPE_UINT32:
  6796. jsonenc_printf(e, "%" PRIu32, val.uint32_val);
  6797. break;
  6798. case UPB_TYPE_INT64:
  6799. jsonenc_printf(e, "\"%" PRId64 "\"", val.int64_val);
  6800. break;
  6801. case UPB_TYPE_UINT64:
  6802. jsonenc_printf(e, "\"%" PRIu64 "\"", val.uint64_val);
  6803. break;
  6804. case UPB_TYPE_STRING:
  6805. jsonenc_string(e, val.str_val);
  6806. break;
  6807. case UPB_TYPE_BYTES:
  6808. jsonenc_bytes(e, val.str_val);
  6809. break;
  6810. case UPB_TYPE_ENUM:
  6811. jsonenc_enum(val.int32_val, f, e);
  6812. break;
  6813. case UPB_TYPE_MESSAGE:
  6814. jsonenc_msgfield(e, val.msg_val, upb_fielddef_msgsubdef(f));
  6815. break;
  6816. }
  6817. }
  6818. static void jsonenc_mapkey(jsonenc *e, upb_msgval val, const upb_fielddef *f) {
  6819. jsonenc_putstr(e, "\"");
  6820. switch (upb_fielddef_type(f)) {
  6821. case UPB_TYPE_BOOL:
  6822. jsonenc_putstr(e, val.bool_val ? "true" : "false");
  6823. break;
  6824. case UPB_TYPE_INT32:
  6825. jsonenc_printf(e, "%" PRId32, val.int32_val);
  6826. break;
  6827. case UPB_TYPE_UINT32:
  6828. jsonenc_printf(e, "%" PRIu32, val.uint32_val);
  6829. break;
  6830. case UPB_TYPE_INT64:
  6831. jsonenc_printf(e, "%" PRId64, val.int64_val);
  6832. break;
  6833. case UPB_TYPE_UINT64:
  6834. jsonenc_printf(e, "%" PRIu64, val.uint64_val);
  6835. break;
  6836. case UPB_TYPE_STRING:
  6837. jsonenc_stringbody(e, val.str_val);
  6838. break;
  6839. default:
  6840. UPB_UNREACHABLE();
  6841. }
  6842. jsonenc_putstr(e, "\":");
  6843. }
  6844. static void jsonenc_array(jsonenc *e, const upb_array *arr,
  6845. const upb_fielddef *f) {
  6846. size_t i;
  6847. size_t size = upb_array_size(arr);
  6848. bool first = true;
  6849. jsonenc_putstr(e, "[");
  6850. for (i = 0; i < size; i++) {
  6851. jsonenc_putsep(e, ",", &first);
  6852. jsonenc_scalar(e, upb_array_get(arr, i), f);
  6853. }
  6854. jsonenc_putstr(e, "]");
  6855. }
  6856. static void jsonenc_map(jsonenc *e, const upb_map *map, const upb_fielddef *f) {
  6857. const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
  6858. const upb_fielddef *key_f = upb_msgdef_itof(entry, 1);
  6859. const upb_fielddef *val_f = upb_msgdef_itof(entry, 2);
  6860. size_t iter = UPB_MAP_BEGIN;
  6861. bool first = true;
  6862. jsonenc_putstr(e, "{");
  6863. while (upb_mapiter_next(map, &iter)) {
  6864. jsonenc_putsep(e, ",", &first);
  6865. jsonenc_mapkey(e, upb_mapiter_key(map, iter), key_f);
  6866. jsonenc_scalar(e, upb_mapiter_value(map, iter), val_f);
  6867. }
  6868. jsonenc_putstr(e, "}");
  6869. }
  6870. static void jsonenc_fieldval(jsonenc *e, const upb_fielddef *f,
  6871. upb_msgval val, bool *first) {
  6872. const char *name;
  6873. if (e->options & UPB_JSONENC_PROTONAMES) {
  6874. name = upb_fielddef_name(f);
  6875. } else {
  6876. name = upb_fielddef_jsonname(f);
  6877. }
  6878. jsonenc_putsep(e, ",", first);
  6879. jsonenc_printf(e, "\"%s\":", name);
  6880. if (upb_fielddef_ismap(f)) {
  6881. jsonenc_map(e, val.map_val, f);
  6882. } else if (upb_fielddef_isseq(f)) {
  6883. jsonenc_array(e, val.array_val, f);
  6884. } else {
  6885. jsonenc_scalar(e, val, f);
  6886. }
  6887. }
  6888. static void jsonenc_msgfields(jsonenc *e, const upb_msg *msg,
  6889. const upb_msgdef *m) {
  6890. upb_msgval val;
  6891. const upb_fielddef *f;
  6892. bool first = true;
  6893. if (e->options & UPB_JSONENC_EMITDEFAULTS) {
  6894. /* Iterate over all fields. */
  6895. upb_msg_field_iter i;
  6896. for (upb_msg_field_begin(&i, m); !upb_msg_field_done(&i);
  6897. upb_msg_field_next(&i)) {
  6898. f = upb_msg_iter_field(&i);
  6899. jsonenc_fieldval(e, f, upb_msg_get(msg, f), &first);
  6900. }
  6901. } else {
  6902. /* Iterate over non-empty fields. */
  6903. size_t iter = UPB_MSG_BEGIN;
  6904. while (upb_msg_next(msg, m, e->ext_pool, &f, &val, &iter)) {
  6905. jsonenc_fieldval(e, f, val, &first);
  6906. }
  6907. }
  6908. }
  6909. static void jsonenc_msg(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
  6910. jsonenc_putstr(e, "{");
  6911. jsonenc_msgfields(e, msg, m);
  6912. jsonenc_putstr(e, "}");
  6913. }
  6914. static size_t jsonenc_nullz(jsonenc *e, size_t size) {
  6915. size_t ret = e->ptr - e->buf + e->overflow;
  6916. if (size > 0) {
  6917. if (e->ptr == e->end) e->ptr--;
  6918. *e->ptr = '\0';
  6919. }
  6920. return ret;
  6921. }
  6922. size_t upb_json_encode(const upb_msg *msg, const upb_msgdef *m,
  6923. const upb_symtab *ext_pool, int options, char *buf,
  6924. size_t size, upb_status *status) {
  6925. jsonenc e;
  6926. e.buf = buf;
  6927. e.ptr = buf;
  6928. e.end = buf + size;
  6929. e.overflow = 0;
  6930. e.options = options;
  6931. e.ext_pool = ext_pool;
  6932. e.status = status;
  6933. e.arena = NULL;
  6934. if (setjmp(e.err)) return -1;
  6935. jsonenc_msgfield(&e, msg, m);
  6936. if (e.arena) upb_arena_free(e.arena);
  6937. return jsonenc_nullz(&e, size);
  6938. }
  6939. /* See port_def.inc. This should #undef all macros #defined there. */
  6940. #undef UPB_MAPTYPE_STRING
  6941. #undef UPB_SIZE
  6942. #undef UPB_PTR_AT
  6943. #undef UPB_READ_ONEOF
  6944. #undef UPB_WRITE_ONEOF
  6945. #undef UPB_INLINE
  6946. #undef UPB_ALIGN_UP
  6947. #undef UPB_ALIGN_DOWN
  6948. #undef UPB_ALIGN_MALLOC
  6949. #undef UPB_ALIGN_OF
  6950. #undef UPB_FORCEINLINE
  6951. #undef UPB_NOINLINE
  6952. #undef UPB_NORETURN
  6953. #undef UPB_MAX
  6954. #undef UPB_MIN
  6955. #undef UPB_UNUSED
  6956. #undef UPB_ASSUME
  6957. #undef UPB_ASSERT
  6958. #undef UPB_ASSERT_DEBUGVAR
  6959. #undef UPB_UNREACHABLE
  6960. #undef UPB_INFINITY
  6961. #undef UPB_NAN
  6962. #undef UPB_MSVC_VSNPRINTF
  6963. #undef _upb_snprintf
  6964. #undef _upb_vsnprintf
  6965. #undef _upb_va_copy