12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555 |
- /*
- Copyright 2013 Daniel Wirtz <dcode@dcode.io>
- Copyright 2009 The Closure Library Authors. All Rights Reserved.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS-IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- /**
- * @license Long.js (c) 2013 Daniel Wirtz <dcode@dcode.io>
- * Released under the Apache License, Version 2.0
- * see: https://github.com/dcodeIO/Long.js for details
- */
- (function(global) {
- "use strict";
- /**
- * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers.
- * See the from* functions below for more convenient ways of constructing Longs.
- * @exports Long
- * @class A Long class for representing a 64 bit two's-complement integer value.
- * @param {number} low The low (signed) 32 bits of the long
- * @param {number} high The high (signed) 32 bits of the long
- * @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed
- * @constructor
- */
- var Long = function(low, high, unsigned) {
- /**
- * The low 32 bits as a signed value.
- * @type {number}
- * @expose
- */
- this.low = low|0;
- /**
- * The high 32 bits as a signed value.
- * @type {number}
- * @expose
- */
- this.high = high|0;
- /**
- * Whether unsigned or not.
- * @type {boolean}
- * @expose
- */
- this.unsigned = !!unsigned;
- };
- // The internal representation of a long is the two given signed, 32-bit values.
- // We use 32-bit pieces because these are the size of integers on which
- // Javascript performs bit-operations. For operations like addition and
- // multiplication, we split each number into 16 bit pieces, which can easily be
- // multiplied within Javascript's floating-point representation without overflow
- // or change in sign.
- //
- // In the algorithms below, we frequently reduce the negative case to the
- // positive case by negating the input(s) and then post-processing the result.
- // Note that we must ALWAYS check specially whether those values are MIN_VALUE
- // (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
- // a positive number, it overflows back into a negative). Not handling this
- // case would often result in infinite recursion.
- //
- // Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the from*
- // methods on which they depend.
- /**
- * Tests if the specified object is a Long.
- * @param {*} obj Object
- * @returns {boolean}
- * @expose
- */
- Long.isLong = function(obj) {
- return (obj && obj instanceof Long) === true;
- };
- /**
- * A cache of the Long representations of small integer values.
- * @type {!Object}
- * @inner
- */
- var INT_CACHE = {};
- /**
- * A cache of the Long representations of small unsigned integer values.
- * @type {!Object}
- * @inner
- */
- var UINT_CACHE = {};
- /**
- * Returns a Long representing the given 32 bit integer value.
- * @param {number} value The 32 bit integer in question
- * @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed
- * @returns {!Long} The corresponding Long value
- * @expose
- */
- Long.fromInt = function(value, unsigned) {
- var obj, cachedObj;
- if (!unsigned) {
- value = value | 0;
- if (-128 <= value && value < 128) {
- cachedObj = INT_CACHE[value];
- if (cachedObj)
- return cachedObj;
- }
- obj = new Long(value, value < 0 ? -1 : 0, false);
- if (-128 <= value && value < 128)
- INT_CACHE[value] = obj;
- return obj;
- } else {
- value = value >>> 0;
- if (0 <= value && value < 256) {
- cachedObj = UINT_CACHE[value];
- if (cachedObj)
- return cachedObj;
- }
- obj = new Long(value, (value | 0) < 0 ? -1 : 0, true);
- if (0 <= value && value < 256)
- UINT_CACHE[value] = obj;
- return obj;
- }
- };
- /**
- * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
- * @param {number} value The number in question
- * @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed
- * @returns {!Long} The corresponding Long value
- * @expose
- */
- Long.fromNumber = function(value, unsigned) {
- unsigned = !!unsigned;
- if (isNaN(value) || !isFinite(value))
- return Long.ZERO;
- if (!unsigned && value <= -TWO_PWR_63_DBL)
- return Long.MIN_VALUE;
- if (!unsigned && value + 1 >= TWO_PWR_63_DBL)
- return Long.MAX_VALUE;
- if (unsigned && value >= TWO_PWR_64_DBL)
- return Long.MAX_UNSIGNED_VALUE;
- if (value < 0)
- return Long.fromNumber(-value, unsigned).negate();
- return new Long((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
- };
- /**
- * Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is
- * assumed to use 32 bits.
- * @param {number} lowBits The low 32 bits
- * @param {number} highBits The high 32 bits
- * @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed
- * @returns {!Long} The corresponding Long value
- * @expose
- */
- Long.fromBits = function(lowBits, highBits, unsigned) {
- return new Long(lowBits, highBits, unsigned);
- };
- /**
- * Returns a Long representation of the given string, written using the specified radix.
- * @param {string} str The textual representation of the Long
- * @param {(boolean|number)=} unsigned Whether unsigned or not, defaults to `false` for signed
- * @param {number=} radix The radix in which the text is written (2-36), defaults to 10
- * @returns {!Long} The corresponding Long value
- * @expose
- */
- Long.fromString = function(str, unsigned, radix) {
- if (str.length === 0)
- throw Error('number format error: empty string');
- if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity")
- return Long.ZERO;
- if (typeof unsigned === 'number') // For goog.math.long compatibility
- radix = unsigned,
- unsigned = false;
- radix = radix || 10;
- if (radix < 2 || 36 < radix)
- throw Error('radix out of range: ' + radix);
- var p;
- if ((p = str.indexOf('-')) > 0)
- throw Error('number format error: interior "-" character: ' + str);
- else if (p === 0)
- return Long.fromString(str.substring(1), unsigned, radix).negate();
- // Do several (8) digits each time through the loop, so as to
- // minimize the calls to the very expensive emulated div.
- var radixToPower = Long.fromNumber(Math.pow(radix, 8));
- var result = Long.ZERO;
- for (var i = 0; i < str.length; i += 8) {
- var size = Math.min(8, str.length - i);
- var value = parseInt(str.substring(i, i + size), radix);
- if (size < 8) {
- var power = Long.fromNumber(Math.pow(radix, size));
- result = result.multiply(power).add(Long.fromNumber(value));
- } else {
- result = result.multiply(radixToPower);
- result = result.add(Long.fromNumber(value));
- }
- }
- result.unsigned = unsigned;
- return result;
- };
- /**
- * Converts the specified value to a Long.
- * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val Value
- * @returns {!Long}
- * @expose
- */
- Long.fromValue = function(val) {
- if (typeof val === 'number')
- return Long.fromNumber(val);
- if (typeof val === 'string')
- return Long.fromString(val);
- if (Long.isLong(val))
- return val;
- // Throws for not an object (undefined, null):
- return new Long(val.low, val.high, val.unsigned);
- };
- // NOTE: the compiler should inline these constant values below and then remove these variables, so there should be
- // no runtime penalty for these.
- /**
- * @type {number}
- * @const
- * @inner
- */
- var TWO_PWR_16_DBL = 1 << 16;
- /**
- * @type {number}
- * @const
- * @inner
- */
- var TWO_PWR_24_DBL = 1 << 24;
- /**
- * @type {number}
- * @const
- * @inner
- */
- var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL;
- /**
- * @type {number}
- * @const
- * @inner
- */
- var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL;
- /**
- * @type {number}
- * @const
- * @inner
- */
- var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2;
- /**
- * @type {!Long}
- * @const
- * @inner
- */
- var TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
- /**
- * Signed zero.
- * @type {!Long}
- * @expose
- */
- Long.ZERO = Long.fromInt(0);
- /**
- * Unsigned zero.
- * @type {!Long}
- * @expose
- */
- Long.UZERO = Long.fromInt(0, true);
- /**
- * Signed one.
- * @type {!Long}
- * @expose
- */
- Long.ONE = Long.fromInt(1);
- /**
- * Unsigned one.
- * @type {!Long}
- * @expose
- */
- Long.UONE = Long.fromInt(1, true);
- /**
- * Signed negative one.
- * @type {!Long}
- * @expose
- */
- Long.NEG_ONE = Long.fromInt(-1);
- /**
- * Maximum signed value.
- * @type {!Long}
- * @expose
- */
- Long.MAX_VALUE = Long.fromBits(0xFFFFFFFF|0, 0x7FFFFFFF|0, false);
- /**
- * Maximum unsigned value.
- * @type {!Long}
- * @expose
- */
- Long.MAX_UNSIGNED_VALUE = Long.fromBits(0xFFFFFFFF|0, 0xFFFFFFFF|0, true);
- /**
- * Minimum signed value.
- * @type {!Long}
- * @expose
- */
- Long.MIN_VALUE = Long.fromBits(0, 0x80000000|0, false);
- /**
- * Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.
- * @returns {number}
- * @expose
- */
- Long.prototype.toInt = function() {
- return this.unsigned ? this.low >>> 0 : this.low;
- };
- /**
- * Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa).
- * @returns {number}
- * @expose
- */
- Long.prototype.toNumber = function() {
- if (this.unsigned) {
- return ((this.high >>> 0) * TWO_PWR_32_DBL) + (this.low >>> 0);
- }
- return this.high * TWO_PWR_32_DBL + (this.low >>> 0);
- };
- /**
- * Converts the Long to a string written in the specified radix.
- * @param {number=} radix Radix (2-36), defaults to 10
- * @returns {string}
- * @override
- * @throws {RangeError} If `radix` is out of range
- * @expose
- */
- Long.prototype.toString = function(radix) {
- radix = radix || 10;
- if (radix < 2 || 36 < radix)
- throw RangeError('radix out of range: ' + radix);
- if (this.isZero())
- return '0';
- var rem;
- if (this.isNegative()) { // Unsigned Longs are never negative
- if (this.equals(Long.MIN_VALUE)) {
- // We need to change the Long value before it can be negated, so we remove
- // the bottom-most digit in this base and then recurse to do the rest.
- var radixLong = Long.fromNumber(radix);
- var div = this.div(radixLong);
- rem = div.multiply(radixLong).subtract(this);
- return div.toString(radix) + rem.toInt().toString(radix);
- } else
- return '-' + this.negate().toString(radix);
- }
- // Do several (6) digits each time through the loop, so as to
- // minimize the calls to the very expensive emulated div.
- var radixToPower = Long.fromNumber(Math.pow(radix, 6), this.unsigned);
- rem = this;
- var result = '';
- while (true) {
- var remDiv = rem.div(radixToPower),
- intval = rem.subtract(remDiv.multiply(radixToPower)).toInt() >>> 0,
- digits = intval.toString(radix);
- rem = remDiv;
- if (rem.isZero())
- return digits + result;
- else {
- while (digits.length < 6)
- digits = '0' + digits;
- result = '' + digits + result;
- }
- }
- };
- /**
- * Gets the high 32 bits as a signed integer.
- * @returns {number} Signed high bits
- * @expose
- */
- Long.prototype.getHighBits = function() {
- return this.high;
- };
- /**
- * Gets the high 32 bits as an unsigned integer.
- * @returns {number} Unsigned high bits
- * @expose
- */
- Long.prototype.getHighBitsUnsigned = function() {
- return this.high >>> 0;
- };
- /**
- * Gets the low 32 bits as a signed integer.
- * @returns {number} Signed low bits
- * @expose
- */
- Long.prototype.getLowBits = function() {
- return this.low;
- };
- /**
- * Gets the low 32 bits as an unsigned integer.
- * @returns {number} Unsigned low bits
- * @expose
- */
- Long.prototype.getLowBitsUnsigned = function() {
- return this.low >>> 0;
- };
- /**
- * Gets the number of bits needed to represent the absolute value of this Long.
- * @returns {number}
- * @expose
- */
- Long.prototype.getNumBitsAbs = function() {
- if (this.isNegative()) // Unsigned Longs are never negative
- return this.equals(Long.MIN_VALUE) ? 64 : this.negate().getNumBitsAbs();
- var val = this.high != 0 ? this.high : this.low;
- for (var bit = 31; bit > 0; bit--)
- if ((val & (1 << bit)) != 0)
- break;
- return this.high != 0 ? bit + 33 : bit + 1;
- };
- /**
- * Tests if this Long's value equals zero.
- * @returns {boolean}
- * @expose
- */
- Long.prototype.isZero = function() {
- return this.high === 0 && this.low === 0;
- };
- /**
- * Tests if this Long's value is negative.
- * @returns {boolean}
- * @expose
- */
- Long.prototype.isNegative = function() {
- return !this.unsigned && this.high < 0;
- };
- /**
- * Tests if this Long's value is positive.
- * @returns {boolean}
- * @expose
- */
- Long.prototype.isPositive = function() {
- return this.unsigned || this.high >= 0;
- };
- /**
- * Tests if this Long's value is odd.
- * @returns {boolean}
- * @expose
- */
- Long.prototype.isOdd = function() {
- return (this.low & 1) === 1;
- };
- /**
- * Tests if this Long's value is even.
- * @returns {boolean}
- * @expose
- */
- Long.prototype.isEven = function() {
- return (this.low & 1) === 0;
- };
- /**
- * Tests if this Long's value equals the specified's.
- * @param {!Long|number|string} other Other value
- * @returns {boolean}
- * @expose
- */
- Long.prototype.equals = function(other) {
- if (!Long.isLong(other))
- other = Long.fromValue(other);
- if (this.unsigned !== other.unsigned && (this.high >>> 31) === 1 && (other.high >>> 31) === 1)
- return false;
- return this.high === other.high && this.low === other.low;
- };
- /**
- * Tests if this Long's value differs from the specified's.
- * @param {!Long|number|string} other Other value
- * @returns {boolean}
- * @expose
- */
- Long.prototype.notEquals = function(other) {
- if (!Long.isLong(other))
- other = Long.fromValue(other);
- return !this.equals(other);
- };
- /**
- * Tests if this Long's value is less than the specified's.
- * @param {!Long|number|string} other Other value
- * @returns {boolean}
- * @expose
- */
- Long.prototype.lessThan = function(other) {
- if (!Long.isLong(other))
- other = Long.fromValue(other);
- return this.compare(other) < 0;
- };
- /**
- * Tests if this Long's value is less than or equal the specified's.
- * @param {!Long|number|string} other Other value
- * @returns {boolean}
- * @expose
- */
- Long.prototype.lessThanOrEqual = function(other) {
- if (!Long.isLong(other))
- other = Long.fromValue(other);
- return this.compare(other) <= 0;
- };
- /**
- * Tests if this Long's value is greater than the specified's.
- * @param {!Long|number|string} other Other value
- * @returns {boolean}
- * @expose
- */
- Long.prototype.greaterThan = function(other) {
- if (!Long.isLong(other))
- other = Long.fromValue(other);
- return this.compare(other) > 0;
- };
- /**
- * Tests if this Long's value is greater than or equal the specified's.
- * @param {!Long|number|string} other Other value
- * @returns {boolean}
- * @expose
- */
- Long.prototype.greaterThanOrEqual = function(other) {
- return this.compare(other) >= 0;
- };
- /**
- * Compares this Long's value with the specified's.
- * @param {!Long|number|string} other Other value
- * @returns {number} 0 if they are the same, 1 if the this is greater and -1
- * if the given one is greater
- * @expose
- */
- Long.prototype.compare = function(other) {
- if (this.equals(other))
- return 0;
- var thisNeg = this.isNegative(),
- otherNeg = other.isNegative();
- if (thisNeg && !otherNeg)
- return -1;
- if (!thisNeg && otherNeg)
- return 1;
- // At this point the sign bits are the same
- if (!this.unsigned)
- return this.subtract(other).isNegative() ? -1 : 1;
- // Both are positive if at least one is unsigned
- return (other.high >>> 0) > (this.high >>> 0) || (other.high === this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1;
- };
- /**
- * Negates this Long's value.
- * @returns {!Long} Negated Long
- * @expose
- */
- Long.prototype.negate = function() {
- if (!this.unsigned && this.equals(Long.MIN_VALUE))
- return Long.MIN_VALUE;
- return this.not().add(Long.ONE);
- };
- /**
- * Returns the sum of this and the specified Long.
- * @param {!Long|number|string} addend Addend
- * @returns {!Long} Sum
- * @expose
- */
- Long.prototype.add = function(addend) {
- if (!Long.isLong(addend))
- addend = Long.fromValue(addend);
- // Divide each number into 4 chunks of 16 bits, and then sum the chunks.
- var a48 = this.high >>> 16;
- var a32 = this.high & 0xFFFF;
- var a16 = this.low >>> 16;
- var a00 = this.low & 0xFFFF;
- var b48 = addend.high >>> 16;
- var b32 = addend.high & 0xFFFF;
- var b16 = addend.low >>> 16;
- var b00 = addend.low & 0xFFFF;
- var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
- c00 += a00 + b00;
- c16 += c00 >>> 16;
- c00 &= 0xFFFF;
- c16 += a16 + b16;
- c32 += c16 >>> 16;
- c16 &= 0xFFFF;
- c32 += a32 + b32;
- c48 += c32 >>> 16;
- c32 &= 0xFFFF;
- c48 += a48 + b48;
- c48 &= 0xFFFF;
- return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
- };
- /**
- * Returns the difference of this and the specified Long.
- * @param {!Long|number|string} subtrahend Subtrahend
- * @returns {!Long} Difference
- * @expose
- */
- Long.prototype.subtract = function(subtrahend) {
- if (!Long.isLong(subtrahend))
- subtrahend = Long.fromValue(subtrahend);
- return this.add(subtrahend.negate());
- };
- /**
- * Returns the product of this and the specified Long.
- * @param {!Long|number|string} multiplier Multiplier
- * @returns {!Long} Product
- * @expose
- */
- Long.prototype.multiply = function(multiplier) {
- if (this.isZero())
- return Long.ZERO;
- if (!Long.isLong(multiplier))
- multiplier = Long.fromValue(multiplier);
- if (multiplier.isZero())
- return Long.ZERO;
- if (this.equals(Long.MIN_VALUE))
- return multiplier.isOdd() ? Long.MIN_VALUE : Long.ZERO;
- if (multiplier.equals(Long.MIN_VALUE))
- return this.isOdd() ? Long.MIN_VALUE : Long.ZERO;
- if (this.isNegative()) {
- if (multiplier.isNegative())
- return this.negate().multiply(multiplier.negate());
- else
- return this.negate().multiply(multiplier).negate();
- } else if (multiplier.isNegative())
- return this.multiply(multiplier.negate()).negate();
- // If both longs are small, use float multiplication
- if (this.lessThan(TWO_PWR_24) && multiplier.lessThan(TWO_PWR_24))
- return Long.fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned);
- // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products.
- // We can skip products that would overflow.
- var a48 = this.high >>> 16;
- var a32 = this.high & 0xFFFF;
- var a16 = this.low >>> 16;
- var a00 = this.low & 0xFFFF;
- var b48 = multiplier.high >>> 16;
- var b32 = multiplier.high & 0xFFFF;
- var b16 = multiplier.low >>> 16;
- var b00 = multiplier.low & 0xFFFF;
- var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
- c00 += a00 * b00;
- c16 += c00 >>> 16;
- c00 &= 0xFFFF;
- c16 += a16 * b00;
- c32 += c16 >>> 16;
- c16 &= 0xFFFF;
- c16 += a00 * b16;
- c32 += c16 >>> 16;
- c16 &= 0xFFFF;
- c32 += a32 * b00;
- c48 += c32 >>> 16;
- c32 &= 0xFFFF;
- c32 += a16 * b16;
- c48 += c32 >>> 16;
- c32 &= 0xFFFF;
- c32 += a00 * b32;
- c48 += c32 >>> 16;
- c32 &= 0xFFFF;
- c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
- c48 &= 0xFFFF;
- return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
- };
- /**
- * Returns this Long divided by the specified.
- * @param {!Long|number|string} divisor Divisor
- * @returns {!Long} Quotient
- * @expose
- */
- Long.prototype.div = function(divisor) {
- if (!Long.isLong(divisor))
- divisor = Long.fromValue(divisor);
- if (divisor.isZero())
- throw(new Error('division by zero'));
- if (this.isZero())
- return this.unsigned ? Long.UZERO : Long.ZERO;
- var approx, rem, res;
- if (this.equals(Long.MIN_VALUE)) {
- if (divisor.equals(Long.ONE) || divisor.equals(Long.NEG_ONE))
- return Long.MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE
- else if (divisor.equals(Long.MIN_VALUE))
- return Long.ONE;
- else {
- // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
- var halfThis = this.shiftRight(1);
- approx = halfThis.div(divisor).shiftLeft(1);
- if (approx.equals(Long.ZERO)) {
- return divisor.isNegative() ? Long.ONE : Long.NEG_ONE;
- } else {
- rem = this.subtract(divisor.multiply(approx));
- res = approx.add(rem.div(divisor));
- return res;
- }
- }
- } else if (divisor.equals(Long.MIN_VALUE))
- return this.unsigned ? Long.UZERO : Long.ZERO;
- if (this.isNegative()) {
- if (divisor.isNegative())
- return this.negate().div(divisor.negate());
- return this.negate().div(divisor).negate();
- } else if (divisor.isNegative())
- return this.div(divisor.negate()).negate();
- // Repeat the following until the remainder is less than other: find a
- // floating-point that approximates remainder / other *from below*, add this
- // into the result, and subtract it from the remainder. It is critical that
- // the approximate value is less than or equal to the real value so that the
- // remainder never becomes negative.
- res = Long.ZERO;
- rem = this;
- while (rem.greaterThanOrEqual(divisor)) {
- // Approximate the result of division. This may be a little greater or
- // smaller than the actual value.
- approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber()));
- // We will tweak the approximate result by changing it in the 48-th digit or
- // the smallest non-fractional digit, whichever is larger.
- var log2 = Math.ceil(Math.log(approx) / Math.LN2),
- delta = (log2 <= 48) ? 1 : Math.pow(2, log2 - 48),
- // Decrease the approximation until it is smaller than the remainder. Note
- // that if it is too large, the product overflows and is negative.
- approxRes = Long.fromNumber(approx),
- approxRem = approxRes.multiply(divisor);
- while (approxRem.isNegative() || approxRem.greaterThan(rem)) {
- approx -= delta;
- approxRes = Long.fromNumber(approx, this.unsigned);
- approxRem = approxRes.multiply(divisor);
- }
- // We know the answer can't be zero... and actually, zero would cause
- // infinite recursion since we would make no progress.
- if (approxRes.isZero())
- approxRes = Long.ONE;
- res = res.add(approxRes);
- rem = rem.subtract(approxRem);
- }
- return res;
- };
- /**
- * Returns this Long modulo the specified.
- * @param {!Long|number|string} divisor Divisor
- * @returns {!Long} Remainder
- * @expose
- */
- Long.prototype.modulo = function(divisor) {
- if (!Long.isLong(divisor))
- divisor = Long.fromValue(divisor);
- return this.subtract(this.div(divisor).multiply(divisor));
- };
- /**
- * Returns the bitwise NOT of this Long.
- * @returns {!Long}
- * @expose
- */
- Long.prototype.not = function() {
- return Long.fromBits(~this.low, ~this.high, this.unsigned);
- };
- /**
- * Returns the bitwise AND of this Long and the specified.
- * @param {!Long|number|string} other Other Long
- * @returns {!Long}
- * @expose
- */
- Long.prototype.and = function(other) {
- if (!Long.isLong(other))
- other = Long.fromValue(other);
- return Long.fromBits(this.low & other.low, this.high & other.high, this.unsigned);
- };
- /**
- * Returns the bitwise OR of this Long and the specified.
- * @param {!Long|number|string} other Other Long
- * @returns {!Long}
- * @expose
- */
- Long.prototype.or = function(other) {
- if (!Long.isLong(other))
- other = Long.fromValue(other);
- return Long.fromBits(this.low | other.low, this.high | other.high, this.unsigned);
- };
- /**
- * Returns the bitwise XOR of this Long and the given one.
- * @param {!Long|number|string} other Other Long
- * @returns {!Long}
- * @expose
- */
- Long.prototype.xor = function(other) {
- if (!Long.isLong(other))
- other = Long.fromValue(other);
- return Long.fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned);
- };
- /**
- * Returns this Long with bits shifted to the left by the given amount.
- * @param {number|!Long} numBits Number of bits
- * @returns {!Long} Shifted Long
- * @expose
- */
- Long.prototype.shiftLeft = function(numBits) {
- if (Long.isLong(numBits))
- numBits = numBits.toInt();
- if ((numBits &= 63) === 0)
- return this;
- else if (numBits < 32)
- return Long.fromBits(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned);
- else
- return Long.fromBits(0, this.low << (numBits - 32), this.unsigned);
- };
- /**
- * Returns this Long with bits arithmetically shifted to the right by the given amount.
- * @param {number|!Long} numBits Number of bits
- * @returns {!Long} Shifted Long
- * @expose
- */
- Long.prototype.shiftRight = function(numBits) {
- if (Long.isLong(numBits))
- numBits = numBits.toInt();
- if ((numBits &= 63) === 0)
- return this;
- else if (numBits < 32)
- return Long.fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned);
- else
- return Long.fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned);
- };
- /**
- * Returns this Long with bits logically shifted to the right by the given amount.
- * @param {number|!Long} numBits Number of bits
- * @returns {!Long} Shifted Long
- * @expose
- */
- Long.prototype.shiftRightUnsigned = function(numBits) {
- if (Long.isLong(numBits))
- numBits = numBits.toInt();
- numBits &= 63;
- if (numBits === 0)
- return this;
- else {
- var high = this.high;
- if (numBits < 32) {
- var low = this.low;
- return Long.fromBits((low >>> numBits) | (high << (32 - numBits)), high >>> numBits, this.unsigned);
- } else if (numBits === 32)
- return Long.fromBits(high, 0, this.unsigned);
- else
- return Long.fromBits(high >>> (numBits - 32), 0, this.unsigned);
- }
- };
- /**
- * Converts this Long to signed.
- * @returns {!Long} Signed long
- * @expose
- */
- Long.prototype.toSigned = function() {
- if (!this.unsigned)
- return this;
- return new Long(this.low, this.high, false);
- };
- /**
- * Converts this Long to unsigned.
- * @returns {!Long} Unsigned long
- * @expose
- */
- Long.prototype.toUnsigned = function() {
- if (this.unsigned)
- return this;
- return new Long(this.low, this.high, true);
- };
- /* CommonJS */ if (typeof require === 'function' && typeof module === 'object' && module && typeof exports === 'object' && exports)
- module["exports"] = Long;
- /* AMD */ else if (typeof define === 'function' && define["amd"])
- define(function() { return Long; });
- /* Global */ else
- (global["dcodeIO"] = global["dcodeIO"] || {})["Long"] = Long;
- })(this);
- /*
- Copyright 2013-2014 Daniel Wirtz <dcode@dcode.io>
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- /**
- * @license ByteBuffer.js (c) 2013-2014 Daniel Wirtz <dcode@dcode.io>
- * This version of ByteBuffer.js uses an ArrayBuffer (AB) as its backing buffer and is compatible with modern browsers.
- * Released under the Apache License, Version 2.0
- * see: https://github.com/dcodeIO/ByteBuffer.js for details
- */ //
- (function(global) {
- "use strict";
- /**
- * @param {function(new: Long, number, number, boolean=)=} Long
- * @returns {function(new: ByteBuffer, number=, boolean=, boolean=)}}
- * @inner
- */
- function loadByteBuffer(Long) {
- /**
- * Constructs a new ByteBuffer.
- * @class The swiss army knife for binary data in JavaScript.
- * @exports ByteBuffer
- * @constructor
- * @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
- * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
- * {@link ByteBuffer.DEFAULT_ENDIAN}.
- * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
- * {@link ByteBuffer.DEFAULT_NOASSERT}.
- * @expose
- */
- var ByteBuffer = function(capacity, littleEndian, noAssert) {
- if (typeof capacity === 'undefined') capacity = ByteBuffer.DEFAULT_CAPACITY;
- if (typeof littleEndian === 'undefined') littleEndian = ByteBuffer.DEFAULT_ENDIAN;
- if (typeof noAssert === 'undefined') noAssert = ByteBuffer.DEFAULT_NOASSERT;
- if (!noAssert) {
- capacity = capacity | 0;
- if (capacity < 0)
- throw RangeError("Illegal capacity");
- littleEndian = !!littleEndian;
- noAssert = !!noAssert;
- }
- /**
- * Backing buffer.
- * @type {!ArrayBuffer}
- * @expose
- */
- this.buffer = capacity === 0 ? EMPTY_BUFFER : new ArrayBuffer(capacity);
- /**
- * Data view to manipulate the backing buffer. Becomes `null` if the backing buffer has a capacity of `0`.
- * @type {?DataView}
- * @expose
- */
- this.view = capacity === 0 ? null : new DataView(this.buffer);
- /**
- * Absolute read/write offset.
- * @type {number}
- * @expose
- * @see ByteBuffer#flip
- * @see ByteBuffer#clear
- */
- this.offset = 0;
- /**
- * Marked offset.
- * @type {number}
- * @expose
- * @see ByteBuffer#mark
- * @see ByteBuffer#reset
- */
- this.markedOffset = -1;
- /**
- * Absolute limit of the contained data. Set to the backing buffer's capacity upon allocation.
- * @type {number}
- * @expose
- * @see ByteBuffer#flip
- * @see ByteBuffer#clear
- */
- this.limit = capacity;
- /**
- * Whether to use little endian byte order, defaults to `false` for big endian.
- * @type {boolean}
- * @expose
- */
- this.littleEndian = typeof littleEndian !== 'undefined' ? !!littleEndian : false;
- /**
- * Whether to skip assertions of offsets and values, defaults to `false`.
- * @type {boolean}
- * @expose
- */
- this.noAssert = !!noAssert;
- };
- /**
- * ByteBuffer version.
- * @type {string}
- * @const
- * @expose
- */
- ByteBuffer.VERSION = "3.5.4";
- /**
- * Little endian constant that can be used instead of its boolean value. Evaluates to `true`.
- * @type {boolean}
- * @const
- * @expose
- */
- ByteBuffer.LITTLE_ENDIAN = true;
- /**
- * Big endian constant that can be used instead of its boolean value. Evaluates to `false`.
- * @type {boolean}
- * @const
- * @expose
- */
- ByteBuffer.BIG_ENDIAN = false;
- /**
- * Default initial capacity of `16`.
- * @type {number}
- * @expose
- */
- ByteBuffer.DEFAULT_CAPACITY = 16;
- /**
- * Default endianess of `false` for big endian.
- * @type {boolean}
- * @expose
- */
- ByteBuffer.DEFAULT_ENDIAN = ByteBuffer.BIG_ENDIAN;
- /**
- * Default no assertions flag of `false`.
- * @type {boolean}
- * @expose
- */
- ByteBuffer.DEFAULT_NOASSERT = false;
- /**
- * A `Long` class for representing a 64-bit two's-complement integer value. May be `null` if Long.js has not been loaded
- * and int64 support is not available.
- * @type {?Long}
- * @const
- * @see https://github.com/dcodeIO/Long.js
- * @expose
- */
- ByteBuffer.Long = Long || null;
- /**
- * @alias ByteBuffer.prototype
- * @inner
- */
- var ByteBufferPrototype = ByteBuffer.prototype;
- // helpers
- /**
- * @type {!ArrayBuffer}
- * @inner
- */
- var EMPTY_BUFFER = new ArrayBuffer(0);
- /**
- * String.fromCharCode reference for compile-time renaming.
- * @type {function(...number):string}
- * @inner
- */
- var stringFromCharCode = String.fromCharCode;
- /**
- * Creates a source function for a string.
- * @param {string} s String to read from
- * @returns {function():number|null} Source function returning the next char code respectively `null` if there are
- * no more characters left.
- * @throws {TypeError} If the argument is invalid
- * @inner
- */
- function stringSource(s) {
- var i=0; return function() {
- return i < s.length ? s.charCodeAt(i++) : null;
- };
- }
- /**
- * Creates a destination function for a string.
- * @returns {function(number=):undefined|string} Destination function successively called with the next char code.
- * Returns the final string when called without arguments.
- * @inner
- */
- function stringDestination() {
- var cs = [], ps = []; return function() {
- if (arguments.length === 0)
- return ps.join('')+stringFromCharCode.apply(String, cs);
- if (cs.length + arguments.length > 1024)
- ps.push(stringFromCharCode.apply(String, cs)),
- cs.length = 0;
- Array.prototype.push.apply(cs, arguments);
- };
- }
- /**
- * Allocates a new ByteBuffer backed by a buffer of the specified capacity.
- * @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
- * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
- * {@link ByteBuffer.DEFAULT_ENDIAN}.
- * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
- * {@link ByteBuffer.DEFAULT_NOASSERT}.
- * @returns {!ByteBuffer}
- * @expose
- */
- ByteBuffer.allocate = function(capacity, littleEndian, noAssert) {
- return new ByteBuffer(capacity, littleEndian, noAssert);
- };
- /**
- * Concatenates multiple ByteBuffers into one.
- * @param {!Array.<!ByteBuffer|!ArrayBuffer|!Uint8Array|string>} buffers Buffers to concatenate
- * @param {(string|boolean)=} encoding String encoding if `buffers` contains a string ("base64", "hex", "binary",
- * defaults to "utf8")
- * @param {boolean=} littleEndian Whether to use little or big endian byte order for the resulting ByteBuffer. Defaults
- * to {@link ByteBuffer.DEFAULT_ENDIAN}.
- * @param {boolean=} noAssert Whether to skip assertions of offsets and values for the resulting ByteBuffer. Defaults to
- * {@link ByteBuffer.DEFAULT_NOASSERT}.
- * @returns {!ByteBuffer} Concatenated ByteBuffer
- * @expose
- */
- ByteBuffer.concat = function(buffers, encoding, littleEndian, noAssert) {
- if (typeof encoding === 'boolean' || typeof encoding !== 'string') {
- noAssert = littleEndian;
- littleEndian = encoding;
- encoding = undefined;
- }
- var capacity = 0;
- for (var i=0, k=buffers.length, length; i<k; ++i) {
- if (!ByteBuffer.isByteBuffer(buffers[i]))
- buffers[i] = ByteBuffer.wrap(buffers[i], encoding);
- length = buffers[i].limit - buffers[i].offset;
- if (length > 0) capacity += length;
- }
- if (capacity === 0)
- return new ByteBuffer(0, littleEndian, noAssert);
- var bb = new ByteBuffer(capacity, littleEndian, noAssert),
- bi;
- var view = new Uint8Array(bb.buffer);
- i=0; while (i<k) {
- bi = buffers[i++];
- length = bi.limit - bi.offset;
- if (length <= 0) continue;
- view.set(new Uint8Array(bi.buffer).subarray(bi.offset, bi.limit), bb.offset);
- bb.offset += length;
- }
- bb.limit = bb.offset;
- bb.offset = 0;
- return bb;
- };
- /**
- * Tests if the specified type is a ByteBuffer.
- * @param {*} bb ByteBuffer to test
- * @returns {boolean} `true` if it is a ByteBuffer, otherwise `false`
- * @expose
- */
- ByteBuffer.isByteBuffer = function(bb) {
- return (bb && bb instanceof ByteBuffer) === true;
- };
- /**
- * Gets the backing buffer type.
- * @returns {Function} `Buffer` for NB builds, `ArrayBuffer` for AB builds (classes)
- * @expose
- */
- ByteBuffer.type = function() {
- return ArrayBuffer;
- };
- /**
- * Wraps a buffer or a string. Sets the allocated ByteBuffer's {@link ByteBuffer#offset} to `0` and its
- * {@link ByteBuffer#limit} to the length of the wrapped data.
- * @param {!ByteBuffer|!ArrayBuffer|!Uint8Array|string|!Array.<number>} buffer Anything that can be wrapped
- * @param {(string|boolean)=} encoding String encoding if `buffer` is a string ("base64", "hex", "binary", defaults to
- * "utf8")
- * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
- * {@link ByteBuffer.DEFAULT_ENDIAN}.
- * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
- * {@link ByteBuffer.DEFAULT_NOASSERT}.
- * @returns {!ByteBuffer} A ByteBuffer wrapping `buffer`
- * @expose
- */
- ByteBuffer.wrap = function(buffer, encoding, littleEndian, noAssert) {
- if (typeof encoding !== 'string') {
- noAssert = littleEndian;
- littleEndian = encoding;
- encoding = undefined;
- }
- if (typeof buffer === 'string') {
- if (typeof encoding === 'undefined')
- encoding = "utf8";
- switch (encoding) {
- case "base64":
- return ByteBuffer.fromBase64(buffer, littleEndian);
- case "hex":
- return ByteBuffer.fromHex(buffer, littleEndian);
- case "binary":
- return ByteBuffer.fromBinary(buffer, littleEndian);
- case "utf8":
- return ByteBuffer.fromUTF8(buffer, littleEndian);
- case "debug":
- return ByteBuffer.fromDebug(buffer, littleEndian);
- default:
- throw Error("Unsupported encoding: "+encoding);
- }
- }
- if (buffer === null || typeof buffer !== 'object')
- throw TypeError("Illegal buffer");
- var bb;
- if (ByteBuffer.isByteBuffer(buffer)) {
- bb = ByteBufferPrototype.clone.call(buffer);
- bb.markedOffset = -1;
- return bb;
- }
- if (buffer instanceof Uint8Array) { // Extract ArrayBuffer from Uint8Array
- bb = new ByteBuffer(0, littleEndian, noAssert);
- if (buffer.length > 0) { // Avoid references to more than one EMPTY_BUFFER
- bb.buffer = buffer.buffer;
- bb.offset = buffer.byteOffset;
- bb.limit = buffer.byteOffset + buffer.length;
- bb.view = buffer.length > 0 ? new DataView(buffer.buffer) : null;
- }
- } else if (buffer instanceof ArrayBuffer) { // Reuse ArrayBuffer
- bb = new ByteBuffer(0, littleEndian, noAssert);
- if (buffer.byteLength > 0) {
- bb.buffer = buffer;
- bb.offset = 0;
- bb.limit = buffer.byteLength;
- bb.view = buffer.byteLength > 0 ? new DataView(buffer) : null;
- }
- } else if (Object.prototype.toString.call(buffer) === "[object Array]") { // Create from octets
- bb = new ByteBuffer(buffer.length, littleEndian, noAssert);
- bb.limit = buffer.length;
- for (i=0; i<buffer.length; ++i)
- bb.view.setUint8(i, buffer[i]);
- } else
- throw TypeError("Illegal buffer"); // Otherwise fail
- return bb;
- };
- // types/ints/int8
- /**
- * Writes an 8bit signed integer.
- * @param {number} value Value to write
- * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.writeInt8 = function(value, offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof value !== 'number' || value % 1 !== 0)
- throw TypeError("Illegal value: "+value+" (not an integer)");
- value |= 0;
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- offset += 1;
- var capacity0 = this.buffer.byteLength;
- if (offset > capacity0)
- this.resize((capacity0 *= 2) > offset ? capacity0 : offset);
- offset -= 1;
- this.view.setInt8(offset, value);
- if (relative) this.offset += 1;
- return this;
- };
- /**
- * Writes an 8bit signed integer. This is an alias of {@link ByteBuffer#writeInt8}.
- * @function
- * @param {number} value Value to write
- * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.writeByte = ByteBufferPrototype.writeInt8;
- /**
- * Reads an 8bit signed integer.
- * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
- * @returns {number} Value read
- * @expose
- */
- ByteBufferPrototype.readInt8 = function(offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 1 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
- }
- var value = this.view.getInt8(offset);
- if (relative) this.offset += 1;
- return value;
- };
- /**
- * Reads an 8bit signed integer. This is an alias of {@link ByteBuffer#readInt8}.
- * @function
- * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
- * @returns {number} Value read
- * @expose
- */
- ByteBufferPrototype.readByte = ByteBufferPrototype.readInt8;
- /**
- * Writes an 8bit unsigned integer.
- * @param {number} value Value to write
- * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.writeUint8 = function(value, offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof value !== 'number' || value % 1 !== 0)
- throw TypeError("Illegal value: "+value+" (not an integer)");
- value >>>= 0;
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- offset += 1;
- var capacity1 = this.buffer.byteLength;
- if (offset > capacity1)
- this.resize((capacity1 *= 2) > offset ? capacity1 : offset);
- offset -= 1;
- this.view.setUint8(offset, value);
- if (relative) this.offset += 1;
- return this;
- };
- /**
- * Reads an 8bit unsigned integer.
- * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `1` if omitted.
- * @returns {number} Value read
- * @expose
- */
- ByteBufferPrototype.readUint8 = function(offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 1 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
- }
- var value = this.view.getUint8(offset);
- if (relative) this.offset += 1;
- return value;
- };
- // types/ints/int16
- /**
- * Writes a 16bit signed integer.
- * @param {number} value Value to write
- * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
- * @throws {TypeError} If `offset` or `value` is not a valid number
- * @throws {RangeError} If `offset` is out of bounds
- * @expose
- */
- ByteBufferPrototype.writeInt16 = function(value, offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof value !== 'number' || value % 1 !== 0)
- throw TypeError("Illegal value: "+value+" (not an integer)");
- value |= 0;
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- offset += 2;
- var capacity2 = this.buffer.byteLength;
- if (offset > capacity2)
- this.resize((capacity2 *= 2) > offset ? capacity2 : offset);
- offset -= 2;
- this.view.setInt16(offset, value, this.littleEndian);
- if (relative) this.offset += 2;
- return this;
- };
- /**
- * Writes a 16bit signed integer. This is an alias of {@link ByteBuffer#writeInt16}.
- * @function
- * @param {number} value Value to write
- * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
- * @throws {TypeError} If `offset` or `value` is not a valid number
- * @throws {RangeError} If `offset` is out of bounds
- * @expose
- */
- ByteBufferPrototype.writeShort = ByteBufferPrototype.writeInt16;
- /**
- * Reads a 16bit signed integer.
- * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
- * @returns {number} Value read
- * @throws {TypeError} If `offset` is not a valid number
- * @throws {RangeError} If `offset` is out of bounds
- * @expose
- */
- ByteBufferPrototype.readInt16 = function(offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 2 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+2+") <= "+this.buffer.byteLength);
- }
- var value = this.view.getInt16(offset, this.littleEndian);
- if (relative) this.offset += 2;
- return value;
- };
- /**
- * Reads a 16bit signed integer. This is an alias of {@link ByteBuffer#readInt16}.
- * @function
- * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
- * @returns {number} Value read
- * @throws {TypeError} If `offset` is not a valid number
- * @throws {RangeError} If `offset` is out of bounds
- * @expose
- */
- ByteBufferPrototype.readShort = ByteBufferPrototype.readInt16;
- /**
- * Writes a 16bit unsigned integer.
- * @param {number} value Value to write
- * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
- * @throws {TypeError} If `offset` or `value` is not a valid number
- * @throws {RangeError} If `offset` is out of bounds
- * @expose
- */
- ByteBufferPrototype.writeUint16 = function(value, offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof value !== 'number' || value % 1 !== 0)
- throw TypeError("Illegal value: "+value+" (not an integer)");
- value >>>= 0;
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- offset += 2;
- var capacity3 = this.buffer.byteLength;
- if (offset > capacity3)
- this.resize((capacity3 *= 2) > offset ? capacity3 : offset);
- offset -= 2;
- this.view.setUint16(offset, value, this.littleEndian);
- if (relative) this.offset += 2;
- return this;
- };
- /**
- * Reads a 16bit unsigned integer.
- * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `2` if omitted.
- * @returns {number} Value read
- * @throws {TypeError} If `offset` is not a valid number
- * @throws {RangeError} If `offset` is out of bounds
- * @expose
- */
- ByteBufferPrototype.readUint16 = function(offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 2 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+2+") <= "+this.buffer.byteLength);
- }
- var value = this.view.getUint16(offset, this.littleEndian);
- if (relative) this.offset += 2;
- return value;
- };
- // types/ints/int32
- /**
- * Writes a 32bit signed integer.
- * @param {number} value Value to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
- * @expose
- */
- ByteBufferPrototype.writeInt32 = function(value, offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof value !== 'number' || value % 1 !== 0)
- throw TypeError("Illegal value: "+value+" (not an integer)");
- value |= 0;
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- offset += 4;
- var capacity4 = this.buffer.byteLength;
- if (offset > capacity4)
- this.resize((capacity4 *= 2) > offset ? capacity4 : offset);
- offset -= 4;
- this.view.setInt32(offset, value, this.littleEndian);
- if (relative) this.offset += 4;
- return this;
- };
- /**
- * Writes a 32bit signed integer. This is an alias of {@link ByteBuffer#writeInt32}.
- * @param {number} value Value to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
- * @expose
- */
- ByteBufferPrototype.writeInt = ByteBufferPrototype.writeInt32;
- /**
- * Reads a 32bit signed integer.
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
- * @returns {number} Value read
- * @expose
- */
- ByteBufferPrototype.readInt32 = function(offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 4 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+4+") <= "+this.buffer.byteLength);
- }
- var value = this.view.getInt32(offset, this.littleEndian);
- if (relative) this.offset += 4;
- return value;
- };
- /**
- * Reads a 32bit signed integer. This is an alias of {@link ByteBuffer#readInt32}.
- * @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} by `4` if omitted.
- * @returns {number} Value read
- * @expose
- */
- ByteBufferPrototype.readInt = ByteBufferPrototype.readInt32;
- /**
- * Writes a 32bit unsigned integer.
- * @param {number} value Value to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
- * @expose
- */
- ByteBufferPrototype.writeUint32 = function(value, offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof value !== 'number' || value % 1 !== 0)
- throw TypeError("Illegal value: "+value+" (not an integer)");
- value >>>= 0;
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- offset += 4;
- var capacity5 = this.buffer.byteLength;
- if (offset > capacity5)
- this.resize((capacity5 *= 2) > offset ? capacity5 : offset);
- offset -= 4;
- this.view.setUint32(offset, value, this.littleEndian);
- if (relative) this.offset += 4;
- return this;
- };
- /**
- * Reads a 32bit unsigned integer.
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
- * @returns {number} Value read
- * @expose
- */
- ByteBufferPrototype.readUint32 = function(offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 4 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+4+") <= "+this.buffer.byteLength);
- }
- var value = this.view.getUint32(offset, this.littleEndian);
- if (relative) this.offset += 4;
- return value;
- };
- // types/ints/int64
- if (Long) {
- /**
- * Writes a 64bit signed integer.
- * @param {number|!Long} value Value to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.writeInt64 = function(value, offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof value === 'number')
- value = Long.fromNumber(value);
- else if (!(value && value instanceof Long))
- throw TypeError("Illegal value: "+value+" (not an integer or Long)");
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- if (typeof value === 'number')
- value = Long.fromNumber(value);
- offset += 8;
- var capacity6 = this.buffer.byteLength;
- if (offset > capacity6)
- this.resize((capacity6 *= 2) > offset ? capacity6 : offset);
- offset -= 8;
- if (this.littleEndian) {
- this.view.setInt32(offset , value.low , true);
- this.view.setInt32(offset+4, value.high, true);
- } else {
- this.view.setInt32(offset , value.high, false);
- this.view.setInt32(offset+4, value.low , false);
- }
- if (relative) this.offset += 8;
- return this;
- };
- /**
- * Writes a 64bit signed integer. This is an alias of {@link ByteBuffer#writeInt64}.
- * @param {number|!Long} value Value to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.writeLong = ByteBufferPrototype.writeInt64;
- /**
- * Reads a 64bit signed integer.
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
- * @returns {!Long}
- * @expose
- */
- ByteBufferPrototype.readInt64 = function(offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 8 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+8+") <= "+this.buffer.byteLength);
- }
- var value = this.littleEndian
- ? new Long(this.view.getInt32(offset , true ), this.view.getInt32(offset+4, true ), false)
- : new Long(this.view.getInt32(offset+4, false), this.view.getInt32(offset , false), false);
- if (relative) this.offset += 8;
- return value;
- };
- /**
- * Reads a 64bit signed integer. This is an alias of {@link ByteBuffer#readInt64}.
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
- * @returns {!Long}
- * @expose
- */
- ByteBufferPrototype.readLong = ByteBufferPrototype.readInt64;
- /**
- * Writes a 64bit unsigned integer.
- * @param {number|!Long} value Value to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.writeUint64 = function(value, offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof value === 'number')
- value = Long.fromNumber(value);
- else if (!(value && value instanceof Long))
- throw TypeError("Illegal value: "+value+" (not an integer or Long)");
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- if (typeof value === 'number')
- value = Long.fromNumber(value);
- offset += 8;
- var capacity7 = this.buffer.byteLength;
- if (offset > capacity7)
- this.resize((capacity7 *= 2) > offset ? capacity7 : offset);
- offset -= 8;
- if (this.littleEndian) {
- this.view.setInt32(offset , value.low , true);
- this.view.setInt32(offset+4, value.high, true);
- } else {
- this.view.setInt32(offset , value.high, false);
- this.view.setInt32(offset+4, value.low , false);
- }
- if (relative) this.offset += 8;
- return this;
- };
- /**
- * Reads a 64bit unsigned integer.
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
- * @returns {!Long}
- * @expose
- */
- ByteBufferPrototype.readUint64 = function(offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 8 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+8+") <= "+this.buffer.byteLength);
- }
- var value = this.littleEndian
- ? new Long(this.view.getInt32(offset , true ), this.view.getInt32(offset+4, true ), true)
- : new Long(this.view.getInt32(offset+4, false), this.view.getInt32(offset , false), true);
- if (relative) this.offset += 8;
- return value;
- };
- } // Long
- // types/floats/float32
- /**
- * Writes a 32bit float.
- * @param {number} value Value to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.writeFloat32 = function(value, offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof value !== 'number')
- throw TypeError("Illegal value: "+value+" (not a number)");
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- offset += 4;
- var capacity8 = this.buffer.byteLength;
- if (offset > capacity8)
- this.resize((capacity8 *= 2) > offset ? capacity8 : offset);
- offset -= 4;
- this.view.setFloat32(offset, value, this.littleEndian);
- if (relative) this.offset += 4;
- return this;
- };
- /**
- * Writes a 32bit float. This is an alias of {@link ByteBuffer#writeFloat32}.
- * @function
- * @param {number} value Value to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.writeFloat = ByteBufferPrototype.writeFloat32;
- /**
- * Reads a 32bit float.
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
- * @returns {number}
- * @expose
- */
- ByteBufferPrototype.readFloat32 = function(offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 4 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+4+") <= "+this.buffer.byteLength);
- }
- var value = this.view.getFloat32(offset, this.littleEndian);
- if (relative) this.offset += 4;
- return value;
- };
- /**
- * Reads a 32bit float. This is an alias of {@link ByteBuffer#readFloat32}.
- * @function
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `4` if omitted.
- * @returns {number}
- * @expose
- */
- ByteBufferPrototype.readFloat = ByteBufferPrototype.readFloat32;
- // types/floats/float64
- /**
- * Writes a 64bit float.
- * @param {number} value Value to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.writeFloat64 = function(value, offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof value !== 'number')
- throw TypeError("Illegal value: "+value+" (not a number)");
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- offset += 8;
- var capacity9 = this.buffer.byteLength;
- if (offset > capacity9)
- this.resize((capacity9 *= 2) > offset ? capacity9 : offset);
- offset -= 8;
- this.view.setFloat64(offset, value, this.littleEndian);
- if (relative) this.offset += 8;
- return this;
- };
- /**
- * Writes a 64bit float. This is an alias of {@link ByteBuffer#writeFloat64}.
- * @function
- * @param {number} value Value to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.writeDouble = ByteBufferPrototype.writeFloat64;
- /**
- * Reads a 64bit float.
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
- * @returns {number}
- * @expose
- */
- ByteBufferPrototype.readFloat64 = function(offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 8 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+8+") <= "+this.buffer.byteLength);
- }
- var value = this.view.getFloat64(offset, this.littleEndian);
- if (relative) this.offset += 8;
- return value;
- };
- /**
- * Reads a 64bit float. This is an alias of {@link ByteBuffer#readFloat64}.
- * @function
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `8` if omitted.
- * @returns {number}
- * @expose
- */
- ByteBufferPrototype.readDouble = ByteBufferPrototype.readFloat64;
- // types/varints/varint32
- /**
- * Maximum number of bytes required to store a 32bit base 128 variable-length integer.
- * @type {number}
- * @const
- * @expose
- */
- ByteBuffer.MAX_VARINT32_BYTES = 5;
- /**
- * Calculates the actual number of bytes required to store a 32bit base 128 variable-length integer.
- * @param {number} value Value to encode
- * @returns {number} Number of bytes required. Capped to {@link ByteBuffer.MAX_VARINT32_BYTES}
- * @expose
- */
- ByteBuffer.calculateVarint32 = function(value) {
- // ref: src/google/protobuf/io/coded_stream.cc
- value = value >>> 0;
- if (value < 1 << 7 ) return 1;
- else if (value < 1 << 14) return 2;
- else if (value < 1 << 21) return 3;
- else if (value < 1 << 28) return 4;
- else return 5;
- };
- /**
- * Zigzag encodes a signed 32bit integer so that it can be effectively used with varint encoding.
- * @param {number} n Signed 32bit integer
- * @returns {number} Unsigned zigzag encoded 32bit integer
- * @expose
- */
- ByteBuffer.zigZagEncode32 = function(n) {
- return (((n |= 0) << 1) ^ (n >> 31)) >>> 0; // ref: src/google/protobuf/wire_format_lite.h
- };
- /**
- * Decodes a zigzag encoded signed 32bit integer.
- * @param {number} n Unsigned zigzag encoded 32bit integer
- * @returns {number} Signed 32bit integer
- * @expose
- */
- ByteBuffer.zigZagDecode32 = function(n) {
- return ((n >>> 1) ^ -(n & 1)) | 0; // // ref: src/google/protobuf/wire_format_lite.h
- };
- /**
- * Writes a 32bit base 128 variable-length integer.
- * @param {number} value Value to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * written if omitted.
- * @returns {!ByteBuffer|number} this if `offset` is omitted, else the actual number of bytes written
- * @expose
- */
- ByteBufferPrototype.writeVarint32 = function(value, offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof value !== 'number' || value % 1 !== 0)
- throw TypeError("Illegal value: "+value+" (not an integer)");
- value |= 0;
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- var size = ByteBuffer.calculateVarint32(value),
- b;
- offset += size;
- var capacity10 = this.buffer.byteLength;
- if (offset > capacity10)
- this.resize((capacity10 *= 2) > offset ? capacity10 : offset);
- offset -= size;
- // ref: http://code.google.com/searchframe#WTeibokF6gE/trunk/src/google/protobuf/io/coded_stream.cc
- this.view.setUint8(offset, b = value | 0x80);
- value >>>= 0;
- if (value >= 1 << 7) {
- b = (value >> 7) | 0x80;
- this.view.setUint8(offset+1, b);
- if (value >= 1 << 14) {
- b = (value >> 14) | 0x80;
- this.view.setUint8(offset+2, b);
- if (value >= 1 << 21) {
- b = (value >> 21) | 0x80;
- this.view.setUint8(offset+3, b);
- if (value >= 1 << 28) {
- this.view.setUint8(offset+4, (value >> 28) & 0x0F);
- size = 5;
- } else {
- this.view.setUint8(offset+3, b & 0x7F);
- size = 4;
- }
- } else {
- this.view.setUint8(offset+2, b & 0x7F);
- size = 3;
- }
- } else {
- this.view.setUint8(offset+1, b & 0x7F);
- size = 2;
- }
- } else {
- this.view.setUint8(offset, b & 0x7F);
- size = 1;
- }
- if (relative) {
- this.offset += size;
- return this;
- }
- return size;
- };
- /**
- * Writes a zig-zag encoded 32bit base 128 variable-length integer.
- * @param {number} value Value to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * written if omitted.
- * @returns {!ByteBuffer|number} this if `offset` is omitted, else the actual number of bytes written
- * @expose
- */
- ByteBufferPrototype.writeVarint32ZigZag = function(value, offset) {
- return this.writeVarint32(ByteBuffer.zigZagEncode32(value), offset);
- };
- /**
- * Reads a 32bit base 128 variable-length integer.
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * written if omitted.
- * @returns {number|!{value: number, length: number}} The value read if offset is omitted, else the value read
- * and the actual number of bytes read.
- * @throws {Error} If it's not a valid varint. Has a property `truncated = true` if there is not enough data available
- * to fully decode the varint.
- * @expose
- */
- ByteBufferPrototype.readVarint32 = function(offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 1 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
- }
- // ref: src/google/protobuf/io/coded_stream.cc
- var size = 0,
- value = 0 >>> 0,
- temp,
- ioffset;
- do {
- ioffset = offset+size;
- if (!this.noAssert && ioffset > this.limit) {
- var err = Error("Truncated");
- err['truncated'] = true;
- throw err;
- }
- temp = this.view.getUint8(ioffset);
- if (size < 5)
- value |= ((temp&0x7F)<<(7*size)) >>> 0;
- ++size;
- } while ((temp & 0x80) === 0x80);
- value = value | 0; // Make sure to discard the higher order bits
- if (relative) {
- this.offset += size;
- return value;
- }
- return {
- "value": value,
- "length": size
- };
- };
- /**
- * Reads a zig-zag encoded 32bit base 128 variable-length integer.
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * written if omitted.
- * @returns {number|!{value: number, length: number}} The value read if offset is omitted, else the value read
- * and the actual number of bytes read.
- * @throws {Error} If it's not a valid varint
- * @expose
- */
- ByteBufferPrototype.readVarint32ZigZag = function(offset) {
- var val = this.readVarint32(offset);
- if (typeof val === 'object')
- val["value"] = ByteBuffer.zigZagDecode32(val["value"]);
- else
- val = ByteBuffer.zigZagDecode32(val);
- return val;
- };
- // types/varints/varint64
- if (Long) {
- /**
- * Maximum number of bytes required to store a 64bit base 128 variable-length integer.
- * @type {number}
- * @const
- * @expose
- */
- ByteBuffer.MAX_VARINT64_BYTES = 10;
- /**
- * Calculates the actual number of bytes required to store a 64bit base 128 variable-length integer.
- * @param {number|!Long} value Value to encode
- * @returns {number} Number of bytes required. Capped to {@link ByteBuffer.MAX_VARINT64_BYTES}
- * @expose
- */
- ByteBuffer.calculateVarint64 = function(value) {
- if (typeof value === 'number')
- value = Long.fromNumber(value);
- // ref: src/google/protobuf/io/coded_stream.cc
- var part0 = value.toInt() >>> 0,
- part1 = value.shiftRightUnsigned(28).toInt() >>> 0,
- part2 = value.shiftRightUnsigned(56).toInt() >>> 0;
- if (part2 == 0) {
- if (part1 == 0) {
- if (part0 < 1 << 14)
- return part0 < 1 << 7 ? 1 : 2;
- else
- return part0 < 1 << 21 ? 3 : 4;
- } else {
- if (part1 < 1 << 14)
- return part1 < 1 << 7 ? 5 : 6;
- else
- return part1 < 1 << 21 ? 7 : 8;
- }
- } else
- return part2 < 1 << 7 ? 9 : 10;
- };
- /**
- * Zigzag encodes a signed 64bit integer so that it can be effectively used with varint encoding.
- * @param {number|!Long} value Signed long
- * @returns {!Long} Unsigned zigzag encoded long
- * @expose
- */
- ByteBuffer.zigZagEncode64 = function(value) {
- if (typeof value === 'number')
- value = Long.fromNumber(value, false);
- else if (value.unsigned !== false) value = value.toSigned();
- // ref: src/google/protobuf/wire_format_lite.h
- return value.shiftLeft(1).xor(value.shiftRight(63)).toUnsigned();
- };
- /**
- * Decodes a zigzag encoded signed 64bit integer.
- * @param {!Long|number} value Unsigned zigzag encoded long or JavaScript number
- * @returns {!Long} Signed long
- * @expose
- */
- ByteBuffer.zigZagDecode64 = function(value) {
- if (typeof value === 'number')
- value = Long.fromNumber(value, false);
- else if (value.unsigned !== false) value = value.toSigned();
- // ref: src/google/protobuf/wire_format_lite.h
- return value.shiftRightUnsigned(1).xor(value.and(Long.ONE).toSigned().negate()).toSigned();
- };
- /**
- * Writes a 64bit base 128 variable-length integer.
- * @param {number|Long} value Value to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * written if omitted.
- * @returns {!ByteBuffer|number} `this` if offset is omitted, else the actual number of bytes written.
- * @expose
- */
- ByteBufferPrototype.writeVarint64 = function(value, offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof value === 'number')
- value = Long.fromNumber(value);
- else if (!(value && value instanceof Long))
- throw TypeError("Illegal value: "+value+" (not an integer or Long)");
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- if (typeof value === 'number')
- value = Long.fromNumber(value, false);
- else if (value.unsigned !== false) value = value.toSigned();
- var size = ByteBuffer.calculateVarint64(value),
- part0 = value.toInt() >>> 0,
- part1 = value.shiftRightUnsigned(28).toInt() >>> 0,
- part2 = value.shiftRightUnsigned(56).toInt() >>> 0;
- offset += size;
- var capacity11 = this.buffer.byteLength;
- if (offset > capacity11)
- this.resize((capacity11 *= 2) > offset ? capacity11 : offset);
- offset -= size;
- switch (size) {
- case 10: this.view.setUint8(offset+9, (part2 >>> 7) & 0x01);
- case 9 : this.view.setUint8(offset+8, size !== 9 ? (part2 ) | 0x80 : (part2 ) & 0x7F);
- case 8 : this.view.setUint8(offset+7, size !== 8 ? (part1 >>> 21) | 0x80 : (part1 >>> 21) & 0x7F);
- case 7 : this.view.setUint8(offset+6, size !== 7 ? (part1 >>> 14) | 0x80 : (part1 >>> 14) & 0x7F);
- case 6 : this.view.setUint8(offset+5, size !== 6 ? (part1 >>> 7) | 0x80 : (part1 >>> 7) & 0x7F);
- case 5 : this.view.setUint8(offset+4, size !== 5 ? (part1 ) | 0x80 : (part1 ) & 0x7F);
- case 4 : this.view.setUint8(offset+3, size !== 4 ? (part0 >>> 21) | 0x80 : (part0 >>> 21) & 0x7F);
- case 3 : this.view.setUint8(offset+2, size !== 3 ? (part0 >>> 14) | 0x80 : (part0 >>> 14) & 0x7F);
- case 2 : this.view.setUint8(offset+1, size !== 2 ? (part0 >>> 7) | 0x80 : (part0 >>> 7) & 0x7F);
- case 1 : this.view.setUint8(offset , size !== 1 ? (part0 ) | 0x80 : (part0 ) & 0x7F);
- }
- if (relative) {
- this.offset += size;
- return this;
- } else {
- return size;
- }
- };
- /**
- * Writes a zig-zag encoded 64bit base 128 variable-length integer.
- * @param {number|Long} value Value to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * written if omitted.
- * @returns {!ByteBuffer|number} `this` if offset is omitted, else the actual number of bytes written.
- * @expose
- */
- ByteBufferPrototype.writeVarint64ZigZag = function(value, offset) {
- return this.writeVarint64(ByteBuffer.zigZagEncode64(value), offset);
- };
- /**
- * Reads a 64bit base 128 variable-length integer. Requires Long.js.
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * read if omitted.
- * @returns {!Long|!{value: Long, length: number}} The value read if offset is omitted, else the value read and
- * the actual number of bytes read.
- * @throws {Error} If it's not a valid varint
- * @expose
- */
- ByteBufferPrototype.readVarint64 = function(offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 1 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
- }
- // ref: src/google/protobuf/io/coded_stream.cc
- var start = offset,
- part0 = 0,
- part1 = 0,
- part2 = 0,
- b = 0;
- b = this.view.getUint8(offset++); part0 = (b & 0x7F) ; if (b & 0x80) {
- b = this.view.getUint8(offset++); part0 |= (b & 0x7F) << 7; if (b & 0x80) {
- b = this.view.getUint8(offset++); part0 |= (b & 0x7F) << 14; if (b & 0x80) {
- b = this.view.getUint8(offset++); part0 |= (b & 0x7F) << 21; if (b & 0x80) {
- b = this.view.getUint8(offset++); part1 = (b & 0x7F) ; if (b & 0x80) {
- b = this.view.getUint8(offset++); part1 |= (b & 0x7F) << 7; if (b & 0x80) {
- b = this.view.getUint8(offset++); part1 |= (b & 0x7F) << 14; if (b & 0x80) {
- b = this.view.getUint8(offset++); part1 |= (b & 0x7F) << 21; if (b & 0x80) {
- b = this.view.getUint8(offset++); part2 = (b & 0x7F) ; if (b & 0x80) {
- b = this.view.getUint8(offset++); part2 |= (b & 0x7F) << 7; if (b & 0x80) {
- throw Error("Buffer overrun"); }}}}}}}}}}
- var value = Long.fromBits(part0 | (part1 << 28), (part1 >>> 4) | (part2) << 24, false);
- if (relative) {
- this.offset = offset;
- return value;
- } else {
- return {
- 'value': value,
- 'length': offset-start
- };
- }
- };
- /**
- * Reads a zig-zag encoded 64bit base 128 variable-length integer. Requires Long.js.
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * read if omitted.
- * @returns {!Long|!{value: Long, length: number}} The value read if offset is omitted, else the value read and
- * the actual number of bytes read.
- * @throws {Error} If it's not a valid varint
- * @expose
- */
- ByteBufferPrototype.readVarint64ZigZag = function(offset) {
- var val = this.readVarint64(offset);
- if (val && val['value'] instanceof Long)
- val["value"] = ByteBuffer.zigZagDecode64(val["value"]);
- else
- val = ByteBuffer.zigZagDecode64(val);
- return val;
- };
- } // Long
- // types/strings/cstring
- /**
- * Writes a NULL-terminated UTF8 encoded string. For this to work the specified string must not contain any NULL
- * characters itself.
- * @param {string} str String to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * contained in `str` + 1 if omitted.
- * @returns {!ByteBuffer|number} this if offset is omitted, else the actual number of bytes written
- * @expose
- */
- ByteBufferPrototype.writeCString = function(str, offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- var i,
- k = str.length;
- if (!this.noAssert) {
- if (typeof str !== 'string')
- throw TypeError("Illegal str: Not a string");
- for (i=0; i<k; ++i) {
- if (str.charCodeAt(i) === 0)
- throw RangeError("Illegal str: Contains NULL-characters");
- }
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- var start = offset;
- // UTF8 strings do not contain zero bytes in between except for the zero character, so:
- k = utfx.calculateUTF16asUTF8(stringSource(str))[1];
- offset += k+1;
- var capacity12 = this.buffer.byteLength;
- if (offset > capacity12)
- this.resize((capacity12 *= 2) > offset ? capacity12 : offset);
- offset -= k+1;
- utfx.encodeUTF16toUTF8(stringSource(str), function(b) {
- this.view.setUint8(offset++, b);
- }.bind(this));
- this.view.setUint8(offset++, 0);
- if (relative) {
- this.offset = offset - start;
- return this;
- }
- return k;
- };
- /**
- * Reads a NULL-terminated UTF8 encoded string. For this to work the string read must not contain any NULL characters
- * itself.
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * read if omitted.
- * @returns {string|!{string: string, length: number}} The string read if offset is omitted, else the string
- * read and the actual number of bytes read.
- * @expose
- */
- ByteBufferPrototype.readCString = function(offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 1 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
- }
- var start = offset,
- temp;
- // UTF8 strings do not contain zero bytes in between except for the zero character itself, so:
- var sd, b = -1;
- utfx.decodeUTF8toUTF16(function() {
- if (b === 0) return null;
- if (offset >= this.limit)
- throw RangeError("Illegal range: Truncated data, "+offset+" < "+this.limit);
- return (b = this.view.getUint8(offset++)) === 0 ? null : b;
- }.bind(this), sd = stringDestination(), true);
- if (relative) {
- this.offset = offset;
- return sd();
- } else {
- return {
- "string": sd(),
- "length": offset - start
- };
- }
- };
- // types/strings/istring
- /**
- * Writes a length as uint32 prefixed UTF8 encoded string.
- * @param {string} str String to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * written if omitted.
- * @returns {!ByteBuffer|number} `this` if `offset` is omitted, else the actual number of bytes written
- * @expose
- * @see ByteBuffer#writeVarint32
- */
- ByteBufferPrototype.writeIString = function(str, offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof str !== 'string')
- throw TypeError("Illegal str: Not a string");
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- var start = offset,
- k;
- k = utfx.calculateUTF16asUTF8(stringSource(str), this.noAssert)[1];
- offset += 4+k;
- var capacity13 = this.buffer.byteLength;
- if (offset > capacity13)
- this.resize((capacity13 *= 2) > offset ? capacity13 : offset);
- offset -= 4+k;
- this.view.setUint32(offset, k, this.littleEndian);
- offset += 4;
- utfx.encodeUTF16toUTF8(stringSource(str), function(b) {
- this.view.setUint8(offset++, b);
- }.bind(this));
- if (offset !== start + 4 + k)
- throw RangeError("Illegal range: Truncated data, "+offset+" == "+(offset+4+k));
- if (relative) {
- this.offset = offset;
- return this;
- }
- return offset - start;
- };
- /**
- * Reads a length as uint32 prefixed UTF8 encoded string.
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * read if omitted.
- * @returns {string|!{string: string, length: number}} The string read if offset is omitted, else the string
- * read and the actual number of bytes read.
- * @expose
- * @see ByteBuffer#readVarint32
- */
- ByteBufferPrototype.readIString = function(offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 4 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+4+") <= "+this.buffer.byteLength);
- }
- var temp = 0,
- start = offset,
- str;
- temp = this.view.getUint32(offset, this.littleEndian);
- offset += 4;
- var k = offset + temp,
- sd;
- utfx.decodeUTF8toUTF16(function() {
- return offset < k ? this.view.getUint8(offset++) : null;
- }.bind(this), sd = stringDestination(), this.noAssert);
- str = sd();
- if (relative) {
- this.offset = offset;
- return str;
- } else {
- return {
- 'string': str,
- 'length': offset - start
- };
- }
- };
- // types/strings/utf8string
- /**
- * Metrics representing number of UTF8 characters. Evaluates to `c`.
- * @type {string}
- * @const
- * @expose
- */
- ByteBuffer.METRICS_CHARS = 'c';
- /**
- * Metrics representing number of bytes. Evaluates to `b`.
- * @type {string}
- * @const
- * @expose
- */
- ByteBuffer.METRICS_BYTES = 'b';
- /**
- * Writes an UTF8 encoded string.
- * @param {string} str String to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} if omitted.
- * @returns {!ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
- * @expose
- */
- ByteBufferPrototype.writeUTF8String = function(str, offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- var k;
- var start = offset;
- k = utfx.calculateUTF16asUTF8(stringSource(str))[1];
- offset += k;
- var capacity14 = this.buffer.byteLength;
- if (offset > capacity14)
- this.resize((capacity14 *= 2) > offset ? capacity14 : offset);
- offset -= k;
- utfx.encodeUTF16toUTF8(stringSource(str), function(b) {
- this.view.setUint8(offset++, b);
- }.bind(this));
- if (relative) {
- this.offset = offset;
- return this;
- }
- return offset - start;
- };
- /**
- * Writes an UTF8 encoded string. This is an alias of {@link ByteBuffer#writeUTF8String}.
- * @function
- * @param {string} str String to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} if omitted.
- * @returns {!ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
- * @expose
- */
- ByteBufferPrototype.writeString = ByteBufferPrototype.writeUTF8String;
- /**
- * Calculates the number of UTF8 characters of a string. JavaScript itself uses UTF-16, so that a string's
- * `length` property does not reflect its actual UTF8 size if it contains code points larger than 0xFFFF.
- * @function
- * @param {string} str String to calculate
- * @returns {number} Number of UTF8 characters
- * @expose
- */
- ByteBuffer.calculateUTF8Chars = function(str) {
- return utfx.calculateUTF16asUTF8(stringSource(str))[0];
- };
- /**
- * Calculates the number of UTF8 bytes of a string.
- * @function
- * @param {string} str String to calculate
- * @returns {number} Number of UTF8 bytes
- * @expose
- */
- ByteBuffer.calculateUTF8Bytes = function(str) {
- return utfx.calculateUTF16asUTF8(stringSource(str))[1];
- };
- /**
- * Reads an UTF8 encoded string.
- * @param {number} length Number of characters or bytes to read.
- * @param {string=} metrics Metrics specifying what `length` is meant to count. Defaults to
- * {@link ByteBuffer.METRICS_CHARS}.
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * read if omitted.
- * @returns {string|!{string: string, length: number}} The string read if offset is omitted, else the string
- * read and the actual number of bytes read.
- * @expose
- */
- ByteBufferPrototype.readUTF8String = function(length, metrics, offset) {
- if (typeof metrics === 'number') {
- offset = metrics;
- metrics = undefined;
- }
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (typeof metrics === 'undefined') metrics = ByteBuffer.METRICS_CHARS;
- if (!this.noAssert) {
- if (typeof length !== 'number' || length % 1 !== 0)
- throw TypeError("Illegal length: "+length+" (not an integer)");
- length |= 0;
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- var i = 0,
- start = offset,
- sd;
- if (metrics === ByteBuffer.METRICS_CHARS) { // The same for node and the browser
- sd = stringDestination();
- utfx.decodeUTF8(function() {
- return i < length && offset < this.limit ? this.view.getUint8(offset++) : null;
- }.bind(this), function(cp) {
- ++i; utfx.UTF8toUTF16(cp, sd);
- }.bind(this));
- if (i !== length)
- throw RangeError("Illegal range: Truncated data, "+i+" == "+length);
- if (relative) {
- this.offset = offset;
- return sd();
- } else {
- return {
- "string": sd(),
- "length": offset - start
- };
- }
- } else if (metrics === ByteBuffer.METRICS_BYTES) {
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + length > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+length+") <= "+this.buffer.byteLength);
- }
- var k = offset + length;
- utfx.decodeUTF8toUTF16(function() {
- return offset < k ? this.view.getUint8(offset++) : null;
- }.bind(this), sd = stringDestination(), this.noAssert);
- if (offset !== k)
- throw RangeError("Illegal range: Truncated data, "+offset+" == "+k);
- if (relative) {
- this.offset = offset;
- return sd();
- } else {
- return {
- 'string': sd(),
- 'length': offset - start
- };
- }
- } else
- throw TypeError("Unsupported metrics: "+metrics);
- };
- /**
- * Reads an UTF8 encoded string. This is an alias of {@link ByteBuffer#readUTF8String}.
- * @function
- * @param {number} length Number of characters or bytes to read
- * @param {number=} metrics Metrics specifying what `n` is meant to count. Defaults to
- * {@link ByteBuffer.METRICS_CHARS}.
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * read if omitted.
- * @returns {string|!{string: string, length: number}} The string read if offset is omitted, else the string
- * read and the actual number of bytes read.
- * @expose
- */
- ByteBufferPrototype.readString = ByteBufferPrototype.readUTF8String;
- // types/strings/vstring
- /**
- * Writes a length as varint32 prefixed UTF8 encoded string.
- * @param {string} str String to write
- * @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * written if omitted.
- * @returns {!ByteBuffer|number} `this` if `offset` is omitted, else the actual number of bytes written
- * @expose
- * @see ByteBuffer#writeVarint32
- */
- ByteBufferPrototype.writeVString = function(str, offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof str !== 'string')
- throw TypeError("Illegal str: Not a string");
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- var start = offset,
- k, l;
- k = utfx.calculateUTF16asUTF8(stringSource(str), this.noAssert)[1];
- l = ByteBuffer.calculateVarint32(k);
- offset += l+k;
- var capacity15 = this.buffer.byteLength;
- if (offset > capacity15)
- this.resize((capacity15 *= 2) > offset ? capacity15 : offset);
- offset -= l+k;
- offset += this.writeVarint32(k, offset);
- utfx.encodeUTF16toUTF8(stringSource(str), function(b) {
- this.view.setUint8(offset++, b);
- }.bind(this));
- if (offset !== start+k+l)
- throw RangeError("Illegal range: Truncated data, "+offset+" == "+(offset+k+l));
- if (relative) {
- this.offset = offset;
- return this;
- }
- return offset - start;
- };
- /**
- * Reads a length as varint32 prefixed UTF8 encoded string.
- * @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * read if omitted.
- * @returns {string|!{string: string, length: number}} The string read if offset is omitted, else the string
- * read and the actual number of bytes read.
- * @expose
- * @see ByteBuffer#readVarint32
- */
- ByteBufferPrototype.readVString = function(offset) {
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 1 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
- }
- var temp = this.readVarint32(offset),
- start = offset,
- str;
- offset += temp['length'];
- temp = temp['value'];
- var k = offset + temp,
- sd = stringDestination();
- utfx.decodeUTF8toUTF16(function() {
- return offset < k ? this.view.getUint8(offset++) : null;
- }.bind(this), sd, this.noAssert);
- str = sd();
- if (relative) {
- this.offset = offset;
- return str;
- } else {
- return {
- 'string': str,
- 'length': offset - start
- };
- }
- };
- /**
- * Appends some data to this ByteBuffer. This will overwrite any contents behind the specified offset up to the appended
- * data's length.
- * @param {!ByteBuffer|!ArrayBuffer|!Uint8Array|string} source Data to append. If `source` is a ByteBuffer, its offsets
- * will be modified according to the performed read operation.
- * @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
- * @param {number=} offset Offset to append at. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * read if omitted.
- * @returns {!ByteBuffer} this
- * @expose
- * @example A relative `<01 02>03.append(<04 05>)` will result in `<01 02 04 05>, 04 05|`
- * @example An absolute `<01 02>03.append(04 05>, 1)` will result in `<01 04>05, 04 05|`
- */
- ByteBufferPrototype.append = function(source, encoding, offset) {
- if (typeof encoding === 'number' || typeof encoding !== 'string') {
- offset = encoding;
- encoding = undefined;
- }
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- if (!(source instanceof ByteBuffer))
- source = ByteBuffer.wrap(source, encoding);
- var length = source.limit - source.offset;
- if (length <= 0) return this; // Nothing to append
- offset += length;
- var capacity16 = this.buffer.byteLength;
- if (offset > capacity16)
- this.resize((capacity16 *= 2) > offset ? capacity16 : offset);
- offset -= length;
- new Uint8Array(this.buffer, offset).set(new Uint8Array(source.buffer).subarray(source.offset, source.limit));
- source.offset += length;
- if (relative) this.offset += length;
- return this;
- };
- /**
- * Appends this ByteBuffer's contents to another ByteBuffer. This will overwrite any contents behind the specified
- * offset up to the length of this ByteBuffer's data.
- * @param {!ByteBuffer} target Target ByteBuffer
- * @param {number=} offset Offset to append to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * read if omitted.
- * @returns {!ByteBuffer} this
- * @expose
- * @see ByteBuffer#append
- */
- ByteBufferPrototype.appendTo = function(target, offset) {
- target.append(this, offset);
- return this;
- };
- /**
- * Enables or disables assertions of argument types and offsets. Assertions are enabled by default but you can opt to
- * disable them if your code already makes sure that everything is valid.
- * @param {boolean} assert `true` to enable assertions, otherwise `false`
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.assert = function(assert) {
- this.noAssert = !assert;
- return this;
- };
- /**
- * Gets the capacity of this ByteBuffer's backing buffer.
- * @returns {number} Capacity of the backing buffer
- * @expose
- */
- ByteBufferPrototype.capacity = function() {
- return this.buffer.byteLength;
- };
- /**
- * Clears this ByteBuffer's offsets by setting {@link ByteBuffer#offset} to `0` and {@link ByteBuffer#limit} to the
- * backing buffer's capacity. Discards {@link ByteBuffer#markedOffset}.
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.clear = function() {
- this.offset = 0;
- this.limit = this.buffer.byteLength;
- this.markedOffset = -1;
- return this;
- };
- /**
- * Creates a cloned instance of this ByteBuffer, preset with this ByteBuffer's values for {@link ByteBuffer#offset},
- * {@link ByteBuffer#markedOffset} and {@link ByteBuffer#limit}.
- * @param {boolean=} copy Whether to copy the backing buffer or to return another view on the same, defaults to `false`
- * @returns {!ByteBuffer} Cloned instance
- * @expose
- */
- ByteBufferPrototype.clone = function(copy) {
- var bb = new ByteBuffer(0, this.littleEndian, this.noAssert);
- if (copy) {
- var buffer = new ArrayBuffer(this.buffer.byteLength);
- new Uint8Array(buffer).set(this.buffer);
- bb.buffer = buffer;
- bb.view = new DataView(buffer);
- } else {
- bb.buffer = this.buffer;
- bb.view = this.view;
- }
- bb.offset = this.offset;
- bb.markedOffset = this.markedOffset;
- bb.limit = this.limit;
- return bb;
- };
- /**
- * Compacts this ByteBuffer to be backed by a {@link ByteBuffer#buffer} of its contents' length. Contents are the bytes
- * between {@link ByteBuffer#offset} and {@link ByteBuffer#limit}. Will set `offset = 0` and `limit = capacity` and
- * adapt {@link ByteBuffer#markedOffset} to the same relative position if set.
- * @param {number=} begin Offset to start at, defaults to {@link ByteBuffer#offset}
- * @param {number=} end Offset to end at, defaults to {@link ByteBuffer#limit}
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.compact = function(begin, end) {
- if (typeof begin === 'undefined') begin = this.offset;
- if (typeof end === 'undefined') end = this.limit;
- if (!this.noAssert) {
- if (typeof begin !== 'number' || begin % 1 !== 0)
- throw TypeError("Illegal begin: Not an integer");
- begin >>>= 0;
- if (typeof end !== 'number' || end % 1 !== 0)
- throw TypeError("Illegal end: Not an integer");
- end >>>= 0;
- if (begin < 0 || begin > end || end > this.buffer.byteLength)
- throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
- }
- if (begin === 0 && end === this.buffer.byteLength)
- return this; // Already compacted
- var len = end - begin;
- if (len === 0) {
- this.buffer = EMPTY_BUFFER;
- this.view = null;
- if (this.markedOffset >= 0) this.markedOffset -= begin;
- this.offset = 0;
- this.limit = 0;
- return this;
- }
- var buffer = new ArrayBuffer(len);
- new Uint8Array(buffer).set(new Uint8Array(this.buffer).subarray(begin, end));
- this.buffer = buffer;
- this.view = new DataView(buffer);
- if (this.markedOffset >= 0) this.markedOffset -= begin;
- this.offset = 0;
- this.limit = len;
- return this;
- };
- /**
- * Creates a copy of this ByteBuffer's contents. Contents are the bytes between {@link ByteBuffer#offset} and
- * {@link ByteBuffer#limit}.
- * @param {number=} begin Begin offset, defaults to {@link ByteBuffer#offset}.
- * @param {number=} end End offset, defaults to {@link ByteBuffer#limit}.
- * @returns {!ByteBuffer} Copy
- * @expose
- */
- ByteBufferPrototype.copy = function(begin, end) {
- if (typeof begin === 'undefined') begin = this.offset;
- if (typeof end === 'undefined') end = this.limit;
- if (!this.noAssert) {
- if (typeof begin !== 'number' || begin % 1 !== 0)
- throw TypeError("Illegal begin: Not an integer");
- begin >>>= 0;
- if (typeof end !== 'number' || end % 1 !== 0)
- throw TypeError("Illegal end: Not an integer");
- end >>>= 0;
- if (begin < 0 || begin > end || end > this.buffer.byteLength)
- throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
- }
- if (begin === end)
- return new ByteBuffer(0, this.littleEndian, this.noAssert);
- var capacity = end - begin,
- bb = new ByteBuffer(capacity, this.littleEndian, this.noAssert);
- bb.offset = 0;
- bb.limit = capacity;
- if (bb.markedOffset >= 0) bb.markedOffset -= begin;
- this.copyTo(bb, 0, begin, end);
- return bb;
- };
- /**
- * Copies this ByteBuffer's contents to another ByteBuffer. Contents are the bytes between {@link ByteBuffer#offset} and
- * {@link ByteBuffer#limit}.
- * @param {!ByteBuffer} target Target ByteBuffer
- * @param {number=} targetOffset Offset to copy to. Will use and increase the target's {@link ByteBuffer#offset}
- * by the number of bytes copied if omitted.
- * @param {number=} sourceOffset Offset to start copying from. Will use and increase {@link ByteBuffer#offset} by the
- * number of bytes copied if omitted.
- * @param {number=} sourceLimit Offset to end copying from, defaults to {@link ByteBuffer#limit}
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.copyTo = function(target, targetOffset, sourceOffset, sourceLimit) {
- var relative,
- targetRelative;
- if (!this.noAssert) {
- if (!ByteBuffer.isByteBuffer(target))
- throw TypeError("Illegal target: Not a ByteBuffer");
- }
- targetOffset = (targetRelative = typeof targetOffset === 'undefined') ? target.offset : targetOffset | 0;
- sourceOffset = (relative = typeof sourceOffset === 'undefined') ? this.offset : sourceOffset | 0;
- sourceLimit = typeof sourceLimit === 'undefined' ? this.limit : sourceLimit | 0;
- if (targetOffset < 0 || targetOffset > target.buffer.byteLength)
- throw RangeError("Illegal target range: 0 <= "+targetOffset+" <= "+target.buffer.byteLength);
- if (sourceOffset < 0 || sourceLimit > this.buffer.byteLength)
- throw RangeError("Illegal source range: 0 <= "+sourceOffset+" <= "+this.buffer.byteLength);
- var len = sourceLimit - sourceOffset;
- if (len === 0)
- return target; // Nothing to copy
- target.ensureCapacity(targetOffset + len);
- new Uint8Array(target.buffer).set(new Uint8Array(this.buffer).subarray(sourceOffset, sourceLimit), targetOffset);
- if (relative) this.offset += len;
- if (targetRelative) target.offset += len;
- return this;
- };
- /**
- * Makes sure that this ByteBuffer is backed by a {@link ByteBuffer#buffer} of at least the specified capacity. If the
- * current capacity is exceeded, it will be doubled. If double the current capacity is less than the required capacity,
- * the required capacity will be used instead.
- * @param {number} capacity Required capacity
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.ensureCapacity = function(capacity) {
- var current = this.buffer.byteLength;
- if (current < capacity)
- return this.resize((current *= 2) > capacity ? current : capacity);
- return this;
- };
- /**
- * Overwrites this ByteBuffer's contents with the specified value. Contents are the bytes between
- * {@link ByteBuffer#offset} and {@link ByteBuffer#limit}.
- * @param {number|string} value Byte value to fill with. If given as a string, the first character is used.
- * @param {number=} begin Begin offset. Will use and increase {@link ByteBuffer#offset} by the number of bytes
- * written if omitted. defaults to {@link ByteBuffer#offset}.
- * @param {number=} end End offset, defaults to {@link ByteBuffer#limit}.
- * @returns {!ByteBuffer} this
- * @expose
- * @example `someByteBuffer.clear().fill(0)` fills the entire backing buffer with zeroes
- */
- ByteBufferPrototype.fill = function(value, begin, end) {
- var relative = typeof begin === 'undefined';
- if (relative) begin = this.offset;
- if (typeof value === 'string' && value.length > 0)
- value = value.charCodeAt(0);
- if (typeof begin === 'undefined') begin = this.offset;
- if (typeof end === 'undefined') end = this.limit;
- if (!this.noAssert) {
- if (typeof value !== 'number' || value % 1 !== 0)
- throw TypeError("Illegal value: "+value+" (not an integer)");
- value |= 0;
- if (typeof begin !== 'number' || begin % 1 !== 0)
- throw TypeError("Illegal begin: Not an integer");
- begin >>>= 0;
- if (typeof end !== 'number' || end % 1 !== 0)
- throw TypeError("Illegal end: Not an integer");
- end >>>= 0;
- if (begin < 0 || begin > end || end > this.buffer.byteLength)
- throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
- }
- if (begin >= end)
- return this; // Nothing to fill
- while (begin < end) this.view.setUint8(begin++, value);
- if (relative) this.offset = begin;
- return this;
- };
- /**
- * Makes this ByteBuffer ready for a new sequence of write or relative read operations. Sets `limit = offset` and
- * `offset = 0`. Make sure always to flip a ByteBuffer when all relative read or write operations are complete.
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.flip = function() {
- this.limit = this.offset;
- this.offset = 0;
- return this;
- };
- /**
- * Marks an offset on this ByteBuffer to be used later.
- * @param {number=} offset Offset to mark. Defaults to {@link ByteBuffer#offset}.
- * @returns {!ByteBuffer} this
- * @throws {TypeError} If `offset` is not a valid number
- * @throws {RangeError} If `offset` is out of bounds
- * @see ByteBuffer#reset
- * @expose
- */
- ByteBufferPrototype.mark = function(offset) {
- offset = typeof offset === 'undefined' ? this.offset : offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- this.markedOffset = offset;
- return this;
- };
- /**
- * Sets the byte order.
- * @param {boolean} littleEndian `true` for little endian byte order, `false` for big endian
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.order = function(littleEndian) {
- if (!this.noAssert) {
- if (typeof littleEndian !== 'boolean')
- throw TypeError("Illegal littleEndian: Not a boolean");
- }
- this.littleEndian = !!littleEndian;
- return this;
- };
- /**
- * Switches (to) little endian byte order.
- * @param {boolean=} littleEndian Defaults to `true`, otherwise uses big endian
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.LE = function(littleEndian) {
- this.littleEndian = typeof littleEndian !== 'undefined' ? !!littleEndian : true;
- return this;
- };
- /**
- * Switches (to) big endian byte order.
- * @param {boolean=} bigEndian Defaults to `true`, otherwise uses little endian
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.BE = function(bigEndian) {
- this.littleEndian = typeof bigEndian !== 'undefined' ? !bigEndian : false;
- return this;
- };
- /**
- * Prepends some data to this ByteBuffer. This will overwrite any contents before the specified offset up to the
- * prepended data's length. If there is not enough space available before the specified `offset`, the backing buffer
- * will be resized and its contents moved accordingly.
- * @param {!ByteBuffer|string|!ArrayBuffer} source Data to prepend. If `source` is a ByteBuffer, its offset will be
- * modified according to the performed read operation.
- * @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
- * @param {number=} offset Offset to prepend at. Will use and decrease {@link ByteBuffer#offset} by the number of bytes
- * prepended if omitted.
- * @returns {!ByteBuffer} this
- * @expose
- * @example A relative `00<01 02 03>.prepend(<04 05>)` results in `<04 05 01 02 03>, 04 05|`
- * @example An absolute `00<01 02 03>.prepend(<04 05>, 2)` results in `04<05 02 03>, 04 05|`
- */
- ByteBufferPrototype.prepend = function(source, encoding, offset) {
- if (typeof encoding === 'number' || typeof encoding !== 'string') {
- offset = encoding;
- encoding = undefined;
- }
- var relative = typeof offset === 'undefined';
- if (relative) offset = this.offset;
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: "+offset+" (not an integer)");
- offset >>>= 0;
- if (offset < 0 || offset + 0 > this.buffer.byteLength)
- throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
- }
- if (!(source instanceof ByteBuffer))
- source = ByteBuffer.wrap(source, encoding);
- var len = source.limit - source.offset;
- if (len <= 0) return this; // Nothing to prepend
- var diff = len - offset;
- var arrayView;
- if (diff > 0) { // Not enough space before offset, so resize + move
- var buffer = new ArrayBuffer(this.buffer.byteLength + diff);
- arrayView = new Uint8Array(buffer);
- arrayView.set(new Uint8Array(this.buffer).subarray(offset, this.buffer.byteLength), len);
- this.buffer = buffer;
- this.view = new DataView(buffer);
- this.offset += diff;
- if (this.markedOffset >= 0) this.markedOffset += diff;
- this.limit += diff;
- offset += diff;
- } else {
- arrayView = new Uint8Array(this.buffer);
- }
- arrayView.set(new Uint8Array(source.buffer).subarray(source.offset, source.limit), offset - len);
- source.offset = source.limit;
- if (relative)
- this.offset -= len;
- return this;
- };
- /**
- * Prepends this ByteBuffer to another ByteBuffer. This will overwrite any contents before the specified offset up to the
- * prepended data's length. If there is not enough space available before the specified `offset`, the backing buffer
- * will be resized and its contents moved accordingly.
- * @param {!ByteBuffer} target Target ByteBuffer
- * @param {number=} offset Offset to prepend at. Will use and decrease {@link ByteBuffer#offset} by the number of bytes
- * prepended if omitted.
- * @returns {!ByteBuffer} this
- * @expose
- * @see ByteBuffer#prepend
- */
- ByteBufferPrototype.prependTo = function(target, offset) {
- target.prepend(this, offset);
- return this;
- };
- /**
- * Prints debug information about this ByteBuffer's contents.
- * @param {function(string)=} out Output function to call, defaults to console.log
- * @expose
- */
- ByteBufferPrototype.printDebug = function(out) {
- if (typeof out !== 'function') out = console.log.bind(console);
- out(
- this.toString()+"\n"+
- "-------------------------------------------------------------------\n"+
- this.toDebug(/* columns */ true)
- );
- };
- /**
- * Gets the number of remaining readable bytes. Contents are the bytes between {@link ByteBuffer#offset} and
- * {@link ByteBuffer#limit}, so this returns `limit - offset`.
- * @returns {number} Remaining readable bytes. May be negative if `offset > limit`.
- * @expose
- */
- ByteBufferPrototype.remaining = function() {
- return this.limit - this.offset;
- };
- /**
- * Resets this ByteBuffer's {@link ByteBuffer#offset}. If an offset has been marked through {@link ByteBuffer#mark}
- * before, `offset` will be set to {@link ByteBuffer#markedOffset}, which will then be discarded. If no offset has been
- * marked, sets `offset = 0`.
- * @returns {!ByteBuffer} this
- * @see ByteBuffer#mark
- * @expose
- */
- ByteBufferPrototype.reset = function() {
- if (this.markedOffset >= 0) {
- this.offset = this.markedOffset;
- this.markedOffset = -1;
- } else {
- this.offset = 0;
- }
- return this;
- };
- /**
- * Resizes this ByteBuffer to be backed by a buffer of at least the given capacity. Will do nothing if already that
- * large or larger.
- * @param {number} capacity Capacity required
- * @returns {!ByteBuffer} this
- * @throws {TypeError} If `capacity` is not a number
- * @throws {RangeError} If `capacity < 0`
- * @expose
- */
- ByteBufferPrototype.resize = function(capacity) {
- if (!this.noAssert) {
- if (typeof capacity !== 'number' || capacity % 1 !== 0)
- throw TypeError("Illegal capacity: "+capacity+" (not an integer)");
- capacity |= 0;
- if (capacity < 0)
- throw RangeError("Illegal capacity: 0 <= "+capacity);
- }
- if (this.buffer.byteLength < capacity) {
- var buffer = new ArrayBuffer(capacity);
- new Uint8Array(buffer).set(new Uint8Array(this.buffer));
- this.buffer = buffer;
- this.view = new DataView(buffer);
- }
- return this;
- };
- /**
- * Reverses this ByteBuffer's contents.
- * @param {number=} begin Offset to start at, defaults to {@link ByteBuffer#offset}
- * @param {number=} end Offset to end at, defaults to {@link ByteBuffer#limit}
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.reverse = function(begin, end) {
- if (typeof begin === 'undefined') begin = this.offset;
- if (typeof end === 'undefined') end = this.limit;
- if (!this.noAssert) {
- if (typeof begin !== 'number' || begin % 1 !== 0)
- throw TypeError("Illegal begin: Not an integer");
- begin >>>= 0;
- if (typeof end !== 'number' || end % 1 !== 0)
- throw TypeError("Illegal end: Not an integer");
- end >>>= 0;
- if (begin < 0 || begin > end || end > this.buffer.byteLength)
- throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
- }
- if (begin === end)
- return this; // Nothing to reverse
- Array.prototype.reverse.call(new Uint8Array(this.buffer).subarray(begin, end));
- this.view = new DataView(this.buffer); // FIXME: Why exactly is this necessary?
- return this;
- };
- /**
- * Skips the next `length` bytes. This will just advance
- * @param {number} length Number of bytes to skip. May also be negative to move the offset back.
- * @returns {!ByteBuffer} this
- * @expose
- */
- ByteBufferPrototype.skip = function(length) {
- if (!this.noAssert) {
- if (typeof length !== 'number' || length % 1 !== 0)
- throw TypeError("Illegal length: "+length+" (not an integer)");
- length |= 0;
- }
- var offset = this.offset + length;
- if (!this.noAssert) {
- if (offset < 0 || offset > this.buffer.byteLength)
- throw RangeError("Illegal length: 0 <= "+this.offset+" + "+length+" <= "+this.buffer.byteLength);
- }
- this.offset = offset;
- return this;
- };
- /**
- * Slices this ByteBuffer by creating a cloned instance with `offset = begin` and `limit = end`.
- * @param {number=} begin Begin offset, defaults to {@link ByteBuffer#offset}.
- * @param {number=} end End offset, defaults to {@link ByteBuffer#limit}.
- * @returns {!ByteBuffer} Clone of this ByteBuffer with slicing applied, backed by the same {@link ByteBuffer#buffer}
- * @expose
- */
- ByteBufferPrototype.slice = function(begin, end) {
- if (typeof begin === 'undefined') begin = this.offset;
- if (typeof end === 'undefined') end = this.limit;
- if (!this.noAssert) {
- if (typeof begin !== 'number' || begin % 1 !== 0)
- throw TypeError("Illegal begin: Not an integer");
- begin >>>= 0;
- if (typeof end !== 'number' || end % 1 !== 0)
- throw TypeError("Illegal end: Not an integer");
- end >>>= 0;
- if (begin < 0 || begin > end || end > this.buffer.byteLength)
- throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
- }
- var bb = this.clone();
- bb.offset = begin;
- bb.limit = end;
- return bb;
- };
- /**
- * Returns a copy of the backing buffer that contains this ByteBuffer's contents. Contents are the bytes between
- * {@link ByteBuffer#offset} and {@link ByteBuffer#limit}. Will transparently {@link ByteBuffer#flip} this
- * ByteBuffer if `offset > limit` but the actual offsets remain untouched.
- * @param {boolean=} forceCopy If `true` returns a copy, otherwise returns a view referencing the same memory if
- * possible. Defaults to `false`
- * @returns {!ArrayBuffer} Contents as an ArrayBuffer
- * @expose
- */
- ByteBufferPrototype.toBuffer = function(forceCopy) {
- var offset = this.offset,
- limit = this.limit;
- if (offset > limit) {
- var t = offset;
- offset = limit;
- limit = t;
- }
- if (!this.noAssert) {
- if (typeof offset !== 'number' || offset % 1 !== 0)
- throw TypeError("Illegal offset: Not an integer");
- offset >>>= 0;
- if (typeof limit !== 'number' || limit % 1 !== 0)
- throw TypeError("Illegal limit: Not an integer");
- limit >>>= 0;
- if (offset < 0 || offset > limit || limit > this.buffer.byteLength)
- throw RangeError("Illegal range: 0 <= "+offset+" <= "+limit+" <= "+this.buffer.byteLength);
- }
- // NOTE: It's not possible to have another ArrayBuffer reference the same memory as the backing buffer. This is
- // possible with Uint8Array#subarray only, but we have to return an ArrayBuffer by contract. So:
- if (!forceCopy && offset === 0 && limit === this.buffer.byteLength) {
- return this.buffer;
- }
- if (offset === limit) {
- return EMPTY_BUFFER;
- }
- var buffer = new ArrayBuffer(limit - offset);
- new Uint8Array(buffer).set(new Uint8Array(this.buffer).subarray(offset, limit), 0);
- return buffer;
- };
- /**
- * Returns a raw buffer compacted to contain this ByteBuffer's contents. Contents are the bytes between
- * {@link ByteBuffer#offset} and {@link ByteBuffer#limit}. Will transparently {@link ByteBuffer#flip} this
- * ByteBuffer if `offset > limit` but the actual offsets remain untouched. This is an alias of
- * {@link ByteBuffer#toBuffer}.
- * @function
- * @param {boolean=} forceCopy If `true` returns a copy, otherwise returns a view referencing the same memory.
- * Defaults to `false`
- * @returns {!ArrayBuffer} Contents as an ArrayBuffer
- * @expose
- */
- ByteBufferPrototype.toArrayBuffer = ByteBufferPrototype.toBuffer;
- /**
- * Converts the ByteBuffer's contents to a string.
- * @param {string=} encoding Output encoding. Returns an informative string representation if omitted but also allows
- * direct conversion to "utf8", "hex", "base64" and "binary" encoding. "debug" returns a hex representation with
- * highlighted offsets.
- * @param {number=} begin Offset to begin at, defaults to {@link ByteBuffer#offset}
- * @param {number=} end Offset to end at, defaults to {@link ByteBuffer#limit}
- * @returns {string} String representation
- * @throws {Error} If `encoding` is invalid
- * @expose
- */
- ByteBufferPrototype.toString = function(encoding, begin, end) {
- if (typeof encoding === 'undefined')
- return "ByteBufferAB(offset="+this.offset+",markedOffset="+this.markedOffset+",limit="+this.limit+",capacity="+this.capacity()+")";
- if (typeof encoding === 'number')
- encoding = "utf8",
- begin = encoding,
- end = begin;
- switch (encoding) {
- case "utf8":
- return this.toUTF8(begin, end);
- case "base64":
- return this.toBase64(begin, end);
- case "hex":
- return this.toHex(begin, end);
- case "binary":
- return this.toBinary(begin, end);
- case "debug":
- return this.toDebug();
- case "columns":
- return this.toColumns();
- default:
- throw Error("Unsupported encoding: "+encoding);
- }
- };
- // lxiv-embeddable
- /**
- * lxiv-embeddable (c) 2014 Daniel Wirtz <dcode@dcode.io>
- * Released under the Apache License, Version 2.0
- * see: https://github.com/dcodeIO/lxiv for details
- */
- var lxiv = function() {
- "use strict";
- /**
- * lxiv namespace.
- * @type {!Object.<string,*>}
- * @exports lxiv
- */
- var lxiv = {};
- /**
- * Character codes for output.
- * @type {!Array.<number>}
- * @inner
- */
- var aout = [
- 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
- 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102,
- 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118,
- 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47
- ];
- /**
- * Character codes for input.
- * @type {!Array.<number>}
- * @inner
- */
- var ain = [];
- for (var i=0, k=aout.length; i<k; ++i)
- ain[aout[i]] = i;
- /**
- * Encodes bytes to base64 char codes.
- * @param {!function():number|null} src Bytes source as a function returning the next byte respectively `null` if
- * there are no more bytes left.
- * @param {!function(number)} dst Characters destination as a function successively called with each encoded char
- * code.
- */
- lxiv.encode = function(src, dst) {
- var b, t;
- while ((b = src()) !== null) {
- dst(aout[(b>>2)&0x3f]);
- t = (b&0x3)<<4;
- if ((b = src()) !== null) {
- t |= (b>>4)&0xf;
- dst(aout[(t|((b>>4)&0xf))&0x3f]);
- t = (b&0xf)<<2;
- if ((b = src()) !== null)
- dst(aout[(t|((b>>6)&0x3))&0x3f]),
- dst(aout[b&0x3f]);
- else
- dst(aout[t&0x3f]),
- dst(61);
- } else
- dst(aout[t&0x3f]),
- dst(61),
- dst(61);
- }
- };
- /**
- * Decodes base64 char codes to bytes.
- * @param {!function():number|null} src Characters source as a function returning the next char code respectively
- * `null` if there are no more characters left.
- * @param {!function(number)} dst Bytes destination as a function successively called with the next byte.
- * @throws {Error} If a character code is invalid
- */
- lxiv.decode = function(src, dst) {
- var c, t1, t2;
- function fail(c) {
- throw Error("Illegal character code: "+c);
- }
- while ((c = src()) !== null) {
- t1 = ain[c];
- if (typeof t1 === 'undefined') fail(c);
- if ((c = src()) !== null) {
- t2 = ain[c];
- if (typeof t2 === 'undefined') fail(c);
- dst((t1<<2)>>>0|(t2&0x30)>>4);
- if ((c = src()) !== null) {
- t1 = ain[c];
- if (typeof t1 === 'undefined')
- if (c === 61) break; else fail(c);
- dst(((t2&0xf)<<4)>>>0|(t1&0x3c)>>2);
- if ((c = src()) !== null) {
- t2 = ain[c];
- if (typeof t2 === 'undefined')
- if (c === 61) break; else fail(c);
- dst(((t1&0x3)<<6)>>>0|t2);
- }
- }
- }
- }
- };
- /**
- * Tests if a string is valid base64.
- * @param {string} str String to test
- * @returns {boolean} `true` if valid, otherwise `false`
- */
- lxiv.test = function(str) {
- return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(str);
- };
- return lxiv;
- }();
- // encodings/base64
- /**
- * Encodes this ByteBuffer's contents to a base64 encoded string.
- * @param {number=} begin Offset to begin at, defaults to {@link ByteBuffer#offset}.
- * @param {number=} end Offset to end at, defaults to {@link ByteBuffer#limit}.
- * @returns {string} Base64 encoded string
- * @expose
- */
- ByteBufferPrototype.toBase64 = function(begin, end) {
- if (typeof begin === 'undefined')
- begin = this.offset;
- if (typeof end === 'undefined')
- end = this.limit;
- if (!this.noAssert) {
- if (typeof begin !== 'number' || begin % 1 !== 0)
- throw TypeError("Illegal begin: Not an integer");
- begin >>>= 0;
- if (typeof end !== 'number' || end % 1 !== 0)
- throw TypeError("Illegal end: Not an integer");
- end >>>= 0;
- if (begin < 0 || begin > end || end > this.buffer.byteLength)
- throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
- }
- var sd; lxiv.encode(function() {
- return begin < end ? this.view.getUint8(begin++) : null;
- }.bind(this), sd = stringDestination());
- return sd();
- };
- /**
- * Decodes a base64 encoded string to a ByteBuffer.
- * @param {string} str String to decode
- * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
- * {@link ByteBuffer.DEFAULT_ENDIAN}.
- * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
- * {@link ByteBuffer.DEFAULT_NOASSERT}.
- * @returns {!ByteBuffer} ByteBuffer
- * @expose
- */
- ByteBuffer.fromBase64 = function(str, littleEndian, noAssert) {
- if (!noAssert) {
- if (typeof str !== 'string')
- throw TypeError("Illegal str: Not a string");
- if (str.length % 4 !== 0)
- throw TypeError("Illegal str: Length not a multiple of 4");
- }
- var bb = new ByteBuffer(str.length/4*3, littleEndian, noAssert),
- i = 0;
- lxiv.decode(stringSource(str), function(b) {
- bb.view.setUint8(i++, b);
- });
- bb.limit = i;
- return bb;
- };
- /**
- * Encodes a binary string to base64 like `window.btoa` does.
- * @param {string} str Binary string
- * @returns {string} Base64 encoded string
- * @see https://developer.mozilla.org/en-US/docs/Web/API/Window.btoa
- * @expose
- */
- ByteBuffer.btoa = function(str) {
- return ByteBuffer.fromBinary(str).toBase64();
- };
- /**
- * Decodes a base64 encoded string to binary like `window.atob` does.
- * @param {string} b64 Base64 encoded string
- * @returns {string} Binary string
- * @see https://developer.mozilla.org/en-US/docs/Web/API/Window.atob
- * @expose
- */
- ByteBuffer.atob = function(b64) {
- return ByteBuffer.fromBase64(b64).toBinary();
- };
- // encodings/binary
- /**
- * Encodes this ByteBuffer to a binary encoded string, that is using only characters 0x00-0xFF as bytes.
- * @param {number=} begin Offset to begin at. Defaults to {@link ByteBuffer#offset}.
- * @param {number=} end Offset to end at. Defaults to {@link ByteBuffer#limit}.
- * @returns {string} Binary encoded string
- * @throws {RangeError} If `offset > limit`
- * @expose
- */
- ByteBufferPrototype.toBinary = function(begin, end) {
- begin = typeof begin === 'undefined' ? this.offset : begin;
- end = typeof end === 'undefined' ? this.limit : end;
- if (!this.noAssert) {
- if (typeof begin !== 'number' || begin % 1 !== 0)
- throw TypeError("Illegal begin: Not an integer");
- begin >>>= 0;
- if (typeof end !== 'number' || end % 1 !== 0)
- throw TypeError("Illegal end: Not an integer");
- end >>>= 0;
- if (begin < 0 || begin > end || end > this.buffer.byteLength)
- throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
- }
- if (begin === end)
- return "";
- var cc = [], pt = [];
- while (begin < end) {
- cc.push(this.view.getUint8(begin++));
- if (cc.length >= 1024)
- pt.push(String.fromCharCode.apply(String, cc)),
- cc = [];
- }
- return pt.join('') + String.fromCharCode.apply(String, cc);
- };
- /**
- * Decodes a binary encoded string, that is using only characters 0x00-0xFF as bytes, to a ByteBuffer.
- * @param {string} str String to decode
- * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
- * {@link ByteBuffer.DEFAULT_ENDIAN}.
- * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
- * {@link ByteBuffer.DEFAULT_NOASSERT}.
- * @returns {!ByteBuffer} ByteBuffer
- * @expose
- */
- ByteBuffer.fromBinary = function(str, littleEndian, noAssert) {
- if (!noAssert) {
- if (typeof str !== 'string')
- throw TypeError("Illegal str: Not a string");
- }
- var i = 0, k = str.length, charCode,
- bb = new ByteBuffer(k, littleEndian, noAssert);
- while (i<k) {
- charCode = str.charCodeAt(i);
- if (!noAssert && charCode > 255)
- throw RangeError("Illegal charCode at "+i+": 0 <= "+charCode+" <= 255");
- bb.view.setUint8(i++, charCode);
- }
- bb.limit = k;
- return bb;
- };
- // encodings/debug
- /**
- * Encodes this ByteBuffer to a hex encoded string with marked offsets. Offset symbols are:
- * * `<` : offset,
- * * `'` : markedOffset,
- * * `>` : limit,
- * * `|` : offset and limit,
- * * `[` : offset and markedOffset,
- * * `]` : markedOffset and limit,
- * * `!` : offset, markedOffset and limit
- * @param {boolean=} columns If `true` returns two columns hex + ascii, defaults to `false`
- * @returns {string|!Array.<string>} Debug string or array of lines if `asArray = true`
- * @expose
- * @example `>00'01 02<03` contains four bytes with `limit=0, markedOffset=1, offset=3`
- * @example `00[01 02 03>` contains four bytes with `offset=markedOffset=1, limit=4`
- * @example `00|01 02 03` contains four bytes with `offset=limit=1, markedOffset=-1`
- * @example `|` contains zero bytes with `offset=limit=0, markedOffset=-1`
- */
- ByteBufferPrototype.toDebug = function(columns) {
- var i = -1,
- k = this.buffer.byteLength,
- b,
- hex = "",
- asc = "",
- out = "";
- while (i<k) {
- if (i !== -1) {
- b = this.view.getUint8(i);
- if (b < 0x10) hex += "0"+b.toString(16).toUpperCase();
- else hex += b.toString(16).toUpperCase();
- if (columns) {
- asc += b > 32 && b < 127 ? String.fromCharCode(b) : '.';
- }
- }
- ++i;
- if (columns) {
- if (i > 0 && i % 16 === 0 && i !== k) {
- while (hex.length < 3*16+3) hex += " ";
- out += hex+asc+"\n";
- hex = asc = "";
- }
- }
- if (i === this.offset && i === this.limit)
- hex += i === this.markedOffset ? "!" : "|";
- else if (i === this.offset)
- hex += i === this.markedOffset ? "[" : "<";
- else if (i === this.limit)
- hex += i === this.markedOffset ? "]" : ">";
- else
- hex += i === this.markedOffset ? "'" : (columns || (i !== 0 && i !== k) ? " " : "");
- }
- if (columns && hex !== " ") {
- while (hex.length < 3*16+3) hex += " ";
- out += hex+asc+"\n";
- }
- return columns ? out : hex;
- };
- /**
- * Decodes a hex encoded string with marked offsets to a ByteBuffer.
- * @param {string} str Debug string to decode (not be generated with `columns = true`)
- * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
- * {@link ByteBuffer.DEFAULT_ENDIAN}.
- * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
- * {@link ByteBuffer.DEFAULT_NOASSERT}.
- * @returns {!ByteBuffer} ByteBuffer
- * @expose
- * @see ByteBuffer#toDebug
- */
- ByteBuffer.fromDebug = function(str, littleEndian, noAssert) {
- var k = str.length,
- bb = new ByteBuffer(((k+1)/3)|0, littleEndian, noAssert);
- var i = 0, j = 0, ch, b,
- rs = false, // Require symbol next
- ho = false, hm = false, hl = false, // Already has offset, markedOffset, limit?
- fail = false;
- while (i<k) {
- switch (ch = str.charAt(i++)) {
- case '!':
- if (!noAssert) {
- if (ho || hm || hl) {
- fail = true; break;
- }
- ho = hm = hl = true;
- }
- bb.offset = bb.markedOffset = bb.limit = j;
- rs = false;
- break;
- case '|':
- if (!noAssert) {
- if (ho || hl) {
- fail = true; break;
- }
- ho = hl = true;
- }
- bb.offset = bb.limit = j;
- rs = false;
- break;
- case '[':
- if (!noAssert) {
- if (ho || hm) {
- fail = true; break;
- }
- ho = hm = true;
- }
- bb.offset = bb.markedOffset = j;
- rs = false;
- break;
- case '<':
- if (!noAssert) {
- if (ho) {
- fail = true; break;
- }
- ho = true;
- }
- bb.offset = j;
- rs = false;
- break;
- case ']':
- if (!noAssert) {
- if (hl || hm) {
- fail = true; break;
- }
- hl = hm = true;
- }
- bb.limit = bb.markedOffset = j;
- rs = false;
- break;
- case '>':
- if (!noAssert) {
- if (hl) {
- fail = true; break;
- }
- hl = true;
- }
- bb.limit = j;
- rs = false;
- break;
- case "'":
- if (!noAssert) {
- if (hm) {
- fail = true; break;
- }
- hm = true;
- }
- bb.markedOffset = j;
- rs = false;
- break;
- case ' ':
- rs = false;
- break;
- default:
- if (!noAssert) {
- if (rs) {
- fail = true; break;
- }
- }
- b = parseInt(ch+str.charAt(i++), 16);
- if (!noAssert) {
- if (isNaN(b) || b < 0 || b > 255)
- throw TypeError("Illegal str: Not a debug encoded string");
- }
- bb.view.setUint8(j++, b);
- rs = true;
- }
- if (fail)
- throw TypeError("Illegal str: Invalid symbol at "+i);
- }
- if (!noAssert) {
- if (!ho || !hl)
- throw TypeError("Illegal str: Missing offset or limit");
- if (j<bb.buffer.byteLength)
- throw TypeError("Illegal str: Not a debug encoded string (is it hex?) "+j+" < "+k);
- }
- return bb;
- };
- // encodings/hex
- /**
- * Encodes this ByteBuffer's contents to a hex encoded string.
- * @param {number=} begin Offset to begin at. Defaults to {@link ByteBuffer#offset}.
- * @param {number=} end Offset to end at. Defaults to {@link ByteBuffer#limit}.
- * @returns {string} Hex encoded string
- * @expose
- */
- ByteBufferPrototype.toHex = function(begin, end) {
- begin = typeof begin === 'undefined' ? this.offset : begin;
- end = typeof end === 'undefined' ? this.limit : end;
- if (!this.noAssert) {
- if (typeof begin !== 'number' || begin % 1 !== 0)
- throw TypeError("Illegal begin: Not an integer");
- begin >>>= 0;
- if (typeof end !== 'number' || end % 1 !== 0)
- throw TypeError("Illegal end: Not an integer");
- end >>>= 0;
- if (begin < 0 || begin > end || end > this.buffer.byteLength)
- throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
- }
- var out = new Array(end - begin),
- b;
- while (begin < end) {
- b = this.view.getUint8(begin++);
- if (b < 0x10)
- out.push("0", b.toString(16));
- else out.push(b.toString(16));
- }
- return out.join('');
- };
- /**
- * Decodes a hex encoded string to a ByteBuffer.
- * @param {string} str String to decode
- * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
- * {@link ByteBuffer.DEFAULT_ENDIAN}.
- * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
- * {@link ByteBuffer.DEFAULT_NOASSERT}.
- * @returns {!ByteBuffer} ByteBuffer
- * @expose
- */
- ByteBuffer.fromHex = function(str, littleEndian, noAssert) {
- if (!noAssert) {
- if (typeof str !== 'string')
- throw TypeError("Illegal str: Not a string");
- if (str.length % 2 !== 0)
- throw TypeError("Illegal str: Length not a multiple of 2");
- }
- var k = str.length,
- bb = new ByteBuffer((k / 2) | 0, littleEndian),
- b;
- for (var i=0, j=0; i<k; i+=2) {
- b = parseInt(str.substring(i, i+2), 16);
- if (!noAssert)
- if (!isFinite(b) || b < 0 || b > 255)
- throw TypeError("Illegal str: Contains non-hex characters");
- bb.view.setUint8(j++, b);
- }
- bb.limit = j;
- return bb;
- };
- // utfx-embeddable
- /**
- * utfx-embeddable (c) 2014 Daniel Wirtz <dcode@dcode.io>
- * Released under the Apache License, Version 2.0
- * see: https://github.com/dcodeIO/utfx for details
- */
- var utfx = function() {
- "use strict";
- /**
- * utfx namespace.
- * @inner
- * @type {!Object.<string,*>}
- */
- var utfx = {};
- /**
- * Maximum valid code point.
- * @type {number}
- * @const
- */
- utfx.MAX_CODEPOINT = 0x10FFFF;
- /**
- * Encodes UTF8 code points to UTF8 bytes.
- * @param {(!function():number|null) | number} src Code points source, either as a function returning the next code point
- * respectively `null` if there are no more code points left or a single numeric code point.
- * @param {!function(number)} dst Bytes destination as a function successively called with the next byte
- */
- utfx.encodeUTF8 = function(src, dst) {
- var cp = null;
- if (typeof src === 'number')
- cp = src,
- src = function() { return null; };
- while (cp !== null || (cp = src()) !== null) {
- if (cp < 0x80)
- dst(cp&0x7F);
- else if (cp < 0x800)
- dst(((cp>>6)&0x1F)|0xC0),
- dst((cp&0x3F)|0x80);
- else if (cp < 0x10000)
- dst(((cp>>12)&0x0F)|0xE0),
- dst(((cp>>6)&0x3F)|0x80),
- dst((cp&0x3F)|0x80);
- else
- dst(((cp>>18)&0x07)|0xF0),
- dst(((cp>>12)&0x3F)|0x80),
- dst(((cp>>6)&0x3F)|0x80),
- dst((cp&0x3F)|0x80);
- cp = null;
- }
- };
- /**
- * Decodes UTF8 bytes to UTF8 code points.
- * @param {!function():number|null} src Bytes source as a function returning the next byte respectively `null` if there
- * are no more bytes left.
- * @param {!function(number)} dst Code points destination as a function successively called with each decoded code point.
- * @throws {RangeError} If a starting byte is invalid in UTF8
- * @throws {Error} If the last sequence is truncated. Has an array property `bytes` holding the
- * remaining bytes.
- */
- utfx.decodeUTF8 = function(src, dst) {
- var a, b, c, d, fail = function(b) {
- b = b.slice(0, b.indexOf(null));
- var err = Error(b.toString());
- err.name = "TruncatedError";
- err['bytes'] = b;
- throw err;
- };
- while ((a = src()) !== null) {
- if ((a&0x80) === 0)
- dst(a);
- else if ((a&0xE0) === 0xC0)
- ((b = src()) === null) && fail([a, b]),
- dst(((a&0x1F)<<6) | (b&0x3F));
- else if ((a&0xF0) === 0xE0)
- ((b=src()) === null || (c=src()) === null) && fail([a, b, c]),
- dst(((a&0x0F)<<12) | ((b&0x3F)<<6) | (c&0x3F));
- else if ((a&0xF8) === 0xF0)
- ((b=src()) === null || (c=src()) === null || (d=src()) === null) && fail([a, b, c ,d]),
- dst(((a&0x07)<<18) | ((b&0x3F)<<12) | ((c&0x3F)<<6) | (d&0x3F));
- else throw RangeError("Illegal starting byte: "+a);
- }
- };
- /**
- * Converts UTF16 characters to UTF8 code points.
- * @param {!function():number|null} src Characters source as a function returning the next char code respectively
- * `null` if there are no more characters left.
- * @param {!function(number)} dst Code points destination as a function successively called with each converted code
- * point.
- */
- utfx.UTF16toUTF8 = function(src, dst) {
- var c1, c2 = null;
- while (true) {
- if ((c1 = c2 !== null ? c2 : src()) === null)
- break;
- if (c1 >= 0xD800 && c1 <= 0xDFFF) {
- if ((c2 = src()) !== null) {
- if (c2 >= 0xDC00 && c2 <= 0xDFFF) {
- dst((c1-0xD800)*0x400+c2-0xDC00+0x10000);
- c2 = null; continue;
- }
- }
- }
- dst(c1);
- }
- if (c2 !== null) dst(c2);
- };
- /**
- * Converts UTF8 code points to UTF16 characters.
- * @param {(!function():number|null) | number} src Code points source, either as a function returning the next code point
- * respectively `null` if there are no more code points left or a single numeric code point.
- * @param {!function(number)} dst Characters destination as a function successively called with each converted char code.
- * @throws {RangeError} If a code point is out of range
- */
- utfx.UTF8toUTF16 = function(src, dst) {
- var cp = null;
- if (typeof src === 'number')
- cp = src, src = function() { return null; };
- while (cp !== null || (cp = src()) !== null) {
- if (cp <= 0xFFFF)
- dst(cp);
- else
- cp -= 0x10000,
- dst((cp>>10)+0xD800),
- dst((cp%0x400)+0xDC00);
- cp = null;
- }
- };
- /**
- * Converts and encodes UTF16 characters to UTF8 bytes.
- * @param {!function():number|null} src Characters source as a function returning the next char code respectively `null`
- * if there are no more characters left.
- * @param {!function(number)} dst Bytes destination as a function successively called with the next byte.
- */
- utfx.encodeUTF16toUTF8 = function(src, dst) {
- utfx.UTF16toUTF8(src, function(cp) {
- utfx.encodeUTF8(cp, dst);
- });
- };
- /**
- * Decodes and converts UTF8 bytes to UTF16 characters.
- * @param {!function():number|null} src Bytes source as a function returning the next byte respectively `null` if there
- * are no more bytes left.
- * @param {!function(number)} dst Characters destination as a function successively called with each converted char code.
- * @throws {RangeError} If a starting byte is invalid in UTF8
- * @throws {Error} If the last sequence is truncated. Has an array property `bytes` holding the remaining bytes.
- */
- utfx.decodeUTF8toUTF16 = function(src, dst) {
- utfx.decodeUTF8(src, function(cp) {
- utfx.UTF8toUTF16(cp, dst);
- });
- };
- /**
- * Calculates the byte length of an UTF8 code point.
- * @param {number} cp UTF8 code point
- * @returns {number} Byte length
- */
- utfx.calculateCodePoint = function(cp) {
- return (cp < 0x80) ? 1 : (cp < 0x800) ? 2 : (cp < 0x10000) ? 3 : 4;
- };
- /**
- * Calculates the number of UTF8 bytes required to store UTF8 code points.
- * @param {(!function():number|null)} src Code points source as a function returning the next code point respectively
- * `null` if there are no more code points left.
- * @returns {number} The number of UTF8 bytes required
- */
- utfx.calculateUTF8 = function(src) {
- var cp, l=0;
- while ((cp = src()) !== null)
- l += utfx.calculateCodePoint(cp);
- return l;
- };
- /**
- * Calculates the number of UTF8 code points respectively UTF8 bytes required to store UTF16 char codes.
- * @param {(!function():number|null)} src Characters source as a function returning the next char code respectively
- * `null` if there are no more characters left.
- * @returns {!Array.<number>} The number of UTF8 code points at index 0 and the number of UTF8 bytes required at index 1.
- */
- utfx.calculateUTF16asUTF8 = function(src) {
- var n=0, l=0;
- utfx.UTF16toUTF8(src, function(cp) {
- ++n; l += utfx.calculateCodePoint(cp);
- });
- return [n,l];
- };
- return utfx;
- }();
- // encodings/utf8
- /**
- * Encodes this ByteBuffer's contents between {@link ByteBuffer#offset} and {@link ByteBuffer#limit} to an UTF8 encoded
- * string.
- * @returns {string} Hex encoded string
- * @throws {RangeError} If `offset > limit`
- * @expose
- */
- ByteBufferPrototype.toUTF8 = function(begin, end) {
- if (typeof begin === 'undefined') begin = this.offset;
- if (typeof end === 'undefined') end = this.limit;
- if (!this.noAssert) {
- if (typeof begin !== 'number' || begin % 1 !== 0)
- throw TypeError("Illegal begin: Not an integer");
- begin >>>= 0;
- if (typeof end !== 'number' || end % 1 !== 0)
- throw TypeError("Illegal end: Not an integer");
- end >>>= 0;
- if (begin < 0 || begin > end || end > this.buffer.byteLength)
- throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength);
- }
- var sd; try {
- utfx.decodeUTF8toUTF16(function() {
- return begin < end ? this.view.getUint8(begin++) : null;
- }.bind(this), sd = stringDestination());
- } catch (e) {
- if (begin !== end)
- throw RangeError("Illegal range: Truncated data, "+begin+" != "+end);
- }
- return sd();
- };
- /**
- * Decodes an UTF8 encoded string to a ByteBuffer.
- * @param {string} str String to decode
- * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
- * {@link ByteBuffer.DEFAULT_ENDIAN}.
- * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
- * {@link ByteBuffer.DEFAULT_NOASSERT}.
- * @returns {!ByteBuffer} ByteBuffer
- * @expose
- */
- ByteBuffer.fromUTF8 = function(str, littleEndian, noAssert) {
- if (!noAssert)
- if (typeof str !== 'string')
- throw TypeError("Illegal str: Not a string");
- var bb = new ByteBuffer(utfx.calculateUTF16asUTF8(stringSource(str), true)[1], littleEndian, noAssert),
- i = 0;
- utfx.encodeUTF16toUTF8(stringSource(str), function(b) {
- bb.view.setUint8(i++, b);
- });
- bb.limit = i;
- return bb;
- };
- return ByteBuffer;
- }
- /* CommonJS */ if (typeof require === 'function' && typeof module === 'object' && module && typeof exports === 'object' && exports)
- module['exports'] = (function() {
- var Long; try { Long = require("long"); } catch (e) {}
- return loadByteBuffer(Long);
- })();
- /* AMD */ else if (typeof define === 'function' && define["amd"])
- define("ByteBuffer", ["Long"], function(Long) { return loadByteBuffer(Long); });
- /* Global */ else
- (global["dcodeIO"] = global["dcodeIO"] || {})["ByteBuffer"] = loadByteBuffer(global["dcodeIO"]["Long"]);
- })(this);
- /*
- Copyright 2013 Daniel Wirtz <dcode@dcode.io>
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- /**
- * @license ProtoBuf.js (c) 2013 Daniel Wirtz <dcode@dcode.io>
- * Released under the Apache License, Version 2.0
- * see: https://github.com/dcodeIO/ProtoBuf.js for details
- */
- (function(global) {
- "use strict";
- function init(ByteBuffer) {
- /**
- * The ProtoBuf namespace.
- * @exports ProtoBuf
- * @namespace
- * @expose
- */
- var ProtoBuf = {};
- /**
- * ProtoBuf.js version.
- * @type {string}
- * @const
- * @expose
- */
- ProtoBuf.VERSION = "3.8.0";
- /**
- * Wire types.
- * @type {Object.<string,number>}
- * @const
- * @expose
- */
- ProtoBuf.WIRE_TYPES = {};
- /**
- * Varint wire type.
- * @type {number}
- * @expose
- */
- ProtoBuf.WIRE_TYPES.VARINT = 0;
- /**
- * Fixed 64 bits wire type.
- * @type {number}
- * @const
- * @expose
- */
- ProtoBuf.WIRE_TYPES.BITS64 = 1;
- /**
- * Length delimited wire type.
- * @type {number}
- * @const
- * @expose
- */
- ProtoBuf.WIRE_TYPES.LDELIM = 2;
- /**
- * Start group wire type.
- * @type {number}
- * @const
- * @expose
- */
- ProtoBuf.WIRE_TYPES.STARTGROUP = 3;
- /**
- * End group wire type.
- * @type {number}
- * @const
- * @expose
- */
- ProtoBuf.WIRE_TYPES.ENDGROUP = 4;
- /**
- * Fixed 32 bits wire type.
- * @type {number}
- * @const
- * @expose
- */
- ProtoBuf.WIRE_TYPES.BITS32 = 5;
- /**
- * Packable wire types.
- * @type {!Array.<number>}
- * @const
- * @expose
- */
- ProtoBuf.PACKABLE_WIRE_TYPES = [
- ProtoBuf.WIRE_TYPES.VARINT,
- ProtoBuf.WIRE_TYPES.BITS64,
- ProtoBuf.WIRE_TYPES.BITS32
- ];
- /**
- * Types.
- * @dict
- * @type {Object.<string,{name: string, wireType: number}>}
- * @const
- * @expose
- */
- ProtoBuf.TYPES = {
- // According to the protobuf spec.
- "int32": {
- name: "int32",
- wireType: ProtoBuf.WIRE_TYPES.VARINT
- },
- "uint32": {
- name: "uint32",
- wireType: ProtoBuf.WIRE_TYPES.VARINT
- },
- "sint32": {
- name: "sint32",
- wireType: ProtoBuf.WIRE_TYPES.VARINT
- },
- "int64": {
- name: "int64",
- wireType: ProtoBuf.WIRE_TYPES.VARINT
- },
- "uint64": {
- name: "uint64",
- wireType: ProtoBuf.WIRE_TYPES.VARINT
- },
- "sint64": {
- name: "sint64",
- wireType: ProtoBuf.WIRE_TYPES.VARINT
- },
- "bool": {
- name: "bool",
- wireType: ProtoBuf.WIRE_TYPES.VARINT
- },
- "double": {
- name: "double",
- wireType: ProtoBuf.WIRE_TYPES.BITS64
- },
- "string": {
- name: "string",
- wireType: ProtoBuf.WIRE_TYPES.LDELIM
- },
- "bytes": {
- name: "bytes",
- wireType: ProtoBuf.WIRE_TYPES.LDELIM
- },
- "fixed32": {
- name: "fixed32",
- wireType: ProtoBuf.WIRE_TYPES.BITS32
- },
- "sfixed32": {
- name: "sfixed32",
- wireType: ProtoBuf.WIRE_TYPES.BITS32
- },
- "fixed64": {
- name: "fixed64",
- wireType: ProtoBuf.WIRE_TYPES.BITS64
- },
- "sfixed64": {
- name: "sfixed64",
- wireType: ProtoBuf.WIRE_TYPES.BITS64
- },
- "float": {
- name: "float",
- wireType: ProtoBuf.WIRE_TYPES.BITS32
- },
- "enum": {
- name: "enum",
- wireType: ProtoBuf.WIRE_TYPES.VARINT
- },
- "message": {
- name: "message",
- wireType: ProtoBuf.WIRE_TYPES.LDELIM
- },
- "group": {
- name: "group",
- wireType: ProtoBuf.WIRE_TYPES.STARTGROUP
- }
- };
- /**
- * Minimum field id.
- * @type {number}
- * @const
- * @expose
- */
- ProtoBuf.ID_MIN = 1;
- /**
- * Maximum field id.
- * @type {number}
- * @const
- * @expose
- */
- ProtoBuf.ID_MAX = 0x1FFFFFFF;
- /**
- * @type {!function(new: ByteBuffer, ...[*])}
- * @expose
- */
- ProtoBuf.ByteBuffer = ByteBuffer;
- /**
- * @type {?function(new: Long, ...[*])}
- * @expose
- */
- ProtoBuf.Long = ByteBuffer.Long || null;
- /**
- * If set to `true`, field names will be converted from underscore notation to camel case. Defaults to `false`.
- * Must be set prior to parsing.
- * @type {boolean}
- * @expose
- */
- ProtoBuf.convertFieldsToCamelCase = false;
- /**
- * By default, messages are populated with (setX, set_x) accessors for each field. This can be disabled by
- * setting this to `false` prior to building messages.
- * @type {boolean}
- * @expose
- */
- ProtoBuf.populateAccessors = true;
- /**
- * @alias ProtoBuf.Util
- * @expose
- */
- ProtoBuf.Util = (function() {
- "use strict";
- // Object.create polyfill
- // ref: https://developer.mozilla.org/de/docs/JavaScript/Reference/Global_Objects/Object/create
- if (!Object.create)
- /** @expose */
- Object.create = function (o) {
- if (arguments.length > 1)
- throw Error('Object.create polyfill only accepts the first parameter.');
- function F() {}
- F.prototype = o;
- return new F();
- };
- /**
- * ProtoBuf utilities.
- * @exports ProtoBuf.Util
- * @namespace
- */
- var Util = {};
- /**
- * Flag if running in node (fs is available) or not.
- * @type {boolean}
- * @const
- * @expose
- */
- Util.IS_NODE = false;
- try {
- // There is no reliable way to detect node.js as an environment, so our
- // best bet is to feature-detect what we actually need.
- Util.IS_NODE =
- typeof require === 'function' &&
- typeof require("fs").readFileSync === 'function' &&
- typeof require("path").resolve === 'function';
- } catch (e) {}
- /**
- * Constructs a XMLHttpRequest object.
- * @return {XMLHttpRequest}
- * @throws {Error} If XMLHttpRequest is not supported
- * @expose
- */
- Util.XHR = function() {
- // No dependencies please, ref: http://www.quirksmode.org/js/xmlhttp.html
- var XMLHttpFactories = [
- function () {return new XMLHttpRequest()},
- function () {return new ActiveXObject("Msxml2.XMLHTTP")},
- function () {return new ActiveXObject("Msxml3.XMLHTTP")},
- function () {return new ActiveXObject("Microsoft.XMLHTTP")}
- ];
- /** @type {?XMLHttpRequest} */
- var xhr = null;
- for (var i=0;i<XMLHttpFactories.length;i++) {
- try { xhr = XMLHttpFactories[i](); }
- catch (e) { continue; }
- break;
- }
- if (!xhr)
- throw Error("XMLHttpRequest is not supported");
- return xhr;
- };
- /**
- * Fetches a resource.
- * @param {string} path Resource path
- * @param {function(?string)=} callback Callback receiving the resource's contents. If omitted the resource will
- * be fetched synchronously. If the request failed, contents will be null.
- * @return {?string|undefined} Resource contents if callback is omitted (null if the request failed), else undefined.
- * @expose
- */
- Util.fetch = function(path, callback) {
- if (callback && typeof callback != 'function')
- callback = null;
- if (Util.IS_NODE) {
- if (callback) {
- require("fs").readFile(path, function(err, data) {
- if (err)
- callback(null);
- else
- callback(""+data);
- });
- } else
- try {
- return require("fs").readFileSync(path);
- } catch (e) {
- return null;
- }
- } else {
- var xhr = Util.XHR();
- xhr.open('GET', path, callback ? true : false);
- // xhr.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
- xhr.setRequestHeader('Accept', 'text/plain');
- if (typeof xhr.overrideMimeType === 'function') xhr.overrideMimeType('text/plain');
- if (callback) {
- xhr.onreadystatechange = function() {
- if (xhr.readyState != 4) return;
- if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))
- callback(xhr.responseText);
- else
- callback(null);
- };
- if (xhr.readyState == 4)
- return;
- xhr.send(null);
- } else {
- xhr.send(null);
- if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))
- return xhr.responseText;
- return null;
- }
- }
- };
- /**
- * Tests if an object is an array.
- * @function
- * @param {*} obj Object to test
- * @returns {boolean} true if it is an array, else false
- * @expose
- */
- Util.isArray = Array.isArray || function(obj) {
- return Object.prototype.toString.call(obj) === "[object Array]";
- };
- return Util;
- })();
- /**
- * Language expressions.
- * @type {!Object.<string,string|!RegExp>}
- * @expose
- */
- ProtoBuf.Lang = {
- OPEN: "{",
- CLOSE: "}",
- OPTOPEN: "[",
- OPTCLOSE: "]",
- OPTEND: ",",
- EQUAL: "=",
- END: ";",
- STRINGOPEN: '"',
- STRINGCLOSE: '"',
- STRINGOPEN_SQ: "'",
- STRINGCLOSE_SQ: "'",
- COPTOPEN: '(',
- COPTCLOSE: ')',
- DELIM: /[\s\{\}=;\[\],'"\(\)]/g,
- // KEYWORD: /^(?:package|option|import|message|enum|extend|service|syntax|extensions|group)$/,
- RULE: /^(?:required|optional|repeated)$/,
- TYPE: /^(?:double|float|int32|uint32|sint32|int64|uint64|sint64|fixed32|sfixed32|fixed64|sfixed64|bool|string|bytes)$/,
- NAME: /^[a-zA-Z_][a-zA-Z_0-9]*$/,
- TYPEDEF: /^[a-zA-Z][a-zA-Z_0-9]*$/,
- TYPEREF: /^(?:\.?[a-zA-Z_][a-zA-Z_0-9]*)+$/,
- FQTYPEREF: /^(?:\.[a-zA-Z][a-zA-Z_0-9]*)+$/,
- NUMBER: /^-?(?:[1-9][0-9]*|0|0x[0-9a-fA-F]+|0[0-7]+|([0-9]*\.[0-9]+([Ee][+-]?[0-9]+)?))$/,
- NUMBER_DEC: /^(?:[1-9][0-9]*|0)$/,
- NUMBER_HEX: /^0x[0-9a-fA-F]+$/,
- NUMBER_OCT: /^0[0-7]+$/,
- NUMBER_FLT: /^[0-9]*\.[0-9]+([Ee][+-]?[0-9]+)?$/,
- ID: /^(?:[1-9][0-9]*|0|0x[0-9a-fA-F]+|0[0-7]+)$/,
- NEGID: /^\-?(?:[1-9][0-9]*|0|0x[0-9a-fA-F]+|0[0-7]+)$/,
- WHITESPACE: /\s/,
- STRING: /['"]([^'"\\]*(\\.[^"\\]*)*)['"]/g,
- BOOL: /^(?:true|false)$/i
- };
- /**
- * @alias ProtoBuf.DotProto
- * @expose
- */
- ProtoBuf.DotProto = (function(ProtoBuf, Lang) {
- "use strict";
- /**
- * Utilities to parse .proto files.
- * @exports ProtoBuf.DotProto
- * @namespace
- */
- var DotProto = {};
- /**
- * Constructs a new Tokenizer.
- * @exports ProtoBuf.DotProto.Tokenizer
- * @class prototype tokenizer
- * @param {string} proto Proto to tokenize
- * @constructor
- */
- var Tokenizer = function(proto) {
- /**
- * Source to parse.
- * @type {string}
- * @expose
- */
- this.source = ""+proto; // In case it's a buffer
- /**
- * Current index.
- * @type {number}
- * @expose
- */
- this.index = 0;
- /**
- * Current line.
- * @type {number}
- * @expose
- */
- this.line = 1;
- /**
- * Stacked values.
- * @type {Array}
- * @expose
- */
- this.stack = [];
- /**
- * Whether currently reading a string or not.
- * @type {boolean}
- * @expose
- */
- this.readingString = false;
- /**
- * Whatever character ends the string. Either a single or double quote character.
- * @type {string}
- * @expose
- */
- this.stringEndsWith = Lang.STRINGCLOSE;
- };
- /**
- * @alias ProtoBuf.DotProto.Tokenizer.prototype
- * @inner
- */
- var TokenizerPrototype = Tokenizer.prototype;
- /**
- * Reads a string beginning at the current index.
- * @return {string} The string
- * @throws {Error} If it's not a valid string
- * @private
- */
- TokenizerPrototype._readString = function() {
- Lang.STRING.lastIndex = this.index-1; // Include the open quote
- var match;
- if ((match = Lang.STRING.exec(this.source)) !== null) {
- var s = match[1];
- this.index = Lang.STRING.lastIndex;
- this.stack.push(this.stringEndsWith);
- return s;
- }
- throw Error("Unterminated string at line "+this.line+", index "+this.index);
- };
- /**
- * Gets the next token and advances by one.
- * @return {?string} Token or `null` on EOF
- * @throws {Error} If it's not a valid proto file
- * @expose
- */
- TokenizerPrototype.next = function() {
- if (this.stack.length > 0)
- return this.stack.shift();
- if (this.index >= this.source.length)
- return null; // No more tokens
- if (this.readingString) {
- this.readingString = false;
- return this._readString();
- }
- var repeat, last;
- do {
- repeat = false;
- // Strip white spaces
- while (Lang.WHITESPACE.test(last = this.source.charAt(this.index))) {
- this.index++;
- if (last === "\n")
- this.line++;
- if (this.index === this.source.length)
- return null;
- }
- // Strip comments
- if (this.source.charAt(this.index) === '/') {
- if (this.source.charAt(++this.index) === '/') { // Single line
- while (this.source.charAt(this.index) !== "\n") {
- this.index++;
- if (this.index == this.source.length)
- return null;
- }
- this.index++;
- this.line++;
- repeat = true;
- } else if (this.source.charAt(this.index) === '*') { /* Block */
- last = '';
- while (last+(last=this.source.charAt(this.index)) !== '*/') {
- this.index++;
- if (last === "\n")
- this.line++;
- if (this.index === this.source.length)
- return null;
- }
- this.index++;
- repeat = true;
- } else
- throw Error("Unterminated comment at line "+this.line+": /"+this.source.charAt(this.index));
- }
- } while (repeat);
- if (this.index === this.source.length) return null;
- // Read the next token
- var end = this.index;
- Lang.DELIM.lastIndex = 0;
- var delim = Lang.DELIM.test(this.source.charAt(end));
- if (!delim) {
- ++end;
- while(end < this.source.length && !Lang.DELIM.test(this.source.charAt(end)))
- end++;
- } else
- ++end;
- var token = this.source.substring(this.index, this.index = end);
- if (token === Lang.STRINGOPEN)
- this.readingString = true,
- this.stringEndsWith = Lang.STRINGCLOSE;
- else if (token === Lang.STRINGOPEN_SQ)
- this.readingString = true,
- this.stringEndsWith = Lang.STRINGCLOSE_SQ;
- return token;
- };
- /**
- * Peeks for the next token.
- * @return {?string} Token or `null` on EOF
- * @throws {Error} If it's not a valid proto file
- * @expose
- */
- TokenizerPrototype.peek = function() {
- if (this.stack.length === 0) {
- var token = this.next();
- if (token === null)
- return null;
- this.stack.push(token);
- }
- return this.stack[0];
- };
- /**
- * Returns a string representation of this object.
- * @return {string} String representation as of "Tokenizer(index/length)"
- * @expose
- */
- TokenizerPrototype.toString = function() {
- return "Tokenizer("+this.index+"/"+this.source.length+" at line "+this.line+")";
- };
- /**
- * @alias ProtoBuf.DotProto.Tokenizer
- * @expose
- */
- DotProto.Tokenizer = Tokenizer;
- /**
- * Constructs a new Parser.
- * @exports ProtoBuf.DotProto.Parser
- * @class prototype parser
- * @param {string} proto Protocol source
- * @constructor
- */
- var Parser = function(proto) {
- /**
- * Tokenizer.
- * @type {ProtoBuf.DotProto.Tokenizer}
- * @expose
- */
- this.tn = new Tokenizer(proto);
- };
- /**
- * @alias ProtoBuf.DotProto.Parser.prototype
- * @inner
- */
- var ParserPrototype = Parser.prototype;
- /**
- * Runs the parser.
- * @return {{package: string|null, messages: Array.<object>, enums: Array.<object>, imports: Array.<string>, options: object<string,*>}}
- * @throws {Error} If the source cannot be parsed
- * @expose
- */
- ParserPrototype.parse = function() {
- var topLevel = {
- "name": "[ROOT]", // temporary
- "package": null,
- "messages": [],
- "enums": [],
- "imports": [],
- "options": {},
- "services": []
- };
- var token, head = true;
- while(token = this.tn.next()) {
- switch (token) {
- case 'package':
- if (!head || topLevel["package"] !== null)
- throw Error("Unexpected package at line "+this.tn.line);
- topLevel["package"] = this._parsePackage(token);
- break;
- case 'import':
- if (!head)
- throw Error("Unexpected import at line "+this.tn.line);
- topLevel.imports.push(this._parseImport(token));
- break;
- case 'message':
- this._parseMessage(topLevel, null, token);
- head = false;
- break;
- case 'enum':
- this._parseEnum(topLevel, token);
- head = false;
- break;
- case 'option':
- if (!head)
- throw Error("Unexpected option at line "+this.tn.line);
- this._parseOption(topLevel, token);
- break;
- case 'service':
- this._parseService(topLevel, token);
- break;
- case 'extend':
- this._parseExtend(topLevel, token);
- break;
- case 'syntax':
- this._parseIgnoredStatement(topLevel, token);
- break;
- default:
- throw Error("Unexpected token at line "+this.tn.line+": "+token);
- }
- }
- delete topLevel["name"];
- return topLevel;
- };
- /**
- * Parses a number value.
- * @param {string} val Number value to parse
- * @return {number} Number
- * @throws {Error} If the number value is invalid
- * @private
- */
- ParserPrototype._parseNumber = function(val) {
- var sign = 1;
- if (val.charAt(0) == '-')
- sign = -1,
- val = val.substring(1);
- if (Lang.NUMBER_DEC.test(val))
- return sign*parseInt(val, 10);
- else if (Lang.NUMBER_HEX.test(val))
- return sign*parseInt(val.substring(2), 16);
- else if (Lang.NUMBER_OCT.test(val))
- return sign*parseInt(val.substring(1), 8);
- else if (Lang.NUMBER_FLT.test(val))
- return sign*parseFloat(val);
- throw Error("Illegal number at line "+this.tn.line+": "+(sign < 0 ? '-' : '')+val);
- };
- /**
- * Parses a (possibly multiline) string.
- * @returns {string}
- * @private
- */
- ParserPrototype._parseString = function() {
- var value = "", token;
- do {
- token = this.tn.next(); // Known to be = this.tn.stringEndsWith
- value += this.tn.next();
- token = this.tn.next();
- if (token !== this.tn.stringEndsWith)
- throw Error("Illegal end of string at line "+this.tn.line+": "+token);
- token = this.tn.peek();
- } while (token === Lang.STRINGOPEN || token === Lang.STRINGOPEN_SQ);
- return value;
- };
- /**
- * Parses an ID value.
- * @param {string} val ID value to parse
- * @param {boolean=} neg Whether the ID may be negative, defaults to `false`
- * @returns {number} ID
- * @throws {Error} If the ID value is invalid
- * @private
- */
- ParserPrototype._parseId = function(val, neg) {
- var id = -1;
- var sign = 1;
- if (val.charAt(0) == '-')
- sign = -1,
- val = val.substring(1);
- if (Lang.NUMBER_DEC.test(val))
- id = parseInt(val);
- else if (Lang.NUMBER_HEX.test(val))
- id = parseInt(val.substring(2), 16);
- else if (Lang.NUMBER_OCT.test(val))
- id = parseInt(val.substring(1), 8);
- else
- throw Error("Illegal id at line "+this.tn.line+": "+(sign < 0 ? '-' : '')+val);
- id = (sign*id)|0; // Force to 32bit
- if (!neg && id < 0)
- throw Error("Illegal id at line "+this.tn.line+": "+(sign < 0 ? '-' : '')+val);
- return id;
- };
- /**
- * Parses the package definition.
- * @param {string} token Initial token
- * @return {string} Package name
- * @throws {Error} If the package definition cannot be parsed
- * @private
- */
- ParserPrototype._parsePackage = function(token) {
- token = this.tn.next();
- if (!Lang.TYPEREF.test(token))
- throw Error("Illegal package name at line "+this.tn.line+": "+token);
- var pkg = token;
- token = this.tn.next();
- if (token != Lang.END)
- throw Error("Illegal end of package at line "+this.tn.line+": "+token);
- return pkg;
- };
- /**
- * Parses an import definition.
- * @param {string} token Initial token
- * @return {string} Import file name
- * @throws {Error} If the import definition cannot be parsed
- * @private
- */
- ParserPrototype._parseImport = function(token) {
- token = this.tn.peek();
- if (token === "public")
- this.tn.next(),
- token = this.tn.peek();
- if (token !== Lang.STRINGOPEN && token !== Lang.STRINGOPEN_SQ)
- throw Error("Illegal start of import at line "+this.tn.line+": "+token);
- var imported = this._parseString();
- token = this.tn.next();
- if (token !== Lang.END)
- throw Error("Illegal end of import at line "+this.tn.line+": "+token);
- return imported;
- };
- /**
- * Parses a namespace option.
- * @param {Object} parent Parent definition
- * @param {string} token Initial token
- * @throws {Error} If the option cannot be parsed
- * @private
- */
- ParserPrototype._parseOption = function(parent, token) {
- token = this.tn.next();
- var custom = false;
- if (token == Lang.COPTOPEN)
- custom = true,
- token = this.tn.next();
- if (!Lang.TYPEREF.test(token))
- // we can allow options of the form google.protobuf.* since they will just get ignored anyways
- if (!/google\.protobuf\./.test(token))
- throw Error("Illegal option name in message "+parent.name+" at line "+this.tn.line+": "+token);
- var name = token;
- token = this.tn.next();
- if (custom) { // (my_method_option).foo, (my_method_option), some_method_option, (foo.my_option).bar
- if (token !== Lang.COPTCLOSE)
- throw Error("Illegal end in message "+parent.name+", option "+name+" at line "+this.tn.line+": "+token);
- name = '('+name+')';
- token = this.tn.next();
- if (Lang.FQTYPEREF.test(token))
- name += token,
- token = this.tn.next();
- }
- if (token !== Lang.EQUAL)
- throw Error("Illegal operator in message "+parent.name+", option "+name+" at line "+this.tn.line+": "+token);
- var value;
- token = this.tn.peek();
- if (token === Lang.STRINGOPEN || token === Lang.STRINGOPEN_SQ)
- value = this._parseString();
- else {
- this.tn.next();
- if (Lang.NUMBER.test(token))
- value = this._parseNumber(token, true);
- else if (Lang.BOOL.test(token))
- value = token === 'true';
- else if (Lang.TYPEREF.test(token))
- value = token;
- else
- throw Error("Illegal option value in message "+parent.name+", option "+name+" at line "+this.tn.line+": "+token);
- }
- token = this.tn.next();
- if (token !== Lang.END)
- throw Error("Illegal end of option in message "+parent.name+", option "+name+" at line "+this.tn.line+": "+token);
- parent["options"][name] = value;
- };
- /**
- * Parses an ignored statement of the form ['keyword', ..., ';'].
- * @param {Object} parent Parent definition
- * @param {string} keyword Initial token
- * @throws {Error} If the directive cannot be parsed
- * @private
- */
- ParserPrototype._parseIgnoredStatement = function(parent, keyword) {
- var token;
- do {
- token = this.tn.next();
- if (token === null)
- throw Error("Unexpected EOF in "+parent.name+", "+keyword+" at line "+this.tn.line);
- if (token === Lang.END)
- break;
- } while (true);
- };
- /**
- * Parses a service definition.
- * @param {Object} parent Parent definition
- * @param {string} token Initial token
- * @throws {Error} If the service cannot be parsed
- * @private
- */
- ParserPrototype._parseService = function(parent, token) {
- token = this.tn.next();
- if (!Lang.NAME.test(token))
- throw Error("Illegal service name at line "+this.tn.line+": "+token);
- var name = token;
- var svc = {
- "name": name,
- "rpc": {},
- "options": {}
- };
- token = this.tn.next();
- if (token !== Lang.OPEN)
- throw Error("Illegal start of service "+name+" at line "+this.tn.line+": "+token);
- do {
- token = this.tn.next();
- if (token === "option")
- this._parseOption(svc, token);
- else if (token === 'rpc')
- this._parseServiceRPC(svc, token);
- else if (token !== Lang.CLOSE)
- throw Error("Illegal type of service "+name+" at line "+this.tn.line+": "+token);
- } while (token !== Lang.CLOSE);
- parent["services"].push(svc);
- };
- /**
- * Parses a RPC service definition of the form ['rpc', name, (request), 'returns', (response)].
- * @param {Object} svc Parent definition
- * @param {string} token Initial token
- * @private
- */
- ParserPrototype._parseServiceRPC = function(svc, token) {
- var type = token;
- token = this.tn.next();
- if (!Lang.NAME.test(token))
- throw Error("Illegal method name in service "+svc["name"]+" at line "+this.tn.line+": "+token);
- var name = token;
- var method = {
- "request": null,
- "response": null,
- "options": {}
- };
- token = this.tn.next();
- if (token !== Lang.COPTOPEN)
- throw Error("Illegal start of request type in service "+svc["name"]+"#"+name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- if (!Lang.TYPEREF.test(token))
- throw Error("Illegal request type in service "+svc["name"]+"#"+name+" at line "+this.tn.line+": "+token);
- method["request"] = token;
- token = this.tn.next();
- if (token != Lang.COPTCLOSE)
- throw Error("Illegal end of request type in service "+svc["name"]+"#"+name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- if (token.toLowerCase() !== "returns")
- throw Error("Illegal delimiter in service "+svc["name"]+"#"+name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- if (token != Lang.COPTOPEN)
- throw Error("Illegal start of response type in service "+svc["name"]+"#"+name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- method["response"] = token;
- token = this.tn.next();
- if (token !== Lang.COPTCLOSE)
- throw Error("Illegal end of response type in service "+svc["name"]+"#"+name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- if (token === Lang.OPEN) {
- do {
- token = this.tn.next();
- if (token === 'option')
- this._parseOption(method, token); // <- will fail for the custom-options example
- else if (token !== Lang.CLOSE)
- throw Error("Illegal start of option inservice "+svc["name"]+"#"+name+" at line "+this.tn.line+": "+token);
- } while (token !== Lang.CLOSE);
- if (this.tn.peek() === Lang.END)
- this.tn.next();
- } else if (token !== Lang.END)
- throw Error("Illegal delimiter in service "+svc["name"]+"#"+name+" at line "+this.tn.line+": "+token);
- if (typeof svc[type] === 'undefined')
- svc[type] = {};
- svc[type][name] = method;
- };
- /**
- * Parses a message definition.
- * @param {Object} parent Parent definition
- * @param {Object} fld Field definition if this is a group, otherwise `null`
- * @param {string} token First token
- * @return {Object}
- * @throws {Error} If the message cannot be parsed
- * @private
- */
- ParserPrototype._parseMessage = function(parent, fld, token) {
- /** @dict */
- var msg = {}; // Note: At some point we might want to exclude the parser, so we need a dict.
- var isGroup = token === "group";
- token = this.tn.next();
- if (!Lang.NAME.test(token))
- throw Error("Illegal "+(isGroup ? "group" : "message")+" name"+(parent ? " in message "+parent["name"] : "")+" at line "+this.tn.line+": "+token);
- msg["name"] = token;
- if (isGroup) {
- token = this.tn.next();
- if (token !== Lang.EQUAL)
- throw Error("Illegal id assignment after group "+msg.name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- try {
- fld["id"] = this._parseId(token);
- } catch (e) {
- throw Error("Illegal field id value for group "+msg.name+"#"+fld.name+" at line "+this.tn.line+": "+token);
- }
- msg["isGroup"] = true;
- }
- msg["fields"] = []; // Note: Using arrays to support also browser that cannot preserve order of object keys.
- msg["enums"] = [];
- msg["messages"] = [];
- msg["options"] = {};
- msg["oneofs"] = {};
- token = this.tn.next();
- if (token === Lang.OPTOPEN && fld)
- this._parseFieldOptions(msg, fld, token),
- token = this.tn.next();
- if (token !== Lang.OPEN)
- throw Error("Illegal start of "+(isGroup ? "group" : "message")+" "+msg.name+" at line "+this.tn.line+": "+token);
- // msg["extensions"] = undefined
- do {
- token = this.tn.next();
- if (token === Lang.CLOSE) {
- token = this.tn.peek();
- if (token === Lang.END)
- this.tn.next();
- break;
- } else if (Lang.RULE.test(token))
- this._parseMessageField(msg, token);
- else if (token === "oneof")
- this._parseMessageOneOf(msg, token);
- else if (token === "enum")
- this._parseEnum(msg, token);
- else if (token === "message")
- this._parseMessage(msg, null, token);
- else if (token === "option")
- this._parseOption(msg, token);
- else if (token === "extensions")
- msg["extensions"] = this._parseExtensions(msg, token);
- else if (token === "extend")
- this._parseExtend(msg, token);
- else
- throw Error("Illegal token in message "+msg.name+" at line "+this.tn.line+": "+token);
- } while (true);
- parent["messages"].push(msg);
- return msg;
- };
- /**
- * Parses a message field.
- * @param {Object} msg Message definition
- * @param {string} token Initial token
- * @returns {!Object} Field descriptor
- * @throws {Error} If the message field cannot be parsed
- * @private
- */
- ParserPrototype._parseMessageField = function(msg, token) {
- /** @dict */
- var fld = {}, grp = null;
- fld["rule"] = token;
- /** @dict */
- fld["options"] = {};
- token = this.tn.next();
- if (token === "group") {
- // "A [legacy] group simply combines a nested message type and a field into a single declaration. In your
- // code, you can treat this message just as if it had a Result type field called result (the latter name is
- // converted to lower-case so that it does not conflict with the former)."
- grp = this._parseMessage(msg, fld, token);
- if (!/^[A-Z]/.test(grp["name"]))
- throw Error('Group names must start with a capital letter');
- fld["type"] = grp["name"];
- fld["name"] = grp["name"].toLowerCase();
- token = this.tn.peek();
- if (token === Lang.END)
- this.tn.next();
- } else {
- if (!Lang.TYPE.test(token) && !Lang.TYPEREF.test(token))
- throw Error("Illegal field type in message "+msg.name+" at line "+this.tn.line+": "+token);
- fld["type"] = token;
- token = this.tn.next();
- if (!Lang.NAME.test(token))
- throw Error("Illegal field name in message "+msg.name+" at line "+this.tn.line+": "+token);
- fld["name"] = token;
- token = this.tn.next();
- if (token !== Lang.EQUAL)
- throw Error("Illegal token in field "+msg.name+"#"+fld.name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- try {
- fld["id"] = this._parseId(token);
- } catch (e) {
- throw Error("Illegal field id in message "+msg.name+"#"+fld.name+" at line "+this.tn.line+": "+token);
- }
- token = this.tn.next();
- if (token === Lang.OPTOPEN)
- this._parseFieldOptions(msg, fld, token),
- token = this.tn.next();
- if (token !== Lang.END)
- throw Error("Illegal delimiter in message "+msg.name+"#"+fld.name+" at line "+this.tn.line+": "+token);
- }
- msg["fields"].push(fld);
- return fld;
- };
- /**
- * Parses a message oneof.
- * @param {Object} msg Message definition
- * @param {string} token Initial token
- * @throws {Error} If the message oneof cannot be parsed
- * @private
- */
- ParserPrototype._parseMessageOneOf = function(msg, token) {
- token = this.tn.next();
- if (!Lang.NAME.test(token))
- throw Error("Illegal oneof name in message "+msg.name+" at line "+this.tn.line+": "+token);
- var name = token,
- fld;
- var fields = [];
- token = this.tn.next();
- if (token !== Lang.OPEN)
- throw Error("Illegal start of oneof "+name+" at line "+this.tn.line+": "+token);
- while (this.tn.peek() !== Lang.CLOSE) {
- fld = this._parseMessageField(msg, "optional");
- fld["oneof"] = name;
- fields.push(fld["id"]);
- }
- this.tn.next();
- msg["oneofs"][name] = fields;
- };
- /**
- * Parses a set of field option definitions.
- * @param {Object} msg Message definition
- * @param {Object} fld Field definition
- * @param {string} token Initial token
- * @throws {Error} If the message field options cannot be parsed
- * @private
- */
- ParserPrototype._parseFieldOptions = function(msg, fld, token) {
- var first = true;
- do {
- token = this.tn.next();
- if (token === Lang.OPTCLOSE)
- break;
- else if (token === Lang.OPTEND) {
- if (first)
- throw Error("Illegal start of options in message "+msg.name+"#"+fld.name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- }
- this._parseFieldOption(msg, fld, token);
- first = false;
- } while (true);
- };
- /**
- * Parses a single field option.
- * @param {Object} msg Message definition
- * @param {Object} fld Field definition
- * @param {string} token Initial token
- * @throws {Error} If the mesage field option cannot be parsed
- * @private
- */
- ParserPrototype._parseFieldOption = function(msg, fld, token) {
- var custom = false;
- if (token === Lang.COPTOPEN)
- token = this.tn.next(),
- custom = true;
- if (!Lang.TYPEREF.test(token))
- throw Error("Illegal field option in "+msg.name+"#"+fld.name+" at line "+this.tn.line+": "+token);
- var name = token;
- token = this.tn.next();
- if (custom) {
- if (token !== Lang.COPTCLOSE)
- throw Error("Illegal delimiter in "+msg.name+"#"+fld.name+" at line "+this.tn.line+": "+token);
- name = '('+name+')';
- token = this.tn.next();
- if (Lang.FQTYPEREF.test(token))
- name += token,
- token = this.tn.next();
- }
- if (token !== Lang.EQUAL)
- throw Error("Illegal token in "+msg.name+"#"+fld.name+" at line "+this.tn.line+": "+token);
- var value;
- token = this.tn.peek();
- if (token === Lang.STRINGOPEN || token === Lang.STRINGOPEN_SQ) {
- value = this._parseString();
- } else if (Lang.NUMBER.test(token, true))
- value = this._parseNumber(this.tn.next(), true);
- else if (Lang.BOOL.test(token))
- value = this.tn.next().toLowerCase() === 'true';
- else if (Lang.TYPEREF.test(token))
- value = this.tn.next(); // TODO: Resolve?
- else
- throw Error("Illegal value in message "+msg.name+"#"+fld.name+", option "+name+" at line "+this.tn.line+": "+token);
- fld["options"][name] = value;
- };
- /**
- * Parses an enum.
- * @param {Object} msg Message definition
- * @param {string} token Initial token
- * @throws {Error} If the enum cannot be parsed
- * @private
- */
- ParserPrototype._parseEnum = function(msg, token) {
- /** @dict */
- var enm = {};
- token = this.tn.next();
- if (!Lang.NAME.test(token))
- throw Error("Illegal enum name in message "+msg.name+" at line "+this.tn.line+": "+token);
- enm["name"] = token;
- token = this.tn.next();
- if (token !== Lang.OPEN)
- throw Error("Illegal start of enum "+enm.name+" at line "+this.tn.line+": "+token);
- enm["values"] = [];
- enm["options"] = {};
- do {
- token = this.tn.next();
- if (token === Lang.CLOSE) {
- token = this.tn.peek();
- if (token === Lang.END)
- this.tn.next();
- break;
- }
- if (token == 'option')
- this._parseOption(enm, token);
- else {
- if (!Lang.NAME.test(token))
- throw Error("Illegal name in enum "+enm.name+" at line "+this.tn.line+": "+token);
- this._parseEnumValue(enm, token);
- }
- } while (true);
- msg["enums"].push(enm);
- };
- /**
- * Parses an enum value.
- * @param {Object} enm Enum definition
- * @param {string} token Initial token
- * @throws {Error} If the enum value cannot be parsed
- * @private
- */
- ParserPrototype._parseEnumValue = function(enm, token) {
- /** @dict */
- var val = {};
- val["name"] = token;
- token = this.tn.next();
- if (token !== Lang.EQUAL)
- throw Error("Illegal token in enum "+enm.name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- try {
- val["id"] = this._parseId(token, true);
- } catch (e) {
- throw Error("Illegal id in enum "+enm.name+" at line "+this.tn.line+": "+token);
- }
- enm["values"].push(val);
- token = this.tn.next();
- if (token === Lang.OPTOPEN) {
- var opt = { 'options' : {} }; // TODO: Actually expose them somehow.
- this._parseFieldOptions(enm, opt, token);
- token = this.tn.next();
- }
- if (token !== Lang.END)
- throw Error("Illegal delimiter in enum "+enm.name+" at line "+this.tn.line+": "+token);
- };
- /**
- * Parses an extensions statement.
- * @param {Object} msg Message object
- * @param {string} token Initial token
- * @throws {Error} If the extensions statement cannot be parsed
- * @private
- */
- ParserPrototype._parseExtensions = function(msg, token) {
- /** @type {Array.<number>} */
- var range = [];
- token = this.tn.next();
- if (token === "min") // FIXME: Does the official implementation support this?
- range.push(ProtoBuf.ID_MIN);
- else if (token === "max")
- range.push(ProtoBuf.ID_MAX);
- else
- range.push(this._parseNumber(token));
- token = this.tn.next();
- if (token !== 'to')
- throw Error("Illegal extensions delimiter in message "+msg.name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- if (token === "min")
- range.push(ProtoBuf.ID_MIN);
- else if (token === "max")
- range.push(ProtoBuf.ID_MAX);
- else
- range.push(this._parseNumber(token));
- token = this.tn.next();
- if (token !== Lang.END)
- throw Error("Illegal extensions delimiter in message "+msg.name+" at line "+this.tn.line+": "+token);
- return range;
- };
- /**
- * Parses an extend block.
- * @param {Object} parent Parent object
- * @param {string} token Initial token
- * @throws {Error} If the extend block cannot be parsed
- * @private
- */
- ParserPrototype._parseExtend = function(parent, token) {
- token = this.tn.next();
- if (!Lang.TYPEREF.test(token))
- throw Error("Illegal message name at line "+this.tn.line+": "+token);
- /** @dict */
- var ext = {};
- ext["ref"] = token;
- ext["fields"] = [];
- token = this.tn.next();
- if (token !== Lang.OPEN)
- throw Error("Illegal start of extend "+ext.name+" at line "+this.tn.line+": "+token);
- do {
- token = this.tn.next();
- if (token === Lang.CLOSE) {
- token = this.tn.peek();
- if (token == Lang.END)
- this.tn.next();
- break;
- } else if (Lang.RULE.test(token))
- this._parseMessageField(ext, token);
- else
- throw Error("Illegal token in extend "+ext.name+" at line "+this.tn.line+": "+token);
- } while (true);
- parent["messages"].push(ext);
- return ext;
- };
- /**
- * Returns a string representation of this object.
- * @returns {string} String representation as of "Parser"
- */
- ParserPrototype.toString = function() {
- return "Parser";
- };
- /**
- * @alias ProtoBuf.DotProto.Parser
- * @expose
- */
- DotProto.Parser = Parser;
- return DotProto;
- })(ProtoBuf, ProtoBuf.Lang);
- /**
- * @alias ProtoBuf.Reflect
- * @expose
- */
- ProtoBuf.Reflect = (function(ProtoBuf) {
- "use strict";
- /**
- * Reflection types.
- * @exports ProtoBuf.Reflect
- * @namespace
- */
- var Reflect = {};
- /**
- * Constructs a Reflect base class.
- * @exports ProtoBuf.Reflect.T
- * @constructor
- * @abstract
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {?ProtoBuf.Reflect.T} parent Parent object
- * @param {string} name Object name
- */
- var T = function(builder, parent, name) {
- /**
- * Builder reference.
- * @type {!ProtoBuf.Builder}
- * @expose
- */
- this.builder = builder;
- /**
- * Parent object.
- * @type {?ProtoBuf.Reflect.T}
- * @expose
- */
- this.parent = parent;
- /**
- * Object name in namespace.
- * @type {string}
- * @expose
- */
- this.name = name;
- /**
- * Fully qualified class name
- * @type {string}
- * @expose
- */
- this.className;
- };
- /**
- * @alias ProtoBuf.Reflect.T.prototype
- * @inner
- */
- var TPrototype = T.prototype;
- /**
- * Returns the fully qualified name of this object.
- * @returns {string} Fully qualified name as of ".PATH.TO.THIS"
- * @expose
- */
- TPrototype.fqn = function() {
- var name = this.name,
- ptr = this;
- do {
- ptr = ptr.parent;
- if (ptr == null)
- break;
- name = ptr.name+"."+name;
- } while (true);
- return name;
- };
- /**
- * Returns a string representation of this Reflect object (its fully qualified name).
- * @param {boolean=} includeClass Set to true to include the class name. Defaults to false.
- * @return String representation
- * @expose
- */
- TPrototype.toString = function(includeClass) {
- return (includeClass ? this.className + " " : "") + this.fqn();
- };
- /**
- * Builds this type.
- * @throws {Error} If this type cannot be built directly
- * @expose
- */
- TPrototype.build = function() {
- throw Error(this.toString(true)+" cannot be built directly");
- };
- /**
- * @alias ProtoBuf.Reflect.T
- * @expose
- */
- Reflect.T = T;
- /**
- * Constructs a new Namespace.
- * @exports ProtoBuf.Reflect.Namespace
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {?ProtoBuf.Reflect.Namespace} parent Namespace parent
- * @param {string} name Namespace name
- * @param {Object.<string,*>=} options Namespace options
- * @constructor
- * @extends ProtoBuf.Reflect.T
- */
- var Namespace = function(builder, parent, name, options) {
- T.call(this, builder, parent, name);
- /**
- * @override
- */
- this.className = "Namespace";
- /**
- * Children inside the namespace.
- * @type {!Array.<ProtoBuf.Reflect.T>}
- */
- this.children = [];
- /**
- * Options.
- * @type {!Object.<string, *>}
- */
- this.options = options || {};
- };
- /**
- * @alias ProtoBuf.Reflect.Namespace.prototype
- * @inner
- */
- var NamespacePrototype = Namespace.prototype = Object.create(T.prototype);
- /**
- * Returns an array of the namespace's children.
- * @param {ProtoBuf.Reflect.T=} type Filter type (returns instances of this type only). Defaults to null (all children).
- * @return {Array.<ProtoBuf.Reflect.T>}
- * @expose
- */
- NamespacePrototype.getChildren = function(type) {
- type = type || null;
- if (type == null)
- return this.children.slice();
- var children = [];
- for (var i=0, k=this.children.length; i<k; ++i)
- if (this.children[i] instanceof type)
- children.push(this.children[i]);
- return children;
- };
- /**
- * Adds a child to the namespace.
- * @param {ProtoBuf.Reflect.T} child Child
- * @throws {Error} If the child cannot be added (duplicate)
- * @expose
- */
- NamespacePrototype.addChild = function(child) {
- var other;
- if (other = this.getChild(child.name)) {
- // Try to revert camelcase transformation on collision
- if (other instanceof Message.Field && other.name !== other.originalName && this.getChild(other.originalName) === null)
- other.name = other.originalName; // Revert previous first (effectively keeps both originals)
- else if (child instanceof Message.Field && child.name !== child.originalName && this.getChild(child.originalName) === null)
- child.name = child.originalName;
- else
- throw Error("Duplicate name in namespace "+this.toString(true)+": "+child.name);
- }
- this.children.push(child);
- };
- /**
- * Gets a child by its name or id.
- * @param {string|number} nameOrId Child name or id
- * @return {?ProtoBuf.Reflect.T} The child or null if not found
- * @expose
- */
- NamespacePrototype.getChild = function(nameOrId) {
- var key = typeof nameOrId === 'number' ? 'id' : 'name';
- for (var i=0, k=this.children.length; i<k; ++i)
- if (this.children[i][key] === nameOrId)
- return this.children[i];
- return null;
- };
- /**
- * Resolves a reflect object inside of this namespace.
- * @param {string} qn Qualified name to resolve
- * @param {boolean=} excludeFields Excludes fields, defaults to `false`
- * @return {?ProtoBuf.Reflect.Namespace} The resolved type or null if not found
- * @expose
- */
- NamespacePrototype.resolve = function(qn, excludeFields) {
- var part = qn.split("."),
- ptr = this,
- i = 0;
- if (part[i] === "") { // Fully qualified name, e.g. ".My.Message'
- while (ptr.parent !== null)
- ptr = ptr.parent;
- i++;
- }
- var child;
- do {
- do {
- child = ptr.getChild(part[i]);
- if (!child || !(child instanceof Reflect.T) || (excludeFields && child instanceof Reflect.Message.Field)) {
- ptr = null;
- break;
- }
- ptr = child; i++;
- } while (i < part.length);
- if (ptr != null)
- break; // Found
- // Else search the parent
- if (this.parent !== null) {
- return this.parent.resolve(qn, excludeFields);
- }
- } while (ptr != null);
- return ptr;
- };
- /**
- * Builds the namespace and returns the runtime counterpart.
- * @return {Object.<string,Function|Object>} Runtime namespace
- * @expose
- */
- NamespacePrototype.build = function() {
- /** @dict */
- var ns = {};
- var children = this.children;
- for (var i=0, k=children.length, child; i<k; ++i) {
- child = children[i];
- if (child instanceof Namespace)
- ns[child.name] = child.build();
- }
- if (Object.defineProperty)
- Object.defineProperty(ns, "$options", { "value": this.buildOpt() });
- return ns;
- };
- /**
- * Builds the namespace's '$options' property.
- * @return {Object.<string,*>}
- */
- NamespacePrototype.buildOpt = function() {
- var opt = {},
- keys = Object.keys(this.options);
- for (var i=0, k=keys.length; i<k; ++i) {
- var key = keys[i],
- val = this.options[keys[i]];
- // TODO: Options are not resolved, yet.
- // if (val instanceof Namespace) {
- // opt[key] = val.build();
- // } else {
- opt[key] = val;
- // }
- }
- return opt;
- };
- /**
- * Gets the value assigned to the option with the specified name.
- * @param {string=} name Returns the option value if specified, otherwise all options are returned.
- * @return {*|Object.<string,*>}null} Option value or NULL if there is no such option
- */
- NamespacePrototype.getOption = function(name) {
- if (typeof name === 'undefined')
- return this.options;
- return typeof this.options[name] !== 'undefined' ? this.options[name] : null;
- };
- /**
- * @alias ProtoBuf.Reflect.Namespace
- * @expose
- */
- Reflect.Namespace = Namespace;
- /**
- * Constructs a new Message.
- * @exports ProtoBuf.Reflect.Message
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.Namespace} parent Parent message or namespace
- * @param {string} name Message name
- * @param {Object.<string,*>=} options Message options
- * @param {boolean=} isGroup `true` if this is a legacy group
- * @constructor
- * @extends ProtoBuf.Reflect.Namespace
- */
- var Message = function(builder, parent, name, options, isGroup) {
- Namespace.call(this, builder, parent, name, options);
- /**
- * @override
- */
- this.className = "Message";
- /**
- * Extensions range.
- * @type {!Array.<number>}
- * @expose
- */
- this.extensions = [ProtoBuf.ID_MIN, ProtoBuf.ID_MAX];
- /**
- * Runtime message class.
- * @type {?function(new:ProtoBuf.Builder.Message)}
- * @expose
- */
- this.clazz = null;
- /**
- * Whether this is a legacy group or not.
- * @type {boolean}
- * @expose
- */
- this.isGroup = !!isGroup;
- // The following cached collections are used to efficiently iterate over or look up fields when decoding.
- /**
- * Cached fields.
- * @type {?Array.<!ProtoBuf.Reflect.Message.Field>}
- * @private
- */
- this._fields = null;
- /**
- * Cached fields by id.
- * @type {?Object.<number,!ProtoBuf.Reflect.Message.Field>}
- * @private
- */
- this._fieldsById = null;
- /**
- * Cached fields by name.
- * @type {?Object.<string,!ProtoBuf.Reflect.Message.Field>}
- * @private
- */
- this._fieldsByName = null;
- };
- /**
- * @alias ProtoBuf.Reflect.Message.prototype
- * @inner
- */
- var MessagePrototype = Message.prototype = Object.create(Namespace.prototype);
- /**
- * Builds the message and returns the runtime counterpart, which is a fully functional class.
- * @see ProtoBuf.Builder.Message
- * @param {boolean=} rebuild Whether to rebuild or not, defaults to false
- * @return {ProtoBuf.Reflect.Message} Message class
- * @throws {Error} If the message cannot be built
- * @expose
- */
- MessagePrototype.build = function(rebuild) {
- if (this.clazz && !rebuild)
- return this.clazz;
- // Create the runtime Message class in its own scope
- var clazz = (function(ProtoBuf, T) {
- var fields = T.getChildren(ProtoBuf.Reflect.Message.Field),
- oneofs = T.getChildren(ProtoBuf.Reflect.Message.OneOf);
- /**
- * Constructs a new runtime Message.
- * @name ProtoBuf.Builder.Message
- * @class Barebone of all runtime messages.
- * @param {!Object.<string,*>|string} values Preset values
- * @param {...string} var_args
- * @constructor
- * @throws {Error} If the message cannot be created
- */
- var Message = function(values, var_args) {
- ProtoBuf.Builder.Message.call(this);
- // Create virtual oneof properties
- for (var i=0, k=oneofs.length; i<k; ++i)
- this[oneofs[i].name] = null;
- // Create fields and set default values
- for (i=0, k=fields.length; i<k; ++i) {
- var field = fields[i];
- this[field.name] = field.repeated ? [] : null;
- if (field.required && field.defaultValue !== null)
- this[field.name] = field.defaultValue;
- }
- if (arguments.length > 0) {
- // Set field values from a values object
- if (arguments.length === 1 && typeof values === 'object' &&
- /* not another Message */ typeof values.encode !== 'function' &&
- /* not a repeated field */ !ProtoBuf.Util.isArray(values) &&
- /* not a ByteBuffer */ !(values instanceof ByteBuffer) &&
- /* not an ArrayBuffer */ !(values instanceof ArrayBuffer) &&
- /* not a Long */ !(ProtoBuf.Long && values instanceof ProtoBuf.Long)) {
- var keys = Object.keys(values);
- for (i=0, k=keys.length; i<k; ++i)
- this.$set(keys[i], values[keys[i]]); // May throw
- } else // Set field values from arguments, in declaration order
- for (i=0, k=arguments.length; i<k; ++i)
- this.$set(fields[i].name, arguments[i]); // May throw
- }
- };
- /**
- * @alias ProtoBuf.Builder.Message.prototype
- * @inner
- */
- var MessagePrototype = Message.prototype = Object.create(ProtoBuf.Builder.Message.prototype);
- /**
- * Adds a value to a repeated field.
- * @name ProtoBuf.Builder.Message#add
- * @function
- * @param {string} key Field name
- * @param {*} value Value to add
- * @param {boolean=} noAssert Whether to assert the value or not (asserts by default)
- * @throws {Error} If the value cannot be added
- * @expose
- */
- MessagePrototype.add = function(key, value, noAssert) {
- var field = T._fieldsByName[key];
- if (!noAssert) {
- if (!field)
- throw Error(this+"#"+key+" is undefined");
- if (!(field instanceof ProtoBuf.Reflect.Message.Field))
- throw Error(this+"#"+key+" is not a field: "+field.toString(true)); // May throw if it's an enum or embedded message
- if (!field.repeated)
- throw Error(this+"#"+key+" is not a repeated field");
- }
- if (this[field.name] === null)
- this[field.name] = [];
- this[field.name].push(noAssert ? value : field.verifyValue(value, true));
- };
- /**
- * Adds a value to a repeated field. This is an alias for {@link ProtoBuf.Builder.Message#add}.
- * @name ProtoBuf.Builder.Message#$add
- * @function
- * @param {string} key Field name
- * @param {*} value Value to add
- * @param {boolean=} noAssert Whether to assert the value or not (asserts by default)
- * @throws {Error} If the value cannot be added
- * @expose
- */
- MessagePrototype.$add = MessagePrototype.add;
- /**
- * Sets a field's value.
- * @name ProtoBuf.Builder.Message#set
- * @function
- * @param {string} key Key
- * @param {*} value Value to set
- * @param {boolean=} noAssert Whether to not assert for an actual field / proper value type, defaults to `false`
- * @returns {!ProtoBuf.Builder.Message} this
- * @throws {Error} If the value cannot be set
- * @expose
- */
- MessagePrototype.set = function(key, value, noAssert) {
- if (key && typeof key === 'object') {
- for (var i in key)
- if (key.hasOwnProperty(i))
- this.$set(i, key[i], noAssert);
- return this;
- }
- var field = T._fieldsByName[key];
- if (!noAssert) {
- if (!field)
- throw Error(this+"#"+key+" is not a field: undefined");
- if (!(field instanceof ProtoBuf.Reflect.Message.Field))
- throw Error(this+"#"+key+" is not a field: "+field.toString(true));
- this[field.name] = (value = field.verifyValue(value)); // May throw
- } else {
- this[field.name] = value;
- }
- if (field.oneof) {
- if (value !== null) {
- if (this[field.oneof.name] !== null)
- this[this[field.oneof.name]] = null; // Unset the previous (field name is the oneof field's value)
- this[field.oneof.name] = field.name;
- } else if (field.oneof.name === key)
- this[field.oneof.name] = null;
- }
- return this;
- };
- /**
- * Sets a field's value. This is an alias for [@link ProtoBuf.Builder.Message#set}.
- * @name ProtoBuf.Builder.Message#$set
- * @function
- * @param {string} key Key
- * @param {*} value Value to set
- * @param {boolean=} noAssert Whether to not assert the value, defaults to `false`
- * @throws {Error} If the value cannot be set
- * @expose
- */
- MessagePrototype.$set = MessagePrototype.set;
- /**
- * Gets a field's value.
- * @name ProtoBuf.Builder.Message#get
- * @function
- * @param {string} key Key
- * @param {boolean=} noAssert Whether to not assert for an actual field, defaults to `false`
- * @return {*} Value
- * @throws {Error} If there is no such field
- * @expose
- */
- MessagePrototype.get = function(key, noAssert) {
- if (noAssert)
- return this[key];
- var field = T._fieldsByName[key];
- if (!field || !(field instanceof ProtoBuf.Reflect.Message.Field))
- throw Error(this+"#"+key+" is not a field: undefined");
- if (!(field instanceof ProtoBuf.Reflect.Message.Field))
- throw Error(this+"#"+key+" is not a field: "+field.toString(true));
- return this[field.name];
- };
- /**
- * Gets a field's value. This is an alias for {@link ProtoBuf.Builder.Message#$get}.
- * @name ProtoBuf.Builder.Message#$get
- * @function
- * @param {string} key Key
- * @return {*} Value
- * @throws {Error} If there is no such field
- * @expose
- */
- MessagePrototype.$get = MessagePrototype.get;
- // Getters and setters
- for (var i=0; i<fields.length; i++) {
- var field = fields[i];
- // no setters for extension fields as these are named by their fqn
- if (field instanceof ProtoBuf.Reflect.Message.ExtensionField)
- continue;
- if (T.builder.options['populateAccessors'])
- (function(field) {
- // set/get[SomeValue]
- var Name = field.originalName.replace(/(_[a-zA-Z])/g, function(match) {
- return match.toUpperCase().replace('_','');
- });
- Name = Name.substring(0,1).toUpperCase() + Name.substring(1);
- // set/get_[some_value] FIXME: Do we really need these?
- var name = field.originalName.replace(/([A-Z])/g, function(match) {
- return "_"+match;
- });
- /**
- * The current field's unbound setter function.
- * @function
- * @param {*} value
- * @param {boolean=} noAssert
- * @returns {!ProtoBuf.Builder.Message}
- * @inner
- */
- var setter = function(value, noAssert) {
- this[field.name] = noAssert ? value : field.verifyValue(value);
- return this;
- };
- /**
- * The current field's unbound getter function.
- * @function
- * @returns {*}
- * @inner
- */
- var getter = function() {
- return this[field.name];
- };
- /**
- * Sets a value. This method is present for each field, but only if there is no name conflict with
- * another field.
- * @name ProtoBuf.Builder.Message#set[SomeField]
- * @function
- * @param {*} value Value to set
- * @param {boolean=} noAssert Whether to not assert the value, defaults to `false`
- * @returns {!ProtoBuf.Builder.Message} this
- * @abstract
- * @throws {Error} If the value cannot be set
- */
- if (T.getChild("set"+Name) === null)
- MessagePrototype["set"+Name] = setter;
- /**
- * Sets a value. This method is present for each field, but only if there is no name conflict with
- * another field.
- * @name ProtoBuf.Builder.Message#set_[some_field]
- * @function
- * @param {*} value Value to set
- * @param {boolean=} noAssert Whether to not assert the value, defaults to `false`
- * @returns {!ProtoBuf.Builder.Message} this
- * @abstract
- * @throws {Error} If the value cannot be set
- */
- if (T.getChild("set_"+name) === null)
- MessagePrototype["set_"+name] = setter;
- /**
- * Gets a value. This method is present for each field, but only if there is no name conflict with
- * another field.
- * @name ProtoBuf.Builder.Message#get[SomeField]
- * @function
- * @abstract
- * @return {*} The value
- */
- if (T.getChild("get"+Name) === null)
- MessagePrototype["get"+Name] = getter;
- /**
- * Gets a value. This method is present for each field, but only if there is no name conflict with
- * another field.
- * @name ProtoBuf.Builder.Message#get_[some_field]
- * @function
- * @return {*} The value
- * @abstract
- */
- if (T.getChild("get_"+name) === null)
- MessagePrototype["get_"+name] = getter;
- })(field);
- }
- // En-/decoding
- /**
- * Encodes the message.
- * @name ProtoBuf.Builder.Message#$encode
- * @function
- * @param {(!ByteBuffer|boolean)=} buffer ByteBuffer to encode to. Will create a new one and flip it if omitted.
- * @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
- * @return {!ByteBuffer} Encoded message as a ByteBuffer
- * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
- * returns the encoded ByteBuffer in the `encoded` property on the error.
- * @expose
- * @see ProtoBuf.Builder.Message#encode64
- * @see ProtoBuf.Builder.Message#encodeHex
- * @see ProtoBuf.Builder.Message#encodeAB
- */
- MessagePrototype.encode = function(buffer, noVerify) {
- if (typeof buffer === 'boolean')
- noVerify = buffer,
- buffer = undefined;
- var isNew = false;
- if (!buffer)
- buffer = new ByteBuffer(),
- isNew = true;
- var le = buffer.littleEndian;
- try {
- T.encode(this, buffer.LE(), noVerify);
- return (isNew ? buffer.flip() : buffer).LE(le);
- } catch (e) {
- buffer.LE(le);
- throw(e);
- }
- };
- /**
- * Calculates the byte length of the message.
- * @name ProtoBuf.Builder.Message#calculate
- * @function
- * @returns {number} Byte length
- * @throws {Error} If the message cannot be calculated or if required fields are missing.
- * @expose
- */
- MessagePrototype.calculate = function() {
- return T.calculate(this);
- };
- /**
- * Encodes the varint32 length-delimited message.
- * @name ProtoBuf.Builder.Message#encodeDelimited
- * @function
- * @param {(!ByteBuffer|boolean)=} buffer ByteBuffer to encode to. Will create a new one and flip it if omitted.
- * @return {!ByteBuffer} Encoded message as a ByteBuffer
- * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
- * returns the encoded ByteBuffer in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.encodeDelimited = function(buffer) {
- var isNew = false;
- if (!buffer)
- buffer = new ByteBuffer(),
- isNew = true;
- var enc = new ByteBuffer().LE();
- T.encode(this, enc).flip();
- buffer.writeVarint32(enc.remaining());
- buffer.append(enc);
- return isNew ? buffer.flip() : buffer;
- };
- /**
- * Directly encodes the message to an ArrayBuffer.
- * @name ProtoBuf.Builder.Message#encodeAB
- * @function
- * @return {ArrayBuffer} Encoded message as ArrayBuffer
- * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
- * returns the encoded ArrayBuffer in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.encodeAB = function() {
- try {
- return this.encode().toArrayBuffer();
- } catch (e) {
- if (e["encoded"]) e["encoded"] = e["encoded"].toArrayBuffer();
- throw(e);
- }
- };
- /**
- * Returns the message as an ArrayBuffer. This is an alias for {@link ProtoBuf.Builder.Message#encodeAB}.
- * @name ProtoBuf.Builder.Message#toArrayBuffer
- * @function
- * @return {ArrayBuffer} Encoded message as ArrayBuffer
- * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
- * returns the encoded ArrayBuffer in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.toArrayBuffer = MessagePrototype.encodeAB;
- /**
- * Directly encodes the message to a node Buffer.
- * @name ProtoBuf.Builder.Message#encodeNB
- * @function
- * @return {!Buffer}
- * @throws {Error} If the message cannot be encoded, not running under node.js or if required fields are
- * missing. The later still returns the encoded node Buffer in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.encodeNB = function() {
- try {
- return this.encode().toBuffer();
- } catch (e) {
- if (e["encoded"]) e["encoded"] = e["encoded"].toBuffer();
- throw(e);
- }
- };
- /**
- * Returns the message as a node Buffer. This is an alias for {@link ProtoBuf.Builder.Message#encodeNB}.
- * @name ProtoBuf.Builder.Message#toBuffer
- * @function
- * @return {!Buffer}
- * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
- * returns the encoded node Buffer in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.toBuffer = MessagePrototype.encodeNB;
- /**
- * Directly encodes the message to a base64 encoded string.
- * @name ProtoBuf.Builder.Message#encode64
- * @function
- * @return {string} Base64 encoded string
- * @throws {Error} If the underlying buffer cannot be encoded or if required fields are missing. The later
- * still returns the encoded base64 string in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.encode64 = function() {
- try {
- return this.encode().toBase64();
- } catch (e) {
- if (e["encoded"]) e["encoded"] = e["encoded"].toBase64();
- throw(e);
- }
- };
- /**
- * Returns the message as a base64 encoded string. This is an alias for {@link ProtoBuf.Builder.Message#encode64}.
- * @name ProtoBuf.Builder.Message#toBase64
- * @function
- * @return {string} Base64 encoded string
- * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
- * returns the encoded base64 string in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.toBase64 = MessagePrototype.encode64;
- /**
- * Directly encodes the message to a hex encoded string.
- * @name ProtoBuf.Builder.Message#encodeHex
- * @function
- * @return {string} Hex encoded string
- * @throws {Error} If the underlying buffer cannot be encoded or if required fields are missing. The later
- * still returns the encoded hex string in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.encodeHex = function() {
- try {
- return this.encode().toHex();
- } catch (e) {
- if (e["encoded"]) e["encoded"] = e["encoded"].toHex();
- throw(e);
- }
- };
- /**
- * Returns the message as a hex encoded string. This is an alias for {@link ProtoBuf.Builder.Message#encodeHex}.
- * @name ProtoBuf.Builder.Message#toHex
- * @function
- * @return {string} Hex encoded string
- * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
- * returns the encoded hex string in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.toHex = MessagePrototype.encodeHex;
- /**
- * Clones a message object to a raw object.
- * @param {*} obj Object to clone
- * @param {boolean} includeBinaryAsBase64 Whether to include binary data as base64 strings or not
- * @returns {*} Cloned object
- * @inner
- */
- function cloneRaw(obj, includeBinaryAsBase64) {
- var clone = {};
- for (var i in obj)
- if (obj.hasOwnProperty(i)) {
- if (obj[i] === null || typeof obj[i] !== 'object')
- clone[i] = obj[i];
- else if (obj[i] instanceof ByteBuffer) {
- if (includeBinaryAsBase64)
- clone[i] = obj[i].toBase64();
- } else // is a non-null object
- clone[i] = cloneRaw(obj[i], includeBinaryAsBase64);
- }
- return clone;
- }
- /**
- * Returns the message's raw payload.
- * @param {boolean=} includeBinaryAsBase64 Whether to include binary data as base64 strings or not, defaults to `false`
- * @returns {Object.<string,*>} Raw payload
- * @expose
- */
- MessagePrototype.toRaw = function(includeBinaryAsBase64) {
- return cloneRaw(this, !!includeBinaryAsBase64);
- };
- /**
- * Decodes a message from the specified buffer or string.
- * @name ProtoBuf.Builder.Message.decode
- * @function
- * @param {!ByteBuffer|!ArrayBuffer|!Buffer|string} buffer Buffer to decode from
- * @param {string=} enc Encoding if buffer is a string: hex, utf8 (not recommended), defaults to base64
- * @return {!ProtoBuf.Builder.Message} Decoded message
- * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
- * returns the decoded message with missing fields in the `decoded` property on the error.
- * @expose
- * @see ProtoBuf.Builder.Message.decode64
- * @see ProtoBuf.Builder.Message.decodeHex
- */
- Message.decode = function(buffer, enc) {
- if (typeof buffer === 'string')
- buffer = ByteBuffer.wrap(buffer, enc ? enc : "base64");
- buffer = buffer instanceof ByteBuffer ? buffer : ByteBuffer.wrap(buffer); // May throw
- var le = buffer.littleEndian;
- try {
- var msg = T.decode(buffer.LE());
- buffer.LE(le);
- return msg;
- } catch (e) {
- buffer.LE(le);
- throw(e);
- }
- };
- /**
- * Decodes a varint32 length-delimited message from the specified buffer or string.
- * @name ProtoBuf.Builder.Message.decodeDelimited
- * @function
- * @param {!ByteBuffer|!ArrayBuffer|!Buffer|string} buffer Buffer to decode from
- * @param {string=} enc Encoding if buffer is a string: hex, utf8 (not recommended), defaults to base64
- * @return {ProtoBuf.Builder.Message} Decoded message or `null` if not enough bytes are available yet
- * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
- * returns the decoded message with missing fields in the `decoded` property on the error.
- * @expose
- */
- Message.decodeDelimited = function(buffer, enc) {
- if (typeof buffer === 'string')
- buffer = ByteBuffer.wrap(buffer, enc ? enc : "base64");
- buffer = buffer instanceof ByteBuffer ? buffer : ByteBuffer.wrap(buffer); // May throw
- if (buffer.remaining() < 1)
- return null;
- var off = buffer.offset,
- len = buffer.readVarint32();
- if (buffer.remaining() < len) {
- buffer.offset = off;
- return null;
- }
- try {
- var msg = T.decode(buffer.slice(buffer.offset, buffer.offset + len).LE());
- buffer.offset += len;
- return msg;
- } catch (err) {
- buffer.offset += len;
- throw err;
- }
- };
- /**
- * Decodes the message from the specified base64 encoded string.
- * @name ProtoBuf.Builder.Message.decode64
- * @function
- * @param {string} str String to decode from
- * @return {!ProtoBuf.Builder.Message} Decoded message
- * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
- * returns the decoded message with missing fields in the `decoded` property on the error.
- * @expose
- */
- Message.decode64 = function(str) {
- return Message.decode(str, "base64");
- };
- /**
- * Decodes the message from the specified hex encoded string.
- * @name ProtoBuf.Builder.Message.decodeHex
- * @function
- * @param {string} str String to decode from
- * @return {!ProtoBuf.Builder.Message} Decoded message
- * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
- * returns the decoded message with missing fields in the `decoded` property on the error.
- * @expose
- */
- Message.decodeHex = function(str) {
- return Message.decode(str, "hex");
- };
- // Utility
- /**
- * Returns a string representation of this Message.
- * @name ProtoBuf.Builder.Message#toString
- * @function
- * @return {string} String representation as of ".Fully.Qualified.MessageName"
- * @expose
- */
- MessagePrototype.toString = function() {
- return T.toString();
- };
- // Properties
- /**
- * Options.
- * @name ProtoBuf.Builder.Message.$options
- * @type {Object.<string,*>}
- * @expose
- */
- var $options; // cc
- /**
- * Reflection type.
- * @name ProtoBuf.Builder.Message#$type
- * @type {!ProtoBuf.Reflect.Message}
- * @expose
- */
- var $type; // cc
- if (Object.defineProperty)
- Object.defineProperty(Message, '$options', { "value": T.buildOpt() }),
- Object.defineProperty(MessagePrototype, "$type", {
- get: function() { return T; }
- });
- return Message;
- })(ProtoBuf, this);
- // Static enums and prototyped sub-messages / cached collections
- this._fields = [];
- this._fieldsById = {};
- this._fieldsByName = {};
- for (var i=0, k=this.children.length, child; i<k; i++) {
- child = this.children[i];
- if (child instanceof Enum)
- clazz[child.name] = child.build();
- else if (child instanceof Message)
- clazz[child.name] = child.build();
- else if (child instanceof Message.Field)
- child.build(),
- this._fields.push(child),
- this._fieldsById[child.id] = child,
- this._fieldsByName[child.name] = child;
- else if (!(child instanceof Message.OneOf) && !(child instanceof Extension)) // Not built
- throw Error("Illegal reflect child of "+this.toString(true)+": "+children[i].toString(true));
- }
- return this.clazz = clazz;
- };
- /**
- * Encodes a runtime message's contents to the specified buffer.
- * @param {!ProtoBuf.Builder.Message} message Runtime message to encode
- * @param {ByteBuffer} buffer ByteBuffer to write to
- * @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
- * @return {ByteBuffer} The ByteBuffer for chaining
- * @throws {Error} If required fields are missing or the message cannot be encoded for another reason
- * @expose
- */
- MessagePrototype.encode = function(message, buffer, noVerify) {
- var fieldMissing = null,
- field;
- for (var i=0, k=this._fields.length, val; i<k; ++i) {
- field = this._fields[i];
- val = message[field.name];
- if (field.required && val === null) {
- if (fieldMissing === null)
- fieldMissing = field;
- } else
- field.encode(noVerify ? val : field.verifyValue(val), buffer);
- }
- if (fieldMissing !== null) {
- var err = Error("Missing at least one required field for "+this.toString(true)+": "+fieldMissing);
- err["encoded"] = buffer; // Still expose what we got
- throw(err);
- }
- return buffer;
- };
- /**
- * Calculates a runtime message's byte length.
- * @param {!ProtoBuf.Builder.Message} message Runtime message to encode
- * @returns {number} Byte length
- * @throws {Error} If required fields are missing or the message cannot be calculated for another reason
- * @expose
- */
- MessagePrototype.calculate = function(message) {
- for (var n=0, i=0, k=this._fields.length, field, val; i<k; ++i) {
- field = this._fields[i];
- val = message[field.name];
- if (field.required && val === null)
- throw Error("Missing at least one required field for "+this.toString(true)+": "+field);
- else
- n += field.calculate(val);
- }
- return n;
- };
- /**
- * Skips all data until the end of the specified group has been reached.
- * @param {number} expectedId Expected GROUPEND id
- * @param {!ByteBuffer} buf ByteBuffer
- * @returns {boolean} `true` if a value as been skipped, `false` if the end has been reached
- * @throws {Error} If it wasn't possible to find the end of the group (buffer overrun or end tag mismatch)
- * @inner
- */
- function skipTillGroupEnd(expectedId, buf) {
- var tag = buf.readVarint32(), // Throws on OOB
- wireType = tag & 0x07,
- id = tag >> 3;
- switch (wireType) {
- case ProtoBuf.WIRE_TYPES.VARINT:
- do tag = buf.readUint8();
- while ((tag & 0x80) === 0x80);
- break;
- case ProtoBuf.WIRE_TYPES.BITS64:
- buf.offset += 8;
- break;
- case ProtoBuf.WIRE_TYPES.LDELIM:
- tag = buf.readVarint32(); // reads the varint
- buf.offset += tag; // skips n bytes
- break;
- case ProtoBuf.WIRE_TYPES.STARTGROUP:
- skipTillGroupEnd(id, buf);
- break;
- case ProtoBuf.WIRE_TYPES.ENDGROUP:
- if (id === expectedId)
- return false;
- else
- throw Error("Illegal GROUPEND after unknown group: "+id+" ("+expectedId+" expected)");
- case ProtoBuf.WIRE_TYPES.BITS32:
- buf.offset += 4;
- break;
- default:
- throw Error("Illegal wire type in unknown group "+expectedId+": "+wireType);
- }
- return true;
- }
- /**
- * Decodes an encoded message and returns the decoded message.
- * @param {ByteBuffer} buffer ByteBuffer to decode from
- * @param {number=} length Message length. Defaults to decode all the available data.
- * @param {number=} expectedGroupEndId Expected GROUPEND id if this is a legacy group
- * @return {ProtoBuf.Builder.Message} Decoded message
- * @throws {Error} If the message cannot be decoded
- * @expose
- */
- MessagePrototype.decode = function(buffer, length, expectedGroupEndId) {
- length = typeof length === 'number' ? length : -1;
- var start = buffer.offset,
- msg = new (this.clazz)(),
- tag, wireType, id, field;
- while (buffer.offset < start+length || (length === -1 && buffer.remaining() > 0)) {
- tag = buffer.readVarint32();
- wireType = tag & 0x07;
- id = tag >> 3;
- if (wireType === ProtoBuf.WIRE_TYPES.ENDGROUP) {
- if (id !== expectedGroupEndId)
- throw Error("Illegal group end indicator for "+this.toString(true)+": "+id+" ("+(expectedGroupEndId ? expectedGroupEndId+" expected" : "not a group")+")");
- break;
- }
- if (!(field = this._fieldsById[id])) {
- // "messages created by your new code can be parsed by your old code: old binaries simply ignore the new field when parsing."
- switch (wireType) {
- case ProtoBuf.WIRE_TYPES.VARINT:
- buffer.readVarint32();
- break;
- case ProtoBuf.WIRE_TYPES.BITS32:
- buffer.offset += 4;
- break;
- case ProtoBuf.WIRE_TYPES.BITS64:
- buffer.offset += 8;
- break;
- case ProtoBuf.WIRE_TYPES.LDELIM:
- var len = buffer.readVarint32();
- buffer.offset += len;
- break;
- case ProtoBuf.WIRE_TYPES.STARTGROUP:
- while (skipTillGroupEnd(id, buffer)) {}
- break;
- default:
- throw Error("Illegal wire type for unknown field "+id+" in "+this.toString(true)+"#decode: "+wireType);
- }
- continue;
- }
- if (field.repeated && !field.options["packed"])
- msg[field.name].push(field.decode(wireType, buffer));
- else {
- msg[field.name] = field.decode(wireType, buffer);
- if (field.oneof) {
- if (this[field.oneof.name] !== null)
- this[this[field.oneof.name]] = null;
- msg[field.oneof.name] = field.name;
- }
- }
- }
- // Check if all required fields are present and set default values for optional fields that are not
- for (var i=0, k=this._fields.length; i<k; ++i) {
- field = this._fields[i];
- if (msg[field.name] === null)
- if (field.required) {
- var err = Error("Missing at least one required field for "+this.toString(true)+": "+field.name);
- err["decoded"] = msg; // Still expose what we got
- throw(err);
- } else if (field.defaultValue !== null)
- msg[field.name] = field.defaultValue;
- }
- return msg;
- };
- /**
- * @alias ProtoBuf.Reflect.Message
- * @expose
- */
- Reflect.Message = Message;
- /**
- * Constructs a new Message Field.
- * @exports ProtoBuf.Reflect.Message.Field
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.Message} message Message reference
- * @param {string} rule Rule, one of requried, optional, repeated
- * @param {string} type Data type, e.g. int32
- * @param {string} name Field name
- * @param {number} id Unique field id
- * @param {Object.<string,*>=} options Options
- * @param {!ProtoBuf.Reflect.Message.OneOf=} oneof Enclosing OneOf
- * @constructor
- * @extends ProtoBuf.Reflect.T
- */
- var Field = function(builder, message, rule, type, name, id, options, oneof) {
- T.call(this, builder, message, name);
- /**
- * @override
- */
- this.className = "Message.Field";
- /**
- * Message field required flag.
- * @type {boolean}
- * @expose
- */
- this.required = rule === "required";
- /**
- * Message field repeated flag.
- * @type {boolean}
- * @expose
- */
- this.repeated = rule === "repeated";
- /**
- * Message field type. Type reference string if unresolved, protobuf type if resolved.
- * @type {string|{name: string, wireType: number}}
- * @expose
- */
- this.type = type;
- /**
- * Resolved type reference inside the global namespace.
- * @type {ProtoBuf.Reflect.T|null}
- * @expose
- */
- this.resolvedType = null;
- /**
- * Unique message field id.
- * @type {number}
- * @expose
- */
- this.id = id;
- /**
- * Message field options.
- * @type {!Object.<string,*>}
- * @dict
- * @expose
- */
- this.options = options || {};
- /**
- * Default value.
- * @type {*}
- * @expose
- */
- this.defaultValue = null;
- /**
- * Enclosing OneOf.
- * @type {?ProtoBuf.Reflect.Message.OneOf}
- * @expose
- */
- this.oneof = oneof || null;
- /**
- * Original field name.
- * @type {string}
- * @expose
- */
- this.originalName = this.name; // Used to revert camelcase transformation on naming collisions
- // Convert field names to camel case notation if the override is set
- if (this.builder.options['convertFieldsToCamelCase'] && !(this instanceof Message.ExtensionField))
- this.name = Field._toCamelCase(this.name);
- };
- /**
- * Converts a field name to camel case.
- * @param {string} name Likely underscore notated name
- * @returns {string} Camel case notated name
- * @private
- */
- Field._toCamelCase = function(name) {
- return name.replace(/_([a-zA-Z])/g, function($0, $1) {
- return $1.toUpperCase();
- });
- };
- /**
- * @alias ProtoBuf.Reflect.Message.Field.prototype
- * @inner
- */
- var FieldPrototype = Field.prototype = Object.create(T.prototype);
- /**
- * Builds the field.
- * @override
- * @expose
- */
- FieldPrototype.build = function() {
- this.defaultValue = typeof this.options['default'] !== 'undefined'
- ? this.verifyValue(this.options['default']) : null;
- };
- /**
- * Makes a Long from a value.
- * @param {{low: number, high: number, unsigned: boolean}|string|number} value Value
- * @param {boolean=} unsigned Whether unsigned or not, defaults to reuse it from Long-like objects or to signed for
- * strings and numbers
- * @returns {!Long}
- * @throws {Error} If the value cannot be converted to a Long
- * @inner
- */
- function mkLong(value, unsigned) {
- if (value && typeof value.low === 'number' && typeof value.high === 'number' && typeof value.unsigned === 'boolean'
- && value.low === value.low && value.high === value.high)
- return new ProtoBuf.Long(value.low, value.high, typeof unsigned === 'undefined' ? value.unsigned : unsigned);
- if (typeof value === 'string')
- return ProtoBuf.Long.fromString(value, unsigned || false, 10);
- if (typeof value === 'number')
- return ProtoBuf.Long.fromNumber(value, unsigned || false);
- throw Error("not convertible to Long");
- }
- /**
- * Checks if the given value can be set for this field.
- * @param {*} value Value to check
- * @param {boolean=} skipRepeated Whether to skip the repeated value check or not. Defaults to false.
- * @return {*} Verified, maybe adjusted, value
- * @throws {Error} If the value cannot be set for this field
- * @expose
- */
- FieldPrototype.verifyValue = function(value, skipRepeated) {
- skipRepeated = skipRepeated || false;
- var fail = function(val, msg) {
- throw Error("Illegal value for "+this.toString(true)+" of type "+this.type.name+": "+val+" ("+msg+")");
- }.bind(this);
- if (value === null) { // NULL values for optional fields
- if (this.required)
- fail(typeof value, "required");
- return null;
- }
- var i;
- if (this.repeated && !skipRepeated) { // Repeated values as arrays
- if (!ProtoBuf.Util.isArray(value))
- value = [value];
- var res = [];
- for (i=0; i<value.length; i++)
- res.push(this.verifyValue(value[i], true));
- return res;
- }
- // All non-repeated fields expect no array
- if (!this.repeated && ProtoBuf.Util.isArray(value))
- fail(typeof value, "no array expected");
- switch (this.type) {
- // Signed 32bit
- case ProtoBuf.TYPES["int32"]:
- case ProtoBuf.TYPES["sint32"]:
- case ProtoBuf.TYPES["sfixed32"]:
- // Account for !NaN: value === value
- if (typeof value !== 'number' || (value === value && value % 1 !== 0))
- fail(typeof value, "not an integer");
- return value > 4294967295 ? value | 0 : value;
- // Unsigned 32bit
- case ProtoBuf.TYPES["uint32"]:
- case ProtoBuf.TYPES["fixed32"]:
- if (typeof value !== 'number' || (value === value && value % 1 !== 0))
- fail(typeof value, "not an integer");
- return value < 0 ? value >>> 0 : value;
- // Signed 64bit
- case ProtoBuf.TYPES["int64"]:
- case ProtoBuf.TYPES["sint64"]:
- case ProtoBuf.TYPES["sfixed64"]: {
- if (ProtoBuf.Long)
- try {
- return mkLong(value, false);
- } catch (e) {
- fail(typeof value, e.message);
- }
- else
- fail(typeof value, "requires Long.js");
- }
- // Unsigned 64bit
- case ProtoBuf.TYPES["uint64"]:
- case ProtoBuf.TYPES["fixed64"]: {
- if (ProtoBuf.Long)
- try {
- return mkLong(value, true);
- } catch (e) {
- fail(typeof value, e.message);
- }
- else
- fail(typeof value, "requires Long.js");
- }
- // Bool
- case ProtoBuf.TYPES["bool"]:
- if (typeof value !== 'boolean')
- fail(typeof value, "not a boolean");
- return value;
- // Float
- case ProtoBuf.TYPES["float"]:
- case ProtoBuf.TYPES["double"]:
- if (typeof value !== 'number')
- fail(typeof value, "not a number");
- return value;
- // Length-delimited string
- case ProtoBuf.TYPES["string"]:
- if (typeof value !== 'string' && !(value && value instanceof String))
- fail(typeof value, "not a string");
- return ""+value; // Convert String object to string
- // Length-delimited bytes
- case ProtoBuf.TYPES["bytes"]:
- if (ByteBuffer.isByteBuffer(value))
- return value;
- return ByteBuffer.wrap(value, "base64");
- // Constant enum value
- case ProtoBuf.TYPES["enum"]: {
- var values = this.resolvedType.getChildren(Enum.Value);
- for (i=0; i<values.length; i++)
- if (values[i].name == value)
- return values[i].id;
- else if (values[i].id == value)
- return values[i].id;
- fail(value, "not a valid enum value");
- }
- // Embedded message
- case ProtoBuf.TYPES["group"]:
- case ProtoBuf.TYPES["message"]: {
- if (!value || typeof value !== 'object')
- fail(typeof value, "object expected");
- if (value instanceof this.resolvedType.clazz)
- return value;
- if (value instanceof ProtoBuf.Builder.Message) {
- // Mismatched type: Convert to object (see: https://github.com/dcodeIO/ProtoBuf.js/issues/180)
- var obj = {};
- for (var i in value)
- if (value.hasOwnProperty(i))
- obj[i] = value[i];
- value = obj;
- }
- // Else let's try to construct one from a key-value object
- return new (this.resolvedType.clazz)(value); // May throw for a hundred of reasons
- }
- }
- // We should never end here
- throw Error("[INTERNAL] Illegal value for "+this.toString(true)+": "+value+" (undefined type "+this.type+")");
- };
- /**
- * Encodes the specified field value to the specified buffer.
- * @param {*} value Verified field value
- * @param {ByteBuffer} buffer ByteBuffer to encode to
- * @return {ByteBuffer} The ByteBuffer for chaining
- * @throws {Error} If the field cannot be encoded
- * @expose
- */
- FieldPrototype.encode = function(value, buffer) {
- if (this.type === null || typeof this.type !== 'object')
- throw Error("[INTERNAL] Unresolved type in "+this.toString(true)+": "+this.type);
- if (value === null || (this.repeated && value.length == 0))
- return buffer; // Optional omitted
- try {
- if (this.repeated) {
- var i;
- // "Only repeated fields of primitive numeric types (types which use the varint, 32-bit, or 64-bit wire
- // types) can be declared 'packed'."
- if (this.options["packed"] && ProtoBuf.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType) >= 0) {
- // "All of the elements of the field are packed into a single key-value pair with wire type 2
- // (length-delimited). Each element is encoded the same way it would be normally, except without a
- // tag preceding it."
- buffer.writeVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.LDELIM);
- buffer.ensureCapacity(buffer.offset += 1); // We do not know the length yet, so let's assume a varint of length 1
- var start = buffer.offset; // Remember where the contents begin
- for (i=0; i<value.length; i++)
- this.encodeValue(value[i], buffer);
- var len = buffer.offset-start,
- varintLen = ByteBuffer.calculateVarint32(len);
- if (varintLen > 1) { // We need to move the contents
- var contents = buffer.slice(start, buffer.offset);
- start += varintLen-1;
- buffer.offset = start;
- buffer.append(contents);
- }
- buffer.writeVarint32(len, start-varintLen);
- } else {
- // "If your message definition has repeated elements (without the [packed=true] option), the encoded
- // message has zero or more key-value pairs with the same tag number"
- for (i=0; i<value.length; i++)
- buffer.writeVarint32((this.id << 3) | this.type.wireType),
- this.encodeValue(value[i], buffer);
- }
- } else
- buffer.writeVarint32((this.id << 3) | this.type.wireType),
- this.encodeValue(value, buffer);
- } catch (e) {
- throw Error("Illegal value for "+this.toString(true)+": "+value+" ("+e+")");
- }
- return buffer;
- };
- /**
- * Encodes a value to the specified buffer. Does not encode the key.
- * @param {*} value Field value
- * @param {ByteBuffer} buffer ByteBuffer to encode to
- * @return {ByteBuffer} The ByteBuffer for chaining
- * @throws {Error} If the value cannot be encoded
- * @expose
- */
- FieldPrototype.encodeValue = function(value, buffer) {
- if (value === null) return buffer; // Nothing to encode
- // Tag has already been written
- switch (this.type) {
- // 32bit signed varint
- case ProtoBuf.TYPES["int32"]:
- // "If you use int32 or int64 as the type for a negative number, the resulting varint is always ten bytes
- // long – it is, effectively, treated like a very large unsigned integer." (see #122)
- if (value < 0)
- buffer.writeVarint64(value);
- else
- buffer.writeVarint32(value);
- break;
- // 32bit unsigned varint
- case ProtoBuf.TYPES["uint32"]:
- buffer.writeVarint32(value);
- break;
- // 32bit varint zig-zag
- case ProtoBuf.TYPES["sint32"]:
- buffer.writeVarint32ZigZag(value);
- break;
- // Fixed unsigned 32bit
- case ProtoBuf.TYPES["fixed32"]:
- buffer.writeUint32(value);
- break;
- // Fixed signed 32bit
- case ProtoBuf.TYPES["sfixed32"]:
- buffer.writeInt32(value);
- break;
- // 64bit varint as-is
- case ProtoBuf.TYPES["int64"]:
- case ProtoBuf.TYPES["uint64"]:
- buffer.writeVarint64(value); // throws
- break;
- // 64bit varint zig-zag
- case ProtoBuf.TYPES["sint64"]:
- buffer.writeVarint64ZigZag(value); // throws
- break;
- // Fixed unsigned 64bit
- case ProtoBuf.TYPES["fixed64"]:
- buffer.writeUint64(value); // throws
- break;
- // Fixed signed 64bit
- case ProtoBuf.TYPES["sfixed64"]:
- buffer.writeInt64(value); // throws
- break;
- // Bool
- case ProtoBuf.TYPES["bool"]:
- if (typeof value === 'string')
- buffer.writeVarint32(value.toLowerCase() === 'false' ? 0 : !!value);
- else
- buffer.writeVarint32(value ? 1 : 0);
- break;
- // Constant enum value
- case ProtoBuf.TYPES["enum"]:
- buffer.writeVarint32(value);
- break;
- // 32bit float
- case ProtoBuf.TYPES["float"]:
- buffer.writeFloat32(value);
- break;
- // 64bit float
- case ProtoBuf.TYPES["double"]:
- buffer.writeFloat64(value);
- break;
- // Length-delimited string
- case ProtoBuf.TYPES["string"]:
- buffer.writeVString(value);
- break;
- // Length-delimited bytes
- case ProtoBuf.TYPES["bytes"]:
- if (value.remaining() < 0)
- throw Error("Illegal value for "+this.toString(true)+": "+value.remaining()+" bytes remaining");
- var prevOffset = value.offset;
- buffer.writeVarint32(value.remaining());
- buffer.append(value);
- value.offset = prevOffset;
- break;
- // Embedded message
- case ProtoBuf.TYPES["message"]:
- var bb = new ByteBuffer().LE();
- this.resolvedType.encode(value, bb);
- buffer.writeVarint32(bb.offset);
- buffer.append(bb.flip());
- break;
- // Legacy group
- case ProtoBuf.TYPES["group"]:
- this.resolvedType.encode(value, buffer);
- buffer.writeVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.ENDGROUP);
- break;
- default:
- // We should never end here
- throw Error("[INTERNAL] Illegal value to encode in "+this.toString(true)+": "+value+" (unknown type)");
- }
- return buffer;
- };
- /**
- * Calculates the length of this field's value on the network level.
- * @param {*} value Field value
- * @returns {number} Byte length
- * @expose
- */
- FieldPrototype.calculate = function(value) {
- value = this.verifyValue(value); // May throw
- if (this.type === null || typeof this.type !== 'object')
- throw Error("[INTERNAL] Unresolved type in "+this.toString(true)+": "+this.type);
- if (value === null || (this.repeated && value.length == 0))
- return 0; // Optional omitted
- var n = 0;
- try {
- if (this.repeated) {
- var i, ni;
- if (this.options["packed"] && ProtoBuf.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType) >= 0) {
- n += ByteBuffer.calculateVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.LDELIM);
- ni = 0;
- for (i=0; i<value.length; i++)
- ni += this.calculateValue(value[i]);
- n += ByteBuffer.calculateVarint32(ni);
- n += ni;
- } else {
- for (i=0; i<value.length; i++)
- n += ByteBuffer.calculateVarint32((this.id << 3) | this.type.wireType),
- n += this.calculateValue(value[i]);
- }
- } else {
- n += ByteBuffer.calculateVarint32((this.id << 3) | this.type.wireType);
- n += this.calculateValue(value);
- }
- } catch (e) {
- throw Error("Illegal value for "+this.toString(true)+": "+value+" ("+e+")");
- }
- return n;
- };
- /**
- * Calculates the byte length of a value.
- * @param {*} value Field value
- * @returns {number} Byte length
- * @throws {Error} If the value cannot be calculated
- * @expose
- */
- FieldPrototype.calculateValue = function(value) {
- if (value === null) return 0; // Nothing to encode
- // Tag has already been written
- var n;
- switch (this.type) {
- case ProtoBuf.TYPES["int32"]:
- return value < 0 ? ByteBuffer.calculateVarint64(value) : ByteBuffer.calculateVarint32(value);
- case ProtoBuf.TYPES["uint32"]:
- return ByteBuffer.calculateVarint32(value);
- case ProtoBuf.TYPES["sint32"]:
- return ByteBuffer.calculateVarint32(ByteBuffer.zigZagEncode32(value));
- case ProtoBuf.TYPES["fixed32"]:
- case ProtoBuf.TYPES["sfixed32"]:
- case ProtoBuf.TYPES["float"]:
- return 4;
- case ProtoBuf.TYPES["int64"]:
- case ProtoBuf.TYPES["uint64"]:
- return ByteBuffer.calculateVarint64(value);
- case ProtoBuf.TYPES["sint64"]:
- return ByteBuffer.calculateVarint64(ByteBuffer.zigZagEncode64(value));
- case ProtoBuf.TYPES["fixed64"]:
- case ProtoBuf.TYPES["sfixed64"]:
- return 8;
- case ProtoBuf.TYPES["bool"]:
- return 1;
- case ProtoBuf.TYPES["enum"]:
- return ByteBuffer.calculateVarint32(value);
- case ProtoBuf.TYPES["double"]:
- return 8;
- case ProtoBuf.TYPES["string"]:
- n = ByteBuffer.calculateUTF8Bytes(value);
- return ByteBuffer.calculateVarint32(n) + n;
- case ProtoBuf.TYPES["bytes"]:
- if (value.remaining() < 0)
- throw Error("Illegal value for "+this.toString(true)+": "+value.remaining()+" bytes remaining");
- return ByteBuffer.calculateVarint32(value.remaining()) + value.remaining();
- case ProtoBuf.TYPES["message"]:
- n = this.resolvedType.calculate(value);
- return ByteBuffer.calculateVarint32(n) + n;
- case ProtoBuf.TYPES["group"]:
- n = this.resolvedType.calculate(value);
- return n + ByteBuffer.calculateVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.ENDGROUP);
- }
- // We should never end here
- throw Error("[INTERNAL] Illegal value to encode in "+this.toString(true)+": "+value+" (unknown type)");
- };
- /**
- * Decode the field value from the specified buffer.
- * @param {number} wireType Leading wire type
- * @param {ByteBuffer} buffer ByteBuffer to decode from
- * @param {boolean=} skipRepeated Whether to skip the repeated check or not. Defaults to false.
- * @return {*} Decoded value
- * @throws {Error} If the field cannot be decoded
- * @expose
- */
- FieldPrototype.decode = function(wireType, buffer, skipRepeated) {
- var value, nBytes;
- if (wireType != this.type.wireType && (skipRepeated || (wireType != ProtoBuf.WIRE_TYPES.LDELIM || !this.repeated)))
- throw Error("Illegal wire type for field "+this.toString(true)+": "+wireType+" ("+this.type.wireType+" expected)");
- if (wireType == ProtoBuf.WIRE_TYPES.LDELIM && this.repeated && this.options["packed"] && ProtoBuf.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType) >= 0) {
- if (!skipRepeated) {
- nBytes = buffer.readVarint32();
- nBytes = buffer.offset + nBytes; // Limit
- var values = [];
- while (buffer.offset < nBytes)
- values.push(this.decode(this.type.wireType, buffer, true));
- return values;
- }
- // Read the next value otherwise...
- }
- switch (this.type) {
- // 32bit signed varint
- case ProtoBuf.TYPES["int32"]:
- return buffer.readVarint32() | 0;
- // 32bit unsigned varint
- case ProtoBuf.TYPES["uint32"]:
- return buffer.readVarint32() >>> 0;
- // 32bit signed varint zig-zag
- case ProtoBuf.TYPES["sint32"]:
- return buffer.readVarint32ZigZag() | 0;
- // Fixed 32bit unsigned
- case ProtoBuf.TYPES["fixed32"]:
- return buffer.readUint32() >>> 0;
- case ProtoBuf.TYPES["sfixed32"]:
- return buffer.readInt32() | 0;
- // 64bit signed varint
- case ProtoBuf.TYPES["int64"]:
- return buffer.readVarint64();
- // 64bit unsigned varint
- case ProtoBuf.TYPES["uint64"]:
- return buffer.readVarint64().toUnsigned();
- // 64bit signed varint zig-zag
- case ProtoBuf.TYPES["sint64"]:
- return buffer.readVarint64ZigZag();
- // Fixed 64bit unsigned
- case ProtoBuf.TYPES["fixed64"]:
- return buffer.readUint64();
- // Fixed 64bit signed
- case ProtoBuf.TYPES["sfixed64"]:
- return buffer.readInt64();
- // Bool varint
- case ProtoBuf.TYPES["bool"]:
- return !!buffer.readVarint32();
- // Constant enum value (varint)
- case ProtoBuf.TYPES["enum"]:
- // The following Builder.Message#set will already throw
- return buffer.readVarint32();
- // 32bit float
- case ProtoBuf.TYPES["float"]:
- return buffer.readFloat();
- // 64bit float
- case ProtoBuf.TYPES["double"]:
- return buffer.readDouble();
- // Length-delimited string
- case ProtoBuf.TYPES["string"]:
- return buffer.readVString();
- // Length-delimited bytes
- case ProtoBuf.TYPES["bytes"]: {
- nBytes = buffer.readVarint32();
- if (buffer.remaining() < nBytes)
- throw Error("Illegal number of bytes for "+this.toString(true)+": "+nBytes+" required but got only "+buffer.remaining());
- value = buffer.clone(); // Offset already set
- value.limit = value.offset+nBytes;
- buffer.offset += nBytes;
- return value;
- }
- // Length-delimited embedded message
- case ProtoBuf.TYPES["message"]: {
- nBytes = buffer.readVarint32();
- return this.resolvedType.decode(buffer, nBytes);
- }
- // Legacy group
- case ProtoBuf.TYPES["group"]:
- return this.resolvedType.decode(buffer, -1, this.id);
- }
- // We should never end here
- throw Error("[INTERNAL] Illegal wire type for "+this.toString(true)+": "+wireType);
- };
- /**
- * @alias ProtoBuf.Reflect.Message.Field
- * @expose
- */
- Reflect.Message.Field = Field;
- /**
- * Constructs a new Message ExtensionField.
- * @exports ProtoBuf.Reflect.Message.ExtensionField
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.Message} message Message reference
- * @param {string} rule Rule, one of requried, optional, repeated
- * @param {string} type Data type, e.g. int32
- * @param {string} name Field name
- * @param {number} id Unique field id
- * @param {Object.<string,*>=} options Options
- * @constructor
- * @extends ProtoBuf.Reflect.Message.Field
- */
- var ExtensionField = function(builder, message, rule, type, name, id, options) {
- Field.call(this, builder, message, rule, type, name, id, options);
- /**
- * Extension reference.
- * @type {!ProtoBuf.Reflect.Extension}
- * @expose
- */
- this.extension;
- };
- // Extends Field
- ExtensionField.prototype = Object.create(Field.prototype);
- /**
- * @alias ProtoBuf.Reflect.Message.ExtensionField
- * @expose
- */
- Reflect.Message.ExtensionField = ExtensionField;
- /**
- * Constructs a new Message OneOf.
- * @exports ProtoBuf.Reflect.Message.OneOf
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.Message} message Message reference
- * @param {string} name OneOf name
- * @constructor
- * @extends ProtoBuf.Reflect.T
- */
- var OneOf = function(builder, message, name) {
- T.call(this, builder, message, name);
- /**
- * Enclosed fields.
- * @type {!Array.<!ProtoBuf.Reflect.Message.Field>}
- * @expose
- */
- this.fields = [];
- };
- /**
- * @alias ProtoBuf.Reflect.Message.OneOf
- * @expose
- */
- Reflect.Message.OneOf = OneOf;
- /**
- * Constructs a new Enum.
- * @exports ProtoBuf.Reflect.Enum
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.T} parent Parent Reflect object
- * @param {string} name Enum name
- * @param {Object.<string,*>=} options Enum options
- * @constructor
- * @extends ProtoBuf.Reflect.Namespace
- */
- var Enum = function(builder, parent, name, options) {
- Namespace.call(this, builder, parent, name, options);
- /**
- * @override
- */
- this.className = "Enum";
- /**
- * Runtime enum object.
- * @type {Object.<string,number>|null}
- * @expose
- */
- this.object = null;
- };
- /**
- * @alias ProtoBuf.Reflect.Enum.prototype
- * @inner
- */
- var EnumPrototype = Enum.prototype = Object.create(Namespace.prototype);
- /**
- * Builds this enum and returns the runtime counterpart.
- * @return {Object<string,*>}
- * @expose
- */
- EnumPrototype.build = function() {
- var enm = {},
- values = this.getChildren(Enum.Value);
- for (var i=0, k=values.length; i<k; ++i)
- enm[values[i]['name']] = values[i]['id'];
- if (Object.defineProperty)
- Object.defineProperty(enm, '$options', { "value": this.buildOpt() });
- return this.object = enm;
- };
- /**
- * @alias ProtoBuf.Reflect.Enum
- * @expose
- */
- Reflect.Enum = Enum;
- /**
- * Constructs a new Enum Value.
- * @exports ProtoBuf.Reflect.Enum.Value
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.Enum} enm Enum reference
- * @param {string} name Field name
- * @param {number} id Unique field id
- * @constructor
- * @extends ProtoBuf.Reflect.T
- */
- var Value = function(builder, enm, name, id) {
- T.call(this, builder, enm, name);
- /**
- * @override
- */
- this.className = "Enum.Value";
- /**
- * Unique enum value id.
- * @type {number}
- * @expose
- */
- this.id = id;
- };
- // Extends T
- Value.prototype = Object.create(T.prototype);
- /**
- * @alias ProtoBuf.Reflect.Enum.Value
- * @expose
- */
- Reflect.Enum.Value = Value;
- /**
- * An extension (field).
- * @exports ProtoBuf.Reflect.Extension
- * @constructor
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.T} parent Parent object
- * @param {string} name Object name
- * @param {!ProtoBuf.Reflect.Message.Field} field Extension field
- */
- var Extension = function(builder, parent, name, field) {
- T.call(this, builder, parent, name);
- /**
- * Extended message field.
- * @type {!ProtoBuf.Reflect.Message.Field}
- * @expose
- */
- this.field = field;
- };
- // Extends T
- Extension.prototype = Object.create(T.prototype);
- /**
- * @alias ProtoBuf.Reflect.Extension
- * @expose
- */
- Reflect.Extension = Extension;
- /**
- * Constructs a new Service.
- * @exports ProtoBuf.Reflect.Service
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.Namespace} root Root
- * @param {string} name Service name
- * @param {Object.<string,*>=} options Options
- * @constructor
- * @extends ProtoBuf.Reflect.Namespace
- */
- var Service = function(builder, root, name, options) {
- Namespace.call(this, builder, root, name, options);
- /**
- * @override
- */
- this.className = "Service";
- /**
- * Built runtime service class.
- * @type {?function(new:ProtoBuf.Builder.Service)}
- */
- this.clazz = null;
- };
- /**
- * @alias ProtoBuf.Reflect.Service.prototype
- * @inner
- */
- var ServicePrototype = Service.prototype = Object.create(Namespace.prototype);
- /**
- * Builds the service and returns the runtime counterpart, which is a fully functional class.
- * @see ProtoBuf.Builder.Service
- * @param {boolean=} rebuild Whether to rebuild or not
- * @return {Function} Service class
- * @throws {Error} If the message cannot be built
- * @expose
- */
- ServicePrototype.build = function(rebuild) {
- if (this.clazz && !rebuild)
- return this.clazz;
- // Create the runtime Service class in its own scope
- return this.clazz = (function(ProtoBuf, T) {
- /**
- * Constructs a new runtime Service.
- * @name ProtoBuf.Builder.Service
- * @param {function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))=} rpcImpl RPC implementation receiving the method name and the message
- * @class Barebone of all runtime services.
- * @constructor
- * @throws {Error} If the service cannot be created
- */
- var Service = function(rpcImpl) {
- ProtoBuf.Builder.Service.call(this);
- /**
- * Service implementation.
- * @name ProtoBuf.Builder.Service#rpcImpl
- * @type {!function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))}
- * @expose
- */
- this.rpcImpl = rpcImpl || function(name, msg, callback) {
- // This is what a user has to implement: A function receiving the method name, the actual message to
- // send (type checked) and the callback that's either provided with the error as its first
- // argument or null and the actual response message.
- setTimeout(callback.bind(this, Error("Not implemented, see: https://github.com/dcodeIO/ProtoBuf.js/wiki/Services")), 0); // Must be async!
- };
- };
- /**
- * @alias ProtoBuf.Builder.Service.prototype
- * @inner
- */
- var ServicePrototype = Service.prototype = Object.create(ProtoBuf.Builder.Service.prototype);
- if (Object.defineProperty)
- Object.defineProperty(Service, "$options", { "value": T.buildOpt() }),
- Object.defineProperty(ServicePrototype, "$options", { "value": Service["$options"] });
- /**
- * Asynchronously performs an RPC call using the given RPC implementation.
- * @name ProtoBuf.Builder.Service.[Method]
- * @function
- * @param {!function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))} rpcImpl RPC implementation
- * @param {ProtoBuf.Builder.Message} req Request
- * @param {function(Error, (ProtoBuf.Builder.Message|ByteBuffer|Buffer|string)=)} callback Callback receiving
- * the error if any and the response either as a pre-parsed message or as its raw bytes
- * @abstract
- */
- /**
- * Asynchronously performs an RPC call using the instance's RPC implementation.
- * @name ProtoBuf.Builder.Service#[Method]
- * @function
- * @param {ProtoBuf.Builder.Message} req Request
- * @param {function(Error, (ProtoBuf.Builder.Message|ByteBuffer|Buffer|string)=)} callback Callback receiving
- * the error if any and the response either as a pre-parsed message or as its raw bytes
- * @abstract
- */
- var rpc = T.getChildren(ProtoBuf.Reflect.Service.RPCMethod);
- for (var i=0; i<rpc.length; i++) {
- (function(method) {
- // service#Method(message, callback)
- ServicePrototype[method.name] = function(req, callback) {
- try {
- if (!req || !(req instanceof method.resolvedRequestType.clazz)) {
- setTimeout(callback.bind(this, Error("Illegal request type provided to service method "+T.name+"#"+method.name)), 0);
- return;
- }
- this.rpcImpl(method.fqn(), req, function(err, res) { // Assumes that this is properly async
- if (err) {
- callback(err);
- return;
- }
- try { res = method.resolvedResponseType.clazz.decode(res); } catch (notABuffer) {}
- if (!res || !(res instanceof method.resolvedResponseType.clazz)) {
- callback(Error("Illegal response type received in service method "+ T.name+"#"+method.name));
- return;
- }
- callback(null, res);
- });
- } catch (err) {
- setTimeout(callback.bind(this, err), 0);
- }
- };
- // Service.Method(rpcImpl, message, callback)
- Service[method.name] = function(rpcImpl, req, callback) {
- new Service(rpcImpl)[method.name](req, callback);
- };
- if (Object.defineProperty)
- Object.defineProperty(Service[method.name], "$options", { "value": method.buildOpt() }),
- Object.defineProperty(ServicePrototype[method.name], "$options", { "value": Service[method.name]["$options"] });
- })(rpc[i]);
- }
- return Service;
- })(ProtoBuf, this);
- };
- /**
- * @alias ProtoBuf.Reflect.Service
- * @expose
- */
- Reflect.Service = Service;
- /**
- * Abstract service method.
- * @exports ProtoBuf.Reflect.Service.Method
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.Service} svc Service
- * @param {string} name Method name
- * @param {Object.<string,*>=} options Options
- * @constructor
- * @extends ProtoBuf.Reflect.T
- */
- var Method = function(builder, svc, name, options) {
- T.call(this, builder, svc, name);
- /**
- * @override
- */
- this.className = "Service.Method";
- /**
- * Options.
- * @type {Object.<string, *>}
- * @expose
- */
- this.options = options || {};
- };
- /**
- * @alias ProtoBuf.Reflect.Service.Method.prototype
- * @inner
- */
- var MethodPrototype = Method.prototype = Object.create(T.prototype);
- /**
- * Builds the method's '$options' property.
- * @name ProtoBuf.Reflect.Service.Method#buildOpt
- * @function
- * @return {Object.<string,*>}
- */
- MethodPrototype.buildOpt = NamespacePrototype.buildOpt;
- /**
- * @alias ProtoBuf.Reflect.Service.Method
- * @expose
- */
- Reflect.Service.Method = Method;
- /**
- * RPC service method.
- * @exports ProtoBuf.Reflect.Service.RPCMethod
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.Service} svc Service
- * @param {string} name Method name
- * @param {string} request Request message name
- * @param {string} response Response message name
- * @param {Object.<string,*>=} options Options
- * @constructor
- * @extends ProtoBuf.Reflect.Service.Method
- */
- var RPCMethod = function(builder, svc, name, request, response, options) {
- Method.call(this, builder, svc, name, options);
- /**
- * @override
- */
- this.className = "Service.RPCMethod";
- /**
- * Request message name.
- * @type {string}
- * @expose
- */
- this.requestName = request;
- /**
- * Response message name.
- * @type {string}
- * @expose
- */
- this.responseName = response;
- /**
- * Resolved request message type.
- * @type {ProtoBuf.Reflect.Message}
- * @expose
- */
- this.resolvedRequestType = null;
- /**
- * Resolved response message type.
- * @type {ProtoBuf.Reflect.Message}
- * @expose
- */
- this.resolvedResponseType = null;
- };
- // Extends Method
- RPCMethod.prototype = Object.create(Method.prototype);
- /**
- * @alias ProtoBuf.Reflect.Service.RPCMethod
- * @expose
- */
- Reflect.Service.RPCMethod = RPCMethod;
- return Reflect;
- })(ProtoBuf);
- /**
- * @alias ProtoBuf.Builder
- * @expose
- */
- ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
- "use strict";
- /**
- * Constructs a new Builder.
- * @exports ProtoBuf.Builder
- * @class Provides the functionality to build protocol messages.
- * @param {Object.<string,*>=} options Options
- * @constructor
- */
- var Builder = function(options) {
- /**
- * Namespace.
- * @type {ProtoBuf.Reflect.Namespace}
- * @expose
- */
- this.ns = new Reflect.Namespace(this, null, ""); // Global namespace
- /**
- * Namespace pointer.
- * @type {ProtoBuf.Reflect.T}
- * @expose
- */
- this.ptr = this.ns;
- /**
- * Resolved flag.
- * @type {boolean}
- * @expose
- */
- this.resolved = false;
- /**
- * The current building result.
- * @type {Object.<string,ProtoBuf.Builder.Message|Object>|null}
- * @expose
- */
- this.result = null;
- /**
- * Imported files.
- * @type {Array.<string>}
- * @expose
- */
- this.files = {};
- /**
- * Import root override.
- * @type {?string}
- * @expose
- */
- this.importRoot = null;
- /**
- * Options.
- * @type {!Object.<string, *>}
- * @expose
- */
- this.options = options || {};
- };
- /**
- * @alias ProtoBuf.Builder.prototype
- * @inner
- */
- var BuilderPrototype = Builder.prototype;
- /**
- * Resets the pointer to the root namespace.
- * @expose
- */
- BuilderPrototype.reset = function() {
- this.ptr = this.ns;
- };
- /**
- * Defines a package on top of the current pointer position and places the pointer on it.
- * @param {string} pkg
- * @param {Object.<string,*>=} options
- * @return {ProtoBuf.Builder} this
- * @throws {Error} If the package name is invalid
- * @expose
- */
- BuilderPrototype.define = function(pkg, options) {
- if (typeof pkg !== 'string' || !Lang.TYPEREF.test(pkg))
- throw Error("Illegal package: "+pkg);
- var part = pkg.split("."), i;
- for (i=0; i<part.length; i++) // To be absolutely sure
- if (!Lang.NAME.test(part[i]))
- throw Error("Illegal package: "+part[i]);
- for (i=0; i<part.length; i++) {
- if (this.ptr.getChild(part[i]) === null) // Keep existing namespace
- this.ptr.addChild(new Reflect.Namespace(this, this.ptr, part[i], options));
- this.ptr = this.ptr.getChild(part[i]);
- }
- return this;
- };
- /**
- * Tests if a definition is a valid message definition.
- * @param {Object.<string,*>} def Definition
- * @return {boolean} true if valid, else false
- * @expose
- */
- Builder.isValidMessage = function(def) {
- // Messages require a string name
- if (typeof def["name"] !== 'string' || !Lang.NAME.test(def["name"]))
- return false;
- // Messages must not contain values (that'd be an enum) or methods (that'd be a service)
- if (typeof def["values"] !== 'undefined' || typeof def["rpc"] !== 'undefined')
- return false;
- // Fields, enums and messages are arrays if provided
- var i;
- if (typeof def["fields"] !== 'undefined') {
- if (!ProtoBuf.Util.isArray(def["fields"]))
- return false;
- var ids = [], id; // IDs must be unique
- for (i=0; i<def["fields"].length; i++) {
- if (!Builder.isValidMessageField(def["fields"][i]))
- return false;
- id = parseInt(def["fields"][i]["id"], 10);
- if (ids.indexOf(id) >= 0)
- return false;
- ids.push(id);
- }
- ids = null;
- }
- if (typeof def["enums"] !== 'undefined') {
- if (!ProtoBuf.Util.isArray(def["enums"]))
- return false;
- for (i=0; i<def["enums"].length; i++)
- if (!Builder.isValidEnum(def["enums"][i]))
- return false;
- }
- if (typeof def["messages"] !== 'undefined') {
- if (!ProtoBuf.Util.isArray(def["messages"]))
- return false;
- for (i=0; i<def["messages"].length; i++)
- if (!Builder.isValidMessage(def["messages"][i]) && !Builder.isValidExtend(def["messages"][i]))
- return false;
- }
- if (typeof def["extensions"] !== 'undefined')
- if (!ProtoBuf.Util.isArray(def["extensions"]) || def["extensions"].length !== 2 || typeof def["extensions"][0] !== 'number' || typeof def["extensions"][1] !== 'number')
- return false;
- return true;
- };
- /**
- * Tests if a definition is a valid message field definition.
- * @param {Object} def Definition
- * @return {boolean} true if valid, else false
- * @expose
- */
- Builder.isValidMessageField = function(def) {
- // Message fields require a string rule, name and type and an id
- if (typeof def["rule"] !== 'string' || typeof def["name"] !== 'string' || typeof def["type"] !== 'string' || typeof def["id"] === 'undefined')
- return false;
- if (!Lang.RULE.test(def["rule"]) || !Lang.NAME.test(def["name"]) || !Lang.TYPEREF.test(def["type"]) || !Lang.ID.test(""+def["id"]))
- return false;
- if (typeof def["options"] !== 'undefined') {
- // Options are objects
- if (typeof def["options"] !== 'object')
- return false;
- // Options are <string,string|number|boolean>
- var keys = Object.keys(def["options"]);
- for (var i=0, key; i<keys.length; i++)
- if (typeof (key = keys[i]) !== 'string' || (typeof def["options"][key] !== 'string' && typeof def["options"][key] !== 'number' && typeof def["options"][key] !== 'boolean'))
- return false;
- }
- return true;
- };
- /**
- * Tests if a definition is a valid enum definition.
- * @param {Object} def Definition
- * @return {boolean} true if valid, else false
- * @expose
- */
- Builder.isValidEnum = function(def) {
- // Enums require a string name
- if (typeof def["name"] !== 'string' || !Lang.NAME.test(def["name"]))
- return false;
- // Enums require at least one value
- if (typeof def["values"] === 'undefined' || !ProtoBuf.Util.isArray(def["values"]) || def["values"].length == 0)
- return false;
- for (var i=0; i<def["values"].length; i++) {
- // Values are objects
- if (typeof def["values"][i] != "object")
- return false;
- // Values require a string name and an id
- if (typeof def["values"][i]["name"] !== 'string' || typeof def["values"][i]["id"] === 'undefined')
- return false;
- if (!Lang.NAME.test(def["values"][i]["name"]) || !Lang.NEGID.test(""+def["values"][i]["id"]))
- return false;
- }
- // It's not important if there are other fields because ["values"] is already unique
- return true;
- };
- /**
- * Creates ths specified protocol types at the current pointer position.
- * @param {Array.<Object.<string,*>>} defs Messages, enums or services to create
- * @return {ProtoBuf.Builder} this
- * @throws {Error} If a message definition is invalid
- * @expose
- */
- BuilderPrototype.create = function(defs) {
- if (!defs)
- return this; // Nothing to create
- if (!ProtoBuf.Util.isArray(defs))
- defs = [defs];
- if (defs.length == 0)
- return this;
- // It's quite hard to keep track of scopes and memory here, so let's do this iteratively.
- var stack = [];
- stack.push(defs); // One level [a, b, c]
- while (stack.length > 0) {
- defs = stack.pop();
- if (ProtoBuf.Util.isArray(defs)) { // Stack always contains entire namespaces
- while (defs.length > 0) {
- var def = defs.shift(); // Namespace always contains an array of messages, enums and services
- if (Builder.isValidMessage(def)) {
- var obj = new Reflect.Message(this, this.ptr, def["name"], def["options"], def["isGroup"]);
- // Create OneOfs
- var oneofs = {};
- if (def["oneofs"]) {
- var keys = Object.keys(def["oneofs"]);
- for (var i=0, k=keys.length; i<k; ++i)
- obj.addChild(oneofs[keys[i]] = new Reflect.Message.OneOf(this, obj, keys[i]));
- }
- // Create fields
- if (def["fields"] && def["fields"].length > 0) {
- for (i=0, k=def["fields"].length; i<k; ++i) { // i:k=Fields
- var fld = def['fields'][i];
- if (obj.getChild(fld['id']) !== null)
- throw Error("Duplicate field id in message "+obj.name+": "+fld['id']);
- if (fld["options"]) {
- var opts = Object.keys(fld["options"]);
- for (var j= 0,l=opts.length; j<l; ++j) { // j:l=Option names
- if (typeof opts[j] !== 'string')
- throw Error("Illegal field option name in message "+obj.name+"#"+fld["name"]+": "+opts[j]);
- if (typeof fld["options"][opts[j]] !== 'string' && typeof fld["options"][opts[j]] !== 'number' && typeof fld["options"][opts[j]] !== 'boolean')
- throw Error("Illegal field option value in message "+obj.name+"#"+fld["name"]+"#"+opts[j]+": "+fld["options"][opts[j]]);
- }
- }
- var oneof = null;
- if (typeof fld["oneof"] === 'string') {
- oneof = oneofs[fld["oneof"]];
- if (typeof oneof === 'undefined')
- throw Error("Illegal oneof in message "+obj.name+"#"+fld["name"]+": "+fld["oneof"]);
- }
- fld = new Reflect.Message.Field(this, obj, fld["rule"], fld["type"], fld["name"], fld["id"], fld["options"], oneof);
- if (oneof)
- oneof.fields.push(fld);
- obj.addChild(fld);
- }
- }
- // Push enums and messages to stack
- var subObj = [];
- if (typeof def["enums"] !== 'undefined' && def['enums'].length > 0)
- for (i=0; i<def["enums"].length; i++)
- subObj.push(def["enums"][i]);
- if (def["messages"] && def["messages"].length > 0)
- for (i=0; i<def["messages"].length; i++)
- subObj.push(def["messages"][i]);
- // Set extension range
- if (def["extensions"]) {
- obj.extensions = def["extensions"];
- if (obj.extensions[0] < ProtoBuf.ID_MIN)
- obj.extensions[0] = ProtoBuf.ID_MIN;
- if (obj.extensions[1] > ProtoBuf.ID_MAX)
- obj.extensions[1] = ProtoBuf.ID_MAX;
- }
- this.ptr.addChild(obj); // Add to current namespace
- if (subObj.length > 0) {
- stack.push(defs); // Push the current level back
- defs = subObj; // Continue processing sub level
- subObj = null;
- this.ptr = obj; // And move the pointer to this namespace
- obj = null;
- continue;
- }
- subObj = null;
- obj = null;
- } else if (Builder.isValidEnum(def)) {
- obj = new Reflect.Enum(this, this.ptr, def["name"], def["options"]);
- for (i=0; i<def["values"].length; i++)
- obj.addChild(new Reflect.Enum.Value(this, obj, def["values"][i]["name"], def["values"][i]["id"]));
- this.ptr.addChild(obj);
- obj = null;
- } else if (Builder.isValidService(def)) {
- obj = new Reflect.Service(this, this.ptr, def["name"], def["options"]);
- for (i in def["rpc"])
- if (def["rpc"].hasOwnProperty(i))
- obj.addChild(new Reflect.Service.RPCMethod(this, obj, i, def["rpc"][i]["request"], def["rpc"][i]["response"], def["rpc"][i]["options"]));
- this.ptr.addChild(obj);
- obj = null;
- } else if (Builder.isValidExtend(def)) {
- obj = this.ptr.resolve(def["ref"]);
- if (obj) {
- for (i=0; i<def["fields"].length; i++) { // i=Fields
- if (obj.getChild(def['fields'][i]['id']) !== null)
- throw Error("Duplicate extended field id in message "+obj.name+": "+def['fields'][i]['id']);
- if (def['fields'][i]['id'] < obj.extensions[0] || def['fields'][i]['id'] > obj.extensions[1])
- throw Error("Illegal extended field id in message "+obj.name+": "+def['fields'][i]['id']+" ("+obj.extensions.join(' to ')+" expected)");
- // Convert extension field names to camel case notation if the override is set
- var name = def["fields"][i]["name"];
- if (this.options['convertFieldsToCamelCase'])
- name = Reflect.Message.Field._toCamelCase(def["fields"][i]["name"]);
- // see #161: Extensions use their fully qualified name as their runtime key and...
- fld = new Reflect.Message.ExtensionField(this, obj, def["fields"][i]["rule"], def["fields"][i]["type"], this.ptr.fqn()+'.'+name, def["fields"][i]["id"], def["fields"][i]["options"]);
- // ...are added on top of the current namespace as an extension which is used for
- // resolving their type later on (the extension always keeps the original name to
- // prevent naming collisions)
- var ext = new Reflect.Extension(this, this.ptr, def["fields"][i]["name"], fld);
- fld.extension = ext;
- this.ptr.addChild(ext);
- obj.addChild(fld);
- }
- } else if (!/\.?google\.protobuf\./.test(def["ref"])) // Silently skip internal extensions
- throw Error("Extended message "+def["ref"]+" is not defined");
- } else
- throw Error("Not a valid definition: "+JSON.stringify(def));
- def = null;
- }
- // Break goes here
- } else
- throw Error("Not a valid namespace: "+JSON.stringify(defs));
- defs = null;
- this.ptr = this.ptr.parent; // This namespace is s done
- }
- this.resolved = false; // Require re-resolve
- this.result = null; // Require re-build
- return this;
- };
- /**
- * Imports another definition into this builder.
- * @param {Object.<string,*>} json Parsed import
- * @param {(string|{root: string, file: string})=} filename Imported file name
- * @return {ProtoBuf.Builder} this
- * @throws {Error} If the definition or file cannot be imported
- * @expose
- */
- BuilderPrototype["import"] = function(json, filename) {
- if (typeof filename === 'string') {
- if (ProtoBuf.Util.IS_NODE)
- filename = require("path")['resolve'](filename);
- if (this.files[filename] === true) {
- this.reset();
- return this; // Skip duplicate imports
- }
- this.files[filename] = true;
- }
- if (!!json['imports'] && json['imports'].length > 0) {
- var importRoot, delim = '/', resetRoot = false;
- if (typeof filename === 'object') { // If an import root is specified, override
- this.importRoot = filename["root"]; resetRoot = true; // ... and reset afterwards
- importRoot = this.importRoot;
- filename = filename["file"];
- if (importRoot.indexOf("\\") >= 0 || filename.indexOf("\\") >= 0) delim = '\\';
- } else if (typeof filename === 'string') {
- if (this.importRoot) // If import root is overridden, use it
- importRoot = this.importRoot;
- else { // Otherwise compute from filename
- if (filename.indexOf("/") >= 0) { // Unix
- importRoot = filename.replace(/\/[^\/]*$/, "");
- if (/* /file.proto */ importRoot === "")
- importRoot = "/";
- } else if (filename.indexOf("\\") >= 0) { // Windows
- importRoot = filename.replace(/\\[^\\]*$/, "");
- delim = '\\';
- } else
- importRoot = ".";
- }
- } else
- importRoot = null;
- for (var i=0; i<json['imports'].length; i++) {
- if (typeof json['imports'][i] === 'string') { // Import file
- if (!importRoot)
- throw Error("Cannot determine import root: File name is unknown");
- var importFilename = json['imports'][i];
- if (/^google\/protobuf\//.test(importFilename))
- continue; // Not needed and therefore not used
- importFilename = importRoot+delim+importFilename;
- if (this.files[importFilename] === true)
- continue; // Already imported
- if (/\.proto$/i.test(importFilename) && !ProtoBuf.DotProto) // If this is a NOPARSE build
- importFilename = importFilename.replace(/\.proto$/, ".json"); // always load the JSON file
- var contents = ProtoBuf.Util.fetch(importFilename);
- if (contents === null)
- throw Error("Failed to import '"+importFilename+"' in '"+filename+"': File not found");
- if (/\.json$/i.test(importFilename)) // Always possible
- this["import"](JSON.parse(contents+""), importFilename); // May throw
- else
- this["import"]((new ProtoBuf.DotProto.Parser(contents+"")).parse(), importFilename); // May throw
- } else // Import structure
- if (!filename)
- this["import"](json['imports'][i]);
- else if (/\.(\w+)$/.test(filename)) // With extension: Append _importN to the name portion to make it unique
- this["import"](json['imports'][i], filename.replace(/^(.+)\.(\w+)$/, function($0, $1, $2) { return $1+"_import"+i+"."+$2; }));
- else // Without extension: Append _importN to make it unique
- this["import"](json['imports'][i], filename+"_import"+i);
- }
- if (resetRoot) // Reset import root override when all imports are done
- this.importRoot = null;
- }
- if (json['messages']) {
- if (json['package'])
- this.define(json['package'], json["options"]);
- this.create(json['messages']);
- this.reset();
- }
- if (json['enums']) {
- if (json['package'])
- this.define(json['package'], json["options"]);
- this.create(json['enums']);
- this.reset();
- }
- if (json['services']) {
- if (json['package'])
- this.define(json['package'], json["options"]);
- this.create(json['services']);
- this.reset();
- }
- if (json['extends']) {
- if (json['package'])
- this.define(json['package'], json["options"]);
- this.create(json['extends']);
- this.reset();
- }
- return this;
- };
- /**
- * Tests if a definition is a valid service definition.
- * @param {Object} def Definition
- * @return {boolean} true if valid, else false
- * @expose
- */
- Builder.isValidService = function(def) {
- // Services require a string name and an rpc object
- return !(typeof def["name"] !== 'string' || !Lang.NAME.test(def["name"]) || typeof def["rpc"] !== 'object');
- };
- /**
- * Tests if a definition is a valid extension.
- * @param {Object} def Definition
- * @returns {boolean} true if valid, else false
- * @expose
- */
- Builder.isValidExtend = function(def) {
- if (typeof def["ref"] !== 'string' || !Lang.TYPEREF.test(def["ref"]))
- return false;
- var i;
- if (typeof def["fields"] !== 'undefined') {
- if (!ProtoBuf.Util.isArray(def["fields"]))
- return false;
- var ids = [], id; // IDs must be unique (does not yet test for the extended message's ids)
- for (i=0; i<def["fields"].length; i++) {
- if (!Builder.isValidMessageField(def["fields"][i]))
- return false;
- id = parseInt(def["id"], 10);
- if (ids.indexOf(id) >= 0)
- return false;
- ids.push(id);
- }
- ids = null;
- }
- return true;
- };
- /**
- * Resolves all namespace objects.
- * @throws {Error} If a type cannot be resolved
- * @expose
- */
- BuilderPrototype.resolveAll = function() {
- // Resolve all reflected objects
- var res;
- if (this.ptr == null || typeof this.ptr.type === 'object')
- return; // Done (already resolved)
- if (this.ptr instanceof Reflect.Namespace) {
- // Build all children
- var children = this.ptr.children;
- for (var i= 0, k=children.length; i<k; ++i)
- this.ptr = children[i],
- this.resolveAll();
- } else if (this.ptr instanceof Reflect.Message.Field) {
- if (!Lang.TYPE.test(this.ptr.type)) { // Resolve type...
- if (!Lang.TYPEREF.test(this.ptr.type))
- throw Error("Illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.type);
- res = (this.ptr instanceof Reflect.Message.ExtensionField ? this.ptr.extension.parent : this.ptr.parent).resolve(this.ptr.type, true);
- if (!res)
- throw Error("Unresolvable type reference in "+this.ptr.toString(true)+": "+this.ptr.type);
- this.ptr.resolvedType = res;
- if (res instanceof Reflect.Enum)
- this.ptr.type = ProtoBuf.TYPES["enum"];
- else if (res instanceof Reflect.Message)
- this.ptr.type = res.isGroup ? ProtoBuf.TYPES["group"] : ProtoBuf.TYPES["message"];
- else
- throw Error("Illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.type);
- } else
- this.ptr.type = ProtoBuf.TYPES[this.ptr.type];
- } else if (this.ptr instanceof ProtoBuf.Reflect.Enum.Value) {
- // No need to build enum values (built in enum)
- } else if (this.ptr instanceof ProtoBuf.Reflect.Service.Method) {
- if (this.ptr instanceof ProtoBuf.Reflect.Service.RPCMethod) {
- res = this.ptr.parent.resolve(this.ptr.requestName);
- if (!res || !(res instanceof ProtoBuf.Reflect.Message))
- throw Error("Illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.requestName);
- this.ptr.resolvedRequestType = res;
- res = this.ptr.parent.resolve(this.ptr.responseName);
- if (!res || !(res instanceof ProtoBuf.Reflect.Message))
- throw Error("Illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.responseName);
- this.ptr.resolvedResponseType = res;
- } else {
- // Should not happen as nothing else is implemented
- throw Error("Illegal service type in "+this.ptr.toString(true));
- }
- } else if (!(this.ptr instanceof ProtoBuf.Reflect.Message.OneOf) && !(this.ptr instanceof ProtoBuf.Reflect.Extension))
- throw Error("Illegal object in namespace: "+typeof(this.ptr)+":"+this.ptr);
- this.reset();
- };
- /**
- * Builds the protocol. This will first try to resolve all definitions and, if this has been successful,
- * return the built package.
- * @param {string=} path Specifies what to return. If omitted, the entire namespace will be returned.
- * @return {ProtoBuf.Builder.Message|Object.<string,*>}
- * @throws {Error} If a type could not be resolved
- * @expose
- */
- BuilderPrototype.build = function(path) {
- this.reset();
- if (!this.resolved)
- this.resolveAll(),
- this.resolved = true,
- this.result = null; // Require re-build
- if (this.result == null) // (Re-)Build
- this.result = this.ns.build();
- if (!path)
- return this.result;
- else {
- var part = path.split(".");
- var ptr = this.result; // Build namespace pointer (no hasChild etc.)
- for (var i=0; i<part.length; i++)
- if (ptr[part[i]])
- ptr = ptr[part[i]];
- else {
- ptr = null;
- break;
- }
- return ptr;
- }
- };
- /**
- * Similar to {@link ProtoBuf.Builder#build}, but looks up the internal reflection descriptor.
- * @param {string=} path Specifies what to return. If omitted, the entire namespace wiil be returned.
- * @return {ProtoBuf.Reflect.T} Reflection descriptor or `null` if not found
- */
- BuilderPrototype.lookup = function(path) {
- return path ? this.ns.resolve(path) : this.ns;
- };
- /**
- * Returns a string representation of this object.
- * @return {string} String representation as of "Builder"
- * @expose
- */
- BuilderPrototype.toString = function() {
- return "Builder";
- };
- // Pseudo types documented in Reflect.js.
- // Exist for the sole purpose of being able to "... instanceof ProtoBuf.Builder.Message" etc.
- Builder.Message = function() {};
- Builder.Service = function() {};
- return Builder;
- })(ProtoBuf, ProtoBuf.Lang, ProtoBuf.Reflect);
- /**
- * Loads a .proto string and returns the Builder.
- * @param {string} proto .proto file contents
- * @param {(ProtoBuf.Builder|string|{root: string, file: string})=} builder Builder to append to. Will create a new one if omitted.
- * @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.
- * @return {ProtoBuf.Builder} Builder to create new messages
- * @throws {Error} If the definition cannot be parsed or built
- * @expose
- */
- ProtoBuf.loadProto = function(proto, builder, filename) {
- if (typeof builder === 'string' || (builder && typeof builder["file"] === 'string' && typeof builder["root"] === 'string'))
- filename = builder,
- builder = undefined;
- return ProtoBuf.loadJson((new ProtoBuf.DotProto.Parser(proto)).parse(), builder, filename);
- };
- /**
- * Loads a .proto string and returns the Builder. This is an alias of {@link ProtoBuf.loadProto}.
- * @function
- * @param {string} proto .proto file contents
- * @param {(ProtoBuf.Builder|string)=} builder Builder to append to. Will create a new one if omitted.
- * @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.
- * @return {ProtoBuf.Builder} Builder to create new messages
- * @throws {Error} If the definition cannot be parsed or built
- * @expose
- */
- ProtoBuf.protoFromString = ProtoBuf.loadProto; // Legacy
- /**
- * Loads a .proto file and returns the Builder.
- * @param {string|{root: string, file: string}} filename Path to proto file or an object specifying 'file' with
- * an overridden 'root' path for all imported files.
- * @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and
- * the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the
- * file will be read synchronously and this function will return the Builder.
- * @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.
- * @return {?ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the
- * request has failed), else undefined
- * @expose
- */
- ProtoBuf.loadProtoFile = function(filename, callback, builder) {
- if (callback && typeof callback === 'object')
- builder = callback,
- callback = null;
- else if (!callback || typeof callback !== 'function')
- callback = null;
- if (callback)
- return ProtoBuf.Util.fetch(typeof filename === 'string' ? filename : filename["root"]+"/"+filename["file"], function(contents) {
- if (contents === null) {
- callback(Error("Failed to fetch file"));
- return;
- }
- try {
- callback(null, ProtoBuf.loadProto(contents, builder, filename));
- } catch (e) {
- callback(e);
- }
- });
- var contents = ProtoBuf.Util.fetch(typeof filename === 'object' ? filename["root"]+"/"+filename["file"] : filename);
- return contents === null ? null : ProtoBuf.loadProto(contents, builder, filename);
- };
- /**
- * Loads a .proto file and returns the Builder. This is an alias of {@link ProtoBuf.loadProtoFile}.
- * @function
- * @param {string|{root: string, file: string}} filename Path to proto file or an object specifying 'file' with
- * an overridden 'root' path for all imported files.
- * @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and
- * the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the
- * file will be read synchronously and this function will return the Builder.
- * @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.
- * @return {!ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the
- * request has failed), else undefined
- * @expose
- */
- ProtoBuf.protoFromFile = ProtoBuf.loadProtoFile; // Legacy
- /**
- * Constructs a new empty Builder.
- * @param {Object.<string,*>=} options Builder options, defaults to global options set on ProtoBuf
- * @return {!ProtoBuf.Builder} Builder
- * @expose
- */
- ProtoBuf.newBuilder = function(options) {
- options = options || {};
- if (typeof options['convertFieldsToCamelCase'] === 'undefined')
- options['convertFieldsToCamelCase'] = ProtoBuf.convertFieldsToCamelCase;
- if (typeof options['populateAccessors'] === 'undefined')
- options['populateAccessors'] = ProtoBuf.populateAccessors;
- return new ProtoBuf.Builder(options);
- };
- /**
- * Loads a .json definition and returns the Builder.
- * @param {!*|string} json JSON definition
- * @param {(ProtoBuf.Builder|string|{root: string, file: string})=} builder Builder to append to. Will create a new one if omitted.
- * @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.
- * @return {ProtoBuf.Builder} Builder to create new messages
- * @throws {Error} If the definition cannot be parsed or built
- * @expose
- */
- ProtoBuf.loadJson = function(json, builder, filename) {
- if (typeof builder === 'string' || (builder && typeof builder["file"] === 'string' && typeof builder["root"] === 'string'))
- filename = builder,
- builder = null;
- if (!builder || typeof builder !== 'object')
- builder = ProtoBuf.newBuilder();
- if (typeof json === 'string')
- json = JSON.parse(json);
- builder["import"](json, filename);
- builder.resolveAll();
- builder.build();
- return builder;
- };
- /**
- * Loads a .json file and returns the Builder.
- * @param {string|!{root: string, file: string}} filename Path to json file or an object specifying 'file' with
- * an overridden 'root' path for all imported files.
- * @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and
- * the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the
- * file will be read synchronously and this function will return the Builder.
- * @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.
- * @return {?ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the
- * request has failed), else undefined
- * @expose
- */
- ProtoBuf.loadJsonFile = function(filename, callback, builder) {
- if (callback && typeof callback === 'object')
- builder = callback,
- callback = null;
- else if (!callback || typeof callback !== 'function')
- callback = null;
- if (callback)
- return ProtoBuf.Util.fetch(typeof filename === 'string' ? filename : filename["root"]+"/"+filename["file"], function(contents) {
- if (contents === null) {
- callback(Error("Failed to fetch file"));
- return;
- }
- try {
- callback(null, ProtoBuf.loadJson(JSON.parse(contents), builder, filename));
- } catch (e) {
- callback(e);
- }
- });
- var contents = ProtoBuf.Util.fetch(typeof filename === 'object' ? filename["root"]+"/"+filename["file"] : filename);
- return contents === null ? null : ProtoBuf.loadJson(JSON.parse(contents), builder, filename);
- };
- return ProtoBuf;
- }
- /* CommonJS */ if (typeof require === 'function' && typeof module === 'object' && module && typeof exports === 'object' && exports)
- module['exports'] = init(require("bytebuffer"));
- /* AMD */ else if (typeof define === 'function' && define["amd"])
- define(["ByteBuffer"], init);
- /* Global */ else
- (global["dcodeIO"] = global["dcodeIO"] || {})["ProtoBuf"] = init(global["dcodeIO"]["ByteBuffer"]);
- })(this);
|