12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497 |
- /* Amalgamated source file */
- #define _XOPEN_SOURCE 700
- #include "upb.h"
- #if UINTPTR_MAX == 0xffffffff
- #define UPB_SIZE(size32, size64) size32
- #else
- #define UPB_SIZE(size32, size64) size64
- #endif
- #define UPB_FIELD_AT(msg, fieldtype, offset) \
- *(fieldtype*)((const char*)(msg) + offset)
- #define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \
- UPB_FIELD_AT(msg, int, case_offset) == case_val \
- ? UPB_FIELD_AT(msg, fieldtype, offset) \
- : default
- #define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \
- UPB_FIELD_AT(msg, int, case_offset) = case_val; \
- UPB_FIELD_AT(msg, fieldtype, offset) = value;
- /* This file was generated by upbc (the upb compiler) from the input
- * file:
- *
- * google/protobuf/descriptor.proto
- *
- * Do not edit -- your changes will be discarded when the file is
- * regenerated. */
- #include <stddef.h>
- static const upb_msglayout *const google_protobuf_FileDescriptorSet_submsgs[1] = {
- &google_protobuf_FileDescriptorProto_msginit,
- };
- static const upb_msglayout_field google_protobuf_FileDescriptorSet__fields[1] = {
- {1, UPB_SIZE(0, 0), 0, 0, 11, 3},
- };
- const upb_msglayout google_protobuf_FileDescriptorSet_msginit = {
- &google_protobuf_FileDescriptorSet_submsgs[0],
- &google_protobuf_FileDescriptorSet__fields[0],
- UPB_SIZE(4, 8), 1, false,
- };
- static const upb_msglayout *const google_protobuf_FileDescriptorProto_submsgs[6] = {
- &google_protobuf_DescriptorProto_msginit,
- &google_protobuf_EnumDescriptorProto_msginit,
- &google_protobuf_FieldDescriptorProto_msginit,
- &google_protobuf_FileOptions_msginit,
- &google_protobuf_ServiceDescriptorProto_msginit,
- &google_protobuf_SourceCodeInfo_msginit,
- };
- static const upb_msglayout_field google_protobuf_FileDescriptorProto__fields[12] = {
- {1, UPB_SIZE(4, 8), 1, 0, 9, 1},
- {2, UPB_SIZE(12, 24), 2, 0, 9, 1},
- {3, UPB_SIZE(36, 72), 0, 0, 9, 3},
- {4, UPB_SIZE(40, 80), 0, 0, 11, 3},
- {5, UPB_SIZE(44, 88), 0, 1, 11, 3},
- {6, UPB_SIZE(48, 96), 0, 4, 11, 3},
- {7, UPB_SIZE(52, 104), 0, 2, 11, 3},
- {8, UPB_SIZE(28, 56), 4, 3, 11, 1},
- {9, UPB_SIZE(32, 64), 5, 5, 11, 1},
- {10, UPB_SIZE(56, 112), 0, 0, 5, 3},
- {11, UPB_SIZE(60, 120), 0, 0, 5, 3},
- {12, UPB_SIZE(20, 40), 3, 0, 9, 1},
- };
- const upb_msglayout google_protobuf_FileDescriptorProto_msginit = {
- &google_protobuf_FileDescriptorProto_submsgs[0],
- &google_protobuf_FileDescriptorProto__fields[0],
- UPB_SIZE(64, 128), 12, false,
- };
- static const upb_msglayout *const google_protobuf_DescriptorProto_submsgs[8] = {
- &google_protobuf_DescriptorProto_msginit,
- &google_protobuf_DescriptorProto_ExtensionRange_msginit,
- &google_protobuf_DescriptorProto_ReservedRange_msginit,
- &google_protobuf_EnumDescriptorProto_msginit,
- &google_protobuf_FieldDescriptorProto_msginit,
- &google_protobuf_MessageOptions_msginit,
- &google_protobuf_OneofDescriptorProto_msginit,
- };
- static const upb_msglayout_field google_protobuf_DescriptorProto__fields[10] = {
- {1, UPB_SIZE(4, 8), 1, 0, 9, 1},
- {2, UPB_SIZE(16, 32), 0, 4, 11, 3},
- {3, UPB_SIZE(20, 40), 0, 0, 11, 3},
- {4, UPB_SIZE(24, 48), 0, 3, 11, 3},
- {5, UPB_SIZE(28, 56), 0, 1, 11, 3},
- {6, UPB_SIZE(32, 64), 0, 4, 11, 3},
- {7, UPB_SIZE(12, 24), 2, 5, 11, 1},
- {8, UPB_SIZE(36, 72), 0, 6, 11, 3},
- {9, UPB_SIZE(40, 80), 0, 2, 11, 3},
- {10, UPB_SIZE(44, 88), 0, 0, 9, 3},
- };
- const upb_msglayout google_protobuf_DescriptorProto_msginit = {
- &google_protobuf_DescriptorProto_submsgs[0],
- &google_protobuf_DescriptorProto__fields[0],
- UPB_SIZE(48, 96), 10, false,
- };
- static const upb_msglayout *const google_protobuf_DescriptorProto_ExtensionRange_submsgs[1] = {
- &google_protobuf_ExtensionRangeOptions_msginit,
- };
- static const upb_msglayout_field google_protobuf_DescriptorProto_ExtensionRange__fields[3] = {
- {1, UPB_SIZE(4, 4), 1, 0, 5, 1},
- {2, UPB_SIZE(8, 8), 2, 0, 5, 1},
- {3, UPB_SIZE(12, 16), 3, 0, 11, 1},
- };
- const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit = {
- &google_protobuf_DescriptorProto_ExtensionRange_submsgs[0],
- &google_protobuf_DescriptorProto_ExtensionRange__fields[0],
- UPB_SIZE(16, 24), 3, false,
- };
- static const upb_msglayout_field google_protobuf_DescriptorProto_ReservedRange__fields[2] = {
- {1, UPB_SIZE(4, 4), 1, 0, 5, 1},
- {2, UPB_SIZE(8, 8), 2, 0, 5, 1},
- };
- const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit = {
- NULL,
- &google_protobuf_DescriptorProto_ReservedRange__fields[0],
- UPB_SIZE(12, 12), 2, false,
- };
- static const upb_msglayout *const google_protobuf_ExtensionRangeOptions_submsgs[1] = {
- &google_protobuf_UninterpretedOption_msginit,
- };
- static const upb_msglayout_field google_protobuf_ExtensionRangeOptions__fields[1] = {
- {999, UPB_SIZE(0, 0), 0, 0, 11, 3},
- };
- const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit = {
- &google_protobuf_ExtensionRangeOptions_submsgs[0],
- &google_protobuf_ExtensionRangeOptions__fields[0],
- UPB_SIZE(4, 8), 1, false,
- };
- static const upb_msglayout *const google_protobuf_FieldDescriptorProto_submsgs[1] = {
- &google_protobuf_FieldOptions_msginit,
- };
- static const upb_msglayout_field google_protobuf_FieldDescriptorProto__fields[10] = {
- {1, UPB_SIZE(32, 32), 5, 0, 9, 1},
- {2, UPB_SIZE(40, 48), 6, 0, 9, 1},
- {3, UPB_SIZE(24, 24), 3, 0, 5, 1},
- {4, UPB_SIZE(8, 8), 1, 0, 14, 1},
- {5, UPB_SIZE(16, 16), 2, 0, 14, 1},
- {6, UPB_SIZE(48, 64), 7, 0, 9, 1},
- {7, UPB_SIZE(56, 80), 8, 0, 9, 1},
- {8, UPB_SIZE(72, 112), 10, 0, 11, 1},
- {9, UPB_SIZE(28, 28), 4, 0, 5, 1},
- {10, UPB_SIZE(64, 96), 9, 0, 9, 1},
- };
- const upb_msglayout google_protobuf_FieldDescriptorProto_msginit = {
- &google_protobuf_FieldDescriptorProto_submsgs[0],
- &google_protobuf_FieldDescriptorProto__fields[0],
- UPB_SIZE(80, 128), 10, false,
- };
- static const upb_msglayout *const google_protobuf_OneofDescriptorProto_submsgs[1] = {
- &google_protobuf_OneofOptions_msginit,
- };
- static const upb_msglayout_field google_protobuf_OneofDescriptorProto__fields[2] = {
- {1, UPB_SIZE(4, 8), 1, 0, 9, 1},
- {2, UPB_SIZE(12, 24), 2, 0, 11, 1},
- };
- const upb_msglayout google_protobuf_OneofDescriptorProto_msginit = {
- &google_protobuf_OneofDescriptorProto_submsgs[0],
- &google_protobuf_OneofDescriptorProto__fields[0],
- UPB_SIZE(16, 32), 2, false,
- };
- static const upb_msglayout *const google_protobuf_EnumDescriptorProto_submsgs[3] = {
- &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit,
- &google_protobuf_EnumOptions_msginit,
- &google_protobuf_EnumValueDescriptorProto_msginit,
- };
- static const upb_msglayout_field google_protobuf_EnumDescriptorProto__fields[5] = {
- {1, UPB_SIZE(4, 8), 1, 0, 9, 1},
- {2, UPB_SIZE(16, 32), 0, 2, 11, 3},
- {3, UPB_SIZE(12, 24), 2, 1, 11, 1},
- {4, UPB_SIZE(20, 40), 0, 0, 11, 3},
- {5, UPB_SIZE(24, 48), 0, 0, 9, 3},
- };
- const upb_msglayout google_protobuf_EnumDescriptorProto_msginit = {
- &google_protobuf_EnumDescriptorProto_submsgs[0],
- &google_protobuf_EnumDescriptorProto__fields[0],
- UPB_SIZE(32, 64), 5, false,
- };
- static const upb_msglayout_field google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[2] = {
- {1, UPB_SIZE(4, 4), 1, 0, 5, 1},
- {2, UPB_SIZE(8, 8), 2, 0, 5, 1},
- };
- const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit = {
- NULL,
- &google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[0],
- UPB_SIZE(12, 12), 2, false,
- };
- static const upb_msglayout *const google_protobuf_EnumValueDescriptorProto_submsgs[1] = {
- &google_protobuf_EnumValueOptions_msginit,
- };
- static const upb_msglayout_field google_protobuf_EnumValueDescriptorProto__fields[3] = {
- {1, UPB_SIZE(8, 8), 2, 0, 9, 1},
- {2, UPB_SIZE(4, 4), 1, 0, 5, 1},
- {3, UPB_SIZE(16, 24), 3, 0, 11, 1},
- };
- const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit = {
- &google_protobuf_EnumValueDescriptorProto_submsgs[0],
- &google_protobuf_EnumValueDescriptorProto__fields[0],
- UPB_SIZE(24, 32), 3, false,
- };
- static const upb_msglayout *const google_protobuf_ServiceDescriptorProto_submsgs[2] = {
- &google_protobuf_MethodDescriptorProto_msginit,
- &google_protobuf_ServiceOptions_msginit,
- };
- static const upb_msglayout_field google_protobuf_ServiceDescriptorProto__fields[3] = {
- {1, UPB_SIZE(4, 8), 1, 0, 9, 1},
- {2, UPB_SIZE(16, 32), 0, 0, 11, 3},
- {3, UPB_SIZE(12, 24), 2, 1, 11, 1},
- };
- const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit = {
- &google_protobuf_ServiceDescriptorProto_submsgs[0],
- &google_protobuf_ServiceDescriptorProto__fields[0],
- UPB_SIZE(24, 48), 3, false,
- };
- static const upb_msglayout *const google_protobuf_MethodDescriptorProto_submsgs[1] = {
- &google_protobuf_MethodOptions_msginit,
- };
- static const upb_msglayout_field google_protobuf_MethodDescriptorProto__fields[6] = {
- {1, UPB_SIZE(4, 8), 3, 0, 9, 1},
- {2, UPB_SIZE(12, 24), 4, 0, 9, 1},
- {3, UPB_SIZE(20, 40), 5, 0, 9, 1},
- {4, UPB_SIZE(28, 56), 6, 0, 11, 1},
- {5, UPB_SIZE(1, 1), 1, 0, 8, 1},
- {6, UPB_SIZE(2, 2), 2, 0, 8, 1},
- };
- const upb_msglayout google_protobuf_MethodDescriptorProto_msginit = {
- &google_protobuf_MethodDescriptorProto_submsgs[0],
- &google_protobuf_MethodDescriptorProto__fields[0],
- UPB_SIZE(32, 64), 6, false,
- };
- static const upb_msglayout *const google_protobuf_FileOptions_submsgs[1] = {
- &google_protobuf_UninterpretedOption_msginit,
- };
- static const upb_msglayout_field google_protobuf_FileOptions__fields[19] = {
- {1, UPB_SIZE(28, 32), 11, 0, 9, 1},
- {8, UPB_SIZE(36, 48), 12, 0, 9, 1},
- {9, UPB_SIZE(8, 8), 1, 0, 14, 1},
- {10, UPB_SIZE(16, 16), 2, 0, 8, 1},
- {11, UPB_SIZE(44, 64), 13, 0, 9, 1},
- {16, UPB_SIZE(17, 17), 3, 0, 8, 1},
- {17, UPB_SIZE(18, 18), 4, 0, 8, 1},
- {18, UPB_SIZE(19, 19), 5, 0, 8, 1},
- {20, UPB_SIZE(20, 20), 6, 0, 8, 1},
- {23, UPB_SIZE(21, 21), 7, 0, 8, 1},
- {27, UPB_SIZE(22, 22), 8, 0, 8, 1},
- {31, UPB_SIZE(23, 23), 9, 0, 8, 1},
- {36, UPB_SIZE(52, 80), 14, 0, 9, 1},
- {37, UPB_SIZE(60, 96), 15, 0, 9, 1},
- {39, UPB_SIZE(68, 112), 16, 0, 9, 1},
- {40, UPB_SIZE(76, 128), 17, 0, 9, 1},
- {41, UPB_SIZE(84, 144), 18, 0, 9, 1},
- {42, UPB_SIZE(24, 24), 10, 0, 8, 1},
- {999, UPB_SIZE(92, 160), 0, 0, 11, 3},
- };
- const upb_msglayout google_protobuf_FileOptions_msginit = {
- &google_protobuf_FileOptions_submsgs[0],
- &google_protobuf_FileOptions__fields[0],
- UPB_SIZE(96, 176), 19, false,
- };
- static const upb_msglayout *const google_protobuf_MessageOptions_submsgs[1] = {
- &google_protobuf_UninterpretedOption_msginit,
- };
- static const upb_msglayout_field google_protobuf_MessageOptions__fields[5] = {
- {1, UPB_SIZE(1, 1), 1, 0, 8, 1},
- {2, UPB_SIZE(2, 2), 2, 0, 8, 1},
- {3, UPB_SIZE(3, 3), 3, 0, 8, 1},
- {7, UPB_SIZE(4, 4), 4, 0, 8, 1},
- {999, UPB_SIZE(8, 8), 0, 0, 11, 3},
- };
- const upb_msglayout google_protobuf_MessageOptions_msginit = {
- &google_protobuf_MessageOptions_submsgs[0],
- &google_protobuf_MessageOptions__fields[0],
- UPB_SIZE(12, 16), 5, false,
- };
- static const upb_msglayout *const google_protobuf_FieldOptions_submsgs[1] = {
- &google_protobuf_UninterpretedOption_msginit,
- };
- static const upb_msglayout_field google_protobuf_FieldOptions__fields[7] = {
- {1, UPB_SIZE(8, 8), 1, 0, 14, 1},
- {2, UPB_SIZE(24, 24), 3, 0, 8, 1},
- {3, UPB_SIZE(25, 25), 4, 0, 8, 1},
- {5, UPB_SIZE(26, 26), 5, 0, 8, 1},
- {6, UPB_SIZE(16, 16), 2, 0, 14, 1},
- {10, UPB_SIZE(27, 27), 6, 0, 8, 1},
- {999, UPB_SIZE(28, 32), 0, 0, 11, 3},
- };
- const upb_msglayout google_protobuf_FieldOptions_msginit = {
- &google_protobuf_FieldOptions_submsgs[0],
- &google_protobuf_FieldOptions__fields[0],
- UPB_SIZE(32, 40), 7, false,
- };
- static const upb_msglayout *const google_protobuf_OneofOptions_submsgs[1] = {
- &google_protobuf_UninterpretedOption_msginit,
- };
- static const upb_msglayout_field google_protobuf_OneofOptions__fields[1] = {
- {999, UPB_SIZE(0, 0), 0, 0, 11, 3},
- };
- const upb_msglayout google_protobuf_OneofOptions_msginit = {
- &google_protobuf_OneofOptions_submsgs[0],
- &google_protobuf_OneofOptions__fields[0],
- UPB_SIZE(4, 8), 1, false,
- };
- static const upb_msglayout *const google_protobuf_EnumOptions_submsgs[1] = {
- &google_protobuf_UninterpretedOption_msginit,
- };
- static const upb_msglayout_field google_protobuf_EnumOptions__fields[3] = {
- {2, UPB_SIZE(1, 1), 1, 0, 8, 1},
- {3, UPB_SIZE(2, 2), 2, 0, 8, 1},
- {999, UPB_SIZE(4, 8), 0, 0, 11, 3},
- };
- const upb_msglayout google_protobuf_EnumOptions_msginit = {
- &google_protobuf_EnumOptions_submsgs[0],
- &google_protobuf_EnumOptions__fields[0],
- UPB_SIZE(8, 16), 3, false,
- };
- static const upb_msglayout *const google_protobuf_EnumValueOptions_submsgs[1] = {
- &google_protobuf_UninterpretedOption_msginit,
- };
- static const upb_msglayout_field google_protobuf_EnumValueOptions__fields[2] = {
- {1, UPB_SIZE(1, 1), 1, 0, 8, 1},
- {999, UPB_SIZE(4, 8), 0, 0, 11, 3},
- };
- const upb_msglayout google_protobuf_EnumValueOptions_msginit = {
- &google_protobuf_EnumValueOptions_submsgs[0],
- &google_protobuf_EnumValueOptions__fields[0],
- UPB_SIZE(8, 16), 2, false,
- };
- static const upb_msglayout *const google_protobuf_ServiceOptions_submsgs[1] = {
- &google_protobuf_UninterpretedOption_msginit,
- };
- static const upb_msglayout_field google_protobuf_ServiceOptions__fields[2] = {
- {33, UPB_SIZE(1, 1), 1, 0, 8, 1},
- {999, UPB_SIZE(4, 8), 0, 0, 11, 3},
- };
- const upb_msglayout google_protobuf_ServiceOptions_msginit = {
- &google_protobuf_ServiceOptions_submsgs[0],
- &google_protobuf_ServiceOptions__fields[0],
- UPB_SIZE(8, 16), 2, false,
- };
- static const upb_msglayout *const google_protobuf_MethodOptions_submsgs[1] = {
- &google_protobuf_UninterpretedOption_msginit,
- };
- static const upb_msglayout_field google_protobuf_MethodOptions__fields[3] = {
- {33, UPB_SIZE(16, 16), 2, 0, 8, 1},
- {34, UPB_SIZE(8, 8), 1, 0, 14, 1},
- {999, UPB_SIZE(20, 24), 0, 0, 11, 3},
- };
- const upb_msglayout google_protobuf_MethodOptions_msginit = {
- &google_protobuf_MethodOptions_submsgs[0],
- &google_protobuf_MethodOptions__fields[0],
- UPB_SIZE(24, 32), 3, false,
- };
- static const upb_msglayout *const google_protobuf_UninterpretedOption_submsgs[1] = {
- &google_protobuf_UninterpretedOption_NamePart_msginit,
- };
- static const upb_msglayout_field google_protobuf_UninterpretedOption__fields[7] = {
- {2, UPB_SIZE(56, 80), 0, 0, 11, 3},
- {3, UPB_SIZE(32, 32), 4, 0, 9, 1},
- {4, UPB_SIZE(8, 8), 1, 0, 4, 1},
- {5, UPB_SIZE(16, 16), 2, 0, 3, 1},
- {6, UPB_SIZE(24, 24), 3, 0, 1, 1},
- {7, UPB_SIZE(40, 48), 5, 0, 12, 1},
- {8, UPB_SIZE(48, 64), 6, 0, 9, 1},
- };
- const upb_msglayout google_protobuf_UninterpretedOption_msginit = {
- &google_protobuf_UninterpretedOption_submsgs[0],
- &google_protobuf_UninterpretedOption__fields[0],
- UPB_SIZE(64, 96), 7, false,
- };
- static const upb_msglayout_field google_protobuf_UninterpretedOption_NamePart__fields[2] = {
- {1, UPB_SIZE(4, 8), 2, 0, 9, 2},
- {2, UPB_SIZE(1, 1), 1, 0, 8, 2},
- };
- const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit = {
- NULL,
- &google_protobuf_UninterpretedOption_NamePart__fields[0],
- UPB_SIZE(16, 32), 2, false,
- };
- static const upb_msglayout *const google_protobuf_SourceCodeInfo_submsgs[1] = {
- &google_protobuf_SourceCodeInfo_Location_msginit,
- };
- static const upb_msglayout_field google_protobuf_SourceCodeInfo__fields[1] = {
- {1, UPB_SIZE(0, 0), 0, 0, 11, 3},
- };
- const upb_msglayout google_protobuf_SourceCodeInfo_msginit = {
- &google_protobuf_SourceCodeInfo_submsgs[0],
- &google_protobuf_SourceCodeInfo__fields[0],
- UPB_SIZE(4, 8), 1, false,
- };
- static const upb_msglayout_field google_protobuf_SourceCodeInfo_Location__fields[5] = {
- {1, UPB_SIZE(20, 40), 0, 0, 5, 3},
- {2, UPB_SIZE(24, 48), 0, 0, 5, 3},
- {3, UPB_SIZE(4, 8), 1, 0, 9, 1},
- {4, UPB_SIZE(12, 24), 2, 0, 9, 1},
- {6, UPB_SIZE(28, 56), 0, 0, 9, 3},
- };
- const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit = {
- NULL,
- &google_protobuf_SourceCodeInfo_Location__fields[0],
- UPB_SIZE(32, 64), 5, false,
- };
- static const upb_msglayout *const google_protobuf_GeneratedCodeInfo_submsgs[1] = {
- &google_protobuf_GeneratedCodeInfo_Annotation_msginit,
- };
- static const upb_msglayout_field google_protobuf_GeneratedCodeInfo__fields[1] = {
- {1, UPB_SIZE(0, 0), 0, 0, 11, 3},
- };
- const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit = {
- &google_protobuf_GeneratedCodeInfo_submsgs[0],
- &google_protobuf_GeneratedCodeInfo__fields[0],
- UPB_SIZE(4, 8), 1, false,
- };
- static const upb_msglayout_field google_protobuf_GeneratedCodeInfo_Annotation__fields[4] = {
- {1, UPB_SIZE(20, 32), 0, 0, 5, 3},
- {2, UPB_SIZE(12, 16), 3, 0, 9, 1},
- {3, UPB_SIZE(4, 4), 1, 0, 5, 1},
- {4, UPB_SIZE(8, 8), 2, 0, 5, 1},
- };
- const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit = {
- NULL,
- &google_protobuf_GeneratedCodeInfo_Annotation__fields[0],
- UPB_SIZE(24, 48), 4, false,
- };
- /* Maps descriptor type -> upb field type. */
- const uint8_t upb_desctype_to_fieldtype[] = {
- UPB_WIRE_TYPE_END_GROUP, /* ENDGROUP */
- UPB_TYPE_DOUBLE, /* DOUBLE */
- UPB_TYPE_FLOAT, /* FLOAT */
- UPB_TYPE_INT64, /* INT64 */
- UPB_TYPE_UINT64, /* UINT64 */
- UPB_TYPE_INT32, /* INT32 */
- UPB_TYPE_UINT64, /* FIXED64 */
- UPB_TYPE_UINT32, /* FIXED32 */
- UPB_TYPE_BOOL, /* BOOL */
- UPB_TYPE_STRING, /* STRING */
- UPB_TYPE_MESSAGE, /* GROUP */
- UPB_TYPE_MESSAGE, /* MESSAGE */
- UPB_TYPE_BYTES, /* BYTES */
- UPB_TYPE_UINT32, /* UINT32 */
- UPB_TYPE_ENUM, /* ENUM */
- UPB_TYPE_INT32, /* SFIXED32 */
- UPB_TYPE_INT64, /* SFIXED64 */
- UPB_TYPE_INT32, /* SINT32 */
- UPB_TYPE_INT64, /* SINT64 */
- };
- /* Data pertaining to the parse. */
- typedef struct {
- /* Current decoding pointer. Points to the beginning of a field until we
- * have finished decoding the whole field. */
- const char *ptr;
- } upb_decstate;
- /* Data pertaining to a single message frame. */
- typedef struct {
- const char *limit;
- int32_t group_number; /* 0 if we are not parsing a group. */
- /* These members are unset for an unknown group frame. */
- char *msg;
- const upb_msglayout *m;
- } upb_decframe;
- #define CHK(x) if (!(x)) { return false; }
- static bool upb_skip_unknowngroup(upb_decstate *d, int field_number,
- const char *limit);
- static bool upb_decode_message(upb_decstate *d, const char *limit,
- int group_number, char *msg,
- const upb_msglayout *l);
- static bool upb_decode_varint(const char **ptr, const char *limit,
- uint64_t *val) {
- uint8_t byte;
- int bitpos = 0;
- const char *p = *ptr;
- *val = 0;
- do {
- CHK(bitpos < 70 && p < limit);
- byte = *p;
- *val |= (uint64_t)(byte & 0x7F) << bitpos;
- p++;
- bitpos += 7;
- } while (byte & 0x80);
- *ptr = p;
- return true;
- }
- static bool upb_decode_varint32(const char **ptr, const char *limit,
- uint32_t *val) {
- uint64_t u64;
- CHK(upb_decode_varint(ptr, limit, &u64) && u64 <= UINT32_MAX);
- *val = u64;
- return true;
- }
- static bool upb_decode_64bit(const char **ptr, const char *limit,
- uint64_t *val) {
- CHK(limit - *ptr >= 8);
- memcpy(val, *ptr, 8);
- *ptr += 8;
- return true;
- }
- static bool upb_decode_32bit(const char **ptr, const char *limit,
- uint32_t *val) {
- CHK(limit - *ptr >= 4);
- memcpy(val, *ptr, 4);
- *ptr += 4;
- return true;
- }
- static bool upb_decode_tag(const char **ptr, const char *limit,
- int *field_number, int *wire_type) {
- uint32_t tag = 0;
- CHK(upb_decode_varint32(ptr, limit, &tag));
- *field_number = tag >> 3;
- *wire_type = tag & 7;
- return true;
- }
- static int32_t upb_zzdecode_32(uint32_t n) {
- return (n >> 1) ^ -(int32_t)(n & 1);
- }
- static int64_t upb_zzdecode_64(uint64_t n) {
- return (n >> 1) ^ -(int64_t)(n & 1);
- }
- static bool upb_decode_string(const char **ptr, const char *limit,
- upb_strview *val) {
- uint32_t len;
- CHK(upb_decode_varint32(ptr, limit, &len) &&
- len < INT32_MAX &&
- limit - *ptr >= (int32_t)len);
- *val = upb_strview_make(*ptr, len);
- *ptr += len;
- return true;
- }
- static void upb_set32(void *msg, size_t ofs, uint32_t val) {
- memcpy((char*)msg + ofs, &val, sizeof(val));
- }
- static bool upb_append_unknown(upb_decstate *d, upb_decframe *frame,
- const char *start) {
- upb_msg_addunknown(frame->msg, start, d->ptr - start);
- return true;
- }
- static bool upb_skip_unknownfielddata(upb_decstate *d, upb_decframe *frame,
- int field_number, int wire_type) {
- switch (wire_type) {
- case UPB_WIRE_TYPE_VARINT: {
- uint64_t val;
- return upb_decode_varint(&d->ptr, frame->limit, &val);
- }
- case UPB_WIRE_TYPE_32BIT: {
- uint32_t val;
- return upb_decode_32bit(&d->ptr, frame->limit, &val);
- }
- case UPB_WIRE_TYPE_64BIT: {
- uint64_t val;
- return upb_decode_64bit(&d->ptr, frame->limit, &val);
- }
- case UPB_WIRE_TYPE_DELIMITED: {
- upb_strview val;
- return upb_decode_string(&d->ptr, frame->limit, &val);
- }
- case UPB_WIRE_TYPE_START_GROUP:
- return upb_skip_unknowngroup(d, field_number, frame->limit);
- case UPB_WIRE_TYPE_END_GROUP:
- CHK(field_number == frame->group_number);
- frame->limit = d->ptr;
- return true;
- }
- return false;
- }
- static bool upb_array_grow(upb_array *arr, size_t elements) {
- size_t needed = arr->len + elements;
- size_t new_size = UPB_MAX(arr->size, 8);
- size_t new_bytes;
- size_t old_bytes;
- void *new_data;
- upb_alloc *alloc = upb_arena_alloc(arr->arena);
- while (new_size < needed) {
- new_size *= 2;
- }
- old_bytes = arr->len * arr->element_size;
- new_bytes = new_size * arr->element_size;
- new_data = upb_realloc(alloc, arr->data, old_bytes, new_bytes);
- CHK(new_data);
- arr->data = new_data;
- arr->size = new_size;
- return true;
- }
- static void *upb_array_reserve(upb_array *arr, size_t elements) {
- if (arr->size - arr->len < elements) {
- CHK(upb_array_grow(arr, elements));
- }
- return (char*)arr->data + (arr->len * arr->element_size);
- }
- static void *upb_array_add(upb_array *arr, size_t elements) {
- void *ret = upb_array_reserve(arr, elements);
- arr->len += elements;
- return ret;
- }
- static upb_array *upb_getarr(upb_decframe *frame,
- const upb_msglayout_field *field) {
- UPB_ASSERT(field->label == UPB_LABEL_REPEATED);
- return *(upb_array**)&frame->msg[field->offset];
- }
- static upb_array *upb_getorcreatearr(upb_decframe *frame,
- const upb_msglayout_field *field) {
- upb_array *arr = upb_getarr(frame, field);
- if (!arr) {
- upb_fieldtype_t type = upb_desctype_to_fieldtype[field->descriptortype];
- arr = upb_array_new(type, upb_msg_arena(frame->msg));
- if (!arr) {
- return NULL;
- }
- *(upb_array**)&frame->msg[field->offset] = arr;
- }
- return arr;
- }
- static void upb_sethasbit(upb_decframe *frame,
- const upb_msglayout_field *field) {
- int32_t hasbit = field->presence;
- UPB_ASSERT(field->presence > 0);
- frame->msg[hasbit / 8] |= (1 << (hasbit % 8));
- }
- static void upb_setoneofcase(upb_decframe *frame,
- const upb_msglayout_field *field) {
- UPB_ASSERT(field->presence < 0);
- upb_set32(frame->msg, ~field->presence, field->number);
- }
- static char *upb_decode_prepareslot(upb_decframe *frame,
- const upb_msglayout_field *field) {
- char *field_mem = frame->msg + field->offset;
- upb_array *arr;
- if (field->label == UPB_LABEL_REPEATED) {
- arr = upb_getorcreatearr(frame, field);
- field_mem = upb_array_reserve(arr, 1);
- }
- return field_mem;
- }
- static void upb_decode_setpresent(upb_decframe *frame,
- const upb_msglayout_field *field) {
- if (field->label == UPB_LABEL_REPEATED) {
- upb_array *arr = upb_getarr(frame, field);
- UPB_ASSERT(arr->len < arr->size);
- arr->len++;
- } else if (field->presence < 0) {
- upb_setoneofcase(frame, field);
- } else if (field->presence > 0) {
- upb_sethasbit(frame, field);
- }
- }
- static bool upb_decode_submsg(upb_decstate *d, upb_decframe *frame,
- const char *limit,
- const upb_msglayout_field *field,
- int group_number) {
- char *submsg_slot = upb_decode_prepareslot(frame, field);
- char *submsg = *(void **)submsg_slot;
- const upb_msglayout *subm;
- subm = frame->m->submsgs[field->submsg_index];
- UPB_ASSERT(subm);
- if (!submsg) {
- submsg = upb_msg_new(subm, upb_msg_arena(frame->msg));
- CHK(submsg);
- *(void**)submsg_slot = submsg;
- }
- upb_decode_message(d, limit, group_number, submsg, subm);
- return true;
- }
- static bool upb_decode_varintfield(upb_decstate *d, upb_decframe *frame,
- const char *field_start,
- const upb_msglayout_field *field) {
- uint64_t val;
- void *field_mem;
- field_mem = upb_decode_prepareslot(frame, field);
- CHK(field_mem);
- CHK(upb_decode_varint(&d->ptr, frame->limit, &val));
- switch ((upb_descriptortype_t)field->descriptortype) {
- case UPB_DESCRIPTOR_TYPE_INT64:
- case UPB_DESCRIPTOR_TYPE_UINT64:
- memcpy(field_mem, &val, sizeof(val));
- break;
- case UPB_DESCRIPTOR_TYPE_INT32:
- case UPB_DESCRIPTOR_TYPE_UINT32:
- case UPB_DESCRIPTOR_TYPE_ENUM: {
- uint32_t val32 = val;
- memcpy(field_mem, &val32, sizeof(val32));
- break;
- }
- case UPB_DESCRIPTOR_TYPE_BOOL: {
- bool valbool = val != 0;
- memcpy(field_mem, &valbool, sizeof(valbool));
- break;
- }
- case UPB_DESCRIPTOR_TYPE_SINT32: {
- int32_t decoded = upb_zzdecode_32(val);
- memcpy(field_mem, &decoded, sizeof(decoded));
- break;
- }
- case UPB_DESCRIPTOR_TYPE_SINT64: {
- int64_t decoded = upb_zzdecode_64(val);
- memcpy(field_mem, &decoded, sizeof(decoded));
- break;
- }
- default:
- return upb_append_unknown(d, frame, field_start);
- }
- upb_decode_setpresent(frame, field);
- return true;
- }
- static bool upb_decode_64bitfield(upb_decstate *d, upb_decframe *frame,
- const char *field_start,
- const upb_msglayout_field *field) {
- void *field_mem;
- uint64_t val;
- field_mem = upb_decode_prepareslot(frame, field);
- CHK(field_mem);
- CHK(upb_decode_64bit(&d->ptr, frame->limit, &val));
- switch ((upb_descriptortype_t)field->descriptortype) {
- case UPB_DESCRIPTOR_TYPE_DOUBLE:
- case UPB_DESCRIPTOR_TYPE_FIXED64:
- case UPB_DESCRIPTOR_TYPE_SFIXED64:
- memcpy(field_mem, &val, sizeof(val));
- break;
- default:
- return upb_append_unknown(d, frame, field_start);
- }
- upb_decode_setpresent(frame, field);
- return true;
- }
- static bool upb_decode_32bitfield(upb_decstate *d, upb_decframe *frame,
- const char *field_start,
- const upb_msglayout_field *field) {
- void *field_mem;
- uint32_t val;
- field_mem = upb_decode_prepareslot(frame, field);
- CHK(field_mem);
- CHK(upb_decode_32bit(&d->ptr, frame->limit, &val));
- switch ((upb_descriptortype_t)field->descriptortype) {
- case UPB_DESCRIPTOR_TYPE_FLOAT:
- case UPB_DESCRIPTOR_TYPE_FIXED32:
- case UPB_DESCRIPTOR_TYPE_SFIXED32:
- memcpy(field_mem, &val, sizeof(val));
- break;
- default:
- return upb_append_unknown(d, frame, field_start);
- }
- upb_decode_setpresent(frame, field);
- return true;
- }
- static bool upb_decode_fixedpacked(upb_array *arr, upb_strview data,
- int elem_size) {
- int elements = data.size / elem_size;
- void *field_mem;
- CHK((size_t)(elements * elem_size) == data.size);
- field_mem = upb_array_add(arr, elements);
- CHK(field_mem);
- memcpy(field_mem, data.data, data.size);
- return true;
- }
- static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame,
- const char *field_start,
- const upb_msglayout_field *field,
- upb_strview val) {
- upb_array *arr = upb_getorcreatearr(frame, field);
- #define VARINT_CASE(ctype, decode) { \
- const char *ptr = val.data; \
- const char *limit = ptr + val.size; \
- while (ptr < limit) { \
- uint64_t val; \
- void *field_mem; \
- ctype decoded; \
- CHK(upb_decode_varint(&ptr, limit, &val)); \
- decoded = (decode)(val); \
- field_mem = upb_array_add(arr, 1); \
- CHK(field_mem); \
- memcpy(field_mem, &decoded, sizeof(ctype)); \
- } \
- return true; \
- }
- switch ((upb_descriptortype_t)field->descriptortype) {
- case UPB_DESCRIPTOR_TYPE_STRING:
- case UPB_DESCRIPTOR_TYPE_BYTES: {
- void *field_mem = upb_array_add(arr, 1);
- CHK(field_mem);
- memcpy(field_mem, &val, sizeof(val));
- return true;
- }
- case UPB_DESCRIPTOR_TYPE_FLOAT:
- case UPB_DESCRIPTOR_TYPE_FIXED32:
- case UPB_DESCRIPTOR_TYPE_SFIXED32:
- return upb_decode_fixedpacked(arr, val, sizeof(int32_t));
- case UPB_DESCRIPTOR_TYPE_DOUBLE:
- case UPB_DESCRIPTOR_TYPE_FIXED64:
- case UPB_DESCRIPTOR_TYPE_SFIXED64:
- return upb_decode_fixedpacked(arr, val, sizeof(int64_t));
- case UPB_DESCRIPTOR_TYPE_INT32:
- case UPB_DESCRIPTOR_TYPE_UINT32:
- case UPB_DESCRIPTOR_TYPE_ENUM:
- /* TODO: proto2 enum field that isn't in the enum. */
- VARINT_CASE(uint32_t, uint32_t);
- case UPB_DESCRIPTOR_TYPE_INT64:
- case UPB_DESCRIPTOR_TYPE_UINT64:
- VARINT_CASE(uint64_t, uint64_t);
- case UPB_DESCRIPTOR_TYPE_BOOL:
- VARINT_CASE(bool, bool);
- case UPB_DESCRIPTOR_TYPE_SINT32:
- VARINT_CASE(int32_t, upb_zzdecode_32);
- case UPB_DESCRIPTOR_TYPE_SINT64:
- VARINT_CASE(int64_t, upb_zzdecode_64);
- case UPB_DESCRIPTOR_TYPE_MESSAGE: {
- const upb_msglayout *subm;
- char *submsg;
- void *field_mem;
- CHK(val.size <= (size_t)(frame->limit - val.data));
- d->ptr -= val.size;
- /* Create elemente message. */
- subm = frame->m->submsgs[field->submsg_index];
- UPB_ASSERT(subm);
- submsg = upb_msg_new(subm, upb_msg_arena(frame->msg));
- CHK(submsg);
- field_mem = upb_array_add(arr, 1);
- CHK(field_mem);
- *(void**)field_mem = submsg;
- return upb_decode_message(
- d, val.data + val.size, frame->group_number, submsg, subm);
- }
- case UPB_DESCRIPTOR_TYPE_GROUP:
- return upb_append_unknown(d, frame, field_start);
- }
- #undef VARINT_CASE
- UPB_UNREACHABLE();
- }
- static bool upb_decode_delimitedfield(upb_decstate *d, upb_decframe *frame,
- const char *field_start,
- const upb_msglayout_field *field) {
- upb_strview val;
- CHK(upb_decode_string(&d->ptr, frame->limit, &val));
- if (field->label == UPB_LABEL_REPEATED) {
- return upb_decode_toarray(d, frame, field_start, field, val);
- } else {
- switch ((upb_descriptortype_t)field->descriptortype) {
- case UPB_DESCRIPTOR_TYPE_STRING:
- case UPB_DESCRIPTOR_TYPE_BYTES: {
- void *field_mem = upb_decode_prepareslot(frame, field);
- CHK(field_mem);
- memcpy(field_mem, &val, sizeof(val));
- break;
- }
- case UPB_DESCRIPTOR_TYPE_MESSAGE:
- CHK(val.size <= (size_t)(frame->limit - val.data));
- d->ptr -= val.size;
- CHK(upb_decode_submsg(d, frame, val.data + val.size, field, 0));
- break;
- default:
- /* TODO(haberman): should we accept the last element of a packed? */
- return upb_append_unknown(d, frame, field_start);
- }
- upb_decode_setpresent(frame, field);
- return true;
- }
- }
- static const upb_msglayout_field *upb_find_field(const upb_msglayout *l,
- uint32_t field_number) {
- /* Lots of optimization opportunities here. */
- int i;
- for (i = 0; i < l->field_count; i++) {
- if (l->fields[i].number == field_number) {
- return &l->fields[i];
- }
- }
- return NULL; /* Unknown field. */
- }
- static bool upb_decode_field(upb_decstate *d, upb_decframe *frame) {
- int field_number;
- int wire_type;
- const char *field_start = d->ptr;
- const upb_msglayout_field *field;
- CHK(upb_decode_tag(&d->ptr, frame->limit, &field_number, &wire_type));
- field = upb_find_field(frame->m, field_number);
- if (field) {
- switch (wire_type) {
- case UPB_WIRE_TYPE_VARINT:
- return upb_decode_varintfield(d, frame, field_start, field);
- case UPB_WIRE_TYPE_32BIT:
- return upb_decode_32bitfield(d, frame, field_start, field);
- case UPB_WIRE_TYPE_64BIT:
- return upb_decode_64bitfield(d, frame, field_start, field);
- case UPB_WIRE_TYPE_DELIMITED:
- return upb_decode_delimitedfield(d, frame, field_start, field);
- case UPB_WIRE_TYPE_START_GROUP:
- CHK(field->descriptortype == UPB_DESCRIPTOR_TYPE_GROUP);
- return upb_decode_submsg(d, frame, frame->limit, field, field_number);
- case UPB_WIRE_TYPE_END_GROUP:
- CHK(frame->group_number == field_number)
- frame->limit = d->ptr;
- return true;
- default:
- return false;
- }
- } else {
- CHK(field_number != 0);
- CHK(upb_skip_unknownfielddata(d, frame, field_number, wire_type));
- CHK(upb_append_unknown(d, frame, field_start));
- return true;
- }
- }
- static bool upb_skip_unknowngroup(upb_decstate *d, int field_number,
- const char *limit) {
- upb_decframe frame;
- frame.msg = NULL;
- frame.m = NULL;
- frame.group_number = field_number;
- frame.limit = limit;
- while (d->ptr < frame.limit) {
- int wire_type;
- int field_number;
- CHK(upb_decode_tag(&d->ptr, frame.limit, &field_number, &wire_type));
- CHK(upb_skip_unknownfielddata(d, &frame, field_number, wire_type));
- }
- return true;
- }
- static bool upb_decode_message(upb_decstate *d, const char *limit,
- int group_number, char *msg,
- const upb_msglayout *l) {
- upb_decframe frame;
- frame.group_number = group_number;
- frame.limit = limit;
- frame.msg = msg;
- frame.m = l;
- while (d->ptr < frame.limit) {
- CHK(upb_decode_field(d, &frame));
- }
- return true;
- }
- bool upb_decode(upb_strview buf, void *msg, const upb_msglayout *l) {
- upb_decstate state;
- state.ptr = buf.data;
- return upb_decode_message(&state, buf.data + buf.size, 0, msg, l);
- }
- #undef CHK
- #include <ctype.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct {
- size_t len;
- char str[1]; /* Null-terminated string data follows. */
- } str_t;
- static str_t *newstr(const char *data, size_t len) {
- str_t *ret = upb_gmalloc(sizeof(*ret) + len);
- if (!ret) return NULL;
- ret->len = len;
- memcpy(ret->str, data, len);
- ret->str[len] = '\0';
- return ret;
- }
- static void freestr(str_t *s) { upb_gfree(s); }
- /* isalpha() etc. from <ctype.h> are locale-dependent, which we don't want. */
- static bool upb_isbetween(char c, char low, char high) {
- return c >= low && c <= high;
- }
- static bool upb_isletter(char c) {
- return upb_isbetween(c, 'A', 'Z') || upb_isbetween(c, 'a', 'z') || c == '_';
- }
- static bool upb_isalphanum(char c) {
- return upb_isletter(c) || upb_isbetween(c, '0', '9');
- }
- static bool upb_isident(const char *str, size_t len, bool full, upb_status *s) {
- bool start = true;
- size_t i;
- for (i = 0; i < len; i++) {
- char c = str[i];
- if (c == '.') {
- if (start || !full) {
- upb_status_seterrf(s, "invalid name: unexpected '.' (%s)", str);
- return false;
- }
- start = true;
- } else if (start) {
- if (!upb_isletter(c)) {
- upb_status_seterrf(
- s, "invalid name: path components must start with a letter (%s)",
- str);
- return false;
- }
- start = false;
- } else {
- if (!upb_isalphanum(c)) {
- upb_status_seterrf(s, "invalid name: non-alphanumeric character (%s)",
- str);
- return false;
- }
- }
- }
- return !start;
- }
- static bool upb_isoneof(const upb_refcounted *def) {
- return def->vtbl == &upb_oneofdef_vtbl;
- }
- static bool upb_isfield(const upb_refcounted *def) {
- return def->vtbl == &upb_fielddef_vtbl;
- }
- static const upb_oneofdef *upb_trygetoneof(const upb_refcounted *def) {
- return upb_isoneof(def) ? (const upb_oneofdef*)def : NULL;
- }
- static const upb_fielddef *upb_trygetfield(const upb_refcounted *def) {
- return upb_isfield(def) ? (const upb_fielddef*)def : NULL;
- }
- /* upb_def ********************************************************************/
- upb_deftype_t upb_def_type(const upb_def *d) { return d->type; }
- const char *upb_def_fullname(const upb_def *d) { return d->fullname; }
- const char *upb_def_name(const upb_def *d) {
- const char *p;
- if (d->fullname == NULL) {
- return NULL;
- } else if ((p = strrchr(d->fullname, '.')) == NULL) {
- /* No '.' in the name, return the full string. */
- return d->fullname;
- } else {
- /* Return one past the last '.'. */
- return p + 1;
- }
- }
- bool upb_def_setfullname(upb_def *def, const char *fullname, upb_status *s) {
- UPB_ASSERT(!upb_def_isfrozen(def));
- if (!upb_isident(fullname, strlen(fullname), true, s)) {
- return false;
- }
- fullname = upb_gstrdup(fullname);
- if (!fullname) {
- upb_upberr_setoom(s);
- return false;
- }
- upb_gfree((void*)def->fullname);
- def->fullname = fullname;
- return true;
- }
- const upb_filedef *upb_def_file(const upb_def *d) { return d->file; }
- static bool upb_def_init(upb_def *def, upb_deftype_t type,
- const struct upb_refcounted_vtbl *vtbl,
- const void *owner) {
- if (!upb_refcounted_init(upb_def_upcast_mutable(def), vtbl, owner)) return false;
- def->type = type;
- def->fullname = NULL;
- def->came_from_user = false;
- def->file = NULL;
- return true;
- }
- static void upb_def_uninit(upb_def *def) {
- upb_gfree((void*)def->fullname);
- }
- static const char *msgdef_name(const upb_msgdef *m) {
- const char *name = upb_def_fullname(upb_msgdef_upcast(m));
- return name ? name : "(anonymous)";
- }
- static bool upb_validate_field(upb_fielddef *f, upb_status *s) {
- if (upb_fielddef_name(f) == NULL || upb_fielddef_number(f) == 0) {
- upb_status_seterrmsg(s, "fielddef must have name and number set");
- return false;
- }
- if (!f->type_is_set_) {
- upb_status_seterrmsg(s, "fielddef type was not initialized");
- return false;
- }
- if (upb_fielddef_lazy(f) &&
- upb_fielddef_descriptortype(f) != UPB_DESCRIPTOR_TYPE_MESSAGE) {
- upb_status_seterrmsg(s,
- "only length-delimited submessage fields may be lazy");
- return false;
- }
- if (upb_fielddef_hassubdef(f)) {
- const upb_def *subdef;
- if (f->subdef_is_symbolic) {
- upb_status_seterrf(s, "field '%s.%s' has not been resolved",
- msgdef_name(f->msg.def), upb_fielddef_name(f));
- return false;
- }
- subdef = upb_fielddef_subdef(f);
- if (subdef == NULL) {
- upb_status_seterrf(s, "field %s.%s is missing required subdef",
- msgdef_name(f->msg.def), upb_fielddef_name(f));
- return false;
- }
- if (!upb_def_isfrozen(subdef) && !subdef->came_from_user) {
- upb_status_seterrf(s,
- "subdef of field %s.%s is not frozen or being frozen",
- msgdef_name(f->msg.def), upb_fielddef_name(f));
- return false;
- }
- }
- if (upb_fielddef_type(f) == UPB_TYPE_ENUM) {
- bool has_default_name = upb_fielddef_enumhasdefaultstr(f);
- bool has_default_number = upb_fielddef_enumhasdefaultint32(f);
- /* Previously verified by upb_validate_enumdef(). */
- UPB_ASSERT(upb_enumdef_numvals(upb_fielddef_enumsubdef(f)) > 0);
- /* We've already validated that we have an associated enumdef and that it
- * has at least one member, so at least one of these should be true.
- * Because if the user didn't set anything, we'll pick up the enum's
- * default, but if the user *did* set something we should at least pick up
- * the one they set (int32 or string). */
- UPB_ASSERT(has_default_name || has_default_number);
- if (!has_default_name) {
- upb_status_seterrf(s,
- "enum default for field %s.%s (%d) is not in the enum",
- msgdef_name(f->msg.def), upb_fielddef_name(f),
- upb_fielddef_defaultint32(f));
- return false;
- }
- if (!has_default_number) {
- upb_status_seterrf(s,
- "enum default for field %s.%s (%s) is not in the enum",
- msgdef_name(f->msg.def), upb_fielddef_name(f),
- upb_fielddef_defaultstr(f, NULL));
- return false;
- }
- /* Lift the effective numeric default into the field's default slot, in case
- * we were only getting it "by reference" from the enumdef. */
- upb_fielddef_setdefaultint32(f, upb_fielddef_defaultint32(f));
- }
- /* Ensure that MapEntry submessages only appear as repeated fields, not
- * optional/required (singular) fields. */
- if (upb_fielddef_type(f) == UPB_TYPE_MESSAGE &&
- upb_fielddef_msgsubdef(f) != NULL) {
- const upb_msgdef *subdef = upb_fielddef_msgsubdef(f);
- if (upb_msgdef_mapentry(subdef) && !upb_fielddef_isseq(f)) {
- upb_status_seterrf(s,
- "Field %s refers to mapentry message but is not "
- "a repeated field",
- upb_fielddef_name(f) ? upb_fielddef_name(f) :
- "(unnamed)");
- return false;
- }
- }
- return true;
- }
- static bool upb_validate_enumdef(const upb_enumdef *e, upb_status *s) {
- if (upb_enumdef_numvals(e) == 0) {
- upb_status_seterrf(s, "enum %s has no members (must have at least one)",
- upb_enumdef_fullname(e));
- return false;
- }
- return true;
- }
- /* All submessage fields are lower than all other fields.
- * Secondly, fields are increasing in order. */
- uint32_t field_rank(const upb_fielddef *f) {
- uint32_t ret = upb_fielddef_number(f);
- const uint32_t high_bit = 1 << 30;
- UPB_ASSERT(ret < high_bit);
- if (!upb_fielddef_issubmsg(f))
- ret |= high_bit;
- return ret;
- }
- int cmp_fields(const void *p1, const void *p2) {
- const upb_fielddef *f1 = *(upb_fielddef*const*)p1;
- const upb_fielddef *f2 = *(upb_fielddef*const*)p2;
- return field_rank(f1) - field_rank(f2);
- }
- static bool assign_msg_indices(upb_msgdef *m, upb_status *s) {
- /* Sort fields. upb internally relies on UPB_TYPE_MESSAGE fields having the
- * lowest indexes, but we do not publicly guarantee this. */
- upb_msg_field_iter j;
- upb_msg_oneof_iter k;
- int i;
- uint32_t selector;
- int n = upb_msgdef_numfields(m);
- upb_fielddef **fields;
- if (n == 0) {
- m->selector_count = UPB_STATIC_SELECTOR_COUNT;
- m->submsg_field_count = 0;
- return true;
- }
- fields = upb_gmalloc(n * sizeof(*fields));
- if (!fields) {
- upb_upberr_setoom(s);
- return false;
- }
- m->submsg_field_count = 0;
- for(i = 0, upb_msg_field_begin(&j, m);
- !upb_msg_field_done(&j);
- upb_msg_field_next(&j), i++) {
- upb_fielddef *f = upb_msg_iter_field(&j);
- UPB_ASSERT(f->msg.def == m);
- if (!upb_validate_field(f, s)) {
- upb_gfree(fields);
- return false;
- }
- if (upb_fielddef_issubmsg(f)) {
- m->submsg_field_count++;
- }
- fields[i] = f;
- }
- qsort(fields, n, sizeof(*fields), cmp_fields);
- selector = UPB_STATIC_SELECTOR_COUNT + m->submsg_field_count;
- for (i = 0; i < n; i++) {
- upb_fielddef *f = fields[i];
- f->index_ = i;
- f->selector_base = selector + upb_handlers_selectorbaseoffset(f);
- selector += upb_handlers_selectorcount(f);
- }
- m->selector_count = selector;
- #ifndef NDEBUG
- {
- /* Verify that all selectors for the message are distinct. */
- #define TRY(type) \
- if (upb_handlers_getselector(f, type, &sel)) upb_inttable_insert(&t, sel, v);
- upb_inttable t;
- upb_value v;
- upb_selector_t sel;
- upb_inttable_init(&t, UPB_CTYPE_BOOL);
- v = upb_value_bool(true);
- upb_inttable_insert(&t, UPB_STARTMSG_SELECTOR, v);
- upb_inttable_insert(&t, UPB_ENDMSG_SELECTOR, v);
- upb_inttable_insert(&t, UPB_UNKNOWN_SELECTOR, v);
- for(upb_msg_field_begin(&j, m);
- !upb_msg_field_done(&j);
- upb_msg_field_next(&j)) {
- upb_fielddef *f = upb_msg_iter_field(&j);
- /* These calls will assert-fail in upb_table if the value already
- * exists. */
- TRY(UPB_HANDLER_INT32);
- TRY(UPB_HANDLER_INT64)
- TRY(UPB_HANDLER_UINT32)
- TRY(UPB_HANDLER_UINT64)
- TRY(UPB_HANDLER_FLOAT)
- TRY(UPB_HANDLER_DOUBLE)
- TRY(UPB_HANDLER_BOOL)
- TRY(UPB_HANDLER_STARTSTR)
- TRY(UPB_HANDLER_STRING)
- TRY(UPB_HANDLER_ENDSTR)
- TRY(UPB_HANDLER_STARTSUBMSG)
- TRY(UPB_HANDLER_ENDSUBMSG)
- TRY(UPB_HANDLER_STARTSEQ)
- TRY(UPB_HANDLER_ENDSEQ)
- }
- upb_inttable_uninit(&t);
- }
- #undef TRY
- #endif
- for(upb_msg_oneof_begin(&k, m), i = 0;
- !upb_msg_oneof_done(&k);
- upb_msg_oneof_next(&k), i++) {
- upb_oneofdef *o = upb_msg_iter_oneof(&k);
- o->index = i;
- }
- upb_gfree(fields);
- return true;
- }
- static void assign_msg_wellknowntype(upb_msgdef *m) {
- const char *name = upb_msgdef_fullname(m);
- if (name == NULL) {
- m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED;
- return;
- }
- if (!strcmp(name, "google.protobuf.Any")) {
- m->well_known_type = UPB_WELLKNOWN_ANY;
- } else if (!strcmp(name, "google.protobuf.FieldMask")) {
- m->well_known_type = UPB_WELLKNOWN_FIELDMASK;
- } else if (!strcmp(name, "google.protobuf.Duration")) {
- m->well_known_type = UPB_WELLKNOWN_DURATION;
- } else if (!strcmp(name, "google.protobuf.Timestamp")) {
- m->well_known_type = UPB_WELLKNOWN_TIMESTAMP;
- } else if (!strcmp(name, "google.protobuf.DoubleValue")) {
- m->well_known_type = UPB_WELLKNOWN_DOUBLEVALUE;
- } else if (!strcmp(name, "google.protobuf.FloatValue")) {
- m->well_known_type = UPB_WELLKNOWN_FLOATVALUE;
- } else if (!strcmp(name, "google.protobuf.Int64Value")) {
- m->well_known_type = UPB_WELLKNOWN_INT64VALUE;
- } else if (!strcmp(name, "google.protobuf.UInt64Value")) {
- m->well_known_type = UPB_WELLKNOWN_UINT64VALUE;
- } else if (!strcmp(name, "google.protobuf.Int32Value")) {
- m->well_known_type = UPB_WELLKNOWN_INT32VALUE;
- } else if (!strcmp(name, "google.protobuf.UInt32Value")) {
- m->well_known_type = UPB_WELLKNOWN_UINT32VALUE;
- } else if (!strcmp(name, "google.protobuf.BoolValue")) {
- m->well_known_type = UPB_WELLKNOWN_BOOLVALUE;
- } else if (!strcmp(name, "google.protobuf.StringValue")) {
- m->well_known_type = UPB_WELLKNOWN_STRINGVALUE;
- } else if (!strcmp(name, "google.protobuf.BytesValue")) {
- m->well_known_type = UPB_WELLKNOWN_BYTESVALUE;
- } else if (!strcmp(name, "google.protobuf.Value")) {
- m->well_known_type = UPB_WELLKNOWN_VALUE;
- } else if (!strcmp(name, "google.protobuf.ListValue")) {
- m->well_known_type = UPB_WELLKNOWN_LISTVALUE;
- } else if (!strcmp(name, "google.protobuf.Struct")) {
- m->well_known_type = UPB_WELLKNOWN_STRUCT;
- } else {
- m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED;
- }
- }
- bool _upb_def_validate(upb_def *const*defs, size_t n, upb_status *s) {
- size_t i;
- /* First perform validation, in two passes so we can check that we have a
- * transitive closure without needing to search. */
- for (i = 0; i < n; i++) {
- upb_def *def = defs[i];
- if (upb_def_isfrozen(def)) {
- /* Could relax this requirement if it's annoying. */
- upb_status_seterrmsg(s, "def is already frozen");
- goto err;
- } else if (def->type == UPB_DEF_FIELD) {
- upb_status_seterrmsg(s, "standalone fielddefs can not be frozen");
- goto err;
- } else {
- /* Set now to detect transitive closure in the second pass. */
- def->came_from_user = true;
- if (def->type == UPB_DEF_ENUM &&
- !upb_validate_enumdef(upb_dyncast_enumdef(def), s)) {
- goto err;
- }
- }
- }
- /* Second pass of validation. Also assign selector bases and indexes, and
- * compact tables. */
- for (i = 0; i < n; i++) {
- upb_def *def = defs[i];
- upb_msgdef *m = upb_dyncast_msgdef_mutable(def);
- upb_enumdef *e = upb_dyncast_enumdef_mutable(def);
- if (m) {
- upb_inttable_compact(&m->itof);
- if (!assign_msg_indices(m, s)) {
- goto err;
- }
- assign_msg_wellknowntype(m);
- /* m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED; */
- } else if (e) {
- upb_inttable_compact(&e->iton);
- }
- }
- return true;
- err:
- for (i = 0; i < n; i++) {
- upb_def *def = defs[i];
- def->came_from_user = false;
- }
- UPB_ASSERT(!(s && upb_ok(s)));
- return false;
- }
- bool upb_def_freeze(upb_def *const* defs, size_t n, upb_status *s) {
- /* Def graph contains FieldDefs between each MessageDef, so double the
- * limit. */
- const size_t maxdepth = UPB_MAX_MESSAGE_DEPTH * 2;
- if (!_upb_def_validate(defs, n, s)) {
- return false;
- }
- /* Validation all passed; freeze the objects. */
- return upb_refcounted_freeze((upb_refcounted *const*)defs, n, s, maxdepth);
- }
- /* upb_enumdef ****************************************************************/
- static void visitenum(const upb_refcounted *r, upb_refcounted_visit *visit,
- void *closure) {
- const upb_enumdef *e = (const upb_enumdef*)r;
- const upb_def *def = upb_enumdef_upcast(e);
- if (upb_def_file(def)) {
- visit(r, upb_filedef_upcast(upb_def_file(def)), closure);
- }
- }
- static void freeenum(upb_refcounted *r) {
- upb_enumdef *e = (upb_enumdef*)r;
- upb_inttable_iter i;
- upb_inttable_begin(&i, &e->iton);
- for( ; !upb_inttable_done(&i); upb_inttable_next(&i)) {
- /* To clean up the upb_gstrdup() from upb_enumdef_addval(). */
- upb_gfree(upb_value_getcstr(upb_inttable_iter_value(&i)));
- }
- upb_strtable_uninit(&e->ntoi);
- upb_inttable_uninit(&e->iton);
- upb_def_uninit(upb_enumdef_upcast_mutable(e));
- upb_gfree(e);
- }
- const struct upb_refcounted_vtbl upb_enumdef_vtbl = {&visitenum, &freeenum};
- upb_enumdef *upb_enumdef_new(const void *owner) {
- upb_enumdef *e = upb_gmalloc(sizeof(*e));
- if (!e) return NULL;
- if (!upb_def_init(upb_enumdef_upcast_mutable(e), UPB_DEF_ENUM,
- &upb_enumdef_vtbl, owner)) {
- goto err2;
- }
- if (!upb_strtable_init(&e->ntoi, UPB_CTYPE_INT32)) goto err2;
- if (!upb_inttable_init(&e->iton, UPB_CTYPE_CSTR)) goto err1;
- return e;
- err1:
- upb_strtable_uninit(&e->ntoi);
- err2:
- upb_gfree(e);
- return NULL;
- }
- bool upb_enumdef_freeze(upb_enumdef *e, upb_status *status) {
- upb_def *d = upb_enumdef_upcast_mutable(e);
- return upb_def_freeze(&d, 1, status);
- }
- const char *upb_enumdef_fullname(const upb_enumdef *e) {
- return upb_def_fullname(upb_enumdef_upcast(e));
- }
- const char *upb_enumdef_name(const upb_enumdef *e) {
- return upb_def_name(upb_enumdef_upcast(e));
- }
- bool upb_enumdef_setfullname(upb_enumdef *e, const char *fullname,
- upb_status *s) {
- return upb_def_setfullname(upb_enumdef_upcast_mutable(e), fullname, s);
- }
- bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num,
- upb_status *status) {
- char *name2;
- if (!upb_isident(name, strlen(name), false, status)) {
- return false;
- }
- if (upb_enumdef_ntoiz(e, name, NULL)) {
- upb_status_seterrf(status, "name '%s' is already defined", name);
- return false;
- }
- if (!upb_strtable_insert(&e->ntoi, name, upb_value_int32(num))) {
- upb_status_seterrmsg(status, "out of memory");
- return false;
- }
- if (!upb_inttable_lookup(&e->iton, num, NULL)) {
- name2 = upb_gstrdup(name);
- if (!name2 || !upb_inttable_insert(&e->iton, num, upb_value_cstr(name2))) {
- upb_status_seterrmsg(status, "out of memory");
- upb_strtable_remove(&e->ntoi, name, NULL);
- return false;
- }
- }
- if (upb_enumdef_numvals(e) == 1) {
- bool ok = upb_enumdef_setdefault(e, num, NULL);
- UPB_ASSERT(ok);
- }
- return true;
- }
- int32_t upb_enumdef_default(const upb_enumdef *e) {
- UPB_ASSERT(upb_enumdef_iton(e, e->defaultval));
- return e->defaultval;
- }
- bool upb_enumdef_setdefault(upb_enumdef *e, int32_t val, upb_status *s) {
- UPB_ASSERT(!upb_enumdef_isfrozen(e));
- if (!upb_enumdef_iton(e, val)) {
- upb_status_seterrf(s, "number '%d' is not in the enum.", val);
- return false;
- }
- e->defaultval = val;
- return true;
- }
- int upb_enumdef_numvals(const upb_enumdef *e) {
- return upb_strtable_count(&e->ntoi);
- }
- void upb_enum_begin(upb_enum_iter *i, const upb_enumdef *e) {
- /* We iterate over the ntoi table, to account for duplicate numbers. */
- upb_strtable_begin(i, &e->ntoi);
- }
- void upb_enum_next(upb_enum_iter *iter) { upb_strtable_next(iter); }
- bool upb_enum_done(upb_enum_iter *iter) { return upb_strtable_done(iter); }
- bool upb_enumdef_ntoi(const upb_enumdef *def, const char *name,
- size_t len, int32_t *num) {
- upb_value v;
- if (!upb_strtable_lookup2(&def->ntoi, name, len, &v)) {
- return false;
- }
- if (num) *num = upb_value_getint32(v);
- return true;
- }
- const char *upb_enumdef_iton(const upb_enumdef *def, int32_t num) {
- upb_value v;
- return upb_inttable_lookup32(&def->iton, num, &v) ?
- upb_value_getcstr(v) : NULL;
- }
- const char *upb_enum_iter_name(upb_enum_iter *iter) {
- return upb_strtable_iter_key(iter);
- }
- int32_t upb_enum_iter_number(upb_enum_iter *iter) {
- return upb_value_getint32(upb_strtable_iter_value(iter));
- }
- /* upb_fielddef ***************************************************************/
- static void upb_fielddef_init_default(upb_fielddef *f);
- static void upb_fielddef_uninit_default(upb_fielddef *f) {
- if (f->type_is_set_ && f->default_is_string && f->defaultval.bytes)
- freestr(f->defaultval.bytes);
- }
- const char *upb_fielddef_fullname(const upb_fielddef *e) {
- return upb_def_fullname(upb_fielddef_upcast(e));
- }
- static void visitfield(const upb_refcounted *r, upb_refcounted_visit *visit,
- void *closure) {
- const upb_fielddef *f = (const upb_fielddef*)r;
- const upb_def *def = upb_fielddef_upcast(f);
- if (upb_fielddef_containingtype(f)) {
- visit(r, upb_msgdef_upcast2(upb_fielddef_containingtype(f)), closure);
- }
- if (upb_fielddef_containingoneof(f)) {
- visit(r, upb_oneofdef_upcast(upb_fielddef_containingoneof(f)), closure);
- }
- if (upb_fielddef_subdef(f)) {
- visit(r, upb_def_upcast(upb_fielddef_subdef(f)), closure);
- }
- if (upb_def_file(def)) {
- visit(r, upb_filedef_upcast(upb_def_file(def)), closure);
- }
- }
- static void freefield(upb_refcounted *r) {
- upb_fielddef *f = (upb_fielddef*)r;
- upb_fielddef_uninit_default(f);
- if (f->subdef_is_symbolic)
- upb_gfree(f->sub.name);
- if (f->msg_is_symbolic)
- upb_gfree(f->msg.name);
- upb_def_uninit(upb_fielddef_upcast_mutable(f));
- upb_gfree(f);
- }
- static const char *enumdefaultstr(const upb_fielddef *f) {
- const upb_enumdef *e;
- UPB_ASSERT(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM);
- e = upb_fielddef_enumsubdef(f);
- if (f->default_is_string && f->defaultval.bytes) {
- /* Default was explicitly set as a string. */
- str_t *s = f->defaultval.bytes;
- return s->str;
- } else if (e) {
- if (!f->default_is_string) {
- /* Default was explicitly set as an integer; look it up in enumdef. */
- const char *name = upb_enumdef_iton(e, f->defaultval.sint);
- if (name) {
- return name;
- }
- } else {
- /* Default is completely unset; pull enumdef default. */
- if (upb_enumdef_numvals(e) > 0) {
- const char *name = upb_enumdef_iton(e, upb_enumdef_default(e));
- UPB_ASSERT(name);
- return name;
- }
- }
- }
- return NULL;
- }
- static bool enumdefaultint32(const upb_fielddef *f, int32_t *val) {
- const upb_enumdef *e;
- UPB_ASSERT(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM);
- e = upb_fielddef_enumsubdef(f);
- if (!f->default_is_string) {
- /* Default was explicitly set as an integer. */
- *val = f->defaultval.sint;
- return true;
- } else if (e) {
- if (f->defaultval.bytes) {
- /* Default was explicitly set as a str; try to lookup corresponding int. */
- str_t *s = f->defaultval.bytes;
- if (upb_enumdef_ntoiz(e, s->str, val)) {
- return true;
- }
- } else {
- /* Default is unset; try to pull in enumdef default. */
- if (upb_enumdef_numvals(e) > 0) {
- *val = upb_enumdef_default(e);
- return true;
- }
- }
- }
- return false;
- }
- const struct upb_refcounted_vtbl upb_fielddef_vtbl = {visitfield, freefield};
- upb_fielddef *upb_fielddef_new(const void *o) {
- upb_fielddef *f = upb_gmalloc(sizeof(*f));
- if (!f) return NULL;
- if (!upb_def_init(upb_fielddef_upcast_mutable(f), UPB_DEF_FIELD,
- &upb_fielddef_vtbl, o)) {
- upb_gfree(f);
- return NULL;
- }
- f->msg.def = NULL;
- f->sub.def = NULL;
- f->oneof = NULL;
- f->subdef_is_symbolic = false;
- f->msg_is_symbolic = false;
- f->label_ = UPB_LABEL_OPTIONAL;
- f->type_ = UPB_TYPE_INT32;
- f->number_ = 0;
- f->type_is_set_ = false;
- f->tagdelim = false;
- f->is_extension_ = false;
- f->lazy_ = false;
- f->packed_ = true;
- /* For the moment we default this to UPB_INTFMT_VARIABLE, since it will work
- * with all integer types and is in some since more "default" since the most
- * normal-looking proto2 types int32/int64/uint32/uint64 use variable.
- *
- * Other options to consider:
- * - there is no default; users must set this manually (like type).
- * - default signed integers to UPB_INTFMT_ZIGZAG, since it's more likely to
- * be an optimal default for signed integers. */
- f->intfmt = UPB_INTFMT_VARIABLE;
- return f;
- }
- bool upb_fielddef_typeisset(const upb_fielddef *f) {
- return f->type_is_set_;
- }
- upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f) {
- UPB_ASSERT(f->type_is_set_);
- return f->type_;
- }
- uint32_t upb_fielddef_index(const upb_fielddef *f) {
- return f->index_;
- }
- upb_label_t upb_fielddef_label(const upb_fielddef *f) {
- return f->label_;
- }
- upb_intfmt_t upb_fielddef_intfmt(const upb_fielddef *f) {
- return f->intfmt;
- }
- bool upb_fielddef_istagdelim(const upb_fielddef *f) {
- return f->tagdelim;
- }
- uint32_t upb_fielddef_number(const upb_fielddef *f) {
- return f->number_;
- }
- bool upb_fielddef_isextension(const upb_fielddef *f) {
- return f->is_extension_;
- }
- bool upb_fielddef_lazy(const upb_fielddef *f) {
- return f->lazy_;
- }
- bool upb_fielddef_packed(const upb_fielddef *f) {
- return f->packed_;
- }
- const char *upb_fielddef_name(const upb_fielddef *f) {
- return upb_def_fullname(upb_fielddef_upcast(f));
- }
- size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len) {
- const char *name = upb_fielddef_name(f);
- size_t src, dst = 0;
- bool ucase_next = false;
- #define WRITE(byte) \
- ++dst; \
- if (dst < len) buf[dst - 1] = byte; \
- else if (dst == len) buf[dst - 1] = '\0'
- if (!name) {
- WRITE('\0');
- return 0;
- }
- /* Implement the transformation as described in the spec:
- * 1. upper case all letters after an underscore.
- * 2. remove all underscores.
- */
- for (src = 0; name[src]; src++) {
- if (name[src] == '_') {
- ucase_next = true;
- continue;
- }
- if (ucase_next) {
- WRITE(toupper(name[src]));
- ucase_next = false;
- } else {
- WRITE(name[src]);
- }
- }
- WRITE('\0');
- return dst;
- #undef WRITE
- }
- const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f) {
- return f->msg_is_symbolic ? NULL : f->msg.def;
- }
- const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f) {
- return f->oneof;
- }
- upb_msgdef *upb_fielddef_containingtype_mutable(upb_fielddef *f) {
- return (upb_msgdef*)upb_fielddef_containingtype(f);
- }
- const char *upb_fielddef_containingtypename(upb_fielddef *f) {
- return f->msg_is_symbolic ? f->msg.name : NULL;
- }
- static void release_containingtype(upb_fielddef *f) {
- if (f->msg_is_symbolic) upb_gfree(f->msg.name);
- }
- bool upb_fielddef_setcontainingtypename(upb_fielddef *f, const char *name,
- upb_status *s) {
- char *name_copy;
- UPB_ASSERT(!upb_fielddef_isfrozen(f));
- if (upb_fielddef_containingtype(f)) {
- upb_status_seterrmsg(s, "field has already been added to a message.");
- return false;
- }
- /* TODO: validate name (upb_isident() doesn't quite work atm because this name
- * may have a leading "."). */
- name_copy = upb_gstrdup(name);
- if (!name_copy) {
- upb_upberr_setoom(s);
- return false;
- }
- release_containingtype(f);
- f->msg.name = name_copy;
- f->msg_is_symbolic = true;
- return true;
- }
- bool upb_fielddef_setname(upb_fielddef *f, const char *name, upb_status *s) {
- if (upb_fielddef_containingtype(f) || upb_fielddef_containingoneof(f)) {
- upb_status_seterrmsg(s, "Already added to message or oneof");
- return false;
- }
- return upb_def_setfullname(upb_fielddef_upcast_mutable(f), name, s);
- }
- static void chkdefaulttype(const upb_fielddef *f, upb_fieldtype_t type) {
- UPB_UNUSED(f);
- UPB_UNUSED(type);
- UPB_ASSERT(f->type_is_set_ && upb_fielddef_type(f) == type);
- }
- int64_t upb_fielddef_defaultint64(const upb_fielddef *f) {
- chkdefaulttype(f, UPB_TYPE_INT64);
- return f->defaultval.sint;
- }
- int32_t upb_fielddef_defaultint32(const upb_fielddef *f) {
- if (f->type_is_set_ && upb_fielddef_type(f) == UPB_TYPE_ENUM) {
- int32_t val;
- bool ok = enumdefaultint32(f, &val);
- UPB_ASSERT(ok);
- return val;
- } else {
- chkdefaulttype(f, UPB_TYPE_INT32);
- return f->defaultval.sint;
- }
- }
- uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f) {
- chkdefaulttype(f, UPB_TYPE_UINT64);
- return f->defaultval.uint;
- }
- uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f) {
- chkdefaulttype(f, UPB_TYPE_UINT32);
- return f->defaultval.uint;
- }
- bool upb_fielddef_defaultbool(const upb_fielddef *f) {
- chkdefaulttype(f, UPB_TYPE_BOOL);
- return f->defaultval.uint;
- }
- float upb_fielddef_defaultfloat(const upb_fielddef *f) {
- chkdefaulttype(f, UPB_TYPE_FLOAT);
- return f->defaultval.flt;
- }
- double upb_fielddef_defaultdouble(const upb_fielddef *f) {
- chkdefaulttype(f, UPB_TYPE_DOUBLE);
- return f->defaultval.dbl;
- }
- const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len) {
- UPB_ASSERT(f->type_is_set_);
- UPB_ASSERT(upb_fielddef_type(f) == UPB_TYPE_STRING ||
- upb_fielddef_type(f) == UPB_TYPE_BYTES ||
- upb_fielddef_type(f) == UPB_TYPE_ENUM);
- if (upb_fielddef_type(f) == UPB_TYPE_ENUM) {
- const char *ret = enumdefaultstr(f);
- UPB_ASSERT(ret);
- /* Enum defaults can't have embedded NULLs. */
- if (len) *len = strlen(ret);
- return ret;
- }
- if (f->default_is_string) {
- str_t *str = f->defaultval.bytes;
- if (len) *len = str->len;
- return str->str;
- }
- return NULL;
- }
- static void upb_fielddef_init_default(upb_fielddef *f) {
- f->default_is_string = false;
- switch (upb_fielddef_type(f)) {
- case UPB_TYPE_DOUBLE: f->defaultval.dbl = 0; break;
- case UPB_TYPE_FLOAT: f->defaultval.flt = 0; break;
- case UPB_TYPE_INT32:
- case UPB_TYPE_INT64: f->defaultval.sint = 0; break;
- case UPB_TYPE_UINT64:
- case UPB_TYPE_UINT32:
- case UPB_TYPE_BOOL: f->defaultval.uint = 0; break;
- case UPB_TYPE_STRING:
- case UPB_TYPE_BYTES:
- f->defaultval.bytes = newstr("", 0);
- f->default_is_string = true;
- break;
- case UPB_TYPE_MESSAGE: break;
- case UPB_TYPE_ENUM:
- /* This is our special sentinel that indicates "not set" for an enum. */
- f->default_is_string = true;
- f->defaultval.bytes = NULL;
- break;
- }
- }
- const upb_def *upb_fielddef_subdef(const upb_fielddef *f) {
- return f->subdef_is_symbolic ? NULL : f->sub.def;
- }
- const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f) {
- const upb_def *def = upb_fielddef_subdef(f);
- return def ? upb_dyncast_msgdef(def) : NULL;
- }
- const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f) {
- const upb_def *def = upb_fielddef_subdef(f);
- return def ? upb_dyncast_enumdef(def) : NULL;
- }
- upb_def *upb_fielddef_subdef_mutable(upb_fielddef *f) {
- return (upb_def*)upb_fielddef_subdef(f);
- }
- const char *upb_fielddef_subdefname(const upb_fielddef *f) {
- if (f->subdef_is_symbolic) {
- return f->sub.name;
- } else if (f->sub.def) {
- return upb_def_fullname(f->sub.def);
- } else {
- return NULL;
- }
- }
- bool upb_fielddef_setnumber(upb_fielddef *f, uint32_t number, upb_status *s) {
- if (upb_fielddef_containingtype(f)) {
- upb_status_seterrmsg(
- s, "cannot change field number after adding to a message");
- return false;
- }
- if (number == 0) {
- upb_status_seterrf(s, "invalid field number (%u)", number);
- return false;
- }
- f->number_ = number;
- return true;
- }
- void upb_fielddef_settype(upb_fielddef *f, upb_fieldtype_t type) {
- UPB_ASSERT(!upb_fielddef_isfrozen(f));
- UPB_ASSERT(upb_fielddef_checktype(type));
- upb_fielddef_uninit_default(f);
- f->type_ = type;
- f->type_is_set_ = true;
- upb_fielddef_init_default(f);
- }
- void upb_fielddef_setdescriptortype(upb_fielddef *f, int type) {
- UPB_ASSERT(!upb_fielddef_isfrozen(f));
- switch (type) {
- case UPB_DESCRIPTOR_TYPE_DOUBLE:
- upb_fielddef_settype(f, UPB_TYPE_DOUBLE);
- break;
- case UPB_DESCRIPTOR_TYPE_FLOAT:
- upb_fielddef_settype(f, UPB_TYPE_FLOAT);
- break;
- case UPB_DESCRIPTOR_TYPE_INT64:
- case UPB_DESCRIPTOR_TYPE_SFIXED64:
- case UPB_DESCRIPTOR_TYPE_SINT64:
- upb_fielddef_settype(f, UPB_TYPE_INT64);
- break;
- case UPB_DESCRIPTOR_TYPE_UINT64:
- case UPB_DESCRIPTOR_TYPE_FIXED64:
- upb_fielddef_settype(f, UPB_TYPE_UINT64);
- break;
- case UPB_DESCRIPTOR_TYPE_INT32:
- case UPB_DESCRIPTOR_TYPE_SFIXED32:
- case UPB_DESCRIPTOR_TYPE_SINT32:
- upb_fielddef_settype(f, UPB_TYPE_INT32);
- break;
- case UPB_DESCRIPTOR_TYPE_UINT32:
- case UPB_DESCRIPTOR_TYPE_FIXED32:
- upb_fielddef_settype(f, UPB_TYPE_UINT32);
- break;
- case UPB_DESCRIPTOR_TYPE_BOOL:
- upb_fielddef_settype(f, UPB_TYPE_BOOL);
- break;
- case UPB_DESCRIPTOR_TYPE_STRING:
- upb_fielddef_settype(f, UPB_TYPE_STRING);
- break;
- case UPB_DESCRIPTOR_TYPE_BYTES:
- upb_fielddef_settype(f, UPB_TYPE_BYTES);
- break;
- case UPB_DESCRIPTOR_TYPE_GROUP:
- case UPB_DESCRIPTOR_TYPE_MESSAGE:
- upb_fielddef_settype(f, UPB_TYPE_MESSAGE);
- break;
- case UPB_DESCRIPTOR_TYPE_ENUM:
- upb_fielddef_settype(f, UPB_TYPE_ENUM);
- break;
- default: UPB_ASSERT(false);
- }
- if (type == UPB_DESCRIPTOR_TYPE_FIXED64 ||
- type == UPB_DESCRIPTOR_TYPE_FIXED32 ||
- type == UPB_DESCRIPTOR_TYPE_SFIXED64 ||
- type == UPB_DESCRIPTOR_TYPE_SFIXED32) {
- upb_fielddef_setintfmt(f, UPB_INTFMT_FIXED);
- } else if (type == UPB_DESCRIPTOR_TYPE_SINT64 ||
- type == UPB_DESCRIPTOR_TYPE_SINT32) {
- upb_fielddef_setintfmt(f, UPB_INTFMT_ZIGZAG);
- } else {
- upb_fielddef_setintfmt(f, UPB_INTFMT_VARIABLE);
- }
- upb_fielddef_settagdelim(f, type == UPB_DESCRIPTOR_TYPE_GROUP);
- }
- upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f) {
- switch (upb_fielddef_type(f)) {
- case UPB_TYPE_FLOAT: return UPB_DESCRIPTOR_TYPE_FLOAT;
- case UPB_TYPE_DOUBLE: return UPB_DESCRIPTOR_TYPE_DOUBLE;
- case UPB_TYPE_BOOL: return UPB_DESCRIPTOR_TYPE_BOOL;
- case UPB_TYPE_STRING: return UPB_DESCRIPTOR_TYPE_STRING;
- case UPB_TYPE_BYTES: return UPB_DESCRIPTOR_TYPE_BYTES;
- case UPB_TYPE_ENUM: return UPB_DESCRIPTOR_TYPE_ENUM;
- case UPB_TYPE_INT32:
- switch (upb_fielddef_intfmt(f)) {
- case UPB_INTFMT_VARIABLE: return UPB_DESCRIPTOR_TYPE_INT32;
- case UPB_INTFMT_FIXED: return UPB_DESCRIPTOR_TYPE_SFIXED32;
- case UPB_INTFMT_ZIGZAG: return UPB_DESCRIPTOR_TYPE_SINT32;
- }
- case UPB_TYPE_INT64:
- switch (upb_fielddef_intfmt(f)) {
- case UPB_INTFMT_VARIABLE: return UPB_DESCRIPTOR_TYPE_INT64;
- case UPB_INTFMT_FIXED: return UPB_DESCRIPTOR_TYPE_SFIXED64;
- case UPB_INTFMT_ZIGZAG: return UPB_DESCRIPTOR_TYPE_SINT64;
- }
- case UPB_TYPE_UINT32:
- switch (upb_fielddef_intfmt(f)) {
- case UPB_INTFMT_VARIABLE: return UPB_DESCRIPTOR_TYPE_UINT32;
- case UPB_INTFMT_FIXED: return UPB_DESCRIPTOR_TYPE_FIXED32;
- case UPB_INTFMT_ZIGZAG: return -1;
- }
- case UPB_TYPE_UINT64:
- switch (upb_fielddef_intfmt(f)) {
- case UPB_INTFMT_VARIABLE: return UPB_DESCRIPTOR_TYPE_UINT64;
- case UPB_INTFMT_FIXED: return UPB_DESCRIPTOR_TYPE_FIXED64;
- case UPB_INTFMT_ZIGZAG: return -1;
- }
- case UPB_TYPE_MESSAGE:
- return upb_fielddef_istagdelim(f) ?
- UPB_DESCRIPTOR_TYPE_GROUP : UPB_DESCRIPTOR_TYPE_MESSAGE;
- }
- return 0;
- }
- void upb_fielddef_setisextension(upb_fielddef *f, bool is_extension) {
- UPB_ASSERT(!upb_fielddef_isfrozen(f));
- f->is_extension_ = is_extension;
- }
- void upb_fielddef_setlazy(upb_fielddef *f, bool lazy) {
- UPB_ASSERT(!upb_fielddef_isfrozen(f));
- f->lazy_ = lazy;
- }
- void upb_fielddef_setpacked(upb_fielddef *f, bool packed) {
- UPB_ASSERT(!upb_fielddef_isfrozen(f));
- f->packed_ = packed;
- }
- void upb_fielddef_setlabel(upb_fielddef *f, upb_label_t label) {
- UPB_ASSERT(!upb_fielddef_isfrozen(f));
- UPB_ASSERT(upb_fielddef_checklabel(label));
- f->label_ = label;
- }
- void upb_fielddef_setintfmt(upb_fielddef *f, upb_intfmt_t fmt) {
- UPB_ASSERT(!upb_fielddef_isfrozen(f));
- UPB_ASSERT(upb_fielddef_checkintfmt(fmt));
- f->intfmt = fmt;
- }
- void upb_fielddef_settagdelim(upb_fielddef *f, bool tag_delim) {
- UPB_ASSERT(!upb_fielddef_isfrozen(f));
- f->tagdelim = tag_delim;
- f->tagdelim = tag_delim;
- }
- static bool checksetdefault(upb_fielddef *f, upb_fieldtype_t type) {
- if (!f->type_is_set_ || upb_fielddef_isfrozen(f) ||
- upb_fielddef_type(f) != type) {
- UPB_ASSERT(false);
- return false;
- }
- if (f->default_is_string) {
- str_t *s = f->defaultval.bytes;
- UPB_ASSERT(s || type == UPB_TYPE_ENUM);
- if (s) freestr(s);
- }
- f->default_is_string = false;
- return true;
- }
- void upb_fielddef_setdefaultint64(upb_fielddef *f, int64_t value) {
- if (checksetdefault(f, UPB_TYPE_INT64))
- f->defaultval.sint = value;
- }
- void upb_fielddef_setdefaultint32(upb_fielddef *f, int32_t value) {
- if ((upb_fielddef_type(f) == UPB_TYPE_ENUM &&
- checksetdefault(f, UPB_TYPE_ENUM)) ||
- checksetdefault(f, UPB_TYPE_INT32)) {
- f->defaultval.sint = value;
- }
- }
- void upb_fielddef_setdefaultuint64(upb_fielddef *f, uint64_t value) {
- if (checksetdefault(f, UPB_TYPE_UINT64))
- f->defaultval.uint = value;
- }
- void upb_fielddef_setdefaultuint32(upb_fielddef *f, uint32_t value) {
- if (checksetdefault(f, UPB_TYPE_UINT32))
- f->defaultval.uint = value;
- }
- void upb_fielddef_setdefaultbool(upb_fielddef *f, bool value) {
- if (checksetdefault(f, UPB_TYPE_BOOL))
- f->defaultval.uint = value;
- }
- void upb_fielddef_setdefaultfloat(upb_fielddef *f, float value) {
- if (checksetdefault(f, UPB_TYPE_FLOAT))
- f->defaultval.flt = value;
- }
- void upb_fielddef_setdefaultdouble(upb_fielddef *f, double value) {
- if (checksetdefault(f, UPB_TYPE_DOUBLE))
- f->defaultval.dbl = value;
- }
- bool upb_fielddef_setdefaultstr(upb_fielddef *f, const void *str, size_t len,
- upb_status *s) {
- str_t *str2;
- UPB_ASSERT(upb_fielddef_isstring(f) || f->type_ == UPB_TYPE_ENUM);
- if (f->type_ == UPB_TYPE_ENUM && !upb_isident(str, len, false, s))
- return false;
- if (f->default_is_string) {
- str_t *s = f->defaultval.bytes;
- UPB_ASSERT(s || f->type_ == UPB_TYPE_ENUM);
- if (s) freestr(s);
- } else {
- UPB_ASSERT(f->type_ == UPB_TYPE_ENUM);
- }
- str2 = newstr(str, len);
- f->defaultval.bytes = str2;
- f->default_is_string = true;
- return true;
- }
- void upb_fielddef_setdefaultcstr(upb_fielddef *f, const char *str,
- upb_status *s) {
- UPB_ASSERT(f->type_is_set_);
- upb_fielddef_setdefaultstr(f, str, str ? strlen(str) : 0, s);
- }
- bool upb_fielddef_enumhasdefaultint32(const upb_fielddef *f) {
- int32_t val;
- UPB_ASSERT(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM);
- return enumdefaultint32(f, &val);
- }
- bool upb_fielddef_enumhasdefaultstr(const upb_fielddef *f) {
- UPB_ASSERT(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM);
- return enumdefaultstr(f) != NULL;
- }
- static bool upb_subdef_typecheck(upb_fielddef *f, const upb_def *subdef,
- upb_status *s) {
- if (f->type_ == UPB_TYPE_MESSAGE) {
- if (upb_dyncast_msgdef(subdef)) return true;
- upb_status_seterrmsg(s, "invalid subdef type for this submessage field");
- return false;
- } else if (f->type_ == UPB_TYPE_ENUM) {
- if (upb_dyncast_enumdef(subdef)) return true;
- upb_status_seterrmsg(s, "invalid subdef type for this enum field");
- return false;
- } else {
- upb_status_seterrmsg(s, "only message and enum fields can have a subdef");
- return false;
- }
- }
- static void release_subdef(upb_fielddef *f) {
- if (f->subdef_is_symbolic) {
- upb_gfree(f->sub.name);
- } else if (f->sub.def) {
- upb_unref2(f->sub.def, f);
- }
- }
- bool upb_fielddef_setsubdef(upb_fielddef *f, const upb_def *subdef,
- upb_status *s) {
- UPB_ASSERT(!upb_fielddef_isfrozen(f));
- UPB_ASSERT(upb_fielddef_hassubdef(f));
- if (subdef && !upb_subdef_typecheck(f, subdef, s)) return false;
- release_subdef(f);
- f->sub.def = subdef;
- f->subdef_is_symbolic = false;
- if (f->sub.def) upb_ref2(f->sub.def, f);
- return true;
- }
- bool upb_fielddef_setmsgsubdef(upb_fielddef *f, const upb_msgdef *subdef,
- upb_status *s) {
- return upb_fielddef_setsubdef(f, upb_msgdef_upcast(subdef), s);
- }
- bool upb_fielddef_setenumsubdef(upb_fielddef *f, const upb_enumdef *subdef,
- upb_status *s) {
- return upb_fielddef_setsubdef(f, upb_enumdef_upcast(subdef), s);
- }
- bool upb_fielddef_setsubdefname(upb_fielddef *f, const char *name,
- upb_status *s) {
- char *name_copy;
- UPB_ASSERT(!upb_fielddef_isfrozen(f));
- if (!upb_fielddef_hassubdef(f)) {
- upb_status_seterrmsg(s, "field type does not accept a subdef");
- return false;
- }
- name_copy = upb_gstrdup(name);
- if (!name_copy) {
- upb_upberr_setoom(s);
- return false;
- }
- /* TODO: validate name (upb_isident() doesn't quite work atm because this name
- * may have a leading "."). */
- release_subdef(f);
- f->sub.name = name_copy;
- f->subdef_is_symbolic = true;
- return true;
- }
- bool upb_fielddef_issubmsg(const upb_fielddef *f) {
- return upb_fielddef_type(f) == UPB_TYPE_MESSAGE;
- }
- bool upb_fielddef_isstring(const upb_fielddef *f) {
- return upb_fielddef_type(f) == UPB_TYPE_STRING ||
- upb_fielddef_type(f) == UPB_TYPE_BYTES;
- }
- bool upb_fielddef_isseq(const upb_fielddef *f) {
- return upb_fielddef_label(f) == UPB_LABEL_REPEATED;
- }
- bool upb_fielddef_isprimitive(const upb_fielddef *f) {
- return !upb_fielddef_isstring(f) && !upb_fielddef_issubmsg(f);
- }
- bool upb_fielddef_ismap(const upb_fielddef *f) {
- return upb_fielddef_isseq(f) && upb_fielddef_issubmsg(f) &&
- upb_msgdef_mapentry(upb_fielddef_msgsubdef(f));
- }
- bool upb_fielddef_haspresence(const upb_fielddef *f) {
- if (upb_fielddef_isseq(f)) return false;
- if (upb_fielddef_issubmsg(f)) return true;
- /* Primitive field: return true unless there is a message that specifies
- * presence should not exist. */
- if (f->msg_is_symbolic || !f->msg.def) return true;
- return f->msg.def->syntax == UPB_SYNTAX_PROTO2;
- }
- bool upb_fielddef_hassubdef(const upb_fielddef *f) {
- return upb_fielddef_issubmsg(f) || upb_fielddef_type(f) == UPB_TYPE_ENUM;
- }
- static bool between(int32_t x, int32_t low, int32_t high) {
- return x >= low && x <= high;
- }
- bool upb_fielddef_checklabel(int32_t label) { return between(label, 1, 3); }
- bool upb_fielddef_checktype(int32_t type) { return between(type, 1, 11); }
- bool upb_fielddef_checkintfmt(int32_t fmt) { return between(fmt, 1, 3); }
- bool upb_fielddef_checkdescriptortype(int32_t type) {
- return between(type, 1, 18);
- }
- /* upb_msgdef *****************************************************************/
- static void visitmsg(const upb_refcounted *r, upb_refcounted_visit *visit,
- void *closure) {
- upb_msg_oneof_iter o;
- const upb_msgdef *m = (const upb_msgdef*)r;
- const upb_def *def = upb_msgdef_upcast(m);
- upb_msg_field_iter i;
- for(upb_msg_field_begin(&i, m);
- !upb_msg_field_done(&i);
- upb_msg_field_next(&i)) {
- upb_fielddef *f = upb_msg_iter_field(&i);
- visit(r, upb_fielddef_upcast2(f), closure);
- }
- for(upb_msg_oneof_begin(&o, m);
- !upb_msg_oneof_done(&o);
- upb_msg_oneof_next(&o)) {
- upb_oneofdef *f = upb_msg_iter_oneof(&o);
- visit(r, upb_oneofdef_upcast(f), closure);
- }
- if (upb_def_file(def)) {
- visit(r, upb_filedef_upcast(upb_def_file(def)), closure);
- }
- }
- static void freemsg(upb_refcounted *r) {
- upb_msgdef *m = (upb_msgdef*)r;
- upb_strtable_uninit(&m->ntof);
- upb_inttable_uninit(&m->itof);
- upb_def_uninit(upb_msgdef_upcast_mutable(m));
- upb_gfree(m);
- }
- const struct upb_refcounted_vtbl upb_msgdef_vtbl = {visitmsg, freemsg};
- upb_msgdef *upb_msgdef_new(const void *owner) {
- upb_msgdef *m = upb_gmalloc(sizeof(*m));
- if (!m) return NULL;
- if (!upb_def_init(upb_msgdef_upcast_mutable(m), UPB_DEF_MSG, &upb_msgdef_vtbl,
- owner)) {
- goto err2;
- }
- if (!upb_inttable_init(&m->itof, UPB_CTYPE_PTR)) goto err2;
- if (!upb_strtable_init(&m->ntof, UPB_CTYPE_PTR)) goto err1;
- m->map_entry = false;
- m->syntax = UPB_SYNTAX_PROTO2;
- return m;
- err1:
- upb_inttable_uninit(&m->itof);
- err2:
- upb_gfree(m);
- return NULL;
- }
- bool upb_msgdef_freeze(upb_msgdef *m, upb_status *status) {
- upb_def *d = upb_msgdef_upcast_mutable(m);
- return upb_def_freeze(&d, 1, status);
- }
- const char *upb_msgdef_fullname(const upb_msgdef *m) {
- return upb_def_fullname(upb_msgdef_upcast(m));
- }
- const char *upb_msgdef_name(const upb_msgdef *m) {
- return upb_def_name(upb_msgdef_upcast(m));
- }
- bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname,
- upb_status *s) {
- return upb_def_setfullname(upb_msgdef_upcast_mutable(m), fullname, s);
- }
- bool upb_msgdef_setsyntax(upb_msgdef *m, upb_syntax_t syntax) {
- if (syntax != UPB_SYNTAX_PROTO2 && syntax != UPB_SYNTAX_PROTO3) {
- return false;
- }
- m->syntax = syntax;
- return true;
- }
- upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m) {
- return m->syntax;
- }
- /* Helper: check that the field |f| is safe to add to msgdef |m|. Set an error
- * on status |s| and return false if not. */
- static bool check_field_add(const upb_msgdef *m, const upb_fielddef *f,
- upb_status *s) {
- if (upb_fielddef_containingtype(f) != NULL) {
- upb_status_seterrmsg(s, "fielddef already belongs to a message");
- return false;
- } else if (upb_fielddef_name(f) == NULL || upb_fielddef_number(f) == 0) {
- upb_status_seterrmsg(s, "field name or number were not set");
- return false;
- } else if (upb_msgdef_itof(m, upb_fielddef_number(f))) {
- upb_status_seterrmsg(s, "duplicate field number");
- return false;
- } else if (upb_strtable_lookup(&m->ntof, upb_fielddef_name(f), NULL)) {
- upb_status_seterrmsg(s, "name conflicts with existing field or oneof");
- return false;
- }
- return true;
- }
- static void add_field(upb_msgdef *m, upb_fielddef *f, const void *ref_donor) {
- release_containingtype(f);
- f->msg.def = m;
- f->msg_is_symbolic = false;
- upb_inttable_insert(&m->itof, upb_fielddef_number(f), upb_value_ptr(f));
- upb_strtable_insert(&m->ntof, upb_fielddef_name(f), upb_value_ptr(f));
- upb_ref2(f, m);
- upb_ref2(m, f);
- if (ref_donor) upb_fielddef_unref(f, ref_donor);
- }
- bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f, const void *ref_donor,
- upb_status *s) {
- /* TODO: extensions need to have a separate namespace, because proto2 allows a
- * top-level extension (ie. one not in any package) to have the same name as a
- * field from the message.
- *
- * This also implies that there needs to be a separate lookup-by-name method
- * for extensions. It seems desirable for iteration to return both extensions
- * and non-extensions though.
- *
- * We also need to validate that the field number is in an extension range iff
- * it is an extension.
- *
- * This method is idempotent. Check if |f| is already part of this msgdef and
- * return immediately if so. */
- if (upb_fielddef_containingtype(f) == m) {
- if (ref_donor) upb_fielddef_unref(f, ref_donor);
- return true;
- }
- /* Check constraints for all fields before performing any action. */
- if (!check_field_add(m, f, s)) {
- return false;
- } else if (upb_fielddef_containingoneof(f) != NULL) {
- /* Fields in a oneof can only be added by adding the oneof to the msgdef. */
- upb_status_seterrmsg(s, "fielddef is part of a oneof");
- return false;
- }
- /* Constraint checks ok, perform the action. */
- add_field(m, f, ref_donor);
- return true;
- }
- bool upb_msgdef_addoneof(upb_msgdef *m, upb_oneofdef *o, const void *ref_donor,
- upb_status *s) {
- upb_oneof_iter it;
- /* Check various conditions that would prevent this oneof from being added. */
- if (upb_oneofdef_containingtype(o)) {
- upb_status_seterrmsg(s, "oneofdef already belongs to a message");
- return false;
- } else if (upb_oneofdef_name(o) == NULL) {
- upb_status_seterrmsg(s, "oneofdef name was not set");
- return false;
- } else if (upb_strtable_lookup(&m->ntof, upb_oneofdef_name(o), NULL)) {
- upb_status_seterrmsg(s, "name conflicts with existing field or oneof");
- return false;
- }
- /* Check that all of the oneof's fields do not conflict with names or numbers
- * of fields already in the message. */
- for (upb_oneof_begin(&it, o); !upb_oneof_done(&it); upb_oneof_next(&it)) {
- const upb_fielddef *f = upb_oneof_iter_field(&it);
- if (!check_field_add(m, f, s)) {
- return false;
- }
- }
- /* Everything checks out -- commit now. */
- /* Add oneof itself first. */
- o->parent = m;
- upb_strtable_insert(&m->ntof, upb_oneofdef_name(o), upb_value_ptr(o));
- upb_ref2(o, m);
- upb_ref2(m, o);
- /* Add each field of the oneof directly to the msgdef. */
- for (upb_oneof_begin(&it, o); !upb_oneof_done(&it); upb_oneof_next(&it)) {
- upb_fielddef *f = upb_oneof_iter_field(&it);
- add_field(m, f, NULL);
- }
- if (ref_donor) upb_oneofdef_unref(o, ref_donor);
- return true;
- }
- const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i) {
- upb_value val;
- return upb_inttable_lookup32(&m->itof, i, &val) ?
- upb_value_getptr(val) : NULL;
- }
- const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name,
- size_t len) {
- upb_value val;
- if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
- return NULL;
- }
- return upb_trygetfield(upb_value_getptr(val));
- }
- const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
- size_t len) {
- upb_value val;
- if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
- return NULL;
- }
- return upb_trygetoneof(upb_value_getptr(val));
- }
- bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len,
- const upb_fielddef **f, const upb_oneofdef **o) {
- upb_value val;
- if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
- return false;
- }
- *o = upb_trygetoneof(upb_value_getptr(val));
- *f = upb_trygetfield(upb_value_getptr(val));
- UPB_ASSERT((*o != NULL) ^ (*f != NULL)); /* Exactly one of the two should be set. */
- return true;
- }
- int upb_msgdef_numfields(const upb_msgdef *m) {
- /* The number table contains only fields. */
- return upb_inttable_count(&m->itof);
- }
- int upb_msgdef_numoneofs(const upb_msgdef *m) {
- /* The name table includes oneofs, and the number table does not. */
- return upb_strtable_count(&m->ntof) - upb_inttable_count(&m->itof);
- }
- void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry) {
- UPB_ASSERT(!upb_msgdef_isfrozen(m));
- m->map_entry = map_entry;
- }
- bool upb_msgdef_mapentry(const upb_msgdef *m) {
- return m->map_entry;
- }
- upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m) {
- return m->well_known_type;
- }
- bool upb_msgdef_isnumberwrapper(const upb_msgdef *m) {
- upb_wellknowntype_t type = upb_msgdef_wellknowntype(m);
- return type >= UPB_WELLKNOWN_DOUBLEVALUE &&
- type <= UPB_WELLKNOWN_UINT32VALUE;
- }
- void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m) {
- upb_inttable_begin(iter, &m->itof);
- }
- void upb_msg_field_next(upb_msg_field_iter *iter) { upb_inttable_next(iter); }
- bool upb_msg_field_done(const upb_msg_field_iter *iter) {
- return upb_inttable_done(iter);
- }
- upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter) {
- return (upb_fielddef*)upb_value_getptr(upb_inttable_iter_value(iter));
- }
- void upb_msg_field_iter_setdone(upb_msg_field_iter *iter) {
- upb_inttable_iter_setdone(iter);
- }
- void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m) {
- upb_strtable_begin(iter, &m->ntof);
- /* We need to skip past any initial fields. */
- while (!upb_strtable_done(iter) &&
- !upb_isoneof(upb_value_getptr(upb_strtable_iter_value(iter)))) {
- upb_strtable_next(iter);
- }
- }
- void upb_msg_oneof_next(upb_msg_oneof_iter *iter) {
- /* We need to skip past fields to return only oneofs. */
- do {
- upb_strtable_next(iter);
- } while (!upb_strtable_done(iter) &&
- !upb_isoneof(upb_value_getptr(upb_strtable_iter_value(iter))));
- }
- bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter) {
- return upb_strtable_done(iter);
- }
- upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter) {
- return (upb_oneofdef*)upb_value_getptr(upb_strtable_iter_value(iter));
- }
- void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter) {
- upb_strtable_iter_setdone(iter);
- }
- /* upb_oneofdef ***************************************************************/
- static void visitoneof(const upb_refcounted *r, upb_refcounted_visit *visit,
- void *closure) {
- const upb_oneofdef *o = (const upb_oneofdef*)r;
- upb_oneof_iter i;
- for (upb_oneof_begin(&i, o); !upb_oneof_done(&i); upb_oneof_next(&i)) {
- const upb_fielddef *f = upb_oneof_iter_field(&i);
- visit(r, upb_fielddef_upcast2(f), closure);
- }
- if (o->parent) {
- visit(r, upb_msgdef_upcast2(o->parent), closure);
- }
- }
- static void freeoneof(upb_refcounted *r) {
- upb_oneofdef *o = (upb_oneofdef*)r;
- upb_strtable_uninit(&o->ntof);
- upb_inttable_uninit(&o->itof);
- upb_gfree((void*)o->name);
- upb_gfree(o);
- }
- const struct upb_refcounted_vtbl upb_oneofdef_vtbl = {visitoneof, freeoneof};
- upb_oneofdef *upb_oneofdef_new(const void *owner) {
- upb_oneofdef *o = upb_gmalloc(sizeof(*o));
- if (!o) {
- return NULL;
- }
- o->parent = NULL;
- o->name = NULL;
- if (!upb_refcounted_init(upb_oneofdef_upcast_mutable(o), &upb_oneofdef_vtbl,
- owner)) {
- goto err2;
- }
- if (!upb_inttable_init(&o->itof, UPB_CTYPE_PTR)) goto err2;
- if (!upb_strtable_init(&o->ntof, UPB_CTYPE_PTR)) goto err1;
- return o;
- err1:
- upb_inttable_uninit(&o->itof);
- err2:
- upb_gfree(o);
- return NULL;
- }
- const char *upb_oneofdef_name(const upb_oneofdef *o) { return o->name; }
- bool upb_oneofdef_setname(upb_oneofdef *o, const char *name, upb_status *s) {
- UPB_ASSERT(!upb_oneofdef_isfrozen(o));
- if (upb_oneofdef_containingtype(o)) {
- upb_status_seterrmsg(s, "oneof already added to a message");
- return false;
- }
- if (!upb_isident(name, strlen(name), true, s)) {
- return false;
- }
- name = upb_gstrdup(name);
- if (!name) {
- upb_status_seterrmsg(s, "One of memory");
- return false;
- }
- upb_gfree((void*)o->name);
- o->name = name;
- return true;
- }
- const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o) {
- return o->parent;
- }
- int upb_oneofdef_numfields(const upb_oneofdef *o) {
- return upb_strtable_count(&o->ntof);
- }
- uint32_t upb_oneofdef_index(const upb_oneofdef *o) {
- return o->index;
- }
- bool upb_oneofdef_addfield(upb_oneofdef *o, upb_fielddef *f,
- const void *ref_donor,
- upb_status *s) {
- UPB_ASSERT(!upb_oneofdef_isfrozen(o));
- UPB_ASSERT(!o->parent || !upb_msgdef_isfrozen(o->parent));
- /* This method is idempotent. Check if |f| is already part of this oneofdef
- * and return immediately if so. */
- if (upb_fielddef_containingoneof(f) == o) {
- return true;
- }
- /* The field must have an OPTIONAL label. */
- if (upb_fielddef_label(f) != UPB_LABEL_OPTIONAL) {
- upb_status_seterrmsg(s, "fields in oneof must have OPTIONAL label");
- return false;
- }
- /* Check that no field with this name or number exists already in the oneof.
- * Also check that the field is not already part of a oneof. */
- if (upb_fielddef_name(f) == NULL || upb_fielddef_number(f) == 0) {
- upb_status_seterrmsg(s, "field name or number were not set");
- return false;
- } else if (upb_oneofdef_itof(o, upb_fielddef_number(f)) ||
- upb_oneofdef_ntofz(o, upb_fielddef_name(f))) {
- upb_status_seterrmsg(s, "duplicate field name or number");
- return false;
- } else if (upb_fielddef_containingoneof(f) != NULL) {
- upb_status_seterrmsg(s, "fielddef already belongs to a oneof");
- return false;
- }
- /* We allow adding a field to the oneof either if the field is not part of a
- * msgdef, or if it is and we are also part of the same msgdef. */
- if (o->parent == NULL) {
- /* If we're not in a msgdef, the field cannot be either. Otherwise we would
- * need to magically add this oneof to a msgdef to remain consistent, which
- * is surprising behavior. */
- if (upb_fielddef_containingtype(f) != NULL) {
- upb_status_seterrmsg(s, "fielddef already belongs to a message, but "
- "oneof does not");
- return false;
- }
- } else {
- /* If we're in a msgdef, the user can add fields that either aren't in any
- * msgdef (in which case they're added to our msgdef) or already a part of
- * our msgdef. */
- if (upb_fielddef_containingtype(f) != NULL &&
- upb_fielddef_containingtype(f) != o->parent) {
- upb_status_seterrmsg(s, "fielddef belongs to a different message "
- "than oneof");
- return false;
- }
- }
- /* Commit phase. First add the field to our parent msgdef, if any, because
- * that may fail; then add the field to our own tables. */
- if (o->parent != NULL && upb_fielddef_containingtype(f) == NULL) {
- if (!upb_msgdef_addfield((upb_msgdef*)o->parent, f, NULL, s)) {
- return false;
- }
- }
- release_containingtype(f);
- f->oneof = o;
- upb_inttable_insert(&o->itof, upb_fielddef_number(f), upb_value_ptr(f));
- upb_strtable_insert(&o->ntof, upb_fielddef_name(f), upb_value_ptr(f));
- upb_ref2(f, o);
- upb_ref2(o, f);
- if (ref_donor) upb_fielddef_unref(f, ref_donor);
- return true;
- }
- const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o,
- const char *name, size_t length) {
- upb_value val;
- return upb_strtable_lookup2(&o->ntof, name, length, &val) ?
- upb_value_getptr(val) : NULL;
- }
- const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num) {
- upb_value val;
- return upb_inttable_lookup32(&o->itof, num, &val) ?
- upb_value_getptr(val) : NULL;
- }
- void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o) {
- upb_inttable_begin(iter, &o->itof);
- }
- void upb_oneof_next(upb_oneof_iter *iter) {
- upb_inttable_next(iter);
- }
- bool upb_oneof_done(upb_oneof_iter *iter) {
- return upb_inttable_done(iter);
- }
- upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter) {
- return (upb_fielddef*)upb_value_getptr(upb_inttable_iter_value(iter));
- }
- void upb_oneof_iter_setdone(upb_oneof_iter *iter) {
- upb_inttable_iter_setdone(iter);
- }
- /* upb_filedef ****************************************************************/
- static void visitfiledef(const upb_refcounted *r, upb_refcounted_visit *visit,
- void *closure) {
- const upb_filedef *f = (const upb_filedef*)r;
- size_t i;
- for(i = 0; i < upb_filedef_defcount(f); i++) {
- visit(r, upb_def_upcast(upb_filedef_def(f, i)), closure);
- }
- }
- static void freefiledef(upb_refcounted *r) {
- upb_filedef *f = (upb_filedef*)r;
- size_t i;
- for(i = 0; i < upb_filedef_depcount(f); i++) {
- upb_filedef_unref(upb_filedef_dep(f, i), f);
- }
- upb_inttable_uninit(&f->defs);
- upb_inttable_uninit(&f->deps);
- upb_gfree((void*)f->name);
- upb_gfree((void*)f->package);
- upb_gfree((void*)f->phpprefix);
- upb_gfree((void*)f->phpnamespace);
- upb_gfree(f);
- }
- const struct upb_refcounted_vtbl upb_filedef_vtbl = {visitfiledef, freefiledef};
- upb_filedef *upb_filedef_new(const void *owner) {
- upb_filedef *f = upb_gmalloc(sizeof(*f));
- if (!f) {
- return NULL;
- }
- f->package = NULL;
- f->name = NULL;
- f->phpprefix = NULL;
- f->phpnamespace = NULL;
- f->syntax = UPB_SYNTAX_PROTO2;
- if (!upb_refcounted_init(upb_filedef_upcast_mutable(f), &upb_filedef_vtbl,
- owner)) {
- goto err;
- }
- if (!upb_inttable_init(&f->defs, UPB_CTYPE_CONSTPTR)) {
- goto err;
- }
- if (!upb_inttable_init(&f->deps, UPB_CTYPE_CONSTPTR)) {
- goto err2;
- }
- return f;
- err2:
- upb_inttable_uninit(&f->defs);
- err:
- upb_gfree(f);
- return NULL;
- }
- const char *upb_filedef_name(const upb_filedef *f) {
- return f->name;
- }
- const char *upb_filedef_package(const upb_filedef *f) {
- return f->package;
- }
- const char *upb_filedef_phpprefix(const upb_filedef *f) {
- return f->phpprefix;
- }
- const char *upb_filedef_phpnamespace(const upb_filedef *f) {
- return f->phpnamespace;
- }
- upb_syntax_t upb_filedef_syntax(const upb_filedef *f) {
- return f->syntax;
- }
- size_t upb_filedef_defcount(const upb_filedef *f) {
- return upb_inttable_count(&f->defs);
- }
- size_t upb_filedef_depcount(const upb_filedef *f) {
- return upb_inttable_count(&f->deps);
- }
- const upb_def *upb_filedef_def(const upb_filedef *f, size_t i) {
- upb_value v;
- if (upb_inttable_lookup32(&f->defs, i, &v)) {
- return upb_value_getconstptr(v);
- } else {
- return NULL;
- }
- }
- const upb_filedef *upb_filedef_dep(const upb_filedef *f, size_t i) {
- upb_value v;
- if (upb_inttable_lookup32(&f->deps, i, &v)) {
- return upb_value_getconstptr(v);
- } else {
- return NULL;
- }
- }
- bool upb_filedef_setname(upb_filedef *f, const char *name, upb_status *s) {
- name = upb_gstrdup(name);
- if (!name) {
- upb_upberr_setoom(s);
- return false;
- }
- upb_gfree((void*)f->name);
- f->name = name;
- return true;
- }
- bool upb_filedef_setpackage(upb_filedef *f, const char *package,
- upb_status *s) {
- if (!upb_isident(package, strlen(package), true, s)) return false;
- package = upb_gstrdup(package);
- if (!package) {
- upb_upberr_setoom(s);
- return false;
- }
- upb_gfree((void*)f->package);
- f->package = package;
- return true;
- }
- bool upb_filedef_setphpprefix(upb_filedef *f, const char *phpprefix,
- upb_status *s) {
- phpprefix = upb_gstrdup(phpprefix);
- if (!phpprefix) {
- upb_upberr_setoom(s);
- return false;
- }
- upb_gfree((void*)f->phpprefix);
- f->phpprefix = phpprefix;
- return true;
- }
- bool upb_filedef_setphpnamespace(upb_filedef *f, const char *phpnamespace,
- upb_status *s) {
- phpnamespace = upb_gstrdup(phpnamespace);
- if (!phpnamespace) {
- upb_upberr_setoom(s);
- return false;
- }
- upb_gfree((void*)f->phpnamespace);
- f->phpnamespace = phpnamespace;
- return true;
- }
- bool upb_filedef_setsyntax(upb_filedef *f, upb_syntax_t syntax,
- upb_status *s) {
- UPB_UNUSED(s);
- if (syntax != UPB_SYNTAX_PROTO2 &&
- syntax != UPB_SYNTAX_PROTO3) {
- upb_status_seterrmsg(s, "Unknown syntax value.");
- return false;
- }
- f->syntax = syntax;
- {
- /* Set all messages in this file to match. */
- size_t i;
- for (i = 0; i < upb_filedef_defcount(f); i++) {
- /* Casting const away is safe since all defs in mutable filedef must
- * also be mutable. */
- upb_def *def = (upb_def*)upb_filedef_def(f, i);
- upb_msgdef *m = upb_dyncast_msgdef_mutable(def);
- if (m) {
- m->syntax = syntax;
- }
- }
- }
- return true;
- }
- bool upb_filedef_adddef(upb_filedef *f, upb_def *def, const void *ref_donor,
- upb_status *s) {
- if (def->file) {
- upb_status_seterrmsg(s, "Def is already part of another filedef.");
- return false;
- }
- if (upb_inttable_push(&f->defs, upb_value_constptr(def))) {
- def->file = f;
- upb_ref2(def, f);
- upb_ref2(f, def);
- if (ref_donor) upb_def_unref(def, ref_donor);
- if (def->type == UPB_DEF_MSG) {
- upb_downcast_msgdef_mutable(def)->syntax = f->syntax;
- }
- return true;
- } else {
- upb_upberr_setoom(s);
- return false;
- }
- }
- bool upb_filedef_adddep(upb_filedef *f, const upb_filedef *dep) {
- if (upb_inttable_push(&f->deps, upb_value_constptr(dep))) {
- /* Regular ref instead of ref2 because files can't form cycles. */
- upb_filedef_ref(dep, f);
- return true;
- } else {
- return false;
- }
- }
- void upb_symtab_free(upb_symtab *s) {
- upb_strtable_iter i;
- upb_strtable_begin(&i, &s->symtab);
- for (; !upb_strtable_done(&i); upb_strtable_next(&i)) {
- const upb_def *def = upb_value_getptr(upb_strtable_iter_value(&i));
- upb_def_unref(def, s);
- }
- upb_strtable_uninit(&s->symtab);
- upb_gfree(s);
- }
- upb_symtab *upb_symtab_new() {
- upb_symtab *s = upb_gmalloc(sizeof(*s));
- if (!s) {
- return NULL;
- }
- upb_strtable_init(&s->symtab, UPB_CTYPE_PTR);
- return s;
- }
- const upb_def *upb_symtab_lookup(const upb_symtab *s, const char *sym) {
- upb_value v;
- upb_def *ret = upb_strtable_lookup(&s->symtab, sym, &v) ?
- upb_value_getptr(v) : NULL;
- return ret;
- }
- const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym) {
- upb_value v;
- upb_def *def = upb_strtable_lookup(&s->symtab, sym, &v) ?
- upb_value_getptr(v) : NULL;
- return def ? upb_dyncast_msgdef(def) : NULL;
- }
- const upb_msgdef *upb_symtab_lookupmsg2(const upb_symtab *s, const char *sym,
- size_t len) {
- upb_value v;
- upb_def *def = upb_strtable_lookup2(&s->symtab, sym, len, &v) ?
- upb_value_getptr(v) : NULL;
- return def ? upb_dyncast_msgdef(def) : NULL;
- }
- const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) {
- upb_value v;
- upb_def *def = upb_strtable_lookup(&s->symtab, sym, &v) ?
- upb_value_getptr(v) : NULL;
- return def ? upb_dyncast_enumdef(def) : NULL;
- }
- /* Given a symbol and the base symbol inside which it is defined, find the
- * symbol's definition in t. */
- static upb_def *upb_resolvename(const upb_strtable *t,
- const char *base, const char *sym) {
- if(strlen(sym) == 0) return NULL;
- if(sym[0] == '.') {
- /* Symbols starting with '.' are absolute, so we do a single lookup.
- * Slice to omit the leading '.' */
- upb_value v;
- return upb_strtable_lookup(t, sym + 1, &v) ? upb_value_getptr(v) : NULL;
- } else {
- /* Remove components from base until we find an entry or run out.
- * TODO: This branch is totally broken, but currently not used. */
- (void)base;
- UPB_ASSERT(false);
- return NULL;
- }
- }
- const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base,
- const char *sym) {
- upb_def *ret = upb_resolvename(&s->symtab, base, sym);
- return ret;
- }
- /* TODO(haberman): we need a lot more testing of error conditions. */
- static bool symtab_add(upb_symtab *s, upb_def *const*defs, size_t n,
- void *ref_donor, upb_refcounted *freeze_also,
- upb_status *status) {
- size_t i;
- size_t add_n;
- size_t freeze_n;
- upb_strtable_iter iter;
- upb_refcounted **add_objs = NULL;
- upb_def **add_defs = NULL;
- size_t add_objs_size;
- upb_strtable addtab;
- if (n == 0 && !freeze_also) {
- return true;
- }
- if (!upb_strtable_init(&addtab, UPB_CTYPE_PTR)) {
- upb_status_seterrmsg(status, "out of memory");
- return false;
- }
- /* Add new defs to our "add" set. */
- for (i = 0; i < n; i++) {
- upb_def *def = defs[i];
- const char *fullname;
- upb_fielddef *f;
- if (upb_def_isfrozen(def)) {
- upb_status_seterrmsg(status, "added defs must be mutable");
- goto err;
- }
- UPB_ASSERT(!upb_def_isfrozen(def));
- fullname = upb_def_fullname(def);
- if (!fullname) {
- upb_status_seterrmsg(
- status, "Anonymous defs cannot be added to a symtab");
- goto err;
- }
- f = upb_dyncast_fielddef_mutable(def);
- if (f) {
- if (!upb_fielddef_containingtypename(f)) {
- upb_status_seterrmsg(status,
- "Standalone fielddefs must have a containing type "
- "(extendee) name set");
- goto err;
- }
- } else {
- if (upb_strtable_lookup(&addtab, fullname, NULL)) {
- upb_status_seterrf(status, "Conflicting defs named '%s'", fullname);
- goto err;
- }
- if (upb_strtable_lookup(&s->symtab, fullname, NULL)) {
- upb_status_seterrf(status, "Symtab already has a def named '%s'",
- fullname);
- goto err;
- }
- if (!upb_strtable_insert(&addtab, fullname, upb_value_ptr(def)))
- goto oom_err;
- upb_def_donateref(def, ref_donor, s);
- }
- if (upb_dyncast_fielddef_mutable(def)) {
- /* TODO(haberman): allow adding extensions attached to files. */
- upb_status_seterrf(status, "Can't add extensions to symtab.\n");
- goto err;
- }
- }
- /* Now using the table, resolve symbolic references for subdefs. */
- upb_strtable_begin(&iter, &addtab);
- for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) {
- const char *base;
- upb_def *def = upb_value_getptr(upb_strtable_iter_value(&iter));
- upb_msgdef *m = upb_dyncast_msgdef_mutable(def);
- upb_msg_field_iter j;
- if (!m) continue;
- /* Type names are resolved relative to the message in which they appear. */
- base = upb_msgdef_fullname(m);
- for(upb_msg_field_begin(&j, m);
- !upb_msg_field_done(&j);
- upb_msg_field_next(&j)) {
- upb_fielddef *f = upb_msg_iter_field(&j);
- const char *name = upb_fielddef_subdefname(f);
- if (name && !upb_fielddef_subdef(f)) {
- /* Try the lookup in the current set of to-be-added defs first. If not
- * there, try existing defs. */
- upb_def *subdef = upb_resolvename(&addtab, base, name);
- if (subdef == NULL) {
- subdef = upb_resolvename(&s->symtab, base, name);
- }
- if (subdef == NULL) {
- upb_status_seterrf(
- status, "couldn't resolve name '%s' in message '%s'", name, base);
- goto err;
- } else if (!upb_fielddef_setsubdef(f, subdef, status)) {
- goto err;
- }
- }
- }
- }
- /* We need an array of the defs in addtab, for passing to
- * upb_refcounted_freeze(). */
- add_objs_size = upb_strtable_count(&addtab);
- if (freeze_also) {
- add_objs_size++;
- }
- add_defs = upb_gmalloc(sizeof(void*) * add_objs_size);
- if (add_defs == NULL) goto oom_err;
- upb_strtable_begin(&iter, &addtab);
- for (add_n = 0; !upb_strtable_done(&iter); upb_strtable_next(&iter)) {
- add_defs[add_n++] = upb_value_getptr(upb_strtable_iter_value(&iter));
- }
- /* Validate defs. */
- if (!_upb_def_validate(add_defs, add_n, status)) {
- goto err;
- }
- /* Cheat a little and give the array a new type.
- * This is probably undefined behavior, but this code will be deleted soon. */
- add_objs = (upb_refcounted**)add_defs;
- freeze_n = add_n;
- if (freeze_also) {
- add_objs[freeze_n++] = freeze_also;
- }
- if (!upb_refcounted_freeze(add_objs, freeze_n, status,
- UPB_MAX_MESSAGE_DEPTH * 2)) {
- goto err;
- }
- /* This must be delayed until all errors have been detected, since error
- * recovery code uses this table to cleanup defs. */
- upb_strtable_uninit(&addtab);
- /* TODO(haberman) we don't properly handle errors after this point (like
- * OOM in upb_strtable_insert() below). */
- for (i = 0; i < add_n; i++) {
- upb_def *def = (upb_def*)add_objs[i];
- const char *name = upb_def_fullname(def);
- bool success;
- success = upb_strtable_insert(&s->symtab, name, upb_value_ptr(def));
- UPB_ASSERT(success);
- }
- upb_gfree(add_defs);
- return true;
- oom_err:
- upb_status_seterrmsg(status, "out of memory");
- err: {
- /* We need to donate the refs back. */
- upb_strtable_begin(&iter, &addtab);
- for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) {
- upb_def *def = upb_value_getptr(upb_strtable_iter_value(&iter));
- upb_def_donateref(def, s, ref_donor);
- }
- }
- upb_strtable_uninit(&addtab);
- upb_gfree(add_defs);
- UPB_ASSERT(!upb_ok(status));
- return false;
- }
- bool upb_symtab_add(upb_symtab *s, upb_def *const*defs, size_t n,
- void *ref_donor, upb_status *status) {
- return symtab_add(s, defs, n, ref_donor, NULL, status);
- }
- bool upb_symtab_addfile(upb_symtab *s, upb_filedef *file, upb_status *status) {
- size_t n;
- size_t i;
- upb_def **defs;
- bool ret;
- n = upb_filedef_defcount(file);
- if (n == 0) {
- return true;
- }
- defs = upb_gmalloc(sizeof(*defs) * n);
- if (defs == NULL) {
- upb_status_seterrmsg(status, "Out of memory");
- return false;
- }
- for (i = 0; i < n; i++) {
- defs[i] = upb_filedef_mutabledef(file, i);
- }
- ret = symtab_add(s, defs, n, NULL, upb_filedef_upcast_mutable(file), status);
- upb_gfree(defs);
- return ret;
- }
- /* Iteration. */
- static void advance_to_matching(upb_symtab_iter *iter) {
- if (iter->type == UPB_DEF_ANY)
- return;
- while (!upb_strtable_done(&iter->iter) &&
- iter->type != upb_symtab_iter_def(iter)->type) {
- upb_strtable_next(&iter->iter);
- }
- }
- void upb_symtab_begin(upb_symtab_iter *iter, const upb_symtab *s,
- upb_deftype_t type) {
- upb_strtable_begin(&iter->iter, &s->symtab);
- iter->type = type;
- advance_to_matching(iter);
- }
- void upb_symtab_next(upb_symtab_iter *iter) {
- upb_strtable_next(&iter->iter);
- advance_to_matching(iter);
- }
- bool upb_symtab_done(const upb_symtab_iter *iter) {
- return upb_strtable_done(&iter->iter);
- }
- const upb_def *upb_symtab_iter_def(const upb_symtab_iter *iter) {
- return upb_value_getptr(upb_strtable_iter_value(&iter->iter));
- }
- /* We encode backwards, to avoid pre-computing lengths (one-pass encode). */
- #define UPB_PB_VARINT_MAX_LEN 10
- #define CHK(x) do { if (!(x)) { return false; } } while(0)
- /* Maps descriptor type -> upb field type. */
- static const uint8_t upb_desctype_to_fieldtype2[] = {
- UPB_WIRE_TYPE_END_GROUP, /* ENDGROUP */
- UPB_TYPE_DOUBLE, /* DOUBLE */
- UPB_TYPE_FLOAT, /* FLOAT */
- UPB_TYPE_INT64, /* INT64 */
- UPB_TYPE_UINT64, /* UINT64 */
- UPB_TYPE_INT32, /* INT32 */
- UPB_TYPE_UINT64, /* FIXED64 */
- UPB_TYPE_UINT32, /* FIXED32 */
- UPB_TYPE_BOOL, /* BOOL */
- UPB_TYPE_STRING, /* STRING */
- UPB_TYPE_MESSAGE, /* GROUP */
- UPB_TYPE_MESSAGE, /* MESSAGE */
- UPB_TYPE_BYTES, /* BYTES */
- UPB_TYPE_UINT32, /* UINT32 */
- UPB_TYPE_ENUM, /* ENUM */
- UPB_TYPE_INT32, /* SFIXED32 */
- UPB_TYPE_INT64, /* SFIXED64 */
- UPB_TYPE_INT32, /* SINT32 */
- UPB_TYPE_INT64, /* SINT64 */
- };
- static size_t upb_encode_varint(uint64_t val, char *buf) {
- size_t i;
- if (val < 128) { buf[0] = val; return 1; }
- i = 0;
- while (val) {
- uint8_t byte = val & 0x7fU;
- val >>= 7;
- if (val) byte |= 0x80U;
- buf[i++] = byte;
- }
- return i;
- }
- static uint32_t upb_zzencode_32(int32_t n) { return (n << 1) ^ (n >> 31); }
- static uint64_t upb_zzencode_64(int64_t n) { return (n << 1) ^ (n >> 63); }
- typedef struct {
- upb_alloc *alloc;
- char *buf, *ptr, *limit;
- } upb_encstate;
- static size_t upb_roundup_pow2(size_t bytes) {
- size_t ret = 128;
- while (ret < bytes) {
- ret *= 2;
- }
- return ret;
- }
- static bool upb_encode_growbuffer(upb_encstate *e, size_t bytes) {
- size_t old_size = e->limit - e->buf;
- size_t new_size = upb_roundup_pow2(bytes + (e->limit - e->ptr));
- char *new_buf = upb_realloc(e->alloc, e->buf, old_size, new_size);
- CHK(new_buf);
- /* We want previous data at the end, realloc() put it at the beginning. */
- memmove(new_buf + new_size - old_size, e->buf, old_size);
- e->ptr = new_buf + new_size - (e->limit - e->ptr);
- e->limit = new_buf + new_size;
- e->buf = new_buf;
- return true;
- }
- /* Call to ensure that at least "bytes" bytes are available for writing at
- * e->ptr. Returns false if the bytes could not be allocated. */
- static bool upb_encode_reserve(upb_encstate *e, size_t bytes) {
- CHK(UPB_LIKELY((size_t)(e->ptr - e->buf) >= bytes) ||
- upb_encode_growbuffer(e, bytes));
- e->ptr -= bytes;
- return true;
- }
- /* Writes the given bytes to the buffer, handling reserve/advance. */
- static bool upb_put_bytes(upb_encstate *e, const void *data, size_t len) {
- CHK(upb_encode_reserve(e, len));
- memcpy(e->ptr, data, len);
- return true;
- }
- static bool upb_put_fixed64(upb_encstate *e, uint64_t val) {
- /* TODO(haberman): byte-swap for big endian. */
- return upb_put_bytes(e, &val, sizeof(uint64_t));
- }
- static bool upb_put_fixed32(upb_encstate *e, uint32_t val) {
- /* TODO(haberman): byte-swap for big endian. */
- return upb_put_bytes(e, &val, sizeof(uint32_t));
- }
- static bool upb_put_varint(upb_encstate *e, uint64_t val) {
- size_t len;
- char *start;
- CHK(upb_encode_reserve(e, UPB_PB_VARINT_MAX_LEN));
- len = upb_encode_varint(val, e->ptr);
- start = e->ptr + UPB_PB_VARINT_MAX_LEN - len;
- memmove(start, e->ptr, len);
- e->ptr = start;
- return true;
- }
- static bool upb_put_double(upb_encstate *e, double d) {
- uint64_t u64;
- UPB_ASSERT(sizeof(double) == sizeof(uint64_t));
- memcpy(&u64, &d, sizeof(uint64_t));
- return upb_put_fixed64(e, u64);
- }
- static bool upb_put_float(upb_encstate *e, float d) {
- uint32_t u32;
- UPB_ASSERT(sizeof(float) == sizeof(uint32_t));
- memcpy(&u32, &d, sizeof(uint32_t));
- return upb_put_fixed32(e, u32);
- }
- static uint32_t upb_readcase(const char *msg, const upb_msglayout_field *f) {
- uint32_t ret;
- uint32_t offset = ~f->presence;
- memcpy(&ret, msg + offset, sizeof(ret));
- return ret;
- }
- static bool upb_readhasbit(const char *msg, const upb_msglayout_field *f) {
- uint32_t hasbit = f->presence;
- UPB_ASSERT(f->presence > 0);
- return msg[hasbit / 8] & (1 << (hasbit % 8));
- }
- static bool upb_put_tag(upb_encstate *e, int field_number, int wire_type) {
- return upb_put_varint(e, (field_number << 3) | wire_type);
- }
- static bool upb_put_fixedarray(upb_encstate *e, const upb_array *arr,
- size_t size) {
- size_t bytes = arr->len * size;
- return upb_put_bytes(e, arr->data, bytes) && upb_put_varint(e, bytes);
- }
- bool upb_encode_message(upb_encstate *e, const char *msg,
- const upb_msglayout *m, size_t *size);
- static bool upb_encode_array(upb_encstate *e, const char *field_mem,
- const upb_msglayout *m,
- const upb_msglayout_field *f) {
- const upb_array *arr = *(const upb_array**)field_mem;
- if (arr == NULL || arr->len == 0) {
- return true;
- }
- UPB_ASSERT(arr->type == upb_desctype_to_fieldtype2[f->descriptortype]);
- #define VARINT_CASE(ctype, encode) { \
- ctype *start = arr->data; \
- ctype *ptr = start + arr->len; \
- size_t pre_len = e->limit - e->ptr; \
- do { \
- ptr--; \
- CHK(upb_put_varint(e, encode)); \
- } while (ptr != start); \
- CHK(upb_put_varint(e, e->limit - e->ptr - pre_len)); \
- } \
- break; \
- do { ; } while(0)
- switch (f->descriptortype) {
- case UPB_DESCRIPTOR_TYPE_DOUBLE:
- CHK(upb_put_fixedarray(e, arr, sizeof(double)));
- break;
- case UPB_DESCRIPTOR_TYPE_FLOAT:
- CHK(upb_put_fixedarray(e, arr, sizeof(float)));
- break;
- case UPB_DESCRIPTOR_TYPE_SFIXED64:
- case UPB_DESCRIPTOR_TYPE_FIXED64:
- CHK(upb_put_fixedarray(e, arr, sizeof(uint64_t)));
- break;
- case UPB_DESCRIPTOR_TYPE_FIXED32:
- case UPB_DESCRIPTOR_TYPE_SFIXED32:
- CHK(upb_put_fixedarray(e, arr, sizeof(uint32_t)));
- break;
- case UPB_DESCRIPTOR_TYPE_INT64:
- case UPB_DESCRIPTOR_TYPE_UINT64:
- VARINT_CASE(uint64_t, *ptr);
- case UPB_DESCRIPTOR_TYPE_UINT32:
- VARINT_CASE(uint32_t, *ptr);
- case UPB_DESCRIPTOR_TYPE_INT32:
- case UPB_DESCRIPTOR_TYPE_ENUM:
- VARINT_CASE(int32_t, (int64_t)*ptr);
- case UPB_DESCRIPTOR_TYPE_BOOL:
- VARINT_CASE(bool, *ptr);
- case UPB_DESCRIPTOR_TYPE_SINT32:
- VARINT_CASE(int32_t, upb_zzencode_32(*ptr));
- case UPB_DESCRIPTOR_TYPE_SINT64:
- VARINT_CASE(int64_t, upb_zzencode_64(*ptr));
- case UPB_DESCRIPTOR_TYPE_STRING:
- case UPB_DESCRIPTOR_TYPE_BYTES: {
- upb_strview *start = arr->data;
- upb_strview *ptr = start + arr->len;
- do {
- ptr--;
- CHK(upb_put_bytes(e, ptr->data, ptr->size) &&
- upb_put_varint(e, ptr->size) &&
- upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
- } while (ptr != start);
- return true;
- }
- case UPB_DESCRIPTOR_TYPE_GROUP: {
- void **start = arr->data;
- void **ptr = start + arr->len;
- const upb_msglayout *subm = m->submsgs[f->submsg_index];
- do {
- size_t size;
- ptr--;
- CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
- upb_encode_message(e, *ptr, subm, &size) &&
- upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP));
- } while (ptr != start);
- return true;
- }
- case UPB_DESCRIPTOR_TYPE_MESSAGE: {
- void **start = arr->data;
- void **ptr = start + arr->len;
- const upb_msglayout *subm = m->submsgs[f->submsg_index];
- do {
- size_t size;
- ptr--;
- CHK(upb_encode_message(e, *ptr, subm, &size) &&
- upb_put_varint(e, size) &&
- upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
- } while (ptr != start);
- return true;
- }
- }
- #undef VARINT_CASE
- /* We encode all primitive arrays as packed, regardless of what was specified
- * in the .proto file. Could special case 1-sized arrays. */
- CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
- return true;
- }
- static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem,
- const upb_msglayout *m,
- const upb_msglayout_field *f,
- bool skip_zero_value) {
- #define CASE(ctype, type, wire_type, encodeval) do { \
- ctype val = *(ctype*)field_mem; \
- if (skip_zero_value && val == 0) { \
- return true; \
- } \
- return upb_put_ ## type(e, encodeval) && \
- upb_put_tag(e, f->number, wire_type); \
- } while(0)
- switch (f->descriptortype) {
- case UPB_DESCRIPTOR_TYPE_DOUBLE:
- CASE(double, double, UPB_WIRE_TYPE_64BIT, val);
- case UPB_DESCRIPTOR_TYPE_FLOAT:
- CASE(float, float, UPB_WIRE_TYPE_32BIT, val);
- case UPB_DESCRIPTOR_TYPE_INT64:
- case UPB_DESCRIPTOR_TYPE_UINT64:
- CASE(uint64_t, varint, UPB_WIRE_TYPE_VARINT, val);
- case UPB_DESCRIPTOR_TYPE_UINT32:
- CASE(uint32_t, varint, UPB_WIRE_TYPE_VARINT, val);
- case UPB_DESCRIPTOR_TYPE_INT32:
- case UPB_DESCRIPTOR_TYPE_ENUM:
- CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, (int64_t)val);
- case UPB_DESCRIPTOR_TYPE_SFIXED64:
- case UPB_DESCRIPTOR_TYPE_FIXED64:
- CASE(uint64_t, fixed64, UPB_WIRE_TYPE_64BIT, val);
- case UPB_DESCRIPTOR_TYPE_FIXED32:
- case UPB_DESCRIPTOR_TYPE_SFIXED32:
- CASE(uint32_t, fixed32, UPB_WIRE_TYPE_32BIT, val);
- case UPB_DESCRIPTOR_TYPE_BOOL:
- CASE(bool, varint, UPB_WIRE_TYPE_VARINT, val);
- case UPB_DESCRIPTOR_TYPE_SINT32:
- CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_32(val));
- case UPB_DESCRIPTOR_TYPE_SINT64:
- CASE(int64_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_64(val));
- case UPB_DESCRIPTOR_TYPE_STRING:
- case UPB_DESCRIPTOR_TYPE_BYTES: {
- upb_strview view = *(upb_strview*)field_mem;
- if (skip_zero_value && view.size == 0) {
- return true;
- }
- return upb_put_bytes(e, view.data, view.size) &&
- upb_put_varint(e, view.size) &&
- upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
- }
- case UPB_DESCRIPTOR_TYPE_GROUP: {
- size_t size;
- void *submsg = *(void **)field_mem;
- const upb_msglayout *subm = m->submsgs[f->submsg_index];
- if (submsg == NULL) {
- return true;
- }
- return upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
- upb_encode_message(e, submsg, subm, &size) &&
- upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP);
- }
- case UPB_DESCRIPTOR_TYPE_MESSAGE: {
- size_t size;
- void *submsg = *(void **)field_mem;
- const upb_msglayout *subm = m->submsgs[f->submsg_index];
- if (submsg == NULL) {
- return true;
- }
- return upb_encode_message(e, submsg, subm, &size) &&
- upb_put_varint(e, size) &&
- upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
- }
- }
- #undef CASE
- UPB_UNREACHABLE();
- }
- bool upb_encode_message(upb_encstate *e, const char *msg,
- const upb_msglayout *m, size_t *size) {
- int i;
- size_t pre_len = e->limit - e->ptr;
- const char *unknown;
- size_t unknown_size;
- for (i = m->field_count - 1; i >= 0; i--) {
- const upb_msglayout_field *f = &m->fields[i];
- if (f->label == UPB_LABEL_REPEATED) {
- CHK(upb_encode_array(e, msg + f->offset, m, f));
- } else {
- bool skip_empty = false;
- if (f->presence == 0) {
- /* Proto3 presence. */
- skip_empty = true;
- } else if (f->presence > 0) {
- /* Proto2 presence: hasbit. */
- if (!upb_readhasbit(msg, f)) {
- continue;
- }
- } else {
- /* Field is in a oneof. */
- if (upb_readcase(msg, f) != f->number) {
- continue;
- }
- }
- CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, skip_empty));
- }
- }
- unknown = upb_msg_getunknown(msg, &unknown_size);
- if (unknown) {
- upb_put_bytes(e, unknown, unknown_size);
- }
- *size = (e->limit - e->ptr) - pre_len;
- return true;
- }
- char *upb_encode(const void *msg, const upb_msglayout *m, upb_arena *arena,
- size_t *size) {
- upb_encstate e;
- e.alloc = upb_arena_alloc(arena);
- e.buf = NULL;
- e.limit = NULL;
- e.ptr = NULL;
- if (!upb_encode_message(&e, msg, m, size)) {
- *size = 0;
- return NULL;
- }
- *size = e.limit - e.ptr;
- if (*size == 0) {
- static char ch;
- return &ch;
- } else {
- UPB_ASSERT(e.ptr);
- return e.ptr;
- }
- }
- #undef CHK
- /*
- ** TODO(haberman): it's unclear whether a lot of the consistency checks should
- ** UPB_ASSERT() or return false.
- */
- #include <string.h>
- static void *upb_calloc(size_t size) {
- void *mem = upb_gmalloc(size);
- if (mem) {
- memset(mem, 0, size);
- }
- return mem;
- }
- /* Defined for the sole purpose of having a unique pointer value for
- * UPB_NO_CLOSURE. */
- char _upb_noclosure;
- static void freehandlers(upb_refcounted *r) {
- upb_handlers *h = (upb_handlers*)r;
- upb_inttable_iter i;
- upb_inttable_begin(&i, &h->cleanup_);
- for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
- void *val = (void*)upb_inttable_iter_key(&i);
- upb_value func_val = upb_inttable_iter_value(&i);
- upb_handlerfree *func = upb_value_getfptr(func_val);
- func(val);
- }
- upb_inttable_uninit(&h->cleanup_);
- upb_msgdef_unref(h->msg, h);
- upb_gfree(h->sub);
- upb_gfree(h);
- }
- static void visithandlers(const upb_refcounted *r, upb_refcounted_visit *visit,
- void *closure) {
- const upb_handlers *h = (const upb_handlers*)r;
- upb_msg_field_iter i;
- for(upb_msg_field_begin(&i, h->msg);
- !upb_msg_field_done(&i);
- upb_msg_field_next(&i)) {
- upb_fielddef *f = upb_msg_iter_field(&i);
- const upb_handlers *sub;
- if (!upb_fielddef_issubmsg(f)) continue;
- sub = upb_handlers_getsubhandlers(h, f);
- if (sub) visit(r, upb_handlers_upcast(sub), closure);
- }
- }
- static const struct upb_refcounted_vtbl vtbl = {visithandlers, freehandlers};
- typedef struct {
- upb_inttable tab; /* maps upb_msgdef* -> upb_handlers*. */
- upb_handlers_callback *callback;
- const void *closure;
- } dfs_state;
- /* TODO(haberman): discard upb_handlers* objects that do not actually have any
- * handlers set and cannot reach any upb_handlers* object that does. This is
- * slightly tricky to do correctly. */
- static upb_handlers *newformsg(const upb_msgdef *m, const void *owner,
- dfs_state *s) {
- upb_msg_field_iter i;
- upb_handlers *h = upb_handlers_new(m, owner);
- if (!h) return NULL;
- if (!upb_inttable_insertptr(&s->tab, m, upb_value_ptr(h))) goto oom;
- s->callback(s->closure, h);
- /* For each submessage field, get or create a handlers object and set it as
- * the subhandlers. */
- for(upb_msg_field_begin(&i, m);
- !upb_msg_field_done(&i);
- upb_msg_field_next(&i)) {
- upb_fielddef *f = upb_msg_iter_field(&i);
- const upb_msgdef *subdef;
- upb_value subm_ent;
- if (!upb_fielddef_issubmsg(f)) continue;
- subdef = upb_downcast_msgdef(upb_fielddef_subdef(f));
- if (upb_inttable_lookupptr(&s->tab, subdef, &subm_ent)) {
- upb_handlers_setsubhandlers(h, f, upb_value_getptr(subm_ent));
- } else {
- upb_handlers *sub_mh = newformsg(subdef, &sub_mh, s);
- if (!sub_mh) goto oom;
- upb_handlers_setsubhandlers(h, f, sub_mh);
- upb_handlers_unref(sub_mh, &sub_mh);
- }
- }
- return h;
- oom:
- upb_handlers_unref(h, owner);
- return NULL;
- }
- /* Given a selector for a STARTSUBMSG handler, resolves to a pointer to the
- * subhandlers for this submessage field. */
- #define SUBH(h, selector) (h->sub[selector])
- /* The selector for a submessage field is the field index. */
- #define SUBH_F(h, f) SUBH(h, f->index_)
- static int32_t trygetsel(upb_handlers *h, const upb_fielddef *f,
- upb_handlertype_t type) {
- upb_selector_t sel;
- UPB_ASSERT(!upb_handlers_isfrozen(h));
- if (upb_handlers_msgdef(h) != upb_fielddef_containingtype(f)) {
- upb_status_seterrf(
- &h->status_, "type mismatch: field %s does not belong to message %s",
- upb_fielddef_name(f), upb_msgdef_fullname(upb_handlers_msgdef(h)));
- return -1;
- }
- if (!upb_handlers_getselector(f, type, &sel)) {
- upb_status_seterrf(
- &h->status_,
- "type mismatch: cannot register handler type %d for field %s",
- type, upb_fielddef_name(f));
- return -1;
- }
- return sel;
- }
- static upb_selector_t handlers_getsel(upb_handlers *h, const upb_fielddef *f,
- upb_handlertype_t type) {
- int32_t sel = trygetsel(h, f, type);
- UPB_ASSERT(sel >= 0);
- return sel;
- }
- static const void **returntype(upb_handlers *h, const upb_fielddef *f,
- upb_handlertype_t type) {
- return &h->table[handlers_getsel(h, f, type)].attr.return_closure_type_;
- }
- static bool doset(upb_handlers *h, int32_t sel, const upb_fielddef *f,
- upb_handlertype_t type, upb_func *func,
- upb_handlerattr *attr) {
- upb_handlerattr set_attr = UPB_HANDLERATTR_INITIALIZER;
- const void *closure_type;
- const void **context_closure_type;
- UPB_ASSERT(!upb_handlers_isfrozen(h));
- if (sel < 0) {
- upb_status_seterrmsg(&h->status_,
- "incorrect handler type for this field.");
- return false;
- }
- if (h->table[sel].func) {
- upb_status_seterrmsg(&h->status_,
- "cannot change handler once it has been set.");
- return false;
- }
- if (attr) {
- set_attr = *attr;
- }
- /* Check that the given closure type matches the closure type that has been
- * established for this context (if any). */
- closure_type = upb_handlerattr_closuretype(&set_attr);
- if (type == UPB_HANDLER_STRING) {
- context_closure_type = returntype(h, f, UPB_HANDLER_STARTSTR);
- } else if (f && upb_fielddef_isseq(f) &&
- type != UPB_HANDLER_STARTSEQ &&
- type != UPB_HANDLER_ENDSEQ) {
- context_closure_type = returntype(h, f, UPB_HANDLER_STARTSEQ);
- } else {
- context_closure_type = &h->top_closure_type;
- }
- if (closure_type && *context_closure_type &&
- closure_type != *context_closure_type) {
- /* TODO(haberman): better message for debugging. */
- if (f) {
- upb_status_seterrf(&h->status_,
- "closure type does not match for field %s",
- upb_fielddef_name(f));
- } else {
- upb_status_seterrmsg(
- &h->status_, "closure type does not match for message-level handler");
- }
- return false;
- }
- if (closure_type)
- *context_closure_type = closure_type;
- /* If this is a STARTSEQ or STARTSTR handler, check that the returned pointer
- * matches any pre-existing expectations about what type is expected. */
- if (type == UPB_HANDLER_STARTSEQ || type == UPB_HANDLER_STARTSTR) {
- const void *return_type = upb_handlerattr_returnclosuretype(&set_attr);
- const void *table_return_type =
- upb_handlerattr_returnclosuretype(&h->table[sel].attr);
- if (return_type && table_return_type && return_type != table_return_type) {
- upb_status_seterrmsg(&h->status_, "closure return type does not match");
- return false;
- }
- if (table_return_type && !return_type)
- upb_handlerattr_setreturnclosuretype(&set_attr, table_return_type);
- }
- h->table[sel].func = (upb_func*)func;
- h->table[sel].attr = set_attr;
- return true;
- }
- /* Returns the effective closure type for this handler (which will propagate
- * from outer frames if this frame has no START* handler). Not implemented for
- * UPB_HANDLER_STRING at the moment since this is not needed. Returns NULL is
- * the effective closure type is unspecified (either no handler was registered
- * to specify it or the handler that was registered did not specify the closure
- * type). */
- const void *effective_closure_type(upb_handlers *h, const upb_fielddef *f,
- upb_handlertype_t type) {
- const void *ret;
- upb_selector_t sel;
- UPB_ASSERT(type != UPB_HANDLER_STRING);
- ret = h->top_closure_type;
- if (upb_fielddef_isseq(f) &&
- type != UPB_HANDLER_STARTSEQ &&
- type != UPB_HANDLER_ENDSEQ &&
- h->table[sel = handlers_getsel(h, f, UPB_HANDLER_STARTSEQ)].func) {
- ret = upb_handlerattr_returnclosuretype(&h->table[sel].attr);
- }
- if (type == UPB_HANDLER_STRING &&
- h->table[sel = handlers_getsel(h, f, UPB_HANDLER_STARTSTR)].func) {
- ret = upb_handlerattr_returnclosuretype(&h->table[sel].attr);
- }
- /* The effective type of the submessage; not used yet.
- * if (type == SUBMESSAGE &&
- * h->table[sel = handlers_getsel(h, f, UPB_HANDLER_STARTSUBMSG)].func) {
- * ret = upb_handlerattr_returnclosuretype(&h->table[sel].attr);
- * } */
- return ret;
- }
- /* Checks whether the START* handler specified by f & type is missing even
- * though it is required to convert the established type of an outer frame
- * ("closure_type") into the established type of an inner frame (represented in
- * the return closure type of this handler's attr. */
- bool checkstart(upb_handlers *h, const upb_fielddef *f, upb_handlertype_t type,
- upb_status *status) {
- const void *closure_type;
- const upb_handlerattr *attr;
- const void *return_closure_type;
- upb_selector_t sel = handlers_getsel(h, f, type);
- if (h->table[sel].func) return true;
- closure_type = effective_closure_type(h, f, type);
- attr = &h->table[sel].attr;
- return_closure_type = upb_handlerattr_returnclosuretype(attr);
- if (closure_type && return_closure_type &&
- closure_type != return_closure_type) {
- upb_status_seterrf(status,
- "expected start handler to return sub type for field %f",
- upb_fielddef_name(f));
- return false;
- }
- return true;
- }
- /* Public interface ***********************************************************/
- upb_handlers *upb_handlers_new(const upb_msgdef *md, const void *owner) {
- int extra;
- upb_handlers *h;
- UPB_ASSERT(upb_msgdef_isfrozen(md));
- extra = sizeof(upb_handlers_tabent) * (md->selector_count - 1);
- h = upb_calloc(sizeof(*h) + extra);
- if (!h) return NULL;
- h->msg = md;
- upb_msgdef_ref(h->msg, h);
- upb_status_clear(&h->status_);
- if (md->submsg_field_count > 0) {
- h->sub = upb_calloc(md->submsg_field_count * sizeof(*h->sub));
- if (!h->sub) goto oom;
- } else {
- h->sub = 0;
- }
- if (!upb_refcounted_init(upb_handlers_upcast_mutable(h), &vtbl, owner))
- goto oom;
- if (!upb_inttable_init(&h->cleanup_, UPB_CTYPE_FPTR)) goto oom;
- /* calloc() above initialized all handlers to NULL. */
- return h;
- oom:
- freehandlers(upb_handlers_upcast_mutable(h));
- return NULL;
- }
- const upb_handlers *upb_handlers_newfrozen(const upb_msgdef *m,
- const void *owner,
- upb_handlers_callback *callback,
- const void *closure) {
- dfs_state state;
- upb_handlers *ret;
- bool ok;
- upb_refcounted *r;
- state.callback = callback;
- state.closure = closure;
- if (!upb_inttable_init(&state.tab, UPB_CTYPE_PTR)) return NULL;
- ret = newformsg(m, owner, &state);
- upb_inttable_uninit(&state.tab);
- if (!ret) return NULL;
- r = upb_handlers_upcast_mutable(ret);
- ok = upb_refcounted_freeze(&r, 1, NULL, UPB_MAX_HANDLER_DEPTH);
- UPB_ASSERT(ok);
- return ret;
- }
- const upb_status *upb_handlers_status(upb_handlers *h) {
- UPB_ASSERT(!upb_handlers_isfrozen(h));
- return &h->status_;
- }
- void upb_handlers_clearerr(upb_handlers *h) {
- UPB_ASSERT(!upb_handlers_isfrozen(h));
- upb_status_clear(&h->status_);
- }
- #define SETTER(name, handlerctype, handlertype) \
- bool upb_handlers_set ## name(upb_handlers *h, const upb_fielddef *f, \
- handlerctype func, upb_handlerattr *attr) { \
- int32_t sel = trygetsel(h, f, handlertype); \
- return doset(h, sel, f, handlertype, (upb_func*)func, attr); \
- }
- SETTER(int32, upb_int32_handlerfunc*, UPB_HANDLER_INT32)
- SETTER(int64, upb_int64_handlerfunc*, UPB_HANDLER_INT64)
- SETTER(uint32, upb_uint32_handlerfunc*, UPB_HANDLER_UINT32)
- SETTER(uint64, upb_uint64_handlerfunc*, UPB_HANDLER_UINT64)
- SETTER(float, upb_float_handlerfunc*, UPB_HANDLER_FLOAT)
- SETTER(double, upb_double_handlerfunc*, UPB_HANDLER_DOUBLE)
- SETTER(bool, upb_bool_handlerfunc*, UPB_HANDLER_BOOL)
- SETTER(startstr, upb_startstr_handlerfunc*, UPB_HANDLER_STARTSTR)
- SETTER(string, upb_string_handlerfunc*, UPB_HANDLER_STRING)
- SETTER(endstr, upb_endfield_handlerfunc*, UPB_HANDLER_ENDSTR)
- SETTER(startseq, upb_startfield_handlerfunc*, UPB_HANDLER_STARTSEQ)
- SETTER(startsubmsg, upb_startfield_handlerfunc*, UPB_HANDLER_STARTSUBMSG)
- SETTER(endsubmsg, upb_endfield_handlerfunc*, UPB_HANDLER_ENDSUBMSG)
- SETTER(endseq, upb_endfield_handlerfunc*, UPB_HANDLER_ENDSEQ)
- #undef SETTER
- bool upb_handlers_setunknown(upb_handlers *h, upb_unknown_handlerfunc *func,
- upb_handlerattr *attr) {
- return doset(h, UPB_UNKNOWN_SELECTOR, NULL, UPB_HANDLER_INT32,
- (upb_func *)func, attr);
- }
- bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func,
- upb_handlerattr *attr) {
- return doset(h, UPB_STARTMSG_SELECTOR, NULL, UPB_HANDLER_INT32,
- (upb_func *)func, attr);
- }
- bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func,
- upb_handlerattr *attr) {
- UPB_ASSERT(!upb_handlers_isfrozen(h));
- return doset(h, UPB_ENDMSG_SELECTOR, NULL, UPB_HANDLER_INT32,
- (upb_func *)func, attr);
- }
- bool upb_handlers_setsubhandlers(upb_handlers *h, const upb_fielddef *f,
- const upb_handlers *sub) {
- UPB_ASSERT(sub);
- UPB_ASSERT(!upb_handlers_isfrozen(h));
- UPB_ASSERT(upb_fielddef_issubmsg(f));
- if (SUBH_F(h, f)) return false; /* Can't reset. */
- if (upb_msgdef_upcast(upb_handlers_msgdef(sub)) != upb_fielddef_subdef(f)) {
- return false;
- }
- SUBH_F(h, f) = sub;
- upb_ref2(sub, h);
- return true;
- }
- const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h,
- const upb_fielddef *f) {
- UPB_ASSERT(upb_fielddef_issubmsg(f));
- return SUBH_F(h, f);
- }
- bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t sel,
- upb_handlerattr *attr) {
- if (!upb_handlers_gethandler(h, sel))
- return false;
- *attr = h->table[sel].attr;
- return true;
- }
- const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h,
- upb_selector_t sel) {
- /* STARTSUBMSG selector in sel is the field's selector base. */
- return SUBH(h, sel - UPB_STATIC_SELECTOR_COUNT);
- }
- const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h) { return h->msg; }
- bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *func) {
- bool ok;
- if (upb_inttable_lookupptr(&h->cleanup_, p, NULL)) {
- return false;
- }
- ok = upb_inttable_insertptr(&h->cleanup_, p, upb_value_fptr(func));
- UPB_ASSERT(ok);
- return true;
- }
- /* "Static" methods ***********************************************************/
- bool upb_handlers_freeze(upb_handlers *const*handlers, int n, upb_status *s) {
- /* TODO: verify we have a transitive closure. */
- int i;
- for (i = 0; i < n; i++) {
- upb_msg_field_iter j;
- upb_handlers *h = handlers[i];
- if (!upb_ok(&h->status_)) {
- upb_status_seterrf(s, "handlers for message %s had error status: %s",
- upb_msgdef_fullname(upb_handlers_msgdef(h)),
- upb_status_errmsg(&h->status_));
- return false;
- }
- /* Check that there are no closure mismatches due to missing Start* handlers
- * or subhandlers with different type-level types. */
- for(upb_msg_field_begin(&j, h->msg);
- !upb_msg_field_done(&j);
- upb_msg_field_next(&j)) {
- const upb_fielddef *f = upb_msg_iter_field(&j);
- if (upb_fielddef_isseq(f)) {
- if (!checkstart(h, f, UPB_HANDLER_STARTSEQ, s))
- return false;
- }
- if (upb_fielddef_isstring(f)) {
- if (!checkstart(h, f, UPB_HANDLER_STARTSTR, s))
- return false;
- }
- if (upb_fielddef_issubmsg(f)) {
- bool hashandler = false;
- if (upb_handlers_gethandler(
- h, handlers_getsel(h, f, UPB_HANDLER_STARTSUBMSG)) ||
- upb_handlers_gethandler(
- h, handlers_getsel(h, f, UPB_HANDLER_ENDSUBMSG))) {
- hashandler = true;
- }
- if (upb_fielddef_isseq(f) &&
- (upb_handlers_gethandler(
- h, handlers_getsel(h, f, UPB_HANDLER_STARTSEQ)) ||
- upb_handlers_gethandler(
- h, handlers_getsel(h, f, UPB_HANDLER_ENDSEQ)))) {
- hashandler = true;
- }
- if (hashandler && !upb_handlers_getsubhandlers(h, f)) {
- /* For now we add an empty subhandlers in this case. It makes the
- * decoder code generator simpler, because it only has to handle two
- * cases (submessage has handlers or not) as opposed to three
- * (submessage has handlers in enclosing message but no subhandlers).
- *
- * This makes parsing less efficient in the case that we want to
- * notice a submessage but skip its contents (like if we're testing
- * for submessage presence or counting the number of repeated
- * submessages). In this case we will end up parsing the submessage
- * field by field and throwing away the results for each, instead of
- * skipping the whole delimited thing at once. If this is an issue we
- * can revisit it, but do remember that this only arises when you have
- * handlers (startseq/startsubmsg/endsubmsg/endseq) set for the
- * submessage but no subhandlers. The uses cases for this are
- * limited. */
- upb_handlers *sub = upb_handlers_new(upb_fielddef_msgsubdef(f), &sub);
- upb_handlers_setsubhandlers(h, f, sub);
- upb_handlers_unref(sub, &sub);
- }
- /* TODO(haberman): check type of submessage.
- * This is slightly tricky; also consider whether we should check that
- * they match at setsubhandlers time. */
- }
- }
- }
- if (!upb_refcounted_freeze((upb_refcounted*const*)handlers, n, s,
- UPB_MAX_HANDLER_DEPTH)) {
- return false;
- }
- return true;
- }
- upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f) {
- switch (upb_fielddef_type(f)) {
- case UPB_TYPE_INT32:
- case UPB_TYPE_ENUM: return UPB_HANDLER_INT32;
- case UPB_TYPE_INT64: return UPB_HANDLER_INT64;
- case UPB_TYPE_UINT32: return UPB_HANDLER_UINT32;
- case UPB_TYPE_UINT64: return UPB_HANDLER_UINT64;
- case UPB_TYPE_FLOAT: return UPB_HANDLER_FLOAT;
- case UPB_TYPE_DOUBLE: return UPB_HANDLER_DOUBLE;
- case UPB_TYPE_BOOL: return UPB_HANDLER_BOOL;
- default: UPB_ASSERT(false); return -1; /* Invalid input. */
- }
- }
- bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type,
- upb_selector_t *s) {
- switch (type) {
- case UPB_HANDLER_INT32:
- case UPB_HANDLER_INT64:
- case UPB_HANDLER_UINT32:
- case UPB_HANDLER_UINT64:
- case UPB_HANDLER_FLOAT:
- case UPB_HANDLER_DOUBLE:
- case UPB_HANDLER_BOOL:
- if (!upb_fielddef_isprimitive(f) ||
- upb_handlers_getprimitivehandlertype(f) != type)
- return false;
- *s = f->selector_base;
- break;
- case UPB_HANDLER_STRING:
- if (upb_fielddef_isstring(f)) {
- *s = f->selector_base;
- } else if (upb_fielddef_lazy(f)) {
- *s = f->selector_base + 3;
- } else {
- return false;
- }
- break;
- case UPB_HANDLER_STARTSTR:
- if (upb_fielddef_isstring(f) || upb_fielddef_lazy(f)) {
- *s = f->selector_base + 1;
- } else {
- return false;
- }
- break;
- case UPB_HANDLER_ENDSTR:
- if (upb_fielddef_isstring(f) || upb_fielddef_lazy(f)) {
- *s = f->selector_base + 2;
- } else {
- return false;
- }
- break;
- case UPB_HANDLER_STARTSEQ:
- if (!upb_fielddef_isseq(f)) return false;
- *s = f->selector_base - 2;
- break;
- case UPB_HANDLER_ENDSEQ:
- if (!upb_fielddef_isseq(f)) return false;
- *s = f->selector_base - 1;
- break;
- case UPB_HANDLER_STARTSUBMSG:
- if (!upb_fielddef_issubmsg(f)) return false;
- /* Selectors for STARTSUBMSG are at the beginning of the table so that the
- * selector can also be used as an index into the "sub" array of
- * subhandlers. The indexes for the two into these two tables are the
- * same, except that in the handler table the static selectors come first. */
- *s = f->index_ + UPB_STATIC_SELECTOR_COUNT;
- break;
- case UPB_HANDLER_ENDSUBMSG:
- if (!upb_fielddef_issubmsg(f)) return false;
- *s = f->selector_base;
- break;
- }
- UPB_ASSERT((size_t)*s < upb_fielddef_containingtype(f)->selector_count);
- return true;
- }
- uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f) {
- return upb_fielddef_isseq(f) ? 2 : 0;
- }
- uint32_t upb_handlers_selectorcount(const upb_fielddef *f) {
- uint32_t ret = 1;
- if (upb_fielddef_isseq(f)) ret += 2; /* STARTSEQ/ENDSEQ */
- if (upb_fielddef_isstring(f)) ret += 2; /* [STRING]/STARTSTR/ENDSTR */
- if (upb_fielddef_issubmsg(f)) {
- /* ENDSUBMSG (STARTSUBMSG is at table beginning) */
- ret += 0;
- if (upb_fielddef_lazy(f)) {
- /* STARTSTR/ENDSTR/STRING (for lazy) */
- ret += 3;
- }
- }
- return ret;
- }
- /* upb_handlerattr ************************************************************/
- void upb_handlerattr_init(upb_handlerattr *attr) {
- upb_handlerattr from = UPB_HANDLERATTR_INITIALIZER;
- memcpy(attr, &from, sizeof(*attr));
- }
- void upb_handlerattr_uninit(upb_handlerattr *attr) {
- UPB_UNUSED(attr);
- }
- bool upb_handlerattr_sethandlerdata(upb_handlerattr *attr, const void *hd) {
- attr->handler_data_ = hd;
- return true;
- }
- bool upb_handlerattr_setclosuretype(upb_handlerattr *attr, const void *type) {
- attr->closure_type_ = type;
- return true;
- }
- const void *upb_handlerattr_closuretype(const upb_handlerattr *attr) {
- return attr->closure_type_;
- }
- bool upb_handlerattr_setreturnclosuretype(upb_handlerattr *attr,
- const void *type) {
- attr->return_closure_type_ = type;
- return true;
- }
- const void *upb_handlerattr_returnclosuretype(const upb_handlerattr *attr) {
- return attr->return_closure_type_;
- }
- bool upb_handlerattr_setalwaysok(upb_handlerattr *attr, bool alwaysok) {
- attr->alwaysok_ = alwaysok;
- return true;
- }
- bool upb_handlerattr_alwaysok(const upb_handlerattr *attr) {
- return attr->alwaysok_;
- }
- /* upb_bufhandle **************************************************************/
- size_t upb_bufhandle_objofs(const upb_bufhandle *h) {
- return h->objofs_;
- }
- /* upb_byteshandler ***********************************************************/
- void upb_byteshandler_init(upb_byteshandler* h) {
- memset(h, 0, sizeof(*h));
- }
- /* For when we support handlerfree callbacks. */
- void upb_byteshandler_uninit(upb_byteshandler* h) {
- UPB_UNUSED(h);
- }
- bool upb_byteshandler_setstartstr(upb_byteshandler *h,
- upb_startstr_handlerfunc *func, void *d) {
- h->table[UPB_STARTSTR_SELECTOR].func = (upb_func*)func;
- h->table[UPB_STARTSTR_SELECTOR].attr.handler_data_ = d;
- return true;
- }
- bool upb_byteshandler_setstring(upb_byteshandler *h,
- upb_string_handlerfunc *func, void *d) {
- h->table[UPB_STRING_SELECTOR].func = (upb_func*)func;
- h->table[UPB_STRING_SELECTOR].attr.handler_data_ = d;
- return true;
- }
- bool upb_byteshandler_setendstr(upb_byteshandler *h,
- upb_endfield_handlerfunc *func, void *d) {
- h->table[UPB_ENDSTR_SELECTOR].func = (upb_func*)func;
- h->table[UPB_ENDSTR_SELECTOR].attr.handler_data_ = d;
- return true;
- }
- /** Handlers for upb_msg ******************************************************/
- typedef struct {
- size_t offset;
- int32_t hasbit;
- } upb_msg_handlerdata;
- /* Fallback implementation if the handler is not specialized by the producer. */
- #define MSG_WRITER(type, ctype) \
- bool upb_msg_set ## type (void *c, const void *hd, ctype val) { \
- uint8_t *m = c; \
- const upb_msg_handlerdata *d = hd; \
- if (d->hasbit > 0) \
- *(uint8_t*)&m[d->hasbit / 8] |= 1 << (d->hasbit % 8); \
- *(ctype*)&m[d->offset] = val; \
- return true; \
- } \
- MSG_WRITER(double, double)
- MSG_WRITER(float, float)
- MSG_WRITER(int32, int32_t)
- MSG_WRITER(int64, int64_t)
- MSG_WRITER(uint32, uint32_t)
- MSG_WRITER(uint64, uint64_t)
- MSG_WRITER(bool, bool)
- bool upb_msg_setscalarhandler(upb_handlers *h, const upb_fielddef *f,
- size_t offset, int32_t hasbit) {
- upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
- bool ok;
- upb_msg_handlerdata *d = upb_gmalloc(sizeof(*d));
- if (!d) return false;
- d->offset = offset;
- d->hasbit = hasbit;
- upb_handlerattr_sethandlerdata(&attr, d);
- upb_handlerattr_setalwaysok(&attr, true);
- upb_handlers_addcleanup(h, d, upb_gfree);
- #define TYPE(u, l) \
- case UPB_TYPE_##u: \
- ok = upb_handlers_set##l(h, f, upb_msg_set##l, &attr); break;
- ok = false;
- switch (upb_fielddef_type(f)) {
- TYPE(INT64, int64);
- TYPE(INT32, int32);
- TYPE(ENUM, int32);
- TYPE(UINT64, uint64);
- TYPE(UINT32, uint32);
- TYPE(DOUBLE, double);
- TYPE(FLOAT, float);
- TYPE(BOOL, bool);
- default: UPB_ASSERT(false); break;
- }
- #undef TYPE
- upb_handlerattr_uninit(&attr);
- return ok;
- }
- bool upb_msg_getscalarhandlerdata(const upb_handlers *h,
- upb_selector_t s,
- upb_fieldtype_t *type,
- size_t *offset,
- int32_t *hasbit) {
- const upb_msg_handlerdata *d;
- upb_func *f = upb_handlers_gethandler(h, s);
- if ((upb_int64_handlerfunc*)f == upb_msg_setint64) {
- *type = UPB_TYPE_INT64;
- } else if ((upb_int32_handlerfunc*)f == upb_msg_setint32) {
- *type = UPB_TYPE_INT32;
- } else if ((upb_uint64_handlerfunc*)f == upb_msg_setuint64) {
- *type = UPB_TYPE_UINT64;
- } else if ((upb_uint32_handlerfunc*)f == upb_msg_setuint32) {
- *type = UPB_TYPE_UINT32;
- } else if ((upb_double_handlerfunc*)f == upb_msg_setdouble) {
- *type = UPB_TYPE_DOUBLE;
- } else if ((upb_float_handlerfunc*)f == upb_msg_setfloat) {
- *type = UPB_TYPE_FLOAT;
- } else if ((upb_bool_handlerfunc*)f == upb_msg_setbool) {
- *type = UPB_TYPE_BOOL;
- } else {
- return false;
- }
- d = upb_handlers_gethandlerdata(h, s);
- *offset = d->offset;
- *hasbit = d->hasbit;
- return true;
- }
- bool upb_fieldtype_mapkeyok(upb_fieldtype_t type) {
- return type == UPB_TYPE_BOOL || type == UPB_TYPE_INT32 ||
- type == UPB_TYPE_UINT32 || type == UPB_TYPE_INT64 ||
- type == UPB_TYPE_UINT64 || type == UPB_TYPE_STRING;
- }
- #define PTR_AT(msg, ofs, type) (type*)((char*)msg + ofs)
- #define VOIDPTR_AT(msg, ofs) PTR_AT(msg, ofs, void)
- #define ENCODE_MAX_NESTING 64
- #define CHECK_TRUE(x) if (!(x)) { return false; }
- /** upb_msgval ****************************************************************/
- #define upb_alignof(t) offsetof(struct { char c; t x; }, x)
- /* These functions will generate real memcpy() calls on ARM sadly, because
- * the compiler assumes they might not be aligned. */
- static upb_msgval upb_msgval_read(const void *p, size_t ofs,
- uint8_t size) {
- upb_msgval val;
- p = (char*)p + ofs;
- memcpy(&val, p, size);
- return val;
- }
- static void upb_msgval_write(void *p, size_t ofs, upb_msgval val,
- uint8_t size) {
- p = (char*)p + ofs;
- memcpy(p, &val, size);
- }
- static size_t upb_msgval_sizeof(upb_fieldtype_t type) {
- switch (type) {
- case UPB_TYPE_DOUBLE:
- case UPB_TYPE_INT64:
- case UPB_TYPE_UINT64:
- return 8;
- case UPB_TYPE_ENUM:
- case UPB_TYPE_INT32:
- case UPB_TYPE_UINT32:
- case UPB_TYPE_FLOAT:
- return 4;
- case UPB_TYPE_BOOL:
- return 1;
- case UPB_TYPE_MESSAGE:
- return sizeof(void*);
- case UPB_TYPE_BYTES:
- case UPB_TYPE_STRING:
- return sizeof(upb_strview);
- }
- UPB_UNREACHABLE();
- }
- static uint8_t upb_msg_fieldsize(const upb_msglayout_field *field) {
- if (field->label == UPB_LABEL_REPEATED) {
- return sizeof(void*);
- } else {
- return upb_msgval_sizeof(upb_desctype_to_fieldtype[field->descriptortype]);
- }
- }
- /* TODO(haberman): this is broken right now because upb_msgval can contain
- * a char* / size_t pair, which is too big for a upb_value. To fix this
- * we'll probably need to dynamically allocate a upb_msgval and store a
- * pointer to that in the tables for extensions/maps. */
- static upb_value upb_toval(upb_msgval val) {
- upb_value ret;
- UPB_UNUSED(val);
- memset(&ret, 0, sizeof(upb_value)); /* XXX */
- return ret;
- }
- static upb_msgval upb_msgval_fromval(upb_value val) {
- upb_msgval ret;
- UPB_UNUSED(val);
- memset(&ret, 0, sizeof(upb_msgval)); /* XXX */
- return ret;
- }
- static upb_ctype_t upb_fieldtotabtype(upb_fieldtype_t type) {
- switch (type) {
- case UPB_TYPE_FLOAT: return UPB_CTYPE_FLOAT;
- case UPB_TYPE_DOUBLE: return UPB_CTYPE_DOUBLE;
- case UPB_TYPE_BOOL: return UPB_CTYPE_BOOL;
- case UPB_TYPE_BYTES:
- case UPB_TYPE_MESSAGE:
- case UPB_TYPE_STRING: return UPB_CTYPE_CONSTPTR;
- case UPB_TYPE_ENUM:
- case UPB_TYPE_INT32: return UPB_CTYPE_INT32;
- case UPB_TYPE_UINT32: return UPB_CTYPE_UINT32;
- case UPB_TYPE_INT64: return UPB_CTYPE_INT64;
- case UPB_TYPE_UINT64: return UPB_CTYPE_UINT64;
- default: UPB_ASSERT(false); return 0;
- }
- }
- /** upb_msg *******************************************************************/
- /* If we always read/write as a consistent type to each address, this shouldn't
- * violate aliasing.
- */
- #define DEREF(msg, ofs, type) *PTR_AT(msg, ofs, type)
- /* Internal members of a upb_msg. We can change this without breaking binary
- * compatibility. We put these before the user's data. The user's upb_msg*
- * points after the upb_msg_internal. */
- /* Used when a message is not extendable. */
- typedef struct {
- /* TODO(haberman): use pointer tagging so we we are slim when known unknown
- * fields are not present. */
- upb_arena *arena;
- char *unknown;
- size_t unknown_len;
- size_t unknown_size;
- } upb_msg_internal;
- /* Used when a message is extendable. */
- typedef struct {
- upb_inttable *extdict;
- upb_msg_internal base;
- } upb_msg_internal_withext;
- static int upb_msg_internalsize(const upb_msglayout *l) {
- return sizeof(upb_msg_internal) - l->extendable * sizeof(void *);
- }
- static upb_msg_internal *upb_msg_getinternal(upb_msg *msg) {
- return VOIDPTR_AT(msg, -sizeof(upb_msg_internal));
- }
- static const upb_msg_internal *upb_msg_getinternal_const(const upb_msg *msg) {
- return VOIDPTR_AT(msg, -sizeof(upb_msg_internal));
- }
- static upb_msg_internal_withext *upb_msg_getinternalwithext(
- upb_msg *msg, const upb_msglayout *l) {
- UPB_ASSERT(l->extendable);
- return VOIDPTR_AT(msg, -sizeof(upb_msg_internal_withext));
- }
- void upb_msg_addunknown(upb_msg *msg, const char *data, size_t len) {
- upb_msg_internal* in = upb_msg_getinternal(msg);
- if (len > in->unknown_size - in->unknown_len) {
- upb_alloc *alloc = upb_arena_alloc(in->arena);
- size_t need = in->unknown_size + len;
- size_t newsize = UPB_MAX(in->unknown_size * 2, need);
- in->unknown = upb_realloc(alloc, in->unknown, in->unknown_size, newsize);
- in->unknown_size = newsize;
- }
- memcpy(in->unknown + in->unknown_len, data, len);
- in->unknown_len += len;
- }
- const char *upb_msg_getunknown(const upb_msg *msg, size_t *len) {
- const upb_msg_internal* in = upb_msg_getinternal_const(msg);
- *len = in->unknown_len;
- return in->unknown;
- }
- static const upb_msglayout_field *upb_msg_checkfield(int field_index,
- const upb_msglayout *l) {
- UPB_ASSERT(field_index >= 0 && field_index < l->field_count);
- return &l->fields[field_index];
- }
- static bool upb_msg_inoneof(const upb_msglayout_field *field) {
- return field->presence < 0;
- }
- static uint32_t *upb_msg_oneofcase(const upb_msg *msg, int field_index,
- const upb_msglayout *l) {
- const upb_msglayout_field *field = upb_msg_checkfield(field_index, l);
- UPB_ASSERT(upb_msg_inoneof(field));
- return PTR_AT(msg, ~field->presence, uint32_t);
- }
- static size_t upb_msg_sizeof(const upb_msglayout *l) {
- return l->size + upb_msg_internalsize(l);
- }
- upb_msg *upb_msg_new(const upb_msglayout *l, upb_arena *a) {
- upb_alloc *alloc = upb_arena_alloc(a);
- void *mem = upb_malloc(alloc, upb_msg_sizeof(l));
- upb_msg_internal *in;
- upb_msg *msg;
- if (!mem) {
- return NULL;
- }
- msg = VOIDPTR_AT(mem, upb_msg_internalsize(l));
- /* Initialize normal members. */
- memset(msg, 0, l->size);
- /* Initialize internal members. */
- in = upb_msg_getinternal(msg);
- in->arena = a;
- in->unknown = NULL;
- in->unknown_len = 0;
- in->unknown_size = 0;
- if (l->extendable) {
- upb_msg_getinternalwithext(msg, l)->extdict = NULL;
- }
- return msg;
- }
- upb_arena *upb_msg_arena(const upb_msg *msg) {
- return upb_msg_getinternal_const(msg)->arena;
- }
- bool upb_msg_has(const upb_msg *msg,
- int field_index,
- const upb_msglayout *l) {
- const upb_msglayout_field *field = upb_msg_checkfield(field_index, l);
- UPB_ASSERT(field->presence);
- if (upb_msg_inoneof(field)) {
- /* Oneofs are set when the oneof number is set to this field. */
- return *upb_msg_oneofcase(msg, field_index, l) == field->number;
- } else {
- /* Other fields are set when their hasbit is set. */
- uint32_t hasbit = field->presence;
- return DEREF(msg, hasbit / 8, char) | (1 << (hasbit % 8));
- }
- }
- upb_msgval upb_msg_get(const upb_msg *msg, int field_index,
- const upb_msglayout *l) {
- const upb_msglayout_field *field = upb_msg_checkfield(field_index, l);
- int size = upb_msg_fieldsize(field);
- return upb_msgval_read(msg, field->offset, size);
- }
- void upb_msg_set(upb_msg *msg, int field_index, upb_msgval val,
- const upb_msglayout *l) {
- const upb_msglayout_field *field = upb_msg_checkfield(field_index, l);
- int size = upb_msg_fieldsize(field);
- upb_msgval_write(msg, field->offset, val, size);
- }
- /** upb_array *****************************************************************/
- #define DEREF_ARR(arr, i, type) ((type*)arr->data)[i]
- upb_array *upb_array_new(upb_fieldtype_t type, upb_arena *a) {
- upb_alloc *alloc = upb_arena_alloc(a);
- upb_array *ret = upb_malloc(alloc, sizeof(upb_array));
- if (!ret) {
- return NULL;
- }
- ret->type = type;
- ret->data = NULL;
- ret->len = 0;
- ret->size = 0;
- ret->element_size = upb_msgval_sizeof(type);
- ret->arena = a;
- return ret;
- }
- size_t upb_array_size(const upb_array *arr) {
- return arr->len;
- }
- upb_fieldtype_t upb_array_type(const upb_array *arr) {
- return arr->type;
- }
- upb_msgval upb_array_get(const upb_array *arr, size_t i) {
- UPB_ASSERT(i < arr->len);
- return upb_msgval_read(arr->data, i * arr->element_size, arr->element_size);
- }
- bool upb_array_set(upb_array *arr, size_t i, upb_msgval val) {
- UPB_ASSERT(i <= arr->len);
- if (i == arr->len) {
- /* Extending the array. */
- if (i == arr->size) {
- /* Need to reallocate. */
- size_t new_size = UPB_MAX(arr->size * 2, 8);
- size_t new_bytes = new_size * arr->element_size;
- size_t old_bytes = arr->size * arr->element_size;
- upb_alloc *alloc = upb_arena_alloc(arr->arena);
- upb_msgval *new_data =
- upb_realloc(alloc, arr->data, old_bytes, new_bytes);
- if (!new_data) {
- return false;
- }
- arr->data = new_data;
- arr->size = new_size;
- }
- arr->len = i + 1;
- }
- upb_msgval_write(arr->data, i * arr->element_size, val, arr->element_size);
- return true;
- }
- /** upb_map *******************************************************************/
- struct upb_map {
- upb_fieldtype_t key_type;
- upb_fieldtype_t val_type;
- /* We may want to optimize this to use inttable where possible, for greater
- * efficiency and lower memory footprint. */
- upb_strtable strtab;
- upb_arena *arena;
- };
- static void upb_map_tokey(upb_fieldtype_t type, upb_msgval *key,
- const char **out_key, size_t *out_len) {
- switch (type) {
- case UPB_TYPE_STRING:
- /* Point to string data of the input key. */
- *out_key = key->str.data;
- *out_len = key->str.size;
- return;
- case UPB_TYPE_BOOL:
- case UPB_TYPE_INT32:
- case UPB_TYPE_UINT32:
- case UPB_TYPE_INT64:
- case UPB_TYPE_UINT64:
- /* Point to the key itself. XXX: big-endian. */
- *out_key = (const char*)key;
- *out_len = upb_msgval_sizeof(type);
- return;
- case UPB_TYPE_BYTES:
- case UPB_TYPE_DOUBLE:
- case UPB_TYPE_ENUM:
- case UPB_TYPE_FLOAT:
- case UPB_TYPE_MESSAGE:
- break; /* Cannot be a map key. */
- }
- UPB_UNREACHABLE();
- }
- static upb_msgval upb_map_fromkey(upb_fieldtype_t type, const char *key,
- size_t len) {
- switch (type) {
- case UPB_TYPE_STRING:
- return upb_msgval_makestr(key, len);
- case UPB_TYPE_BOOL:
- case UPB_TYPE_INT32:
- case UPB_TYPE_UINT32:
- case UPB_TYPE_INT64:
- case UPB_TYPE_UINT64:
- return upb_msgval_read(key, 0, upb_msgval_sizeof(type));
- case UPB_TYPE_BYTES:
- case UPB_TYPE_DOUBLE:
- case UPB_TYPE_ENUM:
- case UPB_TYPE_FLOAT:
- case UPB_TYPE_MESSAGE:
- break; /* Cannot be a map key. */
- }
- UPB_UNREACHABLE();
- }
- upb_map *upb_map_new(upb_fieldtype_t ktype, upb_fieldtype_t vtype,
- upb_arena *a) {
- upb_ctype_t vtabtype = upb_fieldtotabtype(vtype);
- upb_alloc *alloc = upb_arena_alloc(a);
- upb_map *map = upb_malloc(alloc, sizeof(upb_map));
- if (!map) {
- return NULL;
- }
- UPB_ASSERT(upb_fieldtype_mapkeyok(ktype));
- map->key_type = ktype;
- map->val_type = vtype;
- map->arena = a;
- if (!upb_strtable_init2(&map->strtab, vtabtype, alloc)) {
- return NULL;
- }
- return map;
- }
- size_t upb_map_size(const upb_map *map) {
- return upb_strtable_count(&map->strtab);
- }
- upb_fieldtype_t upb_map_keytype(const upb_map *map) {
- return map->key_type;
- }
- upb_fieldtype_t upb_map_valuetype(const upb_map *map) {
- return map->val_type;
- }
- bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val) {
- upb_value tabval;
- const char *key_str;
- size_t key_len;
- bool ret;
- upb_map_tokey(map->key_type, &key, &key_str, &key_len);
- ret = upb_strtable_lookup2(&map->strtab, key_str, key_len, &tabval);
- if (ret) {
- memcpy(val, &tabval, sizeof(tabval));
- }
- return ret;
- }
- bool upb_map_set(upb_map *map, upb_msgval key, upb_msgval val,
- upb_msgval *removed) {
- const char *key_str;
- size_t key_len;
- upb_value tabval = upb_toval(val);
- upb_value removedtabval;
- upb_alloc *a = upb_arena_alloc(map->arena);
- upb_map_tokey(map->key_type, &key, &key_str, &key_len);
- /* TODO(haberman): add overwrite operation to minimize number of lookups. */
- if (upb_strtable_lookup2(&map->strtab, key_str, key_len, NULL)) {
- upb_strtable_remove3(&map->strtab, key_str, key_len, &removedtabval, a);
- memcpy(&removed, &removedtabval, sizeof(removed));
- }
- return upb_strtable_insert3(&map->strtab, key_str, key_len, tabval, a);
- }
- bool upb_map_del(upb_map *map, upb_msgval key) {
- const char *key_str;
- size_t key_len;
- upb_alloc *a = upb_arena_alloc(map->arena);
- upb_map_tokey(map->key_type, &key, &key_str, &key_len);
- return upb_strtable_remove3(&map->strtab, key_str, key_len, NULL, a);
- }
- /** upb_mapiter ***************************************************************/
- struct upb_mapiter {
- upb_strtable_iter iter;
- upb_fieldtype_t key_type;
- };
- size_t upb_mapiter_sizeof() {
- return sizeof(upb_mapiter);
- }
- void upb_mapiter_begin(upb_mapiter *i, const upb_map *map) {
- upb_strtable_begin(&i->iter, &map->strtab);
- i->key_type = map->key_type;
- }
- upb_mapiter *upb_mapiter_new(const upb_map *t, upb_alloc *a) {
- upb_mapiter *ret = upb_malloc(a, upb_mapiter_sizeof());
- if (!ret) {
- return NULL;
- }
- upb_mapiter_begin(ret, t);
- return ret;
- }
- void upb_mapiter_free(upb_mapiter *i, upb_alloc *a) {
- upb_free(a, i);
- }
- void upb_mapiter_next(upb_mapiter *i) {
- upb_strtable_next(&i->iter);
- }
- bool upb_mapiter_done(const upb_mapiter *i) {
- return upb_strtable_done(&i->iter);
- }
- upb_msgval upb_mapiter_key(const upb_mapiter *i) {
- return upb_map_fromkey(i->key_type, upb_strtable_iter_key(&i->iter),
- upb_strtable_iter_keylength(&i->iter));
- }
- upb_msgval upb_mapiter_value(const upb_mapiter *i) {
- return upb_msgval_fromval(upb_strtable_iter_value(&i->iter));
- }
- void upb_mapiter_setdone(upb_mapiter *i) {
- upb_strtable_iter_setdone(&i->iter);
- }
- bool upb_mapiter_isequal(const upb_mapiter *i1, const upb_mapiter *i2) {
- return upb_strtable_iter_isequal(&i1->iter, &i2->iter);
- }
- static bool is_power_of_two(size_t val) {
- return (val & (val - 1)) == 0;
- }
- /* Align up to the given power of 2. */
- static size_t align_up(size_t val, size_t align) {
- UPB_ASSERT(is_power_of_two(align));
- return (val + align - 1) & ~(align - 1);
- }
- static size_t div_round_up(size_t n, size_t d) {
- return (n + d - 1) / d;
- }
- static size_t upb_msgval_sizeof2(upb_fieldtype_t type) {
- switch (type) {
- case UPB_TYPE_DOUBLE:
- case UPB_TYPE_INT64:
- case UPB_TYPE_UINT64:
- return 8;
- case UPB_TYPE_ENUM:
- case UPB_TYPE_INT32:
- case UPB_TYPE_UINT32:
- case UPB_TYPE_FLOAT:
- return 4;
- case UPB_TYPE_BOOL:
- return 1;
- case UPB_TYPE_MESSAGE:
- return sizeof(void*);
- case UPB_TYPE_BYTES:
- case UPB_TYPE_STRING:
- return sizeof(upb_strview);
- }
- UPB_UNREACHABLE();
- }
- static uint8_t upb_msg_fielddefsize(const upb_fielddef *f) {
- if (upb_fielddef_isseq(f)) {
- return sizeof(void*);
- } else {
- return upb_msgval_sizeof2(upb_fielddef_type(f));
- }
- }
- /** upb_msglayout *************************************************************/
- static void upb_msglayout_free(upb_msglayout *l) {
- upb_gfree(l);
- }
- static size_t upb_msglayout_place(upb_msglayout *l, size_t size) {
- size_t ret;
- l->size = align_up(l->size, size);
- ret = l->size;
- l->size += size;
- return ret;
- }
- static bool upb_msglayout_init(const upb_msgdef *m,
- upb_msglayout *l,
- upb_msgfactory *factory) {
- upb_msg_field_iter it;
- upb_msg_oneof_iter oit;
- size_t hasbit;
- size_t submsg_count = 0;
- const upb_msglayout **submsgs;
- upb_msglayout_field *fields;
- for (upb_msg_field_begin(&it, m);
- !upb_msg_field_done(&it);
- upb_msg_field_next(&it)) {
- const upb_fielddef* f = upb_msg_iter_field(&it);
- if (upb_fielddef_issubmsg(f)) {
- submsg_count++;
- }
- }
- memset(l, 0, sizeof(*l));
- fields = upb_gmalloc(upb_msgdef_numfields(m) * sizeof(*fields));
- submsgs = upb_gmalloc(submsg_count * sizeof(*submsgs));
- if ((!fields && upb_msgdef_numfields(m)) ||
- (!submsgs && submsg_count)) {
- /* OOM. */
- upb_gfree(fields);
- upb_gfree(submsgs);
- return false;
- }
- l->field_count = upb_msgdef_numfields(m);
- l->fields = fields;
- l->submsgs = submsgs;
- /* Allocate data offsets in three stages:
- *
- * 1. hasbits.
- * 2. regular fields.
- * 3. oneof fields.
- *
- * OPT: There is a lot of room for optimization here to minimize the size.
- */
- /* Allocate hasbits and set basic field attributes. */
- submsg_count = 0;
- for (upb_msg_field_begin(&it, m), hasbit = 0;
- !upb_msg_field_done(&it);
- upb_msg_field_next(&it)) {
- const upb_fielddef* f = upb_msg_iter_field(&it);
- upb_msglayout_field *field = &fields[upb_fielddef_index(f)];
- field->number = upb_fielddef_number(f);
- field->descriptortype = upb_fielddef_descriptortype(f);
- field->label = upb_fielddef_label(f);
- if (upb_fielddef_issubmsg(f)) {
- const upb_msglayout *sub_layout =
- upb_msgfactory_getlayout(factory, upb_fielddef_msgsubdef(f));
- field->submsg_index = submsg_count++;
- submsgs[field->submsg_index] = sub_layout;
- }
- if (upb_fielddef_haspresence(f) && !upb_fielddef_containingoneof(f)) {
- field->presence = (hasbit++);
- } else {
- field->presence = 0;
- }
- }
- /* Account for space used by hasbits. */
- l->size = div_round_up(hasbit, 8);
- /* Allocate non-oneof fields. */
- for (upb_msg_field_begin(&it, m); !upb_msg_field_done(&it);
- upb_msg_field_next(&it)) {
- const upb_fielddef* f = upb_msg_iter_field(&it);
- size_t field_size = upb_msg_fielddefsize(f);
- size_t index = upb_fielddef_index(f);
- if (upb_fielddef_containingoneof(f)) {
- /* Oneofs are handled separately below. */
- continue;
- }
- fields[index].offset = upb_msglayout_place(l, field_size);
- }
- /* Allocate oneof fields. Each oneof field consists of a uint32 for the case
- * and space for the actual data. */
- for (upb_msg_oneof_begin(&oit, m); !upb_msg_oneof_done(&oit);
- upb_msg_oneof_next(&oit)) {
- const upb_oneofdef* o = upb_msg_iter_oneof(&oit);
- upb_oneof_iter fit;
- size_t case_size = sizeof(uint32_t); /* Could potentially optimize this. */
- size_t field_size = 0;
- uint32_t case_offset;
- uint32_t data_offset;
- /* Calculate field size: the max of all field sizes. */
- for (upb_oneof_begin(&fit, o);
- !upb_oneof_done(&fit);
- upb_oneof_next(&fit)) {
- const upb_fielddef* f = upb_oneof_iter_field(&fit);
- field_size = UPB_MAX(field_size, upb_msg_fielddefsize(f));
- }
- /* Align and allocate case offset. */
- case_offset = upb_msglayout_place(l, case_size);
- data_offset = upb_msglayout_place(l, field_size);
- for (upb_oneof_begin(&fit, o);
- !upb_oneof_done(&fit);
- upb_oneof_next(&fit)) {
- const upb_fielddef* f = upb_oneof_iter_field(&fit);
- fields[upb_fielddef_index(f)].offset = data_offset;
- fields[upb_fielddef_index(f)].presence = ~case_offset;
- }
- }
- /* Size of the entire structure should be a multiple of its greatest
- * alignment. TODO: track overall alignment for real? */
- l->size = align_up(l->size, 8);
- return true;
- }
- /** upb_msgfactory ************************************************************/
- struct upb_msgfactory {
- const upb_symtab *symtab; /* We own a ref. */
- upb_inttable layouts;
- upb_inttable mergehandlers;
- };
- upb_msgfactory *upb_msgfactory_new(const upb_symtab *symtab) {
- upb_msgfactory *ret = upb_gmalloc(sizeof(*ret));
- ret->symtab = symtab;
- upb_inttable_init(&ret->layouts, UPB_CTYPE_PTR);
- upb_inttable_init(&ret->mergehandlers, UPB_CTYPE_CONSTPTR);
- return ret;
- }
- void upb_msgfactory_free(upb_msgfactory *f) {
- upb_inttable_iter i;
- upb_inttable_begin(&i, &f->layouts);
- for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
- upb_msglayout *l = upb_value_getptr(upb_inttable_iter_value(&i));
- upb_msglayout_free(l);
- }
- upb_inttable_begin(&i, &f->mergehandlers);
- for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
- const upb_handlers *h = upb_value_getconstptr(upb_inttable_iter_value(&i));
- upb_handlers_unref(h, f);
- }
- upb_inttable_uninit(&f->layouts);
- upb_inttable_uninit(&f->mergehandlers);
- upb_gfree(f);
- }
- const upb_symtab *upb_msgfactory_symtab(const upb_msgfactory *f) {
- return f->symtab;
- }
- const upb_msglayout *upb_msgfactory_getlayout(upb_msgfactory *f,
- const upb_msgdef *m) {
- upb_value v;
- UPB_ASSERT(upb_symtab_lookupmsg(f->symtab, upb_msgdef_fullname(m)) == m);
- UPB_ASSERT(!upb_msgdef_mapentry(m));
- if (upb_inttable_lookupptr(&f->layouts, m, &v)) {
- UPB_ASSERT(upb_value_getptr(v));
- return upb_value_getptr(v);
- } else {
- /* In case of circular dependency, layout has to be inserted first. */
- upb_msglayout *l = upb_gmalloc(sizeof(*l));
- upb_msgfactory *mutable_f = (void*)f;
- upb_inttable_insertptr(&mutable_f->layouts, m, upb_value_ptr(l));
- UPB_ASSERT(l);
- if (!upb_msglayout_init(m, l, f)) {
- upb_msglayout_free(l);
- }
- return l;
- }
- }
- #if UINTPTR_MAX == 0xffffffff
- #define UPB_SIZE(size32, size64) size32
- #else
- #define UPB_SIZE(size32, size64) size64
- #endif
- #define UPB_FIELD_AT(msg, fieldtype, offset) \
- *(fieldtype*)((const char*)(msg) + offset)
- #define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \
- UPB_FIELD_AT(msg, int, case_offset) == case_val \
- ? UPB_FIELD_AT(msg, fieldtype, offset) \
- : default
- #define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \
- UPB_FIELD_AT(msg, int, case_offset) = case_val; \
- UPB_FIELD_AT(msg, fieldtype, offset) = value;
- #undef UPB_SIZE
- #undef UPB_FIELD_AT
- #undef UPB_READ_ONEOF
- #undef UPB_WRITE_ONEOF
- /*
- ** upb::RefCounted Implementation
- **
- ** Our key invariants are:
- ** 1. reference cycles never span groups
- ** 2. for ref2(to, from), we increment to's count iff group(from) != group(to)
- **
- ** The previous two are how we avoid leaking cycles. Other important
- ** invariants are:
- ** 3. for mutable objects "from" and "to", if there exists a ref2(to, from)
- ** this implies group(from) == group(to). (In practice, what we implement
- ** is even stronger; "from" and "to" will share a group if there has *ever*
- ** been a ref2(to, from), but all that is necessary for correctness is the
- ** weaker one).
- ** 4. mutable and immutable objects are never in the same group.
- */
- #include <setjmp.h>
- static void freeobj(upb_refcounted *o);
- const char untracked_val;
- const void *UPB_UNTRACKED_REF = &untracked_val;
- /* arch-specific atomic primitives *******************************************/
- #ifdef UPB_THREAD_UNSAFE /*---------------------------------------------------*/
- static void atomic_inc(uint32_t *a) { (*a)++; }
- static bool atomic_dec(uint32_t *a) { return --(*a) == 0; }
- #elif defined(__GNUC__) || defined(__clang__) /*------------------------------*/
- static void atomic_inc(uint32_t *a) { __sync_fetch_and_add(a, 1); }
- static bool atomic_dec(uint32_t *a) { return __sync_sub_and_fetch(a, 1) == 0; }
- #elif defined(WIN32) /*-------------------------------------------------------*/
- #include <Windows.h>
- static void atomic_inc(upb_atomic_t *a) { InterlockedIncrement(&a->val); }
- static bool atomic_dec(upb_atomic_t *a) {
- return InterlockedDecrement(&a->val) == 0;
- }
- #else
- #error Atomic primitives not defined for your platform/CPU. \
- Implement them or compile with UPB_THREAD_UNSAFE.
- #endif
- /* All static objects point to this refcount.
- * It is special-cased in ref/unref below. */
- uint32_t static_refcount = -1;
- /* We can avoid atomic ops for statically-declared objects.
- * This is a minor optimization but nice since we can avoid degrading under
- * contention in this case. */
- static void refgroup(uint32_t *group) {
- if (group != &static_refcount)
- atomic_inc(group);
- }
- static bool unrefgroup(uint32_t *group) {
- if (group == &static_refcount) {
- return false;
- } else {
- return atomic_dec(group);
- }
- }
- /* Reference tracking (debug only) ********************************************/
- #ifdef UPB_DEBUG_REFS
- #ifdef UPB_THREAD_UNSAFE
- static void upb_lock() {}
- static void upb_unlock() {}
- #else
- /* User must define functions that lock/unlock a global mutex and link this
- * file against them. */
- void upb_lock();
- void upb_unlock();
- #endif
- /* UPB_DEBUG_REFS mode counts on being able to malloc() memory in some
- * code-paths that can normally never fail, like upb_refcounted_ref(). Since
- * we have no way to propagage out-of-memory errors back to the user, and since
- * these errors can only occur in UPB_DEBUG_REFS mode, we use an allocator that
- * immediately aborts on failure (avoiding the global allocator, which might
- * inject failures). */
- #include <stdlib.h>
- static void *upb_debugrefs_allocfunc(upb_alloc *alloc, void *ptr,
- size_t oldsize, size_t size) {
- UPB_UNUSED(alloc);
- UPB_UNUSED(oldsize);
- if (size == 0) {
- free(ptr);
- return NULL;
- } else {
- void *ret = realloc(ptr, size);
- if (!ret) {
- abort();
- }
- return ret;
- }
- }
- upb_alloc upb_alloc_debugrefs = {&upb_debugrefs_allocfunc};
- typedef struct {
- int count; /* How many refs there are (duplicates only allowed for ref2). */
- bool is_ref2;
- } trackedref;
- static trackedref *trackedref_new(bool is_ref2) {
- trackedref *ret = upb_malloc(&upb_alloc_debugrefs, sizeof(*ret));
- ret->count = 1;
- ret->is_ref2 = is_ref2;
- return ret;
- }
- static void track(const upb_refcounted *r, const void *owner, bool ref2) {
- upb_value v;
- UPB_ASSERT(owner);
- if (owner == UPB_UNTRACKED_REF) return;
- upb_lock();
- if (upb_inttable_lookupptr(r->refs, owner, &v)) {
- trackedref *ref = upb_value_getptr(v);
- /* Since we allow multiple ref2's for the same to/from pair without
- * allocating separate memory for each one, we lose the fine-grained
- * tracking behavior we get with regular refs. Since ref2s only happen
- * inside upb, we'll accept this limitation until/unless there is a really
- * difficult upb-internal bug that can't be figured out without it. */
- UPB_ASSERT(ref2);
- UPB_ASSERT(ref->is_ref2);
- ref->count++;
- } else {
- trackedref *ref = trackedref_new(ref2);
- upb_inttable_insertptr2(r->refs, owner, upb_value_ptr(ref),
- &upb_alloc_debugrefs);
- if (ref2) {
- /* We know this cast is safe when it is a ref2, because it's coming from
- * another refcounted object. */
- const upb_refcounted *from = owner;
- UPB_ASSERT(!upb_inttable_lookupptr(from->ref2s, r, NULL));
- upb_inttable_insertptr2(from->ref2s, r, upb_value_ptr(NULL),
- &upb_alloc_debugrefs);
- }
- }
- upb_unlock();
- }
- static void untrack(const upb_refcounted *r, const void *owner, bool ref2) {
- upb_value v;
- bool found;
- trackedref *ref;
- UPB_ASSERT(owner);
- if (owner == UPB_UNTRACKED_REF) return;
- upb_lock();
- found = upb_inttable_lookupptr(r->refs, owner, &v);
- /* This assert will fail if an owner attempts to release a ref it didn't have. */
- UPB_ASSERT(found);
- ref = upb_value_getptr(v);
- UPB_ASSERT(ref->is_ref2 == ref2);
- if (--ref->count == 0) {
- free(ref);
- upb_inttable_removeptr(r->refs, owner, NULL);
- if (ref2) {
- /* We know this cast is safe when it is a ref2, because it's coming from
- * another refcounted object. */
- const upb_refcounted *from = owner;
- bool removed = upb_inttable_removeptr(from->ref2s, r, NULL);
- UPB_ASSERT(removed);
- }
- }
- upb_unlock();
- }
- static void checkref(const upb_refcounted *r, const void *owner, bool ref2) {
- upb_value v;
- bool found;
- trackedref *ref;
- upb_lock();
- found = upb_inttable_lookupptr(r->refs, owner, &v);
- UPB_ASSERT(found);
- ref = upb_value_getptr(v);
- UPB_ASSERT(ref->is_ref2 == ref2);
- upb_unlock();
- }
- /* Populates the given UPB_CTYPE_INT32 inttable with counts of ref2's that
- * originate from the given owner. */
- static void getref2s(const upb_refcounted *owner, upb_inttable *tab) {
- upb_inttable_iter i;
- upb_lock();
- upb_inttable_begin(&i, owner->ref2s);
- for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
- upb_value v;
- upb_value count;
- trackedref *ref;
- bool found;
- upb_refcounted *to = (upb_refcounted*)upb_inttable_iter_key(&i);
- /* To get the count we need to look in the target's table. */
- found = upb_inttable_lookupptr(to->refs, owner, &v);
- UPB_ASSERT(found);
- ref = upb_value_getptr(v);
- count = upb_value_int32(ref->count);
- upb_inttable_insertptr2(tab, to, count, &upb_alloc_debugrefs);
- }
- upb_unlock();
- }
- typedef struct {
- upb_inttable ref2;
- const upb_refcounted *obj;
- } check_state;
- static void visit_check(const upb_refcounted *obj, const upb_refcounted *subobj,
- void *closure) {
- check_state *s = closure;
- upb_inttable *ref2 = &s->ref2;
- upb_value v;
- bool removed;
- int32_t newcount;
- UPB_ASSERT(obj == s->obj);
- UPB_ASSERT(subobj);
- removed = upb_inttable_removeptr(ref2, subobj, &v);
- /* The following assertion will fail if the visit() function visits a subobj
- * that it did not have a ref2 on, or visits the same subobj too many times. */
- UPB_ASSERT(removed);
- newcount = upb_value_getint32(v) - 1;
- if (newcount > 0) {
- upb_inttable_insert2(ref2, (uintptr_t)subobj, upb_value_int32(newcount),
- &upb_alloc_debugrefs);
- }
- }
- static void visit(const upb_refcounted *r, upb_refcounted_visit *v,
- void *closure) {
- /* In DEBUG_REFS mode we know what existing ref2 refs there are, so we know
- * exactly the set of nodes that visit() should visit. So we verify visit()'s
- * correctness here. */
- check_state state;
- state.obj = r;
- upb_inttable_init2(&state.ref2, UPB_CTYPE_INT32, &upb_alloc_debugrefs);
- getref2s(r, &state.ref2);
- /* This should visit any children in the ref2 table. */
- if (r->vtbl->visit) r->vtbl->visit(r, visit_check, &state);
- /* This assertion will fail if the visit() function missed any children. */
- UPB_ASSERT(upb_inttable_count(&state.ref2) == 0);
- upb_inttable_uninit2(&state.ref2, &upb_alloc_debugrefs);
- if (r->vtbl->visit) r->vtbl->visit(r, v, closure);
- }
- static void trackinit(upb_refcounted *r) {
- r->refs = upb_malloc(&upb_alloc_debugrefs, sizeof(*r->refs));
- r->ref2s = upb_malloc(&upb_alloc_debugrefs, sizeof(*r->ref2s));
- upb_inttable_init2(r->refs, UPB_CTYPE_PTR, &upb_alloc_debugrefs);
- upb_inttable_init2(r->ref2s, UPB_CTYPE_PTR, &upb_alloc_debugrefs);
- }
- static void trackfree(const upb_refcounted *r) {
- upb_inttable_uninit2(r->refs, &upb_alloc_debugrefs);
- upb_inttable_uninit2(r->ref2s, &upb_alloc_debugrefs);
- upb_free(&upb_alloc_debugrefs, r->refs);
- upb_free(&upb_alloc_debugrefs, r->ref2s);
- }
- #else
- static void track(const upb_refcounted *r, const void *owner, bool ref2) {
- UPB_UNUSED(r);
- UPB_UNUSED(owner);
- UPB_UNUSED(ref2);
- }
- static void untrack(const upb_refcounted *r, const void *owner, bool ref2) {
- UPB_UNUSED(r);
- UPB_UNUSED(owner);
- UPB_UNUSED(ref2);
- }
- static void checkref(const upb_refcounted *r, const void *owner, bool ref2) {
- UPB_UNUSED(r);
- UPB_UNUSED(owner);
- UPB_UNUSED(ref2);
- }
- static void trackinit(upb_refcounted *r) {
- UPB_UNUSED(r);
- }
- static void trackfree(const upb_refcounted *r) {
- UPB_UNUSED(r);
- }
- static void visit(const upb_refcounted *r, upb_refcounted_visit *v,
- void *closure) {
- if (r->vtbl->visit) r->vtbl->visit(r, v, closure);
- }
- #endif /* UPB_DEBUG_REFS */
- /* freeze() *******************************************************************/
- /* The freeze() operation is by far the most complicated part of this scheme.
- * We compute strongly-connected components and then mutate the graph such that
- * we preserve the invariants documented at the top of this file. And we must
- * handle out-of-memory errors gracefully (without leaving the graph
- * inconsistent), which adds to the fun. */
- /* The state used by the freeze operation (shared across many functions). */
- typedef struct {
- int depth;
- int maxdepth;
- uint64_t index;
- /* Maps upb_refcounted* -> attributes (color, etc). attr layout varies by
- * color. */
- upb_inttable objattr;
- upb_inttable stack; /* stack of upb_refcounted* for Tarjan's algorithm. */
- upb_inttable groups; /* array of uint32_t*, malloc'd refcounts for new groups */
- upb_status *status;
- jmp_buf err;
- } tarjan;
- static void release_ref2(const upb_refcounted *obj,
- const upb_refcounted *subobj,
- void *closure);
- /* Node attributes -----------------------------------------------------------*/
- /* After our analysis phase all nodes will be either GRAY or WHITE. */
- typedef enum {
- BLACK = 0, /* Object has not been seen. */
- GRAY, /* Object has been found via a refgroup but may not be reachable. */
- GREEN, /* Object is reachable and is currently on the Tarjan stack. */
- WHITE /* Object is reachable and has been assigned a group (SCC). */
- } color_t;
- UPB_NORETURN static void err(tarjan *t) { longjmp(t->err, 1); }
- UPB_NORETURN static void oom(tarjan *t) {
- upb_status_seterrmsg(t->status, "out of memory");
- err(t);
- }
- static uint64_t trygetattr(const tarjan *t, const upb_refcounted *r) {
- upb_value v;
- return upb_inttable_lookupptr(&t->objattr, r, &v) ?
- upb_value_getuint64(v) : 0;
- }
- static uint64_t getattr(const tarjan *t, const upb_refcounted *r) {
- upb_value v;
- bool found = upb_inttable_lookupptr(&t->objattr, r, &v);
- UPB_ASSERT(found);
- return upb_value_getuint64(v);
- }
- static void setattr(tarjan *t, const upb_refcounted *r, uint64_t attr) {
- upb_inttable_removeptr(&t->objattr, r, NULL);
- upb_inttable_insertptr(&t->objattr, r, upb_value_uint64(attr));
- }
- static color_t color(tarjan *t, const upb_refcounted *r) {
- return trygetattr(t, r) & 0x3; /* Color is always stored in the low 2 bits. */
- }
- static void set_gray(tarjan *t, const upb_refcounted *r) {
- UPB_ASSERT(color(t, r) == BLACK);
- setattr(t, r, GRAY);
- }
- /* Pushes an obj onto the Tarjan stack and sets it to GREEN. */
- static void push(tarjan *t, const upb_refcounted *r) {
- UPB_ASSERT(color(t, r) == BLACK || color(t, r) == GRAY);
- /* This defines the attr layout for the GREEN state. "index" and "lowlink"
- * get 31 bits, which is plenty (limit of 2B objects frozen at a time). */
- setattr(t, r, GREEN | (t->index << 2) | (t->index << 33));
- if (++t->index == 0x80000000) {
- upb_status_seterrmsg(t->status, "too many objects to freeze");
- err(t);
- }
- upb_inttable_push(&t->stack, upb_value_ptr((void*)r));
- }
- /* Pops an obj from the Tarjan stack and sets it to WHITE, with a ptr to its
- * SCC group. */
- static upb_refcounted *pop(tarjan *t) {
- upb_refcounted *r = upb_value_getptr(upb_inttable_pop(&t->stack));
- UPB_ASSERT(color(t, r) == GREEN);
- /* This defines the attr layout for nodes in the WHITE state.
- * Top of group stack is [group, NULL]; we point at group. */
- setattr(t, r, WHITE | (upb_inttable_count(&t->groups) - 2) << 8);
- return r;
- }
- static void tarjan_newgroup(tarjan *t) {
- uint32_t *group = upb_gmalloc(sizeof(*group));
- if (!group) oom(t);
- /* Push group and empty group leader (we'll fill in leader later). */
- if (!upb_inttable_push(&t->groups, upb_value_ptr(group)) ||
- !upb_inttable_push(&t->groups, upb_value_ptr(NULL))) {
- upb_gfree(group);
- oom(t);
- }
- *group = 0;
- }
- static uint32_t idx(tarjan *t, const upb_refcounted *r) {
- UPB_ASSERT(color(t, r) == GREEN);
- return (getattr(t, r) >> 2) & 0x7FFFFFFF;
- }
- static uint32_t lowlink(tarjan *t, const upb_refcounted *r) {
- if (color(t, r) == GREEN) {
- return getattr(t, r) >> 33;
- } else {
- return UINT32_MAX;
- }
- }
- static void set_lowlink(tarjan *t, const upb_refcounted *r, uint32_t lowlink) {
- UPB_ASSERT(color(t, r) == GREEN);
- setattr(t, r, ((uint64_t)lowlink << 33) | (getattr(t, r) & 0x1FFFFFFFF));
- }
- static uint32_t *group(tarjan *t, upb_refcounted *r) {
- uint64_t groupnum;
- upb_value v;
- bool found;
- UPB_ASSERT(color(t, r) == WHITE);
- groupnum = getattr(t, r) >> 8;
- found = upb_inttable_lookup(&t->groups, groupnum, &v);
- UPB_ASSERT(found);
- return upb_value_getptr(v);
- }
- /* If the group leader for this object's group has not previously been set,
- * the given object is assigned to be its leader. */
- static upb_refcounted *groupleader(tarjan *t, upb_refcounted *r) {
- uint64_t leader_slot;
- upb_value v;
- bool found;
- UPB_ASSERT(color(t, r) == WHITE);
- leader_slot = (getattr(t, r) >> 8) + 1;
- found = upb_inttable_lookup(&t->groups, leader_slot, &v);
- UPB_ASSERT(found);
- if (upb_value_getptr(v)) {
- return upb_value_getptr(v);
- } else {
- upb_inttable_remove(&t->groups, leader_slot, NULL);
- upb_inttable_insert(&t->groups, leader_slot, upb_value_ptr(r));
- return r;
- }
- }
- /* Tarjan's algorithm --------------------------------------------------------*/
- /* See:
- * http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm */
- static void do_tarjan(const upb_refcounted *obj, tarjan *t);
- static void tarjan_visit(const upb_refcounted *obj,
- const upb_refcounted *subobj,
- void *closure) {
- tarjan *t = closure;
- if (++t->depth > t->maxdepth) {
- upb_status_seterrf(t->status, "graph too deep to freeze (%d)", t->maxdepth);
- err(t);
- } else if (subobj->is_frozen || color(t, subobj) == WHITE) {
- /* Do nothing: we don't want to visit or color already-frozen nodes,
- * and WHITE nodes have already been assigned a SCC. */
- } else if (color(t, subobj) < GREEN) {
- /* Subdef has not yet been visited; recurse on it. */
- do_tarjan(subobj, t);
- set_lowlink(t, obj, UPB_MIN(lowlink(t, obj), lowlink(t, subobj)));
- } else if (color(t, subobj) == GREEN) {
- /* Subdef is in the stack and hence in the current SCC. */
- set_lowlink(t, obj, UPB_MIN(lowlink(t, obj), idx(t, subobj)));
- }
- --t->depth;
- }
- static void do_tarjan(const upb_refcounted *obj, tarjan *t) {
- if (color(t, obj) == BLACK) {
- /* We haven't seen this object's group; mark the whole group GRAY. */
- const upb_refcounted *o = obj;
- do { set_gray(t, o); } while ((o = o->next) != obj);
- }
- push(t, obj);
- visit(obj, tarjan_visit, t);
- if (lowlink(t, obj) == idx(t, obj)) {
- tarjan_newgroup(t);
- while (pop(t) != obj)
- ;
- }
- }
- /* freeze() ------------------------------------------------------------------*/
- static void crossref(const upb_refcounted *r, const upb_refcounted *subobj,
- void *_t) {
- tarjan *t = _t;
- UPB_ASSERT(color(t, r) > BLACK);
- if (color(t, subobj) > BLACK && r->group != subobj->group) {
- /* Previously this ref was not reflected in subobj->group because they
- * were in the same group; now that they are split a ref must be taken. */
- refgroup(subobj->group);
- }
- }
- static bool freeze(upb_refcounted *const*roots, int n, upb_status *s,
- int maxdepth) {
- volatile bool ret = false;
- int i;
- upb_inttable_iter iter;
- /* We run in two passes so that we can allocate all memory before performing
- * any mutation of the input -- this allows us to leave the input unchanged
- * in the case of memory allocation failure. */
- tarjan t;
- t.index = 0;
- t.depth = 0;
- t.maxdepth = maxdepth;
- t.status = s;
- if (!upb_inttable_init(&t.objattr, UPB_CTYPE_UINT64)) goto err1;
- if (!upb_inttable_init(&t.stack, UPB_CTYPE_PTR)) goto err2;
- if (!upb_inttable_init(&t.groups, UPB_CTYPE_PTR)) goto err3;
- if (setjmp(t.err) != 0) goto err4;
- for (i = 0; i < n; i++) {
- if (color(&t, roots[i]) < GREEN) {
- do_tarjan(roots[i], &t);
- }
- }
- /* If we've made it this far, no further errors are possible so it's safe to
- * mutate the objects without risk of leaving them in an inconsistent state. */
- ret = true;
- /* The transformation that follows requires care. The preconditions are:
- * - all objects in attr map are WHITE or GRAY, and are in mutable groups
- * (groups of all mutable objs)
- * - no ref2(to, from) refs have incremented count(to) if both "to" and
- * "from" are in our attr map (this follows from invariants (2) and (3)) */
- /* Pass 1: we remove WHITE objects from their mutable groups, and add them to
- * new groups according to the SCC's we computed. These new groups will
- * consist of only frozen objects. None will be immediately collectible,
- * because WHITE objects are by definition reachable from one of "roots",
- * which the caller must own refs on. */
- upb_inttable_begin(&iter, &t.objattr);
- for(; !upb_inttable_done(&iter); upb_inttable_next(&iter)) {
- upb_refcounted *obj = (upb_refcounted*)upb_inttable_iter_key(&iter);
- /* Since removal from a singly-linked list requires access to the object's
- * predecessor, we consider obj->next instead of obj for moving. With the
- * while() loop we guarantee that we will visit every node's predecessor.
- * Proof:
- * 1. every node's predecessor is in our attr map.
- * 2. though the loop body may change a node's predecessor, it will only
- * change it to be the node we are currently operating on, so with a
- * while() loop we guarantee ourselves the chance to remove each node. */
- while (color(&t, obj->next) == WHITE &&
- group(&t, obj->next) != obj->next->group) {
- upb_refcounted *leader;
- /* Remove from old group. */
- upb_refcounted *move = obj->next;
- if (obj == move) {
- /* Removing the last object from a group. */
- UPB_ASSERT(*obj->group == obj->individual_count);
- upb_gfree(obj->group);
- } else {
- obj->next = move->next;
- /* This may decrease to zero; we'll collect GRAY objects (if any) that
- * remain in the group in the third pass. */
- UPB_ASSERT(*move->group >= move->individual_count);
- *move->group -= move->individual_count;
- }
- /* Add to new group. */
- leader = groupleader(&t, move);
- if (move == leader) {
- /* First object added to new group is its leader. */
- move->group = group(&t, move);
- move->next = move;
- *move->group = move->individual_count;
- } else {
- /* Group already has at least one object in it. */
- UPB_ASSERT(leader->group == group(&t, move));
- move->group = group(&t, move);
- move->next = leader->next;
- leader->next = move;
- *move->group += move->individual_count;
- }
- move->is_frozen = true;
- }
- }
- /* Pass 2: GRAY and WHITE objects "obj" with ref2(to, obj) references must
- * increment count(to) if group(obj) != group(to) (which could now be the
- * case if "to" was just frozen). */
- upb_inttable_begin(&iter, &t.objattr);
- for(; !upb_inttable_done(&iter); upb_inttable_next(&iter)) {
- upb_refcounted *obj = (upb_refcounted*)upb_inttable_iter_key(&iter);
- visit(obj, crossref, &t);
- }
- /* Pass 3: GRAY objects are collected if their group's refcount dropped to
- * zero when we removed its white nodes. This can happen if they had only
- * been kept alive by virtue of sharing a group with an object that was just
- * frozen.
- *
- * It is important that we do this last, since the GRAY object's free()
- * function could call unref2() on just-frozen objects, which will decrement
- * refs that were added in pass 2. */
- upb_inttable_begin(&iter, &t.objattr);
- for(; !upb_inttable_done(&iter); upb_inttable_next(&iter)) {
- upb_refcounted *obj = (upb_refcounted*)upb_inttable_iter_key(&iter);
- if (obj->group == NULL || *obj->group == 0) {
- if (obj->group) {
- upb_refcounted *o;
- /* We eagerly free() the group's count (since we can't easily determine
- * the group's remaining size it's the easiest way to ensure it gets
- * done). */
- upb_gfree(obj->group);
- /* Visit to release ref2's (done in a separate pass since release_ref2
- * depends on o->group being unmodified so it can test merged()). */
- o = obj;
- do { visit(o, release_ref2, NULL); } while ((o = o->next) != obj);
- /* Mark "group" fields as NULL so we know to free the objects later in
- * this loop, but also don't try to delete the group twice. */
- o = obj;
- do { o->group = NULL; } while ((o = o->next) != obj);
- }
- freeobj(obj);
- }
- }
- err4:
- if (!ret) {
- upb_inttable_begin(&iter, &t.groups);
- for(; !upb_inttable_done(&iter); upb_inttable_next(&iter))
- upb_gfree(upb_value_getptr(upb_inttable_iter_value(&iter)));
- }
- upb_inttable_uninit(&t.groups);
- err3:
- upb_inttable_uninit(&t.stack);
- err2:
- upb_inttable_uninit(&t.objattr);
- err1:
- return ret;
- }
- /* Misc internal functions ***************************************************/
- static bool merged(const upb_refcounted *r, const upb_refcounted *r2) {
- return r->group == r2->group;
- }
- static void merge(upb_refcounted *r, upb_refcounted *from) {
- upb_refcounted *base;
- upb_refcounted *tmp;
- if (merged(r, from)) return;
- *r->group += *from->group;
- upb_gfree(from->group);
- base = from;
- /* Set all refcount pointers in the "from" chain to the merged refcount.
- *
- * TODO(haberman): this linear algorithm can result in an overall O(n^2) bound
- * if the user continuously extends a group by one object. Prevent this by
- * using one of the techniques in this paper:
- * http://bioinfo.ict.ac.cn/~dbu/AlgorithmCourses/Lectures/Union-Find-Tarjan.pdf */
- do { from->group = r->group; } while ((from = from->next) != base);
- /* Merge the two circularly linked lists by swapping their next pointers. */
- tmp = r->next;
- r->next = base->next;
- base->next = tmp;
- }
- static void unref(const upb_refcounted *r);
- static void release_ref2(const upb_refcounted *obj,
- const upb_refcounted *subobj,
- void *closure) {
- UPB_UNUSED(closure);
- untrack(subobj, obj, true);
- if (!merged(obj, subobj)) {
- UPB_ASSERT(subobj->is_frozen);
- unref(subobj);
- }
- }
- static void unref(const upb_refcounted *r) {
- if (unrefgroup(r->group)) {
- const upb_refcounted *o;
- upb_gfree(r->group);
- /* In two passes, since release_ref2 needs a guarantee that any subobjs
- * are alive. */
- o = r;
- do { visit(o, release_ref2, NULL); } while((o = o->next) != r);
- o = r;
- do {
- const upb_refcounted *next = o->next;
- UPB_ASSERT(o->is_frozen || o->individual_count == 0);
- freeobj((upb_refcounted*)o);
- o = next;
- } while(o != r);
- }
- }
- static void freeobj(upb_refcounted *o) {
- trackfree(o);
- o->vtbl->free((upb_refcounted*)o);
- }
- /* Public interface ***********************************************************/
- bool upb_refcounted_init(upb_refcounted *r,
- const struct upb_refcounted_vtbl *vtbl,
- const void *owner) {
- #ifndef NDEBUG
- /* Endianness check. This is unrelated to upb_refcounted, it's just a
- * convenient place to put the check that we can be assured will run for
- * basically every program using upb. */
- const int x = 1;
- #ifdef UPB_BIG_ENDIAN
- UPB_ASSERT(*(char*)&x != 1);
- #else
- UPB_ASSERT(*(char*)&x == 1);
- #endif
- #endif
- r->next = r;
- r->vtbl = vtbl;
- r->individual_count = 0;
- r->is_frozen = false;
- r->group = upb_gmalloc(sizeof(*r->group));
- if (!r->group) return false;
- *r->group = 0;
- trackinit(r);
- upb_refcounted_ref(r, owner);
- return true;
- }
- bool upb_refcounted_isfrozen(const upb_refcounted *r) {
- return r->is_frozen;
- }
- void upb_refcounted_ref(const upb_refcounted *r, const void *owner) {
- track(r, owner, false);
- if (!r->is_frozen)
- ((upb_refcounted*)r)->individual_count++;
- refgroup(r->group);
- }
- void upb_refcounted_unref(const upb_refcounted *r, const void *owner) {
- untrack(r, owner, false);
- if (!r->is_frozen)
- ((upb_refcounted*)r)->individual_count--;
- unref(r);
- }
- void upb_refcounted_ref2(const upb_refcounted *r, upb_refcounted *from) {
- UPB_ASSERT(!from->is_frozen); /* Non-const pointer implies this. */
- track(r, from, true);
- if (r->is_frozen) {
- refgroup(r->group);
- } else {
- merge((upb_refcounted*)r, from);
- }
- }
- void upb_refcounted_unref2(const upb_refcounted *r, upb_refcounted *from) {
- UPB_ASSERT(!from->is_frozen); /* Non-const pointer implies this. */
- untrack(r, from, true);
- if (r->is_frozen) {
- unref(r);
- } else {
- UPB_ASSERT(merged(r, from));
- }
- }
- void upb_refcounted_donateref(
- const upb_refcounted *r, const void *from, const void *to) {
- UPB_ASSERT(from != to);
- if (to != NULL)
- upb_refcounted_ref(r, to);
- if (from != NULL)
- upb_refcounted_unref(r, from);
- }
- void upb_refcounted_checkref(const upb_refcounted *r, const void *owner) {
- checkref(r, owner, false);
- }
- bool upb_refcounted_freeze(upb_refcounted *const*roots, int n, upb_status *s,
- int maxdepth) {
- int i;
- bool ret;
- for (i = 0; i < n; i++) {
- UPB_ASSERT(!roots[i]->is_frozen);
- }
- ret = freeze(roots, n, s, maxdepth);
- UPB_ASSERT(!s || ret == upb_ok(s));
- return ret;
- }
- bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink *sink) {
- void *subc;
- bool ret;
- upb_bufhandle handle;
- upb_bufhandle_init(&handle);
- upb_bufhandle_setbuf(&handle, buf, 0);
- ret = upb_bytessink_start(sink, len, &subc);
- if (ret && len != 0) {
- ret = (upb_bytessink_putbuf(sink, subc, buf, len, &handle) >= len);
- }
- if (ret) {
- ret = upb_bytessink_end(sink);
- }
- upb_bufhandle_uninit(&handle);
- return ret;
- }
- struct upb_bufsink {
- upb_byteshandler handler;
- upb_bytessink sink;
- upb_env *env;
- char *ptr;
- size_t len, size;
- };
- static void *upb_bufsink_start(void *_sink, const void *hd, size_t size_hint) {
- upb_bufsink *sink = _sink;
- UPB_UNUSED(hd);
- UPB_UNUSED(size_hint);
- sink->len = 0;
- return sink;
- }
- static size_t upb_bufsink_string(void *_sink, const void *hd, const char *ptr,
- size_t len, const upb_bufhandle *handle) {
- upb_bufsink *sink = _sink;
- size_t new_size = sink->size;
- UPB_ASSERT(new_size > 0);
- UPB_UNUSED(hd);
- UPB_UNUSED(handle);
- while (sink->len + len > new_size) {
- new_size *= 2;
- }
- if (new_size != sink->size) {
- sink->ptr = upb_env_realloc(sink->env, sink->ptr, sink->size, new_size);
- sink->size = new_size;
- }
- memcpy(sink->ptr + sink->len, ptr, len);
- sink->len += len;
- return len;
- }
- upb_bufsink *upb_bufsink_new(upb_env *env) {
- upb_bufsink *sink = upb_env_malloc(env, sizeof(upb_bufsink));
- upb_byteshandler_init(&sink->handler);
- upb_byteshandler_setstartstr(&sink->handler, upb_bufsink_start, NULL);
- upb_byteshandler_setstring(&sink->handler, upb_bufsink_string, NULL);
- upb_bytessink_reset(&sink->sink, &sink->handler, sink);
- sink->env = env;
- sink->size = 32;
- sink->ptr = upb_env_malloc(env, sink->size);
- sink->len = 0;
- return sink;
- }
- void upb_bufsink_free(upb_bufsink *sink) {
- upb_env_free(sink->env, sink->ptr);
- upb_env_free(sink->env, sink);
- }
- upb_bytessink *upb_bufsink_sink(upb_bufsink *sink) {
- return &sink->sink;
- }
- const char *upb_bufsink_getdata(const upb_bufsink *sink, size_t *len) {
- *len = sink->len;
- return sink->ptr;
- }
- /*
- ** upb_table Implementation
- **
- ** Implementation is heavily inspired by Lua's ltable.c.
- */
- #include <string.h>
- #define UPB_MAXARRSIZE 16 /* 64k. */
- /* From Chromium. */
- #define ARRAY_SIZE(x) \
- ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
- static void upb_check_alloc(upb_table *t, upb_alloc *a) {
- UPB_UNUSED(t);
- UPB_UNUSED(a);
- UPB_ASSERT_DEBUGVAR(t->alloc == a);
- }
- static const double MAX_LOAD = 0.85;
- /* The minimum utilization of the array part of a mixed hash/array table. This
- * is a speed/memory-usage tradeoff (though it's not straightforward because of
- * cache effects). The lower this is, the more memory we'll use. */
- static const double MIN_DENSITY = 0.1;
- bool is_pow2(uint64_t v) { return v == 0 || (v & (v - 1)) == 0; }
- int log2ceil(uint64_t v) {
- int ret = 0;
- bool pow2 = is_pow2(v);
- while (v >>= 1) ret++;
- ret = pow2 ? ret : ret + 1; /* Ceiling. */
- return UPB_MIN(UPB_MAXARRSIZE, ret);
- }
- char *upb_strdup(const char *s, upb_alloc *a) {
- return upb_strdup2(s, strlen(s), a);
- }
- char *upb_strdup2(const char *s, size_t len, upb_alloc *a) {
- size_t n;
- char *p;
- /* Prevent overflow errors. */
- if (len == SIZE_MAX) return NULL;
- /* Always null-terminate, even if binary data; but don't rely on the input to
- * have a null-terminating byte since it may be a raw binary buffer. */
- n = len + 1;
- p = upb_malloc(a, n);
- if (p) {
- memcpy(p, s, len);
- p[len] = 0;
- }
- return p;
- }
- /* A type to represent the lookup key of either a strtable or an inttable. */
- typedef union {
- uintptr_t num;
- struct {
- const char *str;
- size_t len;
- } str;
- } lookupkey_t;
- static lookupkey_t strkey2(const char *str, size_t len) {
- lookupkey_t k;
- k.str.str = str;
- k.str.len = len;
- return k;
- }
- static lookupkey_t intkey(uintptr_t key) {
- lookupkey_t k;
- k.num = key;
- return k;
- }
- typedef uint32_t hashfunc_t(upb_tabkey key);
- typedef bool eqlfunc_t(upb_tabkey k1, lookupkey_t k2);
- /* Base table (shared code) ***************************************************/
- /* For when we need to cast away const. */
- static upb_tabent *mutable_entries(upb_table *t) {
- return (upb_tabent*)t->entries;
- }
- static bool isfull(upb_table *t) {
- if (upb_table_size(t) == 0) {
- return true;
- } else {
- return ((double)(t->count + 1) / upb_table_size(t)) > MAX_LOAD;
- }
- }
- static bool init(upb_table *t, upb_ctype_t ctype, uint8_t size_lg2,
- upb_alloc *a) {
- size_t bytes;
- t->count = 0;
- t->ctype = ctype;
- t->size_lg2 = size_lg2;
- t->mask = upb_table_size(t) ? upb_table_size(t) - 1 : 0;
- #ifndef NDEBUG
- t->alloc = a;
- #endif
- bytes = upb_table_size(t) * sizeof(upb_tabent);
- if (bytes > 0) {
- t->entries = upb_malloc(a, bytes);
- if (!t->entries) return false;
- memset(mutable_entries(t), 0, bytes);
- } else {
- t->entries = NULL;
- }
- return true;
- }
- static void uninit(upb_table *t, upb_alloc *a) {
- upb_check_alloc(t, a);
- upb_free(a, mutable_entries(t));
- }
- static upb_tabent *emptyent(upb_table *t) {
- upb_tabent *e = mutable_entries(t) + upb_table_size(t);
- while (1) { if (upb_tabent_isempty(--e)) return e; UPB_ASSERT(e > t->entries); }
- }
- static upb_tabent *getentry_mutable(upb_table *t, uint32_t hash) {
- return (upb_tabent*)upb_getentry(t, hash);
- }
- static const upb_tabent *findentry(const upb_table *t, lookupkey_t key,
- uint32_t hash, eqlfunc_t *eql) {
- const upb_tabent *e;
- if (t->size_lg2 == 0) return NULL;
- e = upb_getentry(t, hash);
- if (upb_tabent_isempty(e)) return NULL;
- while (1) {
- if (eql(e->key, key)) return e;
- if ((e = e->next) == NULL) return NULL;
- }
- }
- static upb_tabent *findentry_mutable(upb_table *t, lookupkey_t key,
- uint32_t hash, eqlfunc_t *eql) {
- return (upb_tabent*)findentry(t, key, hash, eql);
- }
- static bool lookup(const upb_table *t, lookupkey_t key, upb_value *v,
- uint32_t hash, eqlfunc_t *eql) {
- const upb_tabent *e = findentry(t, key, hash, eql);
- if (e) {
- if (v) {
- _upb_value_setval(v, e->val.val, t->ctype);
- }
- return true;
- } else {
- return false;
- }
- }
- /* The given key must not already exist in the table. */
- static void insert(upb_table *t, lookupkey_t key, upb_tabkey tabkey,
- upb_value val, uint32_t hash,
- hashfunc_t *hashfunc, eqlfunc_t *eql) {
- upb_tabent *mainpos_e;
- upb_tabent *our_e;
- UPB_ASSERT(findentry(t, key, hash, eql) == NULL);
- UPB_ASSERT_DEBUGVAR(val.ctype == t->ctype);
- t->count++;
- mainpos_e = getentry_mutable(t, hash);
- our_e = mainpos_e;
- if (upb_tabent_isempty(mainpos_e)) {
- /* Our main position is empty; use it. */
- our_e->next = NULL;
- } else {
- /* Collision. */
- upb_tabent *new_e = emptyent(t);
- /* Head of collider's chain. */
- upb_tabent *chain = getentry_mutable(t, hashfunc(mainpos_e->key));
- if (chain == mainpos_e) {
- /* Existing ent is in its main posisiton (it has the same hash as us, and
- * is the head of our chain). Insert to new ent and append to this chain. */
- new_e->next = mainpos_e->next;
- mainpos_e->next = new_e;
- our_e = new_e;
- } else {
- /* Existing ent is not in its main position (it is a node in some other
- * chain). This implies that no existing ent in the table has our hash.
- * Evict it (updating its chain) and use its ent for head of our chain. */
- *new_e = *mainpos_e; /* copies next. */
- while (chain->next != mainpos_e) {
- chain = (upb_tabent*)chain->next;
- UPB_ASSERT(chain);
- }
- chain->next = new_e;
- our_e = mainpos_e;
- our_e->next = NULL;
- }
- }
- our_e->key = tabkey;
- our_e->val.val = val.val;
- UPB_ASSERT(findentry(t, key, hash, eql) == our_e);
- }
- static bool rm(upb_table *t, lookupkey_t key, upb_value *val,
- upb_tabkey *removed, uint32_t hash, eqlfunc_t *eql) {
- upb_tabent *chain = getentry_mutable(t, hash);
- if (upb_tabent_isempty(chain)) return false;
- if (eql(chain->key, key)) {
- /* Element to remove is at the head of its chain. */
- t->count--;
- if (val) _upb_value_setval(val, chain->val.val, t->ctype);
- if (removed) *removed = chain->key;
- if (chain->next) {
- upb_tabent *move = (upb_tabent*)chain->next;
- *chain = *move;
- move->key = 0; /* Make the slot empty. */
- } else {
- chain->key = 0; /* Make the slot empty. */
- }
- return true;
- } else {
- /* Element to remove is either in a non-head position or not in the
- * table. */
- while (chain->next && !eql(chain->next->key, key)) {
- chain = (upb_tabent*)chain->next;
- }
- if (chain->next) {
- /* Found element to remove. */
- upb_tabent *rm = (upb_tabent*)chain->next;
- t->count--;
- if (val) _upb_value_setval(val, chain->next->val.val, t->ctype);
- if (removed) *removed = rm->key;
- rm->key = 0; /* Make the slot empty. */
- chain->next = rm->next;
- return true;
- } else {
- /* Element to remove is not in the table. */
- return false;
- }
- }
- }
- static size_t next(const upb_table *t, size_t i) {
- do {
- if (++i >= upb_table_size(t))
- return SIZE_MAX;
- } while(upb_tabent_isempty(&t->entries[i]));
- return i;
- }
- static size_t begin(const upb_table *t) {
- return next(t, -1);
- }
- /* upb_strtable ***************************************************************/
- /* A simple "subclass" of upb_table that only adds a hash function for strings. */
- static upb_tabkey strcopy(lookupkey_t k2, upb_alloc *a) {
- uint32_t len = (uint32_t) k2.str.len;
- char *str = upb_malloc(a, k2.str.len + sizeof(uint32_t) + 1);
- if (str == NULL) return 0;
- memcpy(str, &len, sizeof(uint32_t));
- memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len + 1);
- return (uintptr_t)str;
- }
- static uint32_t strhash(upb_tabkey key) {
- uint32_t len;
- char *str = upb_tabstr(key, &len);
- return MurmurHash2(str, len, 0);
- }
- static bool streql(upb_tabkey k1, lookupkey_t k2) {
- uint32_t len;
- char *str = upb_tabstr(k1, &len);
- return len == k2.str.len && memcmp(str, k2.str.str, len) == 0;
- }
- bool upb_strtable_init2(upb_strtable *t, upb_ctype_t ctype, upb_alloc *a) {
- return init(&t->t, ctype, 2, a);
- }
- void upb_strtable_uninit2(upb_strtable *t, upb_alloc *a) {
- size_t i;
- for (i = 0; i < upb_table_size(&t->t); i++)
- upb_free(a, (void*)t->t.entries[i].key);
- uninit(&t->t, a);
- }
- bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_alloc *a) {
- upb_strtable new_table;
- upb_strtable_iter i;
- upb_check_alloc(&t->t, a);
- if (!init(&new_table.t, t->t.ctype, size_lg2, a))
- return false;
- upb_strtable_begin(&i, t);
- for ( ; !upb_strtable_done(&i); upb_strtable_next(&i)) {
- upb_strtable_insert3(
- &new_table,
- upb_strtable_iter_key(&i),
- upb_strtable_iter_keylength(&i),
- upb_strtable_iter_value(&i),
- a);
- }
- upb_strtable_uninit2(t, a);
- *t = new_table;
- return true;
- }
- bool upb_strtable_insert3(upb_strtable *t, const char *k, size_t len,
- upb_value v, upb_alloc *a) {
- lookupkey_t key;
- upb_tabkey tabkey;
- uint32_t hash;
- upb_check_alloc(&t->t, a);
- if (isfull(&t->t)) {
- /* Need to resize. New table of double the size, add old elements to it. */
- if (!upb_strtable_resize(t, t->t.size_lg2 + 1, a)) {
- return false;
- }
- }
- key = strkey2(k, len);
- tabkey = strcopy(key, a);
- if (tabkey == 0) return false;
- hash = MurmurHash2(key.str.str, key.str.len, 0);
- insert(&t->t, key, tabkey, v, hash, &strhash, &streql);
- return true;
- }
- bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
- upb_value *v) {
- uint32_t hash = MurmurHash2(key, len, 0);
- return lookup(&t->t, strkey2(key, len), v, hash, &streql);
- }
- bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len,
- upb_value *val, upb_alloc *alloc) {
- uint32_t hash = MurmurHash2(key, len, 0);
- upb_tabkey tabkey;
- if (rm(&t->t, strkey2(key, len), val, &tabkey, hash, &streql)) {
- upb_free(alloc, (void*)tabkey);
- return true;
- } else {
- return false;
- }
- }
- /* Iteration */
- static const upb_tabent *str_tabent(const upb_strtable_iter *i) {
- return &i->t->t.entries[i->index];
- }
- void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t) {
- i->t = t;
- i->index = begin(&t->t);
- }
- void upb_strtable_next(upb_strtable_iter *i) {
- i->index = next(&i->t->t, i->index);
- }
- bool upb_strtable_done(const upb_strtable_iter *i) {
- return i->index >= upb_table_size(&i->t->t) ||
- upb_tabent_isempty(str_tabent(i));
- }
- const char *upb_strtable_iter_key(const upb_strtable_iter *i) {
- UPB_ASSERT(!upb_strtable_done(i));
- return upb_tabstr(str_tabent(i)->key, NULL);
- }
- size_t upb_strtable_iter_keylength(const upb_strtable_iter *i) {
- uint32_t len;
- UPB_ASSERT(!upb_strtable_done(i));
- upb_tabstr(str_tabent(i)->key, &len);
- return len;
- }
- upb_value upb_strtable_iter_value(const upb_strtable_iter *i) {
- UPB_ASSERT(!upb_strtable_done(i));
- return _upb_value_val(str_tabent(i)->val.val, i->t->t.ctype);
- }
- void upb_strtable_iter_setdone(upb_strtable_iter *i) {
- i->index = SIZE_MAX;
- }
- bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
- const upb_strtable_iter *i2) {
- if (upb_strtable_done(i1) && upb_strtable_done(i2))
- return true;
- return i1->t == i2->t && i1->index == i2->index;
- }
- /* upb_inttable ***************************************************************/
- /* For inttables we use a hybrid structure where small keys are kept in an
- * array and large keys are put in the hash table. */
- static uint32_t inthash(upb_tabkey key) { return upb_inthash(key); }
- static bool inteql(upb_tabkey k1, lookupkey_t k2) {
- return k1 == k2.num;
- }
- static upb_tabval *mutable_array(upb_inttable *t) {
- return (upb_tabval*)t->array;
- }
- static upb_tabval *inttable_val(upb_inttable *t, uintptr_t key) {
- if (key < t->array_size) {
- return upb_arrhas(t->array[key]) ? &(mutable_array(t)[key]) : NULL;
- } else {
- upb_tabent *e =
- findentry_mutable(&t->t, intkey(key), upb_inthash(key), &inteql);
- return e ? &e->val : NULL;
- }
- }
- static const upb_tabval *inttable_val_const(const upb_inttable *t,
- uintptr_t key) {
- return inttable_val((upb_inttable*)t, key);
- }
- size_t upb_inttable_count(const upb_inttable *t) {
- return t->t.count + t->array_count;
- }
- static void check(upb_inttable *t) {
- UPB_UNUSED(t);
- #if defined(UPB_DEBUG_TABLE) && !defined(NDEBUG)
- {
- /* This check is very expensive (makes inserts/deletes O(N)). */
- size_t count = 0;
- upb_inttable_iter i;
- upb_inttable_begin(&i, t);
- for(; !upb_inttable_done(&i); upb_inttable_next(&i), count++) {
- UPB_ASSERT(upb_inttable_lookup(t, upb_inttable_iter_key(&i), NULL));
- }
- UPB_ASSERT(count == upb_inttable_count(t));
- }
- #endif
- }
- bool upb_inttable_sizedinit(upb_inttable *t, upb_ctype_t ctype,
- size_t asize, int hsize_lg2, upb_alloc *a) {
- size_t array_bytes;
- if (!init(&t->t, ctype, hsize_lg2, a)) return false;
- /* Always make the array part at least 1 long, so that we know key 0
- * won't be in the hash part, which simplifies things. */
- t->array_size = UPB_MAX(1, asize);
- t->array_count = 0;
- array_bytes = t->array_size * sizeof(upb_value);
- t->array = upb_malloc(a, array_bytes);
- if (!t->array) {
- uninit(&t->t, a);
- return false;
- }
- memset(mutable_array(t), 0xff, array_bytes);
- check(t);
- return true;
- }
- bool upb_inttable_init2(upb_inttable *t, upb_ctype_t ctype, upb_alloc *a) {
- return upb_inttable_sizedinit(t, ctype, 0, 4, a);
- }
- void upb_inttable_uninit2(upb_inttable *t, upb_alloc *a) {
- uninit(&t->t, a);
- upb_free(a, mutable_array(t));
- }
- bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val,
- upb_alloc *a) {
- upb_tabval tabval;
- tabval.val = val.val;
- UPB_ASSERT(upb_arrhas(tabval)); /* This will reject (uint64_t)-1. Fix this. */
- upb_check_alloc(&t->t, a);
- if (key < t->array_size) {
- UPB_ASSERT(!upb_arrhas(t->array[key]));
- t->array_count++;
- mutable_array(t)[key].val = val.val;
- } else {
- if (isfull(&t->t)) {
- /* Need to resize the hash part, but we re-use the array part. */
- size_t i;
- upb_table new_table;
- if (!init(&new_table, t->t.ctype, t->t.size_lg2 + 1, a)) {
- return false;
- }
- for (i = begin(&t->t); i < upb_table_size(&t->t); i = next(&t->t, i)) {
- const upb_tabent *e = &t->t.entries[i];
- uint32_t hash;
- upb_value v;
- _upb_value_setval(&v, e->val.val, t->t.ctype);
- hash = upb_inthash(e->key);
- insert(&new_table, intkey(e->key), e->key, v, hash, &inthash, &inteql);
- }
- UPB_ASSERT(t->t.count == new_table.count);
- uninit(&t->t, a);
- t->t = new_table;
- }
- insert(&t->t, intkey(key), key, val, upb_inthash(key), &inthash, &inteql);
- }
- check(t);
- return true;
- }
- bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v) {
- const upb_tabval *table_v = inttable_val_const(t, key);
- if (!table_v) return false;
- if (v) _upb_value_setval(v, table_v->val, t->t.ctype);
- return true;
- }
- bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val) {
- upb_tabval *table_v = inttable_val(t, key);
- if (!table_v) return false;
- table_v->val = val.val;
- return true;
- }
- bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val) {
- bool success;
- if (key < t->array_size) {
- if (upb_arrhas(t->array[key])) {
- upb_tabval empty = UPB_TABVALUE_EMPTY_INIT;
- t->array_count--;
- if (val) {
- _upb_value_setval(val, t->array[key].val, t->t.ctype);
- }
- mutable_array(t)[key] = empty;
- success = true;
- } else {
- success = false;
- }
- } else {
- success = rm(&t->t, intkey(key), val, NULL, upb_inthash(key), &inteql);
- }
- check(t);
- return success;
- }
- bool upb_inttable_push2(upb_inttable *t, upb_value val, upb_alloc *a) {
- upb_check_alloc(&t->t, a);
- return upb_inttable_insert2(t, upb_inttable_count(t), val, a);
- }
- upb_value upb_inttable_pop(upb_inttable *t) {
- upb_value val;
- bool ok = upb_inttable_remove(t, upb_inttable_count(t) - 1, &val);
- UPB_ASSERT(ok);
- return val;
- }
- bool upb_inttable_insertptr2(upb_inttable *t, const void *key, upb_value val,
- upb_alloc *a) {
- upb_check_alloc(&t->t, a);
- return upb_inttable_insert2(t, (uintptr_t)key, val, a);
- }
- bool upb_inttable_lookupptr(const upb_inttable *t, const void *key,
- upb_value *v) {
- return upb_inttable_lookup(t, (uintptr_t)key, v);
- }
- bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val) {
- return upb_inttable_remove(t, (uintptr_t)key, val);
- }
- void upb_inttable_compact2(upb_inttable *t, upb_alloc *a) {
- /* A power-of-two histogram of the table keys. */
- size_t counts[UPB_MAXARRSIZE + 1] = {0};
- /* The max key in each bucket. */
- uintptr_t max[UPB_MAXARRSIZE + 1] = {0};
- upb_inttable_iter i;
- size_t arr_count;
- int size_lg2;
- upb_inttable new_t;
- upb_check_alloc(&t->t, a);
- upb_inttable_begin(&i, t);
- for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
- uintptr_t key = upb_inttable_iter_key(&i);
- int bucket = log2ceil(key);
- max[bucket] = UPB_MAX(max[bucket], key);
- counts[bucket]++;
- }
- /* Find the largest power of two that satisfies the MIN_DENSITY
- * definition (while actually having some keys). */
- arr_count = upb_inttable_count(t);
- for (size_lg2 = ARRAY_SIZE(counts) - 1; size_lg2 > 0; size_lg2--) {
- if (counts[size_lg2] == 0) {
- /* We can halve again without losing any entries. */
- continue;
- } else if (arr_count >= (1 << size_lg2) * MIN_DENSITY) {
- break;
- }
- arr_count -= counts[size_lg2];
- }
- UPB_ASSERT(arr_count <= upb_inttable_count(t));
- {
- /* Insert all elements into new, perfectly-sized table. */
- size_t arr_size = max[size_lg2] + 1; /* +1 so arr[max] will fit. */
- size_t hash_count = upb_inttable_count(t) - arr_count;
- size_t hash_size = hash_count ? (hash_count / MAX_LOAD) + 1 : 0;
- size_t hashsize_lg2 = log2ceil(hash_size);
- upb_inttable_sizedinit(&new_t, t->t.ctype, arr_size, hashsize_lg2, a);
- upb_inttable_begin(&i, t);
- for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
- uintptr_t k = upb_inttable_iter_key(&i);
- upb_inttable_insert2(&new_t, k, upb_inttable_iter_value(&i), a);
- }
- UPB_ASSERT(new_t.array_size == arr_size);
- UPB_ASSERT(new_t.t.size_lg2 == hashsize_lg2);
- }
- upb_inttable_uninit2(t, a);
- *t = new_t;
- }
- /* Iteration. */
- static const upb_tabent *int_tabent(const upb_inttable_iter *i) {
- UPB_ASSERT(!i->array_part);
- return &i->t->t.entries[i->index];
- }
- static upb_tabval int_arrent(const upb_inttable_iter *i) {
- UPB_ASSERT(i->array_part);
- return i->t->array[i->index];
- }
- void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t) {
- i->t = t;
- i->index = -1;
- i->array_part = true;
- upb_inttable_next(i);
- }
- void upb_inttable_next(upb_inttable_iter *iter) {
- const upb_inttable *t = iter->t;
- if (iter->array_part) {
- while (++iter->index < t->array_size) {
- if (upb_arrhas(int_arrent(iter))) {
- return;
- }
- }
- iter->array_part = false;
- iter->index = begin(&t->t);
- } else {
- iter->index = next(&t->t, iter->index);
- }
- }
- bool upb_inttable_done(const upb_inttable_iter *i) {
- if (i->array_part) {
- return i->index >= i->t->array_size ||
- !upb_arrhas(int_arrent(i));
- } else {
- return i->index >= upb_table_size(&i->t->t) ||
- upb_tabent_isempty(int_tabent(i));
- }
- }
- uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i) {
- UPB_ASSERT(!upb_inttable_done(i));
- return i->array_part ? i->index : int_tabent(i)->key;
- }
- upb_value upb_inttable_iter_value(const upb_inttable_iter *i) {
- UPB_ASSERT(!upb_inttable_done(i));
- return _upb_value_val(
- i->array_part ? i->t->array[i->index].val : int_tabent(i)->val.val,
- i->t->t.ctype);
- }
- void upb_inttable_iter_setdone(upb_inttable_iter *i) {
- i->index = SIZE_MAX;
- i->array_part = false;
- }
- bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
- const upb_inttable_iter *i2) {
- if (upb_inttable_done(i1) && upb_inttable_done(i2))
- return true;
- return i1->t == i2->t && i1->index == i2->index &&
- i1->array_part == i2->array_part;
- }
- #ifdef UPB_UNALIGNED_READS_OK
- /* -----------------------------------------------------------------------------
- * MurmurHash2, by Austin Appleby (released as public domain).
- * Reformatted and C99-ified by Joshua Haberman.
- * Note - This code makes a few assumptions about how your machine behaves -
- * 1. We can read a 4-byte value from any address without crashing
- * 2. sizeof(int) == 4 (in upb this limitation is removed by using uint32_t
- * And it has a few limitations -
- * 1. It will not work incrementally.
- * 2. It will not produce the same results on little-endian and big-endian
- * machines. */
- uint32_t MurmurHash2(const void *key, size_t len, uint32_t seed) {
- /* 'm' and 'r' are mixing constants generated offline.
- * They're not really 'magic', they just happen to work well. */
- const uint32_t m = 0x5bd1e995;
- const int32_t r = 24;
- /* Initialize the hash to a 'random' value */
- uint32_t h = seed ^ len;
- /* Mix 4 bytes at a time into the hash */
- const uint8_t * data = (const uint8_t *)key;
- while(len >= 4) {
- uint32_t k = *(uint32_t *)data;
- k *= m;
- k ^= k >> r;
- k *= m;
- h *= m;
- h ^= k;
- data += 4;
- len -= 4;
- }
- /* Handle the last few bytes of the input array */
- switch(len) {
- case 3: h ^= data[2] << 16;
- case 2: h ^= data[1] << 8;
- case 1: h ^= data[0]; h *= m;
- };
- /* Do a few final mixes of the hash to ensure the last few
- * bytes are well-incorporated. */
- h ^= h >> 13;
- h *= m;
- h ^= h >> 15;
- return h;
- }
- #else /* !UPB_UNALIGNED_READS_OK */
- /* -----------------------------------------------------------------------------
- * MurmurHashAligned2, by Austin Appleby
- * Same algorithm as MurmurHash2, but only does aligned reads - should be safer
- * on certain platforms.
- * Performance will be lower than MurmurHash2 */
- #define MIX(h,k,m) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }
- uint32_t MurmurHash2(const void * key, size_t len, uint32_t seed) {
- const uint32_t m = 0x5bd1e995;
- const int32_t r = 24;
- const uint8_t * data = (const uint8_t *)key;
- uint32_t h = seed ^ len;
- uint8_t align = (uintptr_t)data & 3;
- if(align && (len >= 4)) {
- /* Pre-load the temp registers */
- uint32_t t = 0, d = 0;
- int32_t sl;
- int32_t sr;
- switch(align) {
- case 1: t |= data[2] << 16;
- case 2: t |= data[1] << 8;
- case 3: t |= data[0];
- }
- t <<= (8 * align);
- data += 4-align;
- len -= 4-align;
- sl = 8 * (4-align);
- sr = 8 * align;
- /* Mix */
- while(len >= 4) {
- uint32_t k;
- d = *(uint32_t *)data;
- t = (t >> sr) | (d << sl);
- k = t;
- MIX(h,k,m);
- t = d;
- data += 4;
- len -= 4;
- }
- /* Handle leftover data in temp registers */
- d = 0;
- if(len >= align) {
- uint32_t k;
- switch(align) {
- case 3: d |= data[2] << 16;
- case 2: d |= data[1] << 8;
- case 1: d |= data[0];
- }
- k = (t >> sr) | (d << sl);
- MIX(h,k,m);
- data += align;
- len -= align;
- /* ----------
- * Handle tail bytes */
- switch(len) {
- case 3: h ^= data[2] << 16;
- case 2: h ^= data[1] << 8;
- case 1: h ^= data[0]; h *= m;
- };
- } else {
- switch(len) {
- case 3: d |= data[2] << 16;
- case 2: d |= data[1] << 8;
- case 1: d |= data[0];
- case 0: h ^= (t >> sr) | (d << sl); h *= m;
- }
- }
- h ^= h >> 13;
- h *= m;
- h ^= h >> 15;
- return h;
- } else {
- while(len >= 4) {
- uint32_t k = *(uint32_t *)data;
- MIX(h,k,m);
- data += 4;
- len -= 4;
- }
- /* ----------
- * Handle tail bytes */
- switch(len) {
- case 3: h ^= data[2] << 16;
- case 2: h ^= data[1] << 8;
- case 1: h ^= data[0]; h *= m;
- };
- h ^= h >> 13;
- h *= m;
- h ^= h >> 15;
- return h;
- }
- }
- #undef MIX
- #endif /* UPB_UNALIGNED_READS_OK */
- #include <errno.h>
- #include <stdarg.h>
- #include <stddef.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- bool upb_dumptostderr(void *closure, const upb_status* status) {
- UPB_UNUSED(closure);
- fprintf(stderr, "%s\n", upb_status_errmsg(status));
- return false;
- }
- /* Guarantee null-termination and provide ellipsis truncation.
- * It may be tempting to "optimize" this by initializing these final
- * four bytes up-front and then being careful never to overwrite them,
- * this is safer and simpler. */
- static void nullz(upb_status *status) {
- const char *ellipsis = "...";
- size_t len = strlen(ellipsis);
- UPB_ASSERT(sizeof(status->msg) > len);
- memcpy(status->msg + sizeof(status->msg) - len, ellipsis, len);
- }
- /* upb_upberr *****************************************************************/
- upb_errorspace upb_upberr = {"upb error"};
- void upb_upberr_setoom(upb_status *status) {
- status->error_space_ = &upb_upberr;
- upb_status_seterrmsg(status, "Out of memory");
- }
- /* upb_status *****************************************************************/
- void upb_status_clear(upb_status *status) {
- if (!status) return;
- status->ok_ = true;
- status->code_ = 0;
- status->msg[0] = '\0';
- }
- bool upb_ok(const upb_status *status) { return status->ok_; }
- upb_errorspace *upb_status_errspace(const upb_status *status) {
- return status->error_space_;
- }
- int upb_status_errcode(const upb_status *status) { return status->code_; }
- const char *upb_status_errmsg(const upb_status *status) { return status->msg; }
- void upb_status_seterrmsg(upb_status *status, const char *msg) {
- if (!status) return;
- status->ok_ = false;
- strncpy(status->msg, msg, sizeof(status->msg));
- nullz(status);
- }
- void upb_status_seterrf(upb_status *status, const char *fmt, ...) {
- va_list args;
- va_start(args, fmt);
- upb_status_vseterrf(status, fmt, args);
- va_end(args);
- }
- void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args) {
- if (!status) return;
- status->ok_ = false;
- _upb_vsnprintf(status->msg, sizeof(status->msg), fmt, args);
- nullz(status);
- }
- void upb_status_copy(upb_status *to, const upb_status *from) {
- if (!to) return;
- *to = *from;
- }
- /* upb_alloc ******************************************************************/
- static void *upb_global_allocfunc(upb_alloc *alloc, void *ptr, size_t oldsize,
- size_t size) {
- UPB_UNUSED(alloc);
- UPB_UNUSED(oldsize);
- if (size == 0) {
- free(ptr);
- return NULL;
- } else {
- return realloc(ptr, size);
- }
- }
- upb_alloc upb_alloc_global = {&upb_global_allocfunc};
- /* upb_arena ******************************************************************/
- /* Be conservative and choose 16 in case anyone is using SSE. */
- static const size_t maxalign = 16;
- static size_t align_up_max(size_t size) {
- return ((size + maxalign - 1) / maxalign) * maxalign;
- }
- typedef struct mem_block {
- struct mem_block *next;
- size_t size;
- size_t used;
- bool owned;
- /* Data follows. */
- } mem_block;
- typedef struct cleanup_ent {
- struct cleanup_ent *next;
- upb_cleanup_func *cleanup;
- void *ud;
- } cleanup_ent;
- static void upb_arena_addblock(upb_arena *a, void *ptr, size_t size,
- bool owned) {
- mem_block *block = ptr;
- block->next = a->block_head;
- block->size = size;
- block->used = align_up_max(sizeof(mem_block));
- block->owned = owned;
- a->block_head = block;
- /* TODO(haberman): ASAN poison. */
- }
- static mem_block *upb_arena_allocblock(upb_arena *a, size_t size) {
- size_t block_size = UPB_MAX(size, a->next_block_size) + sizeof(mem_block);
- mem_block *block = upb_malloc(a->block_alloc, block_size);
- if (!block) {
- return NULL;
- }
- upb_arena_addblock(a, block, block_size, true);
- a->next_block_size = UPB_MIN(block_size * 2, a->max_block_size);
- return block;
- }
- static void *upb_arena_doalloc(upb_alloc *alloc, void *ptr, size_t oldsize,
- size_t size) {
- upb_arena *a = (upb_arena*)alloc; /* upb_alloc is initial member. */
- mem_block *block = a->block_head;
- void *ret;
- if (size == 0) {
- return NULL; /* We are an arena, don't need individual frees. */
- }
- size = align_up_max(size);
- /* TODO(haberman): special-case if this is a realloc of the last alloc? */
- if (!block || block->size - block->used < size) {
- /* Slow path: have to allocate a new block. */
- block = upb_arena_allocblock(a, size);
- if (!block) {
- return NULL; /* Out of memory. */
- }
- }
- ret = (char*)block + block->used;
- block->used += size;
- if (oldsize > 0) {
- memcpy(ret, ptr, oldsize); /* Preserve existing data. */
- }
- /* TODO(haberman): ASAN unpoison. */
- a->bytes_allocated += size;
- return ret;
- }
- /* Public Arena API ***********************************************************/
- void upb_arena_init(upb_arena *a) {
- a->alloc.func = &upb_arena_doalloc;
- a->block_alloc = &upb_alloc_global;
- a->bytes_allocated = 0;
- a->next_block_size = 256;
- a->max_block_size = 16384;
- a->cleanup_head = NULL;
- a->block_head = NULL;
- }
- void upb_arena_init2(upb_arena *a, void *mem, size_t size, upb_alloc *alloc) {
- upb_arena_init(a);
- if (size > sizeof(mem_block)) {
- upb_arena_addblock(a, mem, size, false);
- }
- if (alloc) {
- a->block_alloc = alloc;
- }
- }
- void upb_arena_uninit(upb_arena *a) {
- cleanup_ent *ent = a->cleanup_head;
- mem_block *block = a->block_head;
- while (ent) {
- ent->cleanup(ent->ud);
- ent = ent->next;
- }
- /* Must do this after running cleanup functions, because this will delete
- * the memory we store our cleanup entries in! */
- while (block) {
- mem_block *next = block->next;
- if (block->owned) {
- upb_free(a->block_alloc, block);
- }
- block = next;
- }
- /* Protect against multiple-uninit. */
- a->cleanup_head = NULL;
- a->block_head = NULL;
- }
- bool upb_arena_addcleanup(upb_arena *a, upb_cleanup_func *func, void *ud) {
- cleanup_ent *ent = upb_malloc(&a->alloc, sizeof(cleanup_ent));
- if (!ent) {
- return false; /* Out of memory. */
- }
- ent->cleanup = func;
- ent->ud = ud;
- ent->next = a->cleanup_head;
- a->cleanup_head = ent;
- return true;
- }
- size_t upb_arena_bytesallocated(const upb_arena *a) {
- return a->bytes_allocated;
- }
- /* Standard error functions ***************************************************/
- static bool default_err(void *ud, const upb_status *status) {
- UPB_UNUSED(ud);
- UPB_UNUSED(status);
- return false;
- }
- static bool write_err_to(void *ud, const upb_status *status) {
- upb_status *copy_to = ud;
- upb_status_copy(copy_to, status);
- return false;
- }
- /* upb_env ********************************************************************/
- void upb_env_initonly(upb_env *e) {
- e->ok_ = true;
- e->error_func_ = &default_err;
- e->error_ud_ = NULL;
- }
- void upb_env_init(upb_env *e) {
- upb_arena_init(&e->arena_);
- upb_env_initonly(e);
- }
- void upb_env_init2(upb_env *e, void *mem, size_t n, upb_alloc *alloc) {
- upb_arena_init2(&e->arena_, mem, n, alloc);
- upb_env_initonly(e);
- }
- void upb_env_uninit(upb_env *e) {
- upb_arena_uninit(&e->arena_);
- }
- void upb_env_seterrorfunc(upb_env *e, upb_error_func *func, void *ud) {
- e->error_func_ = func;
- e->error_ud_ = ud;
- }
- void upb_env_reporterrorsto(upb_env *e, upb_status *s) {
- e->error_func_ = &write_err_to;
- e->error_ud_ = s;
- }
- bool upb_env_reporterror(upb_env *e, const upb_status *status) {
- e->ok_ = false;
- return e->error_func_(e->error_ud_, status);
- }
- void *upb_env_malloc(upb_env *e, size_t size) {
- return upb_malloc(&e->arena_.alloc, size);
- }
- void *upb_env_realloc(upb_env *e, void *ptr, size_t oldsize, size_t size) {
- return upb_realloc(&e->arena_.alloc, ptr, oldsize, size);
- }
- void upb_env_free(upb_env *e, void *ptr) {
- upb_free(&e->arena_.alloc, ptr);
- }
- bool upb_env_addcleanup(upb_env *e, upb_cleanup_func *func, void *ud) {
- return upb_arena_addcleanup(&e->arena_, func, ud);
- }
- size_t upb_env_bytesallocated(const upb_env *e) {
- return upb_arena_bytesallocated(&e->arena_);
- }
- /* This file was generated by upbc (the upb compiler) from the input
- * file:
- *
- * upb/descriptor/descriptor.proto
- *
- * Do not edit -- your changes will be discarded when the file is
- * regenerated. */
- static const upb_msgdef msgs[22];
- static const upb_fielddef fields[107];
- static const upb_enumdef enums[5];
- static const upb_tabent strentries[236];
- static const upb_tabent intentries[18];
- static const upb_tabval arrays[187];
- #ifdef UPB_DEBUG_REFS
- static upb_inttable reftables[268];
- #endif
- static const upb_msgdef msgs[22] = {
- UPB_MSGDEF_INIT("google.protobuf.DescriptorProto", 41, 8, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[0], 11, 10), UPB_STRTABLE_INIT(10, 15, UPB_CTYPE_PTR, 4, &strentries[0]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[0], &reftables[1]),
- UPB_MSGDEF_INIT("google.protobuf.DescriptorProto.ExtensionRange", 5, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[11], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[16]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[2], &reftables[3]),
- UPB_MSGDEF_INIT("google.protobuf.DescriptorProto.ReservedRange", 5, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[14], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[20]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[4], &reftables[5]),
- UPB_MSGDEF_INIT("google.protobuf.EnumDescriptorProto", 12, 2, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[17], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[24]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[6], &reftables[7]),
- UPB_MSGDEF_INIT("google.protobuf.EnumOptions", 9, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[0], &arrays[21], 4, 2), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[28]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[8], &reftables[9]),
- UPB_MSGDEF_INIT("google.protobuf.EnumValueDescriptorProto", 9, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[25], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[32]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[10], &reftables[11]),
- UPB_MSGDEF_INIT("google.protobuf.EnumValueOptions", 8, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[2], &arrays[29], 2, 1), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[36]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[12], &reftables[13]),
- UPB_MSGDEF_INIT("google.protobuf.FieldDescriptorProto", 24, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[31], 11, 10), UPB_STRTABLE_INIT(10, 15, UPB_CTYPE_PTR, 4, &strentries[40]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[14], &reftables[15]),
- UPB_MSGDEF_INIT("google.protobuf.FieldOptions", 13, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[4], &arrays[42], 11, 6), UPB_STRTABLE_INIT(7, 15, UPB_CTYPE_PTR, 4, &strentries[56]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[16], &reftables[17]),
- UPB_MSGDEF_INIT("google.protobuf.FileDescriptorProto", 43, 6, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[53], 13, 12), UPB_STRTABLE_INIT(12, 15, UPB_CTYPE_PTR, 4, &strentries[72]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[18], &reftables[19]),
- UPB_MSGDEF_INIT("google.protobuf.FileDescriptorSet", 7, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[66], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[88]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[20], &reftables[21]),
- UPB_MSGDEF_INIT("google.protobuf.FileOptions", 38, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[6], &arrays[68], 42, 17), UPB_STRTABLE_INIT(18, 31, UPB_CTYPE_PTR, 5, &strentries[92]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[22], &reftables[23]),
- UPB_MSGDEF_INIT("google.protobuf.MessageOptions", 11, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[8], &arrays[110], 8, 4), UPB_STRTABLE_INIT(5, 7, UPB_CTYPE_PTR, 3, &strentries[124]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[24], &reftables[25]),
- UPB_MSGDEF_INIT("google.protobuf.MethodDescriptorProto", 16, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[118], 7, 6), UPB_STRTABLE_INIT(6, 7, UPB_CTYPE_PTR, 3, &strentries[132]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[26], &reftables[27]),
- UPB_MSGDEF_INIT("google.protobuf.MethodOptions", 8, 1, UPB_INTTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &intentries[10], &arrays[125], 1, 0), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[140]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[28], &reftables[29]),
- UPB_MSGDEF_INIT("google.protobuf.OneofDescriptorProto", 6, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[126], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[144]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[30], &reftables[31]),
- UPB_MSGDEF_INIT("google.protobuf.ServiceDescriptorProto", 12, 2, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[128], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[148]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[32], &reftables[33]),
- UPB_MSGDEF_INIT("google.protobuf.ServiceOptions", 8, 1, UPB_INTTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &intentries[14], &arrays[132], 1, 0), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[152]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[34], &reftables[35]),
- UPB_MSGDEF_INIT("google.protobuf.SourceCodeInfo", 7, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[133], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[156]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[36], &reftables[37]),
- UPB_MSGDEF_INIT("google.protobuf.SourceCodeInfo.Location", 20, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[135], 7, 5), UPB_STRTABLE_INIT(5, 7, UPB_CTYPE_PTR, 3, &strentries[160]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[38], &reftables[39]),
- UPB_MSGDEF_INIT("google.protobuf.UninterpretedOption", 19, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[142], 9, 7), UPB_STRTABLE_INIT(7, 15, UPB_CTYPE_PTR, 4, &strentries[168]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[40], &reftables[41]),
- UPB_MSGDEF_INIT("google.protobuf.UninterpretedOption.NamePart", 7, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[151], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[184]), false, UPB_SYNTAX_PROTO2, UPB_WELLKNOWN_UNSPECIFIED, &reftables[42], &reftables[43]),
- };
- static const upb_fielddef fields[107] = {
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "aggregate_value", 8, &msgs[20], NULL, 16, 6, {0},&reftables[44], &reftables[45]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "allow_alias", 2, &msgs[4], NULL, 7, 1, {0},&reftables[46], &reftables[47]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "cc_enable_arenas", 31, &msgs[11], NULL, 24, 12, {0},&reftables[48], &reftables[49]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "cc_generic_services", 16, &msgs[11], NULL, 18, 6, {0},&reftables[50], &reftables[51]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "client_streaming", 5, &msgs[13], NULL, 14, 4, {0},&reftables[52], &reftables[53]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "csharp_namespace", 37, &msgs[11], NULL, 28, 14, {0},&reftables[54], &reftables[55]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "ctype", 1, &msgs[8], (const upb_def*)(&enums[2]), 7, 1, {0},&reftables[56], &reftables[57]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "default_value", 7, &msgs[7], NULL, 17, 7, {0},&reftables[58], &reftables[59]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_STRING, 0, false, false, false, false, "dependency", 3, &msgs[9], NULL, 31, 8, {0},&reftables[60], &reftables[61]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 3, &msgs[8], NULL, 9, 3, {0},&reftables[62], &reftables[63]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 33, &msgs[14], NULL, 7, 1, {0},&reftables[64], &reftables[65]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 3, &msgs[12], NULL, 9, 3, {0},&reftables[66], &reftables[67]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 23, &msgs[11], NULL, 22, 10, {0},&reftables[68], &reftables[69]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 1, &msgs[6], NULL, 7, 1, {0},&reftables[70], &reftables[71]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 3, &msgs[4], NULL, 8, 2, {0},&reftables[72], &reftables[73]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 33, &msgs[17], NULL, 7, 1, {0},&reftables[74], &reftables[75]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_DOUBLE, 0, false, false, false, false, "double_value", 6, &msgs[20], NULL, 12, 4, {0},&reftables[76], &reftables[77]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "end", 2, &msgs[2], NULL, 4, 1, {0},&reftables[78], &reftables[79]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "end", 2, &msgs[1], NULL, 4, 1, {0},&reftables[80], &reftables[81]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "enum_type", 5, &msgs[9], (const upb_def*)(&msgs[3]), 14, 1, {0},&reftables[82], &reftables[83]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "enum_type", 4, &msgs[0], (const upb_def*)(&msgs[3]), 19, 2, {0},&reftables[84], &reftables[85]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "extendee", 2, &msgs[7], NULL, 8, 2, {0},&reftables[86], &reftables[87]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "extension", 6, &msgs[0], (const upb_def*)(&msgs[7]), 25, 4, {0},&reftables[88], &reftables[89]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "extension", 7, &msgs[9], (const upb_def*)(&msgs[7]), 20, 3, {0},&reftables[90], &reftables[91]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "extension_range", 5, &msgs[0], (const upb_def*)(&msgs[1]), 22, 3, {0},&reftables[92], &reftables[93]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "field", 2, &msgs[0], (const upb_def*)(&msgs[7]), 13, 0, {0},&reftables[94], &reftables[95]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "file", 1, &msgs[10], (const upb_def*)(&msgs[9]), 6, 0, {0},&reftables[96], &reftables[97]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "go_package", 11, &msgs[11], NULL, 15, 5, {0},&reftables[98], &reftables[99]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "identifier_value", 3, &msgs[20], NULL, 7, 1, {0},&reftables[100], &reftables[101]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "input_type", 2, &msgs[13], NULL, 8, 2, {0},&reftables[102], &reftables[103]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REQUIRED, UPB_TYPE_BOOL, 0, false, false, false, false, "is_extension", 2, &msgs[21], NULL, 6, 1, {0},&reftables[104], &reftables[105]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_generate_equals_and_hash", 20, &msgs[11], NULL, 21, 9, {0},&reftables[106], &reftables[107]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_generic_services", 17, &msgs[11], NULL, 19, 7, {0},&reftables[108], &reftables[109]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_multiple_files", 10, &msgs[11], NULL, 14, 4, {0},&reftables[110], &reftables[111]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "java_outer_classname", 8, &msgs[11], NULL, 10, 2, {0},&reftables[112], &reftables[113]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "java_package", 1, &msgs[11], NULL, 7, 1, {0},&reftables[114], &reftables[115]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_string_check_utf8", 27, &msgs[11], NULL, 23, 11, {0},&reftables[116], &reftables[117]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "javanano_use_deprecated_package", 38, &msgs[11], NULL, 31, 15, {0},&reftables[118], &reftables[119]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "json_name", 10, &msgs[7], NULL, 21, 9, {0},&reftables[120], &reftables[121]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "jstype", 6, &msgs[8], (const upb_def*)(&enums[3]), 11, 5, {0},&reftables[122], &reftables[123]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "label", 4, &msgs[7], (const upb_def*)(&enums[0]), 12, 4, {0},&reftables[124], &reftables[125]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "lazy", 5, &msgs[8], NULL, 10, 4, {0},&reftables[126], &reftables[127]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "leading_comments", 3, &msgs[19], NULL, 9, 2, {0},&reftables[128], &reftables[129]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_STRING, 0, false, false, false, false, "leading_detached_comments", 6, &msgs[19], NULL, 17, 4, {0},&reftables[130], &reftables[131]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "location", 1, &msgs[18], (const upb_def*)(&msgs[19]), 6, 0, {0},&reftables[132], &reftables[133]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "map_entry", 7, &msgs[12], NULL, 10, 4, {0},&reftables[134], &reftables[135]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "message_set_wire_format", 1, &msgs[12], NULL, 7, 1, {0},&reftables[136], &reftables[137]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "message_type", 4, &msgs[9], (const upb_def*)(&msgs[0]), 11, 0, {0},&reftables[138], &reftables[139]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "method", 2, &msgs[16], (const upb_def*)(&msgs[13]), 7, 0, {0},&reftables[140], &reftables[141]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "name", 2, &msgs[20], (const upb_def*)(&msgs[21]), 6, 0, {0},&reftables[142], &reftables[143]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[5], NULL, 5, 1, {0},&reftables[144], &reftables[145]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[9], NULL, 23, 6, {0},&reftables[146], &reftables[147]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[3], NULL, 9, 2, {0},&reftables[148], &reftables[149]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[16], NULL, 9, 2, {0},&reftables[150], &reftables[151]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[15], NULL, 3, 0, {0},&reftables[152], &reftables[153]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[13], NULL, 5, 1, {0},&reftables[154], &reftables[155]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[7], NULL, 5, 1, {0},&reftables[156], &reftables[157]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[0], NULL, 33, 8, {0},&reftables[158], &reftables[159]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REQUIRED, UPB_TYPE_STRING, 0, false, false, false, false, "name_part", 1, &msgs[21], NULL, 3, 0, {0},&reftables[160], &reftables[161]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT64, UPB_INTFMT_VARIABLE, false, false, false, false, "negative_int_value", 5, &msgs[20], NULL, 11, 3, {0},&reftables[162], &reftables[163]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "nested_type", 3, &msgs[0], (const upb_def*)(&msgs[0]), 16, 1, {0},&reftables[164], &reftables[165]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "no_standard_descriptor_accessor", 2, &msgs[12], NULL, 8, 2, {0},&reftables[166], &reftables[167]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "number", 3, &msgs[7], NULL, 11, 3, {0},&reftables[168], &reftables[169]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "number", 2, &msgs[5], NULL, 8, 2, {0},&reftables[170], &reftables[171]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "objc_class_prefix", 36, &msgs[11], NULL, 25, 13, {0},&reftables[172], &reftables[173]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "oneof_decl", 8, &msgs[0], (const upb_def*)(&msgs[15]), 29, 6, {0},&reftables[174], &reftables[175]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "oneof_index", 9, &msgs[7], NULL, 20, 8, {0},&reftables[176], &reftables[177]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "optimize_for", 9, &msgs[11], (const upb_def*)(&enums[4]), 13, 3, {0},&reftables[178], &reftables[179]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 7, &msgs[0], (const upb_def*)(&msgs[12]), 26, 5, {0},&reftables[180], &reftables[181]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 8, &msgs[9], (const upb_def*)(&msgs[11]), 21, 4, {0},&reftables[182], &reftables[183]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 8, &msgs[7], (const upb_def*)(&msgs[8]), 4, 0, {0},&reftables[184], &reftables[185]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 4, &msgs[13], (const upb_def*)(&msgs[14]), 4, 0, {0},&reftables[186], &reftables[187]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 3, &msgs[16], (const upb_def*)(&msgs[17]), 8, 1, {0},&reftables[188], &reftables[189]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 3, &msgs[3], (const upb_def*)(&msgs[4]), 8, 1, {0},&reftables[190], &reftables[191]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 3, &msgs[5], (const upb_def*)(&msgs[6]), 4, 0, {0},&reftables[192], &reftables[193]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "output_type", 3, &msgs[13], NULL, 11, 3, {0},&reftables[194], &reftables[195]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "package", 2, &msgs[9], NULL, 26, 7, {0},&reftables[196], &reftables[197]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "packed", 2, &msgs[8], NULL, 8, 2, {0},&reftables[198], &reftables[199]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, true, "path", 1, &msgs[19], NULL, 5, 0, {0},&reftables[200], &reftables[201]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "php_class_prefix", 40, &msgs[11], NULL, 32, 16, {0},&reftables[202], &reftables[203]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "php_namespace", 41, &msgs[11], NULL, 35, 17, {0},&reftables[204], &reftables[205]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_UINT64, UPB_INTFMT_VARIABLE, false, false, false, false, "positive_int_value", 4, &msgs[20], NULL, 10, 2, {0},&reftables[206], &reftables[207]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "public_dependency", 10, &msgs[9], NULL, 36, 9, {0},&reftables[208], &reftables[209]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "py_generic_services", 18, &msgs[11], NULL, 20, 8, {0},&reftables[210], &reftables[211]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_STRING, 0, false, false, false, false, "reserved_name", 10, &msgs[0], NULL, 38, 9, {0},&reftables[212], &reftables[213]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "reserved_range", 9, &msgs[0], (const upb_def*)(&msgs[2]), 32, 7, {0},&reftables[214], &reftables[215]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "server_streaming", 6, &msgs[13], NULL, 15, 5, {0},&reftables[216], &reftables[217]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "service", 6, &msgs[9], (const upb_def*)(&msgs[16]), 17, 2, {0},&reftables[218], &reftables[219]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "source_code_info", 9, &msgs[9], (const upb_def*)(&msgs[18]), 22, 5, {0},&reftables[220], &reftables[221]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, true, "span", 2, &msgs[19], NULL, 8, 1, {0},&reftables[222], &reftables[223]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "start", 1, &msgs[2], NULL, 3, 0, {0},&reftables[224], &reftables[225]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "start", 1, &msgs[1], NULL, 3, 0, {0},&reftables[226], &reftables[227]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BYTES, 0, false, false, false, false, "string_value", 7, &msgs[20], NULL, 13, 5, {0},&reftables[228], &reftables[229]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "syntax", 12, &msgs[9], NULL, 40, 11, {0},&reftables[230], &reftables[231]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "trailing_comments", 4, &msgs[19], NULL, 12, 3, {0},&reftables[232], &reftables[233]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "type", 5, &msgs[7], (const upb_def*)(&enums[1]), 13, 5, {0},&reftables[234], &reftables[235]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "type_name", 6, &msgs[7], NULL, 14, 6, {0},&reftables[236], &reftables[237]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[12], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[238], &reftables[239]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[17], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[240], &reftables[241]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[11], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[242], &reftables[243]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[14], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[244], &reftables[245]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[8], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[246], &reftables[247]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[6], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[248], &reftables[249]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[4], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[250], &reftables[251]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "value", 2, &msgs[3], (const upb_def*)(&msgs[5]), 7, 0, {0},&reftables[252], &reftables[253]),
- UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "weak", 10, &msgs[8], NULL, 12, 6, {0},&reftables[254], &reftables[255]),
- UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "weak_dependency", 11, &msgs[9], NULL, 39, 10, {0},&reftables[256], &reftables[257]),
- };
- static const upb_enumdef enums[5] = {
- UPB_ENUMDEF_INIT("google.protobuf.FieldDescriptorProto.Label", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[188]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[154], 4, 3), 0, &reftables[258], &reftables[259]),
- UPB_ENUMDEF_INIT("google.protobuf.FieldDescriptorProto.Type", UPB_STRTABLE_INIT(18, 31, UPB_CTYPE_INT32, 5, &strentries[192]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[158], 19, 18), 0, &reftables[260], &reftables[261]),
- UPB_ENUMDEF_INIT("google.protobuf.FieldOptions.CType", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[224]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[177], 3, 3), 0, &reftables[262], &reftables[263]),
- UPB_ENUMDEF_INIT("google.protobuf.FieldOptions.JSType", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[228]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[180], 3, 3), 0, &reftables[264], &reftables[265]),
- UPB_ENUMDEF_INIT("google.protobuf.FileOptions.OptimizeMode", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[232]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[183], 4, 3), 0, &reftables[266], &reftables[267]),
- };
- static const upb_tabent strentries[236] = {
- {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "extension"), UPB_TABVALUE_PTR_INIT(&fields[22]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "reserved_name"), UPB_TABVALUE_PTR_INIT(&fields[84]), NULL},
- {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[57]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "field"), UPB_TABVALUE_PTR_INIT(&fields[25]), &strentries[12]},
- {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "extension_range"), UPB_TABVALUE_PTR_INIT(&fields[24]), &strentries[14]},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "nested_type"), UPB_TABVALUE_PTR_INIT(&fields[60]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "reserved_range"), UPB_TABVALUE_PTR_INIT(&fields[85]), NULL},
- {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[68]), NULL},
- {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "oneof_decl"), UPB_TABVALUE_PTR_INIT(&fields[65]), NULL},
- {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "enum_type"), UPB_TABVALUE_PTR_INIT(&fields[20]), &strentries[13]},
- {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "start"), UPB_TABVALUE_PTR_INIT(&fields[91]), NULL},
- {UPB_TABKEY_STR("\003", "\000", "\000", "\000", "end"), UPB_TABVALUE_PTR_INIT(&fields[18]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "start"), UPB_TABVALUE_PTR_INIT(&fields[90]), NULL},
- {UPB_TABKEY_STR("\003", "\000", "\000", "\000", "end"), UPB_TABVALUE_PTR_INIT(&fields[17]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "value"), UPB_TABVALUE_PTR_INIT(&fields[104]), NULL},
- {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[73]), NULL},
- {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[52]), &strentries[26]},
- {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[103]), NULL},
- {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[14]), NULL},
- {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "allow_alias"), UPB_TABVALUE_PTR_INIT(&fields[1]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "number"), UPB_TABVALUE_PTR_INIT(&fields[63]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[74]), NULL},
- {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[50]), &strentries[34]},
- {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[102]), NULL},
- {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[13]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "oneof_index"), UPB_TABVALUE_PTR_INIT(&fields[66]), NULL},
- {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "label"), UPB_TABVALUE_PTR_INIT(&fields[40]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[56]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "number"), UPB_TABVALUE_PTR_INIT(&fields[62]), &strentries[53]},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\010", "\000", "\000", "\000", "extendee"), UPB_TABVALUE_PTR_INIT(&fields[21]), NULL},
- {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "type_name"), UPB_TABVALUE_PTR_INIT(&fields[96]), NULL},
- {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "json_name"), UPB_TABVALUE_PTR_INIT(&fields[38]), NULL},
- {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "type"), UPB_TABVALUE_PTR_INIT(&fields[95]), &strentries[50]},
- {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "default_value"), UPB_TABVALUE_PTR_INIT(&fields[7]), NULL},
- {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[70]), NULL},
- {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[101]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "weak"), UPB_TABVALUE_PTR_INIT(&fields[105]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "packed"), UPB_TABVALUE_PTR_INIT(&fields[77]), NULL},
- {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "lazy"), UPB_TABVALUE_PTR_INIT(&fields[41]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "ctype"), UPB_TABVALUE_PTR_INIT(&fields[6]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "jstype"), UPB_TABVALUE_PTR_INIT(&fields[39]), NULL},
- {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[9]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "extension"), UPB_TABVALUE_PTR_INIT(&fields[23]), NULL},
- {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "weak_dependency"), UPB_TABVALUE_PTR_INIT(&fields[106]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[51]), NULL},
- {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "service"), UPB_TABVALUE_PTR_INIT(&fields[87]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "source_code_info"), UPB_TABVALUE_PTR_INIT(&fields[88]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "syntax"), UPB_TABVALUE_PTR_INIT(&fields[93]), NULL},
- {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "dependency"), UPB_TABVALUE_PTR_INIT(&fields[8]), NULL},
- {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "message_type"), UPB_TABVALUE_PTR_INIT(&fields[47]), NULL},
- {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "package"), UPB_TABVALUE_PTR_INIT(&fields[76]), NULL},
- {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[69]), &strentries[86]},
- {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "enum_type"), UPB_TABVALUE_PTR_INIT(&fields[19]), NULL},
- {UPB_TABKEY_STR("\021", "\000", "\000", "\000", "public_dependency"), UPB_TABVALUE_PTR_INIT(&fields[82]), &strentries[85]},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "file"), UPB_TABVALUE_PTR_INIT(&fields[26]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\023", "\000", "\000", "\000", "cc_generic_services"), UPB_TABVALUE_PTR_INIT(&fields[3]), NULL},
- {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "csharp_namespace"), UPB_TABVALUE_PTR_INIT(&fields[5]), &strentries[116]},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "go_package"), UPB_TABVALUE_PTR_INIT(&fields[27]), NULL},
- {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "java_package"), UPB_TABVALUE_PTR_INIT(&fields[35]), &strentries[120]},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "java_outer_classname"), UPB_TABVALUE_PTR_INIT(&fields[34]), NULL},
- {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "php_namespace"), UPB_TABVALUE_PTR_INIT(&fields[80]), &strentries[113]},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\023", "\000", "\000", "\000", "java_multiple_files"), UPB_TABVALUE_PTR_INIT(&fields[33]), &strentries[117]},
- {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[99]), NULL},
- {UPB_TABKEY_STR("\025", "\000", "\000", "\000", "java_generic_services"), UPB_TABVALUE_PTR_INIT(&fields[32]), &strentries[118]},
- {UPB_TABKEY_STR("\035", "\000", "\000", "\000", "java_generate_equals_and_hash"), UPB_TABVALUE_PTR_INIT(&fields[31]), NULL},
- {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "php_class_prefix"), UPB_TABVALUE_PTR_INIT(&fields[79]), NULL},
- {UPB_TABKEY_STR("\037", "\000", "\000", "\000", "javanano_use_deprecated_package"), UPB_TABVALUE_PTR_INIT(&fields[37]), &strentries[123]},
- {UPB_TABKEY_STR("\023", "\000", "\000", "\000", "py_generic_services"), UPB_TABVALUE_PTR_INIT(&fields[83]), NULL},
- {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "optimize_for"), UPB_TABVALUE_PTR_INIT(&fields[67]), NULL},
- {UPB_TABKEY_STR("\026", "\000", "\000", "\000", "java_string_check_utf8"), UPB_TABVALUE_PTR_INIT(&fields[36]), NULL},
- {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[12]), &strentries[119]},
- {UPB_TABKEY_STR("\021", "\000", "\000", "\000", "objc_class_prefix"), UPB_TABVALUE_PTR_INIT(&fields[64]), NULL},
- {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "cc_enable_arenas"), UPB_TABVALUE_PTR_INIT(&fields[2]), NULL},
- {UPB_TABKEY_STR("\027", "\000", "\000", "\000", "message_set_wire_format"), UPB_TABVALUE_PTR_INIT(&fields[46]), &strentries[128]},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[97]), NULL},
- {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[11]), NULL},
- {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "map_entry"), UPB_TABVALUE_PTR_INIT(&fields[45]), NULL},
- {UPB_TABKEY_STR("\037", "\000", "\000", "\000", "no_standard_descriptor_accessor"), UPB_TABVALUE_PTR_INIT(&fields[61]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "client_streaming"), UPB_TABVALUE_PTR_INIT(&fields[4]), NULL},
- {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "server_streaming"), UPB_TABVALUE_PTR_INIT(&fields[86]), NULL},
- {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[55]), NULL},
- {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "input_type"), UPB_TABVALUE_PTR_INIT(&fields[29]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "output_type"), UPB_TABVALUE_PTR_INIT(&fields[75]), NULL},
- {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[71]), NULL},
- {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[100]), NULL},
- {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[10]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[54]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[72]), &strentries[150]},
- {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "method"), UPB_TABVALUE_PTR_INIT(&fields[48]), NULL},
- {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[53]), &strentries[149]},
- {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[98]), NULL},
- {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[15]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\010", "\000", "\000", "\000", "location"), UPB_TABVALUE_PTR_INIT(&fields[44]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "span"), UPB_TABVALUE_PTR_INIT(&fields[89]), &strentries[167]},
- {UPB_TABKEY_STR("\031", "\000", "\000", "\000", "leading_detached_comments"), UPB_TABVALUE_PTR_INIT(&fields[43]), &strentries[165]},
- {UPB_TABKEY_STR("\021", "\000", "\000", "\000", "trailing_comments"), UPB_TABVALUE_PTR_INIT(&fields[94]), NULL},
- {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "leading_comments"), UPB_TABVALUE_PTR_INIT(&fields[42]), &strentries[164]},
- {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "path"), UPB_TABVALUE_PTR_INIT(&fields[78]), NULL},
- {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "double_value"), UPB_TABVALUE_PTR_INIT(&fields[16]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[49]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\022", "\000", "\000", "\000", "negative_int_value"), UPB_TABVALUE_PTR_INIT(&fields[59]), NULL},
- {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "aggregate_value"), UPB_TABVALUE_PTR_INIT(&fields[0]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\022", "\000", "\000", "\000", "positive_int_value"), UPB_TABVALUE_PTR_INIT(&fields[81]), NULL},
- {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "identifier_value"), UPB_TABVALUE_PTR_INIT(&fields[28]), NULL},
- {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "string_value"), UPB_TABVALUE_PTR_INIT(&fields[92]), &strentries[182]},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "is_extension"), UPB_TABVALUE_PTR_INIT(&fields[30]), NULL},
- {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "name_part"), UPB_TABVALUE_PTR_INIT(&fields[58]), NULL},
- {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "LABEL_REQUIRED"), UPB_TABVALUE_INT_INIT(2), &strentries[190]},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "LABEL_REPEATED"), UPB_TABVALUE_INT_INIT(3), NULL},
- {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "LABEL_OPTIONAL"), UPB_TABVALUE_INT_INIT(1), NULL},
- {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "TYPE_FIXED64"), UPB_TABVALUE_INT_INIT(6), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_STRING"), UPB_TABVALUE_INT_INIT(9), NULL},
- {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_FLOAT"), UPB_TABVALUE_INT_INIT(2), &strentries[221]},
- {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_DOUBLE"), UPB_TABVALUE_INT_INIT(1), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_INT32"), UPB_TABVALUE_INT_INIT(5), NULL},
- {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "TYPE_SFIXED32"), UPB_TABVALUE_INT_INIT(15), NULL},
- {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "TYPE_FIXED32"), UPB_TABVALUE_INT_INIT(7), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "TYPE_MESSAGE"), UPB_TABVALUE_INT_INIT(11), &strentries[222]},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_INT64"), UPB_TABVALUE_INT_INIT(3), &strentries[219]},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "TYPE_ENUM"), UPB_TABVALUE_INT_INIT(14), NULL},
- {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_UINT32"), UPB_TABVALUE_INT_INIT(13), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_UINT64"), UPB_TABVALUE_INT_INIT(4), &strentries[218]},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "TYPE_SFIXED64"), UPB_TABVALUE_INT_INIT(16), NULL},
- {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_BYTES"), UPB_TABVALUE_INT_INIT(12), NULL},
- {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_SINT64"), UPB_TABVALUE_INT_INIT(18), NULL},
- {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "TYPE_BOOL"), UPB_TABVALUE_INT_INIT(8), NULL},
- {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_GROUP"), UPB_TABVALUE_INT_INIT(10), NULL},
- {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_SINT32"), UPB_TABVALUE_INT_INIT(17), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "CORD"), UPB_TABVALUE_INT_INIT(1), NULL},
- {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "STRING"), UPB_TABVALUE_INT_INIT(0), &strentries[225]},
- {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "STRING_PIECE"), UPB_TABVALUE_INT_INIT(2), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "JS_NORMAL"), UPB_TABVALUE_INT_INIT(0), NULL},
- {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "JS_NUMBER"), UPB_TABVALUE_INT_INIT(2), NULL},
- {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "JS_STRING"), UPB_TABVALUE_INT_INIT(1), NULL},
- {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "CODE_SIZE"), UPB_TABVALUE_INT_INIT(2), NULL},
- {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "SPEED"), UPB_TABVALUE_INT_INIT(1), &strentries[235]},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "LITE_RUNTIME"), UPB_TABVALUE_INT_INIT(3), NULL},
- };
- static const upb_tabent intentries[18] = {
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[103]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[102]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[101]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[99]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[97]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NUM(33), UPB_TABVALUE_PTR_INIT(&fields[10]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[100]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NUM(33), UPB_TABVALUE_PTR_INIT(&fields[15]), NULL},
- {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
- {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[98]), NULL},
- };
- static const upb_tabval arrays[187] = {
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[57]),
- UPB_TABVALUE_PTR_INIT(&fields[25]),
- UPB_TABVALUE_PTR_INIT(&fields[60]),
- UPB_TABVALUE_PTR_INIT(&fields[20]),
- UPB_TABVALUE_PTR_INIT(&fields[24]),
- UPB_TABVALUE_PTR_INIT(&fields[22]),
- UPB_TABVALUE_PTR_INIT(&fields[68]),
- UPB_TABVALUE_PTR_INIT(&fields[65]),
- UPB_TABVALUE_PTR_INIT(&fields[85]),
- UPB_TABVALUE_PTR_INIT(&fields[84]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[91]),
- UPB_TABVALUE_PTR_INIT(&fields[18]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[90]),
- UPB_TABVALUE_PTR_INIT(&fields[17]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[52]),
- UPB_TABVALUE_PTR_INIT(&fields[104]),
- UPB_TABVALUE_PTR_INIT(&fields[73]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[1]),
- UPB_TABVALUE_PTR_INIT(&fields[14]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[50]),
- UPB_TABVALUE_PTR_INIT(&fields[63]),
- UPB_TABVALUE_PTR_INIT(&fields[74]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[13]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[56]),
- UPB_TABVALUE_PTR_INIT(&fields[21]),
- UPB_TABVALUE_PTR_INIT(&fields[62]),
- UPB_TABVALUE_PTR_INIT(&fields[40]),
- UPB_TABVALUE_PTR_INIT(&fields[95]),
- UPB_TABVALUE_PTR_INIT(&fields[96]),
- UPB_TABVALUE_PTR_INIT(&fields[7]),
- UPB_TABVALUE_PTR_INIT(&fields[70]),
- UPB_TABVALUE_PTR_INIT(&fields[66]),
- UPB_TABVALUE_PTR_INIT(&fields[38]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[6]),
- UPB_TABVALUE_PTR_INIT(&fields[77]),
- UPB_TABVALUE_PTR_INIT(&fields[9]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[41]),
- UPB_TABVALUE_PTR_INIT(&fields[39]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[105]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[51]),
- UPB_TABVALUE_PTR_INIT(&fields[76]),
- UPB_TABVALUE_PTR_INIT(&fields[8]),
- UPB_TABVALUE_PTR_INIT(&fields[47]),
- UPB_TABVALUE_PTR_INIT(&fields[19]),
- UPB_TABVALUE_PTR_INIT(&fields[87]),
- UPB_TABVALUE_PTR_INIT(&fields[23]),
- UPB_TABVALUE_PTR_INIT(&fields[69]),
- UPB_TABVALUE_PTR_INIT(&fields[88]),
- UPB_TABVALUE_PTR_INIT(&fields[82]),
- UPB_TABVALUE_PTR_INIT(&fields[106]),
- UPB_TABVALUE_PTR_INIT(&fields[93]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[26]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[35]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[34]),
- UPB_TABVALUE_PTR_INIT(&fields[67]),
- UPB_TABVALUE_PTR_INIT(&fields[33]),
- UPB_TABVALUE_PTR_INIT(&fields[27]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[3]),
- UPB_TABVALUE_PTR_INIT(&fields[32]),
- UPB_TABVALUE_PTR_INIT(&fields[83]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[31]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[12]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[36]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[2]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[64]),
- UPB_TABVALUE_PTR_INIT(&fields[5]),
- UPB_TABVALUE_PTR_INIT(&fields[37]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[79]),
- UPB_TABVALUE_PTR_INIT(&fields[80]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[46]),
- UPB_TABVALUE_PTR_INIT(&fields[61]),
- UPB_TABVALUE_PTR_INIT(&fields[11]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[45]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[55]),
- UPB_TABVALUE_PTR_INIT(&fields[29]),
- UPB_TABVALUE_PTR_INIT(&fields[75]),
- UPB_TABVALUE_PTR_INIT(&fields[71]),
- UPB_TABVALUE_PTR_INIT(&fields[4]),
- UPB_TABVALUE_PTR_INIT(&fields[86]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[54]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[53]),
- UPB_TABVALUE_PTR_INIT(&fields[48]),
- UPB_TABVALUE_PTR_INIT(&fields[72]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[44]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[78]),
- UPB_TABVALUE_PTR_INIT(&fields[89]),
- UPB_TABVALUE_PTR_INIT(&fields[42]),
- UPB_TABVALUE_PTR_INIT(&fields[94]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[43]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[49]),
- UPB_TABVALUE_PTR_INIT(&fields[28]),
- UPB_TABVALUE_PTR_INIT(&fields[81]),
- UPB_TABVALUE_PTR_INIT(&fields[59]),
- UPB_TABVALUE_PTR_INIT(&fields[16]),
- UPB_TABVALUE_PTR_INIT(&fields[92]),
- UPB_TABVALUE_PTR_INIT(&fields[0]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT(&fields[58]),
- UPB_TABVALUE_PTR_INIT(&fields[30]),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT("LABEL_OPTIONAL"),
- UPB_TABVALUE_PTR_INIT("LABEL_REQUIRED"),
- UPB_TABVALUE_PTR_INIT("LABEL_REPEATED"),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT("TYPE_DOUBLE"),
- UPB_TABVALUE_PTR_INIT("TYPE_FLOAT"),
- UPB_TABVALUE_PTR_INIT("TYPE_INT64"),
- UPB_TABVALUE_PTR_INIT("TYPE_UINT64"),
- UPB_TABVALUE_PTR_INIT("TYPE_INT32"),
- UPB_TABVALUE_PTR_INIT("TYPE_FIXED64"),
- UPB_TABVALUE_PTR_INIT("TYPE_FIXED32"),
- UPB_TABVALUE_PTR_INIT("TYPE_BOOL"),
- UPB_TABVALUE_PTR_INIT("TYPE_STRING"),
- UPB_TABVALUE_PTR_INIT("TYPE_GROUP"),
- UPB_TABVALUE_PTR_INIT("TYPE_MESSAGE"),
- UPB_TABVALUE_PTR_INIT("TYPE_BYTES"),
- UPB_TABVALUE_PTR_INIT("TYPE_UINT32"),
- UPB_TABVALUE_PTR_INIT("TYPE_ENUM"),
- UPB_TABVALUE_PTR_INIT("TYPE_SFIXED32"),
- UPB_TABVALUE_PTR_INIT("TYPE_SFIXED64"),
- UPB_TABVALUE_PTR_INIT("TYPE_SINT32"),
- UPB_TABVALUE_PTR_INIT("TYPE_SINT64"),
- UPB_TABVALUE_PTR_INIT("STRING"),
- UPB_TABVALUE_PTR_INIT("CORD"),
- UPB_TABVALUE_PTR_INIT("STRING_PIECE"),
- UPB_TABVALUE_PTR_INIT("JS_NORMAL"),
- UPB_TABVALUE_PTR_INIT("JS_STRING"),
- UPB_TABVALUE_PTR_INIT("JS_NUMBER"),
- UPB_TABVALUE_EMPTY_INIT,
- UPB_TABVALUE_PTR_INIT("SPEED"),
- UPB_TABVALUE_PTR_INIT("CODE_SIZE"),
- UPB_TABVALUE_PTR_INIT("LITE_RUNTIME"),
- };
- #ifdef UPB_DEBUG_REFS
- static upb_inttable reftables[268] = {
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
- };
- #endif
- static const upb_msgdef *refm(const upb_msgdef *m, const void *owner) {
- upb_msgdef_ref(m, owner);
- return m;
- }
- static const upb_enumdef *refe(const upb_enumdef *e, const void *owner) {
- upb_enumdef_ref(e, owner);
- return e;
- }
- /* Public API. */
- const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_get(const void *owner) { return refm(&msgs[0], owner); }
- const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_get(const void *owner) { return refm(&msgs[1], owner); }
- const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_get(const void *owner) { return refm(&msgs[2], owner); }
- const upb_msgdef *upbdefs_google_protobuf_EnumDescriptorProto_get(const void *owner) { return refm(&msgs[3], owner); }
- const upb_msgdef *upbdefs_google_protobuf_EnumOptions_get(const void *owner) { return refm(&msgs[4], owner); }
- const upb_msgdef *upbdefs_google_protobuf_EnumValueDescriptorProto_get(const void *owner) { return refm(&msgs[5], owner); }
- const upb_msgdef *upbdefs_google_protobuf_EnumValueOptions_get(const void *owner) { return refm(&msgs[6], owner); }
- const upb_msgdef *upbdefs_google_protobuf_FieldDescriptorProto_get(const void *owner) { return refm(&msgs[7], owner); }
- const upb_msgdef *upbdefs_google_protobuf_FieldOptions_get(const void *owner) { return refm(&msgs[8], owner); }
- const upb_msgdef *upbdefs_google_protobuf_FileDescriptorProto_get(const void *owner) { return refm(&msgs[9], owner); }
- const upb_msgdef *upbdefs_google_protobuf_FileDescriptorSet_get(const void *owner) { return refm(&msgs[10], owner); }
- const upb_msgdef *upbdefs_google_protobuf_FileOptions_get(const void *owner) { return refm(&msgs[11], owner); }
- const upb_msgdef *upbdefs_google_protobuf_MessageOptions_get(const void *owner) { return refm(&msgs[12], owner); }
- const upb_msgdef *upbdefs_google_protobuf_MethodDescriptorProto_get(const void *owner) { return refm(&msgs[13], owner); }
- const upb_msgdef *upbdefs_google_protobuf_MethodOptions_get(const void *owner) { return refm(&msgs[14], owner); }
- const upb_msgdef *upbdefs_google_protobuf_OneofDescriptorProto_get(const void *owner) { return refm(&msgs[15], owner); }
- const upb_msgdef *upbdefs_google_protobuf_ServiceDescriptorProto_get(const void *owner) { return refm(&msgs[16], owner); }
- const upb_msgdef *upbdefs_google_protobuf_ServiceOptions_get(const void *owner) { return refm(&msgs[17], owner); }
- const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_get(const void *owner) { return refm(&msgs[18], owner); }
- const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_Location_get(const void *owner) { return refm(&msgs[19], owner); }
- const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_get(const void *owner) { return refm(&msgs[20], owner); }
- const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_NamePart_get(const void *owner) { return refm(&msgs[21], owner); }
- const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Label_get(const void *owner) { return refe(&enums[0], owner); }
- const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Type_get(const void *owner) { return refe(&enums[1], owner); }
- const upb_enumdef *upbdefs_google_protobuf_FieldOptions_CType_get(const void *owner) { return refe(&enums[2], owner); }
- const upb_enumdef *upbdefs_google_protobuf_FieldOptions_JSType_get(const void *owner) { return refe(&enums[3], owner); }
- const upb_enumdef *upbdefs_google_protobuf_FileOptions_OptimizeMode_get(const void *owner) { return refe(&enums[4], owner); }
- /*
- ** XXX: The routines in this file that consume a string do not currently
- ** support having the string span buffers. In the future, as upb_sink and
- ** its buffering/sharing functionality evolve there should be an easy and
- ** idiomatic way of correctly handling this case. For now, we accept this
- ** limitation since we currently only parse descriptors from single strings.
- */
- #include <errno.h>
- #include <stdlib.h>
- #include <string.h>
- /* Compares a NULL-terminated string with a non-NULL-terminated string. */
- static bool upb_streq(const char *str, const char *buf, size_t n) {
- return strlen(str) == n && memcmp(str, buf, n) == 0;
- }
- /* We keep a stack of all the messages scopes we are currently in, as well as
- * the top-level file scope. This is necessary to correctly qualify the
- * definitions that are contained inside. "name" tracks the name of the
- * message or package (a bare name -- not qualified by any enclosing scopes). */
- typedef struct {
- char *name;
- /* Index of the first def that is under this scope. For msgdefs, the
- * msgdef itself is at start-1. */
- int start;
- uint32_t oneof_start;
- uint32_t oneof_index;
- } upb_descreader_frame;
- /* The maximum number of nested declarations that are allowed, ie.
- * message Foo {
- * message Bar {
- * message Baz {
- * }
- * }
- * }
- *
- * This is a resource limit that affects how big our runtime stack can grow.
- * TODO: make this a runtime-settable property of the Reader instance. */
- #define UPB_MAX_MESSAGE_NESTING 64
- struct upb_descreader {
- upb_sink sink;
- upb_inttable files;
- upb_strtable files_by_name;
- upb_filedef *file; /* The last file in files. */
- upb_descreader_frame stack[UPB_MAX_MESSAGE_NESTING];
- int stack_len;
- upb_inttable oneofs;
- uint32_t number;
- char *name;
- bool saw_number;
- bool saw_name;
- char *default_string;
- upb_fielddef *f;
- };
- static char *upb_gstrndup(const char *buf, size_t n) {
- char *ret = upb_gmalloc(n + 1);
- if (!ret) return NULL;
- memcpy(ret, buf, n);
- ret[n] = '\0';
- return ret;
- }
- /* Returns a newly allocated string that joins input strings together, for
- * example:
- * join("Foo.Bar", "Baz") -> "Foo.Bar.Baz"
- * join("", "Baz") -> "Baz"
- * Caller owns a ref on the returned string. */
- static char *upb_join(const char *base, const char *name) {
- if (!base || strlen(base) == 0) {
- return upb_gstrdup(name);
- } else {
- char *ret = upb_gmalloc(strlen(base) + strlen(name) + 2);
- if (!ret) {
- return NULL;
- }
- ret[0] = '\0';
- strcat(ret, base);
- strcat(ret, ".");
- strcat(ret, name);
- return ret;
- }
- }
- /* Qualify the defname for all defs starting with offset "start" with "str". */
- static bool upb_descreader_qualify(upb_filedef *f, char *str, int32_t start) {
- size_t i;
- for (i = start; i < upb_filedef_defcount(f); i++) {
- upb_def *def = upb_filedef_mutabledef(f, i);
- char *name = upb_join(str, upb_def_fullname(def));
- if (!name) {
- /* Need better logic here; at this point we've qualified some names but
- * not others. */
- return false;
- }
- upb_def_setfullname(def, name, NULL);
- upb_gfree(name);
- }
- return true;
- }
- /* upb_descreader ************************************************************/
- static upb_msgdef *upb_descreader_top(upb_descreader *r) {
- int index;
- UPB_ASSERT(r->stack_len > 1);
- index = r->stack[r->stack_len-1].start - 1;
- UPB_ASSERT(index >= 0);
- return upb_downcast_msgdef_mutable(upb_filedef_mutabledef(r->file, index));
- }
- static upb_def *upb_descreader_last(upb_descreader *r) {
- return upb_filedef_mutabledef(r->file, upb_filedef_defcount(r->file) - 1);
- }
- /* Start/end handlers for FileDescriptorProto and DescriptorProto (the two
- * entities that have names and can contain sub-definitions. */
- void upb_descreader_startcontainer(upb_descreader *r) {
- upb_descreader_frame *f = &r->stack[r->stack_len++];
- f->start = upb_filedef_defcount(r->file);
- f->oneof_start = upb_inttable_count(&r->oneofs);
- f->oneof_index = 0;
- f->name = NULL;
- }
- bool upb_descreader_endcontainer(upb_descreader *r) {
- upb_descreader_frame *f = &r->stack[r->stack_len - 1];
- while (upb_inttable_count(&r->oneofs) > f->oneof_start) {
- upb_oneofdef *o = upb_value_getptr(upb_inttable_pop(&r->oneofs));
- bool ok = upb_msgdef_addoneof(upb_descreader_top(r), o, &r->oneofs, NULL);
- UPB_ASSERT(ok);
- }
- if (!upb_descreader_qualify(r->file, f->name, f->start)) {
- return false;
- }
- upb_gfree(f->name);
- f->name = NULL;
- r->stack_len--;
- return true;
- }
- void upb_descreader_setscopename(upb_descreader *r, char *str) {
- upb_descreader_frame *f = &r->stack[r->stack_len-1];
- upb_gfree(f->name);
- f->name = str;
- }
- static upb_oneofdef *upb_descreader_getoneof(upb_descreader *r,
- uint32_t index) {
- bool found;
- upb_value val;
- upb_descreader_frame *f = &r->stack[r->stack_len-1];
- /* DescriptorProto messages can be nested, so we will see the nested messages
- * between when we see the FieldDescriptorProto and the OneofDescriptorProto.
- * We need to preserve the oneofs in between these two things. */
- index += f->oneof_start;
- while (upb_inttable_count(&r->oneofs) <= index) {
- upb_inttable_push(&r->oneofs, upb_value_ptr(upb_oneofdef_new(&r->oneofs)));
- }
- found = upb_inttable_lookup(&r->oneofs, index, &val);
- UPB_ASSERT(found);
- return upb_value_getptr(val);
- }
- /** Handlers for google.protobuf.FileDescriptorSet. ***************************/
- static void *fileset_startfile(void *closure, const void *hd) {
- upb_descreader *r = closure;
- UPB_UNUSED(hd);
- r->file = upb_filedef_new(&r->files);
- upb_inttable_push(&r->files, upb_value_ptr(r->file));
- return r;
- }
- /** Handlers for google.protobuf.FileDescriptorProto. *************************/
- static bool file_start(void *closure, const void *hd) {
- upb_descreader *r = closure;
- UPB_UNUSED(hd);
- upb_descreader_startcontainer(r);
- return true;
- }
- static bool file_end(void *closure, const void *hd, upb_status *status) {
- upb_descreader *r = closure;
- UPB_UNUSED(hd);
- UPB_UNUSED(status);
- return upb_descreader_endcontainer(r);
- }
- static size_t file_onname(void *closure, const void *hd, const char *buf,
- size_t n, const upb_bufhandle *handle) {
- upb_descreader *r = closure;
- char *name;
- bool ok;
- UPB_UNUSED(hd);
- UPB_UNUSED(handle);
- name = upb_gstrndup(buf, n);
- upb_strtable_insert(&r->files_by_name, name, upb_value_ptr(r->file));
- /* XXX: see comment at the top of the file. */
- ok = upb_filedef_setname(r->file, name, NULL);
- upb_gfree(name);
- UPB_ASSERT(ok);
- return n;
- }
- static size_t file_onpackage(void *closure, const void *hd, const char *buf,
- size_t n, const upb_bufhandle *handle) {
- upb_descreader *r = closure;
- char *package;
- bool ok;
- UPB_UNUSED(hd);
- UPB_UNUSED(handle);
- package = upb_gstrndup(buf, n);
- /* XXX: see comment at the top of the file. */
- upb_descreader_setscopename(r, package);
- ok = upb_filedef_setpackage(r->file, package, NULL);
- UPB_ASSERT(ok);
- return n;
- }
- static void *file_startphpnamespace(void *closure, const void *hd,
- size_t size_hint) {
- upb_descreader *r = closure;
- bool ok;
- UPB_UNUSED(hd);
- UPB_UNUSED(size_hint);
- ok = upb_filedef_setphpnamespace(r->file, "", NULL);
- UPB_ASSERT(ok);
- return closure;
- }
- static size_t file_onphpnamespace(void *closure, const void *hd,
- const char *buf, size_t n,
- const upb_bufhandle *handle) {
- upb_descreader *r = closure;
- char *php_namespace;
- bool ok;
- UPB_UNUSED(hd);
- UPB_UNUSED(handle);
- php_namespace = upb_gstrndup(buf, n);
- ok = upb_filedef_setphpnamespace(r->file, php_namespace, NULL);
- upb_gfree(php_namespace);
- UPB_ASSERT(ok);
- return n;
- }
- static size_t file_onphpprefix(void *closure, const void *hd, const char *buf,
- size_t n, const upb_bufhandle *handle) {
- upb_descreader *r = closure;
- char *prefix;
- bool ok;
- UPB_UNUSED(hd);
- UPB_UNUSED(handle);
- prefix = upb_gstrndup(buf, n);
- ok = upb_filedef_setphpprefix(r->file, prefix, NULL);
- upb_gfree(prefix);
- UPB_ASSERT(ok);
- return n;
- }
- static size_t file_onsyntax(void *closure, const void *hd, const char *buf,
- size_t n, const upb_bufhandle *handle) {
- upb_descreader *r = closure;
- bool ok;
- UPB_UNUSED(hd);
- UPB_UNUSED(handle);
- /* XXX: see comment at the top of the file. */
- if (upb_streq("proto2", buf, n)) {
- ok = upb_filedef_setsyntax(r->file, UPB_SYNTAX_PROTO2, NULL);
- } else if (upb_streq("proto3", buf, n)) {
- ok = upb_filedef_setsyntax(r->file, UPB_SYNTAX_PROTO3, NULL);
- } else {
- ok = false;
- }
- UPB_ASSERT(ok);
- return n;
- }
- static void *file_startmsg(void *closure, const void *hd) {
- upb_descreader *r = closure;
- upb_msgdef *m = upb_msgdef_new(&m);
- bool ok = upb_filedef_addmsg(r->file, m, &m, NULL);
- UPB_UNUSED(hd);
- UPB_ASSERT(ok);
- return r;
- }
- static void *file_startenum(void *closure, const void *hd) {
- upb_descreader *r = closure;
- upb_enumdef *e = upb_enumdef_new(&e);
- bool ok = upb_filedef_addenum(r->file, e, &e, NULL);
- UPB_UNUSED(hd);
- UPB_ASSERT(ok);
- return r;
- }
- static void *file_startext(void *closure, const void *hd) {
- upb_descreader *r = closure;
- r->f = upb_fielddef_new(r);
- UPB_UNUSED(hd);
- return r;
- }
- static bool file_endext(void *closure, const void *hd) {
- /* The current symtab code can't handle extensions, so we just discard
- * them for now. */
- upb_descreader *r = closure;
- upb_fielddef_unref(r->f, r);
- UPB_UNUSED(hd);
- r->f = NULL;
- return true;
- }
- static size_t file_ondep(void *closure, const void *hd, const char *buf,
- size_t n, const upb_bufhandle *handle) {
- upb_descreader *r = closure;
- upb_value val;
- if (upb_strtable_lookup2(&r->files_by_name, buf, n, &val)) {
- upb_filedef_adddep(r->file, upb_value_getptr(val));
- }
- UPB_UNUSED(hd);
- UPB_UNUSED(handle);
- return n;
- }
- /** Handlers for google.protobuf.EnumValueDescriptorProto. *********************/
- static bool enumval_startmsg(void *closure, const void *hd) {
- upb_descreader *r = closure;
- UPB_UNUSED(hd);
- r->saw_number = false;
- r->saw_name = false;
- return true;
- }
- static size_t enumval_onname(void *closure, const void *hd, const char *buf,
- size_t n, const upb_bufhandle *handle) {
- upb_descreader *r = closure;
- UPB_UNUSED(hd);
- UPB_UNUSED(handle);
- /* XXX: see comment at the top of the file. */
- upb_gfree(r->name);
- r->name = upb_gstrndup(buf, n);
- r->saw_name = true;
- return n;
- }
- static bool enumval_onnumber(void *closure, const void *hd, int32_t val) {
- upb_descreader *r = closure;
- UPB_UNUSED(hd);
- r->number = val;
- r->saw_number = true;
- return true;
- }
- static bool enumval_endmsg(void *closure, const void *hd, upb_status *status) {
- upb_descreader *r = closure;
- upb_enumdef *e;
- UPB_UNUSED(hd);
- if(!r->saw_number || !r->saw_name) {
- upb_status_seterrmsg(status, "Enum value missing name or number.");
- return false;
- }
- e = upb_downcast_enumdef_mutable(upb_descreader_last(r));
- upb_enumdef_addval(e, r->name, r->number, status);
- upb_gfree(r->name);
- r->name = NULL;
- return true;
- }
- /** Handlers for google.protobuf.EnumDescriptorProto. *************************/
- static bool enum_endmsg(void *closure, const void *hd, upb_status *status) {
- upb_descreader *r = closure;
- upb_enumdef *e;
- UPB_UNUSED(hd);
- e = upb_downcast_enumdef_mutable(upb_descreader_last(r));
- if (upb_def_fullname(upb_descreader_last(r)) == NULL) {
- upb_status_seterrmsg(status, "Enum had no name.");
- return false;
- }
- if (upb_enumdef_numvals(e) == 0) {
- upb_status_seterrmsg(status, "Enum had no values.");
- return false;
- }
- return true;
- }
- static size_t enum_onname(void *closure, const void *hd, const char *buf,
- size_t n, const upb_bufhandle *handle) {
- upb_descreader *r = closure;
- char *fullname = upb_gstrndup(buf, n);
- UPB_UNUSED(hd);
- UPB_UNUSED(handle);
- /* XXX: see comment at the top of the file. */
- upb_def_setfullname(upb_descreader_last(r), fullname, NULL);
- upb_gfree(fullname);
- return n;
- }
- /** Handlers for google.protobuf.FieldDescriptorProto *************************/
- static bool field_startmsg(void *closure, const void *hd) {
- upb_descreader *r = closure;
- UPB_UNUSED(hd);
- UPB_ASSERT(r->f);
- upb_gfree(r->default_string);
- r->default_string = NULL;
- /* fielddefs default to packed, but descriptors default to non-packed. */
- upb_fielddef_setpacked(r->f, false);
- return true;
- }
- /* Converts the default value in string "str" into "d". Passes a ref on str.
- * Returns true on success. */
- static bool parse_default(char *str, upb_fielddef *f) {
- bool success = true;
- char *end;
- switch (upb_fielddef_type(f)) {
- case UPB_TYPE_INT32: {
- long val = strtol(str, &end, 0);
- if (val > INT32_MAX || val < INT32_MIN || errno == ERANGE || *end)
- success = false;
- else
- upb_fielddef_setdefaultint32(f, val);
- break;
- }
- case UPB_TYPE_INT64: {
- /* XXX: Need to write our own strtoll, since it's not available in c89. */
- long long val = strtol(str, &end, 0);
- if (val > INT64_MAX || val < INT64_MIN || errno == ERANGE || *end)
- success = false;
- else
- upb_fielddef_setdefaultint64(f, val);
- break;
- }
- case UPB_TYPE_UINT32: {
- unsigned long val = strtoul(str, &end, 0);
- if (val > UINT32_MAX || errno == ERANGE || *end)
- success = false;
- else
- upb_fielddef_setdefaultuint32(f, val);
- break;
- }
- case UPB_TYPE_UINT64: {
- /* XXX: Need to write our own strtoull, since it's not available in c89. */
- unsigned long long val = strtoul(str, &end, 0);
- if (val > UINT64_MAX || errno == ERANGE || *end)
- success = false;
- else
- upb_fielddef_setdefaultuint64(f, val);
- break;
- }
- case UPB_TYPE_DOUBLE: {
- double val = strtod(str, &end);
- if (errno == ERANGE || *end)
- success = false;
- else
- upb_fielddef_setdefaultdouble(f, val);
- break;
- }
- case UPB_TYPE_FLOAT: {
- /* XXX: Need to write our own strtof, since it's not available in c89. */
- float val = strtod(str, &end);
- if (errno == ERANGE || *end)
- success = false;
- else
- upb_fielddef_setdefaultfloat(f, val);
- break;
- }
- case UPB_TYPE_BOOL: {
- if (strcmp(str, "false") == 0)
- upb_fielddef_setdefaultbool(f, false);
- else if (strcmp(str, "true") == 0)
- upb_fielddef_setdefaultbool(f, true);
- else
- success = false;
- break;
- }
- default: abort();
- }
- return success;
- }
- static bool field_endmsg(void *closure, const void *hd, upb_status *status) {
- upb_descreader *r = closure;
- upb_fielddef *f = r->f;
- UPB_UNUSED(hd);
- /* TODO: verify that all required fields were present. */
- UPB_ASSERT(upb_fielddef_number(f) != 0);
- UPB_ASSERT(upb_fielddef_name(f) != NULL);
- UPB_ASSERT((upb_fielddef_subdefname(f) != NULL) == upb_fielddef_hassubdef(f));
- if (r->default_string) {
- if (upb_fielddef_issubmsg(f)) {
- upb_status_seterrmsg(status, "Submessages cannot have defaults.");
- return false;
- }
- if (upb_fielddef_isstring(f) || upb_fielddef_type(f) == UPB_TYPE_ENUM) {
- upb_fielddef_setdefaultcstr(f, r->default_string, NULL);
- } else {
- if (r->default_string && !parse_default(r->default_string, f)) {
- /* We don't worry too much about giving a great error message since the
- * compiler should have ensured this was correct. */
- upb_status_seterrmsg(status, "Error converting default value.");
- return false;
- }
- }
- }
- return true;
- }
- static bool field_onlazy(void *closure, const void *hd, bool val) {
- upb_descreader *r = closure;
- UPB_UNUSED(hd);
- upb_fielddef_setlazy(r->f, val);
- return true;
- }
- static bool field_onpacked(void *closure, const void *hd, bool val) {
- upb_descreader *r = closure;
- UPB_UNUSED(hd);
- upb_fielddef_setpacked(r->f, val);
- return true;
- }
- static bool field_ontype(void *closure, const void *hd, int32_t val) {
- upb_descreader *r = closure;
- UPB_UNUSED(hd);
- upb_fielddef_setdescriptortype(r->f, val);
- return true;
- }
- static bool field_onlabel(void *closure, const void *hd, int32_t val) {
- upb_descreader *r = closure;
- UPB_UNUSED(hd);
- upb_fielddef_setlabel(r->f, val);
- return true;
- }
- static bool field_onnumber(void *closure, const void *hd, int32_t val) {
- upb_descreader *r = closure;
- bool ok;
- UPB_UNUSED(hd);
- ok = upb_fielddef_setnumber(r->f, val, NULL);
- UPB_ASSERT(ok);
- return true;
- }
- static size_t field_onname(void *closure, const void *hd, const char *buf,
- size_t n, const upb_bufhandle *handle) {
- upb_descreader *r = closure;
- char *name = upb_gstrndup(buf, n);
- UPB_UNUSED(hd);
- UPB_UNUSED(handle);
- /* XXX: see comment at the top of the file. */
- upb_fielddef_setname(r->f, name, NULL);
- upb_gfree(name);
- return n;
- }
- static size_t field_ontypename(void *closure, const void *hd, const char *buf,
- size_t n, const upb_bufhandle *handle) {
- upb_descreader *r = closure;
- char *name = upb_gstrndup(buf, n);
- UPB_UNUSED(hd);
- UPB_UNUSED(handle);
- /* XXX: see comment at the top of the file. */
- upb_fielddef_setsubdefname(r->f, name, NULL);
- upb_gfree(name);
- return n;
- }
- static size_t field_onextendee(void *closure, const void *hd, const char *buf,
- size_t n, const upb_bufhandle *handle) {
- upb_descreader *r = closure;
- char *name = upb_gstrndup(buf, n);
- UPB_UNUSED(hd);
- UPB_UNUSED(handle);
- /* XXX: see comment at the top of the file. */
- upb_fielddef_setcontainingtypename(r->f, name, NULL);
- upb_gfree(name);
- return n;
- }
- static size_t field_ondefaultval(void *closure, const void *hd, const char *buf,
- size_t n, const upb_bufhandle *handle) {
- upb_descreader *r = closure;
- UPB_UNUSED(hd);
- UPB_UNUSED(handle);
- /* Have to convert from string to the correct type, but we might not know the
- * type yet, so we save it as a string until the end of the field.
- * XXX: see comment at the top of the file. */
- upb_gfree(r->default_string);
- r->default_string = upb_gstrndup(buf, n);
- return n;
- }
- static bool field_ononeofindex(void *closure, const void *hd, int32_t index) {
- upb_descreader *r = closure;
- upb_oneofdef *o = upb_descreader_getoneof(r, index);
- bool ok = upb_oneofdef_addfield(o, r->f, &r->f, NULL);
- UPB_UNUSED(hd);
- UPB_ASSERT(ok);
- return true;
- }
- /** Handlers for google.protobuf.OneofDescriptorProto. ************************/
- static size_t oneof_name(void *closure, const void *hd, const char *buf,
- size_t n, const upb_bufhandle *handle) {
- upb_descreader *r = closure;
- upb_descreader_frame *f = &r->stack[r->stack_len-1];
- upb_oneofdef *o = upb_descreader_getoneof(r, f->oneof_index++);
- char *name_null_terminated = upb_gstrndup(buf, n);
- bool ok = upb_oneofdef_setname(o, name_null_terminated, NULL);
- UPB_UNUSED(hd);
- UPB_UNUSED(handle);
- UPB_ASSERT(ok);
- free(name_null_terminated);
- return n;
- }
- /** Handlers for google.protobuf.DescriptorProto ******************************/
- static bool msg_start(void *closure, const void *hd) {
- upb_descreader *r = closure;
- UPB_UNUSED(hd);
- upb_descreader_startcontainer(r);
- return true;
- }
- static bool msg_end(void *closure, const void *hd, upb_status *status) {
- upb_descreader *r = closure;
- upb_msgdef *m = upb_descreader_top(r);
- UPB_UNUSED(hd);
- if(!upb_def_fullname(upb_msgdef_upcast_mutable(m))) {
- upb_status_seterrmsg(status, "Encountered message with no name.");
- return false;
- }
- return upb_descreader_endcontainer(r);
- }
- static size_t msg_name(void *closure, const void *hd, const char *buf,
- size_t n, const upb_bufhandle *handle) {
- upb_descreader *r = closure;
- upb_msgdef *m = upb_descreader_top(r);
- /* XXX: see comment at the top of the file. */
- char *name = upb_gstrndup(buf, n);
- UPB_UNUSED(hd);
- UPB_UNUSED(handle);
- upb_def_setfullname(upb_msgdef_upcast_mutable(m), name, NULL);
- upb_descreader_setscopename(r, name); /* Passes ownership of name. */
- return n;
- }
- static void *msg_startmsg(void *closure, const void *hd) {
- upb_descreader *r = closure;
- upb_msgdef *m = upb_msgdef_new(&m);
- bool ok = upb_filedef_addmsg(r->file, m, &m, NULL);
- UPB_UNUSED(hd);
- UPB_ASSERT(ok);
- return r;
- }
- static void *msg_startext(void *closure, const void *hd) {
- upb_descreader *r = closure;
- r->f = upb_fielddef_new(r);
- UPB_UNUSED(hd);
- return r;
- }
- static bool msg_endext(void *closure, const void *hd) {
- /* The current symtab code can't handle extensions, so we just discard
- * them for now. */
- upb_descreader *r = closure;
- upb_fielddef_unref(r->f, r);
- UPB_UNUSED(hd);
- r->f = NULL;
- return true;
- }
- static void *msg_startfield(void *closure, const void *hd) {
- upb_descreader *r = closure;
- r->f = upb_fielddef_new(&r->f);
- /* We can't add the new field to the message until its name/number are
- * filled in. */
- UPB_UNUSED(hd);
- return r;
- }
- static bool msg_endfield(void *closure, const void *hd) {
- upb_descreader *r = closure;
- upb_msgdef *m = upb_descreader_top(r);
- bool ok;
- UPB_UNUSED(hd);
- /* Oneof fields are added to the msgdef through their oneof, so don't need to
- * be added here. */
- if (upb_fielddef_containingoneof(r->f) == NULL) {
- ok = upb_msgdef_addfield(m, r->f, &r->f, NULL);
- UPB_ASSERT(ok);
- }
- r->f = NULL;
- return true;
- }
- static bool msg_onmapentry(void *closure, const void *hd, bool mapentry) {
- upb_descreader *r = closure;
- upb_msgdef *m = upb_descreader_top(r);
- UPB_UNUSED(hd);
- upb_msgdef_setmapentry(m, mapentry);
- r->f = NULL;
- return true;
- }
- /** Code to register handlers *************************************************/
- #define F(msg, field) upbdefs_google_protobuf_ ## msg ## _f_ ## field(m)
- static void reghandlers(const void *closure, upb_handlers *h) {
- const upb_msgdef *m = upb_handlers_msgdef(h);
- UPB_UNUSED(closure);
- if (upbdefs_google_protobuf_FileDescriptorSet_is(m)) {
- upb_handlers_setstartsubmsg(h, F(FileDescriptorSet, file),
- &fileset_startfile, NULL);
- } else if (upbdefs_google_protobuf_DescriptorProto_is(m)) {
- upb_handlers_setstartmsg(h, &msg_start, NULL);
- upb_handlers_setendmsg(h, &msg_end, NULL);
- upb_handlers_setstring(h, F(DescriptorProto, name), &msg_name, NULL);
- upb_handlers_setstartsubmsg(h, F(DescriptorProto, extension), &msg_startext,
- NULL);
- upb_handlers_setendsubmsg(h, F(DescriptorProto, extension), &msg_endext,
- NULL);
- upb_handlers_setstartsubmsg(h, F(DescriptorProto, nested_type),
- &msg_startmsg, NULL);
- upb_handlers_setstartsubmsg(h, F(DescriptorProto, field),
- &msg_startfield, NULL);
- upb_handlers_setendsubmsg(h, F(DescriptorProto, field),
- &msg_endfield, NULL);
- upb_handlers_setstartsubmsg(h, F(DescriptorProto, enum_type),
- &file_startenum, NULL);
- } else if (upbdefs_google_protobuf_FileDescriptorProto_is(m)) {
- upb_handlers_setstartmsg(h, &file_start, NULL);
- upb_handlers_setendmsg(h, &file_end, NULL);
- upb_handlers_setstring(h, F(FileDescriptorProto, name), &file_onname,
- NULL);
- upb_handlers_setstring(h, F(FileDescriptorProto, package), &file_onpackage,
- NULL);
- upb_handlers_setstring(h, F(FileDescriptorProto, syntax), &file_onsyntax,
- NULL);
- upb_handlers_setstartsubmsg(h, F(FileDescriptorProto, message_type),
- &file_startmsg, NULL);
- upb_handlers_setstartsubmsg(h, F(FileDescriptorProto, enum_type),
- &file_startenum, NULL);
- upb_handlers_setstartsubmsg(h, F(FileDescriptorProto, extension),
- &file_startext, NULL);
- upb_handlers_setendsubmsg(h, F(FileDescriptorProto, extension),
- &file_endext, NULL);
- upb_handlers_setstring(h, F(FileDescriptorProto, dependency),
- &file_ondep, NULL);
- } else if (upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)) {
- upb_handlers_setstartmsg(h, &enumval_startmsg, NULL);
- upb_handlers_setendmsg(h, &enumval_endmsg, NULL);
- upb_handlers_setstring(h, F(EnumValueDescriptorProto, name), &enumval_onname, NULL);
- upb_handlers_setint32(h, F(EnumValueDescriptorProto, number), &enumval_onnumber,
- NULL);
- } else if (upbdefs_google_protobuf_EnumDescriptorProto_is(m)) {
- upb_handlers_setendmsg(h, &enum_endmsg, NULL);
- upb_handlers_setstring(h, F(EnumDescriptorProto, name), &enum_onname, NULL);
- } else if (upbdefs_google_protobuf_FieldDescriptorProto_is(m)) {
- upb_handlers_setstartmsg(h, &field_startmsg, NULL);
- upb_handlers_setendmsg(h, &field_endmsg, NULL);
- upb_handlers_setint32(h, F(FieldDescriptorProto, type), &field_ontype,
- NULL);
- upb_handlers_setint32(h, F(FieldDescriptorProto, label), &field_onlabel,
- NULL);
- upb_handlers_setint32(h, F(FieldDescriptorProto, number), &field_onnumber,
- NULL);
- upb_handlers_setstring(h, F(FieldDescriptorProto, name), &field_onname,
- NULL);
- upb_handlers_setstring(h, F(FieldDescriptorProto, type_name),
- &field_ontypename, NULL);
- upb_handlers_setstring(h, F(FieldDescriptorProto, extendee),
- &field_onextendee, NULL);
- upb_handlers_setstring(h, F(FieldDescriptorProto, default_value),
- &field_ondefaultval, NULL);
- upb_handlers_setint32(h, F(FieldDescriptorProto, oneof_index),
- &field_ononeofindex, NULL);
- } else if (upbdefs_google_protobuf_OneofDescriptorProto_is(m)) {
- upb_handlers_setstring(h, F(OneofDescriptorProto, name), &oneof_name, NULL);
- } else if (upbdefs_google_protobuf_FieldOptions_is(m)) {
- upb_handlers_setbool(h, F(FieldOptions, lazy), &field_onlazy, NULL);
- upb_handlers_setbool(h, F(FieldOptions, packed), &field_onpacked, NULL);
- } else if (upbdefs_google_protobuf_MessageOptions_is(m)) {
- upb_handlers_setbool(h, F(MessageOptions, map_entry), &msg_onmapentry, NULL);
- } else if (upbdefs_google_protobuf_FileOptions_is(m)) {
- upb_handlers_setstring(h, F(FileOptions, php_class_prefix),
- &file_onphpprefix, NULL);
- upb_handlers_setstartstr(h, F(FileOptions, php_namespace),
- &file_startphpnamespace, NULL);
- upb_handlers_setstring(h, F(FileOptions, php_namespace),
- &file_onphpnamespace, NULL);
- }
- UPB_ASSERT(upb_ok(upb_handlers_status(h)));
- }
- #undef F
- void descreader_cleanup(void *_r) {
- upb_descreader *r = _r;
- size_t i;
- for (i = 0; i < upb_descreader_filecount(r); i++) {
- upb_filedef_unref(upb_descreader_file(r, i), &r->files);
- }
- upb_gfree(r->name);
- upb_inttable_uninit(&r->files);
- upb_strtable_uninit(&r->files_by_name);
- upb_inttable_uninit(&r->oneofs);
- upb_gfree(r->default_string);
- while (r->stack_len > 0) {
- upb_descreader_frame *f = &r->stack[--r->stack_len];
- upb_gfree(f->name);
- }
- }
- /* Public API ****************************************************************/
- upb_descreader *upb_descreader_create(upb_env *e, const upb_handlers *h) {
- upb_descreader *r = upb_env_malloc(e, sizeof(upb_descreader));
- if (!r || !upb_env_addcleanup(e, descreader_cleanup, r)) {
- return NULL;
- }
- upb_inttable_init(&r->files, UPB_CTYPE_PTR);
- upb_strtable_init(&r->files_by_name, UPB_CTYPE_PTR);
- upb_inttable_init(&r->oneofs, UPB_CTYPE_PTR);
- upb_sink_reset(upb_descreader_input(r), h, r);
- r->stack_len = 0;
- r->name = NULL;
- r->default_string = NULL;
- return r;
- }
- size_t upb_descreader_filecount(const upb_descreader *r) {
- return upb_inttable_count(&r->files);
- }
- upb_filedef *upb_descreader_file(const upb_descreader *r, size_t i) {
- upb_value v;
- if (upb_inttable_lookup(&r->files, i, &v)) {
- return upb_value_getptr(v);
- } else {
- return NULL;
- }
- }
- upb_sink *upb_descreader_input(upb_descreader *r) {
- return &r->sink;
- }
- const upb_handlers *upb_descreader_newhandlers(const void *owner) {
- const upb_msgdef *m = upbdefs_google_protobuf_FileDescriptorSet_get(&m);
- const upb_handlers *h = upb_handlers_newfrozen(m, owner, reghandlers, NULL);
- upb_msgdef_unref(m, &m);
- return h;
- }
- /*
- ** protobuf decoder bytecode compiler
- **
- ** Code to compile a upb::Handlers into bytecode for decoding a protobuf
- ** according to that specific schema and destination handlers.
- **
- ** Compiling to bytecode is always the first step. If we are using the
- ** interpreted decoder we leave it as bytecode and interpret that. If we are
- ** using a JIT decoder we use a code generator to turn the bytecode into native
- ** code, LLVM IR, etc.
- **
- ** Bytecode definition is in decoder.int.h.
- */
- #include <stdarg.h>
- #ifdef UPB_DUMP_BYTECODE
- #include <stdio.h>
- #endif
- #define MAXLABEL 5
- #define EMPTYLABEL -1
- /* mgroup *********************************************************************/
- static void freegroup(upb_refcounted *r) {
- mgroup *g = (mgroup*)r;
- upb_inttable_uninit(&g->methods);
- #ifdef UPB_USE_JIT_X64
- upb_pbdecoder_freejit(g);
- #endif
- upb_gfree(g->bytecode);
- upb_gfree(g);
- }
- static void visitgroup(const upb_refcounted *r, upb_refcounted_visit *visit,
- void *closure) {
- const mgroup *g = (const mgroup*)r;
- upb_inttable_iter i;
- upb_inttable_begin(&i, &g->methods);
- for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
- upb_pbdecodermethod *method = upb_value_getptr(upb_inttable_iter_value(&i));
- visit(r, upb_pbdecodermethod_upcast(method), closure);
- }
- }
- mgroup *newgroup(const void *owner) {
- mgroup *g = upb_gmalloc(sizeof(*g));
- static const struct upb_refcounted_vtbl vtbl = {visitgroup, freegroup};
- upb_refcounted_init(mgroup_upcast_mutable(g), &vtbl, owner);
- upb_inttable_init(&g->methods, UPB_CTYPE_PTR);
- g->bytecode = NULL;
- g->bytecode_end = NULL;
- return g;
- }
- /* upb_pbdecodermethod ********************************************************/
- static void freemethod(upb_refcounted *r) {
- upb_pbdecodermethod *method = (upb_pbdecodermethod*)r;
- if (method->dest_handlers_) {
- upb_handlers_unref(method->dest_handlers_, method);
- }
- upb_inttable_uninit(&method->dispatch);
- upb_gfree(method);
- }
- static void visitmethod(const upb_refcounted *r, upb_refcounted_visit *visit,
- void *closure) {
- const upb_pbdecodermethod *m = (const upb_pbdecodermethod*)r;
- visit(r, m->group, closure);
- }
- static upb_pbdecodermethod *newmethod(const upb_handlers *dest_handlers,
- mgroup *group) {
- static const struct upb_refcounted_vtbl vtbl = {visitmethod, freemethod};
- upb_pbdecodermethod *ret = upb_gmalloc(sizeof(*ret));
- upb_refcounted_init(upb_pbdecodermethod_upcast_mutable(ret), &vtbl, &ret);
- upb_byteshandler_init(&ret->input_handler_);
- /* The method references the group and vice-versa, in a circular reference. */
- upb_ref2(ret, group);
- upb_ref2(group, ret);
- upb_inttable_insertptr(&group->methods, dest_handlers, upb_value_ptr(ret));
- upb_pbdecodermethod_unref(ret, &ret);
- ret->group = mgroup_upcast_mutable(group);
- ret->dest_handlers_ = dest_handlers;
- ret->is_native_ = false; /* If we JIT, it will update this later. */
- upb_inttable_init(&ret->dispatch, UPB_CTYPE_UINT64);
- if (ret->dest_handlers_) {
- upb_handlers_ref(ret->dest_handlers_, ret);
- }
- return ret;
- }
- const upb_handlers *upb_pbdecodermethod_desthandlers(
- const upb_pbdecodermethod *m) {
- return m->dest_handlers_;
- }
- const upb_byteshandler *upb_pbdecodermethod_inputhandler(
- const upb_pbdecodermethod *m) {
- return &m->input_handler_;
- }
- bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m) {
- return m->is_native_;
- }
- const upb_pbdecodermethod *upb_pbdecodermethod_new(
- const upb_pbdecodermethodopts *opts, const void *owner) {
- const upb_pbdecodermethod *ret;
- upb_pbcodecache cache;
- upb_pbcodecache_init(&cache);
- ret = upb_pbcodecache_getdecodermethod(&cache, opts);
- upb_pbdecodermethod_ref(ret, owner);
- upb_pbcodecache_uninit(&cache);
- return ret;
- }
- /* bytecode compiler **********************************************************/
- /* Data used only at compilation time. */
- typedef struct {
- mgroup *group;
- uint32_t *pc;
- int fwd_labels[MAXLABEL];
- int back_labels[MAXLABEL];
- /* For fields marked "lazy", parse them lazily or eagerly? */
- bool lazy;
- } compiler;
- static compiler *newcompiler(mgroup *group, bool lazy) {
- compiler *ret = upb_gmalloc(sizeof(*ret));
- int i;
- ret->group = group;
- ret->lazy = lazy;
- for (i = 0; i < MAXLABEL; i++) {
- ret->fwd_labels[i] = EMPTYLABEL;
- ret->back_labels[i] = EMPTYLABEL;
- }
- return ret;
- }
- static void freecompiler(compiler *c) {
- upb_gfree(c);
- }
- const size_t ptr_words = sizeof(void*) / sizeof(uint32_t);
- /* How many words an instruction is. */
- static int instruction_len(uint32_t instr) {
- switch (getop(instr)) {
- case OP_SETDISPATCH: return 1 + ptr_words;
- case OP_TAGN: return 3;
- case OP_SETBIGGROUPNUM: return 2;
- default: return 1;
- }
- }
- bool op_has_longofs(int32_t instruction) {
- switch (getop(instruction)) {
- case OP_CALL:
- case OP_BRANCH:
- case OP_CHECKDELIM:
- return true;
- /* The "tag" instructions only have 8 bytes available for the jump target,
- * but that is ok because these opcodes only require short jumps. */
- case OP_TAG1:
- case OP_TAG2:
- case OP_TAGN:
- return false;
- default:
- UPB_ASSERT(false);
- return false;
- }
- }
- static int32_t getofs(uint32_t instruction) {
- if (op_has_longofs(instruction)) {
- return (int32_t)instruction >> 8;
- } else {
- return (int8_t)(instruction >> 8);
- }
- }
- static void setofs(uint32_t *instruction, int32_t ofs) {
- if (op_has_longofs(*instruction)) {
- *instruction = getop(*instruction) | ofs << 8;
- } else {
- *instruction = (*instruction & ~0xff00) | ((ofs & 0xff) << 8);
- }
- UPB_ASSERT(getofs(*instruction) == ofs); /* Would fail in cases of overflow. */
- }
- static uint32_t pcofs(compiler *c) { return c->pc - c->group->bytecode; }
- /* Defines a local label at the current PC location. All previous forward
- * references are updated to point to this location. The location is noted
- * for any future backward references. */
- static void label(compiler *c, unsigned int label) {
- int val;
- uint32_t *codep;
- UPB_ASSERT(label < MAXLABEL);
- val = c->fwd_labels[label];
- codep = (val == EMPTYLABEL) ? NULL : c->group->bytecode + val;
- while (codep) {
- int ofs = getofs(*codep);
- setofs(codep, c->pc - codep - instruction_len(*codep));
- codep = ofs ? codep + ofs : NULL;
- }
- c->fwd_labels[label] = EMPTYLABEL;
- c->back_labels[label] = pcofs(c);
- }
- /* Creates a reference to a numbered label; either a forward reference
- * (positive arg) or backward reference (negative arg). For forward references
- * the value returned now is actually a "next" pointer into a linked list of all
- * instructions that use this label and will be patched later when the label is
- * defined with label().
- *
- * The returned value is the offset that should be written into the instruction.
- */
- static int32_t labelref(compiler *c, int label) {
- UPB_ASSERT(label < MAXLABEL);
- if (label == LABEL_DISPATCH) {
- /* No resolving required. */
- return 0;
- } else if (label < 0) {
- /* Backward local label. Relative to the next instruction. */
- uint32_t from = (c->pc + 1) - c->group->bytecode;
- return c->back_labels[-label] - from;
- } else {
- /* Forward local label: prepend to (possibly-empty) linked list. */
- int *lptr = &c->fwd_labels[label];
- int32_t ret = (*lptr == EMPTYLABEL) ? 0 : *lptr - pcofs(c);
- *lptr = pcofs(c);
- return ret;
- }
- }
- static void put32(compiler *c, uint32_t v) {
- mgroup *g = c->group;
- if (c->pc == g->bytecode_end) {
- int ofs = pcofs(c);
- size_t oldsize = g->bytecode_end - g->bytecode;
- size_t newsize = UPB_MAX(oldsize * 2, 64);
- /* TODO(haberman): handle OOM. */
- g->bytecode = upb_grealloc(g->bytecode, oldsize * sizeof(uint32_t),
- newsize * sizeof(uint32_t));
- g->bytecode_end = g->bytecode + newsize;
- c->pc = g->bytecode + ofs;
- }
- *c->pc++ = v;
- }
- static void putop(compiler *c, int op, ...) {
- va_list ap;
- va_start(ap, op);
- switch (op) {
- case OP_SETDISPATCH: {
- uintptr_t ptr = (uintptr_t)va_arg(ap, void*);
- put32(c, OP_SETDISPATCH);
- put32(c, ptr);
- if (sizeof(uintptr_t) > sizeof(uint32_t))
- put32(c, (uint64_t)ptr >> 32);
- break;
- }
- case OP_STARTMSG:
- case OP_ENDMSG:
- case OP_PUSHLENDELIM:
- case OP_POP:
- case OP_SETDELIM:
- case OP_HALT:
- case OP_RET:
- case OP_DISPATCH:
- put32(c, op);
- break;
- case OP_PARSE_DOUBLE:
- case OP_PARSE_FLOAT:
- case OP_PARSE_INT64:
- case OP_PARSE_UINT64:
- case OP_PARSE_INT32:
- case OP_PARSE_FIXED64:
- case OP_PARSE_FIXED32:
- case OP_PARSE_BOOL:
- case OP_PARSE_UINT32:
- case OP_PARSE_SFIXED32:
- case OP_PARSE_SFIXED64:
- case OP_PARSE_SINT32:
- case OP_PARSE_SINT64:
- case OP_STARTSEQ:
- case OP_ENDSEQ:
- case OP_STARTSUBMSG:
- case OP_ENDSUBMSG:
- case OP_STARTSTR:
- case OP_STRING:
- case OP_ENDSTR:
- case OP_PUSHTAGDELIM:
- put32(c, op | va_arg(ap, upb_selector_t) << 8);
- break;
- case OP_SETBIGGROUPNUM:
- put32(c, op);
- put32(c, va_arg(ap, int));
- break;
- case OP_CALL: {
- const upb_pbdecodermethod *method = va_arg(ap, upb_pbdecodermethod *);
- put32(c, op | (method->code_base.ofs - (pcofs(c) + 1)) << 8);
- break;
- }
- case OP_CHECKDELIM:
- case OP_BRANCH: {
- uint32_t instruction = op;
- int label = va_arg(ap, int);
- setofs(&instruction, labelref(c, label));
- put32(c, instruction);
- break;
- }
- case OP_TAG1:
- case OP_TAG2: {
- int label = va_arg(ap, int);
- uint64_t tag = va_arg(ap, uint64_t);
- uint32_t instruction = op | (tag << 16);
- UPB_ASSERT(tag <= 0xffff);
- setofs(&instruction, labelref(c, label));
- put32(c, instruction);
- break;
- }
- case OP_TAGN: {
- int label = va_arg(ap, int);
- uint64_t tag = va_arg(ap, uint64_t);
- uint32_t instruction = op | (upb_value_size(tag) << 16);
- setofs(&instruction, labelref(c, label));
- put32(c, instruction);
- put32(c, tag);
- put32(c, tag >> 32);
- break;
- }
- }
- va_end(ap);
- }
- #if defined(UPB_USE_JIT_X64) || defined(UPB_DUMP_BYTECODE)
- const char *upb_pbdecoder_getopname(unsigned int op) {
- #define QUOTE(x) #x
- #define EXPAND_AND_QUOTE(x) QUOTE(x)
- #define OPNAME(x) OP_##x
- #define OP(x) case OPNAME(x): return EXPAND_AND_QUOTE(OPNAME(x));
- #define T(x) OP(PARSE_##x)
- /* Keep in sync with list in decoder.int.h. */
- switch ((opcode)op) {
- T(DOUBLE) T(FLOAT) T(INT64) T(UINT64) T(INT32) T(FIXED64) T(FIXED32)
- T(BOOL) T(UINT32) T(SFIXED32) T(SFIXED64) T(SINT32) T(SINT64)
- OP(STARTMSG) OP(ENDMSG) OP(STARTSEQ) OP(ENDSEQ) OP(STARTSUBMSG)
- OP(ENDSUBMSG) OP(STARTSTR) OP(STRING) OP(ENDSTR) OP(CALL) OP(RET)
- OP(PUSHLENDELIM) OP(PUSHTAGDELIM) OP(SETDELIM) OP(CHECKDELIM)
- OP(BRANCH) OP(TAG1) OP(TAG2) OP(TAGN) OP(SETDISPATCH) OP(POP)
- OP(SETBIGGROUPNUM) OP(DISPATCH) OP(HALT)
- }
- return "<unknown op>";
- #undef OP
- #undef T
- }
- #endif
- #ifdef UPB_DUMP_BYTECODE
- static void dumpbc(uint32_t *p, uint32_t *end, FILE *f) {
- uint32_t *begin = p;
- while (p < end) {
- fprintf(f, "%p %8tx", p, p - begin);
- uint32_t instr = *p++;
- uint8_t op = getop(instr);
- fprintf(f, " %s", upb_pbdecoder_getopname(op));
- switch ((opcode)op) {
- case OP_SETDISPATCH: {
- const upb_inttable *dispatch;
- memcpy(&dispatch, p, sizeof(void*));
- p += ptr_words;
- const upb_pbdecodermethod *method =
- (void *)((char *)dispatch -
- offsetof(upb_pbdecodermethod, dispatch));
- fprintf(f, " %s", upb_msgdef_fullname(
- upb_handlers_msgdef(method->dest_handlers_)));
- break;
- }
- case OP_DISPATCH:
- case OP_STARTMSG:
- case OP_ENDMSG:
- case OP_PUSHLENDELIM:
- case OP_POP:
- case OP_SETDELIM:
- case OP_HALT:
- case OP_RET:
- break;
- case OP_PARSE_DOUBLE:
- case OP_PARSE_FLOAT:
- case OP_PARSE_INT64:
- case OP_PARSE_UINT64:
- case OP_PARSE_INT32:
- case OP_PARSE_FIXED64:
- case OP_PARSE_FIXED32:
- case OP_PARSE_BOOL:
- case OP_PARSE_UINT32:
- case OP_PARSE_SFIXED32:
- case OP_PARSE_SFIXED64:
- case OP_PARSE_SINT32:
- case OP_PARSE_SINT64:
- case OP_STARTSEQ:
- case OP_ENDSEQ:
- case OP_STARTSUBMSG:
- case OP_ENDSUBMSG:
- case OP_STARTSTR:
- case OP_STRING:
- case OP_ENDSTR:
- case OP_PUSHTAGDELIM:
- fprintf(f, " %d", instr >> 8);
- break;
- case OP_SETBIGGROUPNUM:
- fprintf(f, " %d", *p++);
- break;
- case OP_CHECKDELIM:
- case OP_CALL:
- case OP_BRANCH:
- fprintf(f, " =>0x%tx", p + getofs(instr) - begin);
- break;
- case OP_TAG1:
- case OP_TAG2: {
- fprintf(f, " tag:0x%x", instr >> 16);
- if (getofs(instr)) {
- fprintf(f, " =>0x%tx", p + getofs(instr) - begin);
- }
- break;
- }
- case OP_TAGN: {
- uint64_t tag = *p++;
- tag |= (uint64_t)*p++ << 32;
- fprintf(f, " tag:0x%llx", (long long)tag);
- fprintf(f, " n:%d", instr >> 16);
- if (getofs(instr)) {
- fprintf(f, " =>0x%tx", p + getofs(instr) - begin);
- }
- break;
- }
- }
- fputs("\n", f);
- }
- }
- #endif
- static uint64_t get_encoded_tag(const upb_fielddef *f, int wire_type) {
- uint32_t tag = (upb_fielddef_number(f) << 3) | wire_type;
- uint64_t encoded_tag = upb_vencode32(tag);
- /* No tag should be greater than 5 bytes. */
- UPB_ASSERT(encoded_tag <= 0xffffffffff);
- return encoded_tag;
- }
- static void putchecktag(compiler *c, const upb_fielddef *f,
- int wire_type, int dest) {
- uint64_t tag = get_encoded_tag(f, wire_type);
- switch (upb_value_size(tag)) {
- case 1:
- putop(c, OP_TAG1, dest, tag);
- break;
- case 2:
- putop(c, OP_TAG2, dest, tag);
- break;
- default:
- putop(c, OP_TAGN, dest, tag);
- break;
- }
- }
- static upb_selector_t getsel(const upb_fielddef *f, upb_handlertype_t type) {
- upb_selector_t selector;
- bool ok = upb_handlers_getselector(f, type, &selector);
- UPB_ASSERT(ok);
- return selector;
- }
- /* Takes an existing, primary dispatch table entry and repacks it with a
- * different alternate wire type. Called when we are inserting a secondary
- * dispatch table entry for an alternate wire type. */
- static uint64_t repack(uint64_t dispatch, int new_wt2) {
- uint64_t ofs;
- uint8_t wt1;
- uint8_t old_wt2;
- upb_pbdecoder_unpackdispatch(dispatch, &ofs, &wt1, &old_wt2);
- UPB_ASSERT(old_wt2 == NO_WIRE_TYPE); /* wt2 should not be set yet. */
- return upb_pbdecoder_packdispatch(ofs, wt1, new_wt2);
- }
- /* Marks the current bytecode position as the dispatch target for this message,
- * field, and wire type. */
- static void dispatchtarget(compiler *c, upb_pbdecodermethod *method,
- const upb_fielddef *f, int wire_type) {
- /* Offset is relative to msg base. */
- uint64_t ofs = pcofs(c) - method->code_base.ofs;
- uint32_t fn = upb_fielddef_number(f);
- upb_inttable *d = &method->dispatch;
- upb_value v;
- if (upb_inttable_remove(d, fn, &v)) {
- /* TODO: prioritize based on packed setting in .proto file. */
- uint64_t repacked = repack(upb_value_getuint64(v), wire_type);
- upb_inttable_insert(d, fn, upb_value_uint64(repacked));
- upb_inttable_insert(d, fn + UPB_MAX_FIELDNUMBER, upb_value_uint64(ofs));
- } else {
- uint64_t val = upb_pbdecoder_packdispatch(ofs, wire_type, NO_WIRE_TYPE);
- upb_inttable_insert(d, fn, upb_value_uint64(val));
- }
- }
- static void putpush(compiler *c, const upb_fielddef *f) {
- if (upb_fielddef_descriptortype(f) == UPB_DESCRIPTOR_TYPE_MESSAGE) {
- putop(c, OP_PUSHLENDELIM);
- } else {
- uint32_t fn = upb_fielddef_number(f);
- if (fn >= 1 << 24) {
- putop(c, OP_PUSHTAGDELIM, 0);
- putop(c, OP_SETBIGGROUPNUM, fn);
- } else {
- putop(c, OP_PUSHTAGDELIM, fn);
- }
- }
- }
- static upb_pbdecodermethod *find_submethod(const compiler *c,
- const upb_pbdecodermethod *method,
- const upb_fielddef *f) {
- const upb_handlers *sub =
- upb_handlers_getsubhandlers(method->dest_handlers_, f);
- upb_value v;
- return upb_inttable_lookupptr(&c->group->methods, sub, &v)
- ? upb_value_getptr(v)
- : NULL;
- }
- static void putsel(compiler *c, opcode op, upb_selector_t sel,
- const upb_handlers *h) {
- if (upb_handlers_gethandler(h, sel)) {
- putop(c, op, sel);
- }
- }
- /* Puts an opcode to call a callback, but only if a callback actually exists for
- * this field and handler type. */
- static void maybeput(compiler *c, opcode op, const upb_handlers *h,
- const upb_fielddef *f, upb_handlertype_t type) {
- putsel(c, op, getsel(f, type), h);
- }
- static bool haslazyhandlers(const upb_handlers *h, const upb_fielddef *f) {
- if (!upb_fielddef_lazy(f))
- return false;
- return upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_STARTSTR)) ||
- upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_STRING)) ||
- upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_ENDSTR));
- }
- /* bytecode compiler code generation ******************************************/
- /* Symbolic names for our local labels. */
- #define LABEL_LOOPSTART 1 /* Top of a repeated field loop. */
- #define LABEL_LOOPBREAK 2 /* To jump out of a repeated loop */
- #define LABEL_FIELD 3 /* Jump backward to find the most recent field. */
- #define LABEL_ENDMSG 4 /* To reach the OP_ENDMSG instr for this msg. */
- /* Generates bytecode to parse a single non-lazy message field. */
- static void generate_msgfield(compiler *c, const upb_fielddef *f,
- upb_pbdecodermethod *method) {
- const upb_handlers *h = upb_pbdecodermethod_desthandlers(method);
- const upb_pbdecodermethod *sub_m = find_submethod(c, method, f);
- int wire_type;
- if (!sub_m) {
- /* Don't emit any code for this field at all; it will be parsed as an
- * unknown field.
- *
- * TODO(haberman): we should change this to parse it as a string field
- * instead. It will probably be faster, but more importantly, once we
- * start vending unknown fields, a field shouldn't be treated as unknown
- * just because it doesn't have subhandlers registered. */
- return;
- }
- label(c, LABEL_FIELD);
- wire_type =
- (upb_fielddef_descriptortype(f) == UPB_DESCRIPTOR_TYPE_MESSAGE)
- ? UPB_WIRE_TYPE_DELIMITED
- : UPB_WIRE_TYPE_START_GROUP;
- if (upb_fielddef_isseq(f)) {
- putop(c, OP_CHECKDELIM, LABEL_ENDMSG);
- putchecktag(c, f, wire_type, LABEL_DISPATCH);
- dispatchtarget(c, method, f, wire_type);
- putop(c, OP_PUSHTAGDELIM, 0);
- putop(c, OP_STARTSEQ, getsel(f, UPB_HANDLER_STARTSEQ));
- label(c, LABEL_LOOPSTART);
- putpush(c, f);
- putop(c, OP_STARTSUBMSG, getsel(f, UPB_HANDLER_STARTSUBMSG));
- putop(c, OP_CALL, sub_m);
- putop(c, OP_POP);
- maybeput(c, OP_ENDSUBMSG, h, f, UPB_HANDLER_ENDSUBMSG);
- if (wire_type == UPB_WIRE_TYPE_DELIMITED) {
- putop(c, OP_SETDELIM);
- }
- putop(c, OP_CHECKDELIM, LABEL_LOOPBREAK);
- putchecktag(c, f, wire_type, LABEL_LOOPBREAK);
- putop(c, OP_BRANCH, -LABEL_LOOPSTART);
- label(c, LABEL_LOOPBREAK);
- putop(c, OP_POP);
- maybeput(c, OP_ENDSEQ, h, f, UPB_HANDLER_ENDSEQ);
- } else {
- putop(c, OP_CHECKDELIM, LABEL_ENDMSG);
- putchecktag(c, f, wire_type, LABEL_DISPATCH);
- dispatchtarget(c, method, f, wire_type);
- putpush(c, f);
- putop(c, OP_STARTSUBMSG, getsel(f, UPB_HANDLER_STARTSUBMSG));
- putop(c, OP_CALL, sub_m);
- putop(c, OP_POP);
- maybeput(c, OP_ENDSUBMSG, h, f, UPB_HANDLER_ENDSUBMSG);
- if (wire_type == UPB_WIRE_TYPE_DELIMITED) {
- putop(c, OP_SETDELIM);
- }
- }
- }
- /* Generates bytecode to parse a single string or lazy submessage field. */
- static void generate_delimfield(compiler *c, const upb_fielddef *f,
- upb_pbdecodermethod *method) {
- const upb_handlers *h = upb_pbdecodermethod_desthandlers(method);
- label(c, LABEL_FIELD);
- if (upb_fielddef_isseq(f)) {
- putop(c, OP_CHECKDELIM, LABEL_ENDMSG);
- putchecktag(c, f, UPB_WIRE_TYPE_DELIMITED, LABEL_DISPATCH);
- dispatchtarget(c, method, f, UPB_WIRE_TYPE_DELIMITED);
- putop(c, OP_PUSHTAGDELIM, 0);
- putop(c, OP_STARTSEQ, getsel(f, UPB_HANDLER_STARTSEQ));
- label(c, LABEL_LOOPSTART);
- putop(c, OP_PUSHLENDELIM);
- putop(c, OP_STARTSTR, getsel(f, UPB_HANDLER_STARTSTR));
- /* Need to emit even if no handler to skip past the string. */
- putop(c, OP_STRING, getsel(f, UPB_HANDLER_STRING));
- maybeput(c, OP_ENDSTR, h, f, UPB_HANDLER_ENDSTR);
- putop(c, OP_POP);
- putop(c, OP_SETDELIM);
- putop(c, OP_CHECKDELIM, LABEL_LOOPBREAK);
- putchecktag(c, f, UPB_WIRE_TYPE_DELIMITED, LABEL_LOOPBREAK);
- putop(c, OP_BRANCH, -LABEL_LOOPSTART);
- label(c, LABEL_LOOPBREAK);
- putop(c, OP_POP);
- maybeput(c, OP_ENDSEQ, h, f, UPB_HANDLER_ENDSEQ);
- } else {
- putop(c, OP_CHECKDELIM, LABEL_ENDMSG);
- putchecktag(c, f, UPB_WIRE_TYPE_DELIMITED, LABEL_DISPATCH);
- dispatchtarget(c, method, f, UPB_WIRE_TYPE_DELIMITED);
- putop(c, OP_PUSHLENDELIM);
- putop(c, OP_STARTSTR, getsel(f, UPB_HANDLER_STARTSTR));
- putop(c, OP_STRING, getsel(f, UPB_HANDLER_STRING));
- maybeput(c, OP_ENDSTR, h, f, UPB_HANDLER_ENDSTR);
- putop(c, OP_POP);
- putop(c, OP_SETDELIM);
- }
- }
- /* Generates bytecode to parse a single primitive field. */
- static void generate_primitivefield(compiler *c, const upb_fielddef *f,
- upb_pbdecodermethod *method) {
- const upb_handlers *h = upb_pbdecodermethod_desthandlers(method);
- upb_descriptortype_t descriptor_type = upb_fielddef_descriptortype(f);
- opcode parse_type;
- upb_selector_t sel;
- int wire_type;
- label(c, LABEL_FIELD);
- /* From a decoding perspective, ENUM is the same as INT32. */
- if (descriptor_type == UPB_DESCRIPTOR_TYPE_ENUM)
- descriptor_type = UPB_DESCRIPTOR_TYPE_INT32;
- parse_type = (opcode)descriptor_type;
- /* TODO(haberman): generate packed or non-packed first depending on "packed"
- * setting in the fielddef. This will favor (in speed) whichever was
- * specified. */
- UPB_ASSERT((int)parse_type >= 0 && parse_type <= OP_MAX);
- sel = getsel(f, upb_handlers_getprimitivehandlertype(f));
- wire_type = upb_pb_native_wire_types[upb_fielddef_descriptortype(f)];
- if (upb_fielddef_isseq(f)) {
- putop(c, OP_CHECKDELIM, LABEL_ENDMSG);
- putchecktag(c, f, UPB_WIRE_TYPE_DELIMITED, LABEL_DISPATCH);
- dispatchtarget(c, method, f, UPB_WIRE_TYPE_DELIMITED);
- putop(c, OP_PUSHLENDELIM);
- putop(c, OP_STARTSEQ, getsel(f, UPB_HANDLER_STARTSEQ)); /* Packed */
- label(c, LABEL_LOOPSTART);
- putop(c, parse_type, sel);
- putop(c, OP_CHECKDELIM, LABEL_LOOPBREAK);
- putop(c, OP_BRANCH, -LABEL_LOOPSTART);
- dispatchtarget(c, method, f, wire_type);
- putop(c, OP_PUSHTAGDELIM, 0);
- putop(c, OP_STARTSEQ, getsel(f, UPB_HANDLER_STARTSEQ)); /* Non-packed */
- label(c, LABEL_LOOPSTART);
- putop(c, parse_type, sel);
- putop(c, OP_CHECKDELIM, LABEL_LOOPBREAK);
- putchecktag(c, f, wire_type, LABEL_LOOPBREAK);
- putop(c, OP_BRANCH, -LABEL_LOOPSTART);
- label(c, LABEL_LOOPBREAK);
- putop(c, OP_POP); /* Packed and non-packed join. */
- maybeput(c, OP_ENDSEQ, h, f, UPB_HANDLER_ENDSEQ);
- putop(c, OP_SETDELIM); /* Could remove for non-packed by dup ENDSEQ. */
- } else {
- putop(c, OP_CHECKDELIM, LABEL_ENDMSG);
- putchecktag(c, f, wire_type, LABEL_DISPATCH);
- dispatchtarget(c, method, f, wire_type);
- putop(c, parse_type, sel);
- }
- }
- /* Adds bytecode for parsing the given message to the given decoderplan,
- * while adding all dispatch targets to this message's dispatch table. */
- static void compile_method(compiler *c, upb_pbdecodermethod *method) {
- const upb_handlers *h;
- const upb_msgdef *md;
- uint32_t* start_pc;
- upb_msg_field_iter i;
- upb_value val;
- UPB_ASSERT(method);
- /* Clear all entries in the dispatch table. */
- upb_inttable_uninit(&method->dispatch);
- upb_inttable_init(&method->dispatch, UPB_CTYPE_UINT64);
- h = upb_pbdecodermethod_desthandlers(method);
- md = upb_handlers_msgdef(h);
- method->code_base.ofs = pcofs(c);
- putop(c, OP_SETDISPATCH, &method->dispatch);
- putsel(c, OP_STARTMSG, UPB_STARTMSG_SELECTOR, h);
- label(c, LABEL_FIELD);
- start_pc = c->pc;
- for(upb_msg_field_begin(&i, md);
- !upb_msg_field_done(&i);
- upb_msg_field_next(&i)) {
- const upb_fielddef *f = upb_msg_iter_field(&i);
- upb_fieldtype_t type = upb_fielddef_type(f);
- if (type == UPB_TYPE_MESSAGE && !(haslazyhandlers(h, f) && c->lazy)) {
- generate_msgfield(c, f, method);
- } else if (type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES ||
- type == UPB_TYPE_MESSAGE) {
- generate_delimfield(c, f, method);
- } else {
- generate_primitivefield(c, f, method);
- }
- }
- /* If there were no fields, or if no handlers were defined, we need to
- * generate a non-empty loop body so that we can at least dispatch for unknown
- * fields and check for the end of the message. */
- if (c->pc == start_pc) {
- /* Check for end-of-message. */
- putop(c, OP_CHECKDELIM, LABEL_ENDMSG);
- /* Unconditionally dispatch. */
- putop(c, OP_DISPATCH, 0);
- }
- /* For now we just loop back to the last field of the message (or if none,
- * the DISPATCH opcode for the message). */
- putop(c, OP_BRANCH, -LABEL_FIELD);
- /* Insert both a label and a dispatch table entry for this end-of-msg. */
- label(c, LABEL_ENDMSG);
- val = upb_value_uint64(pcofs(c) - method->code_base.ofs);
- upb_inttable_insert(&method->dispatch, DISPATCH_ENDMSG, val);
- putsel(c, OP_ENDMSG, UPB_ENDMSG_SELECTOR, h);
- putop(c, OP_RET);
- upb_inttable_compact(&method->dispatch);
- }
- /* Populate "methods" with new upb_pbdecodermethod objects reachable from "h".
- * Returns the method for these handlers.
- *
- * Generates a new method for every destination handlers reachable from "h". */
- static void find_methods(compiler *c, const upb_handlers *h) {
- upb_value v;
- upb_msg_field_iter i;
- const upb_msgdef *md;
- if (upb_inttable_lookupptr(&c->group->methods, h, &v))
- return;
- newmethod(h, c->group);
- /* Find submethods. */
- md = upb_handlers_msgdef(h);
- for(upb_msg_field_begin(&i, md);
- !upb_msg_field_done(&i);
- upb_msg_field_next(&i)) {
- const upb_fielddef *f = upb_msg_iter_field(&i);
- const upb_handlers *sub_h;
- if (upb_fielddef_type(f) == UPB_TYPE_MESSAGE &&
- (sub_h = upb_handlers_getsubhandlers(h, f)) != NULL) {
- /* We only generate a decoder method for submessages with handlers.
- * Others will be parsed as unknown fields. */
- find_methods(c, sub_h);
- }
- }
- }
- /* (Re-)compile bytecode for all messages in "msgs."
- * Overwrites any existing bytecode in "c". */
- static void compile_methods(compiler *c) {
- upb_inttable_iter i;
- /* Start over at the beginning of the bytecode. */
- c->pc = c->group->bytecode;
- upb_inttable_begin(&i, &c->group->methods);
- for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
- upb_pbdecodermethod *method = upb_value_getptr(upb_inttable_iter_value(&i));
- compile_method(c, method);
- }
- }
- static void set_bytecode_handlers(mgroup *g) {
- upb_inttable_iter i;
- upb_inttable_begin(&i, &g->methods);
- for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
- upb_pbdecodermethod *m = upb_value_getptr(upb_inttable_iter_value(&i));
- upb_byteshandler *h = &m->input_handler_;
- m->code_base.ptr = g->bytecode + m->code_base.ofs;
- upb_byteshandler_setstartstr(h, upb_pbdecoder_startbc, m->code_base.ptr);
- upb_byteshandler_setstring(h, upb_pbdecoder_decode, g);
- upb_byteshandler_setendstr(h, upb_pbdecoder_end, m);
- }
- }
- /* JIT setup. *****************************************************************/
- #ifdef UPB_USE_JIT_X64
- static void sethandlers(mgroup *g, bool allowjit) {
- g->jit_code = NULL;
- if (allowjit) {
- /* Compile byte-code into machine code, create handlers. */
- upb_pbdecoder_jit(g);
- } else {
- set_bytecode_handlers(g);
- }
- }
- #else /* UPB_USE_JIT_X64 */
- static void sethandlers(mgroup *g, bool allowjit) {
- /* No JIT compiled in; use bytecode handlers unconditionally. */
- UPB_UNUSED(allowjit);
- set_bytecode_handlers(g);
- }
- #endif /* UPB_USE_JIT_X64 */
- /* TODO(haberman): allow this to be constructed for an arbitrary set of dest
- * handlers and other mgroups (but verify we have a transitive closure). */
- const mgroup *mgroup_new(const upb_handlers *dest, bool allowjit, bool lazy,
- const void *owner) {
- mgroup *g;
- compiler *c;
- UPB_UNUSED(allowjit);
- UPB_ASSERT(upb_handlers_isfrozen(dest));
- g = newgroup(owner);
- c = newcompiler(g, lazy);
- find_methods(c, dest);
- /* We compile in two passes:
- * 1. all messages are assigned relative offsets from the beginning of the
- * bytecode (saved in method->code_base).
- * 2. forwards OP_CALL instructions can be correctly linked since message
- * offsets have been previously assigned.
- *
- * Could avoid the second pass by linking OP_CALL instructions somehow. */
- compile_methods(c);
- compile_methods(c);
- g->bytecode_end = c->pc;
- freecompiler(c);
- #ifdef UPB_DUMP_BYTECODE
- {
- FILE *f = fopen("/tmp/upb-bytecode", "w");
- UPB_ASSERT(f);
- dumpbc(g->bytecode, g->bytecode_end, stderr);
- dumpbc(g->bytecode, g->bytecode_end, f);
- fclose(f);
- f = fopen("/tmp/upb-bytecode.bin", "wb");
- UPB_ASSERT(f);
- fwrite(g->bytecode, 1, g->bytecode_end - g->bytecode, f);
- fclose(f);
- }
- #endif
- sethandlers(g, allowjit);
- return g;
- }
- /* upb_pbcodecache ************************************************************/
- void upb_pbcodecache_init(upb_pbcodecache *c) {
- upb_inttable_init(&c->groups, UPB_CTYPE_CONSTPTR);
- c->allow_jit_ = true;
- }
- void upb_pbcodecache_uninit(upb_pbcodecache *c) {
- upb_inttable_iter i;
- upb_inttable_begin(&i, &c->groups);
- for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
- const mgroup *group = upb_value_getconstptr(upb_inttable_iter_value(&i));
- mgroup_unref(group, c);
- }
- upb_inttable_uninit(&c->groups);
- }
- bool upb_pbcodecache_allowjit(const upb_pbcodecache *c) {
- return c->allow_jit_;
- }
- bool upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow) {
- if (upb_inttable_count(&c->groups) > 0)
- return false;
- c->allow_jit_ = allow;
- return true;
- }
- const upb_pbdecodermethod *upb_pbcodecache_getdecodermethod(
- upb_pbcodecache *c, const upb_pbdecodermethodopts *opts) {
- upb_value v;
- bool ok;
- /* Right now we build a new DecoderMethod every time.
- * TODO(haberman): properly cache methods by their true key. */
- const mgroup *g = mgroup_new(opts->handlers, c->allow_jit_, opts->lazy, c);
- upb_inttable_push(&c->groups, upb_value_constptr(g));
- ok = upb_inttable_lookupptr(&g->methods, opts->handlers, &v);
- UPB_ASSERT(ok);
- return upb_value_getptr(v);
- }
- /* upb_pbdecodermethodopts ****************************************************/
- void upb_pbdecodermethodopts_init(upb_pbdecodermethodopts *opts,
- const upb_handlers *h) {
- opts->handlers = h;
- opts->lazy = false;
- }
- void upb_pbdecodermethodopts_setlazy(upb_pbdecodermethodopts *opts, bool lazy) {
- opts->lazy = lazy;
- }
- /*
- ** upb::Decoder (Bytecode Decoder VM)
- **
- ** Bytecode must previously have been generated using the bytecode compiler in
- ** compile_decoder.c. This decoder then walks through the bytecode op-by-op to
- ** parse the input.
- **
- ** Decoding is fully resumable; we just keep a pointer to the current bytecode
- ** instruction and resume from there. A fair amount of the logic here is to
- ** handle the fact that values can span buffer seams and we have to be able to
- ** be capable of suspending/resuming from any byte in the stream. This
- ** sometimes requires keeping a few trailing bytes from the last buffer around
- ** in the "residual" buffer.
- */
- #include <inttypes.h>
- #include <stddef.h>
- #ifdef UPB_DUMP_BYTECODE
- #include <stdio.h>
- #endif
- #define CHECK_SUSPEND(x) if (!(x)) return upb_pbdecoder_suspend(d);
- /* Error messages that are shared between the bytecode and JIT decoders. */
- const char *kPbDecoderStackOverflow = "Nesting too deep.";
- const char *kPbDecoderSubmessageTooLong =
- "Submessage end extends past enclosing submessage.";
- /* Error messages shared within this file. */
- static const char *kUnterminatedVarint = "Unterminated varint.";
- /* upb_pbdecoder **************************************************************/
- static opcode halt = OP_HALT;
- /* A dummy character we can point to when the user passes us a NULL buffer.
- * We need this because in C (NULL + 0) and (NULL - NULL) are undefined
- * behavior, which would invalidate functions like curbufleft(). */
- static const char dummy_char;
- /* Whether an op consumes any of the input buffer. */
- static bool consumes_input(opcode op) {
- switch (op) {
- case OP_SETDISPATCH:
- case OP_STARTMSG:
- case OP_ENDMSG:
- case OP_STARTSEQ:
- case OP_ENDSEQ:
- case OP_STARTSUBMSG:
- case OP_ENDSUBMSG:
- case OP_STARTSTR:
- case OP_ENDSTR:
- case OP_PUSHTAGDELIM:
- case OP_POP:
- case OP_SETDELIM:
- case OP_SETBIGGROUPNUM:
- case OP_CHECKDELIM:
- case OP_CALL:
- case OP_RET:
- case OP_BRANCH:
- return false;
- default:
- return true;
- }
- }
- static size_t stacksize(upb_pbdecoder *d, size_t entries) {
- UPB_UNUSED(d);
- return entries * sizeof(upb_pbdecoder_frame);
- }
- static size_t callstacksize(upb_pbdecoder *d, size_t entries) {
- UPB_UNUSED(d);
- #ifdef UPB_USE_JIT_X64
- if (d->method_->is_native_) {
- /* Each native stack frame needs two pointers, plus we need a few frames for
- * the enter/exit trampolines. */
- size_t ret = entries * sizeof(void*) * 2;
- ret += sizeof(void*) * 10;
- return ret;
- }
- #endif
- return entries * sizeof(uint32_t*);
- }
- static bool in_residual_buf(const upb_pbdecoder *d, const char *p);
- /* It's unfortunate that we have to micro-manage the compiler with
- * UPB_FORCEINLINE and UPB_NOINLINE, especially since this tuning is necessarily
- * specific to one hardware configuration. But empirically on a Core i7,
- * performance increases 30-50% with these annotations. Every instance where
- * these appear, gcc 4.2.1 made the wrong decision and degraded performance in
- * benchmarks. */
- static void seterr(upb_pbdecoder *d, const char *msg) {
- upb_status status = UPB_STATUS_INIT;
- upb_status_seterrmsg(&status, msg);
- upb_env_reporterror(d->env, &status);
- }
- void upb_pbdecoder_seterr(upb_pbdecoder *d, const char *msg) {
- seterr(d, msg);
- }
- /* Buffering ******************************************************************/
- /* We operate on one buffer at a time, which is either the user's buffer passed
- * to our "decode" callback or some residual bytes from the previous buffer. */
- /* How many bytes can be safely read from d->ptr without reading past end-of-buf
- * or past the current delimited end. */
- static size_t curbufleft(const upb_pbdecoder *d) {
- UPB_ASSERT(d->data_end >= d->ptr);
- return d->data_end - d->ptr;
- }
- /* How many bytes are available before end-of-buffer. */
- static size_t bufleft(const upb_pbdecoder *d) {
- return d->end - d->ptr;
- }
- /* Overall stream offset of d->ptr. */
- uint64_t offset(const upb_pbdecoder *d) {
- return d->bufstart_ofs + (d->ptr - d->buf);
- }
- /* How many bytes are available before the end of this delimited region. */
- size_t delim_remaining(const upb_pbdecoder *d) {
- return d->top->end_ofs - offset(d);
- }
- /* Advances d->ptr. */
- static void advance(upb_pbdecoder *d, size_t len) {
- UPB_ASSERT(curbufleft(d) >= len);
- d->ptr += len;
- }
- static bool in_buf(const char *p, const char *buf, const char *end) {
- return p >= buf && p <= end;
- }
- static bool in_residual_buf(const upb_pbdecoder *d, const char *p) {
- return in_buf(p, d->residual, d->residual_end);
- }
- /* Calculates the delim_end value, which is affected by both the current buffer
- * and the parsing stack, so must be called whenever either is updated. */
- static void set_delim_end(upb_pbdecoder *d) {
- size_t delim_ofs = d->top->end_ofs - d->bufstart_ofs;
- if (delim_ofs <= (size_t)(d->end - d->buf)) {
- d->delim_end = d->buf + delim_ofs;
- d->data_end = d->delim_end;
- } else {
- d->data_end = d->end;
- d->delim_end = NULL;
- }
- }
- static void switchtobuf(upb_pbdecoder *d, const char *buf, const char *end) {
- d->ptr = buf;
- d->buf = buf;
- d->end = end;
- set_delim_end(d);
- }
- static void advancetobuf(upb_pbdecoder *d, const char *buf, size_t len) {
- UPB_ASSERT(curbufleft(d) == 0);
- d->bufstart_ofs += (d->end - d->buf);
- switchtobuf(d, buf, buf + len);
- }
- static void checkpoint(upb_pbdecoder *d) {
- /* The assertion here is in the interests of efficiency, not correctness.
- * We are trying to ensure that we don't checkpoint() more often than
- * necessary. */
- UPB_ASSERT(d->checkpoint != d->ptr);
- d->checkpoint = d->ptr;
- }
- /* Skips "bytes" bytes in the stream, which may be more than available. If we
- * skip more bytes than are available, we return a long read count to the caller
- * indicating how many bytes can be skipped over before passing actual data
- * again. Skipped bytes can pass a NULL buffer and the decoder guarantees they
- * won't actually be read.
- */
- static int32_t skip(upb_pbdecoder *d, size_t bytes) {
- UPB_ASSERT(!in_residual_buf(d, d->ptr) || d->size_param == 0);
- UPB_ASSERT(d->skip == 0);
- if (bytes > delim_remaining(d)) {
- seterr(d, "Skipped value extended beyond enclosing submessage.");
- return upb_pbdecoder_suspend(d);
- } else if (bufleft(d) >= bytes) {
- /* Skipped data is all in current buffer, and more is still available. */
- advance(d, bytes);
- d->skip = 0;
- return DECODE_OK;
- } else {
- /* Skipped data extends beyond currently available buffers. */
- d->pc = d->last;
- d->skip = bytes - curbufleft(d);
- d->bufstart_ofs += (d->end - d->buf);
- d->residual_end = d->residual;
- switchtobuf(d, d->residual, d->residual_end);
- return d->size_param + d->skip;
- }
- }
- /* Resumes the decoder from an initial state or from a previous suspend. */
- int32_t upb_pbdecoder_resume(upb_pbdecoder *d, void *p, const char *buf,
- size_t size, const upb_bufhandle *handle) {
- UPB_UNUSED(p); /* Useless; just for the benefit of the JIT. */
- /* d->skip and d->residual_end could probably elegantly be represented
- * as a single variable, to more easily represent this invariant. */
- UPB_ASSERT(!(d->skip && d->residual_end > d->residual));
- /* We need to remember the original size_param, so that the value we return
- * is relative to it, even if we do some skipping first. */
- d->size_param = size;
- d->handle = handle;
- /* Have to handle this case specially (ie. not with skip()) because the user
- * is allowed to pass a NULL buffer here, which won't allow us to safely
- * calculate a d->end or use our normal functions like curbufleft(). */
- if (d->skip && d->skip >= size) {
- d->skip -= size;
- d->bufstart_ofs += size;
- buf = &dummy_char;
- size = 0;
- /* We can't just return now, because we might need to execute some ops
- * like CHECKDELIM, which could call some callbacks and pop the stack. */
- }
- /* We need to pretend that this was the actual buffer param, since some of the
- * calculations assume that d->ptr/d->buf is relative to this. */
- d->buf_param = buf;
- if (!buf) {
- /* NULL buf is ok if its entire span is covered by the "skip" above, but
- * by this point we know that "skip" doesn't cover the buffer. */
- seterr(d, "Passed NULL buffer over non-skippable region.");
- return upb_pbdecoder_suspend(d);
- }
- if (d->residual_end > d->residual) {
- /* We have residual bytes from the last buffer. */
- UPB_ASSERT(d->ptr == d->residual);
- } else {
- switchtobuf(d, buf, buf + size);
- }
- d->checkpoint = d->ptr;
- /* Handle skips that don't cover the whole buffer (as above). */
- if (d->skip) {
- size_t skip_bytes = d->skip;
- d->skip = 0;
- CHECK_RETURN(skip(d, skip_bytes));
- checkpoint(d);
- }
- /* If we're inside an unknown group, continue to parse unknown values. */
- if (d->top->groupnum < 0) {
- CHECK_RETURN(upb_pbdecoder_skipunknown(d, -1, 0));
- checkpoint(d);
- }
- return DECODE_OK;
- }
- /* Suspends the decoder at the last checkpoint, without saving any residual
- * bytes. If there are any unconsumed bytes, returns a short byte count. */
- size_t upb_pbdecoder_suspend(upb_pbdecoder *d) {
- d->pc = d->last;
- if (d->checkpoint == d->residual) {
- /* Checkpoint was in residual buf; no user bytes were consumed. */
- d->ptr = d->residual;
- return 0;
- } else {
- size_t ret = d->size_param - (d->end - d->checkpoint);
- UPB_ASSERT(!in_residual_buf(d, d->checkpoint));
- UPB_ASSERT(d->buf == d->buf_param || d->buf == &dummy_char);
- d->bufstart_ofs += (d->checkpoint - d->buf);
- d->residual_end = d->residual;
- switchtobuf(d, d->residual, d->residual_end);
- return ret;
- }
- }
- /* Suspends the decoder at the last checkpoint, and saves any unconsumed
- * bytes in our residual buffer. This is necessary if we need more user
- * bytes to form a complete value, which might not be contiguous in the
- * user's buffers. Always consumes all user bytes. */
- static size_t suspend_save(upb_pbdecoder *d) {
- /* We hit end-of-buffer before we could parse a full value.
- * Save any unconsumed bytes (if any) to the residual buffer. */
- d->pc = d->last;
- if (d->checkpoint == d->residual) {
- /* Checkpoint was in residual buf; append user byte(s) to residual buf. */
- UPB_ASSERT((d->residual_end - d->residual) + d->size_param <=
- sizeof(d->residual));
- if (!in_residual_buf(d, d->ptr)) {
- d->bufstart_ofs -= (d->residual_end - d->residual);
- }
- memcpy(d->residual_end, d->buf_param, d->size_param);
- d->residual_end += d->size_param;
- } else {
- /* Checkpoint was in user buf; old residual bytes not needed. */
- size_t save;
- UPB_ASSERT(!in_residual_buf(d, d->checkpoint));
- d->ptr = d->checkpoint;
- save = curbufleft(d);
- UPB_ASSERT(save <= sizeof(d->residual));
- memcpy(d->residual, d->ptr, save);
- d->residual_end = d->residual + save;
- d->bufstart_ofs = offset(d);
- }
- switchtobuf(d, d->residual, d->residual_end);
- return d->size_param;
- }
- /* Copies the next "bytes" bytes into "buf" and advances the stream.
- * Requires that this many bytes are available in the current buffer. */
- UPB_FORCEINLINE static void consumebytes(upb_pbdecoder *d, void *buf,
- size_t bytes) {
- UPB_ASSERT(bytes <= curbufleft(d));
- memcpy(buf, d->ptr, bytes);
- advance(d, bytes);
- }
- /* Slow path for getting the next "bytes" bytes, regardless of whether they are
- * available in the current buffer or not. Returns a status code as described
- * in decoder.int.h. */
- UPB_NOINLINE static int32_t getbytes_slow(upb_pbdecoder *d, void *buf,
- size_t bytes) {
- const size_t avail = curbufleft(d);
- consumebytes(d, buf, avail);
- bytes -= avail;
- UPB_ASSERT(bytes > 0);
- if (in_residual_buf(d, d->ptr)) {
- advancetobuf(d, d->buf_param, d->size_param);
- }
- if (curbufleft(d) >= bytes) {
- consumebytes(d, (char *)buf + avail, bytes);
- return DECODE_OK;
- } else if (d->data_end == d->delim_end) {
- seterr(d, "Submessage ended in the middle of a value or group");
- return upb_pbdecoder_suspend(d);
- } else {
- return suspend_save(d);
- }
- }
- /* Gets the next "bytes" bytes, regardless of whether they are available in the
- * current buffer or not. Returns a status code as described in decoder.int.h.
- */
- UPB_FORCEINLINE static int32_t getbytes(upb_pbdecoder *d, void *buf,
- size_t bytes) {
- if (curbufleft(d) >= bytes) {
- /* Buffer has enough data to satisfy. */
- consumebytes(d, buf, bytes);
- return DECODE_OK;
- } else {
- return getbytes_slow(d, buf, bytes);
- }
- }
- UPB_NOINLINE static size_t peekbytes_slow(upb_pbdecoder *d, void *buf,
- size_t bytes) {
- size_t ret = curbufleft(d);
- memcpy(buf, d->ptr, ret);
- if (in_residual_buf(d, d->ptr)) {
- size_t copy = UPB_MIN(bytes - ret, d->size_param);
- memcpy((char *)buf + ret, d->buf_param, copy);
- ret += copy;
- }
- return ret;
- }
- UPB_FORCEINLINE static size_t peekbytes(upb_pbdecoder *d, void *buf,
- size_t bytes) {
- if (curbufleft(d) >= bytes) {
- memcpy(buf, d->ptr, bytes);
- return bytes;
- } else {
- return peekbytes_slow(d, buf, bytes);
- }
- }
- /* Decoding of wire types *****************************************************/
- /* Slow path for decoding a varint from the current buffer position.
- * Returns a status code as described in decoder.int.h. */
- UPB_NOINLINE int32_t upb_pbdecoder_decode_varint_slow(upb_pbdecoder *d,
- uint64_t *u64) {
- uint8_t byte = 0x80;
- int bitpos;
- *u64 = 0;
- for(bitpos = 0; bitpos < 70 && (byte & 0x80); bitpos += 7) {
- CHECK_RETURN(getbytes(d, &byte, 1));
- *u64 |= (uint64_t)(byte & 0x7F) << bitpos;
- }
- if(bitpos == 70 && (byte & 0x80)) {
- seterr(d, kUnterminatedVarint);
- return upb_pbdecoder_suspend(d);
- }
- return DECODE_OK;
- }
- /* Decodes a varint from the current buffer position.
- * Returns a status code as described in decoder.int.h. */
- UPB_FORCEINLINE static int32_t decode_varint(upb_pbdecoder *d, uint64_t *u64) {
- if (curbufleft(d) > 0 && !(*d->ptr & 0x80)) {
- *u64 = *d->ptr;
- advance(d, 1);
- return DECODE_OK;
- } else if (curbufleft(d) >= 10) {
- /* Fast case. */
- upb_decoderet r = upb_vdecode_fast(d->ptr);
- if (r.p == NULL) {
- seterr(d, kUnterminatedVarint);
- return upb_pbdecoder_suspend(d);
- }
- advance(d, r.p - d->ptr);
- *u64 = r.val;
- return DECODE_OK;
- } else {
- /* Slow case -- varint spans buffer seam. */
- return upb_pbdecoder_decode_varint_slow(d, u64);
- }
- }
- /* Decodes a 32-bit varint from the current buffer position.
- * Returns a status code as described in decoder.int.h. */
- UPB_FORCEINLINE static int32_t decode_v32(upb_pbdecoder *d, uint32_t *u32) {
- uint64_t u64;
- int32_t ret = decode_varint(d, &u64);
- if (ret >= 0) return ret;
- if (u64 > UINT32_MAX) {
- seterr(d, "Unterminated 32-bit varint");
- /* TODO(haberman) guarantee that this function return is >= 0 somehow,
- * so we know this path will always be treated as error by our caller.
- * Right now the size_t -> int32_t can overflow and produce negative values.
- */
- *u32 = 0;
- return upb_pbdecoder_suspend(d);
- }
- *u32 = u64;
- return DECODE_OK;
- }
- /* Decodes a fixed32 from the current buffer position.
- * Returns a status code as described in decoder.int.h.
- * TODO: proper byte swapping for big-endian machines. */
- UPB_FORCEINLINE static int32_t decode_fixed32(upb_pbdecoder *d, uint32_t *u32) {
- return getbytes(d, u32, 4);
- }
- /* Decodes a fixed64 from the current buffer position.
- * Returns a status code as described in decoder.int.h.
- * TODO: proper byte swapping for big-endian machines. */
- UPB_FORCEINLINE static int32_t decode_fixed64(upb_pbdecoder *d, uint64_t *u64) {
- return getbytes(d, u64, 8);
- }
- /* Non-static versions of the above functions.
- * These are called by the JIT for fallback paths. */
- int32_t upb_pbdecoder_decode_f32(upb_pbdecoder *d, uint32_t *u32) {
- return decode_fixed32(d, u32);
- }
- int32_t upb_pbdecoder_decode_f64(upb_pbdecoder *d, uint64_t *u64) {
- return decode_fixed64(d, u64);
- }
- static double as_double(uint64_t n) { double d; memcpy(&d, &n, 8); return d; }
- static float as_float(uint32_t n) { float f; memcpy(&f, &n, 4); return f; }
- /* Pushes a frame onto the decoder stack. */
- static bool decoder_push(upb_pbdecoder *d, uint64_t end) {
- upb_pbdecoder_frame *fr = d->top;
- if (end > fr->end_ofs) {
- seterr(d, kPbDecoderSubmessageTooLong);
- return false;
- } else if (fr == d->limit) {
- seterr(d, kPbDecoderStackOverflow);
- return false;
- }
- fr++;
- fr->end_ofs = end;
- fr->dispatch = NULL;
- fr->groupnum = 0;
- d->top = fr;
- return true;
- }
- static bool pushtagdelim(upb_pbdecoder *d, uint32_t arg) {
- /* While we expect to see an "end" tag (either ENDGROUP or a non-sequence
- * field number) prior to hitting any enclosing submessage end, pushing our
- * existing delim end prevents us from continuing to parse values from a
- * corrupt proto that doesn't give us an END tag in time. */
- if (!decoder_push(d, d->top->end_ofs))
- return false;
- d->top->groupnum = arg;
- return true;
- }
- /* Pops a frame from the decoder stack. */
- static void decoder_pop(upb_pbdecoder *d) { d->top--; }
- UPB_NOINLINE int32_t upb_pbdecoder_checktag_slow(upb_pbdecoder *d,
- uint64_t expected) {
- uint64_t data = 0;
- size_t bytes = upb_value_size(expected);
- size_t read = peekbytes(d, &data, bytes);
- if (read == bytes && data == expected) {
- /* Advance past matched bytes. */
- int32_t ok = getbytes(d, &data, read);
- UPB_ASSERT(ok < 0);
- return DECODE_OK;
- } else if (read < bytes && memcmp(&data, &expected, read) == 0) {
- return suspend_save(d);
- } else {
- return DECODE_MISMATCH;
- }
- }
- int32_t upb_pbdecoder_skipunknown(upb_pbdecoder *d, int32_t fieldnum,
- uint8_t wire_type) {
- if (fieldnum >= 0)
- goto have_tag;
- while (true) {
- uint32_t tag;
- CHECK_RETURN(decode_v32(d, &tag));
- wire_type = tag & 0x7;
- fieldnum = tag >> 3;
- have_tag:
- if (fieldnum == 0) {
- seterr(d, "Saw invalid field number (0)");
- return upb_pbdecoder_suspend(d);
- }
- switch (wire_type) {
- case UPB_WIRE_TYPE_32BIT:
- CHECK_RETURN(skip(d, 4));
- break;
- case UPB_WIRE_TYPE_64BIT:
- CHECK_RETURN(skip(d, 8));
- break;
- case UPB_WIRE_TYPE_VARINT: {
- uint64_t u64;
- CHECK_RETURN(decode_varint(d, &u64));
- break;
- }
- case UPB_WIRE_TYPE_DELIMITED: {
- uint32_t len;
- CHECK_RETURN(decode_v32(d, &len));
- CHECK_RETURN(skip(d, len));
- break;
- }
- case UPB_WIRE_TYPE_START_GROUP:
- CHECK_SUSPEND(pushtagdelim(d, -fieldnum));
- break;
- case UPB_WIRE_TYPE_END_GROUP:
- if (fieldnum == -d->top->groupnum) {
- decoder_pop(d);
- } else if (fieldnum == d->top->groupnum) {
- return DECODE_ENDGROUP;
- } else {
- seterr(d, "Unmatched ENDGROUP tag.");
- return upb_pbdecoder_suspend(d);
- }
- break;
- default:
- seterr(d, "Invalid wire type");
- return upb_pbdecoder_suspend(d);
- }
- if (d->top->groupnum >= 0) {
- /* TODO: More code needed for handling unknown groups. */
- upb_sink_putunknown(&d->top->sink, d->checkpoint, d->ptr - d->checkpoint);
- return DECODE_OK;
- }
- /* Unknown group -- continue looping over unknown fields. */
- checkpoint(d);
- }
- }
- static void goto_endmsg(upb_pbdecoder *d) {
- upb_value v;
- bool found = upb_inttable_lookup32(d->top->dispatch, DISPATCH_ENDMSG, &v);
- UPB_ASSERT(found);
- d->pc = d->top->base + upb_value_getuint64(v);
- }
- /* Parses a tag and jumps to the corresponding bytecode instruction for this
- * field.
- *
- * If the tag is unknown (or the wire type doesn't match), parses the field as
- * unknown. If the tag is a valid ENDGROUP tag, jumps to the bytecode
- * instruction for the end of message. */
- static int32_t dispatch(upb_pbdecoder *d) {
- upb_inttable *dispatch = d->top->dispatch;
- uint32_t tag;
- uint8_t wire_type;
- uint32_t fieldnum;
- upb_value val;
- int32_t retval;
- /* Decode tag. */
- CHECK_RETURN(decode_v32(d, &tag));
- wire_type = tag & 0x7;
- fieldnum = tag >> 3;
- /* Lookup tag. Because of packed/non-packed compatibility, we have to
- * check the wire type against two possibilities. */
- if (fieldnum != DISPATCH_ENDMSG &&
- upb_inttable_lookup32(dispatch, fieldnum, &val)) {
- uint64_t v = upb_value_getuint64(val);
- if (wire_type == (v & 0xff)) {
- d->pc = d->top->base + (v >> 16);
- return DECODE_OK;
- } else if (wire_type == ((v >> 8) & 0xff)) {
- bool found =
- upb_inttable_lookup(dispatch, fieldnum + UPB_MAX_FIELDNUMBER, &val);
- UPB_ASSERT(found);
- d->pc = d->top->base + upb_value_getuint64(val);
- return DECODE_OK;
- }
- }
- /* We have some unknown fields (or ENDGROUP) to parse. The DISPATCH or TAG
- * bytecode that triggered this is preceded by a CHECKDELIM bytecode which
- * we need to back up to, so that when we're done skipping unknown data we
- * can re-check the delimited end. */
- d->last--; /* Necessary if we get suspended */
- d->pc = d->last;
- UPB_ASSERT(getop(*d->last) == OP_CHECKDELIM);
- /* Unknown field or ENDGROUP. */
- retval = upb_pbdecoder_skipunknown(d, fieldnum, wire_type);
- CHECK_RETURN(retval);
- if (retval == DECODE_ENDGROUP) {
- goto_endmsg(d);
- return DECODE_OK;
- }
- return DECODE_OK;
- }
- /* Callers know that the stack is more than one deep because the opcodes that
- * call this only occur after PUSH operations. */
- upb_pbdecoder_frame *outer_frame(upb_pbdecoder *d) {
- UPB_ASSERT(d->top != d->stack);
- return d->top - 1;
- }
- /* The main decoding loop *****************************************************/
- /* The main decoder VM function. Uses traditional bytecode dispatch loop with a
- * switch() statement. */
- size_t run_decoder_vm(upb_pbdecoder *d, const mgroup *group,
- const upb_bufhandle* handle) {
- #define VMCASE(op, code) \
- case op: { code; if (consumes_input(op)) checkpoint(d); break; }
- #define PRIMITIVE_OP(type, wt, name, convfunc, ctype) \
- VMCASE(OP_PARSE_ ## type, { \
- ctype val; \
- CHECK_RETURN(decode_ ## wt(d, &val)); \
- upb_sink_put ## name(&d->top->sink, arg, (convfunc)(val)); \
- })
- while(1) {
- int32_t instruction;
- opcode op;
- uint32_t arg;
- int32_t longofs;
- d->last = d->pc;
- instruction = *d->pc++;
- op = getop(instruction);
- arg = instruction >> 8;
- longofs = arg;
- UPB_ASSERT(d->ptr != d->residual_end);
- UPB_UNUSED(group);
- #ifdef UPB_DUMP_BYTECODE
- fprintf(stderr, "s_ofs=%d buf_ofs=%d data_rem=%d buf_rem=%d delim_rem=%d "
- "%x %s (%d)\n",
- (int)offset(d),
- (int)(d->ptr - d->buf),
- (int)(d->data_end - d->ptr),
- (int)(d->end - d->ptr),
- (int)((d->top->end_ofs - d->bufstart_ofs) - (d->ptr - d->buf)),
- (int)(d->pc - 1 - group->bytecode),
- upb_pbdecoder_getopname(op),
- arg);
- #endif
- switch (op) {
- /* Technically, we are losing data if we see a 32-bit varint that is not
- * properly sign-extended. We could detect this and error about the data
- * loss, but proto2 does not do this, so we pass. */
- PRIMITIVE_OP(INT32, varint, int32, int32_t, uint64_t)
- PRIMITIVE_OP(INT64, varint, int64, int64_t, uint64_t)
- PRIMITIVE_OP(UINT32, varint, uint32, uint32_t, uint64_t)
- PRIMITIVE_OP(UINT64, varint, uint64, uint64_t, uint64_t)
- PRIMITIVE_OP(FIXED32, fixed32, uint32, uint32_t, uint32_t)
- PRIMITIVE_OP(FIXED64, fixed64, uint64, uint64_t, uint64_t)
- PRIMITIVE_OP(SFIXED32, fixed32, int32, int32_t, uint32_t)
- PRIMITIVE_OP(SFIXED64, fixed64, int64, int64_t, uint64_t)
- PRIMITIVE_OP(BOOL, varint, bool, bool, uint64_t)
- PRIMITIVE_OP(DOUBLE, fixed64, double, as_double, uint64_t)
- PRIMITIVE_OP(FLOAT, fixed32, float, as_float, uint32_t)
- PRIMITIVE_OP(SINT32, varint, int32, upb_zzdec_32, uint64_t)
- PRIMITIVE_OP(SINT64, varint, int64, upb_zzdec_64, uint64_t)
- VMCASE(OP_SETDISPATCH,
- d->top->base = d->pc - 1;
- memcpy(&d->top->dispatch, d->pc, sizeof(void*));
- d->pc += sizeof(void*) / sizeof(uint32_t);
- )
- VMCASE(OP_STARTMSG,
- CHECK_SUSPEND(upb_sink_startmsg(&d->top->sink));
- )
- VMCASE(OP_ENDMSG,
- CHECK_SUSPEND(upb_sink_endmsg(&d->top->sink, d->status));
- )
- VMCASE(OP_STARTSEQ,
- upb_pbdecoder_frame *outer = outer_frame(d);
- CHECK_SUSPEND(upb_sink_startseq(&outer->sink, arg, &d->top->sink));
- )
- VMCASE(OP_ENDSEQ,
- CHECK_SUSPEND(upb_sink_endseq(&d->top->sink, arg));
- )
- VMCASE(OP_STARTSUBMSG,
- upb_pbdecoder_frame *outer = outer_frame(d);
- CHECK_SUSPEND(upb_sink_startsubmsg(&outer->sink, arg, &d->top->sink));
- )
- VMCASE(OP_ENDSUBMSG,
- CHECK_SUSPEND(upb_sink_endsubmsg(&d->top->sink, arg));
- )
- VMCASE(OP_STARTSTR,
- uint32_t len = delim_remaining(d);
- upb_pbdecoder_frame *outer = outer_frame(d);
- CHECK_SUSPEND(upb_sink_startstr(&outer->sink, arg, len, &d->top->sink));
- if (len == 0) {
- d->pc++; /* Skip OP_STRING. */
- }
- )
- VMCASE(OP_STRING,
- uint32_t len = curbufleft(d);
- size_t n = upb_sink_putstring(&d->top->sink, arg, d->ptr, len, handle);
- if (n > len) {
- if (n > delim_remaining(d)) {
- seterr(d, "Tried to skip past end of string.");
- return upb_pbdecoder_suspend(d);
- } else {
- int32_t ret = skip(d, n);
- /* This shouldn't return DECODE_OK, because n > len. */
- UPB_ASSERT(ret >= 0);
- return ret;
- }
- }
- advance(d, n);
- if (n < len || d->delim_end == NULL) {
- /* We aren't finished with this string yet. */
- d->pc--; /* Repeat OP_STRING. */
- if (n > 0) checkpoint(d);
- return upb_pbdecoder_suspend(d);
- }
- )
- VMCASE(OP_ENDSTR,
- CHECK_SUSPEND(upb_sink_endstr(&d->top->sink, arg));
- )
- VMCASE(OP_PUSHTAGDELIM,
- CHECK_SUSPEND(pushtagdelim(d, arg));
- )
- VMCASE(OP_SETBIGGROUPNUM,
- d->top->groupnum = *d->pc++;
- )
- VMCASE(OP_POP,
- UPB_ASSERT(d->top > d->stack);
- decoder_pop(d);
- )
- VMCASE(OP_PUSHLENDELIM,
- uint32_t len;
- CHECK_RETURN(decode_v32(d, &len));
- CHECK_SUSPEND(decoder_push(d, offset(d) + len));
- set_delim_end(d);
- )
- VMCASE(OP_SETDELIM,
- set_delim_end(d);
- )
- VMCASE(OP_CHECKDELIM,
- /* We are guaranteed of this assert because we never allow ourselves to
- * consume bytes beyond data_end, which covers delim_end when non-NULL.
- */
- UPB_ASSERT(!(d->delim_end && d->ptr > d->delim_end));
- if (d->ptr == d->delim_end)
- d->pc += longofs;
- )
- VMCASE(OP_CALL,
- d->callstack[d->call_len++] = d->pc;
- d->pc += longofs;
- )
- VMCASE(OP_RET,
- UPB_ASSERT(d->call_len > 0);
- d->pc = d->callstack[--d->call_len];
- )
- VMCASE(OP_BRANCH,
- d->pc += longofs;
- )
- VMCASE(OP_TAG1,
- uint8_t expected;
- CHECK_SUSPEND(curbufleft(d) > 0);
- expected = (arg >> 8) & 0xff;
- if (*d->ptr == expected) {
- advance(d, 1);
- } else {
- int8_t shortofs;
- badtag:
- shortofs = arg;
- if (shortofs == LABEL_DISPATCH) {
- CHECK_RETURN(dispatch(d));
- } else {
- d->pc += shortofs;
- break; /* Avoid checkpoint(). */
- }
- }
- )
- VMCASE(OP_TAG2,
- uint16_t expected;
- CHECK_SUSPEND(curbufleft(d) > 0);
- expected = (arg >> 8) & 0xffff;
- if (curbufleft(d) >= 2) {
- uint16_t actual;
- memcpy(&actual, d->ptr, 2);
- if (expected == actual) {
- advance(d, 2);
- } else {
- goto badtag;
- }
- } else {
- int32_t result = upb_pbdecoder_checktag_slow(d, expected);
- if (result == DECODE_MISMATCH) goto badtag;
- if (result >= 0) return result;
- }
- )
- VMCASE(OP_TAGN, {
- uint64_t expected;
- int32_t result;
- memcpy(&expected, d->pc, 8);
- d->pc += 2;
- result = upb_pbdecoder_checktag_slow(d, expected);
- if (result == DECODE_MISMATCH) goto badtag;
- if (result >= 0) return result;
- })
- VMCASE(OP_DISPATCH, {
- CHECK_RETURN(dispatch(d));
- })
- VMCASE(OP_HALT, {
- return d->size_param;
- })
- }
- }
- }
- /* BytesHandler handlers ******************************************************/
- void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint) {
- upb_pbdecoder *d = closure;
- UPB_UNUSED(size_hint);
- d->top->end_ofs = UINT64_MAX;
- d->bufstart_ofs = 0;
- d->call_len = 1;
- d->callstack[0] = &halt;
- d->pc = pc;
- d->skip = 0;
- return d;
- }
- void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint) {
- upb_pbdecoder *d = closure;
- UPB_UNUSED(hd);
- UPB_UNUSED(size_hint);
- d->top->end_ofs = UINT64_MAX;
- d->bufstart_ofs = 0;
- d->call_len = 0;
- d->skip = 0;
- return d;
- }
- bool upb_pbdecoder_end(void *closure, const void *handler_data) {
- upb_pbdecoder *d = closure;
- const upb_pbdecodermethod *method = handler_data;
- uint64_t end;
- char dummy;
- if (d->residual_end > d->residual) {
- seterr(d, "Unexpected EOF: decoder still has buffered unparsed data");
- return false;
- }
- if (d->skip) {
- seterr(d, "Unexpected EOF inside skipped data");
- return false;
- }
- if (d->top->end_ofs != UINT64_MAX) {
- seterr(d, "Unexpected EOF inside delimited string");
- return false;
- }
- /* The user's end() call indicates that the message ends here. */
- end = offset(d);
- d->top->end_ofs = end;
- #ifdef UPB_USE_JIT_X64
- if (method->is_native_) {
- const mgroup *group = (const mgroup*)method->group;
- if (d->top != d->stack)
- d->stack->end_ofs = 0;
- group->jit_code(closure, method->code_base.ptr, &dummy, 0, NULL);
- } else
- #endif
- {
- const uint32_t *p = d->pc;
- d->stack->end_ofs = end;
- /* Check the previous bytecode, but guard against beginning. */
- if (p != method->code_base.ptr) p--;
- if (getop(*p) == OP_CHECKDELIM) {
- /* Rewind from OP_TAG* to OP_CHECKDELIM. */
- UPB_ASSERT(getop(*d->pc) == OP_TAG1 ||
- getop(*d->pc) == OP_TAG2 ||
- getop(*d->pc) == OP_TAGN ||
- getop(*d->pc) == OP_DISPATCH);
- d->pc = p;
- }
- upb_pbdecoder_decode(closure, handler_data, &dummy, 0, NULL);
- }
- if (d->call_len != 0) {
- seterr(d, "Unexpected EOF inside submessage or group");
- return false;
- }
- return true;
- }
- size_t upb_pbdecoder_decode(void *decoder, const void *group, const char *buf,
- size_t size, const upb_bufhandle *handle) {
- int32_t result = upb_pbdecoder_resume(decoder, NULL, buf, size, handle);
- if (result == DECODE_ENDGROUP) goto_endmsg(decoder);
- CHECK_RETURN(result);
- return run_decoder_vm(decoder, group, handle);
- }
- /* Public API *****************************************************************/
- void upb_pbdecoder_reset(upb_pbdecoder *d) {
- d->top = d->stack;
- d->top->groupnum = 0;
- d->ptr = d->residual;
- d->buf = d->residual;
- d->end = d->residual;
- d->residual_end = d->residual;
- }
- upb_pbdecoder *upb_pbdecoder_create(upb_env *e, const upb_pbdecodermethod *m,
- upb_sink *sink) {
- const size_t default_max_nesting = 64;
- #ifndef NDEBUG
- size_t size_before = upb_env_bytesallocated(e);
- #endif
- upb_pbdecoder *d = upb_env_malloc(e, sizeof(upb_pbdecoder));
- if (!d) return NULL;
- d->method_ = m;
- d->callstack = upb_env_malloc(e, callstacksize(d, default_max_nesting));
- d->stack = upb_env_malloc(e, stacksize(d, default_max_nesting));
- if (!d->stack || !d->callstack) {
- return NULL;
- }
- d->env = e;
- d->limit = d->stack + default_max_nesting - 1;
- d->stack_size = default_max_nesting;
- d->status = NULL;
- upb_pbdecoder_reset(d);
- upb_bytessink_reset(&d->input_, &m->input_handler_, d);
- UPB_ASSERT(sink);
- if (d->method_->dest_handlers_) {
- if (sink->handlers != d->method_->dest_handlers_)
- return NULL;
- }
- upb_sink_reset(&d->top->sink, sink->handlers, sink->closure);
- /* If this fails, increase the value in decoder.h. */
- UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(e) - size_before <=
- UPB_PB_DECODER_SIZE);
- return d;
- }
- uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d) {
- return offset(d);
- }
- const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d) {
- return d->method_;
- }
- upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d) {
- return &d->input_;
- }
- size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d) {
- return d->stack_size;
- }
- bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max) {
- UPB_ASSERT(d->top >= d->stack);
- if (max < (size_t)(d->top - d->stack)) {
- /* Can't set a limit smaller than what we are currently at. */
- return false;
- }
- if (max > d->stack_size) {
- /* Need to reallocate stack and callstack to accommodate. */
- size_t old_size = stacksize(d, d->stack_size);
- size_t new_size = stacksize(d, max);
- void *p = upb_env_realloc(d->env, d->stack, old_size, new_size);
- if (!p) {
- return false;
- }
- d->stack = p;
- old_size = callstacksize(d, d->stack_size);
- new_size = callstacksize(d, max);
- p = upb_env_realloc(d->env, d->callstack, old_size, new_size);
- if (!p) {
- return false;
- }
- d->callstack = p;
- d->stack_size = max;
- }
- d->limit = d->stack + max - 1;
- return true;
- }
- /*
- ** upb::Encoder
- **
- ** Since we are implementing pure handlers (ie. without any out-of-band access
- ** to pre-computed lengths), we have to buffer all submessages before we can
- ** emit even their first byte.
- **
- ** Not knowing the size of submessages also means we can't write a perfect
- ** zero-copy implementation, even with buffering. Lengths are stored as
- ** varints, which means that we don't know how many bytes to reserve for the
- ** length until we know what the length is.
- **
- ** This leaves us with three main choices:
- **
- ** 1. buffer all submessage data in a temporary buffer, then copy it exactly
- ** once into the output buffer.
- **
- ** 2. attempt to buffer data directly into the output buffer, estimating how
- ** many bytes each length will take. When our guesses are wrong, use
- ** memmove() to grow or shrink the allotted space.
- **
- ** 3. buffer directly into the output buffer, allocating a max length
- ** ahead-of-time for each submessage length. If we overallocated, we waste
- ** space, but no memcpy() or memmove() is required. This approach requires
- ** defining a maximum size for submessages and rejecting submessages that
- ** exceed that size.
- **
- ** (2) and (3) have the potential to have better performance, but they are more
- ** complicated and subtle to implement:
- **
- ** (3) requires making an arbitrary choice of the maximum message size; it
- ** wastes space when submessages are shorter than this and fails
- ** completely when they are longer. This makes it more finicky and
- ** requires configuration based on the input. It also makes it impossible
- ** to perfectly match the output of reference encoders that always use the
- ** optimal amount of space for each length.
- **
- ** (2) requires guessing the the size upfront, and if multiple lengths are
- ** guessed wrong the minimum required number of memmove() operations may
- ** be complicated to compute correctly. Implemented properly, it may have
- ** a useful amortized or average cost, but more investigation is required
- ** to determine this and what the optimal algorithm is to achieve it.
- **
- ** (1) makes you always pay for exactly one copy, but its implementation is
- ** the simplest and its performance is predictable.
- **
- ** So for now, we implement (1) only. If we wish to optimize later, we should
- ** be able to do it without affecting users.
- **
- ** The strategy is to buffer the segments of data that do *not* depend on
- ** unknown lengths in one buffer, and keep a separate buffer of segment pointers
- ** and lengths. When the top-level submessage ends, we can go beginning to end,
- ** alternating the writing of lengths with memcpy() of the rest of the data.
- ** At the top level though, no buffering is required.
- */
- /* The output buffer is divided into segments; a segment is a string of data
- * that is "ready to go" -- it does not need any varint lengths inserted into
- * the middle. The seams between segments are where varints will be inserted
- * once they are known.
- *
- * We also use the concept of a "run", which is a range of encoded bytes that
- * occur at a single submessage level. Every segment contains one or more runs.
- *
- * A segment can span messages. Consider:
- *
- * .--Submessage lengths---------.
- * | | |
- * | V V
- * V | |--------------- | |-----------------
- * Submessages: | |-----------------------------------------------
- * Top-level msg: ------------------------------------------------------------
- *
- * Segments: ----- ------------------- -----------------
- * Runs: *---- *--------------*--- *----------------
- * (* marks the start)
- *
- * Note that the top-level menssage is not in any segment because it does not
- * have any length preceding it.
- *
- * A segment is only interrupted when another length needs to be inserted. So
- * observe how the second segment spans both the inner submessage and part of
- * the next enclosing message. */
- typedef struct {
- uint32_t msglen; /* The length to varint-encode before this segment. */
- uint32_t seglen; /* Length of the segment. */
- } upb_pb_encoder_segment;
- struct upb_pb_encoder {
- upb_env *env;
- /* Our input and output. */
- upb_sink input_;
- upb_bytessink *output_;
- /* The "subclosure" -- used as the inner closure as part of the bytessink
- * protocol. */
- void *subc;
- /* The output buffer and limit, and our current write position. "buf"
- * initially points to "initbuf", but is dynamically allocated if we need to
- * grow beyond the initial size. */
- char *buf, *ptr, *limit;
- /* The beginning of the current run, or undefined if we are at the top
- * level. */
- char *runbegin;
- /* The list of segments we are accumulating. */
- upb_pb_encoder_segment *segbuf, *segptr, *seglimit;
- /* The stack of enclosing submessages. Each entry in the stack points to the
- * segment where this submessage's length is being accumulated. */
- int *stack, *top, *stacklimit;
- /* Depth of startmsg/endmsg calls. */
- int depth;
- };
- /* low-level buffering ********************************************************/
- /* Low-level functions for interacting with the output buffer. */
- /* TODO(haberman): handle pushback */
- static void putbuf(upb_pb_encoder *e, const char *buf, size_t len) {
- size_t n = upb_bytessink_putbuf(e->output_, e->subc, buf, len, NULL);
- UPB_ASSERT(n == len);
- }
- static upb_pb_encoder_segment *top(upb_pb_encoder *e) {
- return &e->segbuf[*e->top];
- }
- /* Call to ensure that at least "bytes" bytes are available for writing at
- * e->ptr. Returns false if the bytes could not be allocated. */
- static bool reserve(upb_pb_encoder *e, size_t bytes) {
- if ((size_t)(e->limit - e->ptr) < bytes) {
- /* Grow buffer. */
- char *new_buf;
- size_t needed = bytes + (e->ptr - e->buf);
- size_t old_size = e->limit - e->buf;
- size_t new_size = old_size;
- while (new_size < needed) {
- new_size *= 2;
- }
- new_buf = upb_env_realloc(e->env, e->buf, old_size, new_size);
- if (new_buf == NULL) {
- return false;
- }
- e->ptr = new_buf + (e->ptr - e->buf);
- e->runbegin = new_buf + (e->runbegin - e->buf);
- e->limit = new_buf + new_size;
- e->buf = new_buf;
- }
- return true;
- }
- /* Call when "bytes" bytes have been writte at e->ptr. The caller *must* have
- * previously called reserve() with at least this many bytes. */
- static void encoder_advance(upb_pb_encoder *e, size_t bytes) {
- UPB_ASSERT((size_t)(e->limit - e->ptr) >= bytes);
- e->ptr += bytes;
- }
- /* Call when all of the bytes for a handler have been written. Flushes the
- * bytes if possible and necessary, returning false if this failed. */
- static bool commit(upb_pb_encoder *e) {
- if (!e->top) {
- /* We aren't inside a delimited region. Flush our accumulated bytes to
- * the output.
- *
- * TODO(haberman): in the future we may want to delay flushing for
- * efficiency reasons. */
- putbuf(e, e->buf, e->ptr - e->buf);
- e->ptr = e->buf;
- }
- return true;
- }
- /* Writes the given bytes to the buffer, handling reserve/advance. */
- static bool encode_bytes(upb_pb_encoder *e, const void *data, size_t len) {
- if (!reserve(e, len)) {
- return false;
- }
- memcpy(e->ptr, data, len);
- encoder_advance(e, len);
- return true;
- }
- /* Finish the current run by adding the run totals to the segment and message
- * length. */
- static void accumulate(upb_pb_encoder *e) {
- size_t run_len;
- UPB_ASSERT(e->ptr >= e->runbegin);
- run_len = e->ptr - e->runbegin;
- e->segptr->seglen += run_len;
- top(e)->msglen += run_len;
- e->runbegin = e->ptr;
- }
- /* Call to indicate the start of delimited region for which the full length is
- * not yet known. All data will be buffered until the length is known.
- * Delimited regions may be nested; their lengths will all be tracked properly. */
- static bool start_delim(upb_pb_encoder *e) {
- if (e->top) {
- /* We are already buffering, advance to the next segment and push it on the
- * stack. */
- accumulate(e);
- if (++e->top == e->stacklimit) {
- /* TODO(haberman): grow stack? */
- return false;
- }
- if (++e->segptr == e->seglimit) {
- /* Grow segment buffer. */
- size_t old_size =
- (e->seglimit - e->segbuf) * sizeof(upb_pb_encoder_segment);
- size_t new_size = old_size * 2;
- upb_pb_encoder_segment *new_buf =
- upb_env_realloc(e->env, e->segbuf, old_size, new_size);
- if (new_buf == NULL) {
- return false;
- }
- e->segptr = new_buf + (e->segptr - e->segbuf);
- e->seglimit = new_buf + (new_size / sizeof(upb_pb_encoder_segment));
- e->segbuf = new_buf;
- }
- } else {
- /* We were previously at the top level, start buffering. */
- e->segptr = e->segbuf;
- e->top = e->stack;
- e->runbegin = e->ptr;
- }
- *e->top = e->segptr - e->segbuf;
- e->segptr->seglen = 0;
- e->segptr->msglen = 0;
- return true;
- }
- /* Call to indicate the end of a delimited region. We now know the length of
- * the delimited region. If we are not nested inside any other delimited
- * regions, we can now emit all of the buffered data we accumulated. */
- static bool end_delim(upb_pb_encoder *e) {
- size_t msglen;
- accumulate(e);
- msglen = top(e)->msglen;
- if (e->top == e->stack) {
- /* All lengths are now available, emit all buffered data. */
- char buf[UPB_PB_VARINT_MAX_LEN];
- upb_pb_encoder_segment *s;
- const char *ptr = e->buf;
- for (s = e->segbuf; s <= e->segptr; s++) {
- size_t lenbytes = upb_vencode64(s->msglen, buf);
- putbuf(e, buf, lenbytes);
- putbuf(e, ptr, s->seglen);
- ptr += s->seglen;
- }
- e->ptr = e->buf;
- e->top = NULL;
- } else {
- /* Need to keep buffering; propagate length info into enclosing
- * submessages. */
- --e->top;
- top(e)->msglen += msglen + upb_varint_size(msglen);
- }
- return true;
- }
- /* tag_t **********************************************************************/
- /* A precomputed (pre-encoded) tag and length. */
- typedef struct {
- uint8_t bytes;
- char tag[7];
- } tag_t;
- /* Allocates a new tag for this field, and sets it in these handlerattr. */
- static void new_tag(upb_handlers *h, const upb_fielddef *f, upb_wiretype_t wt,
- upb_handlerattr *attr) {
- uint32_t n = upb_fielddef_number(f);
- tag_t *tag = upb_gmalloc(sizeof(tag_t));
- tag->bytes = upb_vencode64((n << 3) | wt, tag->tag);
- upb_handlerattr_init(attr);
- upb_handlerattr_sethandlerdata(attr, tag);
- upb_handlers_addcleanup(h, tag, upb_gfree);
- }
- static bool encode_tag(upb_pb_encoder *e, const tag_t *tag) {
- return encode_bytes(e, tag->tag, tag->bytes);
- }
- /* encoding of wire types *****************************************************/
- static bool encode_fixed64(upb_pb_encoder *e, uint64_t val) {
- /* TODO(haberman): byte-swap for big endian. */
- return encode_bytes(e, &val, sizeof(uint64_t));
- }
- static bool encode_fixed32(upb_pb_encoder *e, uint32_t val) {
- /* TODO(haberman): byte-swap for big endian. */
- return encode_bytes(e, &val, sizeof(uint32_t));
- }
- static bool encode_varint(upb_pb_encoder *e, uint64_t val) {
- if (!reserve(e, UPB_PB_VARINT_MAX_LEN)) {
- return false;
- }
- encoder_advance(e, upb_vencode64(val, e->ptr));
- return true;
- }
- static uint64_t dbl2uint64(double d) {
- uint64_t ret;
- memcpy(&ret, &d, sizeof(uint64_t));
- return ret;
- }
- static uint32_t flt2uint32(float d) {
- uint32_t ret;
- memcpy(&ret, &d, sizeof(uint32_t));
- return ret;
- }
- /* encoding of proto types ****************************************************/
- static bool startmsg(void *c, const void *hd) {
- upb_pb_encoder *e = c;
- UPB_UNUSED(hd);
- if (e->depth++ == 0) {
- upb_bytessink_start(e->output_, 0, &e->subc);
- }
- return true;
- }
- static bool endmsg(void *c, const void *hd, upb_status *status) {
- upb_pb_encoder *e = c;
- UPB_UNUSED(hd);
- UPB_UNUSED(status);
- if (--e->depth == 0) {
- upb_bytessink_end(e->output_);
- }
- return true;
- }
- static void *encode_startdelimfield(void *c, const void *hd) {
- bool ok = encode_tag(c, hd) && commit(c) && start_delim(c);
- return ok ? c : UPB_BREAK;
- }
- static bool encode_unknown(void *c, const void *hd, const char *buf,
- size_t len) {
- UPB_UNUSED(hd);
- return encode_bytes(c, buf, len) && commit(c);
- }
- static bool encode_enddelimfield(void *c, const void *hd) {
- UPB_UNUSED(hd);
- return end_delim(c);
- }
- static void *encode_startgroup(void *c, const void *hd) {
- return (encode_tag(c, hd) && commit(c)) ? c : UPB_BREAK;
- }
- static bool encode_endgroup(void *c, const void *hd) {
- return encode_tag(c, hd) && commit(c);
- }
- static void *encode_startstr(void *c, const void *hd, size_t size_hint) {
- UPB_UNUSED(size_hint);
- return encode_startdelimfield(c, hd);
- }
- static size_t encode_strbuf(void *c, const void *hd, const char *buf,
- size_t len, const upb_bufhandle *h) {
- UPB_UNUSED(hd);
- UPB_UNUSED(h);
- return encode_bytes(c, buf, len) ? len : 0;
- }
- #define T(type, ctype, convert, encode) \
- static bool encode_scalar_##type(void *e, const void *hd, ctype val) { \
- return encode_tag(e, hd) && encode(e, (convert)(val)) && commit(e); \
- } \
- static bool encode_packed_##type(void *e, const void *hd, ctype val) { \
- UPB_UNUSED(hd); \
- return encode(e, (convert)(val)); \
- }
- T(double, double, dbl2uint64, encode_fixed64)
- T(float, float, flt2uint32, encode_fixed32)
- T(int64, int64_t, uint64_t, encode_varint)
- T(int32, int32_t, int64_t, encode_varint)
- T(fixed64, uint64_t, uint64_t, encode_fixed64)
- T(fixed32, uint32_t, uint32_t, encode_fixed32)
- T(bool, bool, bool, encode_varint)
- T(uint32, uint32_t, uint32_t, encode_varint)
- T(uint64, uint64_t, uint64_t, encode_varint)
- T(enum, int32_t, uint32_t, encode_varint)
- T(sfixed32, int32_t, uint32_t, encode_fixed32)
- T(sfixed64, int64_t, uint64_t, encode_fixed64)
- T(sint32, int32_t, upb_zzenc_32, encode_varint)
- T(sint64, int64_t, upb_zzenc_64, encode_varint)
- #undef T
- /* code to build the handlers *************************************************/
- static void newhandlers_callback(const void *closure, upb_handlers *h) {
- const upb_msgdef *m;
- upb_msg_field_iter i;
- UPB_UNUSED(closure);
- upb_handlers_setstartmsg(h, startmsg, NULL);
- upb_handlers_setendmsg(h, endmsg, NULL);
- upb_handlers_setunknown(h, encode_unknown, NULL);
- m = upb_handlers_msgdef(h);
- for(upb_msg_field_begin(&i, m);
- !upb_msg_field_done(&i);
- upb_msg_field_next(&i)) {
- const upb_fielddef *f = upb_msg_iter_field(&i);
- bool packed = upb_fielddef_isseq(f) && upb_fielddef_isprimitive(f) &&
- upb_fielddef_packed(f);
- upb_handlerattr attr;
- upb_wiretype_t wt =
- packed ? UPB_WIRE_TYPE_DELIMITED
- : upb_pb_native_wire_types[upb_fielddef_descriptortype(f)];
- /* Pre-encode the tag for this field. */
- new_tag(h, f, wt, &attr);
- if (packed) {
- upb_handlers_setstartseq(h, f, encode_startdelimfield, &attr);
- upb_handlers_setendseq(h, f, encode_enddelimfield, &attr);
- }
- #define T(upper, lower, upbtype) \
- case UPB_DESCRIPTOR_TYPE_##upper: \
- if (packed) { \
- upb_handlers_set##upbtype(h, f, encode_packed_##lower, &attr); \
- } else { \
- upb_handlers_set##upbtype(h, f, encode_scalar_##lower, &attr); \
- } \
- break;
- switch (upb_fielddef_descriptortype(f)) {
- T(DOUBLE, double, double);
- T(FLOAT, float, float);
- T(INT64, int64, int64);
- T(INT32, int32, int32);
- T(FIXED64, fixed64, uint64);
- T(FIXED32, fixed32, uint32);
- T(BOOL, bool, bool);
- T(UINT32, uint32, uint32);
- T(UINT64, uint64, uint64);
- T(ENUM, enum, int32);
- T(SFIXED32, sfixed32, int32);
- T(SFIXED64, sfixed64, int64);
- T(SINT32, sint32, int32);
- T(SINT64, sint64, int64);
- case UPB_DESCRIPTOR_TYPE_STRING:
- case UPB_DESCRIPTOR_TYPE_BYTES:
- upb_handlers_setstartstr(h, f, encode_startstr, &attr);
- upb_handlers_setendstr(h, f, encode_enddelimfield, &attr);
- upb_handlers_setstring(h, f, encode_strbuf, &attr);
- break;
- case UPB_DESCRIPTOR_TYPE_MESSAGE:
- upb_handlers_setstartsubmsg(h, f, encode_startdelimfield, &attr);
- upb_handlers_setendsubmsg(h, f, encode_enddelimfield, &attr);
- break;
- case UPB_DESCRIPTOR_TYPE_GROUP: {
- /* Endgroup takes a different tag (wire_type = END_GROUP). */
- upb_handlerattr attr2;
- new_tag(h, f, UPB_WIRE_TYPE_END_GROUP, &attr2);
- upb_handlers_setstartsubmsg(h, f, encode_startgroup, &attr);
- upb_handlers_setendsubmsg(h, f, encode_endgroup, &attr2);
- upb_handlerattr_uninit(&attr2);
- break;
- }
- }
- #undef T
- upb_handlerattr_uninit(&attr);
- }
- }
- void upb_pb_encoder_reset(upb_pb_encoder *e) {
- e->segptr = NULL;
- e->top = NULL;
- e->depth = 0;
- }
- /* public API *****************************************************************/
- const upb_handlers *upb_pb_encoder_newhandlers(const upb_msgdef *m,
- const void *owner) {
- return upb_handlers_newfrozen(m, owner, newhandlers_callback, NULL);
- }
- upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h,
- upb_bytessink *output) {
- const size_t initial_bufsize = 256;
- const size_t initial_segbufsize = 16;
- /* TODO(haberman): make this configurable. */
- const size_t stack_size = 64;
- #ifndef NDEBUG
- const size_t size_before = upb_env_bytesallocated(env);
- #endif
- upb_pb_encoder *e = upb_env_malloc(env, sizeof(upb_pb_encoder));
- if (!e) return NULL;
- e->buf = upb_env_malloc(env, initial_bufsize);
- e->segbuf = upb_env_malloc(env, initial_segbufsize * sizeof(*e->segbuf));
- e->stack = upb_env_malloc(env, stack_size * sizeof(*e->stack));
- if (!e->buf || !e->segbuf || !e->stack) {
- return NULL;
- }
- e->limit = e->buf + initial_bufsize;
- e->seglimit = e->segbuf + initial_segbufsize;
- e->stacklimit = e->stack + stack_size;
- upb_pb_encoder_reset(e);
- upb_sink_reset(&e->input_, h, e);
- e->env = env;
- e->output_ = output;
- e->subc = output->closure;
- e->ptr = e->buf;
- /* If this fails, increase the value in encoder.h. */
- UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(env) - size_before <=
- UPB_PB_ENCODER_SIZE);
- return e;
- }
- upb_sink *upb_pb_encoder_input(upb_pb_encoder *e) { return &e->input_; }
- upb_filedef **upb_loaddescriptor(const char *buf, size_t n, const void *owner,
- upb_status *status) {
- /* Create handlers. */
- const upb_pbdecodermethod *decoder_m;
- const upb_handlers *reader_h = upb_descreader_newhandlers(&reader_h);
- upb_env env;
- upb_pbdecodermethodopts opts;
- upb_pbdecoder *decoder;
- upb_descreader *reader;
- bool ok;
- size_t i;
- upb_filedef **ret = NULL;
- upb_pbdecodermethodopts_init(&opts, reader_h);
- decoder_m = upb_pbdecodermethod_new(&opts, &decoder_m);
- upb_env_init(&env);
- upb_env_reporterrorsto(&env, status);
- reader = upb_descreader_create(&env, reader_h);
- decoder = upb_pbdecoder_create(&env, decoder_m, upb_descreader_input(reader));
- /* Push input data. */
- ok = upb_bufsrc_putbuf(buf, n, upb_pbdecoder_input(decoder));
- if (!ok) {
- goto cleanup;
- }
- ret = upb_gmalloc(sizeof (*ret) * (upb_descreader_filecount(reader) + 1));
- if (!ret) {
- goto cleanup;
- }
- for (i = 0; i < upb_descreader_filecount(reader); i++) {
- ret[i] = upb_descreader_file(reader, i);
- upb_filedef_ref(ret[i], owner);
- }
- ret[i] = NULL;
- cleanup:
- upb_env_uninit(&env);
- upb_handlers_unref(reader_h, &reader_h);
- upb_pbdecodermethod_unref(decoder_m, &decoder_m);
- return ret;
- }
- /*
- * upb::pb::TextPrinter
- *
- * OPT: This is not optimized at all. It uses printf() which parses the format
- * string every time, and it allocates memory for every put.
- */
- #include <ctype.h>
- #include <float.h>
- #include <inttypes.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <string.h>
- struct upb_textprinter {
- upb_sink input_;
- upb_bytessink *output_;
- int indent_depth_;
- bool single_line_;
- void *subc;
- };
- #define CHECK(x) if ((x) < 0) goto err;
- static const char *shortname(const char *longname) {
- const char *last = strrchr(longname, '.');
- return last ? last + 1 : longname;
- }
- static int indent(upb_textprinter *p) {
- int i;
- if (!p->single_line_)
- for (i = 0; i < p->indent_depth_; i++)
- upb_bytessink_putbuf(p->output_, p->subc, " ", 2, NULL);
- return 0;
- }
- static int endfield(upb_textprinter *p) {
- const char ch = (p->single_line_ ? ' ' : '\n');
- upb_bytessink_putbuf(p->output_, p->subc, &ch, 1, NULL);
- return 0;
- }
- static int putescaped(upb_textprinter *p, const char *buf, size_t len,
- bool preserve_utf8) {
- /* Based on CEscapeInternal() from Google's protobuf release. */
- char dstbuf[4096], *dst = dstbuf, *dstend = dstbuf + sizeof(dstbuf);
- const char *end = buf + len;
- /* I think hex is prettier and more useful, but proto2 uses octal; should
- * investigate whether it can parse hex also. */
- const bool use_hex = false;
- bool last_hex_escape = false; /* true if last output char was \xNN */
- for (; buf < end; buf++) {
- bool is_hex_escape;
- if (dstend - dst < 4) {
- upb_bytessink_putbuf(p->output_, p->subc, dstbuf, dst - dstbuf, NULL);
- dst = dstbuf;
- }
- is_hex_escape = false;
- switch (*buf) {
- case '\n': *(dst++) = '\\'; *(dst++) = 'n'; break;
- case '\r': *(dst++) = '\\'; *(dst++) = 'r'; break;
- case '\t': *(dst++) = '\\'; *(dst++) = 't'; break;
- case '\"': *(dst++) = '\\'; *(dst++) = '\"'; break;
- case '\'': *(dst++) = '\\'; *(dst++) = '\''; break;
- case '\\': *(dst++) = '\\'; *(dst++) = '\\'; break;
- default:
- /* Note that if we emit \xNN and the buf character after that is a hex
- * digit then that digit must be escaped too to prevent it being
- * interpreted as part of the character code by C. */
- if ((!preserve_utf8 || (uint8_t)*buf < 0x80) &&
- (!isprint(*buf) || (last_hex_escape && isxdigit(*buf)))) {
- sprintf(dst, (use_hex ? "\\x%02x" : "\\%03o"), (uint8_t)*buf);
- is_hex_escape = use_hex;
- dst += 4;
- } else {
- *(dst++) = *buf; break;
- }
- }
- last_hex_escape = is_hex_escape;
- }
- /* Flush remaining data. */
- upb_bytessink_putbuf(p->output_, p->subc, dstbuf, dst - dstbuf, NULL);
- return 0;
- }
- bool putf(upb_textprinter *p, const char *fmt, ...) {
- va_list args;
- va_list args_copy;
- char *str;
- int written;
- int len;
- bool ok;
- va_start(args, fmt);
- /* Run once to get the length of the string. */
- _upb_va_copy(args_copy, args);
- len = _upb_vsnprintf(NULL, 0, fmt, args_copy);
- va_end(args_copy);
- /* + 1 for NULL terminator (vsprintf() requires it even if we don't). */
- str = upb_gmalloc(len + 1);
- if (!str) return false;
- written = vsprintf(str, fmt, args);
- va_end(args);
- UPB_ASSERT(written == len);
- ok = upb_bytessink_putbuf(p->output_, p->subc, str, len, NULL);
- upb_gfree(str);
- return ok;
- }
- /* handlers *******************************************************************/
- static bool textprinter_startmsg(void *c, const void *hd) {
- upb_textprinter *p = c;
- UPB_UNUSED(hd);
- if (p->indent_depth_ == 0) {
- upb_bytessink_start(p->output_, 0, &p->subc);
- }
- return true;
- }
- static bool textprinter_endmsg(void *c, const void *hd, upb_status *s) {
- upb_textprinter *p = c;
- UPB_UNUSED(hd);
- UPB_UNUSED(s);
- if (p->indent_depth_ == 0) {
- upb_bytessink_end(p->output_);
- }
- return true;
- }
- #define TYPE(name, ctype, fmt) \
- static bool textprinter_put ## name(void *closure, const void *handler_data, \
- ctype val) { \
- upb_textprinter *p = closure; \
- const upb_fielddef *f = handler_data; \
- CHECK(indent(p)); \
- putf(p, "%s: " fmt, upb_fielddef_name(f), val); \
- CHECK(endfield(p)); \
- return true; \
- err: \
- return false; \
- }
- static bool textprinter_putbool(void *closure, const void *handler_data,
- bool val) {
- upb_textprinter *p = closure;
- const upb_fielddef *f = handler_data;
- CHECK(indent(p));
- putf(p, "%s: %s", upb_fielddef_name(f), val ? "true" : "false");
- CHECK(endfield(p));
- return true;
- err:
- return false;
- }
- #define STRINGIFY_HELPER(x) #x
- #define STRINGIFY_MACROVAL(x) STRINGIFY_HELPER(x)
- TYPE(int32, int32_t, "%" PRId32)
- TYPE(int64, int64_t, "%" PRId64)
- TYPE(uint32, uint32_t, "%" PRIu32)
- TYPE(uint64, uint64_t, "%" PRIu64)
- TYPE(float, float, "%." STRINGIFY_MACROVAL(FLT_DIG) "g")
- TYPE(double, double, "%." STRINGIFY_MACROVAL(DBL_DIG) "g")
- #undef TYPE
- /* Output a symbolic value from the enum if found, else just print as int32. */
- static bool textprinter_putenum(void *closure, const void *handler_data,
- int32_t val) {
- upb_textprinter *p = closure;
- const upb_fielddef *f = handler_data;
- const upb_enumdef *enum_def = upb_downcast_enumdef(upb_fielddef_subdef(f));
- const char *label = upb_enumdef_iton(enum_def, val);
- if (label) {
- indent(p);
- putf(p, "%s: %s", upb_fielddef_name(f), label);
- endfield(p);
- } else {
- if (!textprinter_putint32(closure, handler_data, val))
- return false;
- }
- return true;
- }
- static void *textprinter_startstr(void *closure, const void *handler_data,
- size_t size_hint) {
- upb_textprinter *p = closure;
- const upb_fielddef *f = handler_data;
- UPB_UNUSED(size_hint);
- indent(p);
- putf(p, "%s: \"", upb_fielddef_name(f));
- return p;
- }
- static bool textprinter_endstr(void *closure, const void *handler_data) {
- upb_textprinter *p = closure;
- UPB_UNUSED(handler_data);
- putf(p, "\"");
- endfield(p);
- return true;
- }
- static size_t textprinter_putstr(void *closure, const void *hd, const char *buf,
- size_t len, const upb_bufhandle *handle) {
- upb_textprinter *p = closure;
- const upb_fielddef *f = hd;
- UPB_UNUSED(handle);
- CHECK(putescaped(p, buf, len, upb_fielddef_type(f) == UPB_TYPE_STRING));
- return len;
- err:
- return 0;
- }
- static void *textprinter_startsubmsg(void *closure, const void *handler_data) {
- upb_textprinter *p = closure;
- const char *name = handler_data;
- CHECK(indent(p));
- putf(p, "%s {%c", name, p->single_line_ ? ' ' : '\n');
- p->indent_depth_++;
- return p;
- err:
- return UPB_BREAK;
- }
- static bool textprinter_endsubmsg(void *closure, const void *handler_data) {
- upb_textprinter *p = closure;
- UPB_UNUSED(handler_data);
- p->indent_depth_--;
- CHECK(indent(p));
- upb_bytessink_putbuf(p->output_, p->subc, "}", 1, NULL);
- CHECK(endfield(p));
- return true;
- err:
- return false;
- }
- static void onmreg(const void *c, upb_handlers *h) {
- const upb_msgdef *m = upb_handlers_msgdef(h);
- upb_msg_field_iter i;
- UPB_UNUSED(c);
- upb_handlers_setstartmsg(h, textprinter_startmsg, NULL);
- upb_handlers_setendmsg(h, textprinter_endmsg, NULL);
- for(upb_msg_field_begin(&i, m);
- !upb_msg_field_done(&i);
- upb_msg_field_next(&i)) {
- upb_fielddef *f = upb_msg_iter_field(&i);
- upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
- upb_handlerattr_sethandlerdata(&attr, f);
- switch (upb_fielddef_type(f)) {
- case UPB_TYPE_INT32:
- upb_handlers_setint32(h, f, textprinter_putint32, &attr);
- break;
- case UPB_TYPE_INT64:
- upb_handlers_setint64(h, f, textprinter_putint64, &attr);
- break;
- case UPB_TYPE_UINT32:
- upb_handlers_setuint32(h, f, textprinter_putuint32, &attr);
- break;
- case UPB_TYPE_UINT64:
- upb_handlers_setuint64(h, f, textprinter_putuint64, &attr);
- break;
- case UPB_TYPE_FLOAT:
- upb_handlers_setfloat(h, f, textprinter_putfloat, &attr);
- break;
- case UPB_TYPE_DOUBLE:
- upb_handlers_setdouble(h, f, textprinter_putdouble, &attr);
- break;
- case UPB_TYPE_BOOL:
- upb_handlers_setbool(h, f, textprinter_putbool, &attr);
- break;
- case UPB_TYPE_STRING:
- case UPB_TYPE_BYTES:
- upb_handlers_setstartstr(h, f, textprinter_startstr, &attr);
- upb_handlers_setstring(h, f, textprinter_putstr, &attr);
- upb_handlers_setendstr(h, f, textprinter_endstr, &attr);
- break;
- case UPB_TYPE_MESSAGE: {
- const char *name =
- upb_fielddef_istagdelim(f)
- ? shortname(upb_msgdef_fullname(upb_fielddef_msgsubdef(f)))
- : upb_fielddef_name(f);
- upb_handlerattr_sethandlerdata(&attr, name);
- upb_handlers_setstartsubmsg(h, f, textprinter_startsubmsg, &attr);
- upb_handlers_setendsubmsg(h, f, textprinter_endsubmsg, &attr);
- break;
- }
- case UPB_TYPE_ENUM:
- upb_handlers_setint32(h, f, textprinter_putenum, &attr);
- break;
- }
- }
- }
- static void textprinter_reset(upb_textprinter *p, bool single_line) {
- p->single_line_ = single_line;
- p->indent_depth_ = 0;
- }
- /* Public API *****************************************************************/
- upb_textprinter *upb_textprinter_create(upb_env *env, const upb_handlers *h,
- upb_bytessink *output) {
- upb_textprinter *p = upb_env_malloc(env, sizeof(upb_textprinter));
- if (!p) return NULL;
- p->output_ = output;
- upb_sink_reset(&p->input_, h, p);
- textprinter_reset(p, false);
- return p;
- }
- const upb_handlers *upb_textprinter_newhandlers(const upb_msgdef *m,
- const void *owner) {
- return upb_handlers_newfrozen(m, owner, &onmreg, NULL);
- }
- upb_sink *upb_textprinter_input(upb_textprinter *p) { return &p->input_; }
- void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line) {
- p->single_line_ = single_line;
- }
- /* Index is descriptor type. */
- const uint8_t upb_pb_native_wire_types[] = {
- UPB_WIRE_TYPE_END_GROUP, /* ENDGROUP */
- UPB_WIRE_TYPE_64BIT, /* DOUBLE */
- UPB_WIRE_TYPE_32BIT, /* FLOAT */
- UPB_WIRE_TYPE_VARINT, /* INT64 */
- UPB_WIRE_TYPE_VARINT, /* UINT64 */
- UPB_WIRE_TYPE_VARINT, /* INT32 */
- UPB_WIRE_TYPE_64BIT, /* FIXED64 */
- UPB_WIRE_TYPE_32BIT, /* FIXED32 */
- UPB_WIRE_TYPE_VARINT, /* BOOL */
- UPB_WIRE_TYPE_DELIMITED, /* STRING */
- UPB_WIRE_TYPE_START_GROUP, /* GROUP */
- UPB_WIRE_TYPE_DELIMITED, /* MESSAGE */
- UPB_WIRE_TYPE_DELIMITED, /* BYTES */
- UPB_WIRE_TYPE_VARINT, /* UINT32 */
- UPB_WIRE_TYPE_VARINT, /* ENUM */
- UPB_WIRE_TYPE_32BIT, /* SFIXED32 */
- UPB_WIRE_TYPE_64BIT, /* SFIXED64 */
- UPB_WIRE_TYPE_VARINT, /* SINT32 */
- UPB_WIRE_TYPE_VARINT, /* SINT64 */
- };
- /* A basic branch-based decoder, uses 32-bit values to get good performance
- * on 32-bit architectures (but performs well on 64-bits also).
- * This scheme comes from the original Google Protobuf implementation
- * (proto2). */
- upb_decoderet upb_vdecode_max8_branch32(upb_decoderet r) {
- upb_decoderet err = {NULL, 0};
- const char *p = r.p;
- uint32_t low = (uint32_t)r.val;
- uint32_t high = 0;
- uint32_t b;
- b = *(p++); low |= (b & 0x7fU) << 14; if (!(b & 0x80)) goto done;
- b = *(p++); low |= (b & 0x7fU) << 21; if (!(b & 0x80)) goto done;
- b = *(p++); low |= (b & 0x7fU) << 28;
- high = (b & 0x7fU) >> 4; if (!(b & 0x80)) goto done;
- b = *(p++); high |= (b & 0x7fU) << 3; if (!(b & 0x80)) goto done;
- b = *(p++); high |= (b & 0x7fU) << 10; if (!(b & 0x80)) goto done;
- b = *(p++); high |= (b & 0x7fU) << 17; if (!(b & 0x80)) goto done;
- b = *(p++); high |= (b & 0x7fU) << 24; if (!(b & 0x80)) goto done;
- b = *(p++); high |= (b & 0x7fU) << 31; if (!(b & 0x80)) goto done;
- return err;
- done:
- r.val = ((uint64_t)high << 32) | low;
- r.p = p;
- return r;
- }
- /* Like the previous, but uses 64-bit values. */
- upb_decoderet upb_vdecode_max8_branch64(upb_decoderet r) {
- const char *p = r.p;
- uint64_t val = r.val;
- uint64_t b;
- upb_decoderet err = {NULL, 0};
- b = *(p++); val |= (b & 0x7fU) << 14; if (!(b & 0x80)) goto done;
- b = *(p++); val |= (b & 0x7fU) << 21; if (!(b & 0x80)) goto done;
- b = *(p++); val |= (b & 0x7fU) << 28; if (!(b & 0x80)) goto done;
- b = *(p++); val |= (b & 0x7fU) << 35; if (!(b & 0x80)) goto done;
- b = *(p++); val |= (b & 0x7fU) << 42; if (!(b & 0x80)) goto done;
- b = *(p++); val |= (b & 0x7fU) << 49; if (!(b & 0x80)) goto done;
- b = *(p++); val |= (b & 0x7fU) << 56; if (!(b & 0x80)) goto done;
- b = *(p++); val |= (b & 0x7fU) << 63; if (!(b & 0x80)) goto done;
- return err;
- done:
- r.val = val;
- r.p = p;
- return r;
- }
- #line 1 "upb/json/parser.rl"
- /*
- ** upb::json::Parser (upb_json_parser)
- **
- ** A parser that uses the Ragel State Machine Compiler to generate
- ** the finite automata.
- **
- ** Ragel only natively handles regular languages, but we can manually
- ** program it a bit to handle context-free languages like JSON, by using
- ** the "fcall" and "fret" constructs.
- **
- ** This parser can handle the basics, but needs several things to be fleshed
- ** out:
- **
- ** - handling of unicode escape sequences (including high surrogate pairs).
- ** - properly check and report errors for unknown fields, stack overflow,
- ** improper array nesting (or lack of nesting).
- ** - handling of base64 sequences with padding characters.
- ** - handling of push-back (non-success returns from sink functions).
- ** - handling of keys/escape-sequences/etc that span input buffers.
- */
- #include <ctype.h>
- #include <errno.h>
- #include <float.h>
- #include <math.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #define UPB_JSON_MAX_DEPTH 64
- /* Type of value message */
- enum {
- VALUE_NULLVALUE = 0,
- VALUE_NUMBERVALUE = 1,
- VALUE_STRINGVALUE = 2,
- VALUE_BOOLVALUE = 3,
- VALUE_STRUCTVALUE = 4,
- VALUE_LISTVALUE = 5
- };
- /* Forward declare */
- static bool is_top_level(upb_json_parser *p);
- static bool is_wellknown_msg(upb_json_parser *p, upb_wellknowntype_t type);
- static bool is_wellknown_field(upb_json_parser *p, upb_wellknowntype_t type);
- static bool is_number_wrapper_object(upb_json_parser *p);
- static bool does_number_wrapper_start(upb_json_parser *p);
- static bool does_number_wrapper_end(upb_json_parser *p);
- static bool is_string_wrapper_object(upb_json_parser *p);
- static bool does_string_wrapper_start(upb_json_parser *p);
- static bool does_string_wrapper_end(upb_json_parser *p);
- static bool is_fieldmask_object(upb_json_parser *p);
- static bool does_fieldmask_start(upb_json_parser *p);
- static bool does_fieldmask_end(upb_json_parser *p);
- static void start_fieldmask_object(upb_json_parser *p);
- static void end_fieldmask_object(upb_json_parser *p);
- static void start_wrapper_object(upb_json_parser *p);
- static void end_wrapper_object(upb_json_parser *p);
- static void start_value_object(upb_json_parser *p, int value_type);
- static void end_value_object(upb_json_parser *p);
- static void start_listvalue_object(upb_json_parser *p);
- static void end_listvalue_object(upb_json_parser *p);
- static void start_structvalue_object(upb_json_parser *p);
- static void end_structvalue_object(upb_json_parser *p);
- static void start_object(upb_json_parser *p);
- static void end_object(upb_json_parser *p);
- static void start_any_object(upb_json_parser *p, const char *ptr);
- static bool end_any_object(upb_json_parser *p, const char *ptr);
- static bool start_subobject(upb_json_parser *p);
- static void end_subobject(upb_json_parser *p);
- static void start_member(upb_json_parser *p);
- static void end_member(upb_json_parser *p);
- static bool end_membername(upb_json_parser *p);
- static void start_any_member(upb_json_parser *p, const char *ptr);
- static void end_any_member(upb_json_parser *p, const char *ptr);
- static bool end_any_membername(upb_json_parser *p);
- size_t parse(void *closure, const void *hd, const char *buf, size_t size,
- const upb_bufhandle *handle);
- static bool end(void *closure, const void *hd);
- static const char eof_ch = 'e';
- /* stringsink */
- typedef struct {
- upb_byteshandler handler;
- upb_bytessink sink;
- char *ptr;
- size_t len, size;
- } upb_stringsink;
- static void *stringsink_start(void *_sink, const void *hd, size_t size_hint) {
- upb_stringsink *sink = _sink;
- sink->len = 0;
- UPB_UNUSED(hd);
- UPB_UNUSED(size_hint);
- return sink;
- }
- static size_t stringsink_string(void *_sink, const void *hd, const char *ptr,
- size_t len, const upb_bufhandle *handle) {
- upb_stringsink *sink = _sink;
- size_t new_size = sink->size;
- UPB_UNUSED(hd);
- UPB_UNUSED(handle);
- while (sink->len + len > new_size) {
- new_size *= 2;
- }
- if (new_size != sink->size) {
- sink->ptr = realloc(sink->ptr, new_size);
- sink->size = new_size;
- }
- memcpy(sink->ptr + sink->len, ptr, len);
- sink->len += len;
- return len;
- }
- void upb_stringsink_init(upb_stringsink *sink) {
- upb_byteshandler_init(&sink->handler);
- upb_byteshandler_setstartstr(&sink->handler, stringsink_start, NULL);
- upb_byteshandler_setstring(&sink->handler, stringsink_string, NULL);
- upb_bytessink_reset(&sink->sink, &sink->handler, sink);
- sink->size = 32;
- sink->ptr = malloc(sink->size);
- sink->len = 0;
- }
- void upb_stringsink_uninit(upb_stringsink *sink) { free(sink->ptr); }
- typedef struct {
- /* For encoding Any value field in binary format. */
- const upb_handlers *encoder_handlers;
- upb_pb_encoder *encoder;
- upb_stringsink stringsink;
- /* For decoding Any value field in json format. */
- upb_json_parsermethod *parser_method;
- upb_json_parser* parser;
- upb_sink sink;
- /* Mark the range of uninterpreted values in json input before type url. */
- const char *before_type_url_start;
- const char *before_type_url_end;
- /* Mark the range of uninterpreted values in json input after type url. */
- const char *after_type_url_start;
- } upb_jsonparser_any_frame;
- typedef struct {
- upb_sink sink;
- /* The current message in which we're parsing, and the field whose value we're
- * expecting next. */
- const upb_msgdef *m;
- const upb_fielddef *f;
- /* The table mapping json name to fielddef for this message. */
- upb_strtable *name_table;
- /* We are in a repeated-field context. We need this flag to decide whether to
- * handle the array as a normal repeated field or a
- * google.protobuf.ListValue/google.protobuf.Value. */
- bool is_repeated;
- /* We are in a repeated-field context, ready to emit mapentries as
- * submessages. This flag alters the start-of-object (open-brace) behavior to
- * begin a sequence of mapentry messages rather than a single submessage. */
- bool is_map;
- /* We are in a map-entry message context. This flag is set when parsing the
- * value field of a single map entry and indicates to all value-field parsers
- * (subobjects, strings, numbers, and bools) that the map-entry submessage
- * should end as soon as the value is parsed. */
- bool is_mapentry;
- /* If |is_map| or |is_mapentry| is true, |mapfield| refers to the parent
- * message's map field that we're currently parsing. This differs from |f|
- * because |f| is the field in the *current* message (i.e., the map-entry
- * message itself), not the parent's field that leads to this map. */
- const upb_fielddef *mapfield;
- /* We are in an Any message context. This flag is set when parsing the Any
- * message and indicates to all field parsers (subobjects, strings, numbers,
- * and bools) that the parsed field should be serialized as binary data or
- * cached (type url not found yet). */
- bool is_any;
- /* The type of packed message in Any. */
- upb_jsonparser_any_frame *any_frame;
- /* True if the field to be parsed is unknown. */
- bool is_unknown_field;
- } upb_jsonparser_frame;
- static void init_frame(upb_jsonparser_frame* frame) {
- frame->m = NULL;
- frame->f = NULL;
- frame->name_table = NULL;
- frame->is_repeated = false;
- frame->is_map = false;
- frame->is_mapentry = false;
- frame->mapfield = NULL;
- frame->is_any = false;
- frame->any_frame = NULL;
- frame->is_unknown_field = false;
- }
- struct upb_json_parser {
- upb_env *env;
- const upb_json_parsermethod *method;
- upb_bytessink input_;
- /* Stack to track the JSON scopes we are in. */
- upb_jsonparser_frame stack[UPB_JSON_MAX_DEPTH];
- upb_jsonparser_frame *top;
- upb_jsonparser_frame *limit;
- upb_status status;
- /* Ragel's internal parsing stack for the parsing state machine. */
- int current_state;
- int parser_stack[UPB_JSON_MAX_DEPTH];
- int parser_top;
- /* The handle for the current buffer. */
- const upb_bufhandle *handle;
- /* Accumulate buffer. See details in parser.rl. */
- const char *accumulated;
- size_t accumulated_len;
- char *accumulate_buf;
- size_t accumulate_buf_size;
- /* Multi-part text data. See details in parser.rl. */
- int multipart_state;
- upb_selector_t string_selector;
- /* Input capture. See details in parser.rl. */
- const char *capture;
- /* Intermediate result of parsing a unicode escape sequence. */
- uint32_t digit;
- /* For resolve type url in Any. */
- const upb_symtab *symtab;
- /* Whether to proceed if unknown field is met. */
- bool ignore_json_unknown;
- /* Cache for parsing timestamp due to base and zone are handled in different
- * handlers. */
- struct tm tm;
- };
- static upb_jsonparser_frame* start_jsonparser_frame(upb_json_parser *p) {
- upb_jsonparser_frame *inner;
- inner = p->top + 1;
- init_frame(inner);
- return inner;
- }
- struct upb_json_parsermethod {
- upb_refcounted base;
- upb_byteshandler input_handler_;
- /* Mainly for the purposes of refcounting, so all the fielddefs we point
- * to stay alive. */
- const upb_msgdef *msg;
- /* Keys are upb_msgdef*, values are upb_strtable (json_name -> fielddef) */
- upb_inttable name_tables;
- };
- #define PARSER_CHECK_RETURN(x) if (!(x)) return false
- static void json_parser_any_frame_reset(upb_jsonparser_any_frame *frame) {
- frame->encoder_handlers = NULL;
- frame->encoder = NULL;
- frame->parser_method = NULL;
- frame->parser = NULL;
- frame->before_type_url_start = NULL;
- frame->before_type_url_end = NULL;
- frame->after_type_url_start = NULL;
- }
- static void json_parser_any_frame_set_payload_type(
- upb_json_parser *p,
- upb_jsonparser_any_frame *frame,
- const upb_msgdef *payload_type) {
- /* Initialize encoder. */
- frame->encoder_handlers =
- upb_pb_encoder_newhandlers(payload_type, &frame->encoder_handlers);
- upb_stringsink_init(&frame->stringsink);
- frame->encoder =
- upb_pb_encoder_create(
- p->env, frame->encoder_handlers,
- &frame->stringsink.sink);
- /* Initialize parser. */
- frame->parser_method =
- upb_json_parsermethod_new(payload_type, &frame->parser_method);
- upb_sink_reset(&frame->sink, frame->encoder_handlers, frame->encoder);
- frame->parser =
- upb_json_parser_create(p->env, frame->parser_method, p->symtab,
- &frame->sink, p->ignore_json_unknown);
- }
- static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) {
- upb_handlers_unref(frame->encoder_handlers,
- &frame->encoder_handlers);
- upb_json_parsermethod_unref(frame->parser_method,
- &frame->parser_method);
- upb_stringsink_uninit(&frame->stringsink);
- }
- static bool json_parser_any_frame_has_type_url(
- upb_jsonparser_any_frame *frame) {
- return frame->encoder != NULL;
- }
- static bool json_parser_any_frame_has_value_before_type_url(
- upb_jsonparser_any_frame *frame) {
- return frame->before_type_url_start != frame->before_type_url_end;
- }
- static bool json_parser_any_frame_has_value_after_type_url(
- upb_jsonparser_any_frame *frame) {
- return frame->after_type_url_start != NULL;
- }
- static bool json_parser_any_frame_has_value(
- upb_jsonparser_any_frame *frame) {
- return json_parser_any_frame_has_value_before_type_url(frame) ||
- json_parser_any_frame_has_value_after_type_url(frame);
- }
- static void json_parser_any_frame_set_before_type_url_end(
- upb_jsonparser_any_frame *frame,
- const char *ptr) {
- if (frame->encoder == NULL) {
- frame->before_type_url_end = ptr;
- }
- }
- static void json_parser_any_frame_set_after_type_url_start_once(
- upb_jsonparser_any_frame *frame,
- const char *ptr) {
- if (json_parser_any_frame_has_type_url(frame) &&
- frame->after_type_url_start == NULL) {
- frame->after_type_url_start = ptr;
- }
- }
- /* Used to signal that a capture has been suspended. */
- static char suspend_capture;
- static upb_selector_t getsel_for_handlertype(upb_json_parser *p,
- upb_handlertype_t type) {
- upb_selector_t sel;
- bool ok = upb_handlers_getselector(p->top->f, type, &sel);
- UPB_ASSERT(ok);
- return sel;
- }
- static upb_selector_t parser_getsel(upb_json_parser *p) {
- return getsel_for_handlertype(
- p, upb_handlers_getprimitivehandlertype(p->top->f));
- }
- static bool check_stack(upb_json_parser *p) {
- if ((p->top + 1) == p->limit) {
- upb_status_seterrmsg(&p->status, "Nesting too deep");
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- return true;
- }
- static void set_name_table(upb_json_parser *p, upb_jsonparser_frame *frame) {
- upb_value v;
- bool ok = upb_inttable_lookupptr(&p->method->name_tables, frame->m, &v);
- UPB_ASSERT(ok);
- frame->name_table = upb_value_getptr(v);
- }
- /* There are GCC/Clang built-ins for overflow checking which we could start
- * using if there was any performance benefit to it. */
- static bool checked_add(size_t a, size_t b, size_t *c) {
- if (SIZE_MAX - a < b) return false;
- *c = a + b;
- return true;
- }
- static size_t saturating_multiply(size_t a, size_t b) {
- /* size_t is unsigned, so this is defined behavior even on overflow. */
- size_t ret = a * b;
- if (b != 0 && ret / b != a) {
- ret = SIZE_MAX;
- }
- return ret;
- }
- /* Base64 decoding ************************************************************/
- /* TODO(haberman): make this streaming. */
- static const signed char b64table[] = {
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, 62/*+*/, -1, -1, -1, 63/*/ */,
- 52/*0*/, 53/*1*/, 54/*2*/, 55/*3*/, 56/*4*/, 57/*5*/, 58/*6*/, 59/*7*/,
- 60/*8*/, 61/*9*/, -1, -1, -1, -1, -1, -1,
- -1, 0/*A*/, 1/*B*/, 2/*C*/, 3/*D*/, 4/*E*/, 5/*F*/, 6/*G*/,
- 07/*H*/, 8/*I*/, 9/*J*/, 10/*K*/, 11/*L*/, 12/*M*/, 13/*N*/, 14/*O*/,
- 15/*P*/, 16/*Q*/, 17/*R*/, 18/*S*/, 19/*T*/, 20/*U*/, 21/*V*/, 22/*W*/,
- 23/*X*/, 24/*Y*/, 25/*Z*/, -1, -1, -1, -1, -1,
- -1, 26/*a*/, 27/*b*/, 28/*c*/, 29/*d*/, 30/*e*/, 31/*f*/, 32/*g*/,
- 33/*h*/, 34/*i*/, 35/*j*/, 36/*k*/, 37/*l*/, 38/*m*/, 39/*n*/, 40/*o*/,
- 41/*p*/, 42/*q*/, 43/*r*/, 44/*s*/, 45/*t*/, 46/*u*/, 47/*v*/, 48/*w*/,
- 49/*x*/, 50/*y*/, 51/*z*/, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1
- };
- /* Returns the table value sign-extended to 32 bits. Knowing that the upper
- * bits will be 1 for unrecognized characters makes it easier to check for
- * this error condition later (see below). */
- int32_t b64lookup(unsigned char ch) { return b64table[ch]; }
- /* Returns true if the given character is not a valid base64 character or
- * padding. */
- bool nonbase64(unsigned char ch) { return b64lookup(ch) == -1 && ch != '='; }
- static bool base64_push(upb_json_parser *p, upb_selector_t sel, const char *ptr,
- size_t len) {
- const char *limit = ptr + len;
- for (; ptr < limit; ptr += 4) {
- uint32_t val;
- char output[3];
- if (limit - ptr < 4) {
- upb_status_seterrf(&p->status,
- "Base64 input for bytes field not a multiple of 4: %s",
- upb_fielddef_name(p->top->f));
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- val = b64lookup(ptr[0]) << 18 |
- b64lookup(ptr[1]) << 12 |
- b64lookup(ptr[2]) << 6 |
- b64lookup(ptr[3]);
- /* Test the upper bit; returns true if any of the characters returned -1. */
- if (val & 0x80000000) {
- goto otherchar;
- }
- output[0] = val >> 16;
- output[1] = (val >> 8) & 0xff;
- output[2] = val & 0xff;
- upb_sink_putstring(&p->top->sink, sel, output, 3, NULL);
- }
- return true;
- otherchar:
- if (nonbase64(ptr[0]) || nonbase64(ptr[1]) || nonbase64(ptr[2]) ||
- nonbase64(ptr[3]) ) {
- upb_status_seterrf(&p->status,
- "Non-base64 characters in bytes field: %s",
- upb_fielddef_name(p->top->f));
- upb_env_reporterror(p->env, &p->status);
- return false;
- } if (ptr[2] == '=') {
- uint32_t val;
- char output;
- /* Last group contains only two input bytes, one output byte. */
- if (ptr[0] == '=' || ptr[1] == '=' || ptr[3] != '=') {
- goto badpadding;
- }
- val = b64lookup(ptr[0]) << 18 |
- b64lookup(ptr[1]) << 12;
- UPB_ASSERT(!(val & 0x80000000));
- output = val >> 16;
- upb_sink_putstring(&p->top->sink, sel, &output, 1, NULL);
- return true;
- } else {
- uint32_t val;
- char output[2];
- /* Last group contains only three input bytes, two output bytes. */
- if (ptr[0] == '=' || ptr[1] == '=' || ptr[2] == '=') {
- goto badpadding;
- }
- val = b64lookup(ptr[0]) << 18 |
- b64lookup(ptr[1]) << 12 |
- b64lookup(ptr[2]) << 6;
- output[0] = val >> 16;
- output[1] = (val >> 8) & 0xff;
- upb_sink_putstring(&p->top->sink, sel, output, 2, NULL);
- return true;
- }
- badpadding:
- upb_status_seterrf(&p->status,
- "Incorrect base64 padding for field: %s (%.*s)",
- upb_fielddef_name(p->top->f),
- 4, ptr);
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- /* Accumulate buffer **********************************************************/
- /* Functionality for accumulating a buffer.
- *
- * Some parts of the parser need an entire value as a contiguous string. For
- * example, to look up a member name in a hash table, or to turn a string into
- * a number, the relevant library routines need the input string to be in
- * contiguous memory, even if the value spanned two or more buffers in the
- * input. These routines handle that.
- *
- * In the common case we can just point to the input buffer to get this
- * contiguous string and avoid any actual copy. So we optimistically begin
- * this way. But there are a few cases where we must instead copy into a
- * separate buffer:
- *
- * 1. The string was not contiguous in the input (it spanned buffers).
- *
- * 2. The string included escape sequences that need to be interpreted to get
- * the true value in a contiguous buffer. */
- static void assert_accumulate_empty(upb_json_parser *p) {
- UPB_ASSERT(p->accumulated == NULL);
- UPB_ASSERT(p->accumulated_len == 0);
- }
- static void accumulate_clear(upb_json_parser *p) {
- p->accumulated = NULL;
- p->accumulated_len = 0;
- }
- /* Used internally by accumulate_append(). */
- static bool accumulate_realloc(upb_json_parser *p, size_t need) {
- void *mem;
- size_t old_size = p->accumulate_buf_size;
- size_t new_size = UPB_MAX(old_size, 128);
- while (new_size < need) {
- new_size = saturating_multiply(new_size, 2);
- }
- mem = upb_env_realloc(p->env, p->accumulate_buf, old_size, new_size);
- if (!mem) {
- upb_status_seterrmsg(&p->status, "Out of memory allocating buffer.");
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- p->accumulate_buf = mem;
- p->accumulate_buf_size = new_size;
- return true;
- }
- /* Logically appends the given data to the append buffer.
- * If "can_alias" is true, we will try to avoid actually copying, but the buffer
- * must be valid until the next accumulate_append() call (if any). */
- static bool accumulate_append(upb_json_parser *p, const char *buf, size_t len,
- bool can_alias) {
- size_t need;
- if (!p->accumulated && can_alias) {
- p->accumulated = buf;
- p->accumulated_len = len;
- return true;
- }
- if (!checked_add(p->accumulated_len, len, &need)) {
- upb_status_seterrmsg(&p->status, "Integer overflow.");
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- if (need > p->accumulate_buf_size && !accumulate_realloc(p, need)) {
- return false;
- }
- if (p->accumulated != p->accumulate_buf) {
- memcpy(p->accumulate_buf, p->accumulated, p->accumulated_len);
- p->accumulated = p->accumulate_buf;
- }
- memcpy(p->accumulate_buf + p->accumulated_len, buf, len);
- p->accumulated_len += len;
- return true;
- }
- /* Returns a pointer to the data accumulated since the last accumulate_clear()
- * call, and writes the length to *len. This with point either to the input
- * buffer or a temporary accumulate buffer. */
- static const char *accumulate_getptr(upb_json_parser *p, size_t *len) {
- UPB_ASSERT(p->accumulated);
- *len = p->accumulated_len;
- return p->accumulated;
- }
- /* Mult-part text data ********************************************************/
- /* When we have text data in the input, it can often come in multiple segments.
- * For example, there may be some raw string data followed by an escape
- * sequence. The two segments are processed with different logic. Also buffer
- * seams in the input can cause multiple segments.
- *
- * As we see segments, there are two main cases for how we want to process them:
- *
- * 1. we want to push the captured input directly to string handlers.
- *
- * 2. we need to accumulate all the parts into a contiguous buffer for further
- * processing (field name lookup, string->number conversion, etc). */
- /* This is the set of states for p->multipart_state. */
- enum {
- /* We are not currently processing multipart data. */
- MULTIPART_INACTIVE = 0,
- /* We are processing multipart data by accumulating it into a contiguous
- * buffer. */
- MULTIPART_ACCUMULATE = 1,
- /* We are processing multipart data by pushing each part directly to the
- * current string handlers. */
- MULTIPART_PUSHEAGERLY = 2
- };
- /* Start a multi-part text value where we accumulate the data for processing at
- * the end. */
- static void multipart_startaccum(upb_json_parser *p) {
- assert_accumulate_empty(p);
- UPB_ASSERT(p->multipart_state == MULTIPART_INACTIVE);
- p->multipart_state = MULTIPART_ACCUMULATE;
- }
- /* Start a multi-part text value where we immediately push text data to a string
- * value with the given selector. */
- static void multipart_start(upb_json_parser *p, upb_selector_t sel) {
- assert_accumulate_empty(p);
- UPB_ASSERT(p->multipart_state == MULTIPART_INACTIVE);
- p->multipart_state = MULTIPART_PUSHEAGERLY;
- p->string_selector = sel;
- }
- static bool multipart_text(upb_json_parser *p, const char *buf, size_t len,
- bool can_alias) {
- switch (p->multipart_state) {
- case MULTIPART_INACTIVE:
- upb_status_seterrmsg(
- &p->status, "Internal error: unexpected state MULTIPART_INACTIVE");
- upb_env_reporterror(p->env, &p->status);
- return false;
- case MULTIPART_ACCUMULATE:
- if (!accumulate_append(p, buf, len, can_alias)) {
- return false;
- }
- break;
- case MULTIPART_PUSHEAGERLY: {
- const upb_bufhandle *handle = can_alias ? p->handle : NULL;
- upb_sink_putstring(&p->top->sink, p->string_selector, buf, len, handle);
- break;
- }
- }
- return true;
- }
- /* Note: this invalidates the accumulate buffer! Call only after reading its
- * contents. */
- static void multipart_end(upb_json_parser *p) {
- UPB_ASSERT(p->multipart_state != MULTIPART_INACTIVE);
- p->multipart_state = MULTIPART_INACTIVE;
- accumulate_clear(p);
- }
- /* Input capture **************************************************************/
- /* Functionality for capturing a region of the input as text. Gracefully
- * handles the case where a buffer seam occurs in the middle of the captured
- * region. */
- static void capture_begin(upb_json_parser *p, const char *ptr) {
- UPB_ASSERT(p->multipart_state != MULTIPART_INACTIVE);
- UPB_ASSERT(p->capture == NULL);
- p->capture = ptr;
- }
- static bool capture_end(upb_json_parser *p, const char *ptr) {
- UPB_ASSERT(p->capture);
- if (multipart_text(p, p->capture, ptr - p->capture, true)) {
- p->capture = NULL;
- return true;
- } else {
- return false;
- }
- }
- /* This is called at the end of each input buffer (ie. when we have hit a
- * buffer seam). If we are in the middle of capturing the input, this
- * processes the unprocessed capture region. */
- static void capture_suspend(upb_json_parser *p, const char **ptr) {
- if (!p->capture) return;
- if (multipart_text(p, p->capture, *ptr - p->capture, false)) {
- /* We use this as a signal that we were in the middle of capturing, and
- * that capturing should resume at the beginning of the next buffer.
- *
- * We can't use *ptr here, because we have no guarantee that this pointer
- * will be valid when we resume (if the underlying memory is freed, then
- * using the pointer at all, even to compare to NULL, is likely undefined
- * behavior). */
- p->capture = &suspend_capture;
- } else {
- /* Need to back up the pointer to the beginning of the capture, since
- * we were not able to actually preserve it. */
- *ptr = p->capture;
- }
- }
- static void capture_resume(upb_json_parser *p, const char *ptr) {
- if (p->capture) {
- UPB_ASSERT(p->capture == &suspend_capture);
- p->capture = ptr;
- }
- }
- /* Callbacks from the parser **************************************************/
- /* These are the functions called directly from the parser itself.
- * We define these in the same order as their declarations in the parser. */
- static char escape_char(char in) {
- switch (in) {
- case 'r': return '\r';
- case 't': return '\t';
- case 'n': return '\n';
- case 'f': return '\f';
- case 'b': return '\b';
- case '/': return '/';
- case '"': return '"';
- case '\\': return '\\';
- default:
- UPB_ASSERT(0);
- return 'x';
- }
- }
- static bool escape(upb_json_parser *p, const char *ptr) {
- char ch = escape_char(*ptr);
- return multipart_text(p, &ch, 1, false);
- }
- static void start_hex(upb_json_parser *p) {
- p->digit = 0;
- }
- static void hexdigit(upb_json_parser *p, const char *ptr) {
- char ch = *ptr;
- p->digit <<= 4;
- if (ch >= '0' && ch <= '9') {
- p->digit += (ch - '0');
- } else if (ch >= 'a' && ch <= 'f') {
- p->digit += ((ch - 'a') + 10);
- } else {
- UPB_ASSERT(ch >= 'A' && ch <= 'F');
- p->digit += ((ch - 'A') + 10);
- }
- }
- static bool end_hex(upb_json_parser *p) {
- uint32_t codepoint = p->digit;
- /* emit the codepoint as UTF-8. */
- char utf8[3]; /* support \u0000 -- \uFFFF -- need only three bytes. */
- int length = 0;
- if (codepoint <= 0x7F) {
- utf8[0] = codepoint;
- length = 1;
- } else if (codepoint <= 0x07FF) {
- utf8[1] = (codepoint & 0x3F) | 0x80;
- codepoint >>= 6;
- utf8[0] = (codepoint & 0x1F) | 0xC0;
- length = 2;
- } else /* codepoint <= 0xFFFF */ {
- utf8[2] = (codepoint & 0x3F) | 0x80;
- codepoint >>= 6;
- utf8[1] = (codepoint & 0x3F) | 0x80;
- codepoint >>= 6;
- utf8[0] = (codepoint & 0x0F) | 0xE0;
- length = 3;
- }
- /* TODO(haberman): Handle high surrogates: if codepoint is a high surrogate
- * we have to wait for the next escape to get the full code point). */
- return multipart_text(p, utf8, length, false);
- }
- static void start_text(upb_json_parser *p, const char *ptr) {
- capture_begin(p, ptr);
- }
- static bool end_text(upb_json_parser *p, const char *ptr) {
- return capture_end(p, ptr);
- }
- static bool start_number(upb_json_parser *p, const char *ptr) {
- if (is_top_level(p)) {
- if (is_number_wrapper_object(p)) {
- start_wrapper_object(p);
- } else if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
- start_value_object(p, VALUE_NUMBERVALUE);
- } else {
- return false;
- }
- } else if (does_number_wrapper_start(p)) {
- if (!start_subobject(p)) {
- return false;
- }
- start_wrapper_object(p);
- } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) {
- if (!start_subobject(p)) {
- return false;
- }
- start_value_object(p, VALUE_NUMBERVALUE);
- }
- multipart_startaccum(p);
- capture_begin(p, ptr);
- return true;
- }
- static bool parse_number(upb_json_parser *p, bool is_quoted);
- static bool end_number_nontop(upb_json_parser *p, const char *ptr) {
- if (!capture_end(p, ptr)) {
- return false;
- }
- if (p->top->f == NULL) {
- multipart_end(p);
- return true;
- }
- return parse_number(p, false);
- }
- static bool end_number(upb_json_parser *p, const char *ptr) {
- if (!end_number_nontop(p, ptr)) {
- return false;
- }
- if (does_number_wrapper_end(p)) {
- end_wrapper_object(p);
- if (!is_top_level(p)) {
- end_subobject(p);
- }
- return true;
- }
- if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
- end_value_object(p);
- if (!is_top_level(p)) {
- end_subobject(p);
- }
- return true;
- }
- return true;
- }
- /* |buf| is NULL-terminated. |buf| itself will never include quotes;
- * |is_quoted| tells us whether this text originally appeared inside quotes. */
- static bool parse_number_from_buffer(upb_json_parser *p, const char *buf,
- bool is_quoted) {
- size_t len = strlen(buf);
- const char *bufend = buf + len;
- char *end;
- upb_fieldtype_t type = upb_fielddef_type(p->top->f);
- double val;
- double dummy;
- double inf = 1.0 / 0.0; /* C89 does not have an INFINITY macro. */
- errno = 0;
- if (len == 0 || buf[0] == ' ') {
- return false;
- }
- /* For integer types, first try parsing with integer-specific routines.
- * If these succeed, they will be more accurate for int64/uint64 than
- * strtod().
- */
- switch (type) {
- case UPB_TYPE_ENUM:
- case UPB_TYPE_INT32: {
- long val = strtol(buf, &end, 0);
- if (errno == ERANGE || end != bufend) {
- break;
- } else if (val > INT32_MAX || val < INT32_MIN) {
- return false;
- } else {
- upb_sink_putint32(&p->top->sink, parser_getsel(p), val);
- return true;
- }
- }
- case UPB_TYPE_UINT32: {
- unsigned long val = strtoul(buf, &end, 0);
- if (end != bufend) {
- break;
- } else if (val > UINT32_MAX || errno == ERANGE) {
- return false;
- } else {
- upb_sink_putuint32(&p->top->sink, parser_getsel(p), val);
- return true;
- }
- }
- /* XXX: We can't handle [u]int64 properly on 32-bit machines because
- * strto[u]ll isn't in C89. */
- case UPB_TYPE_INT64: {
- long val = strtol(buf, &end, 0);
- if (errno == ERANGE || end != bufend) {
- break;
- } else {
- upb_sink_putint64(&p->top->sink, parser_getsel(p), val);
- return true;
- }
- }
- case UPB_TYPE_UINT64: {
- unsigned long val = strtoul(p->accumulated, &end, 0);
- if (end != bufend) {
- break;
- } else if (errno == ERANGE) {
- return false;
- } else {
- upb_sink_putuint64(&p->top->sink, parser_getsel(p), val);
- return true;
- }
- }
- default:
- break;
- }
- if (type != UPB_TYPE_DOUBLE && type != UPB_TYPE_FLOAT && is_quoted) {
- /* Quoted numbers for integer types are not allowed to be in double form. */
- return false;
- }
- if (len == strlen("Infinity") && strcmp(buf, "Infinity") == 0) {
- /* C89 does not have an INFINITY macro. */
- val = inf;
- } else if (len == strlen("-Infinity") && strcmp(buf, "-Infinity") == 0) {
- val = -inf;
- } else {
- val = strtod(buf, &end);
- if (errno == ERANGE || end != bufend) {
- return false;
- }
- }
- switch (type) {
- #define CASE(capitaltype, smalltype, ctype, min, max) \
- case UPB_TYPE_ ## capitaltype: { \
- if (modf(val, &dummy) != 0 || val > max || val < min) { \
- return false; \
- } else { \
- upb_sink_put ## smalltype(&p->top->sink, parser_getsel(p), \
- (ctype)val); \
- return true; \
- } \
- break; \
- }
- case UPB_TYPE_ENUM:
- CASE(INT32, int32, int32_t, INT32_MIN, INT32_MAX);
- CASE(INT64, int64, int64_t, INT64_MIN, INT64_MAX);
- CASE(UINT32, uint32, uint32_t, 0, UINT32_MAX);
- CASE(UINT64, uint64, uint64_t, 0, UINT64_MAX);
- #undef CASE
- case UPB_TYPE_DOUBLE:
- upb_sink_putdouble(&p->top->sink, parser_getsel(p), val);
- return true;
- case UPB_TYPE_FLOAT:
- if ((val > FLT_MAX || val < -FLT_MAX) && val != inf && val != -inf) {
- return false;
- } else {
- upb_sink_putfloat(&p->top->sink, parser_getsel(p), val);
- return true;
- }
- default:
- return false;
- }
- }
- static bool parse_number(upb_json_parser *p, bool is_quoted) {
- size_t len;
- const char *buf;
- /* strtol() and friends unfortunately do not support specifying the length of
- * the input string, so we need to force a copy into a NULL-terminated buffer. */
- if (!multipart_text(p, "\0", 1, false)) {
- return false;
- }
- buf = accumulate_getptr(p, &len);
- if (parse_number_from_buffer(p, buf, is_quoted)) {
- multipart_end(p);
- return true;
- } else {
- upb_status_seterrf(&p->status, "error parsing number: %s", buf);
- upb_env_reporterror(p->env, &p->status);
- multipart_end(p);
- return false;
- }
- }
- static bool parser_putbool(upb_json_parser *p, bool val) {
- bool ok;
- if (p->top->f == NULL) {
- return true;
- }
- if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL) {
- upb_status_seterrf(&p->status,
- "Boolean value specified for non-bool field: %s",
- upb_fielddef_name(p->top->f));
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- ok = upb_sink_putbool(&p->top->sink, parser_getsel(p), val);
- UPB_ASSERT(ok);
- return true;
- }
- static bool end_bool(upb_json_parser *p, bool val) {
- if (is_top_level(p)) {
- if (is_wellknown_msg(p, UPB_WELLKNOWN_BOOLVALUE)) {
- start_wrapper_object(p);
- } else if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
- start_value_object(p, VALUE_BOOLVALUE);
- } else {
- return false;
- }
- } else if (is_wellknown_field(p, UPB_WELLKNOWN_BOOLVALUE)) {
- if (!start_subobject(p)) {
- return false;
- }
- start_wrapper_object(p);
- } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) {
- if (!start_subobject(p)) {
- return false;
- }
- start_value_object(p, VALUE_BOOLVALUE);
- }
- if (p->top->is_unknown_field) {
- return true;
- }
- if (!parser_putbool(p, val)) {
- return false;
- }
- if (is_wellknown_msg(p, UPB_WELLKNOWN_BOOLVALUE)) {
- end_wrapper_object(p);
- if (!is_top_level(p)) {
- end_subobject(p);
- }
- return true;
- }
- if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
- end_value_object(p);
- if (!is_top_level(p)) {
- end_subobject(p);
- }
- return true;
- }
- return true;
- }
- static bool end_null(upb_json_parser *p) {
- const char *zero_ptr = "0";
- if (is_top_level(p)) {
- if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
- start_value_object(p, VALUE_NULLVALUE);
- } else {
- return true;
- }
- } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) {
- if (!start_subobject(p)) {
- return false;
- }
- start_value_object(p, VALUE_NULLVALUE);
- } else {
- return true;
- }
- /* Fill null_value field. */
- multipart_startaccum(p);
- capture_begin(p, zero_ptr);
- capture_end(p, zero_ptr + 1);
- parse_number(p, false);
- end_value_object(p);
- if (!is_top_level(p)) {
- end_subobject(p);
- }
- return true;
- }
- static bool start_any_stringval(upb_json_parser *p) {
- multipart_startaccum(p);
- return true;
- }
- static bool start_stringval(upb_json_parser *p) {
- if (is_top_level(p)) {
- if (is_string_wrapper_object(p)) {
- start_wrapper_object(p);
- } else if (is_wellknown_msg(p, UPB_WELLKNOWN_FIELDMASK)) {
- start_fieldmask_object(p);
- return true;
- } else if (is_wellknown_msg(p, UPB_WELLKNOWN_TIMESTAMP) ||
- is_wellknown_msg(p, UPB_WELLKNOWN_DURATION)) {
- start_object(p);
- } else if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
- start_value_object(p, VALUE_STRINGVALUE);
- } else {
- return false;
- }
- } else if (does_string_wrapper_start(p)) {
- if (!start_subobject(p)) {
- return false;
- }
- start_wrapper_object(p);
- } else if (does_fieldmask_start(p)) {
- if (!start_subobject(p)) {
- return false;
- }
- start_fieldmask_object(p);
- return true;
- } else if (is_wellknown_field(p, UPB_WELLKNOWN_TIMESTAMP) ||
- is_wellknown_field(p, UPB_WELLKNOWN_DURATION)) {
- if (!start_subobject(p)) {
- return false;
- }
- start_object(p);
- } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) {
- if (!start_subobject(p)) {
- return false;
- }
- start_value_object(p, VALUE_STRINGVALUE);
- }
- if (p->top->f == NULL) {
- multipart_startaccum(p);
- return true;
- }
- if (p->top->is_any) {
- return start_any_stringval(p);
- }
- if (upb_fielddef_isstring(p->top->f)) {
- upb_jsonparser_frame *inner;
- upb_selector_t sel;
- if (!check_stack(p)) return false;
- /* Start a new parser frame: parser frames correspond one-to-one with
- * handler frames, and string events occur in a sub-frame. */
- inner = start_jsonparser_frame(p);
- sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR);
- upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink);
- inner->m = p->top->m;
- inner->f = p->top->f;
- p->top = inner;
- if (upb_fielddef_type(p->top->f) == UPB_TYPE_STRING) {
- /* For STRING fields we push data directly to the handlers as it is
- * parsed. We don't do this yet for BYTES fields, because our base64
- * decoder is not streaming.
- *
- * TODO(haberman): make base64 decoding streaming also. */
- multipart_start(p, getsel_for_handlertype(p, UPB_HANDLER_STRING));
- return true;
- } else {
- multipart_startaccum(p);
- return true;
- }
- } else if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL &&
- upb_fielddef_type(p->top->f) != UPB_TYPE_MESSAGE) {
- /* No need to push a frame -- numeric values in quotes remain in the
- * current parser frame. These values must accmulate so we can convert
- * them all at once at the end. */
- multipart_startaccum(p);
- return true;
- } else {
- upb_status_seterrf(&p->status,
- "String specified for bool or submessage field: %s",
- upb_fielddef_name(p->top->f));
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- }
- static bool end_any_stringval(upb_json_parser *p) {
- size_t len;
- const char *buf = accumulate_getptr(p, &len);
- /* Set type_url */
- upb_selector_t sel;
- upb_jsonparser_frame *inner;
- if (!check_stack(p)) return false;
- inner = p->top + 1;
- sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR);
- upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink);
- sel = getsel_for_handlertype(p, UPB_HANDLER_STRING);
- upb_sink_putstring(&inner->sink, sel, buf, len, NULL);
- sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR);
- upb_sink_endstr(&inner->sink, sel);
- multipart_end(p);
- /* Resolve type url */
- if (strncmp(buf, "type.googleapis.com/", 20) == 0 && len > 20) {
- const upb_msgdef *payload_type = NULL;
- buf += 20;
- len -= 20;
- payload_type = upb_symtab_lookupmsg2(p->symtab, buf, len);
- if (payload_type == NULL) {
- upb_status_seterrf(
- &p->status, "Cannot find packed type: %.*s\n", (int)len, buf);
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- json_parser_any_frame_set_payload_type(p, p->top->any_frame, payload_type);
-
- return true;
- } else {
- upb_status_seterrf(
- &p->status, "Invalid type url: %.*s\n", (int)len, buf);
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- }
- static bool end_stringval_nontop(upb_json_parser *p) {
- bool ok = true;
- if (is_wellknown_msg(p, UPB_WELLKNOWN_TIMESTAMP) ||
- is_wellknown_msg(p, UPB_WELLKNOWN_DURATION)) {
- multipart_end(p);
- return true;
- }
- if (p->top->f == NULL) {
- multipart_end(p);
- return true;
- }
- if (p->top->is_any) {
- return end_any_stringval(p);
- }
- switch (upb_fielddef_type(p->top->f)) {
- case UPB_TYPE_BYTES:
- if (!base64_push(p, getsel_for_handlertype(p, UPB_HANDLER_STRING),
- p->accumulated, p->accumulated_len)) {
- return false;
- }
- /* Fall through. */
- case UPB_TYPE_STRING: {
- upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR);
- upb_sink_endstr(&p->top->sink, sel);
- p->top--;
- break;
- }
- case UPB_TYPE_ENUM: {
- /* Resolve enum symbolic name to integer value. */
- const upb_enumdef *enumdef =
- (const upb_enumdef*)upb_fielddef_subdef(p->top->f);
- size_t len;
- const char *buf = accumulate_getptr(p, &len);
- int32_t int_val = 0;
- ok = upb_enumdef_ntoi(enumdef, buf, len, &int_val);
- if (ok) {
- upb_selector_t sel = parser_getsel(p);
- upb_sink_putint32(&p->top->sink, sel, int_val);
- } else {
- upb_status_seterrf(&p->status, "Enum value unknown: '%.*s'", len, buf);
- upb_env_reporterror(p->env, &p->status);
- }
- break;
- }
- case UPB_TYPE_INT32:
- case UPB_TYPE_INT64:
- case UPB_TYPE_UINT32:
- case UPB_TYPE_UINT64:
- case UPB_TYPE_DOUBLE:
- case UPB_TYPE_FLOAT:
- ok = parse_number(p, true);
- break;
- default:
- UPB_ASSERT(false);
- upb_status_seterrmsg(&p->status, "Internal error in JSON decoder");
- upb_env_reporterror(p->env, &p->status);
- ok = false;
- break;
- }
- multipart_end(p);
- return ok;
- }
- static bool end_stringval(upb_json_parser *p) {
- /* FieldMask's stringvals have been ended when handling them. Only need to
- * close FieldMask here.*/
- if (does_fieldmask_end(p)) {
- end_fieldmask_object(p);
- if (!is_top_level(p)) {
- end_subobject(p);
- }
- return true;
- }
- if (!end_stringval_nontop(p)) {
- return false;
- }
- if (does_string_wrapper_end(p)) {
- end_wrapper_object(p);
- if (!is_top_level(p)) {
- end_subobject(p);
- }
- return true;
- }
- if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
- end_value_object(p);
- if (!is_top_level(p)) {
- end_subobject(p);
- }
- return true;
- }
- if (is_wellknown_msg(p, UPB_WELLKNOWN_TIMESTAMP) ||
- is_wellknown_msg(p, UPB_WELLKNOWN_DURATION) ||
- is_wellknown_msg(p, UPB_WELLKNOWN_FIELDMASK)) {
- end_object(p);
- if (!is_top_level(p)) {
- end_subobject(p);
- }
- return true;
- }
- return true;
- }
- static void start_duration_base(upb_json_parser *p, const char *ptr) {
- capture_begin(p, ptr);
- }
- static bool end_duration_base(upb_json_parser *p, const char *ptr) {
- size_t len;
- const char *buf;
- char seconds_buf[14];
- char nanos_buf[12];
- char *end;
- int64_t seconds = 0;
- int32_t nanos = 0;
- double val = 0.0;
- const char *seconds_membername = "seconds";
- const char *nanos_membername = "nanos";
- size_t fraction_start;
- if (!capture_end(p, ptr)) {
- return false;
- }
- buf = accumulate_getptr(p, &len);
- memset(seconds_buf, 0, 14);
- memset(nanos_buf, 0, 12);
- /* Find out base end. The maximus duration is 315576000000, which cannot be
- * represented by double without losing precision. Thus, we need to handle
- * fraction and base separately. */
- for (fraction_start = 0; fraction_start < len && buf[fraction_start] != '.';
- fraction_start++);
- /* Parse base */
- memcpy(seconds_buf, buf, fraction_start);
- seconds = strtol(seconds_buf, &end, 10);
- if (errno == ERANGE || end != seconds_buf + fraction_start) {
- upb_status_seterrf(&p->status, "error parsing duration: %s",
- seconds_buf);
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- if (seconds > 315576000000) {
- upb_status_seterrf(&p->status, "error parsing duration: "
- "maximum acceptable value is "
- "315576000000");
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- if (seconds < -315576000000) {
- upb_status_seterrf(&p->status, "error parsing duration: "
- "minimum acceptable value is "
- "-315576000000");
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- /* Parse fraction */
- nanos_buf[0] = '0';
- memcpy(nanos_buf + 1, buf + fraction_start, len - fraction_start);
- val = strtod(nanos_buf, &end);
- if (errno == ERANGE || end != nanos_buf + len - fraction_start + 1) {
- upb_status_seterrf(&p->status, "error parsing duration: %s",
- nanos_buf);
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- nanos = val * 1000000000;
- if (seconds < 0) nanos = -nanos;
- /* Clean up buffer */
- multipart_end(p);
- /* Set seconds */
- start_member(p);
- capture_begin(p, seconds_membername);
- capture_end(p, seconds_membername + 7);
- end_membername(p);
- upb_sink_putint64(&p->top->sink, parser_getsel(p), seconds);
- end_member(p);
- /* Set nanos */
- start_member(p);
- capture_begin(p, nanos_membername);
- capture_end(p, nanos_membername + 5);
- end_membername(p);
- upb_sink_putint32(&p->top->sink, parser_getsel(p), nanos);
- end_member(p);
- /* Continue previous environment */
- multipart_startaccum(p);
- return true;
- }
- static int parse_timestamp_number(upb_json_parser *p) {
- size_t len;
- const char *buf;
- char *end;
- int val;
- /* atoi() and friends unfortunately do not support specifying the length of
- * the input string, so we need to force a copy into a NULL-terminated buffer. */
- multipart_text(p, "\0", 1, false);
- buf = accumulate_getptr(p, &len);
- val = atoi(buf);
- multipart_end(p);
- multipart_startaccum(p);
- return val;
- }
- static void start_year(upb_json_parser *p, const char *ptr) {
- capture_begin(p, ptr);
- }
- static bool end_year(upb_json_parser *p, const char *ptr) {
- if (!capture_end(p, ptr)) {
- return false;
- }
- p->tm.tm_year = parse_timestamp_number(p) - 1900;
- return true;
- }
- static void start_month(upb_json_parser *p, const char *ptr) {
- capture_begin(p, ptr);
- }
- static bool end_month(upb_json_parser *p, const char *ptr) {
- if (!capture_end(p, ptr)) {
- return false;
- }
- p->tm.tm_mon = parse_timestamp_number(p) - 1;
- return true;
- }
- static void start_day(upb_json_parser *p, const char *ptr) {
- capture_begin(p, ptr);
- }
- static bool end_day(upb_json_parser *p, const char *ptr) {
- if (!capture_end(p, ptr)) {
- return false;
- }
- p->tm.tm_mday = parse_timestamp_number(p);
- return true;
- }
- static void start_hour(upb_json_parser *p, const char *ptr) {
- capture_begin(p, ptr);
- }
- static bool end_hour(upb_json_parser *p, const char *ptr) {
- if (!capture_end(p, ptr)) {
- return false;
- }
- p->tm.tm_hour = parse_timestamp_number(p);
- return true;
- }
- static void start_minute(upb_json_parser *p, const char *ptr) {
- capture_begin(p, ptr);
- }
- static bool end_minute(upb_json_parser *p, const char *ptr) {
- if (!capture_end(p, ptr)) {
- return false;
- }
- p->tm.tm_min = parse_timestamp_number(p);
- return true;
- }
- static void start_second(upb_json_parser *p, const char *ptr) {
- capture_begin(p, ptr);
- }
- static bool end_second(upb_json_parser *p, const char *ptr) {
- if (!capture_end(p, ptr)) {
- return false;
- }
- p->tm.tm_sec = parse_timestamp_number(p);
- return true;
- }
- static void start_timestamp_base(upb_json_parser *p) {
- memset(&p->tm, 0, sizeof(struct tm));
- }
- static void start_timestamp_fraction(upb_json_parser *p, const char *ptr) {
- capture_begin(p, ptr);
- }
- static bool end_timestamp_fraction(upb_json_parser *p, const char *ptr) {
- size_t len;
- const char *buf;
- char nanos_buf[12];
- char *end;
- double val = 0.0;
- int32_t nanos;
- const char *nanos_membername = "nanos";
- memset(nanos_buf, 0, 12);
- if (!capture_end(p, ptr)) {
- return false;
- }
- buf = accumulate_getptr(p, &len);
- if (len > 10) {
- upb_status_seterrf(&p->status,
- "error parsing timestamp: at most 9-digit fraction.");
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- /* Parse nanos */
- nanos_buf[0] = '0';
- memcpy(nanos_buf + 1, buf, len);
- val = strtod(nanos_buf, &end);
- if (errno == ERANGE || end != nanos_buf + len + 1) {
- upb_status_seterrf(&p->status, "error parsing timestamp nanos: %s",
- nanos_buf);
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- nanos = val * 1000000000;
- /* Clean up previous environment */
- multipart_end(p);
- /* Set nanos */
- start_member(p);
- capture_begin(p, nanos_membername);
- capture_end(p, nanos_membername + 5);
- end_membername(p);
- upb_sink_putint32(&p->top->sink, parser_getsel(p), nanos);
- end_member(p);
- /* Continue previous environment */
- multipart_startaccum(p);
- return true;
- }
- static void start_timestamp_zone(upb_json_parser *p, const char *ptr) {
- capture_begin(p, ptr);
- }
- #define EPOCH_YEAR 1970
- #define TM_YEAR_BASE 1900
- static bool isleap(int year) {
- return (year % 4) == 0 && (year % 100 != 0 || (year % 400) == 0);
- }
- const unsigned short int __mon_yday[2][13] = {
- /* Normal years. */
- { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
- /* Leap years. */
- { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
- };
- int64_t epoch(int year, int yday, int hour, int min, int sec) {
- int64_t years = year - EPOCH_YEAR;
- int64_t leap_days = years / 4 - years / 100 + years / 400;
- int64_t days = years * 365 + yday + leap_days;
- int64_t hours = days * 24 + hour;
- int64_t mins = hours * 60 + min;
- int64_t secs = mins * 60 + sec;
- return secs;
- }
- static int64_t upb_mktime(const struct tm *tp) {
- int sec = tp->tm_sec;
- int min = tp->tm_min;
- int hour = tp->tm_hour;
- int mday = tp->tm_mday;
- int mon = tp->tm_mon;
- int year = tp->tm_year + TM_YEAR_BASE;
- /* Calculate day of year from year, month, and day of month. */
- int mon_yday = ((__mon_yday[isleap(year)][mon]) - 1);
- int yday = mon_yday + mday;
- return epoch(year, yday, hour, min, sec);
- }
- static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) {
- size_t len;
- const char *buf;
- int hours = 0;
- int64_t seconds;
- const char *seconds_membername = "seconds";
- if (!capture_end(p, ptr)) {
- return false;
- }
- buf = accumulate_getptr(p, &len);
- if (buf[0] != 'Z') {
- if (sscanf(buf + 1, "%2d:00", &hours) != 1) {
- upb_status_seterrf(&p->status, "error parsing timestamp offset");
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- if (buf[0] == '+') {
- hours = -hours;
- }
- }
- /* Normalize tm */
- seconds = upb_mktime(&p->tm);
- seconds += 3600 * hours;
- /* Check timestamp boundary */
- if (seconds < -62135596800) {
- upb_status_seterrf(&p->status, "error parsing timestamp: "
- "minimum acceptable value is "
- "0001-01-01T00:00:00Z");
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- /* Clean up previous environment */
- multipart_end(p);
- /* Set seconds */
- start_member(p);
- capture_begin(p, seconds_membername);
- capture_end(p, seconds_membername + 7);
- end_membername(p);
- upb_sink_putint64(&p->top->sink, parser_getsel(p), seconds);
- end_member(p);
- /* Continue previous environment */
- multipart_startaccum(p);
- return true;
- }
- static void start_fieldmask_path_text(upb_json_parser *p, const char *ptr) {
- capture_begin(p, ptr);
- }
- static bool end_fieldmask_path_text(upb_json_parser *p, const char *ptr) {
- if (!capture_end(p, ptr)) {
- return false;
- }
- }
- static bool start_fieldmask_path(upb_json_parser *p) {
- upb_jsonparser_frame *inner;
- upb_selector_t sel;
- if (!check_stack(p)) return false;
- /* Start a new parser frame: parser frames correspond one-to-one with
- * handler frames, and string events occur in a sub-frame. */
- inner = start_jsonparser_frame(p);
- sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR);
- upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink);
- inner->m = p->top->m;
- inner->f = p->top->f;
- p->top = inner;
- multipart_startaccum(p);
- return true;
- }
- static bool lower_camel_push(
- upb_json_parser *p, upb_selector_t sel, const char *ptr, size_t len) {
- const char *limit = ptr + len;
- bool first = true;
- for (;ptr < limit; ptr++) {
- if (*ptr >= 'A' && *ptr <= 'Z' && !first) {
- char lower = tolower(*ptr);
- upb_sink_putstring(&p->top->sink, sel, "_", 1, NULL);
- upb_sink_putstring(&p->top->sink, sel, &lower, 1, NULL);
- } else {
- upb_sink_putstring(&p->top->sink, sel, ptr, 1, NULL);
- }
- first = false;
- }
- return true;
- }
- static bool end_fieldmask_path(upb_json_parser *p) {
- upb_selector_t sel;
- if (!lower_camel_push(
- p, getsel_for_handlertype(p, UPB_HANDLER_STRING),
- p->accumulated, p->accumulated_len)) {
- return false;
- }
- sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR);
- upb_sink_endstr(&p->top->sink, sel);
- p->top--;
- multipart_end(p);
- return true;
- }
- static void start_member(upb_json_parser *p) {
- UPB_ASSERT(!p->top->f);
- multipart_startaccum(p);
- }
- /* Helper: invoked during parse_mapentry() to emit the mapentry message's key
- * field based on the current contents of the accumulate buffer. */
- static bool parse_mapentry_key(upb_json_parser *p) {
- size_t len;
- const char *buf = accumulate_getptr(p, &len);
- /* Emit the key field. We do a bit of ad-hoc parsing here because the
- * parser state machine has already decided that this is a string field
- * name, and we are reinterpreting it as some arbitrary key type. In
- * particular, integer and bool keys are quoted, so we need to parse the
- * quoted string contents here. */
- p->top->f = upb_msgdef_itof(p->top->m, UPB_MAPENTRY_KEY);
- if (p->top->f == NULL) {
- upb_status_seterrmsg(&p->status, "mapentry message has no key");
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- switch (upb_fielddef_type(p->top->f)) {
- case UPB_TYPE_INT32:
- case UPB_TYPE_INT64:
- case UPB_TYPE_UINT32:
- case UPB_TYPE_UINT64:
- /* Invoke end_number. The accum buffer has the number's text already. */
- if (!parse_number(p, true)) {
- return false;
- }
- break;
- case UPB_TYPE_BOOL:
- if (len == 4 && !strncmp(buf, "true", 4)) {
- if (!parser_putbool(p, true)) {
- return false;
- }
- } else if (len == 5 && !strncmp(buf, "false", 5)) {
- if (!parser_putbool(p, false)) {
- return false;
- }
- } else {
- upb_status_seterrmsg(&p->status,
- "Map bool key not 'true' or 'false'");
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- multipart_end(p);
- break;
- case UPB_TYPE_STRING:
- case UPB_TYPE_BYTES: {
- upb_sink subsink;
- upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR);
- upb_sink_startstr(&p->top->sink, sel, len, &subsink);
- sel = getsel_for_handlertype(p, UPB_HANDLER_STRING);
- upb_sink_putstring(&subsink, sel, buf, len, NULL);
- sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR);
- upb_sink_endstr(&subsink, sel);
- multipart_end(p);
- break;
- }
- default:
- upb_status_seterrmsg(&p->status, "Invalid field type for map key");
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- return true;
- }
- /* Helper: emit one map entry (as a submessage in the map field sequence). This
- * is invoked from end_membername(), at the end of the map entry's key string,
- * with the map key in the accumulate buffer. It parses the key from that
- * buffer, emits the handler calls to start the mapentry submessage (setting up
- * its subframe in the process), and sets up state in the subframe so that the
- * value parser (invoked next) will emit the mapentry's value field and then
- * end the mapentry message. */
- static bool handle_mapentry(upb_json_parser *p) {
- const upb_fielddef *mapfield;
- const upb_msgdef *mapentrymsg;
- upb_jsonparser_frame *inner;
- upb_selector_t sel;
- /* Map entry: p->top->sink is the seq frame, so we need to start a frame
- * for the mapentry itself, and then set |f| in that frame so that the map
- * value field is parsed, and also set a flag to end the frame after the
- * map-entry value is parsed. */
- if (!check_stack(p)) return false;
- mapfield = p->top->mapfield;
- mapentrymsg = upb_fielddef_msgsubdef(mapfield);
- inner = start_jsonparser_frame(p);
- p->top->f = mapfield;
- sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG);
- upb_sink_startsubmsg(&p->top->sink, sel, &inner->sink);
- inner->m = mapentrymsg;
- inner->mapfield = mapfield;
- /* Don't set this to true *yet* -- we reuse parsing handlers below to push
- * the key field value to the sink, and these handlers will pop the frame
- * if they see is_mapentry (when invoked by the parser state machine, they
- * would have just seen the map-entry value, not key). */
- inner->is_mapentry = false;
- p->top = inner;
- /* send STARTMSG in submsg frame. */
- upb_sink_startmsg(&p->top->sink);
- parse_mapentry_key(p);
- /* Set up the value field to receive the map-entry value. */
- p->top->f = upb_msgdef_itof(p->top->m, UPB_MAPENTRY_VALUE);
- p->top->is_mapentry = true; /* set up to pop frame after value is parsed. */
- p->top->mapfield = mapfield;
- if (p->top->f == NULL) {
- upb_status_seterrmsg(&p->status, "mapentry message has no value");
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- return true;
- }
- static bool end_membername(upb_json_parser *p) {
- UPB_ASSERT(!p->top->f);
- if (!p->top->m) {
- p->top->is_unknown_field = true;
- multipart_end(p);
- return true;
- }
- if (p->top->is_any) {
- return end_any_membername(p);
- } else if (p->top->is_map) {
- return handle_mapentry(p);
- } else {
- size_t len;
- const char *buf = accumulate_getptr(p, &len);
- upb_value v;
- if (upb_strtable_lookup2(p->top->name_table, buf, len, &v)) {
- p->top->f = upb_value_getconstptr(v);
- multipart_end(p);
- return true;
- } else if (p->ignore_json_unknown) {
- p->top->is_unknown_field = true;
- multipart_end(p);
- return true;
- } else {
- upb_status_seterrf(&p->status, "No such field: %.*s\n", (int)len, buf);
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- }
- }
- static bool end_any_membername(upb_json_parser *p) {
- size_t len;
- const char *buf = accumulate_getptr(p, &len);
- upb_value v;
- if (len == 5 && strncmp(buf, "@type", len) == 0) {
- upb_strtable_lookup2(p->top->name_table, "type_url", 8, &v);
- p->top->f = upb_value_getconstptr(v);
- multipart_end(p);
- return true;
- } else {
- p->top->is_unknown_field = true;
- multipart_end(p);
- return true;
- }
- }
- static void end_member(upb_json_parser *p) {
- /* If we just parsed a map-entry value, end that frame too. */
- if (p->top->is_mapentry) {
- upb_status s = UPB_STATUS_INIT;
- upb_selector_t sel;
- bool ok;
- const upb_fielddef *mapfield;
- UPB_ASSERT(p->top > p->stack);
- /* send ENDMSG on submsg. */
- upb_sink_endmsg(&p->top->sink, &s);
- mapfield = p->top->mapfield;
- /* send ENDSUBMSG in repeated-field-of-mapentries frame. */
- p->top--;
- ok = upb_handlers_getselector(mapfield, UPB_HANDLER_ENDSUBMSG, &sel);
- UPB_ASSERT(ok);
- upb_sink_endsubmsg(&p->top->sink, sel);
- }
- p->top->f = NULL;
- p->top->is_unknown_field = false;
- }
- static void start_any_member(upb_json_parser *p, const char *ptr) {
- start_member(p);
- json_parser_any_frame_set_after_type_url_start_once(p->top->any_frame, ptr);
- }
- static void end_any_member(upb_json_parser *p, const char *ptr) {
- json_parser_any_frame_set_before_type_url_end(p->top->any_frame, ptr);
- end_member(p);
- }
- static bool start_subobject(upb_json_parser *p) {
- if (p->top->is_unknown_field) {
- upb_jsonparser_frame *inner;
- if (!check_stack(p)) return false;
- p->top = start_jsonparser_frame(p);
- return true;
- }
- if (upb_fielddef_ismap(p->top->f)) {
- upb_jsonparser_frame *inner;
- upb_selector_t sel;
- /* Beginning of a map. Start a new parser frame in a repeated-field
- * context. */
- if (!check_stack(p)) return false;
- inner = start_jsonparser_frame(p);
- sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ);
- upb_sink_startseq(&p->top->sink, sel, &inner->sink);
- inner->m = upb_fielddef_msgsubdef(p->top->f);
- inner->mapfield = p->top->f;
- inner->is_map = true;
- p->top = inner;
- return true;
- } else if (upb_fielddef_issubmsg(p->top->f)) {
- upb_jsonparser_frame *inner;
- upb_selector_t sel;
- /* Beginning of a subobject. Start a new parser frame in the submsg
- * context. */
- if (!check_stack(p)) return false;
- inner = start_jsonparser_frame(p);
- sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG);
- upb_sink_startsubmsg(&p->top->sink, sel, &inner->sink);
- inner->m = upb_fielddef_msgsubdef(p->top->f);
- set_name_table(p, inner);
- p->top = inner;
- if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) {
- p->top->is_any = true;
- p->top->any_frame =
- upb_env_malloc(p->env, sizeof(upb_jsonparser_any_frame));
- json_parser_any_frame_reset(p->top->any_frame);
- } else {
- p->top->is_any = false;
- p->top->any_frame = NULL;
- }
- return true;
- } else {
- upb_status_seterrf(&p->status,
- "Object specified for non-message/group field: %s",
- upb_fielddef_name(p->top->f));
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- }
- static bool start_subobject_full(upb_json_parser *p) {
- if (is_top_level(p)) {
- if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
- start_value_object(p, VALUE_STRUCTVALUE);
- if (!start_subobject(p)) return false;
- start_structvalue_object(p);
- } else if (is_wellknown_msg(p, UPB_WELLKNOWN_STRUCT)) {
- start_structvalue_object(p);
- } else {
- return true;
- }
- } else if (is_wellknown_field(p, UPB_WELLKNOWN_STRUCT)) {
- if (!start_subobject(p)) return false;
- start_structvalue_object(p);
- } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) {
- if (!start_subobject(p)) return false;
- start_value_object(p, VALUE_STRUCTVALUE);
- if (!start_subobject(p)) return false;
- start_structvalue_object(p);
- }
- return start_subobject(p);
- }
- static void end_subobject(upb_json_parser *p) {
- if (is_top_level(p)) {
- return;
- }
- if (p->top->is_map) {
- upb_selector_t sel;
- p->top--;
- sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSEQ);
- upb_sink_endseq(&p->top->sink, sel);
- } else {
- upb_selector_t sel;
- bool is_unknown = p->top->m == NULL;
- p->top--;
- if (!is_unknown) {
- sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSUBMSG);
- upb_sink_endsubmsg(&p->top->sink, sel);
- }
- }
- }
- static void end_subobject_full(upb_json_parser *p) {
- end_subobject(p);
- if (is_wellknown_msg(p, UPB_WELLKNOWN_STRUCT)) {
- end_structvalue_object(p);
- if (!is_top_level(p)) {
- end_subobject(p);
- }
- }
- if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
- end_value_object(p);
- if (!is_top_level(p)) {
- end_subobject(p);
- }
- }
- }
- static bool start_array(upb_json_parser *p) {
- upb_jsonparser_frame *inner;
- upb_selector_t sel;
- if (is_top_level(p)) {
- if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
- start_value_object(p, VALUE_LISTVALUE);
- if (!start_subobject(p)) return false;
- start_listvalue_object(p);
- } else if (is_wellknown_msg(p, UPB_WELLKNOWN_LISTVALUE)) {
- start_listvalue_object(p);
- } else {
- return false;
- }
- } else if (is_wellknown_field(p, UPB_WELLKNOWN_LISTVALUE) &&
- (!upb_fielddef_isseq(p->top->f) ||
- p->top->is_repeated)) {
- if (!start_subobject(p)) return false;
- start_listvalue_object(p);
- } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE) &&
- (!upb_fielddef_isseq(p->top->f) ||
- p->top->is_repeated)) {
- if (!start_subobject(p)) return false;
- start_value_object(p, VALUE_LISTVALUE);
- if (!start_subobject(p)) return false;
- start_listvalue_object(p);
- }
- if (p->top->is_unknown_field) {
- inner = start_jsonparser_frame(p);
- inner->is_unknown_field = true;
- p->top = inner;
- return true;
- }
- if (!upb_fielddef_isseq(p->top->f)) {
- upb_status_seterrf(&p->status,
- "Array specified for non-repeated field: %s",
- upb_fielddef_name(p->top->f));
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- if (!check_stack(p)) return false;
- inner = start_jsonparser_frame(p);
- sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ);
- upb_sink_startseq(&p->top->sink, sel, &inner->sink);
- inner->m = p->top->m;
- inner->f = p->top->f;
- inner->is_repeated = true;
- p->top = inner;
- return true;
- }
- static void end_array(upb_json_parser *p) {
- upb_selector_t sel;
- UPB_ASSERT(p->top > p->stack);
- p->top--;
- if (p->top->is_unknown_field) {
- return;
- }
- sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSEQ);
- upb_sink_endseq(&p->top->sink, sel);
- if (is_wellknown_msg(p, UPB_WELLKNOWN_LISTVALUE)) {
- end_listvalue_object(p);
- if (!is_top_level(p)) {
- end_subobject(p);
- }
- }
- if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
- end_value_object(p);
- if (!is_top_level(p)) {
- end_subobject(p);
- }
- }
- }
- static void start_object(upb_json_parser *p) {
- if (!p->top->is_map && p->top->m != NULL) {
- upb_sink_startmsg(&p->top->sink);
- }
- }
- static void end_object(upb_json_parser *p) {
- if (!p->top->is_map && p->top->m != NULL) {
- upb_status status;
- upb_status_clear(&status);
- upb_sink_endmsg(&p->top->sink, &status);
- if (!upb_ok(&status)) {
- upb_env_reporterror(p->env, &status);
- }
- }
- }
- static void start_any_object(upb_json_parser *p, const char *ptr) {
- start_object(p);
- p->top->any_frame->before_type_url_start = ptr;
- p->top->any_frame->before_type_url_end = ptr;
- }
- static bool end_any_object(upb_json_parser *p, const char *ptr) {
- const char *value_membername = "value";
- bool is_well_known_packed = false;
- const char *packed_end = ptr + 1;
- upb_selector_t sel;
- upb_jsonparser_frame *inner;
- if (json_parser_any_frame_has_value(p->top->any_frame) &&
- !json_parser_any_frame_has_type_url(p->top->any_frame)) {
- upb_status_seterrmsg(&p->status, "No valid type url");
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- /* Well known types data is represented as value field. */
- if (upb_msgdef_wellknowntype(p->top->any_frame->parser->top->m) !=
- UPB_WELLKNOWN_UNSPECIFIED) {
- is_well_known_packed = true;
- if (json_parser_any_frame_has_value_before_type_url(p->top->any_frame)) {
- p->top->any_frame->before_type_url_start =
- memchr(p->top->any_frame->before_type_url_start, ':',
- p->top->any_frame->before_type_url_end -
- p->top->any_frame->before_type_url_start);
- if (p->top->any_frame->before_type_url_start == NULL) {
- upb_status_seterrmsg(&p->status, "invalid data for well known type.");
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- p->top->any_frame->before_type_url_start++;
- }
- if (json_parser_any_frame_has_value_after_type_url(p->top->any_frame)) {
- p->top->any_frame->after_type_url_start =
- memchr(p->top->any_frame->after_type_url_start, ':',
- (ptr + 1) -
- p->top->any_frame->after_type_url_start);
- if (p->top->any_frame->after_type_url_start == NULL) {
- upb_status_seterrmsg(&p->status, "Invalid data for well known type.");
- upb_env_reporterror(p->env, &p->status);
- return false;
- }
- p->top->any_frame->after_type_url_start++;
- packed_end = ptr;
- }
- }
- if (json_parser_any_frame_has_value_before_type_url(p->top->any_frame)) {
- if (!parse(p->top->any_frame->parser, NULL,
- p->top->any_frame->before_type_url_start,
- p->top->any_frame->before_type_url_end -
- p->top->any_frame->before_type_url_start, NULL)) {
- return false;
- }
- } else {
- if (!is_well_known_packed) {
- if (!parse(p->top->any_frame->parser, NULL, "{", 1, NULL)) {
- return false;
- }
- }
- }
- if (json_parser_any_frame_has_value_before_type_url(p->top->any_frame) &&
- json_parser_any_frame_has_value_after_type_url(p->top->any_frame)) {
- if (!parse(p->top->any_frame->parser, NULL, ",", 1, NULL)) {
- return false;
- }
- }
- if (json_parser_any_frame_has_value_after_type_url(p->top->any_frame)) {
- if (!parse(p->top->any_frame->parser, NULL,
- p->top->any_frame->after_type_url_start,
- packed_end - p->top->any_frame->after_type_url_start, NULL)) {
- return false;
- }
- } else {
- if (!is_well_known_packed) {
- if (!parse(p->top->any_frame->parser, NULL, "}", 1, NULL)) {
- return false;
- }
- }
- }
- if (!end(p->top->any_frame->parser, NULL)) {
- return false;
- }
- p->top->is_any = false;
- /* Set value */
- start_member(p);
- capture_begin(p, value_membername);
- capture_end(p, value_membername + 5);
- end_membername(p);
- if (!check_stack(p)) return false;
- inner = p->top + 1;
- sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR);
- upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink);
- sel = getsel_for_handlertype(p, UPB_HANDLER_STRING);
- upb_sink_putstring(&inner->sink, sel, p->top->any_frame->stringsink.ptr,
- p->top->any_frame->stringsink.len, NULL);
- sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR);
- upb_sink_endstr(&inner->sink, sel);
- end_member(p);
- end_object(p);
- /* Deallocate any parse frame. */
- json_parser_any_frame_free(p->top->any_frame);
- upb_env_free(p->env, p->top->any_frame);
- return true;
- }
- static bool is_string_wrapper(const upb_msgdef *m) {
- upb_wellknowntype_t type = upb_msgdef_wellknowntype(m);
- return type == UPB_WELLKNOWN_STRINGVALUE ||
- type == UPB_WELLKNOWN_BYTESVALUE;
- }
- static bool is_fieldmask(const upb_msgdef *m) {
- upb_wellknowntype_t type = upb_msgdef_wellknowntype(m);
- return type == UPB_WELLKNOWN_FIELDMASK;
- }
- static void start_fieldmask_object(upb_json_parser *p) {
- const char *membername = "paths";
- start_object(p);
- /* Set up context for parsing value */
- start_member(p);
- capture_begin(p, membername);
- capture_end(p, membername + 5);
- end_membername(p);
- start_array(p);
- }
- static void end_fieldmask_object(upb_json_parser *p) {
- end_array(p);
- end_member(p);
- end_object(p);
- }
- static void start_wrapper_object(upb_json_parser *p) {
- const char *membername = "value";
- start_object(p);
- /* Set up context for parsing value */
- start_member(p);
- capture_begin(p, membername);
- capture_end(p, membername + 5);
- end_membername(p);
- }
- static void end_wrapper_object(upb_json_parser *p) {
- end_member(p);
- end_object(p);
- }
- static void start_value_object(upb_json_parser *p, int value_type) {
- const char *nullmember = "null_value";
- const char *numbermember = "number_value";
- const char *stringmember = "string_value";
- const char *boolmember = "bool_value";
- const char *structmember = "struct_value";
- const char *listmember = "list_value";
- const char *membername = "";
- switch (value_type) {
- case VALUE_NULLVALUE:
- membername = nullmember;
- break;
- case VALUE_NUMBERVALUE:
- membername = numbermember;
- break;
- case VALUE_STRINGVALUE:
- membername = stringmember;
- break;
- case VALUE_BOOLVALUE:
- membername = boolmember;
- break;
- case VALUE_STRUCTVALUE:
- membername = structmember;
- break;
- case VALUE_LISTVALUE:
- membername = listmember;
- break;
- }
- start_object(p);
- /* Set up context for parsing value */
- start_member(p);
- capture_begin(p, membername);
- capture_end(p, membername + strlen(membername));
- end_membername(p);
- }
- static void end_value_object(upb_json_parser *p) {
- end_member(p);
- end_object(p);
- }
- static void start_listvalue_object(upb_json_parser *p) {
- const char *membername = "values";
- start_object(p);
- /* Set up context for parsing value */
- start_member(p);
- capture_begin(p, membername);
- capture_end(p, membername + strlen(membername));
- end_membername(p);
- }
- static void end_listvalue_object(upb_json_parser *p) {
- end_member(p);
- end_object(p);
- }
- static void start_structvalue_object(upb_json_parser *p) {
- const char *membername = "fields";
- start_object(p);
- /* Set up context for parsing value */
- start_member(p);
- capture_begin(p, membername);
- capture_end(p, membername + strlen(membername));
- end_membername(p);
- }
- static void end_structvalue_object(upb_json_parser *p) {
- end_member(p);
- end_object(p);
- }
- static bool is_top_level(upb_json_parser *p) {
- return p->top == p->stack && p->top->f == NULL && !p->top->is_unknown_field;
- }
- static bool is_wellknown_msg(upb_json_parser *p, upb_wellknowntype_t type) {
- return p->top->m != NULL && upb_msgdef_wellknowntype(p->top->m) == type;
- }
- static bool is_wellknown_field(upb_json_parser *p, upb_wellknowntype_t type) {
- return p->top->f != NULL &&
- upb_fielddef_issubmsg(p->top->f) &&
- (upb_msgdef_wellknowntype(upb_fielddef_msgsubdef(p->top->f))
- == type);
- }
- static bool does_number_wrapper_start(upb_json_parser *p) {
- return p->top->f != NULL &&
- upb_fielddef_issubmsg(p->top->f) &&
- upb_msgdef_isnumberwrapper(upb_fielddef_msgsubdef(p->top->f));
- }
- static bool does_number_wrapper_end(upb_json_parser *p) {
- return p->top->m != NULL && upb_msgdef_isnumberwrapper(p->top->m);
- }
- static bool is_number_wrapper_object(upb_json_parser *p) {
- return p->top->m != NULL && upb_msgdef_isnumberwrapper(p->top->m);
- }
- static bool does_string_wrapper_start(upb_json_parser *p) {
- return p->top->f != NULL &&
- upb_fielddef_issubmsg(p->top->f) &&
- is_string_wrapper(upb_fielddef_msgsubdef(p->top->f));
- }
- static bool does_string_wrapper_end(upb_json_parser *p) {
- return p->top->m != NULL && is_string_wrapper(p->top->m);
- }
- static bool is_string_wrapper_object(upb_json_parser *p) {
- return p->top->m != NULL && is_string_wrapper(p->top->m);
- }
- static bool does_fieldmask_start(upb_json_parser *p) {
- return p->top->f != NULL &&
- upb_fielddef_issubmsg(p->top->f) &&
- is_fieldmask(upb_fielddef_msgsubdef(p->top->f));
- }
- static bool does_fieldmask_end(upb_json_parser *p) {
- return p->top->m != NULL && is_fieldmask(p->top->m);
- }
- static bool is_fieldmask_object(upb_json_parser *p) {
- return p->top->m != NULL && is_fieldmask(p->top->m);
- }
- #define CHECK_RETURN_TOP(x) if (!(x)) goto error
- /* The actual parser **********************************************************/
- /* What follows is the Ragel parser itself. The language is specified in Ragel
- * and the actions call our C functions above.
- *
- * Ragel has an extensive set of functionality, and we use only a small part of
- * it. There are many action types but we only use a few:
- *
- * ">" -- transition into a machine
- * "%" -- transition out of a machine
- * "@" -- transition into a final state of a machine.
- *
- * "@" transitions are tricky because a machine can transition into a final
- * state repeatedly. But in some cases we know this can't happen, for example
- * a string which is delimited by a final '"' can only transition into its
- * final state once, when the closing '"' is seen. */
- #line 2789 "upb/json/parser.rl"
- #line 2592 "upb/json/parser.c"
- static const char _json_actions[] = {
- 0, 1, 0, 1, 1, 1, 3, 1,
- 4, 1, 6, 1, 7, 1, 8, 1,
- 9, 1, 11, 1, 12, 1, 13, 1,
- 14, 1, 15, 1, 16, 1, 17, 1,
- 18, 1, 19, 1, 20, 1, 22, 1,
- 23, 1, 24, 1, 35, 1, 37, 1,
- 39, 1, 40, 1, 42, 1, 43, 1,
- 44, 1, 46, 1, 48, 1, 49, 1,
- 50, 1, 51, 1, 53, 1, 54, 2,
- 4, 9, 2, 5, 6, 2, 7, 3,
- 2, 7, 9, 2, 21, 26, 2, 25,
- 10, 2, 27, 28, 2, 29, 30, 2,
- 32, 34, 2, 33, 31, 2, 38, 36,
- 2, 40, 42, 2, 45, 2, 2, 46,
- 54, 2, 47, 36, 2, 49, 54, 2,
- 50, 54, 2, 51, 54, 2, 52, 41,
- 2, 53, 54, 3, 32, 34, 35, 4,
- 21, 26, 27, 28
- };
- static const short _json_key_offsets[] = {
- 0, 0, 12, 13, 18, 23, 28, 29,
- 30, 31, 32, 33, 34, 35, 36, 37,
- 38, 43, 44, 48, 53, 58, 63, 67,
- 71, 74, 77, 79, 83, 87, 89, 91,
- 96, 98, 100, 109, 115, 121, 127, 133,
- 135, 139, 142, 144, 146, 149, 150, 154,
- 156, 158, 160, 162, 163, 165, 167, 168,
- 170, 172, 173, 175, 177, 178, 180, 182,
- 183, 185, 187, 191, 193, 195, 196, 197,
- 198, 199, 201, 206, 208, 210, 212, 221,
- 222, 222, 222, 227, 232, 237, 238, 239,
- 240, 241, 241, 242, 243, 244, 244, 245,
- 246, 247, 247, 252, 253, 257, 262, 267,
- 272, 276, 276, 279, 282, 285, 288, 291,
- 294, 294, 294, 294, 294, 294
- };
- static const char _json_trans_keys[] = {
- 32, 34, 45, 91, 102, 110, 116, 123,
- 9, 13, 48, 57, 34, 32, 93, 125,
- 9, 13, 32, 44, 93, 9, 13, 32,
- 93, 125, 9, 13, 97, 108, 115, 101,
- 117, 108, 108, 114, 117, 101, 32, 34,
- 125, 9, 13, 34, 32, 58, 9, 13,
- 32, 93, 125, 9, 13, 32, 44, 125,
- 9, 13, 32, 44, 125, 9, 13, 32,
- 34, 9, 13, 45, 48, 49, 57, 48,
- 49, 57, 46, 69, 101, 48, 57, 69,
- 101, 48, 57, 43, 45, 48, 57, 48,
- 57, 48, 57, 46, 69, 101, 48, 57,
- 34, 92, 34, 92, 34, 47, 92, 98,
- 102, 110, 114, 116, 117, 48, 57, 65,
- 70, 97, 102, 48, 57, 65, 70, 97,
- 102, 48, 57, 65, 70, 97, 102, 48,
- 57, 65, 70, 97, 102, 34, 92, 45,
- 48, 49, 57, 48, 49, 57, 46, 115,
- 48, 57, 115, 48, 57, 34, 46, 115,
- 48, 57, 48, 57, 48, 57, 48, 57,
- 48, 57, 45, 48, 57, 48, 57, 45,
- 48, 57, 48, 57, 84, 48, 57, 48,
- 57, 58, 48, 57, 48, 57, 58, 48,
- 57, 48, 57, 43, 45, 46, 90, 48,
- 57, 48, 57, 58, 48, 48, 34, 48,
- 57, 43, 45, 90, 48, 57, 34, 44,
- 34, 44, 34, 44, 34, 45, 91, 102,
- 110, 116, 123, 48, 57, 34, 32, 93,
- 125, 9, 13, 32, 44, 93, 9, 13,
- 32, 93, 125, 9, 13, 97, 108, 115,
- 101, 117, 108, 108, 114, 117, 101, 32,
- 34, 125, 9, 13, 34, 32, 58, 9,
- 13, 32, 93, 125, 9, 13, 32, 44,
- 125, 9, 13, 32, 44, 125, 9, 13,
- 32, 34, 9, 13, 32, 9, 13, 32,
- 9, 13, 32, 9, 13, 32, 9, 13,
- 32, 9, 13, 32, 9, 13, 0
- };
- static const char _json_single_lengths[] = {
- 0, 8, 1, 3, 3, 3, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1,
- 3, 1, 2, 3, 3, 3, 2, 2,
- 1, 3, 0, 2, 2, 0, 0, 3,
- 2, 2, 9, 0, 0, 0, 0, 2,
- 2, 1, 2, 0, 1, 1, 2, 0,
- 0, 0, 0, 1, 0, 0, 1, 0,
- 0, 1, 0, 0, 1, 0, 0, 1,
- 0, 0, 4, 0, 0, 1, 1, 1,
- 1, 0, 3, 2, 2, 2, 7, 1,
- 0, 0, 3, 3, 3, 1, 1, 1,
- 1, 0, 1, 1, 1, 0, 1, 1,
- 1, 0, 3, 1, 2, 3, 3, 3,
- 2, 0, 1, 1, 1, 1, 1, 1,
- 0, 0, 0, 0, 0, 0
- };
- static const char _json_range_lengths[] = {
- 0, 2, 0, 1, 1, 1, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 1, 0, 1, 1, 1, 1, 1, 1,
- 1, 0, 1, 1, 1, 1, 1, 1,
- 0, 0, 0, 3, 3, 3, 3, 0,
- 1, 1, 0, 1, 1, 0, 1, 1,
- 1, 1, 1, 0, 1, 1, 0, 1,
- 1, 0, 1, 1, 0, 1, 1, 0,
- 1, 1, 0, 1, 1, 0, 0, 0,
- 0, 1, 1, 0, 0, 0, 1, 0,
- 0, 0, 1, 1, 1, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 1, 0, 1, 1, 1, 1,
- 1, 0, 1, 1, 1, 1, 1, 1,
- 0, 0, 0, 0, 0, 0
- };
- static const short _json_index_offsets[] = {
- 0, 0, 11, 13, 18, 23, 28, 30,
- 32, 34, 36, 38, 40, 42, 44, 46,
- 48, 53, 55, 59, 64, 69, 74, 78,
- 82, 85, 89, 91, 95, 99, 101, 103,
- 108, 111, 114, 124, 128, 132, 136, 140,
- 143, 147, 150, 153, 155, 158, 160, 164,
- 166, 168, 170, 172, 174, 176, 178, 180,
- 182, 184, 186, 188, 190, 192, 194, 196,
- 198, 200, 202, 207, 209, 211, 213, 215,
- 217, 219, 221, 226, 229, 232, 235, 244,
- 246, 247, 248, 253, 258, 263, 265, 267,
- 269, 271, 272, 274, 276, 278, 279, 281,
- 283, 285, 286, 291, 293, 297, 302, 307,
- 312, 316, 317, 320, 323, 326, 329, 332,
- 335, 336, 337, 338, 339, 340
- };
- static const unsigned char _json_indicies[] = {
- 0, 2, 3, 4, 5, 6, 7, 8,
- 0, 3, 1, 9, 1, 11, 12, 1,
- 11, 10, 13, 14, 12, 13, 1, 14,
- 1, 1, 14, 10, 15, 1, 16, 1,
- 17, 1, 18, 1, 19, 1, 20, 1,
- 21, 1, 22, 1, 23, 1, 24, 1,
- 25, 26, 27, 25, 1, 28, 1, 29,
- 30, 29, 1, 30, 1, 1, 30, 31,
- 32, 33, 34, 32, 1, 35, 36, 27,
- 35, 1, 36, 26, 36, 1, 37, 38,
- 39, 1, 38, 39, 1, 41, 42, 42,
- 40, 43, 1, 42, 42, 43, 40, 44,
- 44, 45, 1, 45, 1, 45, 40, 41,
- 42, 42, 39, 40, 47, 48, 46, 50,
- 51, 49, 52, 52, 52, 52, 52, 52,
- 52, 52, 53, 1, 54, 54, 54, 1,
- 55, 55, 55, 1, 56, 56, 56, 1,
- 57, 57, 57, 1, 59, 60, 58, 61,
- 62, 63, 1, 64, 65, 1, 66, 67,
- 1, 68, 1, 67, 68, 1, 69, 1,
- 66, 67, 65, 1, 70, 1, 71, 1,
- 72, 1, 73, 1, 74, 1, 75, 1,
- 76, 1, 77, 1, 78, 1, 79, 1,
- 80, 1, 81, 1, 82, 1, 83, 1,
- 84, 1, 85, 1, 86, 1, 87, 1,
- 88, 1, 89, 89, 90, 91, 1, 92,
- 1, 93, 1, 94, 1, 95, 1, 96,
- 1, 97, 1, 98, 1, 99, 99, 100,
- 98, 1, 102, 1, 101, 104, 105, 103,
- 1, 1, 101, 106, 107, 108, 109, 110,
- 111, 112, 107, 1, 113, 1, 114, 115,
- 117, 118, 1, 117, 116, 119, 120, 118,
- 119, 1, 120, 1, 1, 120, 116, 121,
- 1, 122, 1, 123, 1, 124, 1, 125,
- 126, 1, 127, 1, 128, 1, 129, 130,
- 1, 131, 1, 132, 1, 133, 134, 135,
- 136, 134, 1, 137, 1, 138, 139, 138,
- 1, 139, 1, 1, 139, 140, 141, 142,
- 143, 141, 1, 144, 145, 136, 144, 1,
- 145, 135, 145, 1, 146, 147, 147, 1,
- 148, 148, 1, 149, 149, 1, 150, 150,
- 1, 151, 151, 1, 152, 152, 1, 1,
- 1, 1, 1, 1, 1, 0
- };
- static const char _json_trans_targs[] = {
- 1, 0, 2, 107, 3, 6, 10, 13,
- 16, 106, 4, 3, 106, 4, 5, 7,
- 8, 9, 108, 11, 12, 109, 14, 15,
- 110, 16, 17, 111, 18, 18, 19, 20,
- 21, 22, 111, 21, 22, 24, 25, 31,
- 112, 26, 28, 27, 29, 30, 33, 113,
- 34, 33, 113, 34, 32, 35, 36, 37,
- 38, 39, 33, 113, 34, 41, 42, 46,
- 42, 46, 43, 45, 44, 114, 48, 49,
- 50, 51, 52, 53, 54, 55, 56, 57,
- 58, 59, 60, 61, 62, 63, 64, 65,
- 66, 67, 73, 72, 68, 69, 70, 71,
- 72, 115, 74, 67, 72, 76, 116, 76,
- 116, 77, 79, 81, 82, 85, 90, 94,
- 98, 80, 117, 117, 83, 82, 80, 83,
- 84, 86, 87, 88, 89, 117, 91, 92,
- 93, 117, 95, 96, 97, 117, 98, 99,
- 105, 100, 100, 101, 102, 103, 104, 105,
- 103, 104, 117, 106, 106, 106, 106, 106,
- 106
- };
- static const unsigned char _json_trans_actions[] = {
- 0, 0, 113, 107, 53, 0, 0, 0,
- 125, 59, 45, 0, 55, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 101, 51, 47, 0, 0, 45,
- 49, 49, 104, 0, 0, 0, 0, 0,
- 3, 0, 0, 0, 0, 0, 5, 15,
- 0, 0, 71, 7, 13, 0, 74, 9,
- 9, 9, 77, 80, 11, 37, 37, 37,
- 0, 0, 0, 39, 0, 41, 86, 0,
- 0, 0, 17, 19, 0, 21, 23, 0,
- 25, 27, 0, 29, 31, 0, 33, 35,
- 0, 135, 83, 135, 0, 0, 0, 0,
- 0, 92, 0, 89, 89, 98, 43, 0,
- 131, 95, 113, 107, 53, 0, 0, 0,
- 125, 59, 69, 110, 45, 0, 55, 0,
- 0, 0, 0, 0, 0, 119, 0, 0,
- 0, 122, 0, 0, 0, 116, 0, 101,
- 51, 47, 0, 0, 45, 49, 49, 104,
- 0, 0, 128, 0, 57, 63, 65, 61,
- 67
- };
- static const unsigned char _json_eof_actions[] = {
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 1, 0, 1, 0, 0, 1, 1,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 57, 63, 65, 61, 67,
- 0, 0, 0, 0, 0, 0
- };
- static const int json_start = 1;
- static const int json_en_number_machine = 23;
- static const int json_en_string_machine = 32;
- static const int json_en_duration_machine = 40;
- static const int json_en_timestamp_machine = 47;
- static const int json_en_fieldmask_machine = 75;
- static const int json_en_value_machine = 78;
- static const int json_en_main = 1;
- #line 2792 "upb/json/parser.rl"
- size_t parse(void *closure, const void *hd, const char *buf, size_t size,
- const upb_bufhandle *handle) {
- upb_json_parser *parser = closure;
- /* Variables used by Ragel's generated code. */
- int cs = parser->current_state;
- int *stack = parser->parser_stack;
- int top = parser->parser_top;
- const char *p = buf;
- const char *pe = buf + size;
- const char *eof = &eof_ch;
- parser->handle = handle;
- UPB_UNUSED(hd);
- UPB_UNUSED(handle);
- capture_resume(parser, buf);
-
- #line 2870 "upb/json/parser.c"
- {
- int _klen;
- unsigned int _trans;
- const char *_acts;
- unsigned int _nacts;
- const char *_keys;
- if ( p == pe )
- goto _test_eof;
- if ( cs == 0 )
- goto _out;
- _resume:
- _keys = _json_trans_keys + _json_key_offsets[cs];
- _trans = _json_index_offsets[cs];
- _klen = _json_single_lengths[cs];
- if ( _klen > 0 ) {
- const char *_lower = _keys;
- const char *_mid;
- const char *_upper = _keys + _klen - 1;
- while (1) {
- if ( _upper < _lower )
- break;
- _mid = _lower + ((_upper-_lower) >> 1);
- if ( (*p) < *_mid )
- _upper = _mid - 1;
- else if ( (*p) > *_mid )
- _lower = _mid + 1;
- else {
- _trans += (unsigned int)(_mid - _keys);
- goto _match;
- }
- }
- _keys += _klen;
- _trans += _klen;
- }
- _klen = _json_range_lengths[cs];
- if ( _klen > 0 ) {
- const char *_lower = _keys;
- const char *_mid;
- const char *_upper = _keys + (_klen<<1) - 2;
- while (1) {
- if ( _upper < _lower )
- break;
- _mid = _lower + (((_upper-_lower) >> 1) & ~1);
- if ( (*p) < _mid[0] )
- _upper = _mid - 2;
- else if ( (*p) > _mid[1] )
- _lower = _mid + 2;
- else {
- _trans += (unsigned int)((_mid - _keys)>>1);
- goto _match;
- }
- }
- _trans += _klen;
- }
- _match:
- _trans = _json_indicies[_trans];
- cs = _json_trans_targs[_trans];
- if ( _json_trans_actions[_trans] == 0 )
- goto _again;
- _acts = _json_actions + _json_trans_actions[_trans];
- _nacts = (unsigned int) *_acts++;
- while ( _nacts-- > 0 )
- {
- switch ( *_acts++ )
- {
- case 1:
- #line 2597 "upb/json/parser.rl"
- { p--; {cs = stack[--top]; goto _again;} }
- break;
- case 2:
- #line 2599 "upb/json/parser.rl"
- { p--; {stack[top++] = cs; cs = 23;goto _again;} }
- break;
- case 3:
- #line 2603 "upb/json/parser.rl"
- { start_text(parser, p); }
- break;
- case 4:
- #line 2604 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_text(parser, p)); }
- break;
- case 5:
- #line 2610 "upb/json/parser.rl"
- { start_hex(parser); }
- break;
- case 6:
- #line 2611 "upb/json/parser.rl"
- { hexdigit(parser, p); }
- break;
- case 7:
- #line 2612 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_hex(parser)); }
- break;
- case 8:
- #line 2618 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(escape(parser, p)); }
- break;
- case 9:
- #line 2624 "upb/json/parser.rl"
- { p--; {cs = stack[--top]; goto _again;} }
- break;
- case 10:
- #line 2629 "upb/json/parser.rl"
- { start_year(parser, p); }
- break;
- case 11:
- #line 2630 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_year(parser, p)); }
- break;
- case 12:
- #line 2634 "upb/json/parser.rl"
- { start_month(parser, p); }
- break;
- case 13:
- #line 2635 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_month(parser, p)); }
- break;
- case 14:
- #line 2639 "upb/json/parser.rl"
- { start_day(parser, p); }
- break;
- case 15:
- #line 2640 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_day(parser, p)); }
- break;
- case 16:
- #line 2644 "upb/json/parser.rl"
- { start_hour(parser, p); }
- break;
- case 17:
- #line 2645 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_hour(parser, p)); }
- break;
- case 18:
- #line 2649 "upb/json/parser.rl"
- { start_minute(parser, p); }
- break;
- case 19:
- #line 2650 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_minute(parser, p)); }
- break;
- case 20:
- #line 2654 "upb/json/parser.rl"
- { start_second(parser, p); }
- break;
- case 21:
- #line 2655 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_second(parser, p)); }
- break;
- case 22:
- #line 2660 "upb/json/parser.rl"
- { start_duration_base(parser, p); }
- break;
- case 23:
- #line 2661 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_duration_base(parser, p)); }
- break;
- case 24:
- #line 2663 "upb/json/parser.rl"
- { p--; {cs = stack[--top]; goto _again;} }
- break;
- case 25:
- #line 2668 "upb/json/parser.rl"
- { start_timestamp_base(parser); }
- break;
- case 26:
- #line 2670 "upb/json/parser.rl"
- { start_timestamp_fraction(parser, p); }
- break;
- case 27:
- #line 2671 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); }
- break;
- case 28:
- #line 2673 "upb/json/parser.rl"
- { start_timestamp_zone(parser, p); }
- break;
- case 29:
- #line 2674 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); }
- break;
- case 30:
- #line 2676 "upb/json/parser.rl"
- { p--; {cs = stack[--top]; goto _again;} }
- break;
- case 31:
- #line 2681 "upb/json/parser.rl"
- { start_fieldmask_path_text(parser, p); }
- break;
- case 32:
- #line 2682 "upb/json/parser.rl"
- { end_fieldmask_path_text(parser, p); }
- break;
- case 33:
- #line 2687 "upb/json/parser.rl"
- { start_fieldmask_path(parser); }
- break;
- case 34:
- #line 2688 "upb/json/parser.rl"
- { end_fieldmask_path(parser); }
- break;
- case 35:
- #line 2694 "upb/json/parser.rl"
- { p--; {cs = stack[--top]; goto _again;} }
- break;
- case 36:
- #line 2699 "upb/json/parser.rl"
- {
- if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) {
- {stack[top++] = cs; cs = 47;goto _again;}
- } else if (is_wellknown_msg(parser, UPB_WELLKNOWN_DURATION)) {
- {stack[top++] = cs; cs = 40;goto _again;}
- } else if (is_wellknown_msg(parser, UPB_WELLKNOWN_FIELDMASK)) {
- {stack[top++] = cs; cs = 75;goto _again;}
- } else {
- {stack[top++] = cs; cs = 32;goto _again;}
- }
- }
- break;
- case 37:
- #line 2712 "upb/json/parser.rl"
- { p--; {stack[top++] = cs; cs = 78;goto _again;} }
- break;
- case 38:
- #line 2717 "upb/json/parser.rl"
- {
- if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
- start_any_member(parser, p);
- } else {
- start_member(parser);
- }
- }
- break;
- case 39:
- #line 2724 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_membername(parser)); }
- break;
- case 40:
- #line 2727 "upb/json/parser.rl"
- {
- if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
- end_any_member(parser, p);
- } else {
- end_member(parser);
- }
- }
- break;
- case 41:
- #line 2738 "upb/json/parser.rl"
- {
- if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
- start_any_object(parser, p);
- } else {
- start_object(parser);
- }
- }
- break;
- case 42:
- #line 2747 "upb/json/parser.rl"
- {
- if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
- CHECK_RETURN_TOP(end_any_object(parser, p));
- } else {
- end_object(parser);
- }
- }
- break;
- case 43:
- #line 2759 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(start_array(parser)); }
- break;
- case 44:
- #line 2763 "upb/json/parser.rl"
- { end_array(parser); }
- break;
- case 45:
- #line 2768 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(start_number(parser, p)); }
- break;
- case 46:
- #line 2769 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_number(parser, p)); }
- break;
- case 47:
- #line 2771 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(start_stringval(parser)); }
- break;
- case 48:
- #line 2772 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_stringval(parser)); }
- break;
- case 49:
- #line 2774 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_bool(parser, true)); }
- break;
- case 50:
- #line 2776 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_bool(parser, false)); }
- break;
- case 51:
- #line 2778 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_null(parser)); }
- break;
- case 52:
- #line 2780 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(start_subobject_full(parser)); }
- break;
- case 53:
- #line 2781 "upb/json/parser.rl"
- { end_subobject_full(parser); }
- break;
- case 54:
- #line 2786 "upb/json/parser.rl"
- { p--; {cs = stack[--top]; goto _again;} }
- break;
- #line 3194 "upb/json/parser.c"
- }
- }
- _again:
- if ( cs == 0 )
- goto _out;
- if ( ++p != pe )
- goto _resume;
- _test_eof: {}
- if ( p == eof )
- {
- const char *__acts = _json_actions + _json_eof_actions[cs];
- unsigned int __nacts = (unsigned int) *__acts++;
- while ( __nacts-- > 0 ) {
- switch ( *__acts++ ) {
- case 0:
- #line 2595 "upb/json/parser.rl"
- { p--; {cs = stack[--top]; if ( p == pe )
- goto _test_eof;
- goto _again;} }
- break;
- case 46:
- #line 2769 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_number(parser, p)); }
- break;
- case 49:
- #line 2774 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_bool(parser, true)); }
- break;
- case 50:
- #line 2776 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_bool(parser, false)); }
- break;
- case 51:
- #line 2778 "upb/json/parser.rl"
- { CHECK_RETURN_TOP(end_null(parser)); }
- break;
- case 53:
- #line 2781 "upb/json/parser.rl"
- { end_subobject_full(parser); }
- break;
- #line 3236 "upb/json/parser.c"
- }
- }
- }
- _out: {}
- }
- #line 2814 "upb/json/parser.rl"
- if (p != pe) {
- upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p);
- upb_env_reporterror(parser->env, &parser->status);
- } else {
- capture_suspend(parser, &p);
- }
- error:
- /* Save parsing state back to parser. */
- parser->current_state = cs;
- parser->parser_top = top;
- return p - buf;
- }
- static bool end(void *closure, const void *hd) {
- upb_json_parser *parser = closure;
- /* Prevent compile warning on unused static constants. */
- UPB_UNUSED(json_start);
- UPB_UNUSED(json_en_duration_machine);
- UPB_UNUSED(json_en_fieldmask_machine);
- UPB_UNUSED(json_en_number_machine);
- UPB_UNUSED(json_en_string_machine);
- UPB_UNUSED(json_en_timestamp_machine);
- UPB_UNUSED(json_en_value_machine);
- UPB_UNUSED(json_en_main);
- parse(parser, hd, &eof_ch, 0, NULL);
- return parser->current_state >= 106;
- }
- static void json_parser_reset(upb_json_parser *p) {
- int cs;
- int top;
- p->top = p->stack;
- init_frame(p->top);
- /* Emit Ragel initialization of the parser. */
-
- #line 3288 "upb/json/parser.c"
- {
- cs = json_start;
- top = 0;
- }
- #line 2857 "upb/json/parser.rl"
- p->current_state = cs;
- p->parser_top = top;
- accumulate_clear(p);
- p->multipart_state = MULTIPART_INACTIVE;
- p->capture = NULL;
- p->accumulated = NULL;
- upb_status_clear(&p->status);
- }
- static void visit_json_parsermethod(const upb_refcounted *r,
- upb_refcounted_visit *visit,
- void *closure) {
- const upb_json_parsermethod *method = (upb_json_parsermethod*)r;
- visit(r, upb_msgdef_upcast2(method->msg), closure);
- }
- static void free_json_parsermethod(upb_refcounted *r) {
- upb_json_parsermethod *method = (upb_json_parsermethod*)r;
- upb_inttable_iter i;
- upb_inttable_begin(&i, &method->name_tables);
- for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
- upb_value val = upb_inttable_iter_value(&i);
- upb_strtable *t = upb_value_getptr(val);
- upb_strtable_uninit(t);
- upb_gfree(t);
- }
- upb_inttable_uninit(&method->name_tables);
- upb_gfree(r);
- }
- static void add_jsonname_table(upb_json_parsermethod *m, const upb_msgdef* md) {
- upb_msg_field_iter i;
- upb_strtable *t;
- /* It would be nice to stack-allocate this, but protobufs do not limit the
- * length of fields to any reasonable limit. */
- char *buf = NULL;
- size_t len = 0;
- if (upb_inttable_lookupptr(&m->name_tables, md, NULL)) {
- return;
- }
- /* TODO(haberman): handle malloc failure. */
- t = upb_gmalloc(sizeof(*t));
- upb_strtable_init(t, UPB_CTYPE_CONSTPTR);
- upb_inttable_insertptr(&m->name_tables, md, upb_value_ptr(t));
- for(upb_msg_field_begin(&i, md);
- !upb_msg_field_done(&i);
- upb_msg_field_next(&i)) {
- const upb_fielddef *f = upb_msg_iter_field(&i);
- /* Add an entry for the JSON name. */
- size_t field_len = upb_fielddef_getjsonname(f, buf, len);
- if (field_len > len) {
- size_t len2;
- buf = upb_grealloc(buf, 0, field_len);
- len = field_len;
- len2 = upb_fielddef_getjsonname(f, buf, len);
- UPB_ASSERT(len == len2);
- }
- upb_strtable_insert(t, buf, upb_value_constptr(f));
- if (strcmp(buf, upb_fielddef_name(f)) != 0) {
- /* Since the JSON name is different from the regular field name, add an
- * entry for the raw name (compliant proto3 JSON parsers must accept
- * both). */
- upb_strtable_insert(t, upb_fielddef_name(f), upb_value_constptr(f));
- }
- if (upb_fielddef_issubmsg(f)) {
- add_jsonname_table(m, upb_fielddef_msgsubdef(f));
- }
- }
- upb_gfree(buf);
- }
- /* Public API *****************************************************************/
- upb_json_parser *upb_json_parser_create(upb_env *env,
- const upb_json_parsermethod *method,
- const upb_symtab* symtab,
- upb_sink *output,
- bool ignore_json_unknown) {
- #ifndef NDEBUG
- const size_t size_before = upb_env_bytesallocated(env);
- #endif
- upb_json_parser *p = upb_env_malloc(env, sizeof(upb_json_parser));
- if (!p) return false;
- p->env = env;
- p->method = method;
- p->limit = p->stack + UPB_JSON_MAX_DEPTH;
- p->accumulate_buf = NULL;
- p->accumulate_buf_size = 0;
- upb_bytessink_reset(&p->input_, &method->input_handler_, p);
- json_parser_reset(p);
- upb_sink_reset(&p->top->sink, output->handlers, output->closure);
- p->top->m = upb_handlers_msgdef(output->handlers);
- if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) {
- p->top->is_any = true;
- p->top->any_frame =
- upb_env_malloc(p->env, sizeof(upb_jsonparser_any_frame));
- json_parser_any_frame_reset(p->top->any_frame);
- } else {
- p->top->is_any = false;
- p->top->any_frame = NULL;
- }
- set_name_table(p, p->top);
- p->symtab = symtab;
- p->ignore_json_unknown = ignore_json_unknown;
- /* If this fails, uncomment and increase the value in parser.h. */
- /* fprintf(stderr, "%zd\n", upb_env_bytesallocated(env) - size_before); */
- UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(env) - size_before <=
- UPB_JSON_PARSER_SIZE);
- return p;
- }
- upb_bytessink *upb_json_parser_input(upb_json_parser *p) {
- return &p->input_;
- }
- upb_json_parsermethod *upb_json_parsermethod_new(const upb_msgdef* md,
- const void* owner) {
- static const struct upb_refcounted_vtbl vtbl = {visit_json_parsermethod,
- free_json_parsermethod};
- upb_json_parsermethod *ret = upb_gmalloc(sizeof(*ret));
- upb_refcounted_init(upb_json_parsermethod_upcast_mutable(ret), &vtbl, owner);
- ret->msg = md;
- upb_ref2(md, ret);
- upb_byteshandler_init(&ret->input_handler_);
- upb_byteshandler_setstring(&ret->input_handler_, parse, ret);
- upb_byteshandler_setendstr(&ret->input_handler_, end, ret);
- upb_inttable_init(&ret->name_tables, UPB_CTYPE_PTR);
- add_jsonname_table(ret, md);
- return ret;
- }
- const upb_byteshandler *upb_json_parsermethod_inputhandler(
- const upb_json_parsermethod *m) {
- return &m->input_handler_;
- }
- /*
- ** This currently uses snprintf() to format primitives, and could be optimized
- ** further.
- */
- #include <string.h>
- #include <stdint.h>
- #include <time.h>
- struct upb_json_printer {
- upb_sink input_;
- /* BytesSink closure. */
- void *subc_;
- upb_bytessink *output_;
- /* We track the depth so that we know when to emit startstr/endstr on the
- * output. */
- int depth_;
- /* Have we emitted the first element? This state is necessary to emit commas
- * without leaving a trailing comma in arrays/maps. We keep this state per
- * frame depth.
- *
- * Why max_depth * 2? UPB_MAX_HANDLER_DEPTH counts depth as nested messages.
- * We count frames (contexts in which we separate elements by commas) as both
- * repeated fields and messages (maps), and the worst case is a
- * message->repeated field->submessage->repeated field->... nesting. */
- bool first_elem_[UPB_MAX_HANDLER_DEPTH * 2];
- /* To print timestamp, printer needs to cache its seconds and nanos values
- * and convert them when ending timestamp message. See comments of
- * printer_sethandlers_timestamp for more detail. */
- int64_t seconds;
- int32_t nanos;
- };
- /* StringPiece; a pointer plus a length. */
- typedef struct {
- char *ptr;
- size_t len;
- } strpc;
- void freestrpc(void *ptr) {
- strpc *pc = ptr;
- upb_gfree(pc->ptr);
- upb_gfree(pc);
- }
- /* Convert fielddef name to JSON name and return as a string piece. */
- strpc *newstrpc(upb_handlers *h, const upb_fielddef *f,
- bool preserve_fieldnames) {
- /* TODO(haberman): handle malloc failure. */
- strpc *ret = upb_gmalloc(sizeof(*ret));
- if (preserve_fieldnames) {
- ret->ptr = upb_gstrdup(upb_fielddef_name(f));
- ret->len = strlen(ret->ptr);
- } else {
- size_t len;
- ret->len = upb_fielddef_getjsonname(f, NULL, 0);
- ret->ptr = upb_gmalloc(ret->len);
- len = upb_fielddef_getjsonname(f, ret->ptr, ret->len);
- UPB_ASSERT(len == ret->len);
- ret->len--; /* NULL */
- }
- upb_handlers_addcleanup(h, ret, freestrpc);
- return ret;
- }
- /* Convert a null-terminated const char* to a string piece. */
- strpc *newstrpc_str(upb_handlers *h, const char * str) {
- strpc * ret = upb_gmalloc(sizeof(*ret));
- ret->ptr = upb_gstrdup(str);
- ret->len = strlen(str);
- upb_handlers_addcleanup(h, ret, freestrpc);
- return ret;
- }
- /* ------------ JSON string printing: values, maps, arrays ------------------ */
- static void print_data(
- upb_json_printer *p, const char *buf, unsigned int len) {
- /* TODO: Will need to change if we support pushback from the sink. */
- size_t n = upb_bytessink_putbuf(p->output_, p->subc_, buf, len, NULL);
- UPB_ASSERT(n == len);
- }
- static void print_comma(upb_json_printer *p) {
- if (!p->first_elem_[p->depth_]) {
- print_data(p, ",", 1);
- }
- p->first_elem_[p->depth_] = false;
- }
- /* Helpers that print properly formatted elements to the JSON output stream. */
- /* Used for escaping control chars in strings. */
- static const char kControlCharLimit = 0x20;
- UPB_INLINE bool is_json_escaped(char c) {
- /* See RFC 4627. */
- unsigned char uc = (unsigned char)c;
- return uc < kControlCharLimit || uc == '"' || uc == '\\';
- }
- UPB_INLINE const char* json_nice_escape(char c) {
- switch (c) {
- case '"': return "\\\"";
- case '\\': return "\\\\";
- case '\b': return "\\b";
- case '\f': return "\\f";
- case '\n': return "\\n";
- case '\r': return "\\r";
- case '\t': return "\\t";
- default: return NULL;
- }
- }
- /* Write a properly escaped string chunk. The surrounding quotes are *not*
- * printed; this is so that the caller has the option of emitting the string
- * content in chunks. */
- static void putstring(upb_json_printer *p, const char *buf, unsigned int len) {
- const char* unescaped_run = NULL;
- unsigned int i;
- for (i = 0; i < len; i++) {
- char c = buf[i];
- /* Handle escaping. */
- if (is_json_escaped(c)) {
- /* Use a "nice" escape, like \n, if one exists for this character. */
- const char* escape = json_nice_escape(c);
- /* If we don't have a specific 'nice' escape code, use a \uXXXX-style
- * escape. */
- char escape_buf[8];
- if (!escape) {
- unsigned char byte = (unsigned char)c;
- _upb_snprintf(escape_buf, sizeof(escape_buf), "\\u%04x", (int)byte);
- escape = escape_buf;
- }
- /* N.B. that we assume that the input encoding is equal to the output
- * encoding (both UTF-8 for now), so for chars >= 0x20 and != \, ", we
- * can simply pass the bytes through. */
- /* If there's a current run of unescaped chars, print that run first. */
- if (unescaped_run) {
- print_data(p, unescaped_run, &buf[i] - unescaped_run);
- unescaped_run = NULL;
- }
- /* Then print the escape code. */
- print_data(p, escape, strlen(escape));
- } else {
- /* Add to the current unescaped run of characters. */
- if (unescaped_run == NULL) {
- unescaped_run = &buf[i];
- }
- }
- }
- /* If the string ended in a run of unescaped characters, print that last run. */
- if (unescaped_run) {
- print_data(p, unescaped_run, &buf[len] - unescaped_run);
- }
- }
- #define CHKLENGTH(x) if (!(x)) return -1;
- /* Helpers that format floating point values according to our custom formats.
- * Right now we use %.8g and %.17g for float/double, respectively, to match
- * proto2::util::JsonFormat's defaults. May want to change this later. */
- const char neginf[] = "\"-Infinity\"";
- const char inf[] = "\"Infinity\"";
- static size_t fmt_double(double val, char* buf, size_t length) {
- if (val == (1.0 / 0.0)) {
- CHKLENGTH(length >= strlen(inf));
- strcpy(buf, inf);
- return strlen(inf);
- } else if (val == (-1.0 / 0.0)) {
- CHKLENGTH(length >= strlen(neginf));
- strcpy(buf, neginf);
- return strlen(neginf);
- } else {
- size_t n = _upb_snprintf(buf, length, "%.17g", val);
- CHKLENGTH(n > 0 && n < length);
- return n;
- }
- }
- static size_t fmt_float(float val, char* buf, size_t length) {
- size_t n = _upb_snprintf(buf, length, "%.8g", val);
- CHKLENGTH(n > 0 && n < length);
- return n;
- }
- static size_t fmt_bool(bool val, char* buf, size_t length) {
- size_t n = _upb_snprintf(buf, length, "%s", (val ? "true" : "false"));
- CHKLENGTH(n > 0 && n < length);
- return n;
- }
- static size_t fmt_int64_as_number(long long val, char* buf, size_t length) {
- size_t n = _upb_snprintf(buf, length, "%lld", val);
- CHKLENGTH(n > 0 && n < length);
- return n;
- }
- static size_t fmt_uint64_as_number(
- unsigned long long val, char* buf, size_t length) {
- size_t n = _upb_snprintf(buf, length, "%llu", val);
- CHKLENGTH(n > 0 && n < length);
- return n;
- }
- static size_t fmt_int64_as_string(long long val, char* buf, size_t length) {
- size_t n = _upb_snprintf(buf, length, "\"%lld\"", val);
- CHKLENGTH(n > 0 && n < length);
- return n;
- }
- static size_t fmt_uint64_as_string(
- unsigned long long val, char* buf, size_t length) {
- size_t n = _upb_snprintf(buf, length, "\"%llu\"", val);
- CHKLENGTH(n > 0 && n < length);
- return n;
- }
- /* Print a map key given a field name. Called by scalar field handlers and by
- * startseq for repeated fields. */
- static bool putkey(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- const strpc *key = handler_data;
- print_comma(p);
- print_data(p, "\"", 1);
- putstring(p, key->ptr, key->len);
- print_data(p, "\":", 2);
- return true;
- }
- #define CHKFMT(val) if ((val) == (size_t)-1) return false;
- #define CHK(val) if (!(val)) return false;
- #define TYPE_HANDLERS(type, fmt_func) \
- static bool put##type(void *closure, const void *handler_data, type val) { \
- upb_json_printer *p = closure; \
- char data[64]; \
- size_t length = fmt_func(val, data, sizeof(data)); \
- UPB_UNUSED(handler_data); \
- CHKFMT(length); \
- print_data(p, data, length); \
- return true; \
- } \
- static bool scalar_##type(void *closure, const void *handler_data, \
- type val) { \
- CHK(putkey(closure, handler_data)); \
- CHK(put##type(closure, handler_data, val)); \
- return true; \
- } \
- static bool repeated_##type(void *closure, const void *handler_data, \
- type val) { \
- upb_json_printer *p = closure; \
- print_comma(p); \
- CHK(put##type(closure, handler_data, val)); \
- return true; \
- }
- #define TYPE_HANDLERS_MAPKEY(type, fmt_func) \
- static bool putmapkey_##type(void *closure, const void *handler_data, \
- type val) { \
- upb_json_printer *p = closure; \
- char data[64]; \
- size_t length = fmt_func(val, data, sizeof(data)); \
- UPB_UNUSED(handler_data); \
- print_data(p, "\"", 1); \
- print_data(p, data, length); \
- print_data(p, "\":", 2); \
- return true; \
- }
- TYPE_HANDLERS(double, fmt_double)
- TYPE_HANDLERS(float, fmt_float)
- TYPE_HANDLERS(bool, fmt_bool)
- TYPE_HANDLERS(int32_t, fmt_int64_as_number)
- TYPE_HANDLERS(uint32_t, fmt_int64_as_number)
- TYPE_HANDLERS(int64_t, fmt_int64_as_string)
- TYPE_HANDLERS(uint64_t, fmt_uint64_as_string)
- /* double and float are not allowed to be map keys. */
- TYPE_HANDLERS_MAPKEY(bool, fmt_bool)
- TYPE_HANDLERS_MAPKEY(int32_t, fmt_int64_as_number)
- TYPE_HANDLERS_MAPKEY(uint32_t, fmt_int64_as_number)
- TYPE_HANDLERS_MAPKEY(int64_t, fmt_int64_as_number)
- TYPE_HANDLERS_MAPKEY(uint64_t, fmt_uint64_as_number)
- #undef TYPE_HANDLERS
- #undef TYPE_HANDLERS_MAPKEY
- typedef struct {
- void *keyname;
- const upb_enumdef *enumdef;
- } EnumHandlerData;
- static bool scalar_enum(void *closure, const void *handler_data,
- int32_t val) {
- const EnumHandlerData *hd = handler_data;
- upb_json_printer *p = closure;
- const char *symbolic_name;
- CHK(putkey(closure, hd->keyname));
- symbolic_name = upb_enumdef_iton(hd->enumdef, val);
- if (symbolic_name) {
- print_data(p, "\"", 1);
- putstring(p, symbolic_name, strlen(symbolic_name));
- print_data(p, "\"", 1);
- } else {
- putint32_t(closure, NULL, val);
- }
- return true;
- }
- static void print_enum_symbolic_name(upb_json_printer *p,
- const upb_enumdef *def,
- int32_t val) {
- const char *symbolic_name = upb_enumdef_iton(def, val);
- if (symbolic_name) {
- print_data(p, "\"", 1);
- putstring(p, symbolic_name, strlen(symbolic_name));
- print_data(p, "\"", 1);
- } else {
- putint32_t(p, NULL, val);
- }
- }
- static bool repeated_enum(void *closure, const void *handler_data,
- int32_t val) {
- const EnumHandlerData *hd = handler_data;
- upb_json_printer *p = closure;
- print_comma(p);
- print_enum_symbolic_name(p, hd->enumdef, val);
- return true;
- }
- static bool mapvalue_enum(void *closure, const void *handler_data,
- int32_t val) {
- const EnumHandlerData *hd = handler_data;
- upb_json_printer *p = closure;
- print_enum_symbolic_name(p, hd->enumdef, val);
- return true;
- }
- static void *scalar_startsubmsg(void *closure, const void *handler_data) {
- return putkey(closure, handler_data) ? closure : UPB_BREAK;
- }
- static void *repeated_startsubmsg(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- print_comma(p);
- return closure;
- }
- static void start_frame(upb_json_printer *p) {
- p->depth_++;
- p->first_elem_[p->depth_] = true;
- print_data(p, "{", 1);
- }
- static void end_frame(upb_json_printer *p) {
- print_data(p, "}", 1);
- p->depth_--;
- }
- static bool printer_startmsg(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- if (p->depth_ == 0) {
- upb_bytessink_start(p->output_, 0, &p->subc_);
- }
- start_frame(p);
- return true;
- }
- static bool printer_endmsg(void *closure, const void *handler_data, upb_status *s) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- UPB_UNUSED(s);
- end_frame(p);
- if (p->depth_ == 0) {
- upb_bytessink_end(p->output_);
- }
- return true;
- }
- static void *startseq(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- CHK(putkey(closure, handler_data));
- p->depth_++;
- p->first_elem_[p->depth_] = true;
- print_data(p, "[", 1);
- return closure;
- }
- static bool endseq(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- print_data(p, "]", 1);
- p->depth_--;
- return true;
- }
- static void *startmap(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- CHK(putkey(closure, handler_data));
- p->depth_++;
- p->first_elem_[p->depth_] = true;
- print_data(p, "{", 1);
- return closure;
- }
- static bool endmap(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- print_data(p, "}", 1);
- p->depth_--;
- return true;
- }
- static size_t putstr(void *closure, const void *handler_data, const char *str,
- size_t len, const upb_bufhandle *handle) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- UPB_UNUSED(handle);
- putstring(p, str, len);
- return len;
- }
- /* This has to Base64 encode the bytes, because JSON has no "bytes" type. */
- static size_t putbytes(void *closure, const void *handler_data, const char *str,
- size_t len, const upb_bufhandle *handle) {
- upb_json_printer *p = closure;
- /* This is the regular base64, not the "web-safe" version. */
- static const char base64[] =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- /* Base64-encode. */
- char data[16000];
- const char *limit = data + sizeof(data);
- const unsigned char *from = (const unsigned char*)str;
- char *to = data;
- size_t remaining = len;
- size_t bytes;
- UPB_UNUSED(handler_data);
- UPB_UNUSED(handle);
- print_data(p, "\"", 1);
- while (remaining > 2) {
- if (limit - to < 4) {
- bytes = to - data;
- putstring(p, data, bytes);
- to = data;
- }
- to[0] = base64[from[0] >> 2];
- to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)];
- to[2] = base64[((from[1] & 0xf) << 2) | (from[2] >> 6)];
- to[3] = base64[from[2] & 0x3f];
- remaining -= 3;
- to += 4;
- from += 3;
- }
- switch (remaining) {
- case 2:
- to[0] = base64[from[0] >> 2];
- to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)];
- to[2] = base64[(from[1] & 0xf) << 2];
- to[3] = '=';
- to += 4;
- from += 2;
- break;
- case 1:
- to[0] = base64[from[0] >> 2];
- to[1] = base64[((from[0] & 0x3) << 4)];
- to[2] = '=';
- to[3] = '=';
- to += 4;
- from += 1;
- break;
- }
- bytes = to - data;
- putstring(p, data, bytes);
- print_data(p, "\"", 1);
- return len;
- }
- static void *scalar_startstr(void *closure, const void *handler_data,
- size_t size_hint) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- UPB_UNUSED(size_hint);
- CHK(putkey(closure, handler_data));
- print_data(p, "\"", 1);
- return p;
- }
- static size_t scalar_str(void *closure, const void *handler_data,
- const char *str, size_t len,
- const upb_bufhandle *handle) {
- CHK(putstr(closure, handler_data, str, len, handle));
- return len;
- }
- static bool scalar_endstr(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- print_data(p, "\"", 1);
- return true;
- }
- static void *repeated_startstr(void *closure, const void *handler_data,
- size_t size_hint) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- UPB_UNUSED(size_hint);
- print_comma(p);
- print_data(p, "\"", 1);
- return p;
- }
- static size_t repeated_str(void *closure, const void *handler_data,
- const char *str, size_t len,
- const upb_bufhandle *handle) {
- CHK(putstr(closure, handler_data, str, len, handle));
- return len;
- }
- static bool repeated_endstr(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- print_data(p, "\"", 1);
- return true;
- }
- static void *mapkeyval_startstr(void *closure, const void *handler_data,
- size_t size_hint) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- UPB_UNUSED(size_hint);
- print_data(p, "\"", 1);
- return p;
- }
- static size_t mapkey_str(void *closure, const void *handler_data,
- const char *str, size_t len,
- const upb_bufhandle *handle) {
- CHK(putstr(closure, handler_data, str, len, handle));
- return len;
- }
- static bool mapkey_endstr(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- print_data(p, "\":", 2);
- return true;
- }
- static bool mapvalue_endstr(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- print_data(p, "\"", 1);
- return true;
- }
- static size_t scalar_bytes(void *closure, const void *handler_data,
- const char *str, size_t len,
- const upb_bufhandle *handle) {
- CHK(putkey(closure, handler_data));
- CHK(putbytes(closure, handler_data, str, len, handle));
- return len;
- }
- static size_t repeated_bytes(void *closure, const void *handler_data,
- const char *str, size_t len,
- const upb_bufhandle *handle) {
- upb_json_printer *p = closure;
- print_comma(p);
- CHK(putbytes(closure, handler_data, str, len, handle));
- return len;
- }
- static size_t mapkey_bytes(void *closure, const void *handler_data,
- const char *str, size_t len,
- const upb_bufhandle *handle) {
- upb_json_printer *p = closure;
- CHK(putbytes(closure, handler_data, str, len, handle));
- print_data(p, ":", 1);
- return len;
- }
- static void set_enum_hd(upb_handlers *h,
- const upb_fielddef *f,
- bool preserve_fieldnames,
- upb_handlerattr *attr) {
- EnumHandlerData *hd = upb_gmalloc(sizeof(EnumHandlerData));
- hd->enumdef = (const upb_enumdef *)upb_fielddef_subdef(f);
- hd->keyname = newstrpc(h, f, preserve_fieldnames);
- upb_handlers_addcleanup(h, hd, upb_gfree);
- upb_handlerattr_sethandlerdata(attr, hd);
- }
- /* Set up handlers for a mapentry submessage (i.e., an individual key/value pair
- * in a map).
- *
- * TODO: Handle missing key, missing value, out-of-order key/value, or repeated
- * key or value cases properly. The right way to do this is to allocate a
- * temporary structure at the start of a mapentry submessage, store key and
- * value data in it as key and value handlers are called, and then print the
- * key/value pair once at the end of the submessage. If we don't do this, we
- * should at least detect the case and throw an error. However, so far all of
- * our sources that emit mapentry messages do so canonically (with one key
- * field, and then one value field), so this is not a pressing concern at the
- * moment. */
- void printer_sethandlers_mapentry(const void *closure, bool preserve_fieldnames,
- upb_handlers *h) {
- const upb_msgdef *md = upb_handlers_msgdef(h);
- /* A mapentry message is printed simply as '"key": value'. Rather than
- * special-case key and value for every type below, we just handle both
- * fields explicitly here. */
- const upb_fielddef* key_field = upb_msgdef_itof(md, UPB_MAPENTRY_KEY);
- const upb_fielddef* value_field = upb_msgdef_itof(md, UPB_MAPENTRY_VALUE);
- upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER;
- UPB_UNUSED(closure);
- switch (upb_fielddef_type(key_field)) {
- case UPB_TYPE_INT32:
- upb_handlers_setint32(h, key_field, putmapkey_int32_t, &empty_attr);
- break;
- case UPB_TYPE_INT64:
- upb_handlers_setint64(h, key_field, putmapkey_int64_t, &empty_attr);
- break;
- case UPB_TYPE_UINT32:
- upb_handlers_setuint32(h, key_field, putmapkey_uint32_t, &empty_attr);
- break;
- case UPB_TYPE_UINT64:
- upb_handlers_setuint64(h, key_field, putmapkey_uint64_t, &empty_attr);
- break;
- case UPB_TYPE_BOOL:
- upb_handlers_setbool(h, key_field, putmapkey_bool, &empty_attr);
- break;
- case UPB_TYPE_STRING:
- upb_handlers_setstartstr(h, key_field, mapkeyval_startstr, &empty_attr);
- upb_handlers_setstring(h, key_field, mapkey_str, &empty_attr);
- upb_handlers_setendstr(h, key_field, mapkey_endstr, &empty_attr);
- break;
- case UPB_TYPE_BYTES:
- upb_handlers_setstring(h, key_field, mapkey_bytes, &empty_attr);
- break;
- default:
- UPB_ASSERT(false);
- break;
- }
- switch (upb_fielddef_type(value_field)) {
- case UPB_TYPE_INT32:
- upb_handlers_setint32(h, value_field, putint32_t, &empty_attr);
- break;
- case UPB_TYPE_INT64:
- upb_handlers_setint64(h, value_field, putint64_t, &empty_attr);
- break;
- case UPB_TYPE_UINT32:
- upb_handlers_setuint32(h, value_field, putuint32_t, &empty_attr);
- break;
- case UPB_TYPE_UINT64:
- upb_handlers_setuint64(h, value_field, putuint64_t, &empty_attr);
- break;
- case UPB_TYPE_BOOL:
- upb_handlers_setbool(h, value_field, putbool, &empty_attr);
- break;
- case UPB_TYPE_FLOAT:
- upb_handlers_setfloat(h, value_field, putfloat, &empty_attr);
- break;
- case UPB_TYPE_DOUBLE:
- upb_handlers_setdouble(h, value_field, putdouble, &empty_attr);
- break;
- case UPB_TYPE_STRING:
- upb_handlers_setstartstr(h, value_field, mapkeyval_startstr, &empty_attr);
- upb_handlers_setstring(h, value_field, putstr, &empty_attr);
- upb_handlers_setendstr(h, value_field, mapvalue_endstr, &empty_attr);
- break;
- case UPB_TYPE_BYTES:
- upb_handlers_setstring(h, value_field, putbytes, &empty_attr);
- break;
- case UPB_TYPE_ENUM: {
- upb_handlerattr enum_attr = UPB_HANDLERATTR_INITIALIZER;
- set_enum_hd(h, value_field, preserve_fieldnames, &enum_attr);
- upb_handlers_setint32(h, value_field, mapvalue_enum, &enum_attr);
- upb_handlerattr_uninit(&enum_attr);
- break;
- }
- case UPB_TYPE_MESSAGE:
- /* No handler necessary -- the submsg handlers will print the message
- * as appropriate. */
- break;
- }
- upb_handlerattr_uninit(&empty_attr);
- }
- static bool putseconds(void *closure, const void *handler_data,
- int64_t seconds) {
- upb_json_printer *p = closure;
- p->seconds = seconds;
- UPB_UNUSED(handler_data);
- return true;
- }
- static bool putnanos(void *closure, const void *handler_data,
- int32_t nanos) {
- upb_json_printer *p = closure;
- p->nanos = nanos;
- UPB_UNUSED(handler_data);
- return true;
- }
- static void *scalar_startstr_nokey(void *closure, const void *handler_data,
- size_t size_hint) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- UPB_UNUSED(size_hint);
- print_data(p, "\"", 1);
- return p;
- }
- static size_t putstr_nokey(void *closure, const void *handler_data,
- const char *str, size_t len,
- const upb_bufhandle *handle) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- UPB_UNUSED(handle);
- print_data(p, "\"", 1);
- putstring(p, str, len);
- print_data(p, "\"", 1);
- return len + 2;
- }
- static void *startseq_nokey(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- p->depth_++;
- p->first_elem_[p->depth_] = true;
- print_data(p, "[", 1);
- return closure;
- }
- static void *startseq_fieldmask(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- p->depth_++;
- p->first_elem_[p->depth_] = true;
- return closure;
- }
- static bool endseq_fieldmask(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- p->depth_--;
- return true;
- }
- static void *repeated_startstr_fieldmask(
- void *closure, const void *handler_data,
- size_t size_hint) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- UPB_UNUSED(size_hint);
- print_comma(p);
- return p;
- }
- static size_t repeated_str_fieldmask(
- void *closure, const void *handler_data,
- const char *str, size_t len,
- const upb_bufhandle *handle) {
- const char* limit = str + len;
- bool upper = false;
- size_t result_len = 0;
- for (; str < limit; str++) {
- if (*str == '_') {
- upper = true;
- continue;
- }
- if (upper && *str >= 'a' && *str <= 'z') {
- char upper_char = toupper(*str);
- CHK(putstr(closure, handler_data, &upper_char, 1, handle));
- } else {
- CHK(putstr(closure, handler_data, str, 1, handle));
- }
- upper = false;
- result_len++;
- }
- return result_len;
- }
- static void *startmap_nokey(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- p->depth_++;
- p->first_elem_[p->depth_] = true;
- print_data(p, "{", 1);
- return closure;
- }
- static bool putnull(void *closure, const void *handler_data,
- int32_t null) {
- upb_json_printer *p = closure;
- print_data(p, "null", 4);
- UPB_UNUSED(handler_data);
- UPB_UNUSED(null);
- return true;
- }
- static bool printer_startdurationmsg(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- if (p->depth_ == 0) {
- upb_bytessink_start(p->output_, 0, &p->subc_);
- }
- return true;
- }
- #define UPB_DURATION_MAX_JSON_LEN 23
- #define UPB_DURATION_MAX_NANO_LEN 9
- static bool printer_enddurationmsg(void *closure, const void *handler_data,
- upb_status *s) {
- upb_json_printer *p = closure;
- char buffer[UPB_DURATION_MAX_JSON_LEN];
- size_t base_len;
- size_t curr;
- size_t i;
- memset(buffer, 0, UPB_DURATION_MAX_JSON_LEN);
- if (p->seconds < -315576000000) {
- upb_status_seterrf(s, "error parsing duration: "
- "minimum acceptable value is "
- "-315576000000");
- return false;
- }
- if (p->seconds > 315576000000) {
- upb_status_seterrf(s, "error serializing duration: "
- "maximum acceptable value is "
- "315576000000");
- return false;
- }
- _upb_snprintf(buffer, sizeof(buffer), "%ld", (long)p->seconds);
- base_len = strlen(buffer);
- if (p->nanos != 0) {
- char nanos_buffer[UPB_DURATION_MAX_NANO_LEN + 3];
- _upb_snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f",
- p->nanos / 1000000000.0);
- /* Remove trailing 0. */
- for (i = UPB_DURATION_MAX_NANO_LEN + 2;
- nanos_buffer[i] == '0'; i--) {
- nanos_buffer[i] = 0;
- }
- strcpy(buffer + base_len, nanos_buffer + 1);
- }
- curr = strlen(buffer);
- strcpy(buffer + curr, "s");
- p->seconds = 0;
- p->nanos = 0;
- print_data(p, "\"", 1);
- print_data(p, buffer, strlen(buffer));
- print_data(p, "\"", 1);
- if (p->depth_ == 0) {
- upb_bytessink_end(p->output_);
- }
- UPB_UNUSED(handler_data);
- return true;
- }
- static bool printer_starttimestampmsg(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- if (p->depth_ == 0) {
- upb_bytessink_start(p->output_, 0, &p->subc_);
- }
- return true;
- }
- #define UPB_TIMESTAMP_MAX_JSON_LEN 31
- #define UPB_TIMESTAMP_BEFORE_NANO_LEN 19
- #define UPB_TIMESTAMP_MAX_NANO_LEN 9
- static bool printer_endtimestampmsg(void *closure, const void *handler_data,
- upb_status *s) {
- upb_json_printer *p = closure;
- char buffer[UPB_TIMESTAMP_MAX_JSON_LEN];
- time_t time = p->seconds;
- size_t curr;
- size_t i;
- size_t year_length =
- strftime(buffer, UPB_TIMESTAMP_MAX_JSON_LEN, "%Y", gmtime(&time));
- if (p->seconds < -62135596800) {
- upb_status_seterrf(s, "error parsing timestamp: "
- "minimum acceptable value is "
- "0001-01-01T00:00:00Z");
- return false;
- }
- if (p->seconds > 253402300799) {
- upb_status_seterrf(s, "error parsing timestamp: "
- "maximum acceptable value is "
- "9999-12-31T23:59:59Z");
- return false;
- }
- /* strftime doesn't guarantee 4 digits for year. Prepend 0 by ourselves. */
- for (i = 0; i < 4 - year_length; i++) {
- buffer[i] = '0';
- }
- strftime(buffer + (4 - year_length), UPB_TIMESTAMP_MAX_JSON_LEN,
- "%Y-%m-%dT%H:%M:%S", gmtime(&time));
- if (p->nanos != 0) {
- char nanos_buffer[UPB_TIMESTAMP_MAX_NANO_LEN + 3];
- _upb_snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f",
- p->nanos / 1000000000.0);
- /* Remove trailing 0. */
- for (i = UPB_TIMESTAMP_MAX_NANO_LEN + 2;
- nanos_buffer[i] == '0'; i--) {
- nanos_buffer[i] = 0;
- }
- strcpy(buffer + UPB_TIMESTAMP_BEFORE_NANO_LEN, nanos_buffer + 1);
- }
- curr = strlen(buffer);
- strcpy(buffer + curr, "Z");
- p->seconds = 0;
- p->nanos = 0;
- print_data(p, "\"", 1);
- print_data(p, buffer, strlen(buffer));
- print_data(p, "\"", 1);
- if (p->depth_ == 0) {
- upb_bytessink_end(p->output_);
- }
- UPB_UNUSED(handler_data);
- UPB_UNUSED(s);
- return true;
- }
- static bool printer_startmsg_noframe(void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- if (p->depth_ == 0) {
- upb_bytessink_start(p->output_, 0, &p->subc_);
- }
- return true;
- }
- static bool printer_endmsg_noframe(
- void *closure, const void *handler_data, upb_status *s) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- UPB_UNUSED(s);
- if (p->depth_ == 0) {
- upb_bytessink_end(p->output_);
- }
- return true;
- }
- static bool printer_startmsg_fieldmask(
- void *closure, const void *handler_data) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- if (p->depth_ == 0) {
- upb_bytessink_start(p->output_, 0, &p->subc_);
- }
- print_data(p, "\"", 1);
- return true;
- }
- static bool printer_endmsg_fieldmask(
- void *closure, const void *handler_data, upb_status *s) {
- upb_json_printer *p = closure;
- UPB_UNUSED(handler_data);
- UPB_UNUSED(s);
- print_data(p, "\"", 1);
- if (p->depth_ == 0) {
- upb_bytessink_end(p->output_);
- }
- return true;
- }
- static void *scalar_startstr_onlykey(
- void *closure, const void *handler_data, size_t size_hint) {
- upb_json_printer *p = closure;
- UPB_UNUSED(size_hint);
- CHK(putkey(closure, handler_data));
- return p;
- }
- /* Set up handlers for an Any submessage. */
- void printer_sethandlers_any(const void *closure, upb_handlers *h) {
- const upb_msgdef *md = upb_handlers_msgdef(h);
- const upb_fielddef* type_field = upb_msgdef_itof(md, UPB_ANY_TYPE);
- const upb_fielddef* value_field = upb_msgdef_itof(md, UPB_ANY_VALUE);
- upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER;
- /* type_url's json name is "@type" */
- upb_handlerattr type_name_attr = UPB_HANDLERATTR_INITIALIZER;
- upb_handlerattr value_name_attr = UPB_HANDLERATTR_INITIALIZER;
- strpc *type_url_json_name = newstrpc_str(h, "@type");
- strpc *value_json_name = newstrpc_str(h, "value");
- upb_handlerattr_sethandlerdata(&type_name_attr, type_url_json_name);
- upb_handlerattr_sethandlerdata(&value_name_attr, value_json_name);
- /* Set up handlers. */
- upb_handlers_setstartmsg(h, printer_startmsg, &empty_attr);
- upb_handlers_setendmsg(h, printer_endmsg, &empty_attr);
- upb_handlers_setstartstr(h, type_field, scalar_startstr, &type_name_attr);
- upb_handlers_setstring(h, type_field, scalar_str, &empty_attr);
- upb_handlers_setendstr(h, type_field, scalar_endstr, &empty_attr);
- /* This is not the full and correct JSON encoding for the Any value field. It
- * requires further processing by the wrapper code based on the type URL.
- */
- upb_handlers_setstartstr(h, value_field, scalar_startstr_onlykey,
- &value_name_attr);
- UPB_UNUSED(closure);
- }
- /* Set up handlers for a fieldmask submessage. */
- void printer_sethandlers_fieldmask(const void *closure, upb_handlers *h) {
- const upb_msgdef *md = upb_handlers_msgdef(h);
- const upb_fielddef* f = upb_msgdef_itof(md, 1);
- upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER;
- upb_handlers_setstartseq(h, f, startseq_fieldmask, &empty_attr);
- upb_handlers_setendseq(h, f, endseq_fieldmask, &empty_attr);
- upb_handlers_setstartmsg(h, printer_startmsg_fieldmask, &empty_attr);
- upb_handlers_setendmsg(h, printer_endmsg_fieldmask, &empty_attr);
- upb_handlers_setstartstr(h, f, repeated_startstr_fieldmask, &empty_attr);
- upb_handlers_setstring(h, f, repeated_str_fieldmask, &empty_attr);
- UPB_UNUSED(closure);
- }
- /* Set up handlers for a duration submessage. */
- void printer_sethandlers_duration(const void *closure, upb_handlers *h) {
- const upb_msgdef *md = upb_handlers_msgdef(h);
- const upb_fielddef* seconds_field =
- upb_msgdef_itof(md, UPB_DURATION_SECONDS);
- const upb_fielddef* nanos_field =
- upb_msgdef_itof(md, UPB_DURATION_NANOS);
- upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER;
- upb_handlers_setstartmsg(h, printer_startdurationmsg, &empty_attr);
- upb_handlers_setint64(h, seconds_field, putseconds, &empty_attr);
- upb_handlers_setint32(h, nanos_field, putnanos, &empty_attr);
- upb_handlers_setendmsg(h, printer_enddurationmsg, &empty_attr);
- UPB_UNUSED(closure);
- }
- /* Set up handlers for a timestamp submessage. Instead of printing fields
- * separately, the json representation of timestamp follows RFC 3339 */
- void printer_sethandlers_timestamp(const void *closure, upb_handlers *h) {
- const upb_msgdef *md = upb_handlers_msgdef(h);
- const upb_fielddef* seconds_field =
- upb_msgdef_itof(md, UPB_TIMESTAMP_SECONDS);
- const upb_fielddef* nanos_field =
- upb_msgdef_itof(md, UPB_TIMESTAMP_NANOS);
- upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER;
- upb_handlers_setstartmsg(h, printer_starttimestampmsg, &empty_attr);
- upb_handlers_setint64(h, seconds_field, putseconds, &empty_attr);
- upb_handlers_setint32(h, nanos_field, putnanos, &empty_attr);
- upb_handlers_setendmsg(h, printer_endtimestampmsg, &empty_attr);
- UPB_UNUSED(closure);
- }
- void printer_sethandlers_value(const void *closure, upb_handlers *h) {
- const upb_msgdef *md = upb_handlers_msgdef(h);
- upb_msg_field_iter i;
- upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER;
- upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr);
- upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr);
- upb_msg_field_begin(&i, md);
- for(; !upb_msg_field_done(&i); upb_msg_field_next(&i)) {
- const upb_fielddef *f = upb_msg_iter_field(&i);
- switch (upb_fielddef_type(f)) {
- case UPB_TYPE_ENUM:
- upb_handlers_setint32(h, f, putnull, &empty_attr);
- break;
- case UPB_TYPE_DOUBLE:
- upb_handlers_setdouble(h, f, putdouble, &empty_attr);
- break;
- case UPB_TYPE_STRING:
- upb_handlers_setstartstr(h, f, scalar_startstr_nokey, &empty_attr);
- upb_handlers_setstring(h, f, scalar_str, &empty_attr);
- upb_handlers_setendstr(h, f, scalar_endstr, &empty_attr);
- break;
- case UPB_TYPE_BOOL:
- upb_handlers_setbool(h, f, putbool, &empty_attr);
- break;
- case UPB_TYPE_MESSAGE:
- break;
- default:
- UPB_ASSERT(false);
- break;
- }
- }
- UPB_UNUSED(closure);
- }
- #define WRAPPER_SETHANDLERS(wrapper, type, putmethod) \
- void printer_sethandlers_##wrapper(const void *closure, upb_handlers *h) { \
- const upb_msgdef *md = upb_handlers_msgdef(h); \
- const upb_fielddef* f = upb_msgdef_itof(md, 1); \
- upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; \
- upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr); \
- upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr); \
- upb_handlers_set##type(h, f, putmethod, &empty_attr); \
- UPB_UNUSED(closure); \
- }
- WRAPPER_SETHANDLERS(doublevalue, double, putdouble)
- WRAPPER_SETHANDLERS(floatvalue, float, putfloat)
- WRAPPER_SETHANDLERS(int64value, int64, putint64_t)
- WRAPPER_SETHANDLERS(uint64value, uint64, putuint64_t)
- WRAPPER_SETHANDLERS(int32value, int32, putint32_t)
- WRAPPER_SETHANDLERS(uint32value, uint32, putuint32_t)
- WRAPPER_SETHANDLERS(boolvalue, bool, putbool)
- WRAPPER_SETHANDLERS(stringvalue, string, putstr_nokey)
- WRAPPER_SETHANDLERS(bytesvalue, string, putbytes)
- #undef WRAPPER_SETHANDLERS
- void printer_sethandlers_listvalue(const void *closure, upb_handlers *h) {
- const upb_msgdef *md = upb_handlers_msgdef(h);
- const upb_fielddef* f = upb_msgdef_itof(md, 1);
- upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER;
- upb_handlers_setstartseq(h, f, startseq_nokey, &empty_attr);
- upb_handlers_setendseq(h, f, endseq, &empty_attr);
- upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr);
- upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr);
- upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &empty_attr);
- UPB_UNUSED(closure);
- }
- void printer_sethandlers_structvalue(const void *closure, upb_handlers *h) {
- const upb_msgdef *md = upb_handlers_msgdef(h);
- const upb_fielddef* f = upb_msgdef_itof(md, 1);
- upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER;
- upb_handlers_setstartseq(h, f, startmap_nokey, &empty_attr);
- upb_handlers_setendseq(h, f, endmap, &empty_attr);
- upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr);
- upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr);
- upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &empty_attr);
- UPB_UNUSED(closure);
- }
- void printer_sethandlers(const void *closure, upb_handlers *h) {
- const upb_msgdef *md = upb_handlers_msgdef(h);
- bool is_mapentry = upb_msgdef_mapentry(md);
- upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER;
- upb_msg_field_iter i;
- const bool *preserve_fieldnames_ptr = closure;
- const bool preserve_fieldnames = *preserve_fieldnames_ptr;
- if (is_mapentry) {
- /* mapentry messages are sufficiently different that we handle them
- * separately. */
- printer_sethandlers_mapentry(closure, preserve_fieldnames, h);
- return;
- }
- switch (upb_msgdef_wellknowntype(md)) {
- case UPB_WELLKNOWN_UNSPECIFIED:
- break;
- case UPB_WELLKNOWN_ANY:
- printer_sethandlers_any(closure, h);
- return;
- case UPB_WELLKNOWN_FIELDMASK:
- printer_sethandlers_fieldmask(closure, h);
- return;
- case UPB_WELLKNOWN_DURATION:
- printer_sethandlers_duration(closure, h);
- return;
- case UPB_WELLKNOWN_TIMESTAMP:
- printer_sethandlers_timestamp(closure, h);
- return;
- case UPB_WELLKNOWN_VALUE:
- printer_sethandlers_value(closure, h);
- return;
- case UPB_WELLKNOWN_LISTVALUE:
- printer_sethandlers_listvalue(closure, h);
- return;
- case UPB_WELLKNOWN_STRUCT:
- printer_sethandlers_structvalue(closure, h);
- return;
- #define WRAPPER(wellknowntype, name) \
- case wellknowntype: \
- printer_sethandlers_##name(closure, h); \
- return; \
- WRAPPER(UPB_WELLKNOWN_DOUBLEVALUE, doublevalue);
- WRAPPER(UPB_WELLKNOWN_FLOATVALUE, floatvalue);
- WRAPPER(UPB_WELLKNOWN_INT64VALUE, int64value);
- WRAPPER(UPB_WELLKNOWN_UINT64VALUE, uint64value);
- WRAPPER(UPB_WELLKNOWN_INT32VALUE, int32value);
- WRAPPER(UPB_WELLKNOWN_UINT32VALUE, uint32value);
- WRAPPER(UPB_WELLKNOWN_BOOLVALUE, boolvalue);
- WRAPPER(UPB_WELLKNOWN_STRINGVALUE, stringvalue);
- WRAPPER(UPB_WELLKNOWN_BYTESVALUE, bytesvalue);
- #undef WRAPPER
- }
- upb_handlers_setstartmsg(h, printer_startmsg, &empty_attr);
- upb_handlers_setendmsg(h, printer_endmsg, &empty_attr);
- #define TYPE(type, name, ctype) \
- case type: \
- if (upb_fielddef_isseq(f)) { \
- upb_handlers_set##name(h, f, repeated_##ctype, &empty_attr); \
- } else { \
- upb_handlers_set##name(h, f, scalar_##ctype, &name_attr); \
- } \
- break;
- upb_msg_field_begin(&i, md);
- for(; !upb_msg_field_done(&i); upb_msg_field_next(&i)) {
- const upb_fielddef *f = upb_msg_iter_field(&i);
- upb_handlerattr name_attr = UPB_HANDLERATTR_INITIALIZER;
- upb_handlerattr_sethandlerdata(&name_attr,
- newstrpc(h, f, preserve_fieldnames));
- if (upb_fielddef_ismap(f)) {
- upb_handlers_setstartseq(h, f, startmap, &name_attr);
- upb_handlers_setendseq(h, f, endmap, &name_attr);
- } else if (upb_fielddef_isseq(f)) {
- upb_handlers_setstartseq(h, f, startseq, &name_attr);
- upb_handlers_setendseq(h, f, endseq, &empty_attr);
- }
- switch (upb_fielddef_type(f)) {
- TYPE(UPB_TYPE_FLOAT, float, float);
- TYPE(UPB_TYPE_DOUBLE, double, double);
- TYPE(UPB_TYPE_BOOL, bool, bool);
- TYPE(UPB_TYPE_INT32, int32, int32_t);
- TYPE(UPB_TYPE_UINT32, uint32, uint32_t);
- TYPE(UPB_TYPE_INT64, int64, int64_t);
- TYPE(UPB_TYPE_UINT64, uint64, uint64_t);
- case UPB_TYPE_ENUM: {
- /* For now, we always emit symbolic names for enums. We may want an
- * option later to control this behavior, but we will wait for a real
- * need first. */
- upb_handlerattr enum_attr = UPB_HANDLERATTR_INITIALIZER;
- set_enum_hd(h, f, preserve_fieldnames, &enum_attr);
- if (upb_fielddef_isseq(f)) {
- upb_handlers_setint32(h, f, repeated_enum, &enum_attr);
- } else {
- upb_handlers_setint32(h, f, scalar_enum, &enum_attr);
- }
- upb_handlerattr_uninit(&enum_attr);
- break;
- }
- case UPB_TYPE_STRING:
- if (upb_fielddef_isseq(f)) {
- upb_handlers_setstartstr(h, f, repeated_startstr, &empty_attr);
- upb_handlers_setstring(h, f, repeated_str, &empty_attr);
- upb_handlers_setendstr(h, f, repeated_endstr, &empty_attr);
- } else {
- upb_handlers_setstartstr(h, f, scalar_startstr, &name_attr);
- upb_handlers_setstring(h, f, scalar_str, &empty_attr);
- upb_handlers_setendstr(h, f, scalar_endstr, &empty_attr);
- }
- break;
- case UPB_TYPE_BYTES:
- /* XXX: this doesn't support strings that span buffers yet. The base64
- * encoder will need to be made resumable for this to work properly. */
- if (upb_fielddef_isseq(f)) {
- upb_handlers_setstring(h, f, repeated_bytes, &empty_attr);
- } else {
- upb_handlers_setstring(h, f, scalar_bytes, &name_attr);
- }
- break;
- case UPB_TYPE_MESSAGE:
- if (upb_fielddef_isseq(f)) {
- upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &name_attr);
- } else {
- upb_handlers_setstartsubmsg(h, f, scalar_startsubmsg, &name_attr);
- }
- break;
- }
- upb_handlerattr_uninit(&name_attr);
- }
- upb_handlerattr_uninit(&empty_attr);
- #undef TYPE
- }
- static void json_printer_reset(upb_json_printer *p) {
- p->depth_ = 0;
- }
- /* Public API *****************************************************************/
- upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h,
- upb_bytessink *output) {
- #ifndef NDEBUG
- size_t size_before = upb_env_bytesallocated(e);
- #endif
- upb_json_printer *p = upb_env_malloc(e, sizeof(upb_json_printer));
- if (!p) return NULL;
- p->output_ = output;
- json_printer_reset(p);
- upb_sink_reset(&p->input_, h, p);
- p->seconds = 0;
- p->nanos = 0;
- /* If this fails, increase the value in printer.h. */
- UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(e) - size_before <=
- UPB_JSON_PRINTER_SIZE);
- return p;
- }
- upb_sink *upb_json_printer_input(upb_json_printer *p) {
- return &p->input_;
- }
- const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
- bool preserve_fieldnames,
- const void *owner) {
- return upb_handlers_newfrozen(
- md, owner, printer_sethandlers, &preserve_fieldnames);
- }
- #undef UPB_SIZE
- #undef UPB_FIELD_AT
- #undef UPB_READ_ONEOF
- #undef UPB_WRITE_ONEOF
|