php-upb.c 265 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077
  1. /* Amalgamated source file */
  2. #include "php-upb.h"
  3. /*
  4. * This is where we define macros used across upb.
  5. *
  6. * All of these macros are undef'd in port_undef.inc to avoid leaking them to
  7. * users.
  8. *
  9. * The correct usage is:
  10. *
  11. * #include "upb/foobar.h"
  12. * #include "upb/baz.h"
  13. *
  14. * // MUST be last included header.
  15. * #include "upb/port_def.inc"
  16. *
  17. * // Code for this file.
  18. * // <...>
  19. *
  20. * // Can be omitted for .c files, required for .h.
  21. * #include "upb/port_undef.inc"
  22. *
  23. * This file is private and must not be included by users!
  24. */
  25. #include <stdint.h>
  26. #include <stddef.h>
  27. #if UINTPTR_MAX == 0xffffffff
  28. #define UPB_SIZE(size32, size64) size32
  29. #else
  30. #define UPB_SIZE(size32, size64) size64
  31. #endif
  32. /* If we always read/write as a consistent type to each address, this shouldn't
  33. * violate aliasing.
  34. */
  35. #define UPB_PTR_AT(msg, ofs, type) ((type*)((char*)(msg) + (ofs)))
  36. #define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \
  37. *UPB_PTR_AT(msg, case_offset, int) == case_val \
  38. ? *UPB_PTR_AT(msg, offset, fieldtype) \
  39. : default
  40. #define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \
  41. *UPB_PTR_AT(msg, case_offset, int) = case_val; \
  42. *UPB_PTR_AT(msg, offset, fieldtype) = value;
  43. #define UPB_MAPTYPE_STRING 0
  44. /* UPB_INLINE: inline if possible, emit standalone code if required. */
  45. #ifdef __cplusplus
  46. #define UPB_INLINE inline
  47. #elif defined (__GNUC__) || defined(__clang__)
  48. #define UPB_INLINE static __inline__
  49. #else
  50. #define UPB_INLINE static
  51. #endif
  52. #define UPB_ALIGN_UP(size, align) (((size) + (align) - 1) / (align) * (align))
  53. #define UPB_ALIGN_DOWN(size, align) ((size) / (align) * (align))
  54. #define UPB_ALIGN_MALLOC(size) UPB_ALIGN_UP(size, 16)
  55. #define UPB_ALIGN_OF(type) offsetof (struct { char c; type member; }, member)
  56. /* Hints to the compiler about likely/unlikely branches. */
  57. #if defined (__GNUC__) || defined(__clang__)
  58. #define UPB_LIKELY(x) __builtin_expect((x),1)
  59. #define UPB_UNLIKELY(x) __builtin_expect((x),0)
  60. #else
  61. #define UPB_LIKELY(x) (x)
  62. #define UPB_UNLIKELY(x) (x)
  63. #endif
  64. /* Define UPB_BIG_ENDIAN manually if you're on big endian and your compiler
  65. * doesn't provide these preprocessor symbols. */
  66. #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  67. #define UPB_BIG_ENDIAN
  68. #endif
  69. /* Macros for function attributes on compilers that support them. */
  70. #ifdef __GNUC__
  71. #define UPB_FORCEINLINE __inline__ __attribute__((always_inline))
  72. #define UPB_NOINLINE __attribute__((noinline))
  73. #define UPB_NORETURN __attribute__((__noreturn__))
  74. #else /* !defined(__GNUC__) */
  75. #define UPB_FORCEINLINE
  76. #define UPB_NOINLINE
  77. #define UPB_NORETURN
  78. #endif
  79. #if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L
  80. /* C99/C++11 versions. */
  81. #include <stdio.h>
  82. #define _upb_snprintf snprintf
  83. #define _upb_vsnprintf vsnprintf
  84. #define _upb_va_copy(a, b) va_copy(a, b)
  85. #elif defined(_MSC_VER)
  86. /* Microsoft C/C++ versions. */
  87. #include <stdarg.h>
  88. #include <stdio.h>
  89. #if _MSC_VER < 1900
  90. int msvc_snprintf(char* s, size_t n, const char* format, ...);
  91. int msvc_vsnprintf(char* s, size_t n, const char* format, va_list arg);
  92. #define UPB_MSVC_VSNPRINTF
  93. #define _upb_snprintf msvc_snprintf
  94. #define _upb_vsnprintf msvc_vsnprintf
  95. #else
  96. #define _upb_snprintf snprintf
  97. #define _upb_vsnprintf vsnprintf
  98. #endif
  99. #define _upb_va_copy(a, b) va_copy(a, b)
  100. #elif defined __GNUC__
  101. /* A few hacky workarounds for functions not in C89.
  102. * For internal use only!
  103. * TODO(haberman): fix these by including our own implementations, or finding
  104. * another workaround.
  105. */
  106. #define _upb_snprintf __builtin_snprintf
  107. #define _upb_vsnprintf __builtin_vsnprintf
  108. #define _upb_va_copy(a, b) __va_copy(a, b)
  109. #else
  110. #error Need implementations of [v]snprintf and va_copy
  111. #endif
  112. #ifdef __cplusplus
  113. #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || \
  114. (defined(_MSC_VER) && _MSC_VER >= 1900)
  115. /* C++11 is present */
  116. #else
  117. #error upb requires C++11 for C++ support
  118. #endif
  119. #endif
  120. #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
  121. #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
  122. #define UPB_UNUSED(var) (void)var
  123. /* UPB_ASSUME(): in release mode, we tell the compiler to assume this is true.
  124. */
  125. #ifdef NDEBUG
  126. #ifdef __GNUC__
  127. #define UPB_ASSUME(expr) if (!(expr)) __builtin_unreachable()
  128. #elif defined _MSC_VER
  129. #define UPB_ASSUME(expr) if (!(expr)) __assume(0)
  130. #else
  131. #define UPB_ASSUME(expr) do {} if (false && (expr))
  132. #endif
  133. #else
  134. #define UPB_ASSUME(expr) assert(expr)
  135. #endif
  136. /* UPB_ASSERT(): in release mode, we use the expression without letting it be
  137. * evaluated. This prevents "unused variable" warnings. */
  138. #ifdef NDEBUG
  139. #define UPB_ASSERT(expr) do {} while (false && (expr))
  140. #else
  141. #define UPB_ASSERT(expr) assert(expr)
  142. #endif
  143. /* UPB_ASSERT_DEBUGVAR(): assert that uses functions or variables that only
  144. * exist in debug mode. This turns into regular assert. */
  145. #define UPB_ASSERT_DEBUGVAR(expr) assert(expr)
  146. #if defined(__GNUC__) || defined(__clang__)
  147. #define UPB_UNREACHABLE() do { assert(0); __builtin_unreachable(); } while(0)
  148. #else
  149. #define UPB_UNREACHABLE() do { assert(0); } while(0)
  150. #endif
  151. /* UPB_INFINITY representing floating-point positive infinity. */
  152. #include <math.h>
  153. #ifdef INFINITY
  154. #define UPB_INFINITY INFINITY
  155. #else
  156. #define UPB_INFINITY (1.0 / 0.0)
  157. #endif
  158. #include <setjmp.h>
  159. #include <string.h>
  160. /* Maps descriptor type -> upb field type. */
  161. static const uint8_t desctype_to_fieldtype[] = {
  162. -1, /* invalid descriptor type */
  163. UPB_TYPE_DOUBLE, /* DOUBLE */
  164. UPB_TYPE_FLOAT, /* FLOAT */
  165. UPB_TYPE_INT64, /* INT64 */
  166. UPB_TYPE_UINT64, /* UINT64 */
  167. UPB_TYPE_INT32, /* INT32 */
  168. UPB_TYPE_UINT64, /* FIXED64 */
  169. UPB_TYPE_UINT32, /* FIXED32 */
  170. UPB_TYPE_BOOL, /* BOOL */
  171. UPB_TYPE_STRING, /* STRING */
  172. UPB_TYPE_MESSAGE, /* GROUP */
  173. UPB_TYPE_MESSAGE, /* MESSAGE */
  174. UPB_TYPE_BYTES, /* BYTES */
  175. UPB_TYPE_UINT32, /* UINT32 */
  176. UPB_TYPE_ENUM, /* ENUM */
  177. UPB_TYPE_INT32, /* SFIXED32 */
  178. UPB_TYPE_INT64, /* SFIXED64 */
  179. UPB_TYPE_INT32, /* SINT32 */
  180. UPB_TYPE_INT64, /* SINT64 */
  181. };
  182. /* Maps descriptor type -> upb map size. */
  183. static const uint8_t desctype_to_mapsize[] = {
  184. -1, /* invalid descriptor type */
  185. 8, /* DOUBLE */
  186. 4, /* FLOAT */
  187. 8, /* INT64 */
  188. 8, /* UINT64 */
  189. 4, /* INT32 */
  190. 8, /* FIXED64 */
  191. 4, /* FIXED32 */
  192. 1, /* BOOL */
  193. UPB_MAPTYPE_STRING, /* STRING */
  194. sizeof(void *), /* GROUP */
  195. sizeof(void *), /* MESSAGE */
  196. UPB_MAPTYPE_STRING, /* BYTES */
  197. 4, /* UINT32 */
  198. 4, /* ENUM */
  199. 4, /* SFIXED32 */
  200. 8, /* SFIXED64 */
  201. 4, /* SINT32 */
  202. 8, /* SINT64 */
  203. };
  204. static const unsigned fixed32_ok = (1 << UPB_DTYPE_FLOAT) |
  205. (1 << UPB_DTYPE_FIXED32) |
  206. (1 << UPB_DTYPE_SFIXED32);
  207. static const unsigned fixed64_ok = (1 << UPB_DTYPE_DOUBLE) |
  208. (1 << UPB_DTYPE_FIXED64) |
  209. (1 << UPB_DTYPE_SFIXED64);
  210. /* Op: an action to be performed for a wire-type/field-type combination. */
  211. #define OP_SCALAR_LG2(n) (n)
  212. #define OP_FIXPCK_LG2(n) (n + 4)
  213. #define OP_VARPCK_LG2(n) (n + 8)
  214. #define OP_STRING 4
  215. #define OP_SUBMSG 5
  216. static const int8_t varint_ops[19] = {
  217. -1, /* field not found */
  218. -1, /* DOUBLE */
  219. -1, /* FLOAT */
  220. OP_SCALAR_LG2(3), /* INT64 */
  221. OP_SCALAR_LG2(3), /* UINT64 */
  222. OP_SCALAR_LG2(2), /* INT32 */
  223. -1, /* FIXED64 */
  224. -1, /* FIXED32 */
  225. OP_SCALAR_LG2(0), /* BOOL */
  226. -1, /* STRING */
  227. -1, /* GROUP */
  228. -1, /* MESSAGE */
  229. -1, /* BYTES */
  230. OP_SCALAR_LG2(2), /* UINT32 */
  231. OP_SCALAR_LG2(2), /* ENUM */
  232. -1, /* SFIXED32 */
  233. -1, /* SFIXED64 */
  234. OP_SCALAR_LG2(2), /* SINT32 */
  235. OP_SCALAR_LG2(3), /* SINT64 */
  236. };
  237. static const int8_t delim_ops[37] = {
  238. /* For non-repeated field type. */
  239. -1, /* field not found */
  240. -1, /* DOUBLE */
  241. -1, /* FLOAT */
  242. -1, /* INT64 */
  243. -1, /* UINT64 */
  244. -1, /* INT32 */
  245. -1, /* FIXED64 */
  246. -1, /* FIXED32 */
  247. -1, /* BOOL */
  248. OP_STRING, /* STRING */
  249. -1, /* GROUP */
  250. OP_SUBMSG, /* MESSAGE */
  251. OP_STRING, /* BYTES */
  252. -1, /* UINT32 */
  253. -1, /* ENUM */
  254. -1, /* SFIXED32 */
  255. -1, /* SFIXED64 */
  256. -1, /* SINT32 */
  257. -1, /* SINT64 */
  258. /* For repeated field type. */
  259. OP_FIXPCK_LG2(3), /* REPEATED DOUBLE */
  260. OP_FIXPCK_LG2(2), /* REPEATED FLOAT */
  261. OP_VARPCK_LG2(3), /* REPEATED INT64 */
  262. OP_VARPCK_LG2(3), /* REPEATED UINT64 */
  263. OP_VARPCK_LG2(2), /* REPEATED INT32 */
  264. OP_FIXPCK_LG2(3), /* REPEATED FIXED64 */
  265. OP_FIXPCK_LG2(2), /* REPEATED FIXED32 */
  266. OP_VARPCK_LG2(0), /* REPEATED BOOL */
  267. OP_STRING, /* REPEATED STRING */
  268. OP_SUBMSG, /* REPEATED GROUP */
  269. OP_SUBMSG, /* REPEATED MESSAGE */
  270. OP_STRING, /* REPEATED BYTES */
  271. OP_VARPCK_LG2(2), /* REPEATED UINT32 */
  272. OP_VARPCK_LG2(2), /* REPEATED ENUM */
  273. OP_FIXPCK_LG2(2), /* REPEATED SFIXED32 */
  274. OP_FIXPCK_LG2(3), /* REPEATED SFIXED64 */
  275. OP_VARPCK_LG2(2), /* REPEATED SINT32 */
  276. OP_VARPCK_LG2(3), /* REPEATED SINT64 */
  277. };
  278. /* Data pertaining to the parse. */
  279. typedef struct {
  280. const char *limit; /* End of delimited region or end of buffer. */
  281. upb_arena *arena;
  282. int depth;
  283. uint32_t end_group; /* Set to field number of END_GROUP tag, if any. */
  284. jmp_buf err;
  285. } upb_decstate;
  286. typedef union {
  287. bool bool_val;
  288. int32_t int32_val;
  289. int64_t int64_val;
  290. uint32_t uint32_val;
  291. uint64_t uint64_val;
  292. upb_strview str_val;
  293. } wireval;
  294. static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
  295. const upb_msglayout *layout);
  296. UPB_NORETURN static void decode_err(upb_decstate *d) { longjmp(d->err, 1); }
  297. static bool decode_reserve(upb_decstate *d, upb_array *arr, size_t elem) {
  298. bool need_realloc = arr->size - arr->len < elem;
  299. if (need_realloc && !_upb_array_realloc(arr, arr->len + elem, d->arena)) {
  300. decode_err(d);
  301. }
  302. return need_realloc;
  303. }
  304. UPB_NOINLINE
  305. static const char *decode_longvarint64(upb_decstate *d, const char *ptr,
  306. const char *limit, uint64_t *val) {
  307. uint8_t byte;
  308. int bitpos = 0;
  309. uint64_t out = 0;
  310. do {
  311. if (bitpos >= 70 || ptr == limit) decode_err(d);
  312. byte = *ptr;
  313. out |= (uint64_t)(byte & 0x7F) << bitpos;
  314. ptr++;
  315. bitpos += 7;
  316. } while (byte & 0x80);
  317. *val = out;
  318. return ptr;
  319. }
  320. UPB_FORCEINLINE
  321. static const char *decode_varint64(upb_decstate *d, const char *ptr,
  322. const char *limit, uint64_t *val) {
  323. if (UPB_LIKELY(ptr < limit && (*ptr & 0x80) == 0)) {
  324. *val = (uint8_t)*ptr;
  325. return ptr + 1;
  326. } else {
  327. return decode_longvarint64(d, ptr, limit, val);
  328. }
  329. }
  330. static const char *decode_varint32(upb_decstate *d, const char *ptr,
  331. const char *limit, uint32_t *val) {
  332. uint64_t u64;
  333. ptr = decode_varint64(d, ptr, limit, &u64);
  334. if (u64 > UINT32_MAX) decode_err(d);
  335. *val = (uint32_t)u64;
  336. return ptr;
  337. }
  338. static void decode_munge(int type, wireval *val) {
  339. switch (type) {
  340. case UPB_DESCRIPTOR_TYPE_BOOL:
  341. val->bool_val = val->uint64_val != 0;
  342. break;
  343. case UPB_DESCRIPTOR_TYPE_SINT32: {
  344. uint32_t n = val->uint32_val;
  345. val->int32_val = (n >> 1) ^ -(int32_t)(n & 1);
  346. break;
  347. }
  348. case UPB_DESCRIPTOR_TYPE_SINT64: {
  349. uint64_t n = val->uint64_val;
  350. val->int64_val = (n >> 1) ^ -(int64_t)(n & 1);
  351. break;
  352. }
  353. }
  354. }
  355. static const upb_msglayout_field *upb_find_field(const upb_msglayout *l,
  356. uint32_t field_number) {
  357. static upb_msglayout_field none = {0};
  358. /* Lots of optimization opportunities here. */
  359. int i;
  360. if (l == NULL) return &none;
  361. for (i = 0; i < l->field_count; i++) {
  362. if (l->fields[i].number == field_number) {
  363. return &l->fields[i];
  364. }
  365. }
  366. return &none; /* Unknown field. */
  367. }
  368. static upb_msg *decode_newsubmsg(upb_decstate *d, const upb_msglayout *layout,
  369. const upb_msglayout_field *field) {
  370. const upb_msglayout *subl = layout->submsgs[field->submsg_index];
  371. return _upb_msg_new(subl, d->arena);
  372. }
  373. static void decode_tosubmsg(upb_decstate *d, upb_msg *submsg,
  374. const upb_msglayout *layout,
  375. const upb_msglayout_field *field, upb_strview val) {
  376. const upb_msglayout *subl = layout->submsgs[field->submsg_index];
  377. const char *saved_limit = d->limit;
  378. if (--d->depth < 0) decode_err(d);
  379. d->limit = val.data + val.size;
  380. decode_msg(d, val.data, submsg, subl);
  381. d->limit = saved_limit;
  382. if (d->end_group != 0) decode_err(d);
  383. d->depth++;
  384. }
  385. static const char *decode_group(upb_decstate *d, const char *ptr,
  386. upb_msg *submsg, const upb_msglayout *subl,
  387. uint32_t number) {
  388. if (--d->depth < 0) decode_err(d);
  389. ptr = decode_msg(d, ptr, submsg, subl);
  390. if (d->end_group != number) decode_err(d);
  391. d->end_group = 0;
  392. d->depth++;
  393. return ptr;
  394. }
  395. static const char *decode_togroup(upb_decstate *d, const char *ptr,
  396. upb_msg *submsg, const upb_msglayout *layout,
  397. const upb_msglayout_field *field) {
  398. const upb_msglayout *subl = layout->submsgs[field->submsg_index];
  399. return decode_group(d, ptr, submsg, subl, field->number);
  400. }
  401. static const char *decode_toarray(upb_decstate *d, const char *ptr,
  402. upb_msg *msg, const upb_msglayout *layout,
  403. const upb_msglayout_field *field, wireval val,
  404. int op) {
  405. upb_array **arrp = UPB_PTR_AT(msg, field->offset, void);
  406. upb_array *arr = *arrp;
  407. void *mem;
  408. if (!arr) {
  409. upb_fieldtype_t type = desctype_to_fieldtype[field->descriptortype];
  410. arr = _upb_array_new(d->arena, type);
  411. if (!arr) decode_err(d);
  412. *arrp = arr;
  413. }
  414. decode_reserve(d, arr, 1);
  415. switch (op) {
  416. case OP_SCALAR_LG2(0):
  417. case OP_SCALAR_LG2(2):
  418. case OP_SCALAR_LG2(3):
  419. /* Append scalar value. */
  420. mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << op, void);
  421. arr->len++;
  422. memcpy(mem, &val, 1 << op);
  423. return ptr;
  424. case OP_STRING:
  425. /* Append string. */
  426. mem =
  427. UPB_PTR_AT(_upb_array_ptr(arr), arr->len * sizeof(upb_strview), void);
  428. arr->len++;
  429. memcpy(mem, &val, sizeof(upb_strview));
  430. return ptr;
  431. case OP_SUBMSG: {
  432. /* Append submessage / group. */
  433. upb_msg *submsg = decode_newsubmsg(d, layout, field);
  434. *UPB_PTR_AT(_upb_array_ptr(arr), arr->len * sizeof(void *), upb_msg *) =
  435. submsg;
  436. arr->len++;
  437. if (UPB_UNLIKELY(field->descriptortype == UPB_DTYPE_GROUP)) {
  438. ptr = decode_togroup(d, ptr, submsg, layout, field);
  439. } else {
  440. decode_tosubmsg(d, submsg, layout, field, val.str_val);
  441. }
  442. return ptr;
  443. }
  444. case OP_FIXPCK_LG2(2):
  445. case OP_FIXPCK_LG2(3): {
  446. /* Fixed packed. */
  447. int lg2 = op - OP_FIXPCK_LG2(0);
  448. int mask = (1 << lg2) - 1;
  449. size_t count = val.str_val.size >> lg2;
  450. if ((val.str_val.size & mask) != 0) {
  451. decode_err(d); /* Length isn't a round multiple of elem size. */
  452. }
  453. decode_reserve(d, arr, count);
  454. mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
  455. arr->len += count;
  456. memcpy(mem, val.str_val.data, val.str_val.size);
  457. return ptr;
  458. }
  459. case OP_VARPCK_LG2(0):
  460. case OP_VARPCK_LG2(2):
  461. case OP_VARPCK_LG2(3): {
  462. /* Varint packed. */
  463. int lg2 = op - OP_VARPCK_LG2(0);
  464. int scale = 1 << lg2;
  465. const char *ptr = val.str_val.data;
  466. const char *end = ptr + val.str_val.size;
  467. char *out = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
  468. while (ptr < end) {
  469. wireval elem;
  470. ptr = decode_varint64(d, ptr, end, &elem.uint64_val);
  471. decode_munge(field->descriptortype, &elem);
  472. if (decode_reserve(d, arr, 1)) {
  473. out = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
  474. }
  475. arr->len++;
  476. memcpy(out, &elem, scale);
  477. out += scale;
  478. }
  479. if (ptr != end) decode_err(d);
  480. return ptr;
  481. }
  482. default:
  483. UPB_UNREACHABLE();
  484. }
  485. }
  486. static void decode_tomap(upb_decstate *d, upb_msg *msg,
  487. const upb_msglayout *layout,
  488. const upb_msglayout_field *field, wireval val) {
  489. upb_map **map_p = UPB_PTR_AT(msg, field->offset, upb_map *);
  490. upb_map *map = *map_p;
  491. upb_map_entry ent;
  492. const upb_msglayout *entry = layout->submsgs[field->submsg_index];
  493. if (!map) {
  494. /* Lazily create map. */
  495. const upb_msglayout *entry = layout->submsgs[field->submsg_index];
  496. const upb_msglayout_field *key_field = &entry->fields[0];
  497. const upb_msglayout_field *val_field = &entry->fields[1];
  498. char key_size = desctype_to_mapsize[key_field->descriptortype];
  499. char val_size = desctype_to_mapsize[val_field->descriptortype];
  500. UPB_ASSERT(key_field->offset == 0);
  501. UPB_ASSERT(val_field->offset == sizeof(upb_strview));
  502. map = _upb_map_new(d->arena, key_size, val_size);
  503. *map_p = map;
  504. }
  505. /* Parse map entry. */
  506. memset(&ent, 0, sizeof(ent));
  507. if (entry->fields[1].descriptortype == UPB_DESCRIPTOR_TYPE_MESSAGE ||
  508. entry->fields[1].descriptortype == UPB_DESCRIPTOR_TYPE_GROUP) {
  509. /* Create proactively to handle the case where it doesn't appear. */
  510. ent.v.val.val = (uint64_t)_upb_msg_new(entry->submsgs[0], d->arena);
  511. }
  512. decode_tosubmsg(d, &ent.k, layout, field, val.str_val);
  513. /* Insert into map. */
  514. _upb_map_set(map, &ent.k, map->key_size, &ent.v, map->val_size, d->arena);
  515. }
  516. static const char *decode_tomsg(upb_decstate *d, const char *ptr, upb_msg *msg,
  517. const upb_msglayout *layout,
  518. const upb_msglayout_field *field, wireval val,
  519. int op) {
  520. void *mem = UPB_PTR_AT(msg, field->offset, void);
  521. int type = field->descriptortype;
  522. /* Set presence if necessary. */
  523. if (field->presence < 0) {
  524. /* Oneof case */
  525. uint32_t *oneof_case = _upb_oneofcase_field(msg, field);
  526. if (op == OP_SUBMSG && *oneof_case != field->number) {
  527. memset(mem, 0, sizeof(void*));
  528. }
  529. *oneof_case = field->number;
  530. } else if (field->presence > 0) {
  531. _upb_sethas_field(msg, field);
  532. }
  533. /* Store into message. */
  534. switch (op) {
  535. case OP_SUBMSG: {
  536. upb_msg **submsgp = mem;
  537. upb_msg *submsg = *submsgp;
  538. if (!submsg) {
  539. submsg = decode_newsubmsg(d, layout, field);
  540. *submsgp = submsg;
  541. }
  542. if (UPB_UNLIKELY(type == UPB_DTYPE_GROUP)) {
  543. ptr = decode_togroup(d, ptr, submsg, layout, field);
  544. } else {
  545. decode_tosubmsg(d, submsg, layout, field, val.str_val);
  546. }
  547. break;
  548. }
  549. case OP_STRING:
  550. memcpy(mem, &val, sizeof(upb_strview));
  551. break;
  552. case OP_SCALAR_LG2(3):
  553. memcpy(mem, &val, 8);
  554. break;
  555. case OP_SCALAR_LG2(2):
  556. memcpy(mem, &val, 4);
  557. break;
  558. case OP_SCALAR_LG2(0):
  559. memcpy(mem, &val, 1);
  560. break;
  561. default:
  562. UPB_UNREACHABLE();
  563. }
  564. return ptr;
  565. }
  566. static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
  567. const upb_msglayout *layout) {
  568. while (ptr < d->limit) {
  569. uint32_t tag;
  570. const upb_msglayout_field *field;
  571. int field_number;
  572. int wire_type;
  573. const char *field_start = ptr;
  574. wireval val;
  575. int op;
  576. ptr = decode_varint32(d, ptr, d->limit, &tag);
  577. field_number = tag >> 3;
  578. wire_type = tag & 7;
  579. field = upb_find_field(layout, field_number);
  580. switch (wire_type) {
  581. case UPB_WIRE_TYPE_VARINT:
  582. ptr = decode_varint64(d, ptr, d->limit, &val.uint64_val);
  583. op = varint_ops[field->descriptortype];
  584. decode_munge(field->descriptortype, &val);
  585. break;
  586. case UPB_WIRE_TYPE_32BIT:
  587. if (d->limit - ptr < 4) decode_err(d);
  588. memcpy(&val, ptr, 4);
  589. ptr += 4;
  590. op = OP_SCALAR_LG2(2);
  591. if (((1 << field->descriptortype) & fixed32_ok) == 0) goto unknown;
  592. break;
  593. case UPB_WIRE_TYPE_64BIT:
  594. if (d->limit - ptr < 8) decode_err(d);
  595. memcpy(&val, ptr, 8);
  596. ptr += 8;
  597. op = OP_SCALAR_LG2(3);
  598. if (((1 << field->descriptortype) & fixed64_ok) == 0) goto unknown;
  599. break;
  600. case UPB_WIRE_TYPE_DELIMITED: {
  601. uint32_t size;
  602. int ndx = field->descriptortype;
  603. if (_upb_isrepeated(field)) ndx += 18;
  604. ptr = decode_varint32(d, ptr, d->limit, &size);
  605. if (size >= INT32_MAX || (size_t)(d->limit - ptr) < size) {
  606. decode_err(d); /* Length overflow. */
  607. }
  608. val.str_val.data = ptr;
  609. val.str_val.size = size;
  610. ptr += size;
  611. op = delim_ops[ndx];
  612. break;
  613. }
  614. case UPB_WIRE_TYPE_START_GROUP:
  615. val.int32_val = field_number;
  616. op = OP_SUBMSG;
  617. if (field->descriptortype != UPB_DTYPE_GROUP) goto unknown;
  618. break;
  619. case UPB_WIRE_TYPE_END_GROUP:
  620. d->end_group = field_number;
  621. return ptr;
  622. default:
  623. decode_err(d);
  624. }
  625. if (op >= 0) {
  626. /* Parse, using op for dispatch. */
  627. switch (field->label) {
  628. case UPB_LABEL_REPEATED:
  629. case _UPB_LABEL_PACKED:
  630. ptr = decode_toarray(d, ptr, msg, layout, field, val, op);
  631. break;
  632. case _UPB_LABEL_MAP:
  633. decode_tomap(d, msg, layout, field, val);
  634. break;
  635. default:
  636. ptr = decode_tomsg(d, ptr, msg, layout, field, val, op);
  637. break;
  638. }
  639. } else {
  640. unknown:
  641. /* Skip unknown field. */
  642. if (field_number == 0) decode_err(d);
  643. if (wire_type == UPB_WIRE_TYPE_START_GROUP) {
  644. ptr = decode_group(d, ptr, NULL, NULL, field_number);
  645. }
  646. if (msg) {
  647. if (!_upb_msg_addunknown(msg, field_start, ptr - field_start,
  648. d->arena)) {
  649. decode_err(d);
  650. }
  651. }
  652. }
  653. }
  654. if (ptr != d->limit) decode_err(d);
  655. return ptr;
  656. }
  657. bool upb_decode(const char *buf, size_t size, void *msg, const upb_msglayout *l,
  658. upb_arena *arena) {
  659. upb_decstate state;
  660. state.limit = buf + size;
  661. state.arena = arena;
  662. state.depth = 64;
  663. state.end_group = 0;
  664. if (setjmp(state.err)) return false;
  665. if (size == 0) return true;
  666. decode_msg(&state, buf, msg, l);
  667. return state.end_group == 0;
  668. }
  669. #undef OP_SCALAR_LG2
  670. #undef OP_FIXPCK_LG2
  671. #undef OP_VARPCK_LG2
  672. #undef OP_STRING
  673. #undef OP_SUBMSG
  674. /* We encode backwards, to avoid pre-computing lengths (one-pass encode). */
  675. #include <string.h>
  676. #define UPB_PB_VARINT_MAX_LEN 10
  677. #define CHK(x) do { if (!(x)) { return false; } } while(0)
  678. static size_t upb_encode_varint(uint64_t val, char *buf) {
  679. size_t i;
  680. if (val < 128) { buf[0] = val; return 1; }
  681. i = 0;
  682. while (val) {
  683. uint8_t byte = val & 0x7fU;
  684. val >>= 7;
  685. if (val) byte |= 0x80U;
  686. buf[i++] = byte;
  687. }
  688. return i;
  689. }
  690. static uint32_t upb_zzencode_32(int32_t n) { return ((uint32_t)n << 1) ^ (n >> 31); }
  691. static uint64_t upb_zzencode_64(int64_t n) { return ((uint64_t)n << 1) ^ (n >> 63); }
  692. typedef struct {
  693. upb_alloc *alloc;
  694. char *buf, *ptr, *limit;
  695. } upb_encstate;
  696. static size_t upb_roundup_pow2(size_t bytes) {
  697. size_t ret = 128;
  698. while (ret < bytes) {
  699. ret *= 2;
  700. }
  701. return ret;
  702. }
  703. static bool upb_encode_growbuffer(upb_encstate *e, size_t bytes) {
  704. size_t old_size = e->limit - e->buf;
  705. size_t new_size = upb_roundup_pow2(bytes + (e->limit - e->ptr));
  706. char *new_buf = upb_realloc(e->alloc, e->buf, old_size, new_size);
  707. CHK(new_buf);
  708. /* We want previous data at the end, realloc() put it at the beginning. */
  709. if (old_size > 0) {
  710. memmove(new_buf + new_size - old_size, e->buf, old_size);
  711. }
  712. e->ptr = new_buf + new_size - (e->limit - e->ptr);
  713. e->limit = new_buf + new_size;
  714. e->buf = new_buf;
  715. return true;
  716. }
  717. /* Call to ensure that at least "bytes" bytes are available for writing at
  718. * e->ptr. Returns false if the bytes could not be allocated. */
  719. static bool upb_encode_reserve(upb_encstate *e, size_t bytes) {
  720. CHK(UPB_LIKELY((size_t)(e->ptr - e->buf) >= bytes) ||
  721. upb_encode_growbuffer(e, bytes));
  722. e->ptr -= bytes;
  723. return true;
  724. }
  725. /* Writes the given bytes to the buffer, handling reserve/advance. */
  726. static bool upb_put_bytes(upb_encstate *e, const void *data, size_t len) {
  727. if (len == 0) return true;
  728. CHK(upb_encode_reserve(e, len));
  729. memcpy(e->ptr, data, len);
  730. return true;
  731. }
  732. static bool upb_put_fixed64(upb_encstate *e, uint64_t val) {
  733. /* TODO(haberman): byte-swap for big endian. */
  734. return upb_put_bytes(e, &val, sizeof(uint64_t));
  735. }
  736. static bool upb_put_fixed32(upb_encstate *e, uint32_t val) {
  737. /* TODO(haberman): byte-swap for big endian. */
  738. return upb_put_bytes(e, &val, sizeof(uint32_t));
  739. }
  740. static bool upb_put_varint(upb_encstate *e, uint64_t val) {
  741. size_t len;
  742. char *start;
  743. CHK(upb_encode_reserve(e, UPB_PB_VARINT_MAX_LEN));
  744. len = upb_encode_varint(val, e->ptr);
  745. start = e->ptr + UPB_PB_VARINT_MAX_LEN - len;
  746. memmove(start, e->ptr, len);
  747. e->ptr = start;
  748. return true;
  749. }
  750. static bool upb_put_double(upb_encstate *e, double d) {
  751. uint64_t u64;
  752. UPB_ASSERT(sizeof(double) == sizeof(uint64_t));
  753. memcpy(&u64, &d, sizeof(uint64_t));
  754. return upb_put_fixed64(e, u64);
  755. }
  756. static bool upb_put_float(upb_encstate *e, float d) {
  757. uint32_t u32;
  758. UPB_ASSERT(sizeof(float) == sizeof(uint32_t));
  759. memcpy(&u32, &d, sizeof(uint32_t));
  760. return upb_put_fixed32(e, u32);
  761. }
  762. static bool upb_put_tag(upb_encstate *e, int field_number, int wire_type) {
  763. return upb_put_varint(e, (field_number << 3) | wire_type);
  764. }
  765. static bool upb_put_fixedarray(upb_encstate *e, const upb_array *arr,
  766. size_t elem_size, uint32_t tag) {
  767. size_t bytes = arr->len * elem_size;
  768. const char* data = _upb_array_constptr(arr);
  769. const char* ptr = data + bytes - elem_size;
  770. if (tag) {
  771. while (true) {
  772. CHK(upb_put_bytes(e, ptr, elem_size) && upb_put_varint(e, tag));
  773. if (ptr == data) break;
  774. ptr -= elem_size;
  775. }
  776. return true;
  777. } else {
  778. return upb_put_bytes(e, data, bytes) && upb_put_varint(e, bytes);
  779. }
  780. }
  781. bool upb_encode_message(upb_encstate *e, const char *msg,
  782. const upb_msglayout *m, size_t *size);
  783. static bool upb_encode_scalarfield(upb_encstate *e, const void *_field_mem,
  784. const upb_msglayout *m,
  785. const upb_msglayout_field *f,
  786. bool skip_zero_value) {
  787. const char *field_mem = _field_mem;
  788. #define CASE(ctype, type, wire_type, encodeval) do { \
  789. ctype val = *(ctype*)field_mem; \
  790. if (skip_zero_value && val == 0) { \
  791. return true; \
  792. } \
  793. return upb_put_ ## type(e, encodeval) && \
  794. upb_put_tag(e, f->number, wire_type); \
  795. } while(0)
  796. switch (f->descriptortype) {
  797. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  798. CASE(double, double, UPB_WIRE_TYPE_64BIT, val);
  799. case UPB_DESCRIPTOR_TYPE_FLOAT:
  800. CASE(float, float, UPB_WIRE_TYPE_32BIT, val);
  801. case UPB_DESCRIPTOR_TYPE_INT64:
  802. case UPB_DESCRIPTOR_TYPE_UINT64:
  803. CASE(uint64_t, varint, UPB_WIRE_TYPE_VARINT, val);
  804. case UPB_DESCRIPTOR_TYPE_UINT32:
  805. CASE(uint32_t, varint, UPB_WIRE_TYPE_VARINT, val);
  806. case UPB_DESCRIPTOR_TYPE_INT32:
  807. case UPB_DESCRIPTOR_TYPE_ENUM:
  808. CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, (int64_t)val);
  809. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  810. case UPB_DESCRIPTOR_TYPE_FIXED64:
  811. CASE(uint64_t, fixed64, UPB_WIRE_TYPE_64BIT, val);
  812. case UPB_DESCRIPTOR_TYPE_FIXED32:
  813. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  814. CASE(uint32_t, fixed32, UPB_WIRE_TYPE_32BIT, val);
  815. case UPB_DESCRIPTOR_TYPE_BOOL:
  816. CASE(bool, varint, UPB_WIRE_TYPE_VARINT, val);
  817. case UPB_DESCRIPTOR_TYPE_SINT32:
  818. CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_32(val));
  819. case UPB_DESCRIPTOR_TYPE_SINT64:
  820. CASE(int64_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_64(val));
  821. case UPB_DESCRIPTOR_TYPE_STRING:
  822. case UPB_DESCRIPTOR_TYPE_BYTES: {
  823. upb_strview view = *(upb_strview*)field_mem;
  824. if (skip_zero_value && view.size == 0) {
  825. return true;
  826. }
  827. return upb_put_bytes(e, view.data, view.size) &&
  828. upb_put_varint(e, view.size) &&
  829. upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
  830. }
  831. case UPB_DESCRIPTOR_TYPE_GROUP: {
  832. size_t size;
  833. void *submsg = *(void **)field_mem;
  834. const upb_msglayout *subm = m->submsgs[f->submsg_index];
  835. if (submsg == NULL) {
  836. return true;
  837. }
  838. return upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
  839. upb_encode_message(e, submsg, subm, &size) &&
  840. upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP);
  841. }
  842. case UPB_DESCRIPTOR_TYPE_MESSAGE: {
  843. size_t size;
  844. void *submsg = *(void **)field_mem;
  845. const upb_msglayout *subm = m->submsgs[f->submsg_index];
  846. if (submsg == NULL) {
  847. return true;
  848. }
  849. return upb_encode_message(e, submsg, subm, &size) &&
  850. upb_put_varint(e, size) &&
  851. upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
  852. }
  853. }
  854. #undef CASE
  855. UPB_UNREACHABLE();
  856. }
  857. static bool upb_encode_array(upb_encstate *e, const char *field_mem,
  858. const upb_msglayout *m,
  859. const upb_msglayout_field *f) {
  860. const upb_array *arr = *(const upb_array**)field_mem;
  861. bool packed = f->label == _UPB_LABEL_PACKED;
  862. if (arr == NULL || arr->len == 0) {
  863. return true;
  864. }
  865. #define VARINT_CASE(ctype, encode) \
  866. { \
  867. const ctype *start = _upb_array_constptr(arr); \
  868. const ctype *ptr = start + arr->len; \
  869. size_t pre_len = e->limit - e->ptr; \
  870. uint32_t tag = packed ? 0 : (f->number << 3) | UPB_WIRE_TYPE_VARINT; \
  871. do { \
  872. ptr--; \
  873. CHK(upb_put_varint(e, encode)); \
  874. if (tag) CHK(upb_put_varint(e, tag)); \
  875. } while (ptr != start); \
  876. if (!tag) CHK(upb_put_varint(e, e->limit - e->ptr - pre_len)); \
  877. } \
  878. break; \
  879. do { \
  880. ; \
  881. } while (0)
  882. #define TAG(wire_type) (packed ? 0 : (f->number << 3 | wire_type))
  883. switch (f->descriptortype) {
  884. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  885. CHK(upb_put_fixedarray(e, arr, sizeof(double), TAG(UPB_WIRE_TYPE_64BIT)));
  886. break;
  887. case UPB_DESCRIPTOR_TYPE_FLOAT:
  888. CHK(upb_put_fixedarray(e, arr, sizeof(float), TAG(UPB_WIRE_TYPE_32BIT)));
  889. break;
  890. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  891. case UPB_DESCRIPTOR_TYPE_FIXED64:
  892. CHK(upb_put_fixedarray(e, arr, sizeof(uint64_t), TAG(UPB_WIRE_TYPE_64BIT)));
  893. break;
  894. case UPB_DESCRIPTOR_TYPE_FIXED32:
  895. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  896. CHK(upb_put_fixedarray(e, arr, sizeof(uint32_t), TAG(UPB_WIRE_TYPE_32BIT)));
  897. break;
  898. case UPB_DESCRIPTOR_TYPE_INT64:
  899. case UPB_DESCRIPTOR_TYPE_UINT64:
  900. VARINT_CASE(uint64_t, *ptr);
  901. case UPB_DESCRIPTOR_TYPE_UINT32:
  902. VARINT_CASE(uint32_t, *ptr);
  903. case UPB_DESCRIPTOR_TYPE_INT32:
  904. case UPB_DESCRIPTOR_TYPE_ENUM:
  905. VARINT_CASE(int32_t, (int64_t)*ptr);
  906. case UPB_DESCRIPTOR_TYPE_BOOL:
  907. VARINT_CASE(bool, *ptr);
  908. case UPB_DESCRIPTOR_TYPE_SINT32:
  909. VARINT_CASE(int32_t, upb_zzencode_32(*ptr));
  910. case UPB_DESCRIPTOR_TYPE_SINT64:
  911. VARINT_CASE(int64_t, upb_zzencode_64(*ptr));
  912. case UPB_DESCRIPTOR_TYPE_STRING:
  913. case UPB_DESCRIPTOR_TYPE_BYTES: {
  914. const upb_strview *start = _upb_array_constptr(arr);
  915. const upb_strview *ptr = start + arr->len;
  916. do {
  917. ptr--;
  918. CHK(upb_put_bytes(e, ptr->data, ptr->size) &&
  919. upb_put_varint(e, ptr->size) &&
  920. upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
  921. } while (ptr != start);
  922. return true;
  923. }
  924. case UPB_DESCRIPTOR_TYPE_GROUP: {
  925. const void *const*start = _upb_array_constptr(arr);
  926. const void *const*ptr = start + arr->len;
  927. const upb_msglayout *subm = m->submsgs[f->submsg_index];
  928. do {
  929. size_t size;
  930. ptr--;
  931. CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
  932. upb_encode_message(e, *ptr, subm, &size) &&
  933. upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP));
  934. } while (ptr != start);
  935. return true;
  936. }
  937. case UPB_DESCRIPTOR_TYPE_MESSAGE: {
  938. const void *const*start = _upb_array_constptr(arr);
  939. const void *const*ptr = start + arr->len;
  940. const upb_msglayout *subm = m->submsgs[f->submsg_index];
  941. do {
  942. size_t size;
  943. ptr--;
  944. CHK(upb_encode_message(e, *ptr, subm, &size) &&
  945. upb_put_varint(e, size) &&
  946. upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
  947. } while (ptr != start);
  948. return true;
  949. }
  950. }
  951. #undef VARINT_CASE
  952. if (packed) {
  953. CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
  954. }
  955. return true;
  956. }
  957. static bool upb_encode_map(upb_encstate *e, const char *field_mem,
  958. const upb_msglayout *m,
  959. const upb_msglayout_field *f) {
  960. const upb_map *map = *(const upb_map**)field_mem;
  961. const upb_msglayout *entry = m->submsgs[f->submsg_index];
  962. const upb_msglayout_field *key_field = &entry->fields[0];
  963. const upb_msglayout_field *val_field = &entry->fields[1];
  964. upb_strtable_iter i;
  965. if (map == NULL) {
  966. return true;
  967. }
  968. upb_strtable_begin(&i, &map->table);
  969. for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
  970. size_t pre_len = e->limit - e->ptr;
  971. size_t size;
  972. upb_strview key = upb_strtable_iter_key(&i);
  973. const upb_value val = upb_strtable_iter_value(&i);
  974. upb_map_entry ent;
  975. _upb_map_fromkey(key, &ent.k, map->key_size);
  976. _upb_map_fromvalue(val, &ent.v, map->val_size);
  977. CHK(upb_encode_scalarfield(e, &ent.v, entry, val_field, false));
  978. CHK(upb_encode_scalarfield(e, &ent.k, entry, key_field, false));
  979. size = (e->limit - e->ptr) - pre_len;
  980. CHK(upb_put_varint(e, size));
  981. CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
  982. }
  983. return true;
  984. }
  985. bool upb_encode_message(upb_encstate *e, const char *msg,
  986. const upb_msglayout *m, size_t *size) {
  987. int i;
  988. size_t pre_len = e->limit - e->ptr;
  989. const char *unknown;
  990. size_t unknown_size;
  991. unknown = upb_msg_getunknown(msg, &unknown_size);
  992. if (unknown) {
  993. upb_put_bytes(e, unknown, unknown_size);
  994. }
  995. for (i = m->field_count - 1; i >= 0; i--) {
  996. const upb_msglayout_field *f = &m->fields[i];
  997. if (_upb_isrepeated(f)) {
  998. CHK(upb_encode_array(e, msg + f->offset, m, f));
  999. } else if (f->label == _UPB_LABEL_MAP) {
  1000. CHK(upb_encode_map(e, msg + f->offset, m, f));
  1001. } else {
  1002. bool skip_empty = false;
  1003. if (f->presence == 0) {
  1004. /* Proto3 presence. */
  1005. skip_empty = true;
  1006. } else if (f->presence > 0) {
  1007. /* Proto2 presence: hasbit. */
  1008. if (!_upb_hasbit_field(msg, f)) {
  1009. continue;
  1010. }
  1011. } else {
  1012. /* Field is in a oneof. */
  1013. if (_upb_getoneofcase_field(msg, f) != f->number) {
  1014. continue;
  1015. }
  1016. }
  1017. CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, skip_empty));
  1018. }
  1019. }
  1020. *size = (e->limit - e->ptr) - pre_len;
  1021. return true;
  1022. }
  1023. char *upb_encode(const void *msg, const upb_msglayout *m, upb_arena *arena,
  1024. size_t *size) {
  1025. upb_encstate e;
  1026. e.alloc = upb_arena_alloc(arena);
  1027. e.buf = NULL;
  1028. e.limit = NULL;
  1029. e.ptr = NULL;
  1030. if (!upb_encode_message(&e, msg, m, size)) {
  1031. *size = 0;
  1032. return NULL;
  1033. }
  1034. *size = e.limit - e.ptr;
  1035. if (*size == 0) {
  1036. static char ch;
  1037. return &ch;
  1038. } else {
  1039. UPB_ASSERT(e.ptr);
  1040. return e.ptr;
  1041. }
  1042. }
  1043. #undef CHK
  1044. /** upb_msg *******************************************************************/
  1045. static const char _upb_fieldtype_to_sizelg2[12] = {
  1046. 0,
  1047. 0, /* UPB_TYPE_BOOL */
  1048. 2, /* UPB_TYPE_FLOAT */
  1049. 2, /* UPB_TYPE_INT32 */
  1050. 2, /* UPB_TYPE_UINT32 */
  1051. 2, /* UPB_TYPE_ENUM */
  1052. UPB_SIZE(2, 3), /* UPB_TYPE_MESSAGE */
  1053. 3, /* UPB_TYPE_DOUBLE */
  1054. 3, /* UPB_TYPE_INT64 */
  1055. 3, /* UPB_TYPE_UINT64 */
  1056. UPB_SIZE(3, 4), /* UPB_TYPE_STRING */
  1057. UPB_SIZE(3, 4), /* UPB_TYPE_BYTES */
  1058. };
  1059. static uintptr_t tag_arrptr(void* ptr, int elem_size_lg2) {
  1060. UPB_ASSERT(elem_size_lg2 <= 4);
  1061. return (uintptr_t)ptr | elem_size_lg2;
  1062. }
  1063. static int upb_msg_internalsize(const upb_msglayout *l) {
  1064. return sizeof(upb_msg_internal) - l->extendable * sizeof(void *);
  1065. }
  1066. static size_t upb_msg_sizeof(const upb_msglayout *l) {
  1067. return l->size + upb_msg_internalsize(l);
  1068. }
  1069. static const upb_msg_internal *upb_msg_getinternal_const(const upb_msg *msg) {
  1070. ptrdiff_t size = sizeof(upb_msg_internal);
  1071. return UPB_PTR_AT(msg, -size, upb_msg_internal);
  1072. }
  1073. static upb_msg_internal *upb_msg_getinternal(upb_msg *msg) {
  1074. return (upb_msg_internal*)upb_msg_getinternal_const(msg);
  1075. }
  1076. void _upb_msg_clear(upb_msg *msg, const upb_msglayout *l) {
  1077. ptrdiff_t internal = upb_msg_internalsize(l);
  1078. void *mem = UPB_PTR_AT(msg, -internal, char);
  1079. memset(mem, 0, l->size + internal);
  1080. }
  1081. upb_msg *_upb_msg_new(const upb_msglayout *l, upb_arena *a) {
  1082. void *mem = upb_arena_malloc(a, upb_msg_sizeof(l));
  1083. upb_msg *msg;
  1084. if (!mem) {
  1085. return NULL;
  1086. }
  1087. msg = UPB_PTR_AT(mem, upb_msg_internalsize(l), upb_msg);
  1088. _upb_msg_clear(msg, l);
  1089. return msg;
  1090. }
  1091. bool _upb_msg_addunknown(upb_msg *msg, const char *data, size_t len,
  1092. upb_arena *arena) {
  1093. upb_msg_internal *in = upb_msg_getinternal(msg);
  1094. if (len > in->unknown_size - in->unknown_len) {
  1095. upb_alloc *alloc = upb_arena_alloc(arena);
  1096. size_t need = in->unknown_size + len;
  1097. size_t newsize = UPB_MAX(in->unknown_size * 2, need);
  1098. void *mem = upb_realloc(alloc, in->unknown, in->unknown_size, newsize);
  1099. if (!mem) return false;
  1100. in->unknown = mem;
  1101. in->unknown_size = newsize;
  1102. }
  1103. memcpy(in->unknown + in->unknown_len, data, len);
  1104. in->unknown_len += len;
  1105. return true;
  1106. }
  1107. void _upb_msg_discardunknown_shallow(upb_msg *msg) {
  1108. upb_msg_internal *in = upb_msg_getinternal(msg);
  1109. in->unknown_len = 0;
  1110. }
  1111. const char *upb_msg_getunknown(const upb_msg *msg, size_t *len) {
  1112. const upb_msg_internal *in = upb_msg_getinternal_const(msg);
  1113. *len = in->unknown_len;
  1114. return in->unknown;
  1115. }
  1116. /** upb_array *****************************************************************/
  1117. upb_array *_upb_array_new(upb_arena *a, upb_fieldtype_t type) {
  1118. upb_array *arr = upb_arena_malloc(a, sizeof(upb_array));
  1119. if (!arr) {
  1120. return NULL;
  1121. }
  1122. arr->data = tag_arrptr(NULL, _upb_fieldtype_to_sizelg2[type]);
  1123. arr->len = 0;
  1124. arr->size = 0;
  1125. return arr;
  1126. }
  1127. bool _upb_array_realloc(upb_array *arr, size_t min_size, upb_arena *arena) {
  1128. size_t new_size = UPB_MAX(arr->size, 4);
  1129. int elem_size_lg2 = arr->data & 7;
  1130. size_t old_bytes = arr->size << elem_size_lg2;
  1131. size_t new_bytes;
  1132. void* ptr = _upb_array_ptr(arr);
  1133. /* Log2 ceiling of size. */
  1134. while (new_size < min_size) new_size *= 2;
  1135. new_bytes = new_size << elem_size_lg2;
  1136. ptr = upb_arena_realloc(arena, ptr, old_bytes, new_bytes);
  1137. if (!ptr) {
  1138. return false;
  1139. }
  1140. arr->data = tag_arrptr(ptr, elem_size_lg2);
  1141. arr->size = new_size;
  1142. return true;
  1143. }
  1144. static upb_array *getorcreate_array(upb_array **arr_ptr, upb_fieldtype_t type,
  1145. upb_arena *arena) {
  1146. upb_array *arr = *arr_ptr;
  1147. if (!arr) {
  1148. arr = _upb_array_new(arena, type);
  1149. if (!arr) return NULL;
  1150. *arr_ptr = arr;
  1151. }
  1152. return arr;
  1153. }
  1154. void *_upb_array_resize_fallback(upb_array **arr_ptr, size_t size,
  1155. upb_fieldtype_t type, upb_arena *arena) {
  1156. upb_array *arr = getorcreate_array(arr_ptr, type, arena);
  1157. return arr && _upb_array_resize(arr, size, arena) ? _upb_array_ptr(arr) : NULL;
  1158. }
  1159. bool _upb_array_append_fallback(upb_array **arr_ptr, const void *value,
  1160. upb_fieldtype_t type, upb_arena *arena) {
  1161. upb_array *arr = getorcreate_array(arr_ptr, type, arena);
  1162. size_t elem = arr->len;
  1163. int lg2 = _upb_fieldtype_to_sizelg2[type];
  1164. char *data;
  1165. if (!arr || !_upb_array_resize(arr, elem + 1, arena)) return false;
  1166. data = _upb_array_ptr(arr);
  1167. memcpy(data + (elem << lg2), value, 1 << lg2);
  1168. return true;
  1169. }
  1170. /** upb_map *******************************************************************/
  1171. upb_map *_upb_map_new(upb_arena *a, size_t key_size, size_t value_size) {
  1172. upb_map *map = upb_arena_malloc(a, sizeof(upb_map));
  1173. if (!map) {
  1174. return NULL;
  1175. }
  1176. upb_strtable_init2(&map->table, UPB_CTYPE_INT32, upb_arena_alloc(a));
  1177. map->key_size = key_size;
  1178. map->val_size = value_size;
  1179. return map;
  1180. }
  1181. /*
  1182. ** upb_table Implementation
  1183. **
  1184. ** Implementation is heavily inspired by Lua's ltable.c.
  1185. */
  1186. #include <string.h>
  1187. #define UPB_MAXARRSIZE 16 /* 64k. */
  1188. /* From Chromium. */
  1189. #define ARRAY_SIZE(x) \
  1190. ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
  1191. static const double MAX_LOAD = 0.85;
  1192. /* The minimum utilization of the array part of a mixed hash/array table. This
  1193. * is a speed/memory-usage tradeoff (though it's not straightforward because of
  1194. * cache effects). The lower this is, the more memory we'll use. */
  1195. static const double MIN_DENSITY = 0.1;
  1196. bool is_pow2(uint64_t v) { return v == 0 || (v & (v - 1)) == 0; }
  1197. int log2ceil(uint64_t v) {
  1198. int ret = 0;
  1199. bool pow2 = is_pow2(v);
  1200. while (v >>= 1) ret++;
  1201. ret = pow2 ? ret : ret + 1; /* Ceiling. */
  1202. return UPB_MIN(UPB_MAXARRSIZE, ret);
  1203. }
  1204. char *upb_strdup(const char *s, upb_alloc *a) {
  1205. return upb_strdup2(s, strlen(s), a);
  1206. }
  1207. char *upb_strdup2(const char *s, size_t len, upb_alloc *a) {
  1208. size_t n;
  1209. char *p;
  1210. /* Prevent overflow errors. */
  1211. if (len == SIZE_MAX) return NULL;
  1212. /* Always null-terminate, even if binary data; but don't rely on the input to
  1213. * have a null-terminating byte since it may be a raw binary buffer. */
  1214. n = len + 1;
  1215. p = upb_malloc(a, n);
  1216. if (p) {
  1217. memcpy(p, s, len);
  1218. p[len] = 0;
  1219. }
  1220. return p;
  1221. }
  1222. /* A type to represent the lookup key of either a strtable or an inttable. */
  1223. typedef union {
  1224. uintptr_t num;
  1225. struct {
  1226. const char *str;
  1227. size_t len;
  1228. } str;
  1229. } lookupkey_t;
  1230. static lookupkey_t strkey2(const char *str, size_t len) {
  1231. lookupkey_t k;
  1232. k.str.str = str;
  1233. k.str.len = len;
  1234. return k;
  1235. }
  1236. static lookupkey_t intkey(uintptr_t key) {
  1237. lookupkey_t k;
  1238. k.num = key;
  1239. return k;
  1240. }
  1241. typedef uint32_t hashfunc_t(upb_tabkey key);
  1242. typedef bool eqlfunc_t(upb_tabkey k1, lookupkey_t k2);
  1243. /* Base table (shared code) ***************************************************/
  1244. /* For when we need to cast away const. */
  1245. static upb_tabent *mutable_entries(upb_table *t) {
  1246. return (upb_tabent*)t->entries;
  1247. }
  1248. static bool isfull(upb_table *t) {
  1249. if (upb_table_size(t) == 0) {
  1250. return true;
  1251. } else {
  1252. return ((double)(t->count + 1) / upb_table_size(t)) > MAX_LOAD;
  1253. }
  1254. }
  1255. static bool init(upb_table *t, uint8_t size_lg2, upb_alloc *a) {
  1256. size_t bytes;
  1257. t->count = 0;
  1258. t->size_lg2 = size_lg2;
  1259. t->mask = upb_table_size(t) ? upb_table_size(t) - 1 : 0;
  1260. bytes = upb_table_size(t) * sizeof(upb_tabent);
  1261. if (bytes > 0) {
  1262. t->entries = upb_malloc(a, bytes);
  1263. if (!t->entries) return false;
  1264. memset(mutable_entries(t), 0, bytes);
  1265. } else {
  1266. t->entries = NULL;
  1267. }
  1268. return true;
  1269. }
  1270. static void uninit(upb_table *t, upb_alloc *a) {
  1271. upb_free(a, mutable_entries(t));
  1272. }
  1273. static upb_tabent *emptyent(upb_table *t) {
  1274. upb_tabent *e = mutable_entries(t) + upb_table_size(t);
  1275. while (1) { if (upb_tabent_isempty(--e)) return e; UPB_ASSERT(e > t->entries); }
  1276. }
  1277. static upb_tabent *getentry_mutable(upb_table *t, uint32_t hash) {
  1278. return (upb_tabent*)upb_getentry(t, hash);
  1279. }
  1280. static const upb_tabent *findentry(const upb_table *t, lookupkey_t key,
  1281. uint32_t hash, eqlfunc_t *eql) {
  1282. const upb_tabent *e;
  1283. if (t->size_lg2 == 0) return NULL;
  1284. e = upb_getentry(t, hash);
  1285. if (upb_tabent_isempty(e)) return NULL;
  1286. while (1) {
  1287. if (eql(e->key, key)) return e;
  1288. if ((e = e->next) == NULL) return NULL;
  1289. }
  1290. }
  1291. static upb_tabent *findentry_mutable(upb_table *t, lookupkey_t key,
  1292. uint32_t hash, eqlfunc_t *eql) {
  1293. return (upb_tabent*)findentry(t, key, hash, eql);
  1294. }
  1295. static bool lookup(const upb_table *t, lookupkey_t key, upb_value *v,
  1296. uint32_t hash, eqlfunc_t *eql) {
  1297. const upb_tabent *e = findentry(t, key, hash, eql);
  1298. if (e) {
  1299. if (v) {
  1300. _upb_value_setval(v, e->val.val);
  1301. }
  1302. return true;
  1303. } else {
  1304. return false;
  1305. }
  1306. }
  1307. /* The given key must not already exist in the table. */
  1308. static void insert(upb_table *t, lookupkey_t key, upb_tabkey tabkey,
  1309. upb_value val, uint32_t hash,
  1310. hashfunc_t *hashfunc, eqlfunc_t *eql) {
  1311. upb_tabent *mainpos_e;
  1312. upb_tabent *our_e;
  1313. UPB_ASSERT(findentry(t, key, hash, eql) == NULL);
  1314. t->count++;
  1315. mainpos_e = getentry_mutable(t, hash);
  1316. our_e = mainpos_e;
  1317. if (upb_tabent_isempty(mainpos_e)) {
  1318. /* Our main position is empty; use it. */
  1319. our_e->next = NULL;
  1320. } else {
  1321. /* Collision. */
  1322. upb_tabent *new_e = emptyent(t);
  1323. /* Head of collider's chain. */
  1324. upb_tabent *chain = getentry_mutable(t, hashfunc(mainpos_e->key));
  1325. if (chain == mainpos_e) {
  1326. /* Existing ent is in its main posisiton (it has the same hash as us, and
  1327. * is the head of our chain). Insert to new ent and append to this chain. */
  1328. new_e->next = mainpos_e->next;
  1329. mainpos_e->next = new_e;
  1330. our_e = new_e;
  1331. } else {
  1332. /* Existing ent is not in its main position (it is a node in some other
  1333. * chain). This implies that no existing ent in the table has our hash.
  1334. * Evict it (updating its chain) and use its ent for head of our chain. */
  1335. *new_e = *mainpos_e; /* copies next. */
  1336. while (chain->next != mainpos_e) {
  1337. chain = (upb_tabent*)chain->next;
  1338. UPB_ASSERT(chain);
  1339. }
  1340. chain->next = new_e;
  1341. our_e = mainpos_e;
  1342. our_e->next = NULL;
  1343. }
  1344. }
  1345. our_e->key = tabkey;
  1346. our_e->val.val = val.val;
  1347. UPB_ASSERT(findentry(t, key, hash, eql) == our_e);
  1348. }
  1349. static bool rm(upb_table *t, lookupkey_t key, upb_value *val,
  1350. upb_tabkey *removed, uint32_t hash, eqlfunc_t *eql) {
  1351. upb_tabent *chain = getentry_mutable(t, hash);
  1352. if (upb_tabent_isempty(chain)) return false;
  1353. if (eql(chain->key, key)) {
  1354. /* Element to remove is at the head of its chain. */
  1355. t->count--;
  1356. if (val) _upb_value_setval(val, chain->val.val);
  1357. if (removed) *removed = chain->key;
  1358. if (chain->next) {
  1359. upb_tabent *move = (upb_tabent*)chain->next;
  1360. *chain = *move;
  1361. move->key = 0; /* Make the slot empty. */
  1362. } else {
  1363. chain->key = 0; /* Make the slot empty. */
  1364. }
  1365. return true;
  1366. } else {
  1367. /* Element to remove is either in a non-head position or not in the
  1368. * table. */
  1369. while (chain->next && !eql(chain->next->key, key)) {
  1370. chain = (upb_tabent*)chain->next;
  1371. }
  1372. if (chain->next) {
  1373. /* Found element to remove. */
  1374. upb_tabent *rm = (upb_tabent*)chain->next;
  1375. t->count--;
  1376. if (val) _upb_value_setval(val, chain->next->val.val);
  1377. if (removed) *removed = rm->key;
  1378. rm->key = 0; /* Make the slot empty. */
  1379. chain->next = rm->next;
  1380. return true;
  1381. } else {
  1382. /* Element to remove is not in the table. */
  1383. return false;
  1384. }
  1385. }
  1386. }
  1387. static size_t next(const upb_table *t, size_t i) {
  1388. do {
  1389. if (++i >= upb_table_size(t))
  1390. return SIZE_MAX - 1; /* Distinct from -1. */
  1391. } while(upb_tabent_isempty(&t->entries[i]));
  1392. return i;
  1393. }
  1394. static size_t begin(const upb_table *t) {
  1395. return next(t, -1);
  1396. }
  1397. /* upb_strtable ***************************************************************/
  1398. /* A simple "subclass" of upb_table that only adds a hash function for strings. */
  1399. static upb_tabkey strcopy(lookupkey_t k2, upb_alloc *a) {
  1400. uint32_t len = (uint32_t) k2.str.len;
  1401. char *str = upb_malloc(a, k2.str.len + sizeof(uint32_t) + 1);
  1402. if (str == NULL) return 0;
  1403. memcpy(str, &len, sizeof(uint32_t));
  1404. if (k2.str.len) memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len);
  1405. str[sizeof(uint32_t) + k2.str.len] = '\0';
  1406. return (uintptr_t)str;
  1407. }
  1408. static uint32_t strhash(upb_tabkey key) {
  1409. uint32_t len;
  1410. char *str = upb_tabstr(key, &len);
  1411. return upb_murmur_hash2(str, len, 0);
  1412. }
  1413. static bool streql(upb_tabkey k1, lookupkey_t k2) {
  1414. uint32_t len;
  1415. char *str = upb_tabstr(k1, &len);
  1416. return len == k2.str.len && (len == 0 || memcmp(str, k2.str.str, len) == 0);
  1417. }
  1418. bool upb_strtable_init2(upb_strtable *t, upb_ctype_t ctype, upb_alloc *a) {
  1419. UPB_UNUSED(ctype); /* TODO(haberman): rm */
  1420. return init(&t->t, 2, a);
  1421. }
  1422. void upb_strtable_clear(upb_strtable *t) {
  1423. size_t bytes = upb_table_size(&t->t) * sizeof(upb_tabent);
  1424. t->t.count = 0;
  1425. memset((char*)t->t.entries, 0, bytes);
  1426. }
  1427. void upb_strtable_uninit2(upb_strtable *t, upb_alloc *a) {
  1428. size_t i;
  1429. for (i = 0; i < upb_table_size(&t->t); i++)
  1430. upb_free(a, (void*)t->t.entries[i].key);
  1431. uninit(&t->t, a);
  1432. }
  1433. bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_alloc *a) {
  1434. upb_strtable new_table;
  1435. upb_strtable_iter i;
  1436. if (!init(&new_table.t, size_lg2, a))
  1437. return false;
  1438. upb_strtable_begin(&i, t);
  1439. for ( ; !upb_strtable_done(&i); upb_strtable_next(&i)) {
  1440. upb_strview key = upb_strtable_iter_key(&i);
  1441. upb_strtable_insert3(
  1442. &new_table, key.data, key.size,
  1443. upb_strtable_iter_value(&i), a);
  1444. }
  1445. upb_strtable_uninit2(t, a);
  1446. *t = new_table;
  1447. return true;
  1448. }
  1449. bool upb_strtable_insert3(upb_strtable *t, const char *k, size_t len,
  1450. upb_value v, upb_alloc *a) {
  1451. lookupkey_t key;
  1452. upb_tabkey tabkey;
  1453. uint32_t hash;
  1454. if (isfull(&t->t)) {
  1455. /* Need to resize. New table of double the size, add old elements to it. */
  1456. if (!upb_strtable_resize(t, t->t.size_lg2 + 1, a)) {
  1457. return false;
  1458. }
  1459. }
  1460. key = strkey2(k, len);
  1461. tabkey = strcopy(key, a);
  1462. if (tabkey == 0) return false;
  1463. hash = upb_murmur_hash2(key.str.str, key.str.len, 0);
  1464. insert(&t->t, key, tabkey, v, hash, &strhash, &streql);
  1465. return true;
  1466. }
  1467. bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
  1468. upb_value *v) {
  1469. uint32_t hash = upb_murmur_hash2(key, len, 0);
  1470. return lookup(&t->t, strkey2(key, len), v, hash, &streql);
  1471. }
  1472. bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len,
  1473. upb_value *val, upb_alloc *alloc) {
  1474. uint32_t hash = upb_murmur_hash2(key, len, 0);
  1475. upb_tabkey tabkey;
  1476. if (rm(&t->t, strkey2(key, len), val, &tabkey, hash, &streql)) {
  1477. if (alloc) {
  1478. /* Arena-based allocs don't need to free and won't pass this. */
  1479. upb_free(alloc, (void*)tabkey);
  1480. }
  1481. return true;
  1482. } else {
  1483. return false;
  1484. }
  1485. }
  1486. /* Iteration */
  1487. void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t) {
  1488. i->t = t;
  1489. i->index = begin(&t->t);
  1490. }
  1491. void upb_strtable_next(upb_strtable_iter *i) {
  1492. i->index = next(&i->t->t, i->index);
  1493. }
  1494. bool upb_strtable_done(const upb_strtable_iter *i) {
  1495. if (!i->t) return true;
  1496. return i->index >= upb_table_size(&i->t->t) ||
  1497. upb_tabent_isempty(str_tabent(i));
  1498. }
  1499. upb_strview upb_strtable_iter_key(const upb_strtable_iter *i) {
  1500. upb_strview key;
  1501. uint32_t len;
  1502. UPB_ASSERT(!upb_strtable_done(i));
  1503. key.data = upb_tabstr(str_tabent(i)->key, &len);
  1504. key.size = len;
  1505. return key;
  1506. }
  1507. upb_value upb_strtable_iter_value(const upb_strtable_iter *i) {
  1508. UPB_ASSERT(!upb_strtable_done(i));
  1509. return _upb_value_val(str_tabent(i)->val.val);
  1510. }
  1511. void upb_strtable_iter_setdone(upb_strtable_iter *i) {
  1512. i->t = NULL;
  1513. i->index = SIZE_MAX;
  1514. }
  1515. bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
  1516. const upb_strtable_iter *i2) {
  1517. if (upb_strtable_done(i1) && upb_strtable_done(i2))
  1518. return true;
  1519. return i1->t == i2->t && i1->index == i2->index;
  1520. }
  1521. /* upb_inttable ***************************************************************/
  1522. /* For inttables we use a hybrid structure where small keys are kept in an
  1523. * array and large keys are put in the hash table. */
  1524. static uint32_t inthash(upb_tabkey key) { return upb_inthash(key); }
  1525. static bool inteql(upb_tabkey k1, lookupkey_t k2) {
  1526. return k1 == k2.num;
  1527. }
  1528. static upb_tabval *mutable_array(upb_inttable *t) {
  1529. return (upb_tabval*)t->array;
  1530. }
  1531. static upb_tabval *inttable_val(upb_inttable *t, uintptr_t key) {
  1532. if (key < t->array_size) {
  1533. return upb_arrhas(t->array[key]) ? &(mutable_array(t)[key]) : NULL;
  1534. } else {
  1535. upb_tabent *e =
  1536. findentry_mutable(&t->t, intkey(key), upb_inthash(key), &inteql);
  1537. return e ? &e->val : NULL;
  1538. }
  1539. }
  1540. static const upb_tabval *inttable_val_const(const upb_inttable *t,
  1541. uintptr_t key) {
  1542. return inttable_val((upb_inttable*)t, key);
  1543. }
  1544. size_t upb_inttable_count(const upb_inttable *t) {
  1545. return t->t.count + t->array_count;
  1546. }
  1547. static void check(upb_inttable *t) {
  1548. UPB_UNUSED(t);
  1549. #if defined(UPB_DEBUG_TABLE) && !defined(NDEBUG)
  1550. {
  1551. /* This check is very expensive (makes inserts/deletes O(N)). */
  1552. size_t count = 0;
  1553. upb_inttable_iter i;
  1554. upb_inttable_begin(&i, t);
  1555. for(; !upb_inttable_done(&i); upb_inttable_next(&i), count++) {
  1556. UPB_ASSERT(upb_inttable_lookup(t, upb_inttable_iter_key(&i), NULL));
  1557. }
  1558. UPB_ASSERT(count == upb_inttable_count(t));
  1559. }
  1560. #endif
  1561. }
  1562. bool upb_inttable_sizedinit(upb_inttable *t, size_t asize, int hsize_lg2,
  1563. upb_alloc *a) {
  1564. size_t array_bytes;
  1565. if (!init(&t->t, hsize_lg2, a)) return false;
  1566. /* Always make the array part at least 1 long, so that we know key 0
  1567. * won't be in the hash part, which simplifies things. */
  1568. t->array_size = UPB_MAX(1, asize);
  1569. t->array_count = 0;
  1570. array_bytes = t->array_size * sizeof(upb_value);
  1571. t->array = upb_malloc(a, array_bytes);
  1572. if (!t->array) {
  1573. uninit(&t->t, a);
  1574. return false;
  1575. }
  1576. memset(mutable_array(t), 0xff, array_bytes);
  1577. check(t);
  1578. return true;
  1579. }
  1580. bool upb_inttable_init2(upb_inttable *t, upb_ctype_t ctype, upb_alloc *a) {
  1581. UPB_UNUSED(ctype); /* TODO(haberman): rm */
  1582. return upb_inttable_sizedinit(t, 0, 4, a);
  1583. }
  1584. void upb_inttable_uninit2(upb_inttable *t, upb_alloc *a) {
  1585. uninit(&t->t, a);
  1586. upb_free(a, mutable_array(t));
  1587. }
  1588. bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val,
  1589. upb_alloc *a) {
  1590. upb_tabval tabval;
  1591. tabval.val = val.val;
  1592. UPB_ASSERT(upb_arrhas(tabval)); /* This will reject (uint64_t)-1. Fix this. */
  1593. if (key < t->array_size) {
  1594. UPB_ASSERT(!upb_arrhas(t->array[key]));
  1595. t->array_count++;
  1596. mutable_array(t)[key].val = val.val;
  1597. } else {
  1598. if (isfull(&t->t)) {
  1599. /* Need to resize the hash part, but we re-use the array part. */
  1600. size_t i;
  1601. upb_table new_table;
  1602. if (!init(&new_table, t->t.size_lg2 + 1, a)) {
  1603. return false;
  1604. }
  1605. for (i = begin(&t->t); i < upb_table_size(&t->t); i = next(&t->t, i)) {
  1606. const upb_tabent *e = &t->t.entries[i];
  1607. uint32_t hash;
  1608. upb_value v;
  1609. _upb_value_setval(&v, e->val.val);
  1610. hash = upb_inthash(e->key);
  1611. insert(&new_table, intkey(e->key), e->key, v, hash, &inthash, &inteql);
  1612. }
  1613. UPB_ASSERT(t->t.count == new_table.count);
  1614. uninit(&t->t, a);
  1615. t->t = new_table;
  1616. }
  1617. insert(&t->t, intkey(key), key, val, upb_inthash(key), &inthash, &inteql);
  1618. }
  1619. check(t);
  1620. return true;
  1621. }
  1622. bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v) {
  1623. const upb_tabval *table_v = inttable_val_const(t, key);
  1624. if (!table_v) return false;
  1625. if (v) _upb_value_setval(v, table_v->val);
  1626. return true;
  1627. }
  1628. bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val) {
  1629. upb_tabval *table_v = inttable_val(t, key);
  1630. if (!table_v) return false;
  1631. table_v->val = val.val;
  1632. return true;
  1633. }
  1634. bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val) {
  1635. bool success;
  1636. if (key < t->array_size) {
  1637. if (upb_arrhas(t->array[key])) {
  1638. upb_tabval empty = UPB_TABVALUE_EMPTY_INIT;
  1639. t->array_count--;
  1640. if (val) {
  1641. _upb_value_setval(val, t->array[key].val);
  1642. }
  1643. mutable_array(t)[key] = empty;
  1644. success = true;
  1645. } else {
  1646. success = false;
  1647. }
  1648. } else {
  1649. success = rm(&t->t, intkey(key), val, NULL, upb_inthash(key), &inteql);
  1650. }
  1651. check(t);
  1652. return success;
  1653. }
  1654. bool upb_inttable_push2(upb_inttable *t, upb_value val, upb_alloc *a) {
  1655. return upb_inttable_insert2(t, upb_inttable_count(t), val, a);
  1656. }
  1657. upb_value upb_inttable_pop(upb_inttable *t) {
  1658. upb_value val;
  1659. bool ok = upb_inttable_remove(t, upb_inttable_count(t) - 1, &val);
  1660. UPB_ASSERT(ok);
  1661. return val;
  1662. }
  1663. bool upb_inttable_insertptr2(upb_inttable *t, const void *key, upb_value val,
  1664. upb_alloc *a) {
  1665. return upb_inttable_insert2(t, (uintptr_t)key, val, a);
  1666. }
  1667. bool upb_inttable_lookupptr(const upb_inttable *t, const void *key,
  1668. upb_value *v) {
  1669. return upb_inttable_lookup(t, (uintptr_t)key, v);
  1670. }
  1671. bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val) {
  1672. return upb_inttable_remove(t, (uintptr_t)key, val);
  1673. }
  1674. void upb_inttable_compact2(upb_inttable *t, upb_alloc *a) {
  1675. /* A power-of-two histogram of the table keys. */
  1676. size_t counts[UPB_MAXARRSIZE + 1] = {0};
  1677. /* The max key in each bucket. */
  1678. uintptr_t max[UPB_MAXARRSIZE + 1] = {0};
  1679. upb_inttable_iter i;
  1680. size_t arr_count;
  1681. int size_lg2;
  1682. upb_inttable new_t;
  1683. upb_inttable_begin(&i, t);
  1684. for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  1685. uintptr_t key = upb_inttable_iter_key(&i);
  1686. int bucket = log2ceil(key);
  1687. max[bucket] = UPB_MAX(max[bucket], key);
  1688. counts[bucket]++;
  1689. }
  1690. /* Find the largest power of two that satisfies the MIN_DENSITY
  1691. * definition (while actually having some keys). */
  1692. arr_count = upb_inttable_count(t);
  1693. for (size_lg2 = ARRAY_SIZE(counts) - 1; size_lg2 > 0; size_lg2--) {
  1694. if (counts[size_lg2] == 0) {
  1695. /* We can halve again without losing any entries. */
  1696. continue;
  1697. } else if (arr_count >= (1 << size_lg2) * MIN_DENSITY) {
  1698. break;
  1699. }
  1700. arr_count -= counts[size_lg2];
  1701. }
  1702. UPB_ASSERT(arr_count <= upb_inttable_count(t));
  1703. {
  1704. /* Insert all elements into new, perfectly-sized table. */
  1705. size_t arr_size = max[size_lg2] + 1; /* +1 so arr[max] will fit. */
  1706. size_t hash_count = upb_inttable_count(t) - arr_count;
  1707. size_t hash_size = hash_count ? (hash_count / MAX_LOAD) + 1 : 0;
  1708. int hashsize_lg2 = log2ceil(hash_size);
  1709. upb_inttable_sizedinit(&new_t, arr_size, hashsize_lg2, a);
  1710. upb_inttable_begin(&i, t);
  1711. for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  1712. uintptr_t k = upb_inttable_iter_key(&i);
  1713. upb_inttable_insert2(&new_t, k, upb_inttable_iter_value(&i), a);
  1714. }
  1715. UPB_ASSERT(new_t.array_size == arr_size);
  1716. UPB_ASSERT(new_t.t.size_lg2 == hashsize_lg2);
  1717. }
  1718. upb_inttable_uninit2(t, a);
  1719. *t = new_t;
  1720. }
  1721. /* Iteration. */
  1722. static const upb_tabent *int_tabent(const upb_inttable_iter *i) {
  1723. UPB_ASSERT(!i->array_part);
  1724. return &i->t->t.entries[i->index];
  1725. }
  1726. static upb_tabval int_arrent(const upb_inttable_iter *i) {
  1727. UPB_ASSERT(i->array_part);
  1728. return i->t->array[i->index];
  1729. }
  1730. void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t) {
  1731. i->t = t;
  1732. i->index = -1;
  1733. i->array_part = true;
  1734. upb_inttable_next(i);
  1735. }
  1736. void upb_inttable_next(upb_inttable_iter *iter) {
  1737. const upb_inttable *t = iter->t;
  1738. if (iter->array_part) {
  1739. while (++iter->index < t->array_size) {
  1740. if (upb_arrhas(int_arrent(iter))) {
  1741. return;
  1742. }
  1743. }
  1744. iter->array_part = false;
  1745. iter->index = begin(&t->t);
  1746. } else {
  1747. iter->index = next(&t->t, iter->index);
  1748. }
  1749. }
  1750. bool upb_inttable_done(const upb_inttable_iter *i) {
  1751. if (!i->t) return true;
  1752. if (i->array_part) {
  1753. return i->index >= i->t->array_size ||
  1754. !upb_arrhas(int_arrent(i));
  1755. } else {
  1756. return i->index >= upb_table_size(&i->t->t) ||
  1757. upb_tabent_isempty(int_tabent(i));
  1758. }
  1759. }
  1760. uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i) {
  1761. UPB_ASSERT(!upb_inttable_done(i));
  1762. return i->array_part ? i->index : int_tabent(i)->key;
  1763. }
  1764. upb_value upb_inttable_iter_value(const upb_inttable_iter *i) {
  1765. UPB_ASSERT(!upb_inttable_done(i));
  1766. return _upb_value_val(
  1767. i->array_part ? i->t->array[i->index].val : int_tabent(i)->val.val);
  1768. }
  1769. void upb_inttable_iter_setdone(upb_inttable_iter *i) {
  1770. i->t = NULL;
  1771. i->index = SIZE_MAX;
  1772. i->array_part = false;
  1773. }
  1774. bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
  1775. const upb_inttable_iter *i2) {
  1776. if (upb_inttable_done(i1) && upb_inttable_done(i2))
  1777. return true;
  1778. return i1->t == i2->t && i1->index == i2->index &&
  1779. i1->array_part == i2->array_part;
  1780. }
  1781. #if defined(UPB_UNALIGNED_READS_OK) || defined(__s390x__)
  1782. /* -----------------------------------------------------------------------------
  1783. * MurmurHash2, by Austin Appleby (released as public domain).
  1784. * Reformatted and C99-ified by Joshua Haberman.
  1785. * Note - This code makes a few assumptions about how your machine behaves -
  1786. * 1. We can read a 4-byte value from any address without crashing
  1787. * 2. sizeof(int) == 4 (in upb this limitation is removed by using uint32_t
  1788. * And it has a few limitations -
  1789. * 1. It will not work incrementally.
  1790. * 2. It will not produce the same results on little-endian and big-endian
  1791. * machines. */
  1792. uint32_t upb_murmur_hash2(const void *key, size_t len, uint32_t seed) {
  1793. /* 'm' and 'r' are mixing constants generated offline.
  1794. * They're not really 'magic', they just happen to work well. */
  1795. const uint32_t m = 0x5bd1e995;
  1796. const int32_t r = 24;
  1797. /* Initialize the hash to a 'random' value */
  1798. uint32_t h = seed ^ len;
  1799. /* Mix 4 bytes at a time into the hash */
  1800. const uint8_t * data = (const uint8_t *)key;
  1801. while(len >= 4) {
  1802. uint32_t k;
  1803. memcpy(&k, data, sizeof(k));
  1804. k *= m;
  1805. k ^= k >> r;
  1806. k *= m;
  1807. h *= m;
  1808. h ^= k;
  1809. data += 4;
  1810. len -= 4;
  1811. }
  1812. /* Handle the last few bytes of the input array */
  1813. switch(len) {
  1814. case 3: h ^= data[2] << 16;
  1815. case 2: h ^= data[1] << 8;
  1816. case 1: h ^= data[0]; h *= m;
  1817. };
  1818. /* Do a few final mixes of the hash to ensure the last few
  1819. * bytes are well-incorporated. */
  1820. h ^= h >> 13;
  1821. h *= m;
  1822. h ^= h >> 15;
  1823. return h;
  1824. }
  1825. #else /* !UPB_UNALIGNED_READS_OK */
  1826. /* -----------------------------------------------------------------------------
  1827. * MurmurHashAligned2, by Austin Appleby
  1828. * Same algorithm as MurmurHash2, but only does aligned reads - should be safer
  1829. * on certain platforms.
  1830. * Performance will be lower than MurmurHash2 */
  1831. #define MIX(h,k,m) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }
  1832. uint32_t upb_murmur_hash2(const void * key, size_t len, uint32_t seed) {
  1833. const uint32_t m = 0x5bd1e995;
  1834. const int32_t r = 24;
  1835. const uint8_t * data = (const uint8_t *)key;
  1836. uint32_t h = (uint32_t)(seed ^ len);
  1837. uint8_t align = (uintptr_t)data & 3;
  1838. if(align && (len >= 4)) {
  1839. /* Pre-load the temp registers */
  1840. uint32_t t = 0, d = 0;
  1841. int32_t sl;
  1842. int32_t sr;
  1843. switch(align) {
  1844. case 1: t |= data[2] << 16; /* fallthrough */
  1845. case 2: t |= data[1] << 8; /* fallthrough */
  1846. case 3: t |= data[0];
  1847. }
  1848. t <<= (8 * align);
  1849. data += 4-align;
  1850. len -= 4-align;
  1851. sl = 8 * (4-align);
  1852. sr = 8 * align;
  1853. /* Mix */
  1854. while(len >= 4) {
  1855. uint32_t k;
  1856. d = *(uint32_t *)data;
  1857. t = (t >> sr) | (d << sl);
  1858. k = t;
  1859. MIX(h,k,m);
  1860. t = d;
  1861. data += 4;
  1862. len -= 4;
  1863. }
  1864. /* Handle leftover data in temp registers */
  1865. d = 0;
  1866. if(len >= align) {
  1867. uint32_t k;
  1868. switch(align) {
  1869. case 3: d |= data[2] << 16; /* fallthrough */
  1870. case 2: d |= data[1] << 8; /* fallthrough */
  1871. case 1: d |= data[0]; /* fallthrough */
  1872. }
  1873. k = (t >> sr) | (d << sl);
  1874. MIX(h,k,m);
  1875. data += align;
  1876. len -= align;
  1877. /* ----------
  1878. * Handle tail bytes */
  1879. switch(len) {
  1880. case 3: h ^= data[2] << 16; /* fallthrough */
  1881. case 2: h ^= data[1] << 8; /* fallthrough */
  1882. case 1: h ^= data[0]; h *= m; /* fallthrough */
  1883. };
  1884. } else {
  1885. switch(len) {
  1886. case 3: d |= data[2] << 16; /* fallthrough */
  1887. case 2: d |= data[1] << 8; /* fallthrough */
  1888. case 1: d |= data[0]; /* fallthrough */
  1889. case 0: h ^= (t >> sr) | (d << sl); h *= m;
  1890. }
  1891. }
  1892. h ^= h >> 13;
  1893. h *= m;
  1894. h ^= h >> 15;
  1895. return h;
  1896. } else {
  1897. while(len >= 4) {
  1898. uint32_t k = *(uint32_t *)data;
  1899. MIX(h,k,m);
  1900. data += 4;
  1901. len -= 4;
  1902. }
  1903. /* ----------
  1904. * Handle tail bytes */
  1905. switch(len) {
  1906. case 3: h ^= data[2] << 16; /* fallthrough */
  1907. case 2: h ^= data[1] << 8; /* fallthrough */
  1908. case 1: h ^= data[0]; h *= m;
  1909. };
  1910. h ^= h >> 13;
  1911. h *= m;
  1912. h ^= h >> 15;
  1913. return h;
  1914. }
  1915. }
  1916. #undef MIX
  1917. #endif /* UPB_UNALIGNED_READS_OK */
  1918. #include <errno.h>
  1919. #include <stdarg.h>
  1920. #include <stddef.h>
  1921. #include <stdint.h>
  1922. #include <stdio.h>
  1923. #include <stdlib.h>
  1924. #include <string.h>
  1925. /* upb_status *****************************************************************/
  1926. void upb_status_clear(upb_status *status) {
  1927. if (!status) return;
  1928. status->ok = true;
  1929. status->msg[0] = '\0';
  1930. }
  1931. bool upb_ok(const upb_status *status) { return status->ok; }
  1932. const char *upb_status_errmsg(const upb_status *status) { return status->msg; }
  1933. void upb_status_seterrmsg(upb_status *status, const char *msg) {
  1934. if (!status) return;
  1935. status->ok = false;
  1936. strncpy(status->msg, msg, UPB_STATUS_MAX_MESSAGE - 1);
  1937. status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0';
  1938. }
  1939. void upb_status_seterrf(upb_status *status, const char *fmt, ...) {
  1940. va_list args;
  1941. va_start(args, fmt);
  1942. upb_status_vseterrf(status, fmt, args);
  1943. va_end(args);
  1944. }
  1945. void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args) {
  1946. if (!status) return;
  1947. status->ok = false;
  1948. _upb_vsnprintf(status->msg, sizeof(status->msg), fmt, args);
  1949. status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0';
  1950. }
  1951. void upb_status_vappenderrf(upb_status *status, const char *fmt, va_list args) {
  1952. size_t len;
  1953. if (!status) return;
  1954. status->ok = false;
  1955. len = strlen(status->msg);
  1956. _upb_vsnprintf(status->msg + len, sizeof(status->msg) - len, fmt, args);
  1957. status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0';
  1958. }
  1959. /* upb_alloc ******************************************************************/
  1960. static void *upb_global_allocfunc(upb_alloc *alloc, void *ptr, size_t oldsize,
  1961. size_t size) {
  1962. UPB_UNUSED(alloc);
  1963. UPB_UNUSED(oldsize);
  1964. if (size == 0) {
  1965. free(ptr);
  1966. return NULL;
  1967. } else {
  1968. return realloc(ptr, size);
  1969. }
  1970. }
  1971. upb_alloc upb_alloc_global = {&upb_global_allocfunc};
  1972. /* upb_arena ******************************************************************/
  1973. /* Be conservative and choose 16 in case anyone is using SSE. */
  1974. typedef struct mem_block {
  1975. struct mem_block *next;
  1976. uint32_t size;
  1977. uint32_t cleanups;
  1978. /* Data follows. */
  1979. } mem_block;
  1980. typedef struct cleanup_ent {
  1981. upb_cleanup_func *cleanup;
  1982. void *ud;
  1983. } cleanup_ent;
  1984. struct upb_arena {
  1985. _upb_arena_head head;
  1986. uint32_t *cleanups;
  1987. /* Allocator to allocate arena blocks. We are responsible for freeing these
  1988. * when we are destroyed. */
  1989. upb_alloc *block_alloc;
  1990. uint32_t last_size;
  1991. /* When multiple arenas are fused together, each arena points to a parent
  1992. * arena (root points to itself). The root tracks how many live arenas
  1993. * reference it. */
  1994. uint32_t refcount; /* Only used when a->parent == a */
  1995. struct upb_arena *parent;
  1996. /* Linked list of blocks to free/cleanup. */
  1997. mem_block *freelist, *freelist_tail;
  1998. };
  1999. static const size_t memblock_reserve = UPB_ALIGN_UP(sizeof(mem_block), 16);
  2000. static void upb_arena_addblock(upb_arena *a, void *ptr, size_t size) {
  2001. mem_block *block = ptr;
  2002. block->next = a->freelist;
  2003. block->size = (uint32_t)size;
  2004. block->cleanups = 0;
  2005. a->freelist = block;
  2006. a->last_size = block->size;
  2007. if (!a->freelist_tail) a->freelist_tail = block;
  2008. a->head.ptr = UPB_PTR_AT(block, memblock_reserve, char);
  2009. a->head.end = UPB_PTR_AT(block, size, char);
  2010. a->cleanups = &block->cleanups;
  2011. /* TODO(haberman): ASAN poison. */
  2012. }
  2013. static bool upb_arena_allocblock(upb_arena *a, size_t size) {
  2014. size_t block_size = UPB_MAX(size, a->last_size * 2) + memblock_reserve;
  2015. mem_block *block = upb_malloc(a->block_alloc, block_size);
  2016. if (!block) return false;
  2017. upb_arena_addblock(a, block, block_size);
  2018. return true;
  2019. }
  2020. static bool arena_has(upb_arena *a, size_t size) {
  2021. _upb_arena_head *h = (_upb_arena_head*)a;
  2022. return (size_t)(h->end - h->ptr) >= size;
  2023. }
  2024. void *_upb_arena_slowmalloc(upb_arena *a, size_t size) {
  2025. if (!upb_arena_allocblock(a, size)) return NULL; /* Out of memory. */
  2026. UPB_ASSERT(arena_has(a, size));
  2027. return upb_arena_malloc(a, size);
  2028. }
  2029. static void *upb_arena_doalloc(upb_alloc *alloc, void *ptr, size_t oldsize,
  2030. size_t size) {
  2031. upb_arena *a = (upb_arena*)alloc; /* upb_alloc is initial member. */
  2032. return upb_arena_realloc(a, ptr, oldsize, size);
  2033. }
  2034. static upb_arena *arena_findroot(upb_arena *a) {
  2035. /* Path splitting keeps time complexity down, see:
  2036. * https://en.wikipedia.org/wiki/Disjoint-set_data_structure */
  2037. while (a->parent != a) {
  2038. upb_arena *next = a->parent;
  2039. a->parent = next->parent;
  2040. a = next;
  2041. }
  2042. return a;
  2043. }
  2044. /* Public Arena API ***********************************************************/
  2045. upb_arena *arena_initslow(void *mem, size_t n, upb_alloc *alloc) {
  2046. const size_t first_block_overhead = sizeof(upb_arena) + memblock_reserve;
  2047. upb_arena *a;
  2048. /* We need to malloc the initial block. */
  2049. n = first_block_overhead + 256;
  2050. if (!alloc || !(mem = upb_malloc(alloc, n))) {
  2051. return NULL;
  2052. }
  2053. a = UPB_PTR_AT(mem, n - sizeof(*a), upb_arena);
  2054. n -= sizeof(*a);
  2055. a->head.alloc.func = &upb_arena_doalloc;
  2056. a->block_alloc = alloc;
  2057. a->parent = a;
  2058. a->refcount = 1;
  2059. a->freelist = NULL;
  2060. a->freelist_tail = NULL;
  2061. upb_arena_addblock(a, mem, n);
  2062. return a;
  2063. }
  2064. upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc) {
  2065. upb_arena *a;
  2066. /* Round block size down to alignof(*a) since we will allocate the arena
  2067. * itself at the end. */
  2068. n = UPB_ALIGN_DOWN(n, UPB_ALIGN_OF(upb_arena));
  2069. if (UPB_UNLIKELY(n < sizeof(upb_arena))) {
  2070. return arena_initslow(mem, n, alloc);
  2071. }
  2072. a = UPB_PTR_AT(mem, n - sizeof(*a), upb_arena);
  2073. n -= sizeof(*a);
  2074. a->head.alloc.func = &upb_arena_doalloc;
  2075. a->block_alloc = alloc;
  2076. a->parent = a;
  2077. a->refcount = 1;
  2078. a->last_size = 128;
  2079. a->head.ptr = mem;
  2080. a->head.end = UPB_PTR_AT(mem, n, char);
  2081. a->freelist = NULL;
  2082. a->cleanups = NULL;
  2083. return a;
  2084. }
  2085. static void arena_dofree(upb_arena *a) {
  2086. mem_block *block = a->freelist;
  2087. UPB_ASSERT(a->parent == a);
  2088. UPB_ASSERT(a->refcount == 0);
  2089. while (block) {
  2090. /* Load first since we are deleting block. */
  2091. mem_block *next = block->next;
  2092. if (block->cleanups > 0) {
  2093. cleanup_ent *end = UPB_PTR_AT(block, block->size, void);
  2094. cleanup_ent *ptr = end - block->cleanups;
  2095. for (; ptr < end; ptr++) {
  2096. ptr->cleanup(ptr->ud);
  2097. }
  2098. }
  2099. upb_free(a->block_alloc, block);
  2100. block = next;
  2101. }
  2102. }
  2103. void upb_arena_free(upb_arena *a) {
  2104. a = arena_findroot(a);
  2105. if (--a->refcount == 0) arena_dofree(a);
  2106. }
  2107. bool upb_arena_addcleanup(upb_arena *a, void *ud, upb_cleanup_func *func) {
  2108. cleanup_ent *ent;
  2109. if (!a->cleanups || !arena_has(a, sizeof(cleanup_ent))) {
  2110. if (!upb_arena_allocblock(a, 128)) return false; /* Out of memory. */
  2111. UPB_ASSERT(arena_has(a, sizeof(cleanup_ent)));
  2112. }
  2113. a->head.end -= sizeof(cleanup_ent);
  2114. ent = (cleanup_ent*)a->head.end;
  2115. (*a->cleanups)++;
  2116. ent->cleanup = func;
  2117. ent->ud = ud;
  2118. return true;
  2119. }
  2120. void upb_arena_fuse(upb_arena *a1, upb_arena *a2) {
  2121. upb_arena *r1 = arena_findroot(a1);
  2122. upb_arena *r2 = arena_findroot(a2);
  2123. if (r1 == r2) return; /* Already fused. */
  2124. /* We want to join the smaller tree to the larger tree.
  2125. * So swap first if they are backwards. */
  2126. if (r1->refcount < r2->refcount) {
  2127. upb_arena *tmp = r1;
  2128. r1 = r2;
  2129. r2 = tmp;
  2130. }
  2131. /* r1 takes over r2's freelist and refcount. */
  2132. r1->refcount += r2->refcount;
  2133. if (r2->freelist_tail) {
  2134. UPB_ASSERT(r2->freelist_tail->next == NULL);
  2135. r2->freelist_tail->next = r1->freelist;
  2136. r1->freelist = r2->freelist;
  2137. }
  2138. r2->parent = r1;
  2139. }
  2140. /* This file was generated by upbc (the upb compiler) from the input
  2141. * file:
  2142. *
  2143. * google/protobuf/descriptor.proto
  2144. *
  2145. * Do not edit -- your changes will be discarded when the file is
  2146. * regenerated. */
  2147. #include <stddef.h>
  2148. static const upb_msglayout *const google_protobuf_FileDescriptorSet_submsgs[1] = {
  2149. &google_protobuf_FileDescriptorProto_msginit,
  2150. };
  2151. static const upb_msglayout_field google_protobuf_FileDescriptorSet__fields[1] = {
  2152. {1, UPB_SIZE(0, 0), 0, 0, 11, 3},
  2153. };
  2154. const upb_msglayout google_protobuf_FileDescriptorSet_msginit = {
  2155. &google_protobuf_FileDescriptorSet_submsgs[0],
  2156. &google_protobuf_FileDescriptorSet__fields[0],
  2157. UPB_SIZE(4, 8), 1, false,
  2158. };
  2159. static const upb_msglayout *const google_protobuf_FileDescriptorProto_submsgs[6] = {
  2160. &google_protobuf_DescriptorProto_msginit,
  2161. &google_protobuf_EnumDescriptorProto_msginit,
  2162. &google_protobuf_FieldDescriptorProto_msginit,
  2163. &google_protobuf_FileOptions_msginit,
  2164. &google_protobuf_ServiceDescriptorProto_msginit,
  2165. &google_protobuf_SourceCodeInfo_msginit,
  2166. };
  2167. static const upb_msglayout_field google_protobuf_FileDescriptorProto__fields[12] = {
  2168. {1, UPB_SIZE(4, 8), 1, 0, 9, 1},
  2169. {2, UPB_SIZE(12, 24), 2, 0, 9, 1},
  2170. {3, UPB_SIZE(36, 72), 0, 0, 9, 3},
  2171. {4, UPB_SIZE(40, 80), 0, 0, 11, 3},
  2172. {5, UPB_SIZE(44, 88), 0, 1, 11, 3},
  2173. {6, UPB_SIZE(48, 96), 0, 4, 11, 3},
  2174. {7, UPB_SIZE(52, 104), 0, 2, 11, 3},
  2175. {8, UPB_SIZE(28, 56), 4, 3, 11, 1},
  2176. {9, UPB_SIZE(32, 64), 5, 5, 11, 1},
  2177. {10, UPB_SIZE(56, 112), 0, 0, 5, 3},
  2178. {11, UPB_SIZE(60, 120), 0, 0, 5, 3},
  2179. {12, UPB_SIZE(20, 40), 3, 0, 9, 1},
  2180. };
  2181. const upb_msglayout google_protobuf_FileDescriptorProto_msginit = {
  2182. &google_protobuf_FileDescriptorProto_submsgs[0],
  2183. &google_protobuf_FileDescriptorProto__fields[0],
  2184. UPB_SIZE(64, 128), 12, false,
  2185. };
  2186. static const upb_msglayout *const google_protobuf_DescriptorProto_submsgs[8] = {
  2187. &google_protobuf_DescriptorProto_msginit,
  2188. &google_protobuf_DescriptorProto_ExtensionRange_msginit,
  2189. &google_protobuf_DescriptorProto_ReservedRange_msginit,
  2190. &google_protobuf_EnumDescriptorProto_msginit,
  2191. &google_protobuf_FieldDescriptorProto_msginit,
  2192. &google_protobuf_MessageOptions_msginit,
  2193. &google_protobuf_OneofDescriptorProto_msginit,
  2194. };
  2195. static const upb_msglayout_field google_protobuf_DescriptorProto__fields[10] = {
  2196. {1, UPB_SIZE(4, 8), 1, 0, 9, 1},
  2197. {2, UPB_SIZE(16, 32), 0, 4, 11, 3},
  2198. {3, UPB_SIZE(20, 40), 0, 0, 11, 3},
  2199. {4, UPB_SIZE(24, 48), 0, 3, 11, 3},
  2200. {5, UPB_SIZE(28, 56), 0, 1, 11, 3},
  2201. {6, UPB_SIZE(32, 64), 0, 4, 11, 3},
  2202. {7, UPB_SIZE(12, 24), 2, 5, 11, 1},
  2203. {8, UPB_SIZE(36, 72), 0, 6, 11, 3},
  2204. {9, UPB_SIZE(40, 80), 0, 2, 11, 3},
  2205. {10, UPB_SIZE(44, 88), 0, 0, 9, 3},
  2206. };
  2207. const upb_msglayout google_protobuf_DescriptorProto_msginit = {
  2208. &google_protobuf_DescriptorProto_submsgs[0],
  2209. &google_protobuf_DescriptorProto__fields[0],
  2210. UPB_SIZE(48, 96), 10, false,
  2211. };
  2212. static const upb_msglayout *const google_protobuf_DescriptorProto_ExtensionRange_submsgs[1] = {
  2213. &google_protobuf_ExtensionRangeOptions_msginit,
  2214. };
  2215. static const upb_msglayout_field google_protobuf_DescriptorProto_ExtensionRange__fields[3] = {
  2216. {1, UPB_SIZE(4, 4), 1, 0, 5, 1},
  2217. {2, UPB_SIZE(8, 8), 2, 0, 5, 1},
  2218. {3, UPB_SIZE(12, 16), 3, 0, 11, 1},
  2219. };
  2220. const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit = {
  2221. &google_protobuf_DescriptorProto_ExtensionRange_submsgs[0],
  2222. &google_protobuf_DescriptorProto_ExtensionRange__fields[0],
  2223. UPB_SIZE(16, 24), 3, false,
  2224. };
  2225. static const upb_msglayout_field google_protobuf_DescriptorProto_ReservedRange__fields[2] = {
  2226. {1, UPB_SIZE(4, 4), 1, 0, 5, 1},
  2227. {2, UPB_SIZE(8, 8), 2, 0, 5, 1},
  2228. };
  2229. const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit = {
  2230. NULL,
  2231. &google_protobuf_DescriptorProto_ReservedRange__fields[0],
  2232. UPB_SIZE(12, 12), 2, false,
  2233. };
  2234. static const upb_msglayout *const google_protobuf_ExtensionRangeOptions_submsgs[1] = {
  2235. &google_protobuf_UninterpretedOption_msginit,
  2236. };
  2237. static const upb_msglayout_field google_protobuf_ExtensionRangeOptions__fields[1] = {
  2238. {999, UPB_SIZE(0, 0), 0, 0, 11, 3},
  2239. };
  2240. const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit = {
  2241. &google_protobuf_ExtensionRangeOptions_submsgs[0],
  2242. &google_protobuf_ExtensionRangeOptions__fields[0],
  2243. UPB_SIZE(4, 8), 1, false,
  2244. };
  2245. static const upb_msglayout *const google_protobuf_FieldDescriptorProto_submsgs[1] = {
  2246. &google_protobuf_FieldOptions_msginit,
  2247. };
  2248. static const upb_msglayout_field google_protobuf_FieldDescriptorProto__fields[11] = {
  2249. {1, UPB_SIZE(36, 40), 6, 0, 9, 1},
  2250. {2, UPB_SIZE(44, 56), 7, 0, 9, 1},
  2251. {3, UPB_SIZE(24, 24), 3, 0, 5, 1},
  2252. {4, UPB_SIZE(8, 8), 1, 0, 14, 1},
  2253. {5, UPB_SIZE(16, 16), 2, 0, 14, 1},
  2254. {6, UPB_SIZE(52, 72), 8, 0, 9, 1},
  2255. {7, UPB_SIZE(60, 88), 9, 0, 9, 1},
  2256. {8, UPB_SIZE(76, 120), 11, 0, 11, 1},
  2257. {9, UPB_SIZE(28, 28), 4, 0, 5, 1},
  2258. {10, UPB_SIZE(68, 104), 10, 0, 9, 1},
  2259. {17, UPB_SIZE(32, 32), 5, 0, 8, 1},
  2260. };
  2261. const upb_msglayout google_protobuf_FieldDescriptorProto_msginit = {
  2262. &google_protobuf_FieldDescriptorProto_submsgs[0],
  2263. &google_protobuf_FieldDescriptorProto__fields[0],
  2264. UPB_SIZE(80, 128), 11, false,
  2265. };
  2266. static const upb_msglayout *const google_protobuf_OneofDescriptorProto_submsgs[1] = {
  2267. &google_protobuf_OneofOptions_msginit,
  2268. };
  2269. static const upb_msglayout_field google_protobuf_OneofDescriptorProto__fields[2] = {
  2270. {1, UPB_SIZE(4, 8), 1, 0, 9, 1},
  2271. {2, UPB_SIZE(12, 24), 2, 0, 11, 1},
  2272. };
  2273. const upb_msglayout google_protobuf_OneofDescriptorProto_msginit = {
  2274. &google_protobuf_OneofDescriptorProto_submsgs[0],
  2275. &google_protobuf_OneofDescriptorProto__fields[0],
  2276. UPB_SIZE(16, 32), 2, false,
  2277. };
  2278. static const upb_msglayout *const google_protobuf_EnumDescriptorProto_submsgs[3] = {
  2279. &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit,
  2280. &google_protobuf_EnumOptions_msginit,
  2281. &google_protobuf_EnumValueDescriptorProto_msginit,
  2282. };
  2283. static const upb_msglayout_field google_protobuf_EnumDescriptorProto__fields[5] = {
  2284. {1, UPB_SIZE(4, 8), 1, 0, 9, 1},
  2285. {2, UPB_SIZE(16, 32), 0, 2, 11, 3},
  2286. {3, UPB_SIZE(12, 24), 2, 1, 11, 1},
  2287. {4, UPB_SIZE(20, 40), 0, 0, 11, 3},
  2288. {5, UPB_SIZE(24, 48), 0, 0, 9, 3},
  2289. };
  2290. const upb_msglayout google_protobuf_EnumDescriptorProto_msginit = {
  2291. &google_protobuf_EnumDescriptorProto_submsgs[0],
  2292. &google_protobuf_EnumDescriptorProto__fields[0],
  2293. UPB_SIZE(32, 64), 5, false,
  2294. };
  2295. static const upb_msglayout_field google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[2] = {
  2296. {1, UPB_SIZE(4, 4), 1, 0, 5, 1},
  2297. {2, UPB_SIZE(8, 8), 2, 0, 5, 1},
  2298. };
  2299. const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit = {
  2300. NULL,
  2301. &google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[0],
  2302. UPB_SIZE(12, 12), 2, false,
  2303. };
  2304. static const upb_msglayout *const google_protobuf_EnumValueDescriptorProto_submsgs[1] = {
  2305. &google_protobuf_EnumValueOptions_msginit,
  2306. };
  2307. static const upb_msglayout_field google_protobuf_EnumValueDescriptorProto__fields[3] = {
  2308. {1, UPB_SIZE(8, 8), 2, 0, 9, 1},
  2309. {2, UPB_SIZE(4, 4), 1, 0, 5, 1},
  2310. {3, UPB_SIZE(16, 24), 3, 0, 11, 1},
  2311. };
  2312. const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit = {
  2313. &google_protobuf_EnumValueDescriptorProto_submsgs[0],
  2314. &google_protobuf_EnumValueDescriptorProto__fields[0],
  2315. UPB_SIZE(24, 32), 3, false,
  2316. };
  2317. static const upb_msglayout *const google_protobuf_ServiceDescriptorProto_submsgs[2] = {
  2318. &google_protobuf_MethodDescriptorProto_msginit,
  2319. &google_protobuf_ServiceOptions_msginit,
  2320. };
  2321. static const upb_msglayout_field google_protobuf_ServiceDescriptorProto__fields[3] = {
  2322. {1, UPB_SIZE(4, 8), 1, 0, 9, 1},
  2323. {2, UPB_SIZE(16, 32), 0, 0, 11, 3},
  2324. {3, UPB_SIZE(12, 24), 2, 1, 11, 1},
  2325. };
  2326. const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit = {
  2327. &google_protobuf_ServiceDescriptorProto_submsgs[0],
  2328. &google_protobuf_ServiceDescriptorProto__fields[0],
  2329. UPB_SIZE(24, 48), 3, false,
  2330. };
  2331. static const upb_msglayout *const google_protobuf_MethodDescriptorProto_submsgs[1] = {
  2332. &google_protobuf_MethodOptions_msginit,
  2333. };
  2334. static const upb_msglayout_field google_protobuf_MethodDescriptorProto__fields[6] = {
  2335. {1, UPB_SIZE(4, 8), 3, 0, 9, 1},
  2336. {2, UPB_SIZE(12, 24), 4, 0, 9, 1},
  2337. {3, UPB_SIZE(20, 40), 5, 0, 9, 1},
  2338. {4, UPB_SIZE(28, 56), 6, 0, 11, 1},
  2339. {5, UPB_SIZE(1, 1), 1, 0, 8, 1},
  2340. {6, UPB_SIZE(2, 2), 2, 0, 8, 1},
  2341. };
  2342. const upb_msglayout google_protobuf_MethodDescriptorProto_msginit = {
  2343. &google_protobuf_MethodDescriptorProto_submsgs[0],
  2344. &google_protobuf_MethodDescriptorProto__fields[0],
  2345. UPB_SIZE(32, 64), 6, false,
  2346. };
  2347. static const upb_msglayout *const google_protobuf_FileOptions_submsgs[1] = {
  2348. &google_protobuf_UninterpretedOption_msginit,
  2349. };
  2350. static const upb_msglayout_field google_protobuf_FileOptions__fields[21] = {
  2351. {1, UPB_SIZE(28, 32), 11, 0, 9, 1},
  2352. {8, UPB_SIZE(36, 48), 12, 0, 9, 1},
  2353. {9, UPB_SIZE(8, 8), 1, 0, 14, 1},
  2354. {10, UPB_SIZE(16, 16), 2, 0, 8, 1},
  2355. {11, UPB_SIZE(44, 64), 13, 0, 9, 1},
  2356. {16, UPB_SIZE(17, 17), 3, 0, 8, 1},
  2357. {17, UPB_SIZE(18, 18), 4, 0, 8, 1},
  2358. {18, UPB_SIZE(19, 19), 5, 0, 8, 1},
  2359. {20, UPB_SIZE(20, 20), 6, 0, 8, 1},
  2360. {23, UPB_SIZE(21, 21), 7, 0, 8, 1},
  2361. {27, UPB_SIZE(22, 22), 8, 0, 8, 1},
  2362. {31, UPB_SIZE(23, 23), 9, 0, 8, 1},
  2363. {36, UPB_SIZE(52, 80), 14, 0, 9, 1},
  2364. {37, UPB_SIZE(60, 96), 15, 0, 9, 1},
  2365. {39, UPB_SIZE(68, 112), 16, 0, 9, 1},
  2366. {40, UPB_SIZE(76, 128), 17, 0, 9, 1},
  2367. {41, UPB_SIZE(84, 144), 18, 0, 9, 1},
  2368. {42, UPB_SIZE(24, 24), 10, 0, 8, 1},
  2369. {44, UPB_SIZE(92, 160), 19, 0, 9, 1},
  2370. {45, UPB_SIZE(100, 176), 20, 0, 9, 1},
  2371. {999, UPB_SIZE(108, 192), 0, 0, 11, 3},
  2372. };
  2373. const upb_msglayout google_protobuf_FileOptions_msginit = {
  2374. &google_protobuf_FileOptions_submsgs[0],
  2375. &google_protobuf_FileOptions__fields[0],
  2376. UPB_SIZE(112, 208), 21, false,
  2377. };
  2378. static const upb_msglayout *const google_protobuf_MessageOptions_submsgs[1] = {
  2379. &google_protobuf_UninterpretedOption_msginit,
  2380. };
  2381. static const upb_msglayout_field google_protobuf_MessageOptions__fields[5] = {
  2382. {1, UPB_SIZE(1, 1), 1, 0, 8, 1},
  2383. {2, UPB_SIZE(2, 2), 2, 0, 8, 1},
  2384. {3, UPB_SIZE(3, 3), 3, 0, 8, 1},
  2385. {7, UPB_SIZE(4, 4), 4, 0, 8, 1},
  2386. {999, UPB_SIZE(8, 8), 0, 0, 11, 3},
  2387. };
  2388. const upb_msglayout google_protobuf_MessageOptions_msginit = {
  2389. &google_protobuf_MessageOptions_submsgs[0],
  2390. &google_protobuf_MessageOptions__fields[0],
  2391. UPB_SIZE(12, 16), 5, false,
  2392. };
  2393. static const upb_msglayout *const google_protobuf_FieldOptions_submsgs[1] = {
  2394. &google_protobuf_UninterpretedOption_msginit,
  2395. };
  2396. static const upb_msglayout_field google_protobuf_FieldOptions__fields[7] = {
  2397. {1, UPB_SIZE(8, 8), 1, 0, 14, 1},
  2398. {2, UPB_SIZE(24, 24), 3, 0, 8, 1},
  2399. {3, UPB_SIZE(25, 25), 4, 0, 8, 1},
  2400. {5, UPB_SIZE(26, 26), 5, 0, 8, 1},
  2401. {6, UPB_SIZE(16, 16), 2, 0, 14, 1},
  2402. {10, UPB_SIZE(27, 27), 6, 0, 8, 1},
  2403. {999, UPB_SIZE(28, 32), 0, 0, 11, 3},
  2404. };
  2405. const upb_msglayout google_protobuf_FieldOptions_msginit = {
  2406. &google_protobuf_FieldOptions_submsgs[0],
  2407. &google_protobuf_FieldOptions__fields[0],
  2408. UPB_SIZE(32, 40), 7, false,
  2409. };
  2410. static const upb_msglayout *const google_protobuf_OneofOptions_submsgs[1] = {
  2411. &google_protobuf_UninterpretedOption_msginit,
  2412. };
  2413. static const upb_msglayout_field google_protobuf_OneofOptions__fields[1] = {
  2414. {999, UPB_SIZE(0, 0), 0, 0, 11, 3},
  2415. };
  2416. const upb_msglayout google_protobuf_OneofOptions_msginit = {
  2417. &google_protobuf_OneofOptions_submsgs[0],
  2418. &google_protobuf_OneofOptions__fields[0],
  2419. UPB_SIZE(4, 8), 1, false,
  2420. };
  2421. static const upb_msglayout *const google_protobuf_EnumOptions_submsgs[1] = {
  2422. &google_protobuf_UninterpretedOption_msginit,
  2423. };
  2424. static const upb_msglayout_field google_protobuf_EnumOptions__fields[3] = {
  2425. {2, UPB_SIZE(1, 1), 1, 0, 8, 1},
  2426. {3, UPB_SIZE(2, 2), 2, 0, 8, 1},
  2427. {999, UPB_SIZE(4, 8), 0, 0, 11, 3},
  2428. };
  2429. const upb_msglayout google_protobuf_EnumOptions_msginit = {
  2430. &google_protobuf_EnumOptions_submsgs[0],
  2431. &google_protobuf_EnumOptions__fields[0],
  2432. UPB_SIZE(8, 16), 3, false,
  2433. };
  2434. static const upb_msglayout *const google_protobuf_EnumValueOptions_submsgs[1] = {
  2435. &google_protobuf_UninterpretedOption_msginit,
  2436. };
  2437. static const upb_msglayout_field google_protobuf_EnumValueOptions__fields[2] = {
  2438. {1, UPB_SIZE(1, 1), 1, 0, 8, 1},
  2439. {999, UPB_SIZE(4, 8), 0, 0, 11, 3},
  2440. };
  2441. const upb_msglayout google_protobuf_EnumValueOptions_msginit = {
  2442. &google_protobuf_EnumValueOptions_submsgs[0],
  2443. &google_protobuf_EnumValueOptions__fields[0],
  2444. UPB_SIZE(8, 16), 2, false,
  2445. };
  2446. static const upb_msglayout *const google_protobuf_ServiceOptions_submsgs[1] = {
  2447. &google_protobuf_UninterpretedOption_msginit,
  2448. };
  2449. static const upb_msglayout_field google_protobuf_ServiceOptions__fields[2] = {
  2450. {33, UPB_SIZE(1, 1), 1, 0, 8, 1},
  2451. {999, UPB_SIZE(4, 8), 0, 0, 11, 3},
  2452. };
  2453. const upb_msglayout google_protobuf_ServiceOptions_msginit = {
  2454. &google_protobuf_ServiceOptions_submsgs[0],
  2455. &google_protobuf_ServiceOptions__fields[0],
  2456. UPB_SIZE(8, 16), 2, false,
  2457. };
  2458. static const upb_msglayout *const google_protobuf_MethodOptions_submsgs[1] = {
  2459. &google_protobuf_UninterpretedOption_msginit,
  2460. };
  2461. static const upb_msglayout_field google_protobuf_MethodOptions__fields[3] = {
  2462. {33, UPB_SIZE(16, 16), 2, 0, 8, 1},
  2463. {34, UPB_SIZE(8, 8), 1, 0, 14, 1},
  2464. {999, UPB_SIZE(20, 24), 0, 0, 11, 3},
  2465. };
  2466. const upb_msglayout google_protobuf_MethodOptions_msginit = {
  2467. &google_protobuf_MethodOptions_submsgs[0],
  2468. &google_protobuf_MethodOptions__fields[0],
  2469. UPB_SIZE(24, 32), 3, false,
  2470. };
  2471. static const upb_msglayout *const google_protobuf_UninterpretedOption_submsgs[1] = {
  2472. &google_protobuf_UninterpretedOption_NamePart_msginit,
  2473. };
  2474. static const upb_msglayout_field google_protobuf_UninterpretedOption__fields[7] = {
  2475. {2, UPB_SIZE(56, 80), 0, 0, 11, 3},
  2476. {3, UPB_SIZE(32, 32), 4, 0, 9, 1},
  2477. {4, UPB_SIZE(8, 8), 1, 0, 4, 1},
  2478. {5, UPB_SIZE(16, 16), 2, 0, 3, 1},
  2479. {6, UPB_SIZE(24, 24), 3, 0, 1, 1},
  2480. {7, UPB_SIZE(40, 48), 5, 0, 12, 1},
  2481. {8, UPB_SIZE(48, 64), 6, 0, 9, 1},
  2482. };
  2483. const upb_msglayout google_protobuf_UninterpretedOption_msginit = {
  2484. &google_protobuf_UninterpretedOption_submsgs[0],
  2485. &google_protobuf_UninterpretedOption__fields[0],
  2486. UPB_SIZE(64, 96), 7, false,
  2487. };
  2488. static const upb_msglayout_field google_protobuf_UninterpretedOption_NamePart__fields[2] = {
  2489. {1, UPB_SIZE(4, 8), 2, 0, 9, 2},
  2490. {2, UPB_SIZE(1, 1), 1, 0, 8, 2},
  2491. };
  2492. const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit = {
  2493. NULL,
  2494. &google_protobuf_UninterpretedOption_NamePart__fields[0],
  2495. UPB_SIZE(16, 32), 2, false,
  2496. };
  2497. static const upb_msglayout *const google_protobuf_SourceCodeInfo_submsgs[1] = {
  2498. &google_protobuf_SourceCodeInfo_Location_msginit,
  2499. };
  2500. static const upb_msglayout_field google_protobuf_SourceCodeInfo__fields[1] = {
  2501. {1, UPB_SIZE(0, 0), 0, 0, 11, 3},
  2502. };
  2503. const upb_msglayout google_protobuf_SourceCodeInfo_msginit = {
  2504. &google_protobuf_SourceCodeInfo_submsgs[0],
  2505. &google_protobuf_SourceCodeInfo__fields[0],
  2506. UPB_SIZE(4, 8), 1, false,
  2507. };
  2508. static const upb_msglayout_field google_protobuf_SourceCodeInfo_Location__fields[5] = {
  2509. {1, UPB_SIZE(20, 40), 0, 0, 5, _UPB_LABEL_PACKED},
  2510. {2, UPB_SIZE(24, 48), 0, 0, 5, _UPB_LABEL_PACKED},
  2511. {3, UPB_SIZE(4, 8), 1, 0, 9, 1},
  2512. {4, UPB_SIZE(12, 24), 2, 0, 9, 1},
  2513. {6, UPB_SIZE(28, 56), 0, 0, 9, 3},
  2514. };
  2515. const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit = {
  2516. NULL,
  2517. &google_protobuf_SourceCodeInfo_Location__fields[0],
  2518. UPB_SIZE(32, 64), 5, false,
  2519. };
  2520. static const upb_msglayout *const google_protobuf_GeneratedCodeInfo_submsgs[1] = {
  2521. &google_protobuf_GeneratedCodeInfo_Annotation_msginit,
  2522. };
  2523. static const upb_msglayout_field google_protobuf_GeneratedCodeInfo__fields[1] = {
  2524. {1, UPB_SIZE(0, 0), 0, 0, 11, 3},
  2525. };
  2526. const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit = {
  2527. &google_protobuf_GeneratedCodeInfo_submsgs[0],
  2528. &google_protobuf_GeneratedCodeInfo__fields[0],
  2529. UPB_SIZE(4, 8), 1, false,
  2530. };
  2531. static const upb_msglayout_field google_protobuf_GeneratedCodeInfo_Annotation__fields[4] = {
  2532. {1, UPB_SIZE(20, 32), 0, 0, 5, _UPB_LABEL_PACKED},
  2533. {2, UPB_SIZE(12, 16), 3, 0, 9, 1},
  2534. {3, UPB_SIZE(4, 4), 1, 0, 5, 1},
  2535. {4, UPB_SIZE(8, 8), 2, 0, 5, 1},
  2536. };
  2537. const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit = {
  2538. NULL,
  2539. &google_protobuf_GeneratedCodeInfo_Annotation__fields[0],
  2540. UPB_SIZE(24, 48), 4, false,
  2541. };
  2542. /* This file was generated by upbc (the upb compiler) from the input
  2543. * file:
  2544. *
  2545. * google/protobuf/descriptor.proto
  2546. *
  2547. * Do not edit -- your changes will be discarded when the file is
  2548. * regenerated. */
  2549. extern const upb_msglayout google_protobuf_FileDescriptorSet_msginit;
  2550. extern const upb_msglayout google_protobuf_FileDescriptorProto_msginit;
  2551. extern const upb_msglayout google_protobuf_DescriptorProto_msginit;
  2552. extern const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit;
  2553. extern const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit;
  2554. extern const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit;
  2555. extern const upb_msglayout google_protobuf_FieldDescriptorProto_msginit;
  2556. extern const upb_msglayout google_protobuf_OneofDescriptorProto_msginit;
  2557. extern const upb_msglayout google_protobuf_EnumDescriptorProto_msginit;
  2558. extern const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit;
  2559. extern const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit;
  2560. extern const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit;
  2561. extern const upb_msglayout google_protobuf_MethodDescriptorProto_msginit;
  2562. extern const upb_msglayout google_protobuf_FileOptions_msginit;
  2563. extern const upb_msglayout google_protobuf_MessageOptions_msginit;
  2564. extern const upb_msglayout google_protobuf_FieldOptions_msginit;
  2565. extern const upb_msglayout google_protobuf_OneofOptions_msginit;
  2566. extern const upb_msglayout google_protobuf_EnumOptions_msginit;
  2567. extern const upb_msglayout google_protobuf_EnumValueOptions_msginit;
  2568. extern const upb_msglayout google_protobuf_ServiceOptions_msginit;
  2569. extern const upb_msglayout google_protobuf_MethodOptions_msginit;
  2570. extern const upb_msglayout google_protobuf_UninterpretedOption_msginit;
  2571. extern const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit;
  2572. extern const upb_msglayout google_protobuf_SourceCodeInfo_msginit;
  2573. extern const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit;
  2574. extern const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit;
  2575. extern const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit;
  2576. static const upb_msglayout *layouts[27] = {
  2577. &google_protobuf_FileDescriptorSet_msginit,
  2578. &google_protobuf_FileDescriptorProto_msginit,
  2579. &google_protobuf_DescriptorProto_msginit,
  2580. &google_protobuf_DescriptorProto_ExtensionRange_msginit,
  2581. &google_protobuf_DescriptorProto_ReservedRange_msginit,
  2582. &google_protobuf_ExtensionRangeOptions_msginit,
  2583. &google_protobuf_FieldDescriptorProto_msginit,
  2584. &google_protobuf_OneofDescriptorProto_msginit,
  2585. &google_protobuf_EnumDescriptorProto_msginit,
  2586. &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit,
  2587. &google_protobuf_EnumValueDescriptorProto_msginit,
  2588. &google_protobuf_ServiceDescriptorProto_msginit,
  2589. &google_protobuf_MethodDescriptorProto_msginit,
  2590. &google_protobuf_FileOptions_msginit,
  2591. &google_protobuf_MessageOptions_msginit,
  2592. &google_protobuf_FieldOptions_msginit,
  2593. &google_protobuf_OneofOptions_msginit,
  2594. &google_protobuf_EnumOptions_msginit,
  2595. &google_protobuf_EnumValueOptions_msginit,
  2596. &google_protobuf_ServiceOptions_msginit,
  2597. &google_protobuf_MethodOptions_msginit,
  2598. &google_protobuf_UninterpretedOption_msginit,
  2599. &google_protobuf_UninterpretedOption_NamePart_msginit,
  2600. &google_protobuf_SourceCodeInfo_msginit,
  2601. &google_protobuf_SourceCodeInfo_Location_msginit,
  2602. &google_protobuf_GeneratedCodeInfo_msginit,
  2603. &google_protobuf_GeneratedCodeInfo_Annotation_msginit,
  2604. };
  2605. static const char descriptor[7619] = {'\n', ' ', 'g', 'o', 'o', 'g', 'l', 'e', '/', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '/', 'd', 'e', 's', 'c', 'r', 'i', 'p',
  2606. 't', 'o', 'r', '.', 'p', 'r', 'o', 't', 'o', '\022', '\017', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u',
  2607. 'f', '\"', 'M', '\n', '\021', 'F', 'i', 'l', 'e', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'S', 'e', 't', '\022', '8', '\n',
  2608. '\004', 'f', 'i', 'l', 'e', '\030', '\001', ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't',
  2609. 'o', 'b', 'u', 'f', '.', 'F', 'i', 'l', 'e', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R',
  2610. '\004', 'f', 'i', 'l', 'e', '\"', '\344', '\004', '\n', '\023', 'F', 'i', 'l', 'e', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P',
  2611. 'r', 'o', 't', 'o', '\022', '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e', '\022',
  2612. '\030', '\n', '\007', 'p', 'a', 'c', 'k', 'a', 'g', 'e', '\030', '\002', ' ', '\001', '(', '\t', 'R', '\007', 'p', 'a', 'c', 'k', 'a', 'g', 'e',
  2613. '\022', '\036', '\n', '\n', 'd', 'e', 'p', 'e', 'n', 'd', 'e', 'n', 'c', 'y', '\030', '\003', ' ', '\003', '(', '\t', 'R', '\n', 'd', 'e', 'p',
  2614. 'e', 'n', 'd', 'e', 'n', 'c', 'y', '\022', '+', '\n', '\021', 'p', 'u', 'b', 'l', 'i', 'c', '_', 'd', 'e', 'p', 'e', 'n', 'd', 'e',
  2615. 'n', 'c', 'y', '\030', '\n', ' ', '\003', '(', '\005', 'R', '\020', 'p', 'u', 'b', 'l', 'i', 'c', 'D', 'e', 'p', 'e', 'n', 'd', 'e', 'n',
  2616. 'c', 'y', '\022', '\'', '\n', '\017', 'w', 'e', 'a', 'k', '_', 'd', 'e', 'p', 'e', 'n', 'd', 'e', 'n', 'c', 'y', '\030', '\013', ' ', '\003',
  2617. '(', '\005', 'R', '\016', 'w', 'e', 'a', 'k', 'D', 'e', 'p', 'e', 'n', 'd', 'e', 'n', 'c', 'y', '\022', 'C', '\n', '\014', 'm', 'e', 's',
  2618. 's', 'a', 'g', 'e', '_', 't', 'y', 'p', 'e', '\030', '\004', ' ', '\003', '(', '\013', '2', ' ', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.',
  2619. 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R',
  2620. '\013', 'm', 'e', 's', 's', 'a', 'g', 'e', 'T', 'y', 'p', 'e', '\022', 'A', '\n', '\t', 'e', 'n', 'u', 'm', '_', 't', 'y', 'p', 'e',
  2621. '\030', '\005', ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.',
  2622. 'E', 'n', 'u', 'm', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\010', 'e', 'n', 'u', 'm',
  2623. 'T', 'y', 'p', 'e', '\022', 'A', '\n', '\007', 's', 'e', 'r', 'v', 'i', 'c', 'e', '\030', '\006', ' ', '\003', '(', '\013', '2', '\'', '.', 'g',
  2624. 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 'D', 'e', 's',
  2625. 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\007', 's', 'e', 'r', 'v', 'i', 'c', 'e', '\022', 'C', '\n', '\t',
  2626. 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\030', '\007', ' ', '\003', '(', '\013', '2', '%', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.',
  2627. 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P',
  2628. 'r', 'o', 't', 'o', 'R', '\t', 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\022', '6', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n',
  2629. 's', '\030', '\010', ' ', '\001', '(', '\013', '2', '\034', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f',
  2630. '.', 'F', 'i', 'l', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\022', 'I', '\n', '\020',
  2631. 's', 'o', 'u', 'r', 'c', 'e', '_', 'c', 'o', 'd', 'e', '_', 'i', 'n', 'f', 'o', '\030', '\t', ' ', '\001', '(', '\013', '2', '\037', '.',
  2632. 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'S', 'o', 'u', 'r', 'c', 'e', 'C', 'o', 'd',
  2633. 'e', 'I', 'n', 'f', 'o', 'R', '\016', 's', 'o', 'u', 'r', 'c', 'e', 'C', 'o', 'd', 'e', 'I', 'n', 'f', 'o', '\022', '\026', '\n', '\006',
  2634. 's', 'y', 'n', 't', 'a', 'x', '\030', '\014', ' ', '\001', '(', '\t', 'R', '\006', 's', 'y', 'n', 't', 'a', 'x', '\"', '\271', '\006', '\n', '\017',
  2635. 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022', '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001',
  2636. ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e', '\022', ';', '\n', '\005', 'f', 'i', 'e', 'l', 'd', '\030', '\002', ' ', '\003', '(', '\013',
  2637. '2', '%', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'D',
  2638. 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\005', 'f', 'i', 'e', 'l', 'd', '\022', 'C', '\n', '\t',
  2639. 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\030', '\006', ' ', '\003', '(', '\013', '2', '%', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.',
  2640. 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P',
  2641. 'r', 'o', 't', 'o', 'R', '\t', 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\022', 'A', '\n', '\013', 'n', 'e', 's', 't', 'e', 'd',
  2642. '_', 't', 'y', 'p', 'e', '\030', '\003', ' ', '\003', '(', '\013', '2', ' ', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't',
  2643. 'o', 'b', 'u', 'f', '.', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\n', 'n', 'e', 's',
  2644. 't', 'e', 'd', 'T', 'y', 'p', 'e', '\022', 'A', '\n', '\t', 'e', 'n', 'u', 'm', '_', 't', 'y', 'p', 'e', '\030', '\004', ' ', '\003', '(',
  2645. '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'E', 'n', 'u', 'm', 'D',
  2646. 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\010', 'e', 'n', 'u', 'm', 'T', 'y', 'p', 'e', '\022',
  2647. 'X', '\n', '\017', 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '_', 'r', 'a', 'n', 'g', 'e', '\030', '\005', ' ', '\003', '(', '\013', '2',
  2648. '/', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'D', 'e', 's', 'c', 'r', 'i', 'p',
  2649. 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '.', 'E', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', 'R', 'a', 'n', 'g', 'e', 'R', '\016',
  2650. 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', 'R', 'a', 'n', 'g', 'e', '\022', 'D', '\n', '\n', 'o', 'n', 'e', 'o', 'f', '_', 'd',
  2651. 'e', 'c', 'l', '\030', '\010', ' ', '\003', '(', '\013', '2', '%', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b',
  2652. 'u', 'f', '.', 'O', 'n', 'e', 'o', 'f', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\t',
  2653. 'o', 'n', 'e', 'o', 'f', 'D', 'e', 'c', 'l', '\022', '9', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\030', '\007', ' ', '\001', '(',
  2654. '\013', '2', '\037', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'M', 'e', 's', 's', 'a',
  2655. 'g', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\022', 'U', '\n', '\016', 'r', 'e', 's',
  2656. 'e', 'r', 'v', 'e', 'd', '_', 'r', 'a', 'n', 'g', 'e', '\030', '\t', ' ', '\003', '(', '\013', '2', '.', '.', 'g', 'o', 'o', 'g', 'l',
  2657. 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't',
  2658. 'o', '.', 'R', 'e', 's', 'e', 'r', 'v', 'e', 'd', 'R', 'a', 'n', 'g', 'e', 'R', '\r', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd',
  2659. 'R', 'a', 'n', 'g', 'e', '\022', '#', '\n', '\r', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd', '_', 'n', 'a', 'm', 'e', '\030', '\n', ' ',
  2660. '\003', '(', '\t', 'R', '\014', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd', 'N', 'a', 'm', 'e', '\032', 'z', '\n', '\016', 'E', 'x', 't', 'e',
  2661. 'n', 's', 'i', 'o', 'n', 'R', 'a', 'n', 'g', 'e', '\022', '\024', '\n', '\005', 's', 't', 'a', 'r', 't', '\030', '\001', ' ', '\001', '(', '\005',
  2662. 'R', '\005', 's', 't', 'a', 'r', 't', '\022', '\020', '\n', '\003', 'e', 'n', 'd', '\030', '\002', ' ', '\001', '(', '\005', 'R', '\003', 'e', 'n', 'd',
  2663. '\022', '@', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\030', '\003', ' ', '\001', '(', '\013', '2', '&', '.', 'g', 'o', 'o', 'g', 'l',
  2664. 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'E', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', 'R', 'a', 'n', 'g', 'e',
  2665. 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\032', '7', '\n', '\r', 'R', 'e', 's', 'e', 'r',
  2666. 'v', 'e', 'd', 'R', 'a', 'n', 'g', 'e', '\022', '\024', '\n', '\005', 's', 't', 'a', 'r', 't', '\030', '\001', ' ', '\001', '(', '\005', 'R', '\005',
  2667. 's', 't', 'a', 'r', 't', '\022', '\020', '\n', '\003', 'e', 'n', 'd', '\030', '\002', ' ', '\001', '(', '\005', 'R', '\003', 'e', 'n', 'd', '\"', '|',
  2668. '\n', '\025', 'E', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', 'R', 'a', 'n', 'g', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', 'X',
  2669. '\n', '\024', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007',
  2670. ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n',
  2671. 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e',
  2672. 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002', '\"',
  2673. '\301', '\006', '\n', '\024', 'F', 'i', 'e', 'l', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022',
  2674. '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e', '\022', '\026', '\n', '\006', 'n', 'u',
  2675. 'm', 'b', 'e', 'r', '\030', '\003', ' ', '\001', '(', '\005', 'R', '\006', 'n', 'u', 'm', 'b', 'e', 'r', '\022', 'A', '\n', '\005', 'l', 'a', 'b',
  2676. 'e', 'l', '\030', '\004', ' ', '\001', '(', '\016', '2', '+', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u',
  2677. 'f', '.', 'F', 'i', 'e', 'l', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '.', 'L', 'a',
  2678. 'b', 'e', 'l', 'R', '\005', 'l', 'a', 'b', 'e', 'l', '\022', '>', '\n', '\004', 't', 'y', 'p', 'e', '\030', '\005', ' ', '\001', '(', '\016', '2',
  2679. '*', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'D', 'e',
  2680. 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '.', 'T', 'y', 'p', 'e', 'R', '\004', 't', 'y', 'p', 'e', '\022',
  2681. '\033', '\n', '\t', 't', 'y', 'p', 'e', '_', 'n', 'a', 'm', 'e', '\030', '\006', ' ', '\001', '(', '\t', 'R', '\010', 't', 'y', 'p', 'e', 'N',
  2682. 'a', 'm', 'e', '\022', '\032', '\n', '\010', 'e', 'x', 't', 'e', 'n', 'd', 'e', 'e', '\030', '\002', ' ', '\001', '(', '\t', 'R', '\010', 'e', 'x',
  2683. 't', 'e', 'n', 'd', 'e', 'e', '\022', '#', '\n', '\r', 'd', 'e', 'f', 'a', 'u', 'l', 't', '_', 'v', 'a', 'l', 'u', 'e', '\030', '\007',
  2684. ' ', '\001', '(', '\t', 'R', '\014', 'd', 'e', 'f', 'a', 'u', 'l', 't', 'V', 'a', 'l', 'u', 'e', '\022', '\037', '\n', '\013', 'o', 'n', 'e',
  2685. 'o', 'f', '_', 'i', 'n', 'd', 'e', 'x', '\030', '\t', ' ', '\001', '(', '\005', 'R', '\n', 'o', 'n', 'e', 'o', 'f', 'I', 'n', 'd', 'e',
  2686. 'x', '\022', '\033', '\n', '\t', 'j', 's', 'o', 'n', '_', 'n', 'a', 'm', 'e', '\030', '\n', ' ', '\001', '(', '\t', 'R', '\010', 'j', 's', 'o',
  2687. 'n', 'N', 'a', 'm', 'e', '\022', '7', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\030', '\010', ' ', '\001', '(', '\013', '2', '\035', '.',
  2688. 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'O', 'p', 't', 'i',
  2689. 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\022', '\'', '\n', '\017', 'p', 'r', 'o', 't', 'o', '3', '_', 'o', 'p',
  2690. 't', 'i', 'o', 'n', 'a', 'l', '\030', '\021', ' ', '\001', '(', '\010', 'R', '\016', 'p', 'r', 'o', 't', 'o', '3', 'O', 'p', 't', 'i', 'o',
  2691. 'n', 'a', 'l', '\"', '\266', '\002', '\n', '\004', 'T', 'y', 'p', 'e', '\022', '\017', '\n', '\013', 'T', 'Y', 'P', 'E', '_', 'D', 'O', 'U', 'B',
  2692. 'L', 'E', '\020', '\001', '\022', '\016', '\n', '\n', 'T', 'Y', 'P', 'E', '_', 'F', 'L', 'O', 'A', 'T', '\020', '\002', '\022', '\016', '\n', '\n', 'T',
  2693. 'Y', 'P', 'E', '_', 'I', 'N', 'T', '6', '4', '\020', '\003', '\022', '\017', '\n', '\013', 'T', 'Y', 'P', 'E', '_', 'U', 'I', 'N', 'T', '6',
  2694. '4', '\020', '\004', '\022', '\016', '\n', '\n', 'T', 'Y', 'P', 'E', '_', 'I', 'N', 'T', '3', '2', '\020', '\005', '\022', '\020', '\n', '\014', 'T', 'Y',
  2695. 'P', 'E', '_', 'F', 'I', 'X', 'E', 'D', '6', '4', '\020', '\006', '\022', '\020', '\n', '\014', 'T', 'Y', 'P', 'E', '_', 'F', 'I', 'X', 'E',
  2696. 'D', '3', '2', '\020', '\007', '\022', '\r', '\n', '\t', 'T', 'Y', 'P', 'E', '_', 'B', 'O', 'O', 'L', '\020', '\010', '\022', '\017', '\n', '\013', 'T',
  2697. 'Y', 'P', 'E', '_', 'S', 'T', 'R', 'I', 'N', 'G', '\020', '\t', '\022', '\016', '\n', '\n', 'T', 'Y', 'P', 'E', '_', 'G', 'R', 'O', 'U',
  2698. 'P', '\020', '\n', '\022', '\020', '\n', '\014', 'T', 'Y', 'P', 'E', '_', 'M', 'E', 'S', 'S', 'A', 'G', 'E', '\020', '\013', '\022', '\016', '\n', '\n',
  2699. 'T', 'Y', 'P', 'E', '_', 'B', 'Y', 'T', 'E', 'S', '\020', '\014', '\022', '\017', '\n', '\013', 'T', 'Y', 'P', 'E', '_', 'U', 'I', 'N', 'T',
  2700. '3', '2', '\020', '\r', '\022', '\r', '\n', '\t', 'T', 'Y', 'P', 'E', '_', 'E', 'N', 'U', 'M', '\020', '\016', '\022', '\021', '\n', '\r', 'T', 'Y',
  2701. 'P', 'E', '_', 'S', 'F', 'I', 'X', 'E', 'D', '3', '2', '\020', '\017', '\022', '\021', '\n', '\r', 'T', 'Y', 'P', 'E', '_', 'S', 'F', 'I',
  2702. 'X', 'E', 'D', '6', '4', '\020', '\020', '\022', '\017', '\n', '\013', 'T', 'Y', 'P', 'E', '_', 'S', 'I', 'N', 'T', '3', '2', '\020', '\021', '\022',
  2703. '\017', '\n', '\013', 'T', 'Y', 'P', 'E', '_', 'S', 'I', 'N', 'T', '6', '4', '\020', '\022', '\"', 'C', '\n', '\005', 'L', 'a', 'b', 'e', 'l',
  2704. '\022', '\022', '\n', '\016', 'L', 'A', 'B', 'E', 'L', '_', 'O', 'P', 'T', 'I', 'O', 'N', 'A', 'L', '\020', '\001', '\022', '\022', '\n', '\016', 'L',
  2705. 'A', 'B', 'E', 'L', '_', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D', '\020', '\002', '\022', '\022', '\n', '\016', 'L', 'A', 'B', 'E', 'L', '_',
  2706. 'R', 'E', 'P', 'E', 'A', 'T', 'E', 'D', '\020', '\003', '\"', 'c', '\n', '\024', 'O', 'n', 'e', 'o', 'f', 'D', 'e', 's', 'c', 'r', 'i',
  2707. 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022', '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004',
  2708. 'n', 'a', 'm', 'e', '\022', '7', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\030', '\002', ' ', '\001', '(', '\013', '2', '\035', '.', 'g',
  2709. 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'O', 'n', 'e', 'o', 'f', 'O', 'p', 't', 'i', 'o',
  2710. 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\"', '\343', '\002', '\n', '\023', 'E', 'n', 'u', 'm', 'D', 'e', 's', 'c', 'r',
  2711. 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022', '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R',
  2712. '\004', 'n', 'a', 'm', 'e', '\022', '?', '\n', '\005', 'v', 'a', 'l', 'u', 'e', '\030', '\002', ' ', '\003', '(', '\013', '2', ')', '.', 'g', 'o',
  2713. 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'E', 'n', 'u', 'm', 'V', 'a', 'l', 'u', 'e', 'D', 'e',
  2714. 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\005', 'v', 'a', 'l', 'u', 'e', '\022', '6', '\n', '\007', 'o',
  2715. 'p', 't', 'i', 'o', 'n', 's', '\030', '\003', ' ', '\001', '(', '\013', '2', '\034', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o',
  2716. 't', 'o', 'b', 'u', 'f', '.', 'E', 'n', 'u', 'm', 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n',
  2717. 's', '\022', ']', '\n', '\016', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd', '_', 'r', 'a', 'n', 'g', 'e', '\030', '\004', ' ', '\003', '(', '\013',
  2718. '2', '6', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'E', 'n', 'u', 'm', 'D', 'e',
  2719. 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '.', 'E', 'n', 'u', 'm', 'R', 'e', 's', 'e', 'r', 'v', 'e',
  2720. 'd', 'R', 'a', 'n', 'g', 'e', 'R', '\r', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd', 'R', 'a', 'n', 'g', 'e', '\022', '#', '\n', '\r',
  2721. 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd', '_', 'n', 'a', 'm', 'e', '\030', '\005', ' ', '\003', '(', '\t', 'R', '\014', 'r', 'e', 's', 'e',
  2722. 'r', 'v', 'e', 'd', 'N', 'a', 'm', 'e', '\032', ';', '\n', '\021', 'E', 'n', 'u', 'm', 'R', 'e', 's', 'e', 'r', 'v', 'e', 'd', 'R',
  2723. 'a', 'n', 'g', 'e', '\022', '\024', '\n', '\005', 's', 't', 'a', 'r', 't', '\030', '\001', ' ', '\001', '(', '\005', 'R', '\005', 's', 't', 'a', 'r',
  2724. 't', '\022', '\020', '\n', '\003', 'e', 'n', 'd', '\030', '\002', ' ', '\001', '(', '\005', 'R', '\003', 'e', 'n', 'd', '\"', '\203', '\001', '\n', '\030', 'E',
  2725. 'n', 'u', 'm', 'V', 'a', 'l', 'u', 'e', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022', '\022',
  2726. '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e', '\022', '\026', '\n', '\006', 'n', 'u', 'm',
  2727. 'b', 'e', 'r', '\030', '\002', ' ', '\001', '(', '\005', 'R', '\006', 'n', 'u', 'm', 'b', 'e', 'r', '\022', ';', '\n', '\007', 'o', 'p', 't', 'i',
  2728. 'o', 'n', 's', '\030', '\003', ' ', '\001', '(', '\013', '2', '!', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b',
  2729. 'u', 'f', '.', 'E', 'n', 'u', 'm', 'V', 'a', 'l', 'u', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i',
  2730. 'o', 'n', 's', '\"', '\247', '\001', '\n', '\026', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r',
  2731. 'P', 'r', 'o', 't', 'o', '\022', '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e',
  2732. '\022', '>', '\n', '\006', 'm', 'e', 't', 'h', 'o', 'd', '\030', '\002', ' ', '\003', '(', '\013', '2', '&', '.', 'g', 'o', 'o', 'g', 'l', 'e',
  2733. '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'M', 'e', 't', 'h', 'o', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o',
  2734. 'r', 'P', 'r', 'o', 't', 'o', 'R', '\006', 'm', 'e', 't', 'h', 'o', 'd', '\022', '9', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's',
  2735. '\030', '\003', ' ', '\001', '(', '\013', '2', '\037', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.',
  2736. 'S', 'e', 'r', 'v', 'i', 'c', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\"', '\211',
  2737. '\002', '\n', '\025', 'M', 'e', 't', 'h', 'o', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022',
  2738. '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e', '\022', '\035', '\n', '\n', 'i', 'n',
  2739. 'p', 'u', 't', '_', 't', 'y', 'p', 'e', '\030', '\002', ' ', '\001', '(', '\t', 'R', '\t', 'i', 'n', 'p', 'u', 't', 'T', 'y', 'p', 'e',
  2740. '\022', '\037', '\n', '\013', 'o', 'u', 't', 'p', 'u', 't', '_', 't', 'y', 'p', 'e', '\030', '\003', ' ', '\001', '(', '\t', 'R', '\n', 'o', 'u',
  2741. 't', 'p', 'u', 't', 'T', 'y', 'p', 'e', '\022', '8', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\030', '\004', ' ', '\001', '(', '\013',
  2742. '2', '\036', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'M', 'e', 't', 'h', 'o', 'd',
  2743. 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\022', '0', '\n', '\020', 'c', 'l', 'i', 'e', 'n',
  2744. 't', '_', 's', 't', 'r', 'e', 'a', 'm', 'i', 'n', 'g', '\030', '\005', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R',
  2745. '\017', 'c', 'l', 'i', 'e', 'n', 't', 'S', 't', 'r', 'e', 'a', 'm', 'i', 'n', 'g', '\022', '0', '\n', '\020', 's', 'e', 'r', 'v', 'e',
  2746. 'r', '_', 's', 't', 'r', 'e', 'a', 'm', 'i', 'n', 'g', '\030', '\006', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R',
  2747. '\017', 's', 'e', 'r', 'v', 'e', 'r', 'S', 't', 'r', 'e', 'a', 'm', 'i', 'n', 'g', '\"', '\221', '\t', '\n', '\013', 'F', 'i', 'l', 'e',
  2748. 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', '!', '\n', '\014', 'j', 'a', 'v', 'a', '_', 'p', 'a', 'c', 'k', 'a', 'g', 'e', '\030', '\001',
  2749. ' ', '\001', '(', '\t', 'R', '\013', 'j', 'a', 'v', 'a', 'P', 'a', 'c', 'k', 'a', 'g', 'e', '\022', '0', '\n', '\024', 'j', 'a', 'v', 'a',
  2750. '_', 'o', 'u', 't', 'e', 'r', '_', 'c', 'l', 'a', 's', 's', 'n', 'a', 'm', 'e', '\030', '\010', ' ', '\001', '(', '\t', 'R', '\022', 'j',
  2751. 'a', 'v', 'a', 'O', 'u', 't', 'e', 'r', 'C', 'l', 'a', 's', 's', 'n', 'a', 'm', 'e', '\022', '5', '\n', '\023', 'j', 'a', 'v', 'a',
  2752. '_', 'm', 'u', 'l', 't', 'i', 'p', 'l', 'e', '_', 'f', 'i', 'l', 'e', 's', '\030', '\n', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a',
  2753. 'l', 's', 'e', 'R', '\021', 'j', 'a', 'v', 'a', 'M', 'u', 'l', 't', 'i', 'p', 'l', 'e', 'F', 'i', 'l', 'e', 's', '\022', 'D', '\n',
  2754. '\035', 'j', 'a', 'v', 'a', '_', 'g', 'e', 'n', 'e', 'r', 'a', 't', 'e', '_', 'e', 'q', 'u', 'a', 'l', 's', '_', 'a', 'n', 'd',
  2755. '_', 'h', 'a', 's', 'h', '\030', '\024', ' ', '\001', '(', '\010', 'B', '\002', '\030', '\001', 'R', '\031', 'j', 'a', 'v', 'a', 'G', 'e', 'n', 'e',
  2756. 'r', 'a', 't', 'e', 'E', 'q', 'u', 'a', 'l', 's', 'A', 'n', 'd', 'H', 'a', 's', 'h', '\022', ':', '\n', '\026', 'j', 'a', 'v', 'a',
  2757. '_', 's', 't', 'r', 'i', 'n', 'g', '_', 'c', 'h', 'e', 'c', 'k', '_', 'u', 't', 'f', '8', '\030', '\033', ' ', '\001', '(', '\010', ':',
  2758. '\005', 'f', 'a', 'l', 's', 'e', 'R', '\023', 'j', 'a', 'v', 'a', 'S', 't', 'r', 'i', 'n', 'g', 'C', 'h', 'e', 'c', 'k', 'U', 't',
  2759. 'f', '8', '\022', 'S', '\n', '\014', 'o', 'p', 't', 'i', 'm', 'i', 'z', 'e', '_', 'f', 'o', 'r', '\030', '\t', ' ', '\001', '(', '\016', '2',
  2760. ')', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'l', 'e', 'O', 'p', 't',
  2761. 'i', 'o', 'n', 's', '.', 'O', 'p', 't', 'i', 'm', 'i', 'z', 'e', 'M', 'o', 'd', 'e', ':', '\005', 'S', 'P', 'E', 'E', 'D', 'R',
  2762. '\013', 'o', 'p', 't', 'i', 'm', 'i', 'z', 'e', 'F', 'o', 'r', '\022', '\035', '\n', '\n', 'g', 'o', '_', 'p', 'a', 'c', 'k', 'a', 'g',
  2763. 'e', '\030', '\013', ' ', '\001', '(', '\t', 'R', '\t', 'g', 'o', 'P', 'a', 'c', 'k', 'a', 'g', 'e', '\022', '5', '\n', '\023', 'c', 'c', '_',
  2764. 'g', 'e', 'n', 'e', 'r', 'i', 'c', '_', 's', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\030', '\020', ' ', '\001', '(', '\010', ':', '\005', 'f',
  2765. 'a', 'l', 's', 'e', 'R', '\021', 'c', 'c', 'G', 'e', 'n', 'e', 'r', 'i', 'c', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\022', '9',
  2766. '\n', '\025', 'j', 'a', 'v', 'a', '_', 'g', 'e', 'n', 'e', 'r', 'i', 'c', '_', 's', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\030', '\021',
  2767. ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\023', 'j', 'a', 'v', 'a', 'G', 'e', 'n', 'e', 'r', 'i', 'c', 'S',
  2768. 'e', 'r', 'v', 'i', 'c', 'e', 's', '\022', '5', '\n', '\023', 'p', 'y', '_', 'g', 'e', 'n', 'e', 'r', 'i', 'c', '_', 's', 'e', 'r',
  2769. 'v', 'i', 'c', 'e', 's', '\030', '\022', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\021', 'p', 'y', 'G', 'e', 'n',
  2770. 'e', 'r', 'i', 'c', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\022', '7', '\n', '\024', 'p', 'h', 'p', '_', 'g', 'e', 'n', 'e', 'r',
  2771. 'i', 'c', '_', 's', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\030', '*', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R',
  2772. '\022', 'p', 'h', 'p', 'G', 'e', 'n', 'e', 'r', 'i', 'c', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\022', '%', '\n', '\n', 'd', 'e',
  2773. 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\030', '\027', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n', 'd', 'e',
  2774. 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\022', '.', '\n', '\020', 'c', 'c', '_', 'e', 'n', 'a', 'b', 'l', 'e', '_', 'a', 'r', 'e',
  2775. 'n', 'a', 's', '\030', '\037', ' ', '\001', '(', '\010', ':', '\004', 't', 'r', 'u', 'e', 'R', '\016', 'c', 'c', 'E', 'n', 'a', 'b', 'l', 'e',
  2776. 'A', 'r', 'e', 'n', 'a', 's', '\022', '*', '\n', '\021', 'o', 'b', 'j', 'c', '_', 'c', 'l', 'a', 's', 's', '_', 'p', 'r', 'e', 'f',
  2777. 'i', 'x', '\030', '$', ' ', '\001', '(', '\t', 'R', '\017', 'o', 'b', 'j', 'c', 'C', 'l', 'a', 's', 's', 'P', 'r', 'e', 'f', 'i', 'x',
  2778. '\022', ')', '\n', '\020', 'c', 's', 'h', 'a', 'r', 'p', '_', 'n', 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e', '\030', '%', ' ', '\001', '(',
  2779. '\t', 'R', '\017', 'c', 's', 'h', 'a', 'r', 'p', 'N', 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e', '\022', '!', '\n', '\014', 's', 'w', 'i',
  2780. 'f', 't', '_', 'p', 'r', 'e', 'f', 'i', 'x', '\030', '\'', ' ', '\001', '(', '\t', 'R', '\013', 's', 'w', 'i', 'f', 't', 'P', 'r', 'e',
  2781. 'f', 'i', 'x', '\022', '(', '\n', '\020', 'p', 'h', 'p', '_', 'c', 'l', 'a', 's', 's', '_', 'p', 'r', 'e', 'f', 'i', 'x', '\030', '(',
  2782. ' ', '\001', '(', '\t', 'R', '\016', 'p', 'h', 'p', 'C', 'l', 'a', 's', 's', 'P', 'r', 'e', 'f', 'i', 'x', '\022', '#', '\n', '\r', 'p',
  2783. 'h', 'p', '_', 'n', 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e', '\030', ')', ' ', '\001', '(', '\t', 'R', '\014', 'p', 'h', 'p', 'N', 'a',
  2784. 'm', 'e', 's', 'p', 'a', 'c', 'e', '\022', '4', '\n', '\026', 'p', 'h', 'p', '_', 'm', 'e', 't', 'a', 'd', 'a', 't', 'a', '_', 'n',
  2785. 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e', '\030', ',', ' ', '\001', '(', '\t', 'R', '\024', 'p', 'h', 'p', 'M', 'e', 't', 'a', 'd', 'a',
  2786. 't', 'a', 'N', 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e', '\022', '!', '\n', '\014', 'r', 'u', 'b', 'y', '_', 'p', 'a', 'c', 'k', 'a',
  2787. 'g', 'e', '\030', '-', ' ', '\001', '(', '\t', 'R', '\013', 'r', 'u', 'b', 'y', 'P', 'a', 'c', 'k', 'a', 'g', 'e', '\022', 'X', '\n', '\024',
  2788. 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003',
  2789. '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n',
  2790. 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p',
  2791. 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '\"', ':', '\n', '\014', 'O', 'p', 't', 'i', 'm', 'i', 'z', 'e', 'M', 'o',
  2792. 'd', 'e', '\022', '\t', '\n', '\005', 'S', 'P', 'E', 'E', 'D', '\020', '\001', '\022', '\r', '\n', '\t', 'C', 'O', 'D', 'E', '_', 'S', 'I', 'Z',
  2793. 'E', '\020', '\002', '\022', '\020', '\n', '\014', 'L', 'I', 'T', 'E', '_', 'R', 'U', 'N', 'T', 'I', 'M', 'E', '\020', '\003', '*', '\t', '\010', '\350',
  2794. '\007', '\020', '\200', '\200', '\200', '\200', '\002', 'J', '\004', '\010', '&', '\020', '\'', '\"', '\321', '\002', '\n', '\016', 'M', 'e', 's', 's', 'a', 'g', 'e',
  2795. 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', '<', '\n', '\027', 'm', 'e', 's', 's', 'a', 'g', 'e', '_', 's', 'e', 't', '_', 'w', 'i',
  2796. 'r', 'e', '_', 'f', 'o', 'r', 'm', 'a', 't', '\030', '\001', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\024', 'm',
  2797. 'e', 's', 's', 'a', 'g', 'e', 'S', 'e', 't', 'W', 'i', 'r', 'e', 'F', 'o', 'r', 'm', 'a', 't', '\022', 'L', '\n', '\037', 'n', 'o',
  2798. '_', 's', 't', 'a', 'n', 'd', 'a', 'r', 'd', '_', 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', '_', 'a', 'c', 'c', 'e',
  2799. 's', 's', 'o', 'r', '\030', '\002', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\034', 'n', 'o', 'S', 't', 'a', 'n',
  2800. 'd', 'a', 'r', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'A', 'c', 'c', 'e', 's', 's', 'o', 'r', '\022', '%', '\n',
  2801. '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\030', '\003', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R',
  2802. '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\022', '\033', '\n', '\t', 'm', 'a', 'p', '_', 'e', 'n', 't', 'r', 'y', '\030',
  2803. '\007', ' ', '\001', '(', '\010', 'R', '\010', 'm', 'a', 'p', 'E', 'n', 't', 'r', 'y', '\022', 'X', '\n', '\024', 'u', 'n', 'i', 'n', 't', 'e',
  2804. 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2', '$', '.', 'g',
  2805. 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e',
  2806. 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O',
  2807. 'p', 't', 'i', 'o', 'n', '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002', 'J', '\004', '\010', '\010', '\020', '\t', 'J', '\004', '\010',
  2808. '\t', '\020', '\n', '\"', '\342', '\003', '\n', '\014', 'F', 'i', 'e', 'l', 'd', 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', 'A', '\n', '\005', 'c',
  2809. 't', 'y', 'p', 'e', '\030', '\001', ' ', '\001', '(', '\016', '2', '#', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o',
  2810. 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'O', 'p', 't', 'i', 'o', 'n', 's', '.', 'C', 'T', 'y', 'p', 'e', ':', '\006', 'S',
  2811. 'T', 'R', 'I', 'N', 'G', 'R', '\005', 'c', 't', 'y', 'p', 'e', '\022', '\026', '\n', '\006', 'p', 'a', 'c', 'k', 'e', 'd', '\030', '\002', ' ',
  2812. '\001', '(', '\010', 'R', '\006', 'p', 'a', 'c', 'k', 'e', 'd', '\022', 'G', '\n', '\006', 'j', 's', 't', 'y', 'p', 'e', '\030', '\006', ' ', '\001',
  2813. '(', '\016', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l',
  2814. 'd', 'O', 'p', 't', 'i', 'o', 'n', 's', '.', 'J', 'S', 'T', 'y', 'p', 'e', ':', '\t', 'J', 'S', '_', 'N', 'O', 'R', 'M', 'A',
  2815. 'L', 'R', '\006', 'j', 's', 't', 'y', 'p', 'e', '\022', '\031', '\n', '\004', 'l', 'a', 'z', 'y', '\030', '\005', ' ', '\001', '(', '\010', ':', '\005',
  2816. 'f', 'a', 'l', 's', 'e', 'R', '\004', 'l', 'a', 'z', 'y', '\022', '%', '\n', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd',
  2817. '\030', '\003', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd',
  2818. '\022', '\031', '\n', '\004', 'w', 'e', 'a', 'k', '\030', '\n', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\004', 'w', 'e',
  2819. 'a', 'k', '\022', 'X', '\n', '\024', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o',
  2820. 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u',
  2821. 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n',
  2822. 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '\"', '/', '\n', '\005', 'C', 'T', 'y', 'p',
  2823. 'e', '\022', '\n', '\n', '\006', 'S', 'T', 'R', 'I', 'N', 'G', '\020', '\000', '\022', '\010', '\n', '\004', 'C', 'O', 'R', 'D', '\020', '\001', '\022', '\020',
  2824. '\n', '\014', 'S', 'T', 'R', 'I', 'N', 'G', '_', 'P', 'I', 'E', 'C', 'E', '\020', '\002', '\"', '5', '\n', '\006', 'J', 'S', 'T', 'y', 'p',
  2825. 'e', '\022', '\r', '\n', '\t', 'J', 'S', '_', 'N', 'O', 'R', 'M', 'A', 'L', '\020', '\000', '\022', '\r', '\n', '\t', 'J', 'S', '_', 'S', 'T',
  2826. 'R', 'I', 'N', 'G', '\020', '\001', '\022', '\r', '\n', '\t', 'J', 'S', '_', 'N', 'U', 'M', 'B', 'E', 'R', '\020', '\002', '*', '\t', '\010', '\350',
  2827. '\007', '\020', '\200', '\200', '\200', '\200', '\002', 'J', '\004', '\010', '\004', '\020', '\005', '\"', 's', '\n', '\014', 'O', 'n', 'e', 'o', 'f', 'O', 'p', 't',
  2828. 'i', 'o', 'n', 's', '\022', 'X', '\n', '\024', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't',
  2829. 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o',
  2830. 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023',
  2831. 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '*', '\t', '\010', '\350', '\007', '\020',
  2832. '\200', '\200', '\200', '\200', '\002', '\"', '\300', '\001', '\n', '\013', 'E', 'n', 'u', 'm', 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', '\037', '\n', '\013',
  2833. 'a', 'l', 'l', 'o', 'w', '_', 'a', 'l', 'i', 'a', 's', '\030', '\002', ' ', '\001', '(', '\010', 'R', '\n', 'a', 'l', 'l', 'o', 'w', 'A',
  2834. 'l', 'i', 'a', 's', '\022', '%', '\n', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\030', '\003', ' ', '\001', '(', '\010', ':',
  2835. '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\022', 'X', '\n', '\024', 'u', 'n', 'i',
  2836. 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2',
  2837. '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r',
  2838. 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't',
  2839. 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002', 'J', '\004', '\010', '\005', '\020', '\006',
  2840. '\"', '\236', '\001', '\n', '\020', 'E', 'n', 'u', 'm', 'V', 'a', 'l', 'u', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', '%', '\n', '\n',
  2841. 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\030', '\001', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n',
  2842. 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\022', 'X', '\n', '\024', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't',
  2843. 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e',
  2844. '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p',
  2845. 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n',
  2846. '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002', '\"', '\234', '\001', '\n', '\016', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 'O', 'p',
  2847. 't', 'i', 'o', 'n', 's', '\022', '%', '\n', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\030', '!', ' ', '\001', '(', '\010',
  2848. ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\022', 'X', '\n', '\024', 'u', 'n',
  2849. 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013',
  2850. '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e',
  2851. 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e',
  2852. 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002', '\"', '\340', '\002', '\n', '\r',
  2853. 'M', 'e', 't', 'h', 'o', 'd', 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', '%', '\n', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't',
  2854. 'e', 'd', '\030', '!', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't',
  2855. 'e', 'd', '\022', 'q', '\n', '\021', 'i', 'd', 'e', 'm', 'p', 'o', 't', 'e', 'n', 'c', 'y', '_', 'l', 'e', 'v', 'e', 'l', '\030', '\"',
  2856. ' ', '\001', '(', '\016', '2', '/', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'M', 'e',
  2857. 't', 'h', 'o', 'd', 'O', 'p', 't', 'i', 'o', 'n', 's', '.', 'I', 'd', 'e', 'm', 'p', 'o', 't', 'e', 'n', 'c', 'y', 'L', 'e',
  2858. 'v', 'e', 'l', ':', '\023', 'I', 'D', 'E', 'M', 'P', 'O', 'T', 'E', 'N', 'C', 'Y', '_', 'U', 'N', 'K', 'N', 'O', 'W', 'N', 'R',
  2859. '\020', 'i', 'd', 'e', 'm', 'p', 'o', 't', 'e', 'n', 'c', 'y', 'L', 'e', 'v', 'e', 'l', '\022', 'X', '\n', '\024', 'u', 'n', 'i', 'n',
  2860. 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2', '$',
  2861. '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p',
  2862. 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e',
  2863. 'd', 'O', 'p', 't', 'i', 'o', 'n', '\"', 'P', '\n', '\020', 'I', 'd', 'e', 'm', 'p', 'o', 't', 'e', 'n', 'c', 'y', 'L', 'e', 'v',
  2864. 'e', 'l', '\022', '\027', '\n', '\023', 'I', 'D', 'E', 'M', 'P', 'O', 'T', 'E', 'N', 'C', 'Y', '_', 'U', 'N', 'K', 'N', 'O', 'W', 'N',
  2865. '\020', '\000', '\022', '\023', '\n', '\017', 'N', 'O', '_', 'S', 'I', 'D', 'E', '_', 'E', 'F', 'F', 'E', 'C', 'T', 'S', '\020', '\001', '\022', '\016',
  2866. '\n', '\n', 'I', 'D', 'E', 'M', 'P', 'O', 'T', 'E', 'N', 'T', '\020', '\002', '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002',
  2867. '\"', '\232', '\003', '\n', '\023', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '\022',
  2868. 'A', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\002', ' ', '\003', '(', '\013', '2', '-', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r',
  2869. 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o',
  2870. 'n', '.', 'N', 'a', 'm', 'e', 'P', 'a', 'r', 't', 'R', '\004', 'n', 'a', 'm', 'e', '\022', ')', '\n', '\020', 'i', 'd', 'e', 'n', 't',
  2871. 'i', 'f', 'i', 'e', 'r', '_', 'v', 'a', 'l', 'u', 'e', '\030', '\003', ' ', '\001', '(', '\t', 'R', '\017', 'i', 'd', 'e', 'n', 't', 'i',
  2872. 'f', 'i', 'e', 'r', 'V', 'a', 'l', 'u', 'e', '\022', ',', '\n', '\022', 'p', 'o', 's', 'i', 't', 'i', 'v', 'e', '_', 'i', 'n', 't',
  2873. '_', 'v', 'a', 'l', 'u', 'e', '\030', '\004', ' ', '\001', '(', '\004', 'R', '\020', 'p', 'o', 's', 'i', 't', 'i', 'v', 'e', 'I', 'n', 't',
  2874. 'V', 'a', 'l', 'u', 'e', '\022', ',', '\n', '\022', 'n', 'e', 'g', 'a', 't', 'i', 'v', 'e', '_', 'i', 'n', 't', '_', 'v', 'a', 'l',
  2875. 'u', 'e', '\030', '\005', ' ', '\001', '(', '\003', 'R', '\020', 'n', 'e', 'g', 'a', 't', 'i', 'v', 'e', 'I', 'n', 't', 'V', 'a', 'l', 'u',
  2876. 'e', '\022', '!', '\n', '\014', 'd', 'o', 'u', 'b', 'l', 'e', '_', 'v', 'a', 'l', 'u', 'e', '\030', '\006', ' ', '\001', '(', '\001', 'R', '\013',
  2877. 'd', 'o', 'u', 'b', 'l', 'e', 'V', 'a', 'l', 'u', 'e', '\022', '!', '\n', '\014', 's', 't', 'r', 'i', 'n', 'g', '_', 'v', 'a', 'l',
  2878. 'u', 'e', '\030', '\007', ' ', '\001', '(', '\014', 'R', '\013', 's', 't', 'r', 'i', 'n', 'g', 'V', 'a', 'l', 'u', 'e', '\022', '\'', '\n', '\017',
  2879. 'a', 'g', 'g', 'r', 'e', 'g', 'a', 't', 'e', '_', 'v', 'a', 'l', 'u', 'e', '\030', '\010', ' ', '\001', '(', '\t', 'R', '\016', 'a', 'g',
  2880. 'g', 'r', 'e', 'g', 'a', 't', 'e', 'V', 'a', 'l', 'u', 'e', '\032', 'J', '\n', '\010', 'N', 'a', 'm', 'e', 'P', 'a', 'r', 't', '\022',
  2881. '\033', '\n', '\t', 'n', 'a', 'm', 'e', '_', 'p', 'a', 'r', 't', '\030', '\001', ' ', '\002', '(', '\t', 'R', '\010', 'n', 'a', 'm', 'e', 'P',
  2882. 'a', 'r', 't', '\022', '!', '\n', '\014', 'i', 's', '_', 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\030', '\002', ' ', '\002', '(', '\010',
  2883. 'R', '\013', 'i', 's', 'E', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\"', '\247', '\002', '\n', '\016', 'S', 'o', 'u', 'r', 'c', 'e', 'C',
  2884. 'o', 'd', 'e', 'I', 'n', 'f', 'o', '\022', 'D', '\n', '\010', 'l', 'o', 'c', 'a', 't', 'i', 'o', 'n', '\030', '\001', ' ', '\003', '(', '\013',
  2885. '2', '(', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'S', 'o', 'u', 'r', 'c', 'e',
  2886. 'C', 'o', 'd', 'e', 'I', 'n', 'f', 'o', '.', 'L', 'o', 'c', 'a', 't', 'i', 'o', 'n', 'R', '\010', 'l', 'o', 'c', 'a', 't', 'i',
  2887. 'o', 'n', '\032', '\316', '\001', '\n', '\010', 'L', 'o', 'c', 'a', 't', 'i', 'o', 'n', '\022', '\026', '\n', '\004', 'p', 'a', 't', 'h', '\030', '\001',
  2888. ' ', '\003', '(', '\005', 'B', '\002', '\020', '\001', 'R', '\004', 'p', 'a', 't', 'h', '\022', '\026', '\n', '\004', 's', 'p', 'a', 'n', '\030', '\002', ' ',
  2889. '\003', '(', '\005', 'B', '\002', '\020', '\001', 'R', '\004', 's', 'p', 'a', 'n', '\022', ')', '\n', '\020', 'l', 'e', 'a', 'd', 'i', 'n', 'g', '_',
  2890. 'c', 'o', 'm', 'm', 'e', 'n', 't', 's', '\030', '\003', ' ', '\001', '(', '\t', 'R', '\017', 'l', 'e', 'a', 'd', 'i', 'n', 'g', 'C', 'o',
  2891. 'm', 'm', 'e', 'n', 't', 's', '\022', '+', '\n', '\021', 't', 'r', 'a', 'i', 'l', 'i', 'n', 'g', '_', 'c', 'o', 'm', 'm', 'e', 'n',
  2892. 't', 's', '\030', '\004', ' ', '\001', '(', '\t', 'R', '\020', 't', 'r', 'a', 'i', 'l', 'i', 'n', 'g', 'C', 'o', 'm', 'm', 'e', 'n', 't',
  2893. 's', '\022', ':', '\n', '\031', 'l', 'e', 'a', 'd', 'i', 'n', 'g', '_', 'd', 'e', 't', 'a', 'c', 'h', 'e', 'd', '_', 'c', 'o', 'm',
  2894. 'm', 'e', 'n', 't', 's', '\030', '\006', ' ', '\003', '(', '\t', 'R', '\027', 'l', 'e', 'a', 'd', 'i', 'n', 'g', 'D', 'e', 't', 'a', 'c',
  2895. 'h', 'e', 'd', 'C', 'o', 'm', 'm', 'e', 'n', 't', 's', '\"', '\321', '\001', '\n', '\021', 'G', 'e', 'n', 'e', 'r', 'a', 't', 'e', 'd',
  2896. 'C', 'o', 'd', 'e', 'I', 'n', 'f', 'o', '\022', 'M', '\n', '\n', 'a', 'n', 'n', 'o', 't', 'a', 't', 'i', 'o', 'n', '\030', '\001', ' ',
  2897. '\003', '(', '\013', '2', '-', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'G', 'e', 'n',
  2898. 'e', 'r', 'a', 't', 'e', 'd', 'C', 'o', 'd', 'e', 'I', 'n', 'f', 'o', '.', 'A', 'n', 'n', 'o', 't', 'a', 't', 'i', 'o', 'n',
  2899. 'R', '\n', 'a', 'n', 'n', 'o', 't', 'a', 't', 'i', 'o', 'n', '\032', 'm', '\n', '\n', 'A', 'n', 'n', 'o', 't', 'a', 't', 'i', 'o',
  2900. 'n', '\022', '\026', '\n', '\004', 'p', 'a', 't', 'h', '\030', '\001', ' ', '\003', '(', '\005', 'B', '\002', '\020', '\001', 'R', '\004', 'p', 'a', 't', 'h',
  2901. '\022', '\037', '\n', '\013', 's', 'o', 'u', 'r', 'c', 'e', '_', 'f', 'i', 'l', 'e', '\030', '\002', ' ', '\001', '(', '\t', 'R', '\n', 's', 'o',
  2902. 'u', 'r', 'c', 'e', 'F', 'i', 'l', 'e', '\022', '\024', '\n', '\005', 'b', 'e', 'g', 'i', 'n', '\030', '\003', ' ', '\001', '(', '\005', 'R', '\005',
  2903. 'b', 'e', 'g', 'i', 'n', '\022', '\020', '\n', '\003', 'e', 'n', 'd', '\030', '\004', ' ', '\001', '(', '\005', 'R', '\003', 'e', 'n', 'd', 'B', '\217',
  2904. '\001', '\n', '\023', 'c', 'o', 'm', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', 'B', '\020', 'D',
  2905. 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 's', 'H', '\001', 'Z', '>', 'g', 'i', 't', 'h', 'u', 'b',
  2906. '.', 'c', 'o', 'm', '/', 'g', 'o', 'l', 'a', 'n', 'g', '/', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '/', 'p', 'r', 'o', 't',
  2907. 'o', 'c', '-', 'g', 'e', 'n', '-', 'g', 'o', '/', 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', ';', 'd', 'e', 's', 'c',
  2908. 'r', 'i', 'p', 't', 'o', 'r', '\370', '\001', '\001', '\242', '\002', '\003', 'G', 'P', 'B', '\252', '\002', '\032', 'G', 'o', 'o', 'g', 'l', 'e', '.',
  2909. 'P', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'R', 'e', 'f', 'l', 'e', 'c', 't', 'i', 'o', 'n',
  2910. };
  2911. static upb_def_init *deps[1] = {
  2912. NULL
  2913. };
  2914. upb_def_init google_protobuf_descriptor_proto_upbdefinit = {
  2915. deps,
  2916. layouts,
  2917. "google/protobuf/descriptor.proto",
  2918. UPB_STRVIEW_INIT(descriptor, 7619)
  2919. };
  2920. #include <ctype.h>
  2921. #include <errno.h>
  2922. #include <stdlib.h>
  2923. #include <string.h>
  2924. typedef struct {
  2925. size_t len;
  2926. char str[1]; /* Null-terminated string data follows. */
  2927. } str_t;
  2928. static str_t *newstr(upb_alloc *alloc, const char *data, size_t len) {
  2929. str_t *ret = upb_malloc(alloc, sizeof(*ret) + len);
  2930. if (!ret) return NULL;
  2931. ret->len = len;
  2932. if (len) memcpy(ret->str, data, len);
  2933. ret->str[len] = '\0';
  2934. return ret;
  2935. }
  2936. struct upb_fielddef {
  2937. const upb_filedef *file;
  2938. const upb_msgdef *msgdef;
  2939. const char *full_name;
  2940. const char *json_name;
  2941. union {
  2942. int64_t sint;
  2943. uint64_t uint;
  2944. double dbl;
  2945. float flt;
  2946. bool boolean;
  2947. str_t *str;
  2948. } defaultval;
  2949. const upb_oneofdef *oneof;
  2950. union {
  2951. const upb_msgdef *msgdef;
  2952. const upb_enumdef *enumdef;
  2953. const google_protobuf_FieldDescriptorProto *unresolved;
  2954. } sub;
  2955. uint32_t number_;
  2956. uint16_t index_;
  2957. uint16_t layout_index;
  2958. uint32_t selector_base; /* Used to index into a upb::Handlers table. */
  2959. bool is_extension_;
  2960. bool lazy_;
  2961. bool packed_;
  2962. bool proto3_optional_;
  2963. upb_descriptortype_t type_;
  2964. upb_label_t label_;
  2965. };
  2966. struct upb_msgdef {
  2967. const upb_msglayout *layout;
  2968. const upb_filedef *file;
  2969. const char *full_name;
  2970. uint32_t selector_count;
  2971. uint32_t submsg_field_count;
  2972. /* Tables for looking up fields by number and name. */
  2973. upb_inttable itof;
  2974. upb_strtable ntof;
  2975. const upb_fielddef *fields;
  2976. const upb_oneofdef *oneofs;
  2977. int field_count;
  2978. int oneof_count;
  2979. int real_oneof_count;
  2980. /* Is this a map-entry message? */
  2981. bool map_entry;
  2982. upb_wellknowntype_t well_known_type;
  2983. /* TODO(haberman): proper extension ranges (there can be multiple). */
  2984. };
  2985. struct upb_enumdef {
  2986. const upb_filedef *file;
  2987. const char *full_name;
  2988. upb_strtable ntoi;
  2989. upb_inttable iton;
  2990. int32_t defaultval;
  2991. };
  2992. struct upb_oneofdef {
  2993. const upb_msgdef *parent;
  2994. const char *full_name;
  2995. uint32_t index;
  2996. upb_strtable ntof;
  2997. upb_inttable itof;
  2998. };
  2999. struct upb_filedef {
  3000. const char *name;
  3001. const char *package;
  3002. const char *phpprefix;
  3003. const char *phpnamespace;
  3004. upb_syntax_t syntax;
  3005. const upb_filedef **deps;
  3006. const upb_msgdef *msgs;
  3007. const upb_enumdef *enums;
  3008. const upb_fielddef *exts;
  3009. int dep_count;
  3010. int msg_count;
  3011. int enum_count;
  3012. int ext_count;
  3013. };
  3014. struct upb_symtab {
  3015. upb_arena *arena;
  3016. upb_strtable syms; /* full_name -> packed def ptr */
  3017. upb_strtable files; /* file_name -> upb_filedef* */
  3018. };
  3019. /* Inside a symtab we store tagged pointers to specific def types. */
  3020. typedef enum {
  3021. UPB_DEFTYPE_FIELD = 0,
  3022. /* Only inside symtab table. */
  3023. UPB_DEFTYPE_MSG = 1,
  3024. UPB_DEFTYPE_ENUM = 2,
  3025. /* Only inside message table. */
  3026. UPB_DEFTYPE_ONEOF = 1,
  3027. UPB_DEFTYPE_FIELD_JSONNAME = 2
  3028. } upb_deftype_t;
  3029. static const void *unpack_def(upb_value v, upb_deftype_t type) {
  3030. uintptr_t num = (uintptr_t)upb_value_getconstptr(v);
  3031. return (num & 3) == type ? (const void*)(num & ~3) : NULL;
  3032. }
  3033. static upb_value pack_def(const void *ptr, upb_deftype_t type) {
  3034. uintptr_t num = (uintptr_t)ptr | type;
  3035. return upb_value_constptr((const void*)num);
  3036. }
  3037. /* isalpha() etc. from <ctype.h> are locale-dependent, which we don't want. */
  3038. static bool upb_isbetween(char c, char low, char high) {
  3039. return c >= low && c <= high;
  3040. }
  3041. static bool upb_isletter(char c) {
  3042. return upb_isbetween(c, 'A', 'Z') || upb_isbetween(c, 'a', 'z') || c == '_';
  3043. }
  3044. static bool upb_isalphanum(char c) {
  3045. return upb_isletter(c) || upb_isbetween(c, '0', '9');
  3046. }
  3047. static bool upb_isident(upb_strview name, bool full, upb_status *s) {
  3048. const char *str = name.data;
  3049. size_t len = name.size;
  3050. bool start = true;
  3051. size_t i;
  3052. for (i = 0; i < len; i++) {
  3053. char c = str[i];
  3054. if (c == '.') {
  3055. if (start || !full) {
  3056. upb_status_seterrf(s, "invalid name: unexpected '.' (%s)", str);
  3057. return false;
  3058. }
  3059. start = true;
  3060. } else if (start) {
  3061. if (!upb_isletter(c)) {
  3062. upb_status_seterrf(
  3063. s, "invalid name: path components must start with a letter (%s)",
  3064. str);
  3065. return false;
  3066. }
  3067. start = false;
  3068. } else {
  3069. if (!upb_isalphanum(c)) {
  3070. upb_status_seterrf(s, "invalid name: non-alphanumeric character (%s)",
  3071. str);
  3072. return false;
  3073. }
  3074. }
  3075. }
  3076. return !start;
  3077. }
  3078. static const char *shortdefname(const char *fullname) {
  3079. const char *p;
  3080. if (fullname == NULL) {
  3081. return NULL;
  3082. } else if ((p = strrchr(fullname, '.')) == NULL) {
  3083. /* No '.' in the name, return the full string. */
  3084. return fullname;
  3085. } else {
  3086. /* Return one past the last '.'. */
  3087. return p + 1;
  3088. }
  3089. }
  3090. /* All submessage fields are lower than all other fields.
  3091. * Secondly, fields are increasing in order. */
  3092. uint32_t field_rank(const upb_fielddef *f) {
  3093. uint32_t ret = upb_fielddef_number(f);
  3094. const uint32_t high_bit = 1 << 30;
  3095. UPB_ASSERT(ret < high_bit);
  3096. if (!upb_fielddef_issubmsg(f))
  3097. ret |= high_bit;
  3098. return ret;
  3099. }
  3100. int cmp_fields(const void *p1, const void *p2) {
  3101. const upb_fielddef *f1 = *(upb_fielddef*const*)p1;
  3102. const upb_fielddef *f2 = *(upb_fielddef*const*)p2;
  3103. return field_rank(f1) - field_rank(f2);
  3104. }
  3105. /* A few implementation details of handlers. We put these here to avoid
  3106. * a def -> handlers dependency. */
  3107. #define UPB_STATIC_SELECTOR_COUNT 3 /* Warning: also in upb/handlers.h. */
  3108. static uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f) {
  3109. return upb_fielddef_isseq(f) ? 2 : 0;
  3110. }
  3111. static uint32_t upb_handlers_selectorcount(const upb_fielddef *f) {
  3112. uint32_t ret = 1;
  3113. if (upb_fielddef_isseq(f)) ret += 2; /* STARTSEQ/ENDSEQ */
  3114. if (upb_fielddef_isstring(f)) ret += 2; /* [STRING]/STARTSTR/ENDSTR */
  3115. if (upb_fielddef_issubmsg(f)) {
  3116. /* ENDSUBMSG (STARTSUBMSG is at table beginning) */
  3117. ret += 0;
  3118. if (upb_fielddef_lazy(f)) {
  3119. /* STARTSTR/ENDSTR/STRING (for lazy) */
  3120. ret += 3;
  3121. }
  3122. }
  3123. return ret;
  3124. }
  3125. static void upb_status_setoom(upb_status *status) {
  3126. upb_status_seterrmsg(status, "out of memory");
  3127. }
  3128. static bool assign_msg_indices(upb_msgdef *m, upb_status *s) {
  3129. /* Sort fields. upb internally relies on UPB_TYPE_MESSAGE fields having the
  3130. * lowest indexes, but we do not publicly guarantee this. */
  3131. upb_msg_field_iter j;
  3132. int i;
  3133. uint32_t selector;
  3134. int n = upb_msgdef_numfields(m);
  3135. upb_fielddef **fields;
  3136. if (n == 0) {
  3137. m->selector_count = UPB_STATIC_SELECTOR_COUNT;
  3138. m->submsg_field_count = 0;
  3139. return true;
  3140. }
  3141. fields = upb_gmalloc(n * sizeof(*fields));
  3142. if (!fields) {
  3143. upb_status_setoom(s);
  3144. return false;
  3145. }
  3146. m->submsg_field_count = 0;
  3147. for(i = 0, upb_msg_field_begin(&j, m);
  3148. !upb_msg_field_done(&j);
  3149. upb_msg_field_next(&j), i++) {
  3150. upb_fielddef *f = upb_msg_iter_field(&j);
  3151. UPB_ASSERT(f->msgdef == m);
  3152. if (upb_fielddef_issubmsg(f)) {
  3153. m->submsg_field_count++;
  3154. }
  3155. fields[i] = f;
  3156. }
  3157. qsort(fields, n, sizeof(*fields), cmp_fields);
  3158. selector = UPB_STATIC_SELECTOR_COUNT + m->submsg_field_count;
  3159. for (i = 0; i < n; i++) {
  3160. upb_fielddef *f = fields[i];
  3161. f->index_ = i;
  3162. f->selector_base = selector + upb_handlers_selectorbaseoffset(f);
  3163. selector += upb_handlers_selectorcount(f);
  3164. }
  3165. m->selector_count = selector;
  3166. upb_gfree(fields);
  3167. return true;
  3168. }
  3169. static bool check_oneofs(upb_msgdef *m, upb_status *s) {
  3170. int i;
  3171. int first_synthetic = -1;
  3172. upb_oneofdef *mutable_oneofs = (upb_oneofdef*)m->oneofs;
  3173. for (i = 0; i < m->oneof_count; i++) {
  3174. mutable_oneofs[i].index = i;
  3175. if (upb_oneofdef_issynthetic(&mutable_oneofs[i])) {
  3176. if (first_synthetic == -1) {
  3177. first_synthetic = i;
  3178. }
  3179. } else {
  3180. if (first_synthetic != -1) {
  3181. upb_status_seterrf(
  3182. s, "Synthetic oneofs must be after all other oneofs: %s",
  3183. upb_oneofdef_name(&mutable_oneofs[i]));
  3184. return false;
  3185. }
  3186. }
  3187. }
  3188. if (first_synthetic == -1) {
  3189. m->real_oneof_count = m->oneof_count;
  3190. } else {
  3191. m->real_oneof_count = first_synthetic;
  3192. }
  3193. return true;
  3194. }
  3195. static void assign_msg_wellknowntype(upb_msgdef *m) {
  3196. const char *name = upb_msgdef_fullname(m);
  3197. if (name == NULL) {
  3198. m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED;
  3199. return;
  3200. }
  3201. if (!strcmp(name, "google.protobuf.Any")) {
  3202. m->well_known_type = UPB_WELLKNOWN_ANY;
  3203. } else if (!strcmp(name, "google.protobuf.FieldMask")) {
  3204. m->well_known_type = UPB_WELLKNOWN_FIELDMASK;
  3205. } else if (!strcmp(name, "google.protobuf.Duration")) {
  3206. m->well_known_type = UPB_WELLKNOWN_DURATION;
  3207. } else if (!strcmp(name, "google.protobuf.Timestamp")) {
  3208. m->well_known_type = UPB_WELLKNOWN_TIMESTAMP;
  3209. } else if (!strcmp(name, "google.protobuf.DoubleValue")) {
  3210. m->well_known_type = UPB_WELLKNOWN_DOUBLEVALUE;
  3211. } else if (!strcmp(name, "google.protobuf.FloatValue")) {
  3212. m->well_known_type = UPB_WELLKNOWN_FLOATVALUE;
  3213. } else if (!strcmp(name, "google.protobuf.Int64Value")) {
  3214. m->well_known_type = UPB_WELLKNOWN_INT64VALUE;
  3215. } else if (!strcmp(name, "google.protobuf.UInt64Value")) {
  3216. m->well_known_type = UPB_WELLKNOWN_UINT64VALUE;
  3217. } else if (!strcmp(name, "google.protobuf.Int32Value")) {
  3218. m->well_known_type = UPB_WELLKNOWN_INT32VALUE;
  3219. } else if (!strcmp(name, "google.protobuf.UInt32Value")) {
  3220. m->well_known_type = UPB_WELLKNOWN_UINT32VALUE;
  3221. } else if (!strcmp(name, "google.protobuf.BoolValue")) {
  3222. m->well_known_type = UPB_WELLKNOWN_BOOLVALUE;
  3223. } else if (!strcmp(name, "google.protobuf.StringValue")) {
  3224. m->well_known_type = UPB_WELLKNOWN_STRINGVALUE;
  3225. } else if (!strcmp(name, "google.protobuf.BytesValue")) {
  3226. m->well_known_type = UPB_WELLKNOWN_BYTESVALUE;
  3227. } else if (!strcmp(name, "google.protobuf.Value")) {
  3228. m->well_known_type = UPB_WELLKNOWN_VALUE;
  3229. } else if (!strcmp(name, "google.protobuf.ListValue")) {
  3230. m->well_known_type = UPB_WELLKNOWN_LISTVALUE;
  3231. } else if (!strcmp(name, "google.protobuf.Struct")) {
  3232. m->well_known_type = UPB_WELLKNOWN_STRUCT;
  3233. } else {
  3234. m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED;
  3235. }
  3236. }
  3237. /* upb_enumdef ****************************************************************/
  3238. const char *upb_enumdef_fullname(const upb_enumdef *e) {
  3239. return e->full_name;
  3240. }
  3241. const char *upb_enumdef_name(const upb_enumdef *e) {
  3242. return shortdefname(e->full_name);
  3243. }
  3244. const upb_filedef *upb_enumdef_file(const upb_enumdef *e) {
  3245. return e->file;
  3246. }
  3247. int32_t upb_enumdef_default(const upb_enumdef *e) {
  3248. UPB_ASSERT(upb_enumdef_iton(e, e->defaultval));
  3249. return e->defaultval;
  3250. }
  3251. int upb_enumdef_numvals(const upb_enumdef *e) {
  3252. return (int)upb_strtable_count(&e->ntoi);
  3253. }
  3254. void upb_enum_begin(upb_enum_iter *i, const upb_enumdef *e) {
  3255. /* We iterate over the ntoi table, to account for duplicate numbers. */
  3256. upb_strtable_begin(i, &e->ntoi);
  3257. }
  3258. void upb_enum_next(upb_enum_iter *iter) { upb_strtable_next(iter); }
  3259. bool upb_enum_done(upb_enum_iter *iter) { return upb_strtable_done(iter); }
  3260. bool upb_enumdef_ntoi(const upb_enumdef *def, const char *name,
  3261. size_t len, int32_t *num) {
  3262. upb_value v;
  3263. if (!upb_strtable_lookup2(&def->ntoi, name, len, &v)) {
  3264. return false;
  3265. }
  3266. if (num) *num = upb_value_getint32(v);
  3267. return true;
  3268. }
  3269. const char *upb_enumdef_iton(const upb_enumdef *def, int32_t num) {
  3270. upb_value v;
  3271. return upb_inttable_lookup32(&def->iton, num, &v) ?
  3272. upb_value_getcstr(v) : NULL;
  3273. }
  3274. const char *upb_enum_iter_name(upb_enum_iter *iter) {
  3275. return upb_strtable_iter_key(iter).data;
  3276. }
  3277. int32_t upb_enum_iter_number(upb_enum_iter *iter) {
  3278. return upb_value_getint32(upb_strtable_iter_value(iter));
  3279. }
  3280. /* upb_fielddef ***************************************************************/
  3281. const char *upb_fielddef_fullname(const upb_fielddef *f) {
  3282. return f->full_name;
  3283. }
  3284. upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f) {
  3285. switch (f->type_) {
  3286. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  3287. return UPB_TYPE_DOUBLE;
  3288. case UPB_DESCRIPTOR_TYPE_FLOAT:
  3289. return UPB_TYPE_FLOAT;
  3290. case UPB_DESCRIPTOR_TYPE_INT64:
  3291. case UPB_DESCRIPTOR_TYPE_SINT64:
  3292. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  3293. return UPB_TYPE_INT64;
  3294. case UPB_DESCRIPTOR_TYPE_INT32:
  3295. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  3296. case UPB_DESCRIPTOR_TYPE_SINT32:
  3297. return UPB_TYPE_INT32;
  3298. case UPB_DESCRIPTOR_TYPE_UINT64:
  3299. case UPB_DESCRIPTOR_TYPE_FIXED64:
  3300. return UPB_TYPE_UINT64;
  3301. case UPB_DESCRIPTOR_TYPE_UINT32:
  3302. case UPB_DESCRIPTOR_TYPE_FIXED32:
  3303. return UPB_TYPE_UINT32;
  3304. case UPB_DESCRIPTOR_TYPE_ENUM:
  3305. return UPB_TYPE_ENUM;
  3306. case UPB_DESCRIPTOR_TYPE_BOOL:
  3307. return UPB_TYPE_BOOL;
  3308. case UPB_DESCRIPTOR_TYPE_STRING:
  3309. return UPB_TYPE_STRING;
  3310. case UPB_DESCRIPTOR_TYPE_BYTES:
  3311. return UPB_TYPE_BYTES;
  3312. case UPB_DESCRIPTOR_TYPE_GROUP:
  3313. case UPB_DESCRIPTOR_TYPE_MESSAGE:
  3314. return UPB_TYPE_MESSAGE;
  3315. }
  3316. UPB_UNREACHABLE();
  3317. }
  3318. upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f) {
  3319. return f->type_;
  3320. }
  3321. uint32_t upb_fielddef_index(const upb_fielddef *f) {
  3322. return f->index_;
  3323. }
  3324. upb_label_t upb_fielddef_label(const upb_fielddef *f) {
  3325. return f->label_;
  3326. }
  3327. uint32_t upb_fielddef_number(const upb_fielddef *f) {
  3328. return f->number_;
  3329. }
  3330. bool upb_fielddef_isextension(const upb_fielddef *f) {
  3331. return f->is_extension_;
  3332. }
  3333. bool upb_fielddef_lazy(const upb_fielddef *f) {
  3334. return f->lazy_;
  3335. }
  3336. bool upb_fielddef_packed(const upb_fielddef *f) {
  3337. return f->packed_;
  3338. }
  3339. const char *upb_fielddef_name(const upb_fielddef *f) {
  3340. return shortdefname(f->full_name);
  3341. }
  3342. const char *upb_fielddef_jsonname(const upb_fielddef *f) {
  3343. return f->json_name;
  3344. }
  3345. uint32_t upb_fielddef_selectorbase(const upb_fielddef *f) {
  3346. return f->selector_base;
  3347. }
  3348. const upb_filedef *upb_fielddef_file(const upb_fielddef *f) {
  3349. return f->file;
  3350. }
  3351. const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f) {
  3352. return f->msgdef;
  3353. }
  3354. const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f) {
  3355. return f->oneof;
  3356. }
  3357. const upb_oneofdef *upb_fielddef_realcontainingoneof(const upb_fielddef *f) {
  3358. if (!f->oneof || upb_oneofdef_issynthetic(f->oneof)) return NULL;
  3359. return f->oneof;
  3360. }
  3361. static void chkdefaulttype(const upb_fielddef *f, int ctype) {
  3362. UPB_UNUSED(f);
  3363. UPB_UNUSED(ctype);
  3364. }
  3365. int64_t upb_fielddef_defaultint64(const upb_fielddef *f) {
  3366. chkdefaulttype(f, UPB_TYPE_INT64);
  3367. return f->defaultval.sint;
  3368. }
  3369. int32_t upb_fielddef_defaultint32(const upb_fielddef *f) {
  3370. chkdefaulttype(f, UPB_TYPE_INT32);
  3371. return (int32_t)f->defaultval.sint;
  3372. }
  3373. uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f) {
  3374. chkdefaulttype(f, UPB_TYPE_UINT64);
  3375. return f->defaultval.uint;
  3376. }
  3377. uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f) {
  3378. chkdefaulttype(f, UPB_TYPE_UINT32);
  3379. return (uint32_t)f->defaultval.uint;
  3380. }
  3381. bool upb_fielddef_defaultbool(const upb_fielddef *f) {
  3382. chkdefaulttype(f, UPB_TYPE_BOOL);
  3383. return f->defaultval.boolean;
  3384. }
  3385. float upb_fielddef_defaultfloat(const upb_fielddef *f) {
  3386. chkdefaulttype(f, UPB_TYPE_FLOAT);
  3387. return f->defaultval.flt;
  3388. }
  3389. double upb_fielddef_defaultdouble(const upb_fielddef *f) {
  3390. chkdefaulttype(f, UPB_TYPE_DOUBLE);
  3391. return f->defaultval.dbl;
  3392. }
  3393. const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len) {
  3394. str_t *str = f->defaultval.str;
  3395. UPB_ASSERT(upb_fielddef_type(f) == UPB_TYPE_STRING ||
  3396. upb_fielddef_type(f) == UPB_TYPE_BYTES ||
  3397. upb_fielddef_type(f) == UPB_TYPE_ENUM);
  3398. if (str) {
  3399. if (len) *len = str->len;
  3400. return str->str;
  3401. } else {
  3402. if (len) *len = 0;
  3403. return NULL;
  3404. }
  3405. }
  3406. const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f) {
  3407. return upb_fielddef_type(f) == UPB_TYPE_MESSAGE ? f->sub.msgdef : NULL;
  3408. }
  3409. const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f) {
  3410. return upb_fielddef_type(f) == UPB_TYPE_ENUM ? f->sub.enumdef : NULL;
  3411. }
  3412. const upb_msglayout_field *upb_fielddef_layout(const upb_fielddef *f) {
  3413. return &f->msgdef->layout->fields[f->layout_index];
  3414. }
  3415. bool upb_fielddef_issubmsg(const upb_fielddef *f) {
  3416. return upb_fielddef_type(f) == UPB_TYPE_MESSAGE;
  3417. }
  3418. bool upb_fielddef_isstring(const upb_fielddef *f) {
  3419. return upb_fielddef_type(f) == UPB_TYPE_STRING ||
  3420. upb_fielddef_type(f) == UPB_TYPE_BYTES;
  3421. }
  3422. bool upb_fielddef_isseq(const upb_fielddef *f) {
  3423. return upb_fielddef_label(f) == UPB_LABEL_REPEATED;
  3424. }
  3425. bool upb_fielddef_isprimitive(const upb_fielddef *f) {
  3426. return !upb_fielddef_isstring(f) && !upb_fielddef_issubmsg(f);
  3427. }
  3428. bool upb_fielddef_ismap(const upb_fielddef *f) {
  3429. return upb_fielddef_isseq(f) && upb_fielddef_issubmsg(f) &&
  3430. upb_msgdef_mapentry(upb_fielddef_msgsubdef(f));
  3431. }
  3432. bool upb_fielddef_hassubdef(const upb_fielddef *f) {
  3433. return upb_fielddef_issubmsg(f) || upb_fielddef_type(f) == UPB_TYPE_ENUM;
  3434. }
  3435. bool upb_fielddef_haspresence(const upb_fielddef *f) {
  3436. if (upb_fielddef_isseq(f)) return false;
  3437. return upb_fielddef_issubmsg(f) || upb_fielddef_containingoneof(f) ||
  3438. f->file->syntax == UPB_SYNTAX_PROTO2;
  3439. }
  3440. static bool between(int32_t x, int32_t low, int32_t high) {
  3441. return x >= low && x <= high;
  3442. }
  3443. bool upb_fielddef_checklabel(int32_t label) { return between(label, 1, 3); }
  3444. bool upb_fielddef_checktype(int32_t type) { return between(type, 1, 11); }
  3445. bool upb_fielddef_checkintfmt(int32_t fmt) { return between(fmt, 1, 3); }
  3446. bool upb_fielddef_checkdescriptortype(int32_t type) {
  3447. return between(type, 1, 18);
  3448. }
  3449. /* upb_msgdef *****************************************************************/
  3450. const char *upb_msgdef_fullname(const upb_msgdef *m) {
  3451. return m->full_name;
  3452. }
  3453. const upb_filedef *upb_msgdef_file(const upb_msgdef *m) {
  3454. return m->file;
  3455. }
  3456. const char *upb_msgdef_name(const upb_msgdef *m) {
  3457. return shortdefname(m->full_name);
  3458. }
  3459. upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m) {
  3460. return m->file->syntax;
  3461. }
  3462. size_t upb_msgdef_selectorcount(const upb_msgdef *m) {
  3463. return m->selector_count;
  3464. }
  3465. uint32_t upb_msgdef_submsgfieldcount(const upb_msgdef *m) {
  3466. return m->submsg_field_count;
  3467. }
  3468. const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i) {
  3469. upb_value val;
  3470. return upb_inttable_lookup32(&m->itof, i, &val) ?
  3471. upb_value_getconstptr(val) : NULL;
  3472. }
  3473. const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name,
  3474. size_t len) {
  3475. upb_value val;
  3476. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  3477. return NULL;
  3478. }
  3479. return unpack_def(val, UPB_DEFTYPE_FIELD);
  3480. }
  3481. const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
  3482. size_t len) {
  3483. upb_value val;
  3484. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  3485. return NULL;
  3486. }
  3487. return unpack_def(val, UPB_DEFTYPE_ONEOF);
  3488. }
  3489. bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len,
  3490. const upb_fielddef **f, const upb_oneofdef **o) {
  3491. upb_value val;
  3492. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  3493. return false;
  3494. }
  3495. *o = unpack_def(val, UPB_DEFTYPE_ONEOF);
  3496. *f = unpack_def(val, UPB_DEFTYPE_FIELD);
  3497. return *o || *f; /* False if this was a JSON name. */
  3498. }
  3499. const upb_fielddef *upb_msgdef_lookupjsonname(const upb_msgdef *m,
  3500. const char *name, size_t len) {
  3501. upb_value val;
  3502. const upb_fielddef* f;
  3503. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  3504. return NULL;
  3505. }
  3506. f = unpack_def(val, UPB_DEFTYPE_FIELD);
  3507. if (!f) f = unpack_def(val, UPB_DEFTYPE_FIELD_JSONNAME);
  3508. return f;
  3509. }
  3510. int upb_msgdef_numfields(const upb_msgdef *m) {
  3511. return m->field_count;
  3512. }
  3513. int upb_msgdef_numoneofs(const upb_msgdef *m) {
  3514. return m->oneof_count;
  3515. }
  3516. int upb_msgdef_numrealoneofs(const upb_msgdef *m) {
  3517. return m->real_oneof_count;
  3518. }
  3519. const upb_msglayout *upb_msgdef_layout(const upb_msgdef *m) {
  3520. return m->layout;
  3521. }
  3522. const upb_fielddef *_upb_msgdef_field(const upb_msgdef *m, int i) {
  3523. if (i >= m->field_count) return NULL;
  3524. return &m->fields[i];
  3525. }
  3526. bool upb_msgdef_mapentry(const upb_msgdef *m) {
  3527. return m->map_entry;
  3528. }
  3529. upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m) {
  3530. return m->well_known_type;
  3531. }
  3532. bool upb_msgdef_isnumberwrapper(const upb_msgdef *m) {
  3533. upb_wellknowntype_t type = upb_msgdef_wellknowntype(m);
  3534. return type >= UPB_WELLKNOWN_DOUBLEVALUE &&
  3535. type <= UPB_WELLKNOWN_UINT32VALUE;
  3536. }
  3537. bool upb_msgdef_iswrapper(const upb_msgdef *m) {
  3538. upb_wellknowntype_t type = upb_msgdef_wellknowntype(m);
  3539. return type >= UPB_WELLKNOWN_DOUBLEVALUE &&
  3540. type <= UPB_WELLKNOWN_BOOLVALUE;
  3541. }
  3542. void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m) {
  3543. upb_inttable_begin(iter, &m->itof);
  3544. }
  3545. void upb_msg_field_next(upb_msg_field_iter *iter) { upb_inttable_next(iter); }
  3546. bool upb_msg_field_done(const upb_msg_field_iter *iter) {
  3547. return upb_inttable_done(iter);
  3548. }
  3549. upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter) {
  3550. return (upb_fielddef *)upb_value_getconstptr(upb_inttable_iter_value(iter));
  3551. }
  3552. void upb_msg_field_iter_setdone(upb_msg_field_iter *iter) {
  3553. upb_inttable_iter_setdone(iter);
  3554. }
  3555. bool upb_msg_field_iter_isequal(const upb_msg_field_iter * iter1,
  3556. const upb_msg_field_iter * iter2) {
  3557. return upb_inttable_iter_isequal(iter1, iter2);
  3558. }
  3559. void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m) {
  3560. upb_strtable_begin(iter, &m->ntof);
  3561. /* We need to skip past any initial fields. */
  3562. while (!upb_strtable_done(iter) &&
  3563. !unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF)) {
  3564. upb_strtable_next(iter);
  3565. }
  3566. }
  3567. void upb_msg_oneof_next(upb_msg_oneof_iter *iter) {
  3568. /* We need to skip past fields to return only oneofs. */
  3569. do {
  3570. upb_strtable_next(iter);
  3571. } while (!upb_strtable_done(iter) &&
  3572. !unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF));
  3573. }
  3574. bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter) {
  3575. return upb_strtable_done(iter);
  3576. }
  3577. const upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter) {
  3578. return unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF);
  3579. }
  3580. void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter) {
  3581. upb_strtable_iter_setdone(iter);
  3582. }
  3583. bool upb_msg_oneof_iter_isequal(const upb_msg_oneof_iter *iter1,
  3584. const upb_msg_oneof_iter *iter2) {
  3585. return upb_strtable_iter_isequal(iter1, iter2);
  3586. }
  3587. /* upb_oneofdef ***************************************************************/
  3588. const char *upb_oneofdef_name(const upb_oneofdef *o) {
  3589. return shortdefname(o->full_name);
  3590. }
  3591. const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o) {
  3592. return o->parent;
  3593. }
  3594. int upb_oneofdef_numfields(const upb_oneofdef *o) {
  3595. return (int)upb_strtable_count(&o->ntof);
  3596. }
  3597. uint32_t upb_oneofdef_index(const upb_oneofdef *o) {
  3598. return o->index;
  3599. }
  3600. bool upb_oneofdef_issynthetic(const upb_oneofdef *o) {
  3601. upb_inttable_iter iter;
  3602. const upb_fielddef *f;
  3603. upb_inttable_begin(&iter, &o->itof);
  3604. if (upb_oneofdef_numfields(o) != 1) return false;
  3605. f = upb_value_getptr(upb_inttable_iter_value(&iter));
  3606. UPB_ASSERT(f);
  3607. return f->proto3_optional_;
  3608. }
  3609. const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o,
  3610. const char *name, size_t length) {
  3611. upb_value val;
  3612. return upb_strtable_lookup2(&o->ntof, name, length, &val) ?
  3613. upb_value_getptr(val) : NULL;
  3614. }
  3615. const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num) {
  3616. upb_value val;
  3617. return upb_inttable_lookup32(&o->itof, num, &val) ?
  3618. upb_value_getptr(val) : NULL;
  3619. }
  3620. void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o) {
  3621. upb_inttable_begin(iter, &o->itof);
  3622. }
  3623. void upb_oneof_next(upb_oneof_iter *iter) {
  3624. upb_inttable_next(iter);
  3625. }
  3626. bool upb_oneof_done(upb_oneof_iter *iter) {
  3627. return upb_inttable_done(iter);
  3628. }
  3629. upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter) {
  3630. return (upb_fielddef *)upb_value_getconstptr(upb_inttable_iter_value(iter));
  3631. }
  3632. void upb_oneof_iter_setdone(upb_oneof_iter *iter) {
  3633. upb_inttable_iter_setdone(iter);
  3634. }
  3635. /* Dynamic Layout Generation. *************************************************/
  3636. static size_t div_round_up(size_t n, size_t d) {
  3637. return (n + d - 1) / d;
  3638. }
  3639. static size_t upb_msgval_sizeof(upb_fieldtype_t type) {
  3640. switch (type) {
  3641. case UPB_TYPE_DOUBLE:
  3642. case UPB_TYPE_INT64:
  3643. case UPB_TYPE_UINT64:
  3644. return 8;
  3645. case UPB_TYPE_ENUM:
  3646. case UPB_TYPE_INT32:
  3647. case UPB_TYPE_UINT32:
  3648. case UPB_TYPE_FLOAT:
  3649. return 4;
  3650. case UPB_TYPE_BOOL:
  3651. return 1;
  3652. case UPB_TYPE_MESSAGE:
  3653. return sizeof(void*);
  3654. case UPB_TYPE_BYTES:
  3655. case UPB_TYPE_STRING:
  3656. return sizeof(upb_strview);
  3657. }
  3658. UPB_UNREACHABLE();
  3659. }
  3660. static uint8_t upb_msg_fielddefsize(const upb_fielddef *f) {
  3661. if (upb_msgdef_mapentry(upb_fielddef_containingtype(f))) {
  3662. upb_map_entry ent;
  3663. UPB_ASSERT(sizeof(ent.k) == sizeof(ent.v));
  3664. return sizeof(ent.k);
  3665. } else if (upb_fielddef_isseq(f)) {
  3666. return sizeof(void*);
  3667. } else {
  3668. return upb_msgval_sizeof(upb_fielddef_type(f));
  3669. }
  3670. }
  3671. static uint32_t upb_msglayout_place(upb_msglayout *l, size_t size) {
  3672. uint32_t ret;
  3673. l->size = UPB_ALIGN_UP(l->size, size);
  3674. ret = l->size;
  3675. l->size += size;
  3676. return ret;
  3677. }
  3678. static int field_number_cmp(const void *p1, const void *p2) {
  3679. const upb_msglayout_field *f1 = p1;
  3680. const upb_msglayout_field *f2 = p2;
  3681. return f1->number - f2->number;
  3682. }
  3683. static void assign_layout_indices(const upb_msgdef *m, upb_msglayout_field *fields) {
  3684. int i;
  3685. int n = upb_msgdef_numfields(m);
  3686. for (i = 0; i < n; i++) {
  3687. upb_fielddef *f = (upb_fielddef*)upb_msgdef_itof(m, fields[i].number);
  3688. UPB_ASSERT(f);
  3689. f->layout_index = i;
  3690. }
  3691. }
  3692. /* This function is the dynamic equivalent of message_layout.{cc,h} in upbc.
  3693. * It computes a dynamic layout for all of the fields in |m|. */
  3694. static bool make_layout(const upb_symtab *symtab, const upb_msgdef *m) {
  3695. upb_msglayout *l = (upb_msglayout*)m->layout;
  3696. upb_msg_field_iter it;
  3697. upb_msg_oneof_iter oit;
  3698. size_t hasbit;
  3699. size_t submsg_count = m->submsg_field_count;
  3700. const upb_msglayout **submsgs;
  3701. upb_msglayout_field *fields;
  3702. upb_alloc *alloc = upb_arena_alloc(symtab->arena);
  3703. memset(l, 0, sizeof(*l));
  3704. fields = upb_malloc(alloc, upb_msgdef_numfields(m) * sizeof(*fields));
  3705. submsgs = upb_malloc(alloc, submsg_count * sizeof(*submsgs));
  3706. if ((!fields && upb_msgdef_numfields(m)) ||
  3707. (!submsgs && submsg_count)) {
  3708. /* OOM. */
  3709. return false;
  3710. }
  3711. l->field_count = upb_msgdef_numfields(m);
  3712. l->fields = fields;
  3713. l->submsgs = submsgs;
  3714. if (upb_msgdef_mapentry(m)) {
  3715. /* TODO(haberman): refactor this method so this special case is more
  3716. * elegant. */
  3717. const upb_fielddef *key = upb_msgdef_itof(m, 1);
  3718. const upb_fielddef *val = upb_msgdef_itof(m, 2);
  3719. fields[0].number = 1;
  3720. fields[1].number = 2;
  3721. fields[0].label = UPB_LABEL_OPTIONAL;
  3722. fields[1].label = UPB_LABEL_OPTIONAL;
  3723. fields[0].presence = 0;
  3724. fields[1].presence = 0;
  3725. fields[0].descriptortype = upb_fielddef_descriptortype(key);
  3726. fields[1].descriptortype = upb_fielddef_descriptortype(val);
  3727. fields[0].offset = 0;
  3728. fields[1].offset = sizeof(upb_strview);
  3729. fields[1].submsg_index = 0;
  3730. if (upb_fielddef_type(val) == UPB_TYPE_MESSAGE) {
  3731. submsgs[0] = upb_fielddef_msgsubdef(val)->layout;
  3732. }
  3733. l->field_count = 2;
  3734. l->size = 2 * sizeof(upb_strview);
  3735. l->size = UPB_ALIGN_UP(l->size, 8);
  3736. return true;
  3737. }
  3738. /* Allocate data offsets in three stages:
  3739. *
  3740. * 1. hasbits.
  3741. * 2. regular fields.
  3742. * 3. oneof fields.
  3743. *
  3744. * OPT: There is a lot of room for optimization here to minimize the size.
  3745. */
  3746. /* Allocate hasbits and set basic field attributes. */
  3747. submsg_count = 0;
  3748. for (upb_msg_field_begin(&it, m), hasbit = 0;
  3749. !upb_msg_field_done(&it);
  3750. upb_msg_field_next(&it)) {
  3751. upb_fielddef* f = upb_msg_iter_field(&it);
  3752. upb_msglayout_field *field = &fields[upb_fielddef_index(f)];
  3753. field->number = upb_fielddef_number(f);
  3754. field->descriptortype = upb_fielddef_descriptortype(f);
  3755. field->label = upb_fielddef_label(f);
  3756. if (upb_fielddef_ismap(f)) {
  3757. field->label = _UPB_LABEL_MAP;
  3758. } else if (upb_fielddef_packed(f)) {
  3759. field->label = _UPB_LABEL_PACKED;
  3760. }
  3761. if (upb_fielddef_issubmsg(f)) {
  3762. const upb_msgdef *subm = upb_fielddef_msgsubdef(f);
  3763. field->submsg_index = submsg_count++;
  3764. submsgs[field->submsg_index] = subm->layout;
  3765. }
  3766. if (upb_fielddef_haspresence(f) && !upb_fielddef_realcontainingoneof(f)) {
  3767. /* We don't use hasbit 0, so that 0 can indicate "no presence" in the
  3768. * table. This wastes one hasbit, but we don't worry about it for now. */
  3769. field->presence = ++hasbit;
  3770. } else {
  3771. field->presence = 0;
  3772. }
  3773. }
  3774. /* Account for space used by hasbits. */
  3775. l->size = div_round_up(hasbit, 8);
  3776. /* Allocate non-oneof fields. */
  3777. for (upb_msg_field_begin(&it, m); !upb_msg_field_done(&it);
  3778. upb_msg_field_next(&it)) {
  3779. const upb_fielddef* f = upb_msg_iter_field(&it);
  3780. size_t field_size = upb_msg_fielddefsize(f);
  3781. size_t index = upb_fielddef_index(f);
  3782. if (upb_fielddef_realcontainingoneof(f)) {
  3783. /* Oneofs are handled separately below. */
  3784. continue;
  3785. }
  3786. fields[index].offset = upb_msglayout_place(l, field_size);
  3787. }
  3788. /* Allocate oneof fields. Each oneof field consists of a uint32 for the case
  3789. * and space for the actual data. */
  3790. for (upb_msg_oneof_begin(&oit, m); !upb_msg_oneof_done(&oit);
  3791. upb_msg_oneof_next(&oit)) {
  3792. const upb_oneofdef* o = upb_msg_iter_oneof(&oit);
  3793. upb_oneof_iter fit;
  3794. size_t case_size = sizeof(uint32_t); /* Could potentially optimize this. */
  3795. size_t field_size = 0;
  3796. uint32_t case_offset;
  3797. uint32_t data_offset;
  3798. if (upb_oneofdef_issynthetic(o)) continue;
  3799. /* Calculate field size: the max of all field sizes. */
  3800. for (upb_oneof_begin(&fit, o);
  3801. !upb_oneof_done(&fit);
  3802. upb_oneof_next(&fit)) {
  3803. const upb_fielddef* f = upb_oneof_iter_field(&fit);
  3804. field_size = UPB_MAX(field_size, upb_msg_fielddefsize(f));
  3805. }
  3806. /* Align and allocate case offset. */
  3807. case_offset = upb_msglayout_place(l, case_size);
  3808. data_offset = upb_msglayout_place(l, field_size);
  3809. for (upb_oneof_begin(&fit, o);
  3810. !upb_oneof_done(&fit);
  3811. upb_oneof_next(&fit)) {
  3812. const upb_fielddef* f = upb_oneof_iter_field(&fit);
  3813. fields[upb_fielddef_index(f)].offset = data_offset;
  3814. fields[upb_fielddef_index(f)].presence = ~case_offset;
  3815. }
  3816. }
  3817. /* Size of the entire structure should be a multiple of its greatest
  3818. * alignment. TODO: track overall alignment for real? */
  3819. l->size = UPB_ALIGN_UP(l->size, 8);
  3820. /* Sort fields by number. */
  3821. qsort(fields, upb_msgdef_numfields(m), sizeof(*fields), field_number_cmp);
  3822. assign_layout_indices(m, fields);
  3823. return true;
  3824. }
  3825. /* Code to build defs from descriptor protos. *********************************/
  3826. /* There is a question of how much validation to do here. It will be difficult
  3827. * to perfectly match the amount of validation performed by proto2. But since
  3828. * this code is used to directly build defs from Ruby (for example) we do need
  3829. * to validate important constraints like uniqueness of names and numbers. */
  3830. #define CHK(x) if (!(x)) { return false; }
  3831. #define CHK_OOM(x) if (!(x)) { upb_status_setoom(ctx->status); return false; }
  3832. typedef struct {
  3833. const upb_symtab *symtab;
  3834. upb_filedef *file; /* File we are building. */
  3835. upb_alloc *alloc; /* Allocate defs here. */
  3836. upb_alloc *tmp; /* Alloc for addtab and any other tmp data. */
  3837. upb_strtable *addtab; /* full_name -> packed def ptr for new defs */
  3838. const upb_msglayout **layouts; /* NULL if we should build layouts. */
  3839. upb_status *status; /* Record errors here. */
  3840. } symtab_addctx;
  3841. static char* strviewdup(const symtab_addctx *ctx, upb_strview view) {
  3842. return upb_strdup2(view.data, view.size, ctx->alloc);
  3843. }
  3844. static bool streql2(const char *a, size_t n, const char *b) {
  3845. return n == strlen(b) && memcmp(a, b, n) == 0;
  3846. }
  3847. static bool streql_view(upb_strview view, const char *b) {
  3848. return streql2(view.data, view.size, b);
  3849. }
  3850. static const char *makefullname(const symtab_addctx *ctx, const char *prefix,
  3851. upb_strview name) {
  3852. if (prefix) {
  3853. /* ret = prefix + '.' + name; */
  3854. size_t n = strlen(prefix);
  3855. char *ret = upb_malloc(ctx->alloc, n + name.size + 2);
  3856. CHK_OOM(ret);
  3857. strcpy(ret, prefix);
  3858. ret[n] = '.';
  3859. memcpy(&ret[n + 1], name.data, name.size);
  3860. ret[n + 1 + name.size] = '\0';
  3861. return ret;
  3862. } else {
  3863. return strviewdup(ctx, name);
  3864. }
  3865. }
  3866. size_t getjsonname(const char *name, char *buf, size_t len) {
  3867. size_t src, dst = 0;
  3868. bool ucase_next = false;
  3869. #define WRITE(byte) \
  3870. ++dst; \
  3871. if (dst < len) buf[dst - 1] = byte; \
  3872. else if (dst == len) buf[dst - 1] = '\0'
  3873. if (!name) {
  3874. WRITE('\0');
  3875. return 0;
  3876. }
  3877. /* Implement the transformation as described in the spec:
  3878. * 1. upper case all letters after an underscore.
  3879. * 2. remove all underscores.
  3880. */
  3881. for (src = 0; name[src]; src++) {
  3882. if (name[src] == '_') {
  3883. ucase_next = true;
  3884. continue;
  3885. }
  3886. if (ucase_next) {
  3887. WRITE(toupper(name[src]));
  3888. ucase_next = false;
  3889. } else {
  3890. WRITE(name[src]);
  3891. }
  3892. }
  3893. WRITE('\0');
  3894. return dst;
  3895. #undef WRITE
  3896. }
  3897. static char* makejsonname(const char* name, upb_alloc *alloc) {
  3898. size_t size = getjsonname(name, NULL, 0);
  3899. char* json_name = upb_malloc(alloc, size);
  3900. getjsonname(name, json_name, size);
  3901. return json_name;
  3902. }
  3903. static bool symtab_add(const symtab_addctx *ctx, const char *name,
  3904. upb_value v) {
  3905. upb_value tmp;
  3906. if (upb_strtable_lookup(ctx->addtab, name, &tmp) ||
  3907. upb_strtable_lookup(&ctx->symtab->syms, name, &tmp)) {
  3908. upb_status_seterrf(ctx->status, "duplicate symbol '%s'", name);
  3909. return false;
  3910. }
  3911. CHK_OOM(upb_strtable_insert3(ctx->addtab, name, strlen(name), v, ctx->tmp));
  3912. return true;
  3913. }
  3914. /* Given a symbol and the base symbol inside which it is defined, find the
  3915. * symbol's definition in t. */
  3916. static bool resolvename(const upb_strtable *t, const upb_fielddef *f,
  3917. const char *base, upb_strview sym,
  3918. upb_deftype_t type, upb_status *status,
  3919. const void **def) {
  3920. if(sym.size == 0) return false;
  3921. if(sym.data[0] == '.') {
  3922. /* Symbols starting with '.' are absolute, so we do a single lookup.
  3923. * Slice to omit the leading '.' */
  3924. upb_value v;
  3925. if (!upb_strtable_lookup2(t, sym.data + 1, sym.size - 1, &v)) {
  3926. return false;
  3927. }
  3928. *def = unpack_def(v, type);
  3929. if (!*def) {
  3930. upb_status_seterrf(status,
  3931. "type mismatch when resolving field %s, name %s",
  3932. f->full_name, sym.data);
  3933. return false;
  3934. }
  3935. return true;
  3936. } else {
  3937. /* Remove components from base until we find an entry or run out.
  3938. * TODO: This branch is totally broken, but currently not used. */
  3939. (void)base;
  3940. UPB_ASSERT(false);
  3941. return false;
  3942. }
  3943. }
  3944. const void *symtab_resolve(const symtab_addctx *ctx, const upb_fielddef *f,
  3945. const char *base, upb_strview sym,
  3946. upb_deftype_t type) {
  3947. const void *ret;
  3948. if (!resolvename(ctx->addtab, f, base, sym, type, ctx->status, &ret) &&
  3949. !resolvename(&ctx->symtab->syms, f, base, sym, type, ctx->status, &ret)) {
  3950. if (upb_ok(ctx->status)) {
  3951. upb_status_seterrf(ctx->status, "couldn't resolve name '%s'", sym.data);
  3952. }
  3953. return false;
  3954. }
  3955. return ret;
  3956. }
  3957. static bool create_oneofdef(
  3958. const symtab_addctx *ctx, upb_msgdef *m,
  3959. const google_protobuf_OneofDescriptorProto *oneof_proto) {
  3960. upb_oneofdef *o;
  3961. upb_strview name = google_protobuf_OneofDescriptorProto_name(oneof_proto);
  3962. upb_value v;
  3963. o = (upb_oneofdef*)&m->oneofs[m->oneof_count++];
  3964. o->parent = m;
  3965. o->full_name = makefullname(ctx, m->full_name, name);
  3966. v = pack_def(o, UPB_DEFTYPE_ONEOF);
  3967. CHK_OOM(symtab_add(ctx, o->full_name, v));
  3968. CHK_OOM(upb_strtable_insert3(&m->ntof, name.data, name.size, v, ctx->alloc));
  3969. CHK_OOM(upb_inttable_init2(&o->itof, UPB_CTYPE_CONSTPTR, ctx->alloc));
  3970. CHK_OOM(upb_strtable_init2(&o->ntof, UPB_CTYPE_CONSTPTR, ctx->alloc));
  3971. return true;
  3972. }
  3973. static bool parse_default(const symtab_addctx *ctx, const char *str, size_t len,
  3974. upb_fielddef *f) {
  3975. char *end;
  3976. char nullz[64];
  3977. errno = 0;
  3978. switch (upb_fielddef_type(f)) {
  3979. case UPB_TYPE_INT32:
  3980. case UPB_TYPE_INT64:
  3981. case UPB_TYPE_UINT32:
  3982. case UPB_TYPE_UINT64:
  3983. case UPB_TYPE_DOUBLE:
  3984. case UPB_TYPE_FLOAT:
  3985. /* Standard C number parsing functions expect null-terminated strings. */
  3986. if (len >= sizeof(nullz) - 1) {
  3987. return false;
  3988. }
  3989. memcpy(nullz, str, len);
  3990. nullz[len] = '\0';
  3991. str = nullz;
  3992. break;
  3993. default:
  3994. break;
  3995. }
  3996. switch (upb_fielddef_type(f)) {
  3997. case UPB_TYPE_INT32: {
  3998. long val = strtol(str, &end, 0);
  3999. CHK(val <= INT32_MAX && val >= INT32_MIN && errno != ERANGE && !*end);
  4000. f->defaultval.sint = val;
  4001. break;
  4002. }
  4003. case UPB_TYPE_ENUM: {
  4004. const upb_enumdef *e = f->sub.enumdef;
  4005. int32_t val;
  4006. CHK(upb_enumdef_ntoi(e, str, len, &val));
  4007. f->defaultval.sint = val;
  4008. break;
  4009. }
  4010. case UPB_TYPE_INT64: {
  4011. /* XXX: Need to write our own strtoll, since it's not available in c89. */
  4012. int64_t val = strtol(str, &end, 0);
  4013. CHK(val <= INT64_MAX && val >= INT64_MIN && errno != ERANGE && !*end);
  4014. f->defaultval.sint = val;
  4015. break;
  4016. }
  4017. case UPB_TYPE_UINT32: {
  4018. unsigned long val = strtoul(str, &end, 0);
  4019. CHK(val <= UINT32_MAX && errno != ERANGE && !*end);
  4020. f->defaultval.uint = val;
  4021. break;
  4022. }
  4023. case UPB_TYPE_UINT64: {
  4024. /* XXX: Need to write our own strtoull, since it's not available in c89. */
  4025. uint64_t val = strtoul(str, &end, 0);
  4026. CHK(val <= UINT64_MAX && errno != ERANGE && !*end);
  4027. f->defaultval.uint = val;
  4028. break;
  4029. }
  4030. case UPB_TYPE_DOUBLE: {
  4031. double val = strtod(str, &end);
  4032. CHK(errno != ERANGE && !*end);
  4033. f->defaultval.dbl = val;
  4034. break;
  4035. }
  4036. case UPB_TYPE_FLOAT: {
  4037. /* XXX: Need to write our own strtof, since it's not available in c89. */
  4038. float val = strtod(str, &end);
  4039. CHK(errno != ERANGE && !*end);
  4040. f->defaultval.flt = val;
  4041. break;
  4042. }
  4043. case UPB_TYPE_BOOL: {
  4044. if (streql2(str, len, "false")) {
  4045. f->defaultval.boolean = false;
  4046. } else if (streql2(str, len, "true")) {
  4047. f->defaultval.boolean = true;
  4048. } else {
  4049. return false;
  4050. }
  4051. break;
  4052. }
  4053. case UPB_TYPE_STRING:
  4054. f->defaultval.str = newstr(ctx->alloc, str, len);
  4055. break;
  4056. case UPB_TYPE_BYTES:
  4057. /* XXX: need to interpret the C-escaped value. */
  4058. f->defaultval.str = newstr(ctx->alloc, str, len);
  4059. break;
  4060. case UPB_TYPE_MESSAGE:
  4061. /* Should not have a default value. */
  4062. return false;
  4063. }
  4064. return true;
  4065. }
  4066. static void set_default_default(const symtab_addctx *ctx, upb_fielddef *f) {
  4067. switch (upb_fielddef_type(f)) {
  4068. case UPB_TYPE_INT32:
  4069. case UPB_TYPE_INT64:
  4070. case UPB_TYPE_ENUM:
  4071. f->defaultval.sint = 0;
  4072. break;
  4073. case UPB_TYPE_UINT64:
  4074. case UPB_TYPE_UINT32:
  4075. f->defaultval.uint = 0;
  4076. break;
  4077. case UPB_TYPE_DOUBLE:
  4078. case UPB_TYPE_FLOAT:
  4079. f->defaultval.dbl = 0;
  4080. break;
  4081. case UPB_TYPE_STRING:
  4082. case UPB_TYPE_BYTES:
  4083. f->defaultval.str = newstr(ctx->alloc, NULL, 0);
  4084. break;
  4085. case UPB_TYPE_BOOL:
  4086. f->defaultval.boolean = false;
  4087. break;
  4088. case UPB_TYPE_MESSAGE:
  4089. break;
  4090. }
  4091. }
  4092. static bool create_fielddef(
  4093. const symtab_addctx *ctx, const char *prefix, upb_msgdef *m,
  4094. const google_protobuf_FieldDescriptorProto *field_proto) {
  4095. upb_alloc *alloc = ctx->alloc;
  4096. upb_fielddef *f;
  4097. const google_protobuf_FieldOptions *options;
  4098. upb_strview name;
  4099. const char *full_name;
  4100. const char *json_name;
  4101. const char *shortname;
  4102. uint32_t field_number;
  4103. if (!google_protobuf_FieldDescriptorProto_has_name(field_proto)) {
  4104. upb_status_seterrmsg(ctx->status, "field has no name");
  4105. return false;
  4106. }
  4107. name = google_protobuf_FieldDescriptorProto_name(field_proto);
  4108. CHK(upb_isident(name, false, ctx->status));
  4109. full_name = makefullname(ctx, prefix, name);
  4110. shortname = shortdefname(full_name);
  4111. if (google_protobuf_FieldDescriptorProto_has_json_name(field_proto)) {
  4112. json_name = strviewdup(
  4113. ctx, google_protobuf_FieldDescriptorProto_json_name(field_proto));
  4114. } else {
  4115. json_name = makejsonname(shortname, ctx->alloc);
  4116. }
  4117. field_number = google_protobuf_FieldDescriptorProto_number(field_proto);
  4118. if (field_number == 0 || field_number > UPB_MAX_FIELDNUMBER) {
  4119. upb_status_seterrf(ctx->status, "invalid field number (%u)", field_number);
  4120. return false;
  4121. }
  4122. if (m) {
  4123. /* direct message field. */
  4124. upb_value v, field_v, json_v;
  4125. size_t json_size;
  4126. f = (upb_fielddef*)&m->fields[m->field_count++];
  4127. f->msgdef = m;
  4128. f->is_extension_ = false;
  4129. if (upb_strtable_lookup(&m->ntof, shortname, NULL)) {
  4130. upb_status_seterrf(ctx->status, "duplicate field name (%s)", shortname);
  4131. return false;
  4132. }
  4133. if (upb_strtable_lookup(&m->ntof, json_name, NULL)) {
  4134. upb_status_seterrf(ctx->status, "duplicate json_name (%s)", json_name);
  4135. return false;
  4136. }
  4137. if (upb_inttable_lookup(&m->itof, field_number, NULL)) {
  4138. upb_status_seterrf(ctx->status, "duplicate field number (%u)",
  4139. field_number);
  4140. return false;
  4141. }
  4142. field_v = pack_def(f, UPB_DEFTYPE_FIELD);
  4143. json_v = pack_def(f, UPB_DEFTYPE_FIELD_JSONNAME);
  4144. v = upb_value_constptr(f);
  4145. json_size = strlen(json_name);
  4146. CHK_OOM(
  4147. upb_strtable_insert3(&m->ntof, name.data, name.size, field_v, alloc));
  4148. CHK_OOM(upb_inttable_insert2(&m->itof, field_number, v, alloc));
  4149. if (strcmp(shortname, json_name) != 0) {
  4150. upb_strtable_insert3(&m->ntof, json_name, json_size, json_v, alloc);
  4151. }
  4152. if (ctx->layouts) {
  4153. const upb_msglayout_field *fields = m->layout->fields;
  4154. int count = m->layout->field_count;
  4155. bool found = false;
  4156. int i;
  4157. for (i = 0; i < count; i++) {
  4158. if (fields[i].number == field_number) {
  4159. f->layout_index = i;
  4160. found = true;
  4161. break;
  4162. }
  4163. }
  4164. UPB_ASSERT(found);
  4165. }
  4166. } else {
  4167. /* extension field. */
  4168. f = (upb_fielddef*)&ctx->file->exts[ctx->file->ext_count++];
  4169. f->is_extension_ = true;
  4170. CHK_OOM(symtab_add(ctx, full_name, pack_def(f, UPB_DEFTYPE_FIELD)));
  4171. }
  4172. f->full_name = full_name;
  4173. f->json_name = json_name;
  4174. f->file = ctx->file;
  4175. f->type_ = (int)google_protobuf_FieldDescriptorProto_type(field_proto);
  4176. f->label_ = (int)google_protobuf_FieldDescriptorProto_label(field_proto);
  4177. f->number_ = field_number;
  4178. f->oneof = NULL;
  4179. f->proto3_optional_ =
  4180. google_protobuf_FieldDescriptorProto_proto3_optional(field_proto);
  4181. /* We can't resolve the subdef or (in the case of extensions) the containing
  4182. * message yet, because it may not have been defined yet. We stash a pointer
  4183. * to the field_proto until later when we can properly resolve it. */
  4184. f->sub.unresolved = field_proto;
  4185. if (f->label_ == UPB_LABEL_REQUIRED && f->file->syntax == UPB_SYNTAX_PROTO3) {
  4186. upb_status_seterrf(ctx->status, "proto3 fields cannot be required (%s)",
  4187. f->full_name);
  4188. return false;
  4189. }
  4190. if (google_protobuf_FieldDescriptorProto_has_oneof_index(field_proto)) {
  4191. int oneof_index =
  4192. google_protobuf_FieldDescriptorProto_oneof_index(field_proto);
  4193. upb_oneofdef *oneof;
  4194. upb_value v = upb_value_constptr(f);
  4195. if (upb_fielddef_label(f) != UPB_LABEL_OPTIONAL) {
  4196. upb_status_seterrf(ctx->status,
  4197. "fields in oneof must have OPTIONAL label (%s)",
  4198. f->full_name);
  4199. return false;
  4200. }
  4201. if (!m) {
  4202. upb_status_seterrf(ctx->status,
  4203. "oneof_index provided for extension field (%s)",
  4204. f->full_name);
  4205. return false;
  4206. }
  4207. if (oneof_index >= m->oneof_count) {
  4208. upb_status_seterrf(ctx->status, "oneof_index out of range (%s)",
  4209. f->full_name);
  4210. return false;
  4211. }
  4212. oneof = (upb_oneofdef*)&m->oneofs[oneof_index];
  4213. f->oneof = oneof;
  4214. CHK(upb_inttable_insert2(&oneof->itof, f->number_, v, alloc));
  4215. CHK(upb_strtable_insert3(&oneof->ntof, name.data, name.size, v, alloc));
  4216. } else {
  4217. f->oneof = NULL;
  4218. }
  4219. if (google_protobuf_FieldDescriptorProto_has_options(field_proto)) {
  4220. options = google_protobuf_FieldDescriptorProto_options(field_proto);
  4221. f->lazy_ = google_protobuf_FieldOptions_lazy(options);
  4222. f->packed_ = google_protobuf_FieldOptions_packed(options);
  4223. } else {
  4224. f->lazy_ = false;
  4225. f->packed_ = false;
  4226. }
  4227. return true;
  4228. }
  4229. static bool create_enumdef(
  4230. const symtab_addctx *ctx, const char *prefix,
  4231. const google_protobuf_EnumDescriptorProto *enum_proto) {
  4232. upb_enumdef *e;
  4233. const google_protobuf_EnumValueDescriptorProto *const *values;
  4234. upb_strview name;
  4235. size_t i, n;
  4236. name = google_protobuf_EnumDescriptorProto_name(enum_proto);
  4237. CHK(upb_isident(name, false, ctx->status));
  4238. e = (upb_enumdef*)&ctx->file->enums[ctx->file->enum_count++];
  4239. e->full_name = makefullname(ctx, prefix, name);
  4240. CHK_OOM(symtab_add(ctx, e->full_name, pack_def(e, UPB_DEFTYPE_ENUM)));
  4241. CHK_OOM(upb_strtable_init2(&e->ntoi, UPB_CTYPE_INT32, ctx->alloc));
  4242. CHK_OOM(upb_inttable_init2(&e->iton, UPB_CTYPE_CSTR, ctx->alloc));
  4243. e->file = ctx->file;
  4244. e->defaultval = 0;
  4245. values = google_protobuf_EnumDescriptorProto_value(enum_proto, &n);
  4246. if (n == 0) {
  4247. upb_status_seterrf(ctx->status,
  4248. "enums must contain at least one value (%s)",
  4249. e->full_name);
  4250. return false;
  4251. }
  4252. for (i = 0; i < n; i++) {
  4253. const google_protobuf_EnumValueDescriptorProto *value = values[i];
  4254. upb_strview name = google_protobuf_EnumValueDescriptorProto_name(value);
  4255. char *name2 = strviewdup(ctx, name);
  4256. int32_t num = google_protobuf_EnumValueDescriptorProto_number(value);
  4257. upb_value v = upb_value_int32(num);
  4258. if (i == 0 && e->file->syntax == UPB_SYNTAX_PROTO3 && num != 0) {
  4259. upb_status_seterrf(ctx->status,
  4260. "for proto3, the first enum value must be zero (%s)",
  4261. e->full_name);
  4262. return false;
  4263. }
  4264. if (upb_strtable_lookup(&e->ntoi, name2, NULL)) {
  4265. upb_status_seterrf(ctx->status, "duplicate enum label '%s'", name2);
  4266. return false;
  4267. }
  4268. CHK_OOM(name2)
  4269. CHK_OOM(
  4270. upb_strtable_insert3(&e->ntoi, name2, strlen(name2), v, ctx->alloc));
  4271. if (!upb_inttable_lookup(&e->iton, num, NULL)) {
  4272. upb_value v = upb_value_cstr(name2);
  4273. CHK_OOM(upb_inttable_insert2(&e->iton, num, v, ctx->alloc));
  4274. }
  4275. }
  4276. upb_inttable_compact2(&e->iton, ctx->alloc);
  4277. return true;
  4278. }
  4279. static bool create_msgdef(symtab_addctx *ctx, const char *prefix,
  4280. const google_protobuf_DescriptorProto *msg_proto) {
  4281. upb_msgdef *m;
  4282. const google_protobuf_MessageOptions *options;
  4283. const google_protobuf_OneofDescriptorProto *const *oneofs;
  4284. const google_protobuf_FieldDescriptorProto *const *fields;
  4285. const google_protobuf_EnumDescriptorProto *const *enums;
  4286. const google_protobuf_DescriptorProto *const *msgs;
  4287. size_t i, n;
  4288. upb_strview name;
  4289. name = google_protobuf_DescriptorProto_name(msg_proto);
  4290. CHK(upb_isident(name, false, ctx->status));
  4291. m = (upb_msgdef*)&ctx->file->msgs[ctx->file->msg_count++];
  4292. m->full_name = makefullname(ctx, prefix, name);
  4293. CHK_OOM(symtab_add(ctx, m->full_name, pack_def(m, UPB_DEFTYPE_MSG)));
  4294. CHK_OOM(upb_inttable_init2(&m->itof, UPB_CTYPE_CONSTPTR, ctx->alloc));
  4295. CHK_OOM(upb_strtable_init2(&m->ntof, UPB_CTYPE_CONSTPTR, ctx->alloc));
  4296. m->file = ctx->file;
  4297. m->map_entry = false;
  4298. options = google_protobuf_DescriptorProto_options(msg_proto);
  4299. if (options) {
  4300. m->map_entry = google_protobuf_MessageOptions_map_entry(options);
  4301. }
  4302. if (ctx->layouts) {
  4303. m->layout = *ctx->layouts;
  4304. ctx->layouts++;
  4305. } else {
  4306. /* Allocate now (to allow cross-linking), populate later. */
  4307. m->layout = upb_malloc(ctx->alloc, sizeof(*m->layout));
  4308. }
  4309. oneofs = google_protobuf_DescriptorProto_oneof_decl(msg_proto, &n);
  4310. m->oneof_count = 0;
  4311. m->oneofs = upb_malloc(ctx->alloc, sizeof(*m->oneofs) * n);
  4312. for (i = 0; i < n; i++) {
  4313. CHK(create_oneofdef(ctx, m, oneofs[i]));
  4314. }
  4315. fields = google_protobuf_DescriptorProto_field(msg_proto, &n);
  4316. m->field_count = 0;
  4317. m->fields = upb_malloc(ctx->alloc, sizeof(*m->fields) * n);
  4318. for (i = 0; i < n; i++) {
  4319. CHK(create_fielddef(ctx, m->full_name, m, fields[i]));
  4320. }
  4321. CHK(assign_msg_indices(m, ctx->status));
  4322. CHK(check_oneofs(m, ctx->status));
  4323. assign_msg_wellknowntype(m);
  4324. upb_inttable_compact2(&m->itof, ctx->alloc);
  4325. /* This message is built. Now build nested messages and enums. */
  4326. enums = google_protobuf_DescriptorProto_enum_type(msg_proto, &n);
  4327. for (i = 0; i < n; i++) {
  4328. CHK(create_enumdef(ctx, m->full_name, enums[i]));
  4329. }
  4330. msgs = google_protobuf_DescriptorProto_nested_type(msg_proto, &n);
  4331. for (i = 0; i < n; i++) {
  4332. CHK(create_msgdef(ctx, m->full_name, msgs[i]));
  4333. }
  4334. return true;
  4335. }
  4336. typedef struct {
  4337. int msg_count;
  4338. int enum_count;
  4339. int ext_count;
  4340. } decl_counts;
  4341. static void count_types_in_msg(const google_protobuf_DescriptorProto *msg_proto,
  4342. decl_counts *counts) {
  4343. const google_protobuf_DescriptorProto *const *msgs;
  4344. size_t i, n;
  4345. counts->msg_count++;
  4346. msgs = google_protobuf_DescriptorProto_nested_type(msg_proto, &n);
  4347. for (i = 0; i < n; i++) {
  4348. count_types_in_msg(msgs[i], counts);
  4349. }
  4350. google_protobuf_DescriptorProto_enum_type(msg_proto, &n);
  4351. counts->enum_count += n;
  4352. google_protobuf_DescriptorProto_extension(msg_proto, &n);
  4353. counts->ext_count += n;
  4354. }
  4355. static void count_types_in_file(
  4356. const google_protobuf_FileDescriptorProto *file_proto,
  4357. decl_counts *counts) {
  4358. const google_protobuf_DescriptorProto *const *msgs;
  4359. size_t i, n;
  4360. msgs = google_protobuf_FileDescriptorProto_message_type(file_proto, &n);
  4361. for (i = 0; i < n; i++) {
  4362. count_types_in_msg(msgs[i], counts);
  4363. }
  4364. google_protobuf_FileDescriptorProto_enum_type(file_proto, &n);
  4365. counts->enum_count += n;
  4366. google_protobuf_FileDescriptorProto_extension(file_proto, &n);
  4367. counts->ext_count += n;
  4368. }
  4369. static bool resolve_fielddef(const symtab_addctx *ctx, const char *prefix,
  4370. upb_fielddef *f) {
  4371. upb_strview name;
  4372. const google_protobuf_FieldDescriptorProto *field_proto = f->sub.unresolved;
  4373. if (f->is_extension_) {
  4374. if (!google_protobuf_FieldDescriptorProto_has_extendee(field_proto)) {
  4375. upb_status_seterrf(ctx->status,
  4376. "extension for field '%s' had no extendee",
  4377. f->full_name);
  4378. return false;
  4379. }
  4380. name = google_protobuf_FieldDescriptorProto_extendee(field_proto);
  4381. f->msgdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_MSG);
  4382. CHK(f->msgdef);
  4383. }
  4384. if ((upb_fielddef_issubmsg(f) || f->type_ == UPB_DESCRIPTOR_TYPE_ENUM) &&
  4385. !google_protobuf_FieldDescriptorProto_has_type_name(field_proto)) {
  4386. upb_status_seterrf(ctx->status, "field '%s' is missing type name",
  4387. f->full_name);
  4388. return false;
  4389. }
  4390. name = google_protobuf_FieldDescriptorProto_type_name(field_proto);
  4391. if (upb_fielddef_issubmsg(f)) {
  4392. f->sub.msgdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_MSG);
  4393. CHK(f->sub.msgdef);
  4394. } else if (f->type_ == UPB_DESCRIPTOR_TYPE_ENUM) {
  4395. f->sub.enumdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_ENUM);
  4396. CHK(f->sub.enumdef);
  4397. }
  4398. /* Have to delay resolving of the default value until now because of the enum
  4399. * case, since enum defaults are specified with a label. */
  4400. if (google_protobuf_FieldDescriptorProto_has_default_value(field_proto)) {
  4401. upb_strview defaultval =
  4402. google_protobuf_FieldDescriptorProto_default_value(field_proto);
  4403. if (f->file->syntax == UPB_SYNTAX_PROTO3) {
  4404. upb_status_seterrf(ctx->status,
  4405. "proto3 fields cannot have explicit defaults (%s)",
  4406. f->full_name);
  4407. return false;
  4408. }
  4409. if (upb_fielddef_issubmsg(f)) {
  4410. upb_status_seterrf(ctx->status,
  4411. "message fields cannot have explicit defaults (%s)",
  4412. f->full_name);
  4413. return false;
  4414. }
  4415. if (!parse_default(ctx, defaultval.data, defaultval.size, f)) {
  4416. upb_status_seterrf(ctx->status,
  4417. "couldn't parse default '" UPB_STRVIEW_FORMAT
  4418. "' for field (%s)",
  4419. UPB_STRVIEW_ARGS(defaultval), f->full_name);
  4420. return false;
  4421. }
  4422. } else {
  4423. set_default_default(ctx, f);
  4424. }
  4425. return true;
  4426. }
  4427. static bool build_filedef(
  4428. symtab_addctx *ctx, upb_filedef *file,
  4429. const google_protobuf_FileDescriptorProto *file_proto) {
  4430. upb_alloc *alloc = ctx->alloc;
  4431. const google_protobuf_FileOptions *file_options_proto;
  4432. const google_protobuf_DescriptorProto *const *msgs;
  4433. const google_protobuf_EnumDescriptorProto *const *enums;
  4434. const google_protobuf_FieldDescriptorProto *const *exts;
  4435. const upb_strview* strs;
  4436. size_t i, n;
  4437. decl_counts counts = {0};
  4438. count_types_in_file(file_proto, &counts);
  4439. file->msgs = upb_malloc(alloc, sizeof(*file->msgs) * counts.msg_count);
  4440. file->enums = upb_malloc(alloc, sizeof(*file->enums) * counts.enum_count);
  4441. file->exts = upb_malloc(alloc, sizeof(*file->exts) * counts.ext_count);
  4442. CHK_OOM(counts.msg_count == 0 || file->msgs);
  4443. CHK_OOM(counts.enum_count == 0 || file->enums);
  4444. CHK_OOM(counts.ext_count == 0 || file->exts);
  4445. /* We increment these as defs are added. */
  4446. file->msg_count = 0;
  4447. file->enum_count = 0;
  4448. file->ext_count = 0;
  4449. if (!google_protobuf_FileDescriptorProto_has_name(file_proto)) {
  4450. upb_status_seterrmsg(ctx->status, "File has no name");
  4451. return false;
  4452. }
  4453. file->name =
  4454. strviewdup(ctx, google_protobuf_FileDescriptorProto_name(file_proto));
  4455. file->phpprefix = NULL;
  4456. file->phpnamespace = NULL;
  4457. if (google_protobuf_FileDescriptorProto_has_package(file_proto)) {
  4458. upb_strview package =
  4459. google_protobuf_FileDescriptorProto_package(file_proto);
  4460. CHK(upb_isident(package, true, ctx->status));
  4461. file->package = strviewdup(ctx, package);
  4462. } else {
  4463. file->package = NULL;
  4464. }
  4465. if (google_protobuf_FileDescriptorProto_has_syntax(file_proto)) {
  4466. upb_strview syntax =
  4467. google_protobuf_FileDescriptorProto_syntax(file_proto);
  4468. if (streql_view(syntax, "proto2")) {
  4469. file->syntax = UPB_SYNTAX_PROTO2;
  4470. } else if (streql_view(syntax, "proto3")) {
  4471. file->syntax = UPB_SYNTAX_PROTO3;
  4472. } else {
  4473. upb_status_seterrf(ctx->status, "Invalid syntax '" UPB_STRVIEW_FORMAT "'",
  4474. UPB_STRVIEW_ARGS(syntax));
  4475. return false;
  4476. }
  4477. } else {
  4478. file->syntax = UPB_SYNTAX_PROTO2;
  4479. }
  4480. /* Read options. */
  4481. file_options_proto = google_protobuf_FileDescriptorProto_options(file_proto);
  4482. if (file_options_proto) {
  4483. if (google_protobuf_FileOptions_has_php_class_prefix(file_options_proto)) {
  4484. file->phpprefix = strviewdup(
  4485. ctx,
  4486. google_protobuf_FileOptions_php_class_prefix(file_options_proto));
  4487. }
  4488. if (google_protobuf_FileOptions_has_php_namespace(file_options_proto)) {
  4489. file->phpnamespace = strviewdup(
  4490. ctx, google_protobuf_FileOptions_php_namespace(file_options_proto));
  4491. }
  4492. }
  4493. /* Verify dependencies. */
  4494. strs = google_protobuf_FileDescriptorProto_dependency(file_proto, &n);
  4495. file->deps = upb_malloc(alloc, sizeof(*file->deps) * n) ;
  4496. CHK_OOM(n == 0 || file->deps);
  4497. for (i = 0; i < n; i++) {
  4498. upb_strview dep_name = strs[i];
  4499. upb_value v;
  4500. if (!upb_strtable_lookup2(&ctx->symtab->files, dep_name.data,
  4501. dep_name.size, &v)) {
  4502. upb_status_seterrf(ctx->status,
  4503. "Depends on file '" UPB_STRVIEW_FORMAT
  4504. "', but it has not been loaded",
  4505. UPB_STRVIEW_ARGS(dep_name));
  4506. return false;
  4507. }
  4508. file->deps[i] = upb_value_getconstptr(v);
  4509. }
  4510. /* Create messages. */
  4511. msgs = google_protobuf_FileDescriptorProto_message_type(file_proto, &n);
  4512. for (i = 0; i < n; i++) {
  4513. CHK(create_msgdef(ctx, file->package, msgs[i]));
  4514. }
  4515. /* Create enums. */
  4516. enums = google_protobuf_FileDescriptorProto_enum_type(file_proto, &n);
  4517. for (i = 0; i < n; i++) {
  4518. CHK(create_enumdef(ctx, file->package, enums[i]));
  4519. }
  4520. /* Create extensions. */
  4521. exts = google_protobuf_FileDescriptorProto_extension(file_proto, &n);
  4522. file->exts = upb_malloc(alloc, sizeof(*file->exts) * n);
  4523. CHK_OOM(n == 0 || file->exts);
  4524. for (i = 0; i < n; i++) {
  4525. CHK(create_fielddef(ctx, file->package, NULL, exts[i]));
  4526. }
  4527. /* Now that all names are in the table, build layouts and resolve refs. */
  4528. for (i = 0; i < (size_t)file->ext_count; i++) {
  4529. CHK(resolve_fielddef(ctx, file->package, (upb_fielddef*)&file->exts[i]));
  4530. }
  4531. for (i = 0; i < (size_t)file->msg_count; i++) {
  4532. const upb_msgdef *m = &file->msgs[i];
  4533. int j;
  4534. for (j = 0; j < m->field_count; j++) {
  4535. CHK(resolve_fielddef(ctx, m->full_name, (upb_fielddef*)&m->fields[j]));
  4536. }
  4537. }
  4538. if (!ctx->layouts) {
  4539. for (i = 0; i < (size_t)file->msg_count; i++) {
  4540. const upb_msgdef *m = &file->msgs[i];
  4541. make_layout(ctx->symtab, m);
  4542. }
  4543. }
  4544. return true;
  4545. }
  4546. static bool upb_symtab_addtotabs(upb_symtab *s, symtab_addctx *ctx) {
  4547. const upb_filedef *file = ctx->file;
  4548. upb_alloc *alloc = upb_arena_alloc(s->arena);
  4549. upb_strtable_iter iter;
  4550. CHK_OOM(upb_strtable_insert3(&s->files, file->name, strlen(file->name),
  4551. upb_value_constptr(file), alloc));
  4552. upb_strtable_begin(&iter, ctx->addtab);
  4553. for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) {
  4554. upb_strview key = upb_strtable_iter_key(&iter);
  4555. upb_value value = upb_strtable_iter_value(&iter);
  4556. CHK_OOM(upb_strtable_insert3(&s->syms, key.data, key.size, value, alloc));
  4557. }
  4558. return true;
  4559. }
  4560. /* upb_filedef ****************************************************************/
  4561. const char *upb_filedef_name(const upb_filedef *f) {
  4562. return f->name;
  4563. }
  4564. const char *upb_filedef_package(const upb_filedef *f) {
  4565. return f->package;
  4566. }
  4567. const char *upb_filedef_phpprefix(const upb_filedef *f) {
  4568. return f->phpprefix;
  4569. }
  4570. const char *upb_filedef_phpnamespace(const upb_filedef *f) {
  4571. return f->phpnamespace;
  4572. }
  4573. upb_syntax_t upb_filedef_syntax(const upb_filedef *f) {
  4574. return f->syntax;
  4575. }
  4576. int upb_filedef_msgcount(const upb_filedef *f) {
  4577. return f->msg_count;
  4578. }
  4579. int upb_filedef_depcount(const upb_filedef *f) {
  4580. return f->dep_count;
  4581. }
  4582. int upb_filedef_enumcount(const upb_filedef *f) {
  4583. return f->enum_count;
  4584. }
  4585. const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i) {
  4586. return i < 0 || i >= f->dep_count ? NULL : f->deps[i];
  4587. }
  4588. const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i) {
  4589. return i < 0 || i >= f->msg_count ? NULL : &f->msgs[i];
  4590. }
  4591. const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i) {
  4592. return i < 0 || i >= f->enum_count ? NULL : &f->enums[i];
  4593. }
  4594. void upb_symtab_free(upb_symtab *s) {
  4595. upb_arena_free(s->arena);
  4596. upb_gfree(s);
  4597. }
  4598. upb_symtab *upb_symtab_new(void) {
  4599. upb_symtab *s = upb_gmalloc(sizeof(*s));
  4600. upb_alloc *alloc;
  4601. if (!s) {
  4602. return NULL;
  4603. }
  4604. s->arena = upb_arena_new();
  4605. alloc = upb_arena_alloc(s->arena);
  4606. if (!upb_strtable_init2(&s->syms, UPB_CTYPE_CONSTPTR, alloc) ||
  4607. !upb_strtable_init2(&s->files, UPB_CTYPE_CONSTPTR, alloc)) {
  4608. upb_arena_free(s->arena);
  4609. upb_gfree(s);
  4610. s = NULL;
  4611. }
  4612. return s;
  4613. }
  4614. const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym) {
  4615. upb_value v;
  4616. return upb_strtable_lookup(&s->syms, sym, &v) ?
  4617. unpack_def(v, UPB_DEFTYPE_MSG) : NULL;
  4618. }
  4619. const upb_msgdef *upb_symtab_lookupmsg2(const upb_symtab *s, const char *sym,
  4620. size_t len) {
  4621. upb_value v;
  4622. return upb_strtable_lookup2(&s->syms, sym, len, &v) ?
  4623. unpack_def(v, UPB_DEFTYPE_MSG) : NULL;
  4624. }
  4625. const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) {
  4626. upb_value v;
  4627. return upb_strtable_lookup(&s->syms, sym, &v) ?
  4628. unpack_def(v, UPB_DEFTYPE_ENUM) : NULL;
  4629. }
  4630. const upb_filedef *upb_symtab_lookupfile(const upb_symtab *s, const char *name) {
  4631. upb_value v;
  4632. return upb_strtable_lookup(&s->files, name, &v) ? upb_value_getconstptr(v)
  4633. : NULL;
  4634. }
  4635. const upb_filedef *upb_symtab_lookupfile2(
  4636. const upb_symtab *s, const char *name, size_t len) {
  4637. upb_value v;
  4638. return upb_strtable_lookup2(&s->files, name, len, &v) ?
  4639. upb_value_getconstptr(v) : NULL;
  4640. }
  4641. int upb_symtab_filecount(const upb_symtab *s) {
  4642. return (int)upb_strtable_count(&s->files);
  4643. }
  4644. static const upb_filedef *_upb_symtab_addfile(
  4645. upb_symtab *s, const google_protobuf_FileDescriptorProto *file_proto,
  4646. const upb_msglayout **layouts, upb_status *status) {
  4647. upb_arena *tmparena = upb_arena_new();
  4648. upb_strtable addtab;
  4649. upb_alloc *alloc = upb_arena_alloc(s->arena);
  4650. upb_filedef *file = upb_malloc(alloc, sizeof(*file));
  4651. bool ok;
  4652. symtab_addctx ctx;
  4653. ctx.file = file;
  4654. ctx.symtab = s;
  4655. ctx.alloc = alloc;
  4656. ctx.tmp = upb_arena_alloc(tmparena);
  4657. ctx.addtab = &addtab;
  4658. ctx.layouts = layouts;
  4659. ctx.status = status;
  4660. ok = file && upb_strtable_init2(&addtab, UPB_CTYPE_CONSTPTR, ctx.tmp) &&
  4661. build_filedef(&ctx, file, file_proto) && upb_symtab_addtotabs(s, &ctx);
  4662. upb_arena_free(tmparena);
  4663. return ok ? file : NULL;
  4664. }
  4665. const upb_filedef *upb_symtab_addfile(
  4666. upb_symtab *s, const google_protobuf_FileDescriptorProto *file_proto,
  4667. upb_status *status) {
  4668. return _upb_symtab_addfile(s, file_proto, NULL, status);
  4669. }
  4670. /* Include here since we want most of this file to be stdio-free. */
  4671. #include <stdio.h>
  4672. bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init) {
  4673. /* Since this function should never fail (it would indicate a bug in upb) we
  4674. * print errors to stderr instead of returning error status to the user. */
  4675. upb_def_init **deps = init->deps;
  4676. google_protobuf_FileDescriptorProto *file;
  4677. upb_arena *arena;
  4678. upb_status status;
  4679. upb_status_clear(&status);
  4680. if (upb_strtable_lookup(&s->files, init->filename, NULL)) {
  4681. return true;
  4682. }
  4683. arena = upb_arena_new();
  4684. for (; *deps; deps++) {
  4685. if (!_upb_symtab_loaddefinit(s, *deps)) goto err;
  4686. }
  4687. file = google_protobuf_FileDescriptorProto_parse(
  4688. init->descriptor.data, init->descriptor.size, arena);
  4689. if (!file) {
  4690. upb_status_seterrf(
  4691. &status,
  4692. "Failed to parse compiled-in descriptor for file '%s'. This should "
  4693. "never happen.",
  4694. init->filename);
  4695. goto err;
  4696. }
  4697. if (!_upb_symtab_addfile(s, file, init->layouts, &status)) goto err;
  4698. upb_arena_free(arena);
  4699. return true;
  4700. err:
  4701. fprintf(stderr, "Error loading compiled-in descriptor: %s\n",
  4702. upb_status_errmsg(&status));
  4703. upb_arena_free(arena);
  4704. return false;
  4705. }
  4706. #undef CHK
  4707. #undef CHK_OOM
  4708. #include <string.h>
  4709. static size_t get_field_size(const upb_msglayout_field *f) {
  4710. static unsigned char sizes[] = {
  4711. 0,/* 0 */
  4712. 8, /* UPB_DESCRIPTOR_TYPE_DOUBLE */
  4713. 4, /* UPB_DESCRIPTOR_TYPE_FLOAT */
  4714. 8, /* UPB_DESCRIPTOR_TYPE_INT64 */
  4715. 8, /* UPB_DESCRIPTOR_TYPE_UINT64 */
  4716. 4, /* UPB_DESCRIPTOR_TYPE_INT32 */
  4717. 8, /* UPB_DESCRIPTOR_TYPE_FIXED64 */
  4718. 4, /* UPB_DESCRIPTOR_TYPE_FIXED32 */
  4719. 1, /* UPB_DESCRIPTOR_TYPE_BOOL */
  4720. sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_STRING */
  4721. sizeof(void*), /* UPB_DESCRIPTOR_TYPE_GROUP */
  4722. sizeof(void*), /* UPB_DESCRIPTOR_TYPE_MESSAGE */
  4723. sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_BYTES */
  4724. 4, /* UPB_DESCRIPTOR_TYPE_UINT32 */
  4725. 4, /* UPB_DESCRIPTOR_TYPE_ENUM */
  4726. 4, /* UPB_DESCRIPTOR_TYPE_SFIXED32 */
  4727. 8, /* UPB_DESCRIPTOR_TYPE_SFIXED64 */
  4728. 4, /* UPB_DESCRIPTOR_TYPE_SINT32 */
  4729. 8, /* UPB_DESCRIPTOR_TYPE_SINT64 */
  4730. };
  4731. return _upb_repeated_or_map(f) ? sizeof(void *) : sizes[f->descriptortype];
  4732. }
  4733. /* Strings/bytes are special-cased in maps. */
  4734. static char _upb_fieldtype_to_mapsize[12] = {
  4735. 0,
  4736. 1, /* UPB_TYPE_BOOL */
  4737. 4, /* UPB_TYPE_FLOAT */
  4738. 4, /* UPB_TYPE_INT32 */
  4739. 4, /* UPB_TYPE_UINT32 */
  4740. 4, /* UPB_TYPE_ENUM */
  4741. sizeof(void*), /* UPB_TYPE_MESSAGE */
  4742. 8, /* UPB_TYPE_DOUBLE */
  4743. 8, /* UPB_TYPE_INT64 */
  4744. 8, /* UPB_TYPE_UINT64 */
  4745. 0, /* UPB_TYPE_STRING */
  4746. 0, /* UPB_TYPE_BYTES */
  4747. };
  4748. /** upb_msg *******************************************************************/
  4749. upb_msg *upb_msg_new(const upb_msgdef *m, upb_arena *a) {
  4750. return _upb_msg_new(upb_msgdef_layout(m), a);
  4751. }
  4752. static bool in_oneof(const upb_msglayout_field *field) {
  4753. return field->presence < 0;
  4754. }
  4755. static upb_msgval _upb_msg_getraw(const upb_msg *msg, const upb_fielddef *f) {
  4756. const upb_msglayout_field *field = upb_fielddef_layout(f);
  4757. const char *mem = UPB_PTR_AT(msg, field->offset, char);
  4758. upb_msgval val = {0};
  4759. memcpy(&val, mem, get_field_size(field));
  4760. return val;
  4761. }
  4762. bool upb_msg_has(const upb_msg *msg, const upb_fielddef *f) {
  4763. const upb_msglayout_field *field = upb_fielddef_layout(f);
  4764. if (in_oneof(field)) {
  4765. return _upb_getoneofcase_field(msg, field) == field->number;
  4766. } else if (field->presence > 0) {
  4767. return _upb_hasbit_field(msg, field);
  4768. } else {
  4769. UPB_ASSERT(field->descriptortype == UPB_DESCRIPTOR_TYPE_MESSAGE ||
  4770. field->descriptortype == UPB_DESCRIPTOR_TYPE_GROUP);
  4771. return _upb_msg_getraw(msg, f).msg_val != NULL;
  4772. }
  4773. }
  4774. const upb_fielddef *upb_msg_whichoneof(const upb_msg *msg,
  4775. const upb_oneofdef *o) {
  4776. upb_oneof_iter i;
  4777. const upb_fielddef *f;
  4778. const upb_msglayout_field *field;
  4779. const upb_msgdef *m = upb_oneofdef_containingtype(o);
  4780. uint32_t oneof_case;
  4781. /* This is far from optimal. */
  4782. upb_oneof_begin(&i, o);
  4783. if (upb_oneof_done(&i)) return false;
  4784. f = upb_oneof_iter_field(&i);
  4785. field = upb_fielddef_layout(f);
  4786. oneof_case = _upb_getoneofcase_field(msg, field);
  4787. return oneof_case ? upb_msgdef_itof(m, oneof_case) : NULL;
  4788. }
  4789. upb_msgval upb_msg_get(const upb_msg *msg, const upb_fielddef *f) {
  4790. if (!upb_fielddef_haspresence(f) || upb_msg_has(msg, f)) {
  4791. return _upb_msg_getraw(msg, f);
  4792. } else {
  4793. /* TODO(haberman): change upb_fielddef to not require this switch(). */
  4794. upb_msgval val = {0};
  4795. switch (upb_fielddef_type(f)) {
  4796. case UPB_TYPE_INT32:
  4797. case UPB_TYPE_ENUM:
  4798. val.int32_val = upb_fielddef_defaultint32(f);
  4799. break;
  4800. case UPB_TYPE_INT64:
  4801. val.int64_val = upb_fielddef_defaultint64(f);
  4802. break;
  4803. case UPB_TYPE_UINT32:
  4804. val.uint32_val = upb_fielddef_defaultuint32(f);
  4805. break;
  4806. case UPB_TYPE_UINT64:
  4807. val.uint64_val = upb_fielddef_defaultuint64(f);
  4808. break;
  4809. case UPB_TYPE_FLOAT:
  4810. val.float_val = upb_fielddef_defaultfloat(f);
  4811. break;
  4812. case UPB_TYPE_DOUBLE:
  4813. val.double_val = upb_fielddef_defaultdouble(f);
  4814. break;
  4815. case UPB_TYPE_BOOL:
  4816. val.double_val = upb_fielddef_defaultbool(f);
  4817. break;
  4818. case UPB_TYPE_STRING:
  4819. case UPB_TYPE_BYTES:
  4820. val.str_val.data = upb_fielddef_defaultstr(f, &val.str_val.size);
  4821. break;
  4822. case UPB_TYPE_MESSAGE:
  4823. val.msg_val = NULL;
  4824. break;
  4825. }
  4826. return val;
  4827. }
  4828. }
  4829. upb_mutmsgval upb_msg_mutable(upb_msg *msg, const upb_fielddef *f,
  4830. upb_arena *a) {
  4831. const upb_msglayout_field *field = upb_fielddef_layout(f);
  4832. upb_mutmsgval ret;
  4833. char *mem = UPB_PTR_AT(msg, field->offset, char);
  4834. bool wrong_oneof =
  4835. in_oneof(field) && _upb_getoneofcase_field(msg, field) != field->number;
  4836. memcpy(&ret, mem, sizeof(void*));
  4837. if (a && (!ret.msg || wrong_oneof)) {
  4838. if (upb_fielddef_ismap(f)) {
  4839. const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
  4840. const upb_fielddef *key = upb_msgdef_itof(entry, UPB_MAPENTRY_KEY);
  4841. const upb_fielddef *value = upb_msgdef_itof(entry, UPB_MAPENTRY_VALUE);
  4842. ret.map = upb_map_new(a, upb_fielddef_type(key), upb_fielddef_type(value));
  4843. } else if (upb_fielddef_isseq(f)) {
  4844. ret.array = upb_array_new(a, upb_fielddef_type(f));
  4845. } else {
  4846. UPB_ASSERT(upb_fielddef_issubmsg(f));
  4847. ret.msg = upb_msg_new(upb_fielddef_msgsubdef(f), a);
  4848. }
  4849. memcpy(mem, &ret, sizeof(void*));
  4850. if (wrong_oneof) {
  4851. *_upb_oneofcase_field(msg, field) = field->number;
  4852. } else if (field->presence > 0) {
  4853. _upb_sethas_field(msg, field);
  4854. }
  4855. }
  4856. return ret;
  4857. }
  4858. void upb_msg_set(upb_msg *msg, const upb_fielddef *f, upb_msgval val,
  4859. upb_arena *a) {
  4860. const upb_msglayout_field *field = upb_fielddef_layout(f);
  4861. char *mem = UPB_PTR_AT(msg, field->offset, char);
  4862. UPB_UNUSED(a); /* We reserve the right to make set insert into a map. */
  4863. memcpy(mem, &val, get_field_size(field));
  4864. if (field->presence > 0) {
  4865. _upb_sethas_field(msg, field);
  4866. } else if (in_oneof(field)) {
  4867. *_upb_oneofcase_field(msg, field) = field->number;
  4868. }
  4869. }
  4870. void upb_msg_clearfield(upb_msg *msg, const upb_fielddef *f) {
  4871. const upb_msglayout_field *field = upb_fielddef_layout(f);
  4872. char *mem = UPB_PTR_AT(msg, field->offset, char);
  4873. if (field->presence > 0) {
  4874. _upb_clearhas_field(msg, field);
  4875. } else if (in_oneof(field)) {
  4876. uint32_t *oneof_case = _upb_oneofcase_field(msg, field);
  4877. if (*oneof_case != field->number) return;
  4878. *oneof_case = 0;
  4879. }
  4880. memset(mem, 0, get_field_size(field));
  4881. }
  4882. void upb_msg_clear(upb_msg *msg, const upb_msgdef *m) {
  4883. _upb_msg_clear(msg, upb_msgdef_layout(m));
  4884. }
  4885. bool upb_msg_next(const upb_msg *msg, const upb_msgdef *m,
  4886. const upb_symtab *ext_pool, const upb_fielddef **out_f,
  4887. upb_msgval *out_val, size_t *iter) {
  4888. size_t i = *iter;
  4889. const upb_msgval zero = {0};
  4890. const upb_fielddef *f;
  4891. UPB_UNUSED(ext_pool);
  4892. while ((f = _upb_msgdef_field(m, (int)++i)) != NULL) {
  4893. upb_msgval val = _upb_msg_getraw(msg, f);
  4894. /* Skip field if unset or empty. */
  4895. if (upb_fielddef_haspresence(f)) {
  4896. if (!upb_msg_has(msg, f)) continue;
  4897. } else {
  4898. upb_msgval test = val;
  4899. if (upb_fielddef_isstring(f) && !upb_fielddef_isseq(f)) {
  4900. /* Clear string pointer, only size matters (ptr could be non-NULL). */
  4901. test.str_val.data = NULL;
  4902. }
  4903. /* Continue if NULL or 0. */
  4904. if (memcmp(&test, &zero, sizeof(test)) == 0) continue;
  4905. /* Continue on empty array or map. */
  4906. if (upb_fielddef_ismap(f)) {
  4907. if (upb_map_size(test.map_val) == 0) continue;
  4908. } else if (upb_fielddef_isseq(f)) {
  4909. if (upb_array_size(test.array_val) == 0) continue;
  4910. }
  4911. }
  4912. *out_val = val;
  4913. *out_f = f;
  4914. *iter = i;
  4915. return true;
  4916. }
  4917. *iter = i;
  4918. return false;
  4919. }
  4920. bool _upb_msg_discardunknown(upb_msg *msg, const upb_msgdef *m, int depth) {
  4921. size_t iter = UPB_MSG_BEGIN;
  4922. const upb_fielddef *f;
  4923. upb_msgval val;
  4924. bool ret = true;
  4925. if (--depth == 0) return false;
  4926. _upb_msg_discardunknown_shallow(msg);
  4927. while (upb_msg_next(msg, m, NULL /*ext_pool*/, &f, &val, &iter)) {
  4928. const upb_msgdef *subm = upb_fielddef_msgsubdef(f);
  4929. if (!subm) continue;
  4930. if (upb_fielddef_ismap(f)) {
  4931. const upb_fielddef *val_f = upb_msgdef_itof(subm, 2);
  4932. const upb_msgdef *val_m = upb_fielddef_msgsubdef(val_f);
  4933. upb_map *map = (upb_map*)val.map_val;
  4934. size_t iter = UPB_MAP_BEGIN;
  4935. if (!val_m) continue;
  4936. while (upb_mapiter_next(map, &iter)) {
  4937. upb_msgval map_val = upb_mapiter_value(map, iter);
  4938. if (!_upb_msg_discardunknown((upb_msg*)map_val.msg_val, val_m, depth)) {
  4939. ret = false;
  4940. }
  4941. }
  4942. } else if (upb_fielddef_isseq(f)) {
  4943. const upb_array *arr = val.array_val;
  4944. size_t i, n = upb_array_size(arr);
  4945. for (i = 0; i < n; i++) {
  4946. upb_msgval elem = upb_array_get(arr, i);
  4947. if (!_upb_msg_discardunknown((upb_msg*)elem.msg_val, subm, depth)) {
  4948. ret = false;
  4949. }
  4950. }
  4951. } else {
  4952. if (!_upb_msg_discardunknown((upb_msg*)val.msg_val, subm, depth)) {
  4953. ret = false;
  4954. }
  4955. }
  4956. }
  4957. return ret;
  4958. }
  4959. bool upb_msg_discardunknown(upb_msg *msg, const upb_msgdef *m, int maxdepth) {
  4960. return _upb_msg_discardunknown(msg, m, maxdepth);
  4961. }
  4962. /** upb_array *****************************************************************/
  4963. upb_array *upb_array_new(upb_arena *a, upb_fieldtype_t type) {
  4964. return _upb_array_new(a, type);
  4965. }
  4966. size_t upb_array_size(const upb_array *arr) {
  4967. return arr->len;
  4968. }
  4969. upb_msgval upb_array_get(const upb_array *arr, size_t i) {
  4970. upb_msgval ret;
  4971. const char* data = _upb_array_constptr(arr);
  4972. int lg2 = arr->data & 7;
  4973. UPB_ASSERT(i < arr->len);
  4974. memcpy(&ret, data + (i << lg2), 1 << lg2);
  4975. return ret;
  4976. }
  4977. void upb_array_set(upb_array *arr, size_t i, upb_msgval val) {
  4978. char* data = _upb_array_ptr(arr);
  4979. int lg2 = arr->data & 7;
  4980. UPB_ASSERT(i < arr->len);
  4981. memcpy(data + (i << lg2), &val, 1 << lg2);
  4982. }
  4983. bool upb_array_append(upb_array *arr, upb_msgval val, upb_arena *arena) {
  4984. if (!_upb_array_realloc(arr, arr->len + 1, arena)) {
  4985. return false;
  4986. }
  4987. arr->len++;
  4988. upb_array_set(arr, arr->len - 1, val);
  4989. return true;
  4990. }
  4991. bool upb_array_resize(upb_array *arr, size_t size, upb_arena *arena) {
  4992. return _upb_array_resize(arr, size, arena);
  4993. }
  4994. /** upb_map *******************************************************************/
  4995. upb_map *upb_map_new(upb_arena *a, upb_fieldtype_t key_type,
  4996. upb_fieldtype_t value_type) {
  4997. return _upb_map_new(a, _upb_fieldtype_to_mapsize[key_type],
  4998. _upb_fieldtype_to_mapsize[value_type]);
  4999. }
  5000. size_t upb_map_size(const upb_map *map) {
  5001. return _upb_map_size(map);
  5002. }
  5003. bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val) {
  5004. return _upb_map_get(map, &key, map->key_size, val, map->val_size);
  5005. }
  5006. bool upb_map_set(upb_map *map, upb_msgval key, upb_msgval val,
  5007. upb_arena *arena) {
  5008. return _upb_map_set(map, &key, map->key_size, &val, map->val_size, arena);
  5009. }
  5010. bool upb_map_delete(upb_map *map, upb_msgval key) {
  5011. return _upb_map_delete(map, &key, map->key_size);
  5012. }
  5013. bool upb_mapiter_next(const upb_map *map, size_t *iter) {
  5014. return _upb_map_next(map, iter);
  5015. }
  5016. bool upb_mapiter_done(const upb_map *map, size_t iter) {
  5017. upb_strtable_iter i;
  5018. UPB_ASSERT(iter != UPB_MAP_BEGIN);
  5019. i.t = &map->table;
  5020. i.index = iter;
  5021. return upb_strtable_done(&i);
  5022. }
  5023. /* Returns the key and value for this entry of the map. */
  5024. upb_msgval upb_mapiter_key(const upb_map *map, size_t iter) {
  5025. upb_strtable_iter i;
  5026. upb_msgval ret;
  5027. i.t = &map->table;
  5028. i.index = iter;
  5029. _upb_map_fromkey(upb_strtable_iter_key(&i), &ret, map->key_size);
  5030. return ret;
  5031. }
  5032. upb_msgval upb_mapiter_value(const upb_map *map, size_t iter) {
  5033. upb_strtable_iter i;
  5034. upb_msgval ret;
  5035. i.t = &map->table;
  5036. i.index = iter;
  5037. _upb_map_fromvalue(upb_strtable_iter_value(&i), &ret, map->val_size);
  5038. return ret;
  5039. }
  5040. /* void upb_mapiter_setvalue(upb_map *map, size_t iter, upb_msgval value); */
  5041. #ifdef UPB_MSVC_VSNPRINTF
  5042. /* Visual C++ earlier than 2015 doesn't have standard C99 snprintf and
  5043. * vsnprintf. To support them, missing functions are manually implemented
  5044. * using the existing secure functions. */
  5045. int msvc_vsnprintf(char* s, size_t n, const char* format, va_list arg) {
  5046. if (!s) {
  5047. return _vscprintf(format, arg);
  5048. }
  5049. int ret = _vsnprintf_s(s, n, _TRUNCATE, format, arg);
  5050. if (ret < 0) {
  5051. ret = _vscprintf(format, arg);
  5052. }
  5053. return ret;
  5054. }
  5055. int msvc_snprintf(char* s, size_t n, const char* format, ...) {
  5056. va_list arg;
  5057. va_start(arg, format);
  5058. int ret = msvc_vsnprintf(s, n, format, arg);
  5059. va_end(arg);
  5060. return ret;
  5061. }
  5062. #endif
  5063. #include <errno.h>
  5064. #include <float.h>
  5065. #include <inttypes.h>
  5066. #include <limits.h>
  5067. #include <setjmp.h>
  5068. #include <stdlib.h>
  5069. #include <string.h>
  5070. /* Special header, must be included last. */
  5071. typedef struct {
  5072. const char *ptr, *end;
  5073. upb_arena *arena; /* TODO: should we have a tmp arena for tmp data? */
  5074. const upb_symtab *any_pool;
  5075. int depth;
  5076. upb_status *status;
  5077. jmp_buf err;
  5078. int line;
  5079. const char *line_begin;
  5080. bool is_first;
  5081. int options;
  5082. const upb_fielddef *debug_field;
  5083. } jsondec;
  5084. enum { JD_OBJECT, JD_ARRAY, JD_STRING, JD_NUMBER, JD_TRUE, JD_FALSE, JD_NULL };
  5085. /* Forward declarations of mutually-recursive functions. */
  5086. static void jsondec_wellknown(jsondec *d, upb_msg *msg, const upb_msgdef *m);
  5087. static upb_msgval jsondec_value(jsondec *d, const upb_fielddef *f);
  5088. static void jsondec_wellknownvalue(jsondec *d, upb_msg *msg,
  5089. const upb_msgdef *m);
  5090. static void jsondec_object(jsondec *d, upb_msg *msg, const upb_msgdef *m);
  5091. static bool jsondec_streql(upb_strview str, const char *lit) {
  5092. return str.size == strlen(lit) && memcmp(str.data, lit, str.size) == 0;
  5093. }
  5094. UPB_NORETURN static void jsondec_err(jsondec *d, const char *msg) {
  5095. upb_status_seterrf(d->status, "Error parsing JSON @%d:%d: %s", d->line,
  5096. (int)(d->ptr - d->line_begin), msg);
  5097. longjmp(d->err, 1);
  5098. }
  5099. UPB_NORETURN static void jsondec_errf(jsondec *d, const char *fmt, ...) {
  5100. va_list argp;
  5101. upb_status_seterrf(d->status, "Error parsing JSON @%d:%d: ", d->line,
  5102. (int)(d->ptr - d->line_begin));
  5103. va_start(argp, fmt);
  5104. upb_status_vappenderrf(d->status, fmt, argp);
  5105. va_end(argp);
  5106. longjmp(d->err, 1);
  5107. }
  5108. static void jsondec_skipws(jsondec *d) {
  5109. while (d->ptr != d->end) {
  5110. switch (*d->ptr) {
  5111. case '\n':
  5112. d->line++;
  5113. d->line_begin = d->ptr;
  5114. /* Fallthrough. */
  5115. case '\r':
  5116. case '\t':
  5117. case ' ':
  5118. d->ptr++;
  5119. break;
  5120. default:
  5121. return;
  5122. }
  5123. }
  5124. jsondec_err(d, "Unexpected EOF");
  5125. }
  5126. static bool jsondec_tryparsech(jsondec *d, char ch) {
  5127. if (d->ptr == d->end || *d->ptr != ch) return false;
  5128. d->ptr++;
  5129. return true;
  5130. }
  5131. static void jsondec_parselit(jsondec *d, const char *lit) {
  5132. size_t avail = d->end - d->ptr;
  5133. size_t len = strlen(lit);
  5134. if (avail < len || memcmp(d->ptr, lit, len) != 0) {
  5135. jsondec_errf(d, "Expected: '%s'", lit);
  5136. }
  5137. d->ptr += len;
  5138. }
  5139. static void jsondec_wsch(jsondec *d, char ch) {
  5140. jsondec_skipws(d);
  5141. if (!jsondec_tryparsech(d, ch)) {
  5142. jsondec_errf(d, "Expected: '%c'", ch);
  5143. }
  5144. }
  5145. static void jsondec_true(jsondec *d) { jsondec_parselit(d, "true"); }
  5146. static void jsondec_false(jsondec *d) { jsondec_parselit(d, "false"); }
  5147. static void jsondec_null(jsondec *d) { jsondec_parselit(d, "null"); }
  5148. static void jsondec_entrysep(jsondec *d) {
  5149. jsondec_skipws(d);
  5150. jsondec_parselit(d, ":");
  5151. }
  5152. static int jsondec_rawpeek(jsondec *d) {
  5153. switch (*d->ptr) {
  5154. case '{':
  5155. return JD_OBJECT;
  5156. case '[':
  5157. return JD_ARRAY;
  5158. case '"':
  5159. return JD_STRING;
  5160. case '-':
  5161. case '0':
  5162. case '1':
  5163. case '2':
  5164. case '3':
  5165. case '4':
  5166. case '5':
  5167. case '6':
  5168. case '7':
  5169. case '8':
  5170. case '9':
  5171. return JD_NUMBER;
  5172. case 't':
  5173. return JD_TRUE;
  5174. case 'f':
  5175. return JD_FALSE;
  5176. case 'n':
  5177. return JD_NULL;
  5178. default:
  5179. jsondec_errf(d, "Unexpected character: '%c'", *d->ptr);
  5180. }
  5181. }
  5182. /* JSON object/array **********************************************************/
  5183. /* These are used like so:
  5184. *
  5185. * jsondec_objstart(d);
  5186. * while (jsondec_objnext(d)) {
  5187. * ...
  5188. * }
  5189. * jsondec_objend(d) */
  5190. static int jsondec_peek(jsondec *d) {
  5191. jsondec_skipws(d);
  5192. return jsondec_rawpeek(d);
  5193. }
  5194. static void jsondec_push(jsondec *d) {
  5195. if (--d->depth < 0) {
  5196. jsondec_err(d, "Recursion limit exceeded");
  5197. }
  5198. d->is_first = true;
  5199. }
  5200. static bool jsondec_seqnext(jsondec *d, char end_ch) {
  5201. bool is_first = d->is_first;
  5202. d->is_first = false;
  5203. jsondec_skipws(d);
  5204. if (*d->ptr == end_ch) return false;
  5205. if (!is_first) jsondec_parselit(d, ",");
  5206. return true;
  5207. }
  5208. static void jsondec_arrstart(jsondec *d) {
  5209. jsondec_push(d);
  5210. jsondec_wsch(d, '[');
  5211. }
  5212. static void jsondec_arrend(jsondec *d) {
  5213. d->depth++;
  5214. jsondec_wsch(d, ']');
  5215. }
  5216. static bool jsondec_arrnext(jsondec *d) {
  5217. return jsondec_seqnext(d, ']');
  5218. }
  5219. static void jsondec_objstart(jsondec *d) {
  5220. jsondec_push(d);
  5221. jsondec_wsch(d, '{');
  5222. }
  5223. static void jsondec_objend(jsondec *d) {
  5224. d->depth++;
  5225. jsondec_wsch(d, '}');
  5226. }
  5227. static bool jsondec_objnext(jsondec *d) {
  5228. if (!jsondec_seqnext(d, '}')) return false;
  5229. if (jsondec_peek(d) != JD_STRING) {
  5230. jsondec_err(d, "Object must start with string");
  5231. }
  5232. return true;
  5233. }
  5234. /* JSON number ****************************************************************/
  5235. static bool jsondec_tryskipdigits(jsondec *d) {
  5236. const char *start = d->ptr;
  5237. while (d->ptr < d->end) {
  5238. if (*d->ptr < '0' || *d->ptr > '9') {
  5239. break;
  5240. }
  5241. d->ptr++;
  5242. }
  5243. return d->ptr != start;
  5244. }
  5245. static void jsondec_skipdigits(jsondec *d) {
  5246. if (!jsondec_tryskipdigits(d)) {
  5247. jsondec_err(d, "Expected one or more digits");
  5248. }
  5249. }
  5250. static double jsondec_number(jsondec *d) {
  5251. const char *start = d->ptr;
  5252. assert(jsondec_rawpeek(d) == JD_NUMBER);
  5253. /* Skip over the syntax of a number, as specified by JSON. */
  5254. if (*d->ptr == '-') d->ptr++;
  5255. if (jsondec_tryparsech(d, '0')) {
  5256. if (jsondec_tryskipdigits(d)) {
  5257. jsondec_err(d, "number cannot have leading zero");
  5258. }
  5259. } else {
  5260. jsondec_skipdigits(d);
  5261. }
  5262. if (d->ptr == d->end) goto parse;
  5263. if (jsondec_tryparsech(d, '.')) {
  5264. jsondec_skipdigits(d);
  5265. }
  5266. if (d->ptr == d->end) goto parse;
  5267. if (*d->ptr == 'e' || *d->ptr == 'E') {
  5268. d->ptr++;
  5269. if (d->ptr == d->end) {
  5270. jsondec_err(d, "Unexpected EOF in number");
  5271. }
  5272. if (*d->ptr == '+' || *d->ptr == '-') {
  5273. d->ptr++;
  5274. }
  5275. jsondec_skipdigits(d);
  5276. }
  5277. parse:
  5278. /* Having verified the syntax of a JSON number, use strtod() to parse
  5279. * (strtod() accepts a superset of JSON syntax). */
  5280. errno = 0;
  5281. {
  5282. char* end;
  5283. double val = strtod(start, &end);
  5284. assert(end == d->ptr);
  5285. /* Currently the min/max-val conformance tests fail if we check this. Does
  5286. * this mean the conformance tests are wrong or strtod() is wrong, or
  5287. * something else? Investigate further. */
  5288. /*
  5289. if (errno == ERANGE) {
  5290. jsondec_err(d, "Number out of range");
  5291. }
  5292. */
  5293. if (val > DBL_MAX || val < -DBL_MAX) {
  5294. jsondec_err(d, "Number out of range");
  5295. }
  5296. return val;
  5297. }
  5298. }
  5299. /* JSON string ****************************************************************/
  5300. static char jsondec_escape(jsondec *d) {
  5301. switch (*d->ptr++) {
  5302. case '"':
  5303. return '\"';
  5304. case '\\':
  5305. return '\\';
  5306. case '/':
  5307. return '/';
  5308. case 'b':
  5309. return '\b';
  5310. case 'f':
  5311. return '\f';
  5312. case 'n':
  5313. return '\n';
  5314. case 'r':
  5315. return '\r';
  5316. case 't':
  5317. return '\t';
  5318. default:
  5319. jsondec_err(d, "Invalid escape char");
  5320. }
  5321. }
  5322. static uint32_t jsondec_codepoint(jsondec *d) {
  5323. uint32_t cp = 0;
  5324. const char *end;
  5325. if (d->end - d->ptr < 4) {
  5326. jsondec_err(d, "EOF inside string");
  5327. }
  5328. end = d->ptr + 4;
  5329. while (d->ptr < end) {
  5330. char ch = *d->ptr++;
  5331. if (ch >= '0' && ch <= '9') {
  5332. ch -= '0';
  5333. } else if (ch >= 'a' && ch <= 'f') {
  5334. ch = ch - 'a' + 10;
  5335. } else if (ch >= 'A' && ch <= 'F') {
  5336. ch = ch - 'A' + 10;
  5337. } else {
  5338. jsondec_err(d, "Invalid hex digit");
  5339. }
  5340. cp = (cp << 4) | ch;
  5341. }
  5342. return cp;
  5343. }
  5344. /* Parses a \uXXXX unicode escape (possibly a surrogate pair). */
  5345. static size_t jsondec_unicode(jsondec *d, char* out) {
  5346. uint32_t cp = jsondec_codepoint(d);
  5347. if (cp >= 0xd800 && cp <= 0xdbff) {
  5348. /* Surrogate pair: two 16-bit codepoints become a 32-bit codepoint. */
  5349. uint32_t high = cp;
  5350. uint32_t low;
  5351. jsondec_parselit(d, "\\u");
  5352. low = jsondec_codepoint(d);
  5353. if (low < 0xdc00 || low > 0xdfff) {
  5354. jsondec_err(d, "Invalid low surrogate");
  5355. }
  5356. cp = (high & 0x3ff) << 10;
  5357. cp |= (low & 0x3ff);
  5358. cp += 0x10000;
  5359. } else if (cp >= 0xdc00 && cp <= 0xdfff) {
  5360. jsondec_err(d, "Unpaired low surrogate");
  5361. }
  5362. /* Write to UTF-8 */
  5363. if (cp <= 0x7f) {
  5364. out[0] = cp;
  5365. return 1;
  5366. } else if (cp <= 0x07FF) {
  5367. out[0] = ((cp >> 6) & 0x1F) | 0xC0;
  5368. out[1] = ((cp >> 0) & 0x3F) | 0x80;
  5369. return 2;
  5370. } else if (cp <= 0xFFFF) {
  5371. out[0] = ((cp >> 12) & 0x0F) | 0xE0;
  5372. out[1] = ((cp >> 6) & 0x3F) | 0x80;
  5373. out[2] = ((cp >> 0) & 0x3F) | 0x80;
  5374. return 3;
  5375. } else if (cp < 0x10FFFF) {
  5376. out[0] = ((cp >> 18) & 0x07) | 0xF0;
  5377. out[1] = ((cp >> 12) & 0x3f) | 0x80;
  5378. out[2] = ((cp >> 6) & 0x3f) | 0x80;
  5379. out[3] = ((cp >> 0) & 0x3f) | 0x80;
  5380. return 4;
  5381. } else {
  5382. jsondec_err(d, "Invalid codepoint");
  5383. }
  5384. }
  5385. static void jsondec_resize(jsondec *d, char **buf, char **end, char **buf_end) {
  5386. size_t oldsize = *buf_end - *buf;
  5387. size_t len = *end - *buf;
  5388. size_t size = UPB_MAX(8, 2 * oldsize);
  5389. *buf = upb_arena_realloc(d->arena, *buf, len, size);
  5390. *end = *buf + len;
  5391. *buf_end = *buf + size;
  5392. }
  5393. static upb_strview jsondec_string(jsondec *d) {
  5394. char *buf = NULL;
  5395. char *end = NULL;
  5396. char *buf_end = NULL;
  5397. jsondec_skipws(d);
  5398. if (*d->ptr++ != '"') {
  5399. jsondec_err(d, "Expected string");
  5400. }
  5401. while (d->ptr < d->end) {
  5402. char ch = *d->ptr++;
  5403. if (end == buf_end) {
  5404. jsondec_resize(d, &buf, &end, &buf_end);
  5405. }
  5406. switch (ch) {
  5407. case '"': {
  5408. upb_strview ret;
  5409. ret.data = buf;
  5410. ret.size = end - buf;
  5411. return ret;
  5412. }
  5413. case '\\':
  5414. if (d->ptr == d->end) goto eof;
  5415. if (*d->ptr == 'u') {
  5416. d->ptr++;
  5417. if (buf_end - end < 4) {
  5418. /* Allow space for maximum-sized code point (4 bytes). */
  5419. jsondec_resize(d, &buf, &end, &buf_end);
  5420. }
  5421. end += jsondec_unicode(d, end);
  5422. } else {
  5423. *end++ = jsondec_escape(d);
  5424. }
  5425. break;
  5426. default:
  5427. if ((unsigned char)*d->ptr < 0x20) {
  5428. jsondec_err(d, "Invalid char in JSON string");
  5429. }
  5430. *end++ = ch;
  5431. break;
  5432. }
  5433. }
  5434. eof:
  5435. jsondec_err(d, "EOF inside string");
  5436. }
  5437. static void jsondec_skipval(jsondec *d) {
  5438. switch (jsondec_peek(d)) {
  5439. case JD_OBJECT:
  5440. jsondec_objstart(d);
  5441. while (jsondec_objnext(d)) {
  5442. jsondec_string(d);
  5443. jsondec_entrysep(d);
  5444. jsondec_skipval(d);
  5445. }
  5446. jsondec_objend(d);
  5447. break;
  5448. case JD_ARRAY:
  5449. jsondec_arrstart(d);
  5450. while (jsondec_arrnext(d)) {
  5451. jsondec_skipval(d);
  5452. }
  5453. jsondec_arrend(d);
  5454. break;
  5455. case JD_TRUE:
  5456. jsondec_true(d);
  5457. break;
  5458. case JD_FALSE:
  5459. jsondec_false(d);
  5460. break;
  5461. case JD_NULL:
  5462. jsondec_null(d);
  5463. break;
  5464. case JD_STRING:
  5465. jsondec_string(d);
  5466. break;
  5467. case JD_NUMBER:
  5468. jsondec_number(d);
  5469. break;
  5470. }
  5471. }
  5472. /* Base64 decoding for bytes fields. ******************************************/
  5473. static unsigned int jsondec_base64_tablelookup(const char ch) {
  5474. /* Table includes the normal base64 chars plus the URL-safe variant. */
  5475. const signed char table[256] = {
  5476. -1, -1, -1, -1, -1, -1, -1,
  5477. -1, -1, -1, -1, -1, -1, -1,
  5478. -1, -1, -1, -1, -1, -1, -1,
  5479. -1, -1, -1, -1, -1, -1, -1,
  5480. -1, -1, -1, -1, -1, -1, -1,
  5481. -1, -1, -1, -1, -1, -1, -1,
  5482. -1, 62 /*+*/, -1, 62 /*-*/, -1, 63 /*/ */, 52 /*0*/,
  5483. 53 /*1*/, 54 /*2*/, 55 /*3*/, 56 /*4*/, 57 /*5*/, 58 /*6*/, 59 /*7*/,
  5484. 60 /*8*/, 61 /*9*/, -1, -1, -1, -1, -1,
  5485. -1, -1, 0 /*A*/, 1 /*B*/, 2 /*C*/, 3 /*D*/, 4 /*E*/,
  5486. 5 /*F*/, 6 /*G*/, 07 /*H*/, 8 /*I*/, 9 /*J*/, 10 /*K*/, 11 /*L*/,
  5487. 12 /*M*/, 13 /*N*/, 14 /*O*/, 15 /*P*/, 16 /*Q*/, 17 /*R*/, 18 /*S*/,
  5488. 19 /*T*/, 20 /*U*/, 21 /*V*/, 22 /*W*/, 23 /*X*/, 24 /*Y*/, 25 /*Z*/,
  5489. -1, -1, -1, -1, 63 /*_*/, -1, 26 /*a*/,
  5490. 27 /*b*/, 28 /*c*/, 29 /*d*/, 30 /*e*/, 31 /*f*/, 32 /*g*/, 33 /*h*/,
  5491. 34 /*i*/, 35 /*j*/, 36 /*k*/, 37 /*l*/, 38 /*m*/, 39 /*n*/, 40 /*o*/,
  5492. 41 /*p*/, 42 /*q*/, 43 /*r*/, 44 /*s*/, 45 /*t*/, 46 /*u*/, 47 /*v*/,
  5493. 48 /*w*/, 49 /*x*/, 50 /*y*/, 51 /*z*/, -1, -1, -1,
  5494. -1, -1, -1, -1, -1, -1, -1,
  5495. -1, -1, -1, -1, -1, -1, -1,
  5496. -1, -1, -1, -1, -1, -1, -1,
  5497. -1, -1, -1, -1, -1, -1, -1,
  5498. -1, -1, -1, -1, -1, -1, -1,
  5499. -1, -1, -1, -1, -1, -1, -1,
  5500. -1, -1, -1, -1, -1, -1, -1,
  5501. -1, -1, -1, -1, -1, -1, -1,
  5502. -1, -1, -1, -1, -1, -1, -1,
  5503. -1, -1, -1, -1, -1, -1, -1,
  5504. -1, -1, -1, -1, -1, -1, -1,
  5505. -1, -1, -1, -1, -1, -1, -1,
  5506. -1, -1, -1, -1, -1, -1, -1,
  5507. -1, -1, -1, -1, -1, -1, -1,
  5508. -1, -1, -1, -1, -1, -1, -1,
  5509. -1, -1, -1, -1, -1, -1, -1,
  5510. -1, -1, -1, -1, -1, -1, -1,
  5511. -1, -1, -1, -1, -1, -1, -1,
  5512. -1, -1, -1, -1};
  5513. /* Sign-extend return value so high bit will be set on any unexpected char. */
  5514. return table[(unsigned)ch];
  5515. }
  5516. static char *jsondec_partialbase64(jsondec *d, const char *ptr, const char *end,
  5517. char *out) {
  5518. int32_t val = -1;
  5519. switch (end - ptr) {
  5520. case 2:
  5521. val = jsondec_base64_tablelookup(ptr[0]) << 18 |
  5522. jsondec_base64_tablelookup(ptr[1]) << 12;
  5523. out[0] = val >> 16;
  5524. out += 1;
  5525. break;
  5526. case 3:
  5527. val = jsondec_base64_tablelookup(ptr[0]) << 18 |
  5528. jsondec_base64_tablelookup(ptr[1]) << 12 |
  5529. jsondec_base64_tablelookup(ptr[2]) << 6;
  5530. out[0] = val >> 16;
  5531. out[1] = (val >> 8) & 0xff;
  5532. out += 2;
  5533. break;
  5534. }
  5535. if (val < 0) {
  5536. jsondec_err(d, "Corrupt base64");
  5537. }
  5538. return out;
  5539. }
  5540. static size_t jsondec_base64(jsondec *d, upb_strview str) {
  5541. /* We decode in place. This is safe because this is a new buffer (not
  5542. * aliasing the input) and because base64 decoding shrinks 4 bytes into 3. */
  5543. char *out = (char*)str.data;
  5544. const char *ptr = str.data;
  5545. const char *end = ptr + str.size;
  5546. const char *end4 = ptr + (str.size & -4); /* Round down to multiple of 4. */
  5547. for (; ptr < end4; ptr += 4, out += 3) {
  5548. int val = jsondec_base64_tablelookup(ptr[0]) << 18 |
  5549. jsondec_base64_tablelookup(ptr[1]) << 12 |
  5550. jsondec_base64_tablelookup(ptr[2]) << 6 |
  5551. jsondec_base64_tablelookup(ptr[3]) << 0;
  5552. if (val < 0) {
  5553. /* Junk chars or padding. Remove trailing padding, if any. */
  5554. if (end - ptr == 4 && ptr[3] == '=') {
  5555. if (ptr[2] == '=') {
  5556. end -= 2;
  5557. } else {
  5558. end -= 1;
  5559. }
  5560. }
  5561. break;
  5562. }
  5563. out[0] = val >> 16;
  5564. out[1] = (val >> 8) & 0xff;
  5565. out[2] = val & 0xff;
  5566. }
  5567. if (ptr < end) {
  5568. /* Process remaining chars. We do not require padding. */
  5569. out = jsondec_partialbase64(d, ptr, end, out);
  5570. }
  5571. return out - str.data;
  5572. }
  5573. /* Low-level integer parsing **************************************************/
  5574. /* We use these hand-written routines instead of strto[u]l() because the "long
  5575. * long" variants aren't in c89. Also our version allows setting a ptr limit. */
  5576. static const char *jsondec_buftouint64(jsondec *d, const char *ptr,
  5577. const char *end, uint64_t *val) {
  5578. uint64_t u64 = 0;
  5579. while (ptr < end) {
  5580. unsigned ch = *ptr - '0';
  5581. if (ch >= 10) break;
  5582. if (u64 > UINT64_MAX / 10 || u64 * 10 > UINT64_MAX - ch) {
  5583. jsondec_err(d, "Integer overflow");
  5584. }
  5585. u64 *= 10;
  5586. u64 += ch;
  5587. ptr++;
  5588. }
  5589. *val = u64;
  5590. return ptr;
  5591. }
  5592. static const char *jsondec_buftoint64(jsondec *d, const char *ptr,
  5593. const char *end, int64_t *val) {
  5594. bool neg = false;
  5595. uint64_t u64;
  5596. if (ptr != end && *ptr == '-') {
  5597. ptr++;
  5598. neg = true;
  5599. }
  5600. ptr = jsondec_buftouint64(d, ptr, end, &u64);
  5601. if (u64 > (uint64_t)INT64_MAX + neg) {
  5602. jsondec_err(d, "Integer overflow");
  5603. }
  5604. *val = neg ? -u64 : u64;
  5605. return ptr;
  5606. }
  5607. static uint64_t jsondec_strtouint64(jsondec *d, upb_strview str) {
  5608. const char *end = str.data + str.size;
  5609. uint64_t ret;
  5610. if (jsondec_buftouint64(d, str.data, end, &ret) != end) {
  5611. jsondec_err(d, "Non-number characters in quoted integer");
  5612. }
  5613. return ret;
  5614. }
  5615. static int64_t jsondec_strtoint64(jsondec *d, upb_strview str) {
  5616. const char *end = str.data + str.size;
  5617. int64_t ret;
  5618. if (jsondec_buftoint64(d, str.data, end, &ret) != end) {
  5619. jsondec_err(d, "Non-number characters in quoted integer");
  5620. }
  5621. return ret;
  5622. }
  5623. /* Primitive value types ******************************************************/
  5624. /* Parse INT32 or INT64 value. */
  5625. static upb_msgval jsondec_int(jsondec *d, const upb_fielddef *f) {
  5626. upb_msgval val;
  5627. switch (jsondec_peek(d)) {
  5628. case JD_NUMBER: {
  5629. double dbl = jsondec_number(d);
  5630. if (dbl > 9223372036854774784.0 || dbl < -9223372036854775808.0) {
  5631. jsondec_err(d, "JSON number is out of range.");
  5632. }
  5633. val.int64_val = dbl; /* must be guarded, overflow here is UB */
  5634. if (val.int64_val != dbl) {
  5635. jsondec_errf(d, "JSON number was not integral (%d != %" PRId64 ")", dbl,
  5636. val.int64_val);
  5637. }
  5638. break;
  5639. }
  5640. case JD_STRING: {
  5641. upb_strview str = jsondec_string(d);
  5642. val.int64_val = jsondec_strtoint64(d, str);
  5643. break;
  5644. }
  5645. default:
  5646. jsondec_err(d, "Expected number or string");
  5647. }
  5648. if (upb_fielddef_type(f) == UPB_TYPE_INT32) {
  5649. if (val.int64_val > INT32_MAX || val.int64_val < INT32_MIN) {
  5650. jsondec_err(d, "Integer out of range.");
  5651. }
  5652. val.int32_val = (int32_t)val.int64_val;
  5653. }
  5654. return val;
  5655. }
  5656. /* Parse UINT32 or UINT64 value. */
  5657. static upb_msgval jsondec_uint(jsondec *d, const upb_fielddef *f) {
  5658. upb_msgval val;
  5659. switch (jsondec_peek(d)) {
  5660. case JD_NUMBER: {
  5661. double dbl = jsondec_number(d);
  5662. if (dbl > 18446744073709549568.0 || dbl < 0) {
  5663. jsondec_err(d, "JSON number is out of range.");
  5664. }
  5665. val.uint64_val = dbl; /* must be guarded, overflow here is UB */
  5666. if (val.uint64_val != dbl) {
  5667. jsondec_errf(d, "JSON number was not integral (%d != %" PRIu64 ")", dbl,
  5668. val.uint64_val);
  5669. }
  5670. break;
  5671. }
  5672. case JD_STRING: {
  5673. upb_strview str = jsondec_string(d);
  5674. val.uint64_val = jsondec_strtouint64(d, str);
  5675. break;
  5676. }
  5677. default:
  5678. jsondec_err(d, "Expected number or string");
  5679. }
  5680. if (upb_fielddef_type(f) == UPB_TYPE_UINT32) {
  5681. if (val.uint64_val > UINT32_MAX) {
  5682. jsondec_err(d, "Integer out of range.");
  5683. }
  5684. val.uint32_val = (uint32_t)val.uint64_val;
  5685. }
  5686. return val;
  5687. }
  5688. /* Parse DOUBLE or FLOAT value. */
  5689. static upb_msgval jsondec_double(jsondec *d, const upb_fielddef *f) {
  5690. upb_strview str;
  5691. upb_msgval val;
  5692. switch (jsondec_peek(d)) {
  5693. case JD_NUMBER:
  5694. val.double_val = jsondec_number(d);
  5695. break;
  5696. case JD_STRING:
  5697. str = jsondec_string(d);
  5698. if (jsondec_streql(str, "NaN")) {
  5699. val.double_val = 0.0 / 0.0;
  5700. } else if (jsondec_streql(str, "Infinity")) {
  5701. val.double_val = UPB_INFINITY;
  5702. } else if (jsondec_streql(str, "-Infinity")) {
  5703. val.double_val = -UPB_INFINITY;
  5704. } else {
  5705. val.double_val = strtod(str.data, NULL);
  5706. }
  5707. break;
  5708. default:
  5709. jsondec_err(d, "Expected number or string");
  5710. }
  5711. if (upb_fielddef_type(f) == UPB_TYPE_FLOAT) {
  5712. if (val.double_val != UPB_INFINITY && val.double_val != -UPB_INFINITY &&
  5713. (val.double_val > FLT_MAX || val.double_val < -FLT_MAX)) {
  5714. jsondec_err(d, "Float out of range");
  5715. }
  5716. val.float_val = val.double_val;
  5717. }
  5718. return val;
  5719. }
  5720. /* Parse STRING or BYTES value. */
  5721. static upb_msgval jsondec_strfield(jsondec *d, const upb_fielddef *f) {
  5722. upb_msgval val;
  5723. val.str_val = jsondec_string(d);
  5724. if (upb_fielddef_type(f) == UPB_TYPE_BYTES) {
  5725. val.str_val.size = jsondec_base64(d, val.str_val);
  5726. }
  5727. return val;
  5728. }
  5729. static upb_msgval jsondec_enum(jsondec *d, const upb_fielddef *f) {
  5730. if (jsondec_peek(d) == JD_STRING) {
  5731. const upb_enumdef *e = upb_fielddef_enumsubdef(f);
  5732. upb_strview str = jsondec_string(d);
  5733. upb_msgval val;
  5734. if (!upb_enumdef_ntoi(e, str.data, str.size, &val.int32_val)) {
  5735. if (d->options & UPB_JSONDEC_IGNOREUNKNOWN) {
  5736. val.int32_val = 0;
  5737. } else {
  5738. jsondec_errf(d, "Unknown enumerator: '" UPB_STRVIEW_FORMAT "'",
  5739. UPB_STRVIEW_ARGS(str));
  5740. }
  5741. }
  5742. return val;
  5743. } else {
  5744. return jsondec_int(d, f);
  5745. }
  5746. }
  5747. static upb_msgval jsondec_bool(jsondec *d, const upb_fielddef *f) {
  5748. bool is_map_key = upb_fielddef_number(f) == 1 &&
  5749. upb_msgdef_mapentry(upb_fielddef_containingtype(f));
  5750. upb_msgval val;
  5751. if (is_map_key) {
  5752. upb_strview str = jsondec_string(d);
  5753. if (jsondec_streql(str, "true")) {
  5754. val.bool_val = true;
  5755. } else if (jsondec_streql(str, "false")) {
  5756. val.bool_val = false;
  5757. } else {
  5758. jsondec_err(d, "Invalid boolean map key");
  5759. }
  5760. } else {
  5761. switch (jsondec_peek(d)) {
  5762. case JD_TRUE:
  5763. val.bool_val = true;
  5764. jsondec_true(d);
  5765. break;
  5766. case JD_FALSE:
  5767. val.bool_val = false;
  5768. jsondec_false(d);
  5769. break;
  5770. default:
  5771. jsondec_err(d, "Expected true or false");
  5772. }
  5773. }
  5774. return val;
  5775. }
  5776. /* Composite types (array/message/map) ****************************************/
  5777. static void jsondec_array(jsondec *d, upb_msg *msg, const upb_fielddef *f) {
  5778. upb_array *arr = upb_msg_mutable(msg, f, d->arena).array;
  5779. jsondec_arrstart(d);
  5780. while (jsondec_arrnext(d)) {
  5781. upb_msgval elem = jsondec_value(d, f);
  5782. upb_array_append(arr, elem, d->arena);
  5783. }
  5784. jsondec_arrend(d);
  5785. }
  5786. static void jsondec_map(jsondec *d, upb_msg *msg, const upb_fielddef *f) {
  5787. upb_map *map = upb_msg_mutable(msg, f, d->arena).map;
  5788. const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
  5789. const upb_fielddef *key_f = upb_msgdef_itof(entry, 1);
  5790. const upb_fielddef *val_f = upb_msgdef_itof(entry, 2);
  5791. jsondec_objstart(d);
  5792. while (jsondec_objnext(d)) {
  5793. upb_msgval key, val;
  5794. key = jsondec_value(d, key_f);
  5795. jsondec_entrysep(d);
  5796. val = jsondec_value(d, val_f);
  5797. upb_map_set(map, key, val, d->arena);
  5798. }
  5799. jsondec_objend(d);
  5800. }
  5801. static void jsondec_tomsg(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  5802. if (upb_msgdef_wellknowntype(m) == UPB_WELLKNOWN_UNSPECIFIED) {
  5803. jsondec_object(d, msg, m);
  5804. } else {
  5805. jsondec_wellknown(d, msg, m);
  5806. }
  5807. }
  5808. static upb_msgval jsondec_msg(jsondec *d, const upb_fielddef *f) {
  5809. const upb_msgdef *m = upb_fielddef_msgsubdef(f);
  5810. upb_msg *msg = upb_msg_new(m, d->arena);
  5811. upb_msgval val;
  5812. jsondec_tomsg(d, msg, m);
  5813. val.msg_val = msg;
  5814. return val;
  5815. }
  5816. static bool jsondec_isvalue(const upb_fielddef *f) {
  5817. return upb_fielddef_type(f) == UPB_TYPE_MESSAGE &&
  5818. upb_msgdef_wellknowntype(upb_fielddef_msgsubdef(f)) ==
  5819. UPB_WELLKNOWN_VALUE;
  5820. }
  5821. static void jsondec_field(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  5822. upb_strview name;
  5823. const upb_fielddef *f;
  5824. const upb_fielddef *preserved;
  5825. name = jsondec_string(d);
  5826. jsondec_entrysep(d);
  5827. f = upb_msgdef_lookupjsonname(m, name.data, name.size);
  5828. if (!f) {
  5829. if ((d->options & UPB_JSONDEC_IGNOREUNKNOWN) == 0) {
  5830. jsondec_errf(d, "Unknown field: '" UPB_STRVIEW_FORMAT "'",
  5831. UPB_STRVIEW_ARGS(name));
  5832. }
  5833. jsondec_skipval(d);
  5834. return;
  5835. }
  5836. if (upb_fielddef_containingoneof(f) &&
  5837. upb_msg_whichoneof(msg, upb_fielddef_containingoneof(f))) {
  5838. jsondec_err(d, "More than one field for this oneof.");
  5839. }
  5840. if (jsondec_peek(d) == JD_NULL && !jsondec_isvalue(f)) {
  5841. /* JSON "null" indicates a default value, so no need to set anything. */
  5842. jsondec_null(d);
  5843. return;
  5844. }
  5845. preserved = d->debug_field;
  5846. d->debug_field = f;
  5847. if (upb_fielddef_ismap(f)) {
  5848. jsondec_map(d, msg, f);
  5849. } else if (upb_fielddef_isseq(f)) {
  5850. jsondec_array(d, msg, f);
  5851. } else if (upb_fielddef_issubmsg(f)) {
  5852. upb_msg *submsg = upb_msg_mutable(msg, f, d->arena).msg;
  5853. const upb_msgdef *subm = upb_fielddef_msgsubdef(f);
  5854. jsondec_tomsg(d, submsg, subm);
  5855. } else {
  5856. upb_msgval val = jsondec_value(d, f);
  5857. upb_msg_set(msg, f, val, d->arena);
  5858. }
  5859. d->debug_field = preserved;
  5860. }
  5861. static void jsondec_object(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  5862. jsondec_objstart(d);
  5863. while (jsondec_objnext(d)) {
  5864. jsondec_field(d, msg, m);
  5865. }
  5866. jsondec_objend(d);
  5867. }
  5868. static upb_msgval jsondec_value(jsondec *d, const upb_fielddef *f) {
  5869. switch (upb_fielddef_type(f)) {
  5870. case UPB_TYPE_BOOL:
  5871. return jsondec_bool(d, f);
  5872. case UPB_TYPE_FLOAT:
  5873. case UPB_TYPE_DOUBLE:
  5874. return jsondec_double(d, f);
  5875. case UPB_TYPE_UINT32:
  5876. case UPB_TYPE_UINT64:
  5877. return jsondec_uint(d, f);
  5878. case UPB_TYPE_INT32:
  5879. case UPB_TYPE_INT64:
  5880. return jsondec_int(d, f);
  5881. case UPB_TYPE_STRING:
  5882. case UPB_TYPE_BYTES:
  5883. return jsondec_strfield(d, f);
  5884. case UPB_TYPE_ENUM:
  5885. return jsondec_enum(d, f);
  5886. case UPB_TYPE_MESSAGE:
  5887. return jsondec_msg(d, f);
  5888. default:
  5889. UPB_UNREACHABLE();
  5890. }
  5891. }
  5892. /* Well-known types ***********************************************************/
  5893. static int jsondec_tsdigits(jsondec *d, const char **ptr, size_t digits,
  5894. const char *after) {
  5895. uint64_t val;
  5896. const char *p = *ptr;
  5897. const char *end = p + digits;
  5898. size_t after_len = after ? strlen(after) : 0;
  5899. UPB_ASSERT(digits <= 9); /* int can't overflow. */
  5900. if (jsondec_buftouint64(d, p, end, &val) != end ||
  5901. (after_len && memcmp(end, after, after_len) != 0)) {
  5902. jsondec_err(d, "Malformed timestamp");
  5903. }
  5904. UPB_ASSERT(val < INT_MAX);
  5905. *ptr = end + after_len;
  5906. return (int)val;
  5907. }
  5908. static int jsondec_nanos(jsondec *d, const char **ptr, const char *end) {
  5909. uint64_t nanos = 0;
  5910. const char *p = *ptr;
  5911. if (p != end && *p == '.') {
  5912. const char *nano_end = jsondec_buftouint64(d, p + 1, end, &nanos);
  5913. int digits = (int)(nano_end - p - 1);
  5914. int exp_lg10 = 9 - digits;
  5915. if (digits > 9) {
  5916. jsondec_err(d, "Too many digits for partial seconds");
  5917. }
  5918. while (exp_lg10--) nanos *= 10;
  5919. *ptr = nano_end;
  5920. }
  5921. UPB_ASSERT(nanos < INT_MAX);
  5922. return (int)nanos;
  5923. }
  5924. /* jsondec_epochdays(1970, 1, 1) == 1970-01-01 == 0. */
  5925. int jsondec_epochdays(int y, int m, int d) {
  5926. const uint32_t year_base = 4800; /* Before min year, multiple of 400. */
  5927. const uint32_t m_adj = m - 3; /* March-based month. */
  5928. const uint32_t carry = m_adj > (uint32_t)m ? 1 : 0;
  5929. const uint32_t adjust = carry ? 12 : 0;
  5930. const uint32_t y_adj = y + year_base - carry;
  5931. const uint32_t month_days = ((m_adj + adjust) * 62719 + 769) / 2048;
  5932. const uint32_t leap_days = y_adj / 4 - y_adj / 100 + y_adj / 400;
  5933. return y_adj * 365 + leap_days + month_days + (d - 1) - 2472632;
  5934. }
  5935. static int64_t jsondec_unixtime(int y, int m, int d, int h, int min, int s) {
  5936. return (int64_t)jsondec_epochdays(y, m, d) * 86400 + h * 3600 + min * 60 + s;
  5937. }
  5938. static void jsondec_timestamp(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  5939. upb_msgval seconds;
  5940. upb_msgval nanos;
  5941. upb_strview str = jsondec_string(d);
  5942. const char *ptr = str.data;
  5943. const char *end = ptr + str.size;
  5944. if (str.size < 20) goto malformed;
  5945. {
  5946. /* 1972-01-01T01:00:00 */
  5947. int year = jsondec_tsdigits(d, &ptr, 4, "-");
  5948. int mon = jsondec_tsdigits(d, &ptr, 2, "-");
  5949. int day = jsondec_tsdigits(d, &ptr, 2, "T");
  5950. int hour = jsondec_tsdigits(d, &ptr, 2, ":");
  5951. int min = jsondec_tsdigits(d, &ptr, 2, ":");
  5952. int sec = jsondec_tsdigits(d, &ptr, 2, NULL);
  5953. seconds.int64_val = jsondec_unixtime(year, mon, day, hour, min, sec);
  5954. }
  5955. nanos.int32_val = jsondec_nanos(d, &ptr, end);
  5956. {
  5957. /* [+-]08:00 or Z */
  5958. int ofs = 0;
  5959. bool neg = false;
  5960. if (ptr == end) goto malformed;
  5961. switch (*ptr++) {
  5962. case '-':
  5963. neg = true;
  5964. /* fallthrough */
  5965. case '+':
  5966. if ((end - ptr) != 5) goto malformed;
  5967. ofs = jsondec_tsdigits(d, &ptr, 2, ":00");
  5968. ofs *= 60 * 60;
  5969. seconds.int64_val += (neg ? ofs : -ofs);
  5970. break;
  5971. case 'Z':
  5972. if (ptr != end) goto malformed;
  5973. break;
  5974. default:
  5975. goto malformed;
  5976. }
  5977. }
  5978. if (seconds.int64_val < -62135596800) {
  5979. jsondec_err(d, "Timestamp out of range");
  5980. }
  5981. upb_msg_set(msg, upb_msgdef_itof(m, 1), seconds, d->arena);
  5982. upb_msg_set(msg, upb_msgdef_itof(m, 2), nanos, d->arena);
  5983. return;
  5984. malformed:
  5985. jsondec_err(d, "Malformed timestamp");
  5986. }
  5987. static void jsondec_duration(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  5988. upb_msgval seconds;
  5989. upb_msgval nanos;
  5990. upb_strview str = jsondec_string(d);
  5991. const char *ptr = str.data;
  5992. const char *end = ptr + str.size;
  5993. /* "3.000000001s", "3s", etc. */
  5994. ptr = jsondec_buftoint64(d, ptr, end, &seconds.int64_val);
  5995. nanos.int32_val = jsondec_nanos(d, &ptr, end);
  5996. if (end - ptr != 1 || *ptr != 's') {
  5997. jsondec_err(d, "Malformed duration");
  5998. }
  5999. if (seconds.int64_val < -315576000000LL || seconds.int64_val > 315576000000LL) {
  6000. jsondec_err(d, "Duration out of range");
  6001. }
  6002. if (seconds.int64_val < 0) {
  6003. nanos.int32_val = - nanos.int32_val;
  6004. }
  6005. upb_msg_set(msg, upb_msgdef_itof(m, 1), seconds, d->arena);
  6006. upb_msg_set(msg, upb_msgdef_itof(m, 2), nanos, d->arena);
  6007. }
  6008. static void jsondec_listvalue(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6009. const upb_fielddef *values_f = upb_msgdef_itof(m, 1);
  6010. const upb_msgdef *value_m = upb_fielddef_msgsubdef(values_f);
  6011. upb_array *values = upb_msg_mutable(msg, values_f, d->arena).array;
  6012. jsondec_arrstart(d);
  6013. while (jsondec_arrnext(d)) {
  6014. upb_msg *value_msg = upb_msg_new(value_m, d->arena);
  6015. upb_msgval value;
  6016. value.msg_val = value_msg;
  6017. upb_array_append(values, value, d->arena);
  6018. jsondec_wellknownvalue(d, value_msg, value_m);
  6019. }
  6020. jsondec_arrend(d);
  6021. }
  6022. static void jsondec_struct(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6023. const upb_fielddef *fields_f = upb_msgdef_itof(m, 1);
  6024. const upb_msgdef *entry_m = upb_fielddef_msgsubdef(fields_f);
  6025. const upb_fielddef *value_f = upb_msgdef_itof(entry_m, 2);
  6026. const upb_msgdef *value_m = upb_fielddef_msgsubdef(value_f);
  6027. upb_map *fields = upb_msg_mutable(msg, fields_f, d->arena).map;
  6028. jsondec_objstart(d);
  6029. while (jsondec_objnext(d)) {
  6030. upb_msgval key, value;
  6031. upb_msg *value_msg = upb_msg_new(value_m, d->arena);
  6032. key.str_val = jsondec_string(d);
  6033. value.msg_val = value_msg;
  6034. upb_map_set(fields, key, value, d->arena);
  6035. jsondec_entrysep(d);
  6036. jsondec_wellknownvalue(d, value_msg, value_m);
  6037. }
  6038. jsondec_objend(d);
  6039. }
  6040. static void jsondec_wellknownvalue(jsondec *d, upb_msg *msg,
  6041. const upb_msgdef *m) {
  6042. upb_msgval val;
  6043. const upb_fielddef *f;
  6044. upb_msg *submsg;
  6045. switch (jsondec_peek(d)) {
  6046. case JD_NUMBER:
  6047. /* double number_value = 2; */
  6048. f = upb_msgdef_itof(m, 2);
  6049. val.double_val = jsondec_number(d);
  6050. break;
  6051. case JD_STRING:
  6052. /* string string_value = 3; */
  6053. f = upb_msgdef_itof(m, 3);
  6054. val.str_val = jsondec_string(d);
  6055. break;
  6056. case JD_FALSE:
  6057. /* bool bool_value = 4; */
  6058. f = upb_msgdef_itof(m, 4);
  6059. val.bool_val = false;
  6060. jsondec_false(d);
  6061. break;
  6062. case JD_TRUE:
  6063. /* bool bool_value = 4; */
  6064. f = upb_msgdef_itof(m, 4);
  6065. val.bool_val = true;
  6066. jsondec_true(d);
  6067. break;
  6068. case JD_NULL:
  6069. /* NullValue null_value = 1; */
  6070. f = upb_msgdef_itof(m, 1);
  6071. val.int32_val = 0;
  6072. jsondec_null(d);
  6073. break;
  6074. /* Note: these cases return, because upb_msg_mutable() is enough. */
  6075. case JD_OBJECT:
  6076. /* Struct struct_value = 5; */
  6077. f = upb_msgdef_itof(m, 5);
  6078. submsg = upb_msg_mutable(msg, f, d->arena).msg;
  6079. jsondec_struct(d, submsg, upb_fielddef_msgsubdef(f));
  6080. return;
  6081. case JD_ARRAY:
  6082. /* ListValue list_value = 6; */
  6083. f = upb_msgdef_itof(m, 6);
  6084. submsg = upb_msg_mutable(msg, f, d->arena).msg;
  6085. jsondec_listvalue(d, submsg, upb_fielddef_msgsubdef(f));
  6086. return;
  6087. default:
  6088. UPB_UNREACHABLE();
  6089. }
  6090. upb_msg_set(msg, f, val, d->arena);
  6091. }
  6092. static upb_strview jsondec_mask(jsondec *d, const char *buf, const char *end) {
  6093. /* FieldMask fields grow due to inserted '_' characters, so we can't do the
  6094. * transform in place. */
  6095. const char *ptr = buf;
  6096. upb_strview ret;
  6097. char *out;
  6098. ret.size = end - ptr;
  6099. while (ptr < end) {
  6100. ret.size += (*ptr >= 'A' && *ptr <= 'Z');
  6101. ptr++;
  6102. }
  6103. out = upb_arena_malloc(d->arena, ret.size);
  6104. ptr = buf;
  6105. ret.data = out;
  6106. while (ptr < end) {
  6107. char ch = *ptr++;
  6108. if (ch >= 'A' && ch <= 'Z') {
  6109. *out++ = '_';
  6110. *out++ = ch + 32;
  6111. } else if (ch == '_') {
  6112. jsondec_err(d, "field mask may not contain '_'");
  6113. } else {
  6114. *out++ = ch;
  6115. }
  6116. }
  6117. return ret;
  6118. }
  6119. static void jsondec_fieldmask(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6120. /* repeated string paths = 1; */
  6121. const upb_fielddef *paths_f = upb_msgdef_itof(m, 1);
  6122. upb_array *arr = upb_msg_mutable(msg, paths_f, d->arena).array;
  6123. upb_strview str = jsondec_string(d);
  6124. const char *ptr = str.data;
  6125. const char *end = ptr + str.size;
  6126. upb_msgval val;
  6127. while (ptr < end) {
  6128. const char *elem_end = memchr(ptr, ',', end - ptr);
  6129. if (elem_end) {
  6130. val.str_val = jsondec_mask(d, ptr, elem_end);
  6131. ptr = elem_end + 1;
  6132. } else {
  6133. val.str_val = jsondec_mask(d, ptr, end);
  6134. ptr = end;
  6135. }
  6136. upb_array_append(arr, val, d->arena);
  6137. }
  6138. }
  6139. static void jsondec_anyfield(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6140. if (upb_msgdef_wellknowntype(m) == UPB_WELLKNOWN_UNSPECIFIED) {
  6141. /* For regular types: {"@type": "[user type]", "f1": <V1>, "f2": <V2>}
  6142. * where f1, f2, etc. are the normal fields of this type. */
  6143. jsondec_field(d, msg, m);
  6144. } else {
  6145. /* For well-known types: {"@type": "[well-known type]", "value": <X>}
  6146. * where <X> is whatever encoding the WKT normally uses. */
  6147. upb_strview str = jsondec_string(d);
  6148. jsondec_entrysep(d);
  6149. if (!jsondec_streql(str, "value")) {
  6150. jsondec_err(d, "Key for well-known type must be 'value'");
  6151. }
  6152. jsondec_wellknown(d, msg, m);
  6153. }
  6154. }
  6155. static const upb_msgdef *jsondec_typeurl(jsondec *d, upb_msg *msg,
  6156. const upb_msgdef *m) {
  6157. const upb_fielddef *type_url_f = upb_msgdef_itof(m, 1);
  6158. const upb_msgdef *type_m;
  6159. upb_strview type_url = jsondec_string(d);
  6160. const char *end = type_url.data + type_url.size;
  6161. const char *ptr = end;
  6162. upb_msgval val;
  6163. val.str_val = type_url;
  6164. upb_msg_set(msg, type_url_f, val, d->arena);
  6165. /* Find message name after the last '/' */
  6166. while (ptr > type_url.data && *--ptr != '/') {}
  6167. if (ptr == type_url.data || ptr == end) {
  6168. jsondec_err(d, "Type url must have at least one '/' and non-empty host");
  6169. }
  6170. ptr++;
  6171. type_m = upb_symtab_lookupmsg2(d->any_pool, ptr, end - ptr);
  6172. if (!type_m) {
  6173. jsondec_err(d, "Type was not found");
  6174. }
  6175. return type_m;
  6176. }
  6177. static void jsondec_any(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6178. /* string type_url = 1;
  6179. * bytes value = 2; */
  6180. const upb_fielddef *value_f = upb_msgdef_itof(m, 2);
  6181. upb_msg *any_msg;
  6182. const upb_msgdef *any_m = NULL;
  6183. const char *pre_type_data = NULL;
  6184. const char *pre_type_end = NULL;
  6185. upb_msgval encoded;
  6186. jsondec_objstart(d);
  6187. /* Scan looking for "@type", which is not necessarily first. */
  6188. while (!any_m && jsondec_objnext(d)) {
  6189. const char *start = d->ptr;
  6190. upb_strview name = jsondec_string(d);
  6191. jsondec_entrysep(d);
  6192. if (jsondec_streql(name, "@type")) {
  6193. any_m = jsondec_typeurl(d, msg, m);
  6194. if (pre_type_data) {
  6195. pre_type_end = start;
  6196. while (*pre_type_end != ',') pre_type_end--;
  6197. }
  6198. } else {
  6199. if (!pre_type_data) pre_type_data = start;
  6200. jsondec_skipval(d);
  6201. }
  6202. }
  6203. if (!any_m) {
  6204. jsondec_err(d, "Any object didn't contain a '@type' field");
  6205. }
  6206. any_msg = upb_msg_new(any_m, d->arena);
  6207. if (pre_type_data) {
  6208. size_t len = pre_type_end - pre_type_data + 1;
  6209. char *tmp = upb_arena_malloc(d->arena, len);
  6210. const char *saved_ptr = d->ptr;
  6211. const char *saved_end = d->end;
  6212. memcpy(tmp, pre_type_data, len - 1);
  6213. tmp[len - 1] = '}';
  6214. d->ptr = tmp;
  6215. d->end = tmp + len;
  6216. d->is_first = true;
  6217. while (jsondec_objnext(d)) {
  6218. jsondec_anyfield(d, any_msg, any_m);
  6219. }
  6220. d->ptr = saved_ptr;
  6221. d->end = saved_end;
  6222. }
  6223. while (jsondec_objnext(d)) {
  6224. jsondec_anyfield(d, any_msg, any_m);
  6225. }
  6226. jsondec_objend(d);
  6227. encoded.str_val.data = upb_encode(any_msg, upb_msgdef_layout(any_m), d->arena,
  6228. &encoded.str_val.size);
  6229. upb_msg_set(msg, value_f, encoded, d->arena);
  6230. }
  6231. static void jsondec_wrapper(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6232. const upb_fielddef *value_f = upb_msgdef_itof(m, 1);
  6233. upb_msgval val = jsondec_value(d, value_f);
  6234. upb_msg_set(msg, value_f, val, d->arena);
  6235. }
  6236. static void jsondec_wellknown(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6237. switch (upb_msgdef_wellknowntype(m)) {
  6238. case UPB_WELLKNOWN_ANY:
  6239. jsondec_any(d, msg, m);
  6240. break;
  6241. case UPB_WELLKNOWN_FIELDMASK:
  6242. jsondec_fieldmask(d, msg, m);
  6243. break;
  6244. case UPB_WELLKNOWN_DURATION:
  6245. jsondec_duration(d, msg, m);
  6246. break;
  6247. case UPB_WELLKNOWN_TIMESTAMP:
  6248. jsondec_timestamp(d, msg, m);
  6249. break;
  6250. case UPB_WELLKNOWN_VALUE:
  6251. jsondec_wellknownvalue(d, msg, m);
  6252. break;
  6253. case UPB_WELLKNOWN_LISTVALUE:
  6254. jsondec_listvalue(d, msg, m);
  6255. break;
  6256. case UPB_WELLKNOWN_STRUCT:
  6257. jsondec_struct(d, msg, m);
  6258. break;
  6259. case UPB_WELLKNOWN_DOUBLEVALUE:
  6260. case UPB_WELLKNOWN_FLOATVALUE:
  6261. case UPB_WELLKNOWN_INT64VALUE:
  6262. case UPB_WELLKNOWN_UINT64VALUE:
  6263. case UPB_WELLKNOWN_INT32VALUE:
  6264. case UPB_WELLKNOWN_UINT32VALUE:
  6265. case UPB_WELLKNOWN_STRINGVALUE:
  6266. case UPB_WELLKNOWN_BYTESVALUE:
  6267. case UPB_WELLKNOWN_BOOLVALUE:
  6268. jsondec_wrapper(d, msg, m);
  6269. break;
  6270. default:
  6271. UPB_UNREACHABLE();
  6272. }
  6273. }
  6274. bool upb_json_decode(const char *buf, size_t size, upb_msg *msg,
  6275. const upb_msgdef *m, const upb_symtab *any_pool,
  6276. int options, upb_arena *arena, upb_status *status) {
  6277. jsondec d;
  6278. d.ptr = buf;
  6279. d.end = buf + size;
  6280. d.arena = arena;
  6281. d.any_pool = any_pool;
  6282. d.status = status;
  6283. d.options = options;
  6284. d.depth = 64;
  6285. d.line = 1;
  6286. d.line_begin = d.ptr;
  6287. d.debug_field = NULL;
  6288. d.is_first = false;
  6289. if (setjmp(d.err)) return false;
  6290. jsondec_tomsg(&d, msg, m);
  6291. return true;
  6292. }
  6293. #include <ctype.h>
  6294. #include <float.h>
  6295. #include <inttypes.h>
  6296. #include <stdarg.h>
  6297. #include <stdio.h>
  6298. #include <string.h>
  6299. #include <setjmp.h>
  6300. typedef struct {
  6301. char *buf, *ptr, *end;
  6302. size_t overflow;
  6303. int indent_depth;
  6304. int options;
  6305. const upb_symtab *ext_pool;
  6306. jmp_buf err;
  6307. upb_status *status;
  6308. upb_arena *arena;
  6309. } jsonenc;
  6310. static void jsonenc_msg(jsonenc *e, const upb_msg *msg, const upb_msgdef *m);
  6311. static void jsonenc_scalar(jsonenc *e, upb_msgval val, const upb_fielddef *f);
  6312. static void jsonenc_msgfield(jsonenc *e, const upb_msg *msg,
  6313. const upb_msgdef *m);
  6314. static void jsonenc_msgfields(jsonenc *e, const upb_msg *msg,
  6315. const upb_msgdef *m);
  6316. static void jsonenc_value(jsonenc *e, const upb_msg *msg, const upb_msgdef *m);
  6317. UPB_NORETURN static void jsonenc_err(jsonenc *e, const char *msg) {
  6318. upb_status_seterrmsg(e->status, msg);
  6319. longjmp(e->err, 1);
  6320. }
  6321. UPB_NORETURN static void jsonenc_errf(jsonenc *e, const char *fmt, ...) {
  6322. va_list argp;
  6323. va_start(argp, fmt);
  6324. upb_status_vseterrf(e->status, fmt, argp);
  6325. va_end(argp);
  6326. longjmp(e->err, 1);
  6327. }
  6328. static upb_arena *jsonenc_arena(jsonenc *e) {
  6329. /* Create lazily, since it's only needed for Any */
  6330. if (!e->arena) {
  6331. e->arena = upb_arena_new();
  6332. }
  6333. return e->arena;
  6334. }
  6335. static void jsonenc_putbytes(jsonenc *e, const void *data, size_t len) {
  6336. size_t have = e->end - e->ptr;
  6337. if (UPB_LIKELY(have >= len)) {
  6338. memcpy(e->ptr, data, len);
  6339. e->ptr += len;
  6340. } else {
  6341. if (have) memcpy(e->ptr, data, have);
  6342. e->ptr += have;
  6343. e->overflow += (len - have);
  6344. }
  6345. }
  6346. static void jsonenc_putstr(jsonenc *e, const char *str) {
  6347. jsonenc_putbytes(e, str, strlen(str));
  6348. }
  6349. static void jsonenc_printf(jsonenc *e, const char *fmt, ...) {
  6350. size_t n;
  6351. size_t have = e->end - e->ptr;
  6352. va_list args;
  6353. va_start(args, fmt);
  6354. n = _upb_vsnprintf(e->ptr, have, fmt, args);
  6355. va_end(args);
  6356. if (UPB_LIKELY(have > n)) {
  6357. e->ptr += n;
  6358. } else {
  6359. e->ptr += have;
  6360. e->overflow += (n - have);
  6361. }
  6362. }
  6363. static void jsonenc_nanos(jsonenc *e, int32_t nanos) {
  6364. int digits = 9;
  6365. if (nanos == 0) return;
  6366. if (nanos < 0 || nanos >= 1000000000) {
  6367. jsonenc_err(e, "error formatting timestamp as JSON: invalid nanos");
  6368. }
  6369. while (nanos % 1000 == 0) {
  6370. nanos /= 1000;
  6371. digits -= 3;
  6372. }
  6373. jsonenc_printf(e, ".%0.*" PRId32, digits, nanos);
  6374. }
  6375. static void jsonenc_timestamp(jsonenc *e, const upb_msg *msg,
  6376. const upb_msgdef *m) {
  6377. const upb_fielddef *seconds_f = upb_msgdef_itof(m, 1);
  6378. const upb_fielddef *nanos_f = upb_msgdef_itof(m, 2);
  6379. int64_t seconds = upb_msg_get(msg, seconds_f).int64_val;
  6380. int32_t nanos = upb_msg_get(msg, nanos_f).int32_val;
  6381. int L, N, I, J, K, hour, min, sec;
  6382. if (seconds < -62135596800) {
  6383. jsonenc_err(e,
  6384. "error formatting timestamp as JSON: minimum acceptable value "
  6385. "is 0001-01-01T00:00:00Z");
  6386. } else if (seconds > 253402300799) {
  6387. jsonenc_err(e,
  6388. "error formatting timestamp as JSON: maximum acceptable value "
  6389. "is 9999-12-31T23:59:59Z");
  6390. }
  6391. /* Julian Day -> Y/M/D, Algorithm from:
  6392. * Fliegel, H. F., and Van Flandern, T. C., "A Machine Algorithm for
  6393. * Processing Calendar Dates," Communications of the Association of
  6394. * Computing Machines, vol. 11 (1968), p. 657. */
  6395. L = (int)(seconds / 86400) + 68569 + 2440588;
  6396. N = 4 * L / 146097;
  6397. L = L - (146097 * N + 3) / 4;
  6398. I = 4000 * (L + 1) / 1461001;
  6399. L = L - 1461 * I / 4 + 31;
  6400. J = 80 * L / 2447;
  6401. K = L - 2447 * J / 80;
  6402. L = J / 11;
  6403. J = J + 2 - 12 * L;
  6404. I = 100 * (N - 49) + I + L;
  6405. sec = seconds % 60;
  6406. min = (seconds / 60) % 60;
  6407. hour = (seconds / 3600) % 24;
  6408. jsonenc_printf(e, "\"%04d-%02d-%02dT%02d:%02d:%02d", I, J, K, hour, min, sec);
  6409. jsonenc_nanos(e, nanos);
  6410. jsonenc_putstr(e, "Z\"");
  6411. }
  6412. static void jsonenc_duration(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
  6413. const upb_fielddef *seconds_f = upb_msgdef_itof(m, 1);
  6414. const upb_fielddef *nanos_f = upb_msgdef_itof(m, 2);
  6415. int64_t seconds = upb_msg_get(msg, seconds_f).int64_val;
  6416. int32_t nanos = upb_msg_get(msg, nanos_f).int32_val;
  6417. if (seconds > 315576000000 || seconds < -315576000000 ||
  6418. (seconds < 0) != (nanos < 0)) {
  6419. jsonenc_err(e, "bad duration");
  6420. }
  6421. if (nanos < 0) {
  6422. nanos = -nanos;
  6423. }
  6424. jsonenc_printf(e, "\"%" PRId64, seconds);
  6425. jsonenc_nanos(e, nanos);
  6426. jsonenc_putstr(e, "s\"");
  6427. }
  6428. static void jsonenc_enum(int32_t val, const upb_fielddef *f, jsonenc *e) {
  6429. const upb_enumdef *e_def = upb_fielddef_enumsubdef(f);
  6430. const char *name = upb_enumdef_iton(e_def, val);
  6431. if (name) {
  6432. jsonenc_printf(e, "\"%s\"", name);
  6433. } else {
  6434. jsonenc_printf(e, "%" PRId32, val);
  6435. }
  6436. }
  6437. static void jsonenc_bytes(jsonenc *e, upb_strview str) {
  6438. /* This is the regular base64, not the "web-safe" version. */
  6439. static const char base64[] =
  6440. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  6441. const unsigned char *ptr = (unsigned char*)str.data;
  6442. const unsigned char *end = ptr + str.size;
  6443. char buf[4];
  6444. jsonenc_putstr(e, "\"");
  6445. while (end - ptr >= 3) {
  6446. buf[0] = base64[ptr[0] >> 2];
  6447. buf[1] = base64[((ptr[0] & 0x3) << 4) | (ptr[1] >> 4)];
  6448. buf[2] = base64[((ptr[1] & 0xf) << 2) | (ptr[2] >> 6)];
  6449. buf[3] = base64[ptr[2] & 0x3f];
  6450. jsonenc_putbytes(e, buf, 4);
  6451. ptr += 3;
  6452. }
  6453. switch (end - ptr) {
  6454. case 2:
  6455. buf[0] = base64[ptr[0] >> 2];
  6456. buf[1] = base64[((ptr[0] & 0x3) << 4) | (ptr[1] >> 4)];
  6457. buf[2] = base64[(ptr[1] & 0xf) << 2];
  6458. buf[3] = '=';
  6459. jsonenc_putbytes(e, buf, 4);
  6460. break;
  6461. case 1:
  6462. buf[0] = base64[ptr[0] >> 2];
  6463. buf[1] = base64[((ptr[0] & 0x3) << 4)];
  6464. buf[2] = '=';
  6465. buf[3] = '=';
  6466. jsonenc_putbytes(e, buf, 4);
  6467. break;
  6468. }
  6469. jsonenc_putstr(e, "\"");
  6470. }
  6471. static void jsonenc_stringbody(jsonenc *e, upb_strview str) {
  6472. const char *ptr = str.data;
  6473. const char *end = ptr + str.size;
  6474. while (ptr < end) {
  6475. switch (*ptr) {
  6476. case '\n':
  6477. jsonenc_putstr(e, "\\n");
  6478. break;
  6479. case '\r':
  6480. jsonenc_putstr(e, "\\r");
  6481. break;
  6482. case '\t':
  6483. jsonenc_putstr(e, "\\t");
  6484. break;
  6485. case '\"':
  6486. jsonenc_putstr(e, "\\\"");
  6487. break;
  6488. case '\f':
  6489. jsonenc_putstr(e, "\\f");
  6490. break;
  6491. case '\b':
  6492. jsonenc_putstr(e, "\\b");
  6493. break;
  6494. case '\\':
  6495. jsonenc_putstr(e, "\\\\");
  6496. break;
  6497. default:
  6498. if ((uint8_t)*ptr < 0x20) {
  6499. jsonenc_printf(e, "\\u%04x", (int)(uint8_t)*ptr);
  6500. } else {
  6501. /* This could be a non-ASCII byte. We rely on the string being valid
  6502. * UTF-8. */
  6503. jsonenc_putbytes(e, ptr, 1);
  6504. }
  6505. break;
  6506. }
  6507. ptr++;
  6508. }
  6509. }
  6510. static void jsonenc_string(jsonenc *e, upb_strview str) {
  6511. jsonenc_putstr(e, "\"");
  6512. jsonenc_stringbody(e, str);
  6513. jsonenc_putstr(e, "\"");
  6514. }
  6515. static void jsonenc_double(jsonenc *e, const char *fmt, double val) {
  6516. if (val == UPB_INFINITY) {
  6517. jsonenc_putstr(e, "\"Infinity\"");
  6518. } else if (val == -UPB_INFINITY) {
  6519. jsonenc_putstr(e, "\"-Infinity\"");
  6520. } else if (val != val) {
  6521. jsonenc_putstr(e, "\"NaN\"");
  6522. } else {
  6523. jsonenc_printf(e, fmt, val);
  6524. }
  6525. }
  6526. static void jsonenc_wrapper(jsonenc *e, const upb_msg *msg,
  6527. const upb_msgdef *m) {
  6528. const upb_fielddef *val_f = upb_msgdef_itof(m, 1);
  6529. upb_msgval val = upb_msg_get(msg, val_f);
  6530. jsonenc_scalar(e, val, val_f);
  6531. }
  6532. static const upb_msgdef *jsonenc_getanymsg(jsonenc *e, upb_strview type_url) {
  6533. /* Find last '/', if any. */
  6534. const char *end = type_url.data + type_url.size;
  6535. const char *ptr = end;
  6536. const upb_msgdef *ret;
  6537. if (!e->ext_pool) {
  6538. jsonenc_err(e, "Tried to encode Any, but no symtab was provided");
  6539. }
  6540. if (type_url.size == 0) goto badurl;
  6541. while (true) {
  6542. if (--ptr == type_url.data) {
  6543. /* Type URL must contain at least one '/', with host before. */
  6544. goto badurl;
  6545. }
  6546. if (*ptr == '/') {
  6547. ptr++;
  6548. break;
  6549. }
  6550. }
  6551. ret = upb_symtab_lookupmsg2(e->ext_pool, ptr, end - ptr);
  6552. if (!ret) {
  6553. jsonenc_errf(e, "Couldn't find Any type: %.*s (full URL: " UPB_STRVIEW_FORMAT ")", (int)(end - ptr), ptr, UPB_STRVIEW_ARGS(type_url));
  6554. }
  6555. return ret;
  6556. badurl:
  6557. jsonenc_errf(
  6558. e, "Bad type URL: " UPB_STRVIEW_FORMAT, UPB_STRVIEW_ARGS(type_url));
  6559. }
  6560. static void jsonenc_any(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
  6561. const upb_fielddef *type_url_f = upb_msgdef_itof(m, 1);
  6562. const upb_fielddef *value_f = upb_msgdef_itof(m, 2);
  6563. upb_strview type_url = upb_msg_get(msg, type_url_f).str_val;
  6564. upb_strview value = upb_msg_get(msg, value_f).str_val;
  6565. const upb_msgdef *any_m = jsonenc_getanymsg(e, type_url);
  6566. const upb_msglayout *any_layout = upb_msgdef_layout(any_m);
  6567. upb_arena *arena = jsonenc_arena(e);
  6568. upb_msg *any = upb_msg_new(any_m, arena);
  6569. if (!upb_decode(value.data, value.size, any, any_layout, arena)) {
  6570. jsonenc_err(e, "Error decoding message in Any");
  6571. }
  6572. jsonenc_putstr(e, "{\"@type\":");
  6573. jsonenc_string(e, type_url);
  6574. jsonenc_putstr(e, ",");
  6575. if (upb_msgdef_wellknowntype(any_m) == UPB_WELLKNOWN_UNSPECIFIED) {
  6576. /* Regular messages: {"@type": "...","foo": 1, "bar": 2} */
  6577. jsonenc_msgfields(e, any, any_m);
  6578. } else {
  6579. /* Well-known type: {"@type": "...","value": <well-known encoding>} */
  6580. jsonenc_putstr(e, "\"value\":");
  6581. jsonenc_msgfield(e, any, any_m);
  6582. }
  6583. jsonenc_putstr(e, "}");
  6584. }
  6585. static void jsonenc_putsep(jsonenc *e, const char *str, bool *first) {
  6586. if (*first) {
  6587. *first = false;
  6588. } else {
  6589. jsonenc_putstr(e, str);
  6590. }
  6591. }
  6592. static void jsonenc_fieldpath(jsonenc *e, upb_strview path) {
  6593. const char *ptr = path.data;
  6594. const char *end = ptr + path.size;
  6595. while (ptr < end) {
  6596. char ch = *ptr;
  6597. if (ch >= 'A' && ch <= 'Z') {
  6598. jsonenc_err(e, "Field mask element may not have upper-case letter.");
  6599. } else if (ch == '_') {
  6600. if (ptr == end - 1 || *(ptr + 1) < 'a' || *(ptr + 1) > 'z') {
  6601. jsonenc_err(e, "Underscore must be followed by a lowercase letter.");
  6602. }
  6603. ch = *++ptr - 32;
  6604. }
  6605. jsonenc_putbytes(e, &ch, 1);
  6606. ptr++;
  6607. }
  6608. }
  6609. static void jsonenc_fieldmask(jsonenc *e, const upb_msg *msg,
  6610. const upb_msgdef *m) {
  6611. const upb_fielddef *paths_f = upb_msgdef_itof(m, 1);
  6612. const upb_array *paths = upb_msg_get(msg, paths_f).array_val;
  6613. bool first = true;
  6614. size_t i, n = 0;
  6615. if (paths) n = upb_array_size(paths);
  6616. jsonenc_putstr(e, "\"");
  6617. for (i = 0; i < n; i++) {
  6618. jsonenc_putsep(e, ",", &first);
  6619. jsonenc_fieldpath(e, upb_array_get(paths, i).str_val);
  6620. }
  6621. jsonenc_putstr(e, "\"");
  6622. }
  6623. static void jsonenc_struct(jsonenc *e, const upb_msg *msg,
  6624. const upb_msgdef *m) {
  6625. const upb_fielddef *fields_f = upb_msgdef_itof(m, 1);
  6626. const upb_map *fields = upb_msg_get(msg, fields_f).map_val;
  6627. const upb_msgdef *entry_m = upb_fielddef_msgsubdef(fields_f);
  6628. const upb_fielddef *value_f = upb_msgdef_itof(entry_m, 2);
  6629. size_t iter = UPB_MAP_BEGIN;
  6630. bool first = true;
  6631. jsonenc_putstr(e, "{");
  6632. if (fields) {
  6633. while (upb_mapiter_next(fields, &iter)) {
  6634. upb_msgval key = upb_mapiter_key(fields, iter);
  6635. upb_msgval val = upb_mapiter_value(fields, iter);
  6636. jsonenc_putsep(e, ",", &first);
  6637. jsonenc_string(e, key.str_val);
  6638. jsonenc_putstr(e, ":");
  6639. jsonenc_value(e, val.msg_val, upb_fielddef_msgsubdef(value_f));
  6640. }
  6641. }
  6642. jsonenc_putstr(e, "}");
  6643. }
  6644. static void jsonenc_listvalue(jsonenc *e, const upb_msg *msg,
  6645. const upb_msgdef *m) {
  6646. const upb_fielddef *values_f = upb_msgdef_itof(m, 1);
  6647. const upb_msgdef *values_m = upb_fielddef_msgsubdef(values_f);
  6648. const upb_array *values = upb_msg_get(msg, values_f).array_val;
  6649. size_t i;
  6650. bool first = true;
  6651. jsonenc_putstr(e, "[");
  6652. if (values) {
  6653. const size_t size = upb_array_size(values);
  6654. for (i = 0; i < size; i++) {
  6655. upb_msgval elem = upb_array_get(values, i);
  6656. jsonenc_putsep(e, ",", &first);
  6657. jsonenc_value(e, elem.msg_val, values_m);
  6658. }
  6659. }
  6660. jsonenc_putstr(e, "]");
  6661. }
  6662. static void jsonenc_value(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
  6663. /* TODO(haberman): do we want a reflection method to get oneof case? */
  6664. size_t iter = UPB_MSG_BEGIN;
  6665. const upb_fielddef *f;
  6666. upb_msgval val;
  6667. if (!upb_msg_next(msg, m, NULL, &f, &val, &iter)) {
  6668. jsonenc_err(e, "No value set in Value proto");
  6669. }
  6670. switch (upb_fielddef_number(f)) {
  6671. case 1:
  6672. jsonenc_putstr(e, "null");
  6673. break;
  6674. case 2:
  6675. jsonenc_double(e, "%.17g", val.double_val);
  6676. break;
  6677. case 3:
  6678. jsonenc_string(e, val.str_val);
  6679. break;
  6680. case 4:
  6681. jsonenc_putstr(e, val.bool_val ? "true" : "false");
  6682. break;
  6683. case 5:
  6684. jsonenc_struct(e, val.msg_val, upb_fielddef_msgsubdef(f));
  6685. break;
  6686. case 6:
  6687. jsonenc_listvalue(e, val.msg_val, upb_fielddef_msgsubdef(f));
  6688. break;
  6689. }
  6690. }
  6691. static void jsonenc_msgfield(jsonenc *e, const upb_msg *msg,
  6692. const upb_msgdef *m) {
  6693. switch (upb_msgdef_wellknowntype(m)) {
  6694. case UPB_WELLKNOWN_UNSPECIFIED:
  6695. jsonenc_msg(e, msg, m);
  6696. break;
  6697. case UPB_WELLKNOWN_ANY:
  6698. jsonenc_any(e, msg, m);
  6699. break;
  6700. case UPB_WELLKNOWN_FIELDMASK:
  6701. jsonenc_fieldmask(e, msg, m);
  6702. break;
  6703. case UPB_WELLKNOWN_DURATION:
  6704. jsonenc_duration(e, msg, m);
  6705. break;
  6706. case UPB_WELLKNOWN_TIMESTAMP:
  6707. jsonenc_timestamp(e, msg, m);
  6708. break;
  6709. case UPB_WELLKNOWN_DOUBLEVALUE:
  6710. case UPB_WELLKNOWN_FLOATVALUE:
  6711. case UPB_WELLKNOWN_INT64VALUE:
  6712. case UPB_WELLKNOWN_UINT64VALUE:
  6713. case UPB_WELLKNOWN_INT32VALUE:
  6714. case UPB_WELLKNOWN_UINT32VALUE:
  6715. case UPB_WELLKNOWN_STRINGVALUE:
  6716. case UPB_WELLKNOWN_BYTESVALUE:
  6717. case UPB_WELLKNOWN_BOOLVALUE:
  6718. jsonenc_wrapper(e, msg, m);
  6719. break;
  6720. case UPB_WELLKNOWN_VALUE:
  6721. jsonenc_value(e, msg, m);
  6722. break;
  6723. case UPB_WELLKNOWN_LISTVALUE:
  6724. jsonenc_listvalue(e, msg, m);
  6725. break;
  6726. case UPB_WELLKNOWN_STRUCT:
  6727. jsonenc_struct(e, msg, m);
  6728. break;
  6729. }
  6730. }
  6731. static void jsonenc_scalar(jsonenc *e, upb_msgval val, const upb_fielddef *f) {
  6732. switch (upb_fielddef_type(f)) {
  6733. case UPB_TYPE_BOOL:
  6734. jsonenc_putstr(e, val.bool_val ? "true" : "false");
  6735. break;
  6736. case UPB_TYPE_FLOAT:
  6737. jsonenc_double(e, "%.9g", val.float_val);
  6738. break;
  6739. case UPB_TYPE_DOUBLE:
  6740. jsonenc_double(e, "%.17g", val.double_val);
  6741. break;
  6742. case UPB_TYPE_INT32:
  6743. jsonenc_printf(e, "%" PRId32, val.int32_val);
  6744. break;
  6745. case UPB_TYPE_UINT32:
  6746. jsonenc_printf(e, "%" PRIu32, val.uint32_val);
  6747. break;
  6748. case UPB_TYPE_INT64:
  6749. jsonenc_printf(e, "\"%" PRId64 "\"", val.int64_val);
  6750. break;
  6751. case UPB_TYPE_UINT64:
  6752. jsonenc_printf(e, "\"%" PRIu64 "\"", val.uint64_val);
  6753. break;
  6754. case UPB_TYPE_STRING:
  6755. jsonenc_string(e, val.str_val);
  6756. break;
  6757. case UPB_TYPE_BYTES:
  6758. jsonenc_bytes(e, val.str_val);
  6759. break;
  6760. case UPB_TYPE_ENUM:
  6761. jsonenc_enum(val.int32_val, f, e);
  6762. break;
  6763. case UPB_TYPE_MESSAGE:
  6764. jsonenc_msgfield(e, val.msg_val, upb_fielddef_msgsubdef(f));
  6765. break;
  6766. }
  6767. }
  6768. static void jsonenc_mapkey(jsonenc *e, upb_msgval val, const upb_fielddef *f) {
  6769. jsonenc_putstr(e, "\"");
  6770. switch (upb_fielddef_type(f)) {
  6771. case UPB_TYPE_BOOL:
  6772. jsonenc_putstr(e, val.bool_val ? "true" : "false");
  6773. break;
  6774. case UPB_TYPE_INT32:
  6775. jsonenc_printf(e, "%" PRId32, val.int32_val);
  6776. break;
  6777. case UPB_TYPE_UINT32:
  6778. jsonenc_printf(e, "%" PRIu32, val.uint32_val);
  6779. break;
  6780. case UPB_TYPE_INT64:
  6781. jsonenc_printf(e, "%" PRId64, val.int64_val);
  6782. break;
  6783. case UPB_TYPE_UINT64:
  6784. jsonenc_printf(e, "%" PRIu64, val.uint64_val);
  6785. break;
  6786. case UPB_TYPE_STRING:
  6787. jsonenc_stringbody(e, val.str_val);
  6788. break;
  6789. default:
  6790. UPB_UNREACHABLE();
  6791. }
  6792. jsonenc_putstr(e, "\":");
  6793. }
  6794. static void jsonenc_array(jsonenc *e, const upb_array *arr,
  6795. const upb_fielddef *f) {
  6796. size_t i;
  6797. size_t size = upb_array_size(arr);
  6798. bool first = true;
  6799. jsonenc_putstr(e, "[");
  6800. for (i = 0; i < size; i++) {
  6801. jsonenc_putsep(e, ",", &first);
  6802. jsonenc_scalar(e, upb_array_get(arr, i), f);
  6803. }
  6804. jsonenc_putstr(e, "]");
  6805. }
  6806. static void jsonenc_map(jsonenc *e, const upb_map *map, const upb_fielddef *f) {
  6807. const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
  6808. const upb_fielddef *key_f = upb_msgdef_itof(entry, 1);
  6809. const upb_fielddef *val_f = upb_msgdef_itof(entry, 2);
  6810. size_t iter = UPB_MAP_BEGIN;
  6811. bool first = true;
  6812. jsonenc_putstr(e, "{");
  6813. while (upb_mapiter_next(map, &iter)) {
  6814. jsonenc_putsep(e, ",", &first);
  6815. jsonenc_mapkey(e, upb_mapiter_key(map, iter), key_f);
  6816. jsonenc_scalar(e, upb_mapiter_value(map, iter), val_f);
  6817. }
  6818. jsonenc_putstr(e, "}");
  6819. }
  6820. static void jsonenc_fieldval(jsonenc *e, const upb_fielddef *f,
  6821. upb_msgval val, bool *first) {
  6822. const char *name;
  6823. if (e->options & UPB_JSONENC_PROTONAMES) {
  6824. name = upb_fielddef_name(f);
  6825. } else {
  6826. name = upb_fielddef_jsonname(f);
  6827. }
  6828. jsonenc_putsep(e, ",", first);
  6829. jsonenc_printf(e, "\"%s\":", name);
  6830. if (upb_fielddef_ismap(f)) {
  6831. jsonenc_map(e, val.map_val, f);
  6832. } else if (upb_fielddef_isseq(f)) {
  6833. jsonenc_array(e, val.array_val, f);
  6834. } else {
  6835. jsonenc_scalar(e, val, f);
  6836. }
  6837. }
  6838. static void jsonenc_msgfields(jsonenc *e, const upb_msg *msg,
  6839. const upb_msgdef *m) {
  6840. upb_msgval val;
  6841. const upb_fielddef *f;
  6842. bool first = true;
  6843. if (e->options & UPB_JSONENC_EMITDEFAULTS) {
  6844. /* Iterate over all fields. */
  6845. upb_msg_field_iter i;
  6846. for (upb_msg_field_begin(&i, m); !upb_msg_field_done(&i);
  6847. upb_msg_field_next(&i)) {
  6848. f = upb_msg_iter_field(&i);
  6849. jsonenc_fieldval(e, f, upb_msg_get(msg, f), &first);
  6850. }
  6851. } else {
  6852. /* Iterate over non-empty fields. */
  6853. size_t iter = UPB_MSG_BEGIN;
  6854. while (upb_msg_next(msg, m, e->ext_pool, &f, &val, &iter)) {
  6855. jsonenc_fieldval(e, f, val, &first);
  6856. }
  6857. }
  6858. }
  6859. static void jsonenc_msg(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
  6860. jsonenc_putstr(e, "{");
  6861. jsonenc_msgfields(e, msg, m);
  6862. jsonenc_putstr(e, "}");
  6863. }
  6864. static size_t jsonenc_nullz(jsonenc *e, size_t size) {
  6865. size_t ret = e->ptr - e->buf + e->overflow;
  6866. if (size > 0) {
  6867. if (e->ptr == e->end) e->ptr--;
  6868. *e->ptr = '\0';
  6869. }
  6870. return ret;
  6871. }
  6872. size_t upb_json_encode(const upb_msg *msg, const upb_msgdef *m,
  6873. const upb_symtab *ext_pool, int options, char *buf,
  6874. size_t size, upb_status *status) {
  6875. jsonenc e;
  6876. e.buf = buf;
  6877. e.ptr = buf;
  6878. e.end = buf + size;
  6879. e.overflow = 0;
  6880. e.options = options;
  6881. e.ext_pool = ext_pool;
  6882. e.status = status;
  6883. e.arena = NULL;
  6884. if (setjmp(e.err)) return -1;
  6885. jsonenc_msgfield(&e, msg, m);
  6886. if (e.arena) upb_arena_free(e.arena);
  6887. return jsonenc_nullz(&e, size);
  6888. }
  6889. /* See port_def.inc. This should #undef all macros #defined there. */
  6890. #undef UPB_MAPTYPE_STRING
  6891. #undef UPB_SIZE
  6892. #undef UPB_PTR_AT
  6893. #undef UPB_READ_ONEOF
  6894. #undef UPB_WRITE_ONEOF
  6895. #undef UPB_INLINE
  6896. #undef UPB_ALIGN_UP
  6897. #undef UPB_ALIGN_DOWN
  6898. #undef UPB_ALIGN_MALLOC
  6899. #undef UPB_ALIGN_OF
  6900. #undef UPB_FORCEINLINE
  6901. #undef UPB_NOINLINE
  6902. #undef UPB_NORETURN
  6903. #undef UPB_MAX
  6904. #undef UPB_MIN
  6905. #undef UPB_UNUSED
  6906. #undef UPB_ASSUME
  6907. #undef UPB_ASSERT
  6908. #undef UPB_ASSERT_DEBUGVAR
  6909. #undef UPB_UNREACHABLE
  6910. #undef UPB_INFINITY
  6911. #undef UPB_MSVC_VSNPRINTF
  6912. #undef _upb_snprintf
  6913. #undef _upb_vsnprintf
  6914. #undef _upb_va_copy