excel.js 1.2 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627
  1. /*
  2. * @Author: Jeffrey Wang
  3. * @Desc: 整理强大的 SheetJS 功能,依赖 XLSX.js 和 FileSaver
  4. * @Version: v1.4
  5. * @Date: 2018-03-24 09:54:17
  6. * @Last Modified by: Jeffrey Wang
  7. * @Last Modified time: 2019-01-15 11:49:09
  8. */
  9. if (typeof layui === 'undefined' && typeof jQuery === 'undefined') {
  10. console.error('非layui调用请先加载jQuery')
  11. }
  12. if (typeof jQuery !== 'undefined') {
  13. $ = jQuery
  14. }
  15. LAY_EXCEL = {
  16. /**
  17. * 兼容老版本的导出函数
  18. * @param {[type]} data [description]
  19. * @param {[type]} filename [description]
  20. * @param {[type]} type [description]
  21. * @return {[type]} [description]
  22. */
  23. downloadExl: function(data, filename, type) {
  24. type = type ? type : 'xlsx';
  25. this.exportExcel({sheet1: data}, filename+'.'+type, type, null);
  26. },
  27. /**
  28. * 导出Excel并弹出下载框,具体使用方法和范围请参考文档
  29. * @param data object
  30. * @param {[type]} filename [description]
  31. * @param {[type]} type [description]
  32. * @param {[type]} opt [description]
  33. * @return {[type]} [description]
  34. */
  35. exportExcel : function(data, filename, type, opt) {
  36. type = type ? type : 'xlsx';
  37. filename = filename ? filename : '导出数据.'+type;
  38. // 创建一个 XLSX 对象
  39. var wb = XLSX.utils.book_new();
  40. // 1. 定义excel对的基本属性
  41. var Props = {
  42. Title: filename,
  43. Subject: 'Export From web browser',
  44. Author: "excel.wj2015.com",
  45. Manager: '',
  46. Company: '',
  47. Category: '',
  48. Keywords: '',
  49. Comments: '',
  50. LastAuthor: '',
  51. CreatedData: new Date(),
  52. };
  53. opt && opt.Props && (Props = $.extend(Props, opt.Props));
  54. // 默认进行压缩
  55. wb.compression = opt ? opt.compression : true
  56. if(wb.compression !== false) {
  57. wb.compression = true
  58. }
  59. wb.Props = Props;
  60. // 特殊属性实现,比如合并单元格
  61. var wbExtend = {
  62. '!merges': null
  63. ,'!margins': null
  64. ,'!cols': null
  65. ,'!rows': null
  66. ,'!protect': null
  67. ,'!autofilter': null
  68. };
  69. opt && opt.extend && (wbExtend = $.extend(wbExtend, opt.extend));
  70. // 清理空配置
  71. for (var key in wbExtend) {
  72. if (!wbExtend.hasOwnProperty(key)) {
  73. continue;
  74. }
  75. if (!wbExtend[key]) {
  76. delete wbExtend[key];
  77. }
  78. }
  79. // 判断 data 如果是 sheet 级别数据,自动加 sheet1
  80. if ($.isArray(data)) {
  81. data = {sheet1: data};
  82. }
  83. for(var sheet_name in data) {
  84. if (!data.hasOwnProperty(sheet_name)) {
  85. continue;
  86. }
  87. var content = data[sheet_name];
  88. // 2. 设置sheet名称
  89. wb.SheetNames.push(sheet_name);
  90. // 3. 分配工作表对象到 sheet
  91. var is_aoa = false;
  92. if (content.length && content[0] && $.isArray(content[0])) {
  93. is_aoa = true;
  94. }
  95. if (is_aoa) {
  96. ws = XLSX.utils.aoa_to_sheet(content);
  97. } else {
  98. var option = {};
  99. if (content.length) {
  100. option.headers = content.unshift();
  101. option.skipHeader = true;
  102. // 分离并重组样式
  103. var splitRes = this.splitContent(content);
  104. }
  105. var ws = XLSX.utils.json_to_sheet(content, option);
  106. // 特殊属性,支持单独设置某个sheet的属性
  107. if (wbExtend[sheet_name]) {
  108. $.extend(ws, wbExtend[sheet_name]);
  109. } else {
  110. $.extend(ws, wbExtend);
  111. }
  112. // 合并样式
  113. if (typeof splitRes !== 'undefined') {
  114. this.mergeCellOpt(ws, splitRes.style);
  115. }
  116. }
  117. wb.Sheets[sheet_name] = ws;
  118. };
  119. // 4. 输出工作表
  120. var wbout = XLSX.write(wb, {bookType: type, type: 'binary', cellStyles: true, compression: wb.compression});
  121. // 5. 跨浏览器支持,采用 FileSaver 三方库
  122. saveAs(new Blob([this.s2ab(wbout)], {type: "application/octet-stream"}), filename);
  123. },
  124. /**
  125. * 分离内容和样式
  126. * @param {[type]} content [description]
  127. * @return {[type]} [description]
  128. */
  129. splitContent: function(content) {
  130. var styleContent = {};
  131. // 扫描每个单元格,如果是对象则等表格转换完毕后分离出来重新赋值
  132. for (var line = 0; line < content.length; line++) {
  133. var lineData = content[line];
  134. var rowIndex = 0;
  135. for (var row in lineData) {
  136. if (!lineData.hasOwnProperty(row)) {
  137. continue;
  138. }
  139. var rowData = lineData[row];
  140. if (typeof rowData === 'object') {
  141. // typeof null == object
  142. if (rowData !== null) {
  143. styleContent[this.numToTitle(rowIndex+1)+(parseInt(line)+1)] = rowData;
  144. } else {
  145. lineData[row] = '';
  146. }
  147. } else {
  148. // JeffreyWang 2019-03-10针对 0 的hack处理
  149. if (rowData === 0) {
  150. rowData = {
  151. v: '0',
  152. s: {
  153. alignment: {
  154. horizontal: 'right'
  155. }
  156. }
  157. }
  158. }
  159. styleContent[this.numToTitle(rowIndex+1)+(parseInt(line)+1)] = rowData;
  160. }
  161. rowIndex++;
  162. }
  163. }
  164. return {
  165. content: content,
  166. style: styleContent
  167. };
  168. },
  169. /**
  170. * 合并内容和样式
  171. * @param {[type]} ws [description]
  172. * @param {[type]} style [description]
  173. * @return {[type]} [description]
  174. */
  175. mergeCellOpt: function(ws, style) {
  176. for (var row in style) {
  177. if (!style.hasOwnProperty(row)) {
  178. continue;
  179. }
  180. var rowOpt = style[row];
  181. if (ws[row]) {
  182. // 其他属性做一个初始化
  183. var otherOpt = ['t', 'w', 'f', 'r', 'h', 'c', 'z', 'l', 's'];
  184. for (var i = 0; i < otherOpt.length; i++) {
  185. ws[row][otherOpt[i]] = ws[row][otherOpt[i]];
  186. }
  187. $.extend(ws[row], rowOpt);
  188. }
  189. }
  190. },
  191. /**
  192. * 将table转换为JSON数据
  193. * @param dom
  194. */
  195. tableToJson: function(dom) {
  196. dom = $(dom)
  197. var head = []
  198. dom.find('thead > tr').each(function () {
  199. var line = []
  200. $(this).find('td').each(function () {
  201. line.push($(this).text())
  202. })
  203. head.push(line)
  204. })
  205. var body = [];
  206. dom.find('tbody > tr').each(function () {
  207. var line = []
  208. $(this).find('td').each(function () {
  209. line.push($(this).text())
  210. })
  211. body.push(line)
  212. })
  213. return {
  214. head: head,
  215. body: body
  216. }
  217. },
  218. // 测试代码:
  219. // for(i=1;i<100;i++){var change = layui.excel.numToTitle(i);console.log(i, change, layui.excel.titleToNum(change));}
  220. // numsToTitle备忘录提效
  221. numsTitleCache: {},
  222. // titleToTitle 备忘录提效
  223. titleNumsCache: {},
  224. /**
  225. * 将数字(从一开始)转换为 A、B、C...AA、AB
  226. * @param {[int]} num [description]
  227. * @return {[type]} [description]
  228. */
  229. numToTitle: function(num) {
  230. if (this.numsTitleCache[num]) {
  231. return this.numsTitleCache[num];
  232. }
  233. var ans = '';
  234. if (num > 26) {
  235. // 要注意小心 26 的倍数导致的无限递归问题
  236. var dec = num % 26;
  237. ans = this.numToTitle((num - dec)/26) + this.numToTitle(dec?dec:26);
  238. this.numsTitleCache[num] = ans;
  239. this.titleNumsCache[ans] = num;
  240. return ans;
  241. } else {
  242. // A 的 ascii 为 0,顺位相加
  243. ans = String.fromCharCode(64 + num);
  244. this.numsTitleCache[num] = ans;
  245. this.titleNumsCache[ans] = num;
  246. return ans;
  247. }
  248. },
  249. /**
  250. * 将A、B、AA、ABC转换为 1、2、3形式的数字
  251. * @param {[type]} title [description]
  252. * @return {number} [description]
  253. */
  254. titleToNum: function(title) {
  255. if (this.titleNumsCache[title]) {
  256. return this.titleNumsCache[title];
  257. }
  258. var len = title.length;
  259. var total = 0;
  260. for (var index in title) {
  261. if (!title.hasOwnProperty(index)) {
  262. continue;
  263. }
  264. var char = title[index];
  265. var code = char.charCodeAt() - 64;
  266. total += code * Math.pow(26, len - index - 1);
  267. }
  268. this.numsTitleCache[total] = title;
  269. this.titleNumsCache[title] = total;
  270. return total;
  271. },
  272. /**
  273. * 批量设置单元格属性
  274. * @param {array} data [sheet级别的数据]
  275. * @param {string} range [范围字符串,比如 A1:C12,开始位置默认 A1,结束位置默认整个表格右下角]
  276. * @param {object} config [批量设置的单元格属性]
  277. * @param {function} filter [回调函数,传递函数生效,返回值作为新的值(可用于过滤、规则替换样式等骚操作)]
  278. * @return {array} [重新渲染后的 sheet 数据]
  279. */
  280. setExportCellStyle: function(data, range, config, filter) {
  281. if (typeof data !== 'object' || !data.length || !data[0] || !Object.keys(data[0]).length) {
  282. return [];
  283. }
  284. // 以 rowIndex 为键,field 为值
  285. var fieldKeys = Object.keys(data[0]);
  286. var maxCol = data.length -1;
  287. var maxRow = fieldKeys.length - 1;
  288. // 默认 A1 ~ 右下角
  289. var startPos = {c: 0, r: 0};
  290. var endPos = {c: maxCol, r: maxRow};
  291. if (range && typeof range === 'string') {
  292. var rangeArr = range.split(':');
  293. if (rangeArr[0].length) {
  294. startPos = this.splitPosition(rangeArr[0]);
  295. }
  296. if (typeof rangeArr[1] !== 'undefined' && rangeArr[1] !== '') {
  297. endPos = this.splitPosition(rangeArr[1]);
  298. }
  299. } else {
  300. // pass
  301. }
  302. // position范围限制 - 考虑到特殊情况取消此限制
  303. // startPos.c = startPos.c < maxCol ? startPos.c : maxCol;
  304. // endPos.c = endPos.c < maxCol ? endPos.c : maxCol;
  305. // startPos.r = startPos.r < maxRow ? startPos.r : maxRow;
  306. // endPos.r = endPos.r < maxRow ? endPos.r : maxRow;
  307. if (startPos.c > endPos.c) {
  308. console.error('开始列不得大于结束列');
  309. }
  310. if (startPos.r > endPos.r) {
  311. console.error('开始行不得大于结束行');
  312. }
  313. // 遍历范围内的数据,进行样式设置,按从上到下从左到右按行遍历
  314. for (var currentRow = startPos.r; currentRow <= endPos.r; currentRow++) {
  315. for (var currentCol = startPos.c; currentCol <= endPos.c; currentCol++) {
  316. // 如果有回调则执行回调判断,否则全部更新,如果遇到超出数据范围的,自动置空
  317. var row = data[currentRow];
  318. if (!row) {
  319. row = {};
  320. for (var key = 0; key < fieldKeys.length; key++) {
  321. row[fieldKeys[key]] = '';
  322. }
  323. data[currentRow] = row;
  324. }
  325. var cell = row[fieldKeys[currentCol]];
  326. var newCell = null;
  327. if (cell === null || cell === undefined) {
  328. cell = '';
  329. }
  330. // 手工合并(相同的则以当前函数config为准)
  331. if (typeof cell === 'object') {
  332. newCell = $.extend(true, {}, cell, config);
  333. } else {
  334. newCell = $.extend(true, {}, {v: cell}, config);
  335. }
  336. if (
  337. typeof filter === 'function'
  338. ) {
  339. newCell = filter(cell, newCell, row, config, currentRow, currentCol, fieldKeys[currentCol]);
  340. } else {
  341. }
  342. // 回写
  343. data[currentRow][fieldKeys[currentCol]] = newCell;
  344. }
  345. }
  346. return data;
  347. },
  348. /**
  349. * 合并单元格快速生成配置的函数 传入 [ ['开始坐标 A1', '结束坐标 D2'], ['开始坐标 B2', '结束坐标 E3'] ]
  350. * @param {[type]} origin [description]
  351. * @return {[type]} [description]
  352. */
  353. makeMergeConfig: function(origin) {
  354. var merge = [];
  355. for (var index = 0; index < origin.length; index++) {
  356. merge.push({
  357. s: this.splitPosition(origin[index][0]),
  358. e: this.splitPosition(origin[index][1]),
  359. });
  360. }
  361. return merge;
  362. },
  363. /**
  364. * 自动生成列宽配置
  365. * @param {$ObjMap} data [A、B、C的宽度映射]
  366. * @param {number} defaultNum [description]
  367. * @return {$ObjMap} [description]
  368. */
  369. makeColConfig: function(data, defaultNum) {
  370. defaultNum = defaultNum > 0 ? defaultNum : 50;
  371. // 将列的 ABC 转换为 index
  372. var change = [];
  373. var startIndex = 0;
  374. for (var index in data) {
  375. if (!data.hasOwnProperty(index)) {
  376. continue;
  377. }
  378. var item = data[index];
  379. if (index.match && index.match(/[A-Z]*/)) {
  380. var currentIndex = this.titleToNum(index) - 1;
  381. // 填充未配置的单元格
  382. while (startIndex < currentIndex) {
  383. change.push({wpx: defaultNum});
  384. startIndex++;
  385. }
  386. startIndex = currentIndex+1;
  387. change.push({wpx: item > 0 ? item : defaultNum});
  388. }
  389. };
  390. return change;
  391. },
  392. /**
  393. * 自动生成列高配置
  394. * @param {[type]} data [description]
  395. * @param {[type]} defaultNum [description]
  396. * @return {[type]} [description]
  397. */
  398. makeRowConfig: function(data, defaultNum) {
  399. defaultNum = defaultNum > 0 ? defaultNum : 10;
  400. // 将列的 ABC 转换为 index
  401. var change = [];
  402. var startIndex = 0;
  403. for (var index in data) {
  404. if (!data.hasOwnProperty(index)) {
  405. continue;
  406. }
  407. var item = data[index];
  408. if (index.match && index.match(/[0-9]*/)) {
  409. var currentIndex = parseInt(index) - 1;
  410. // 填充未配置的行
  411. while (startIndex < currentIndex) {
  412. change.push({hpx: defaultNum});
  413. startIndex++;
  414. }
  415. startIndex = currentIndex+1;
  416. change.push({hpx: item > 0 ? item : defaultNum});
  417. }
  418. };
  419. return change;
  420. },
  421. /**
  422. * 将A1分离成 {c: 0, r: 0} 格式的数据
  423. * @param {string} pos [description]
  424. * @return {{r: number, c: number}} [description]
  425. */
  426. splitPosition: function(pos) {
  427. var res = pos.match('^([A-Z]+)([0-9]+)$');
  428. if (!res) {
  429. return {c: 0, r: 0};
  430. }
  431. // 转换结果相比需要的结果需要减一转换
  432. return {
  433. c: this.titleToNum(res[1]) - 1,
  434. r: parseInt(res[2]) - 1
  435. }
  436. },
  437. /**
  438. * 将二进制数据转为8位字节
  439. * @param {[type]} s [description]
  440. * @return {[type]} [description]
  441. */
  442. s2ab: function(s) {
  443. var buf = new ArrayBuffer(s.length);
  444. var view = new Uint8Array(buf);
  445. for (var i = 0; i < s.length; i++) {
  446. view[i] = s.charCodeAt(i) & 0xFF;
  447. }
  448. return buf;
  449. },
  450. /**
  451. * 将导出的数据格式,转换为可以aoa导出的格式
  452. * @return {[type]} [description]
  453. */
  454. filterDataToAoaData: function(filterData){
  455. var aoaData = [];
  456. layui.each(filterData, function(index, item) {
  457. var itemData = [];
  458. for (var i in item) {
  459. if (!item.hasOwnProperty(i)) {
  460. continue;
  461. }
  462. itemData.push(item[i]);
  463. }
  464. aoaData.push(itemData);
  465. });
  466. return aoaData;
  467. },
  468. /**
  469. * 梳理导出的数据,包括字段排序和多余数据过滤,具体功能请参见文档
  470. * @param {[type]} data [需要梳理的数据]
  471. * @param {[type]} fields [支持数组和对象,用于映射关系和字段排序]
  472. * @return {[type]} [description]
  473. */
  474. filterExportData: function(data, fields) {
  475. // PS:之所以不直接引用 data 节省内存,是因为担心如果 fields 可能存在如下情况: { "id": 'test_id', 'test_id': 'new_id' },会导致处理异常
  476. var exportData = [];
  477. var true_fields = [];
  478. // filed 支持两种模式,数组则单纯排序,对象则转换映射关系,为了统一处理,将数组转换为符合要求的映射关系对象
  479. if (Array.isArray(fields)) {
  480. for (var i = 0; i< fields.length; i++) {
  481. true_fields[fields[i]] = fields[i];
  482. }
  483. } else {
  484. true_fields = fields;
  485. }
  486. for (var i = 0; i < data.length; i++) {
  487. var item = data[i];
  488. exportData[i] = {};
  489. for (var key in true_fields) {
  490. if (!true_fields.hasOwnProperty(key)) {
  491. continue;
  492. }
  493. var new_field_name = key;
  494. var old_field_name = true_fields[key];
  495. // 如果传入的是回调,则回调的值则为新值
  496. if (typeof old_field_name === 'function' && old_field_name.apply) {
  497. exportData[i][new_field_name] = old_field_name.apply(window, [item[new_field_name], item, data]);
  498. } else {
  499. if (typeof item[old_field_name] !== 'undefined') {
  500. exportData[i][new_field_name] = item[old_field_name];
  501. } else {
  502. exportData[i][new_field_name] = '';
  503. }
  504. }
  505. }
  506. }
  507. return exportData;
  508. },
  509. /**
  510. * 梳理导入的数据,参数意义可参考 filterExportData
  511. * @param {[type]} data [description]
  512. * @param {[type]} fields [description]
  513. * @return {[type]} [description]
  514. */
  515. filterImportData: function(data, fields) {
  516. var that = this;
  517. layui.each(data, function(fileindex, xlsx) {
  518. layui.each(xlsx, function(sheetname, content) {
  519. xlsx[sheetname] = that.filterExportData(content, fields);
  520. });
  521. });
  522. return data;
  523. },
  524. /**
  525. * 读取Excel,支持多文件多表格读取
  526. * @param {[type]} files [description]
  527. * @param {[type]} opt [description]
  528. * @param {Function} callback [description]
  529. * @return {[type]} [description]
  530. */
  531. importExcel: function(files, opt, callback) {
  532. var option = {
  533. header: 'A',
  534. range: null,
  535. fields: null,
  536. };
  537. $.extend(option, opt);
  538. var that = this;
  539. if (files.length < 1) {
  540. throw {code: 999, 'message': '传入文件为空'};
  541. }
  542. var supportReadMime = [
  543. 'application/vnd.ms-excel',
  544. 'application/msexcel',
  545. 'application/x-msexcel',
  546. 'application/x-ms-excel',
  547. 'application/x-excel',
  548. 'application/x-dos_ms_excel',
  549. 'application/xls',
  550. 'application/x-xls',
  551. 'application/vnd-xls',
  552. 'application/csv',
  553. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  554. ''
  555. ];
  556. layui.each(files, function(index, item) {
  557. if (supportReadMime.indexOf(item.type) === -1) {
  558. throw {code: 999, message: item.name+'('+item.type+')为不支持的文件类型'};
  559. }
  560. });
  561. // 按照二进制读取
  562. var data = {};
  563. layui.each(files, function(index, item) {
  564. var reader = new FileReader();
  565. if (!reader) {
  566. throw {code: 999, message: '不支持FileReader,请更换更新的浏览器'};
  567. }
  568. // 读取excel表格对象
  569. reader.onload = function(ev) {
  570. var wb = XLSX.read(ev.target.result, {
  571. type: 'binary'
  572. });
  573. var excelData = {};
  574. layui.each(wb.Sheets, function(sheet, sheetObj) {
  575. // 全为空的去掉
  576. if (wb.Sheets.hasOwnProperty(sheet)) {
  577. var opt = {
  578. header: option.header
  579. };
  580. if (option.range) {
  581. opt.range = option.range;
  582. }
  583. excelData[sheet] = XLSX.utils.sheet_to_json(sheetObj, opt);
  584. // 支持梳理数据
  585. if (option.fields) {
  586. excelData[sheet] = that.filterExportData(excelData[sheet], option.fields);
  587. }
  588. }
  589. });
  590. data[index] = excelData;
  591. // 全部读取完毕才执行
  592. if (index === files.length - 1) {
  593. callback && callback.apply && callback.apply(window, [data]);
  594. }
  595. };
  596. reader.readAsBinaryString(item);
  597. });
  598. }
  599. }
  600. if (typeof layui !== 'undefined') {
  601. layui.define(['jquery'], function(exports){
  602. $ = layui.jquery;
  603. exports('excel', LAY_EXCEL);
  604. });
  605. }
  606. /*---------split--------*//* Blob.js
  607. * A Blob, File, FileReader & URL implementation.
  608. * 2018-08-09
  609. *
  610. * By Eli Grey, http://eligrey.com
  611. * By Jimmy Wärting, https://github.com/jimmywarting
  612. * License: MIT
  613. * See https://github.com/eligrey/Blob.js/blob/master/LICENSE.md
  614. */
  615. ;(function(){
  616. var global = typeof window === 'object'
  617. ? window : typeof self === 'object'
  618. ? self : this
  619. var BlobBuilder = global.BlobBuilder
  620. || global.WebKitBlobBuilder
  621. || global.MSBlobBuilder
  622. || global.MozBlobBuilder;
  623. global.URL = global.URL || global.webkitURL || function(href, a) {
  624. a = document.createElement('a')
  625. a.href = href
  626. return a
  627. }
  628. var origBlob = global.Blob
  629. var createObjectURL = URL.createObjectURL
  630. var revokeObjectURL = URL.revokeObjectURL
  631. var strTag = global.Symbol && global.Symbol.toStringTag
  632. var blobSupported = false
  633. var blobSupportsArrayBufferView = false
  634. var arrayBufferSupported = !!global.ArrayBuffer
  635. var blobBuilderSupported = BlobBuilder
  636. && BlobBuilder.prototype.append
  637. && BlobBuilder.prototype.getBlob;
  638. try {
  639. // Check if Blob constructor is supported
  640. blobSupported = new Blob(['ä']).size === 2
  641. // Check if Blob constructor supports ArrayBufferViews
  642. // Fails in Safari 6, so we need to map to ArrayBuffers there.
  643. blobSupportsArrayBufferView = new Blob([new Uint8Array([1,2])]).size === 2
  644. } catch(e) {}
  645. /**
  646. * Helper function that maps ArrayBufferViews to ArrayBuffers
  647. * Used by BlobBuilder constructor and old browsers that didn't
  648. * support it in the Blob constructor.
  649. */
  650. function mapArrayBufferViews(ary) {
  651. return ary.map(function(chunk) {
  652. if (chunk.buffer instanceof ArrayBuffer) {
  653. var buf = chunk.buffer;
  654. // if this is a subarray, make a copy so we only
  655. // include the subarray region from the underlying buffer
  656. if (chunk.byteLength !== buf.byteLength) {
  657. var copy = new Uint8Array(chunk.byteLength);
  658. copy.set(new Uint8Array(buf, chunk.byteOffset, chunk.byteLength));
  659. buf = copy.buffer;
  660. }
  661. return buf;
  662. }
  663. return chunk;
  664. });
  665. }
  666. function BlobBuilderConstructor(ary, options) {
  667. options = options || {};
  668. var bb = new BlobBuilder();
  669. mapArrayBufferViews(ary).forEach(function(part) {
  670. bb.append(part);
  671. });
  672. return options.type ? bb.getBlob(options.type) : bb.getBlob();
  673. };
  674. function BlobConstructor(ary, options) {
  675. return new origBlob(mapArrayBufferViews(ary), options || {});
  676. };
  677. if (global.Blob) {
  678. BlobBuilderConstructor.prototype = Blob.prototype;
  679. BlobConstructor.prototype = Blob.prototype;
  680. }
  681. function FakeBlobBuilder() {
  682. function toUTF8Array(str) {
  683. var utf8 = [];
  684. for (var i=0; i < str.length; i++) {
  685. var charcode = str.charCodeAt(i);
  686. if (charcode < 0x80) utf8.push(charcode);
  687. else if (charcode < 0x800) {
  688. utf8.push(0xc0 | (charcode >> 6),
  689. 0x80 | (charcode & 0x3f));
  690. }
  691. else if (charcode < 0xd800 || charcode >= 0xe000) {
  692. utf8.push(0xe0 | (charcode >> 12),
  693. 0x80 | ((charcode>>6) & 0x3f),
  694. 0x80 | (charcode & 0x3f));
  695. }
  696. // surrogate pair
  697. else {
  698. i++;
  699. // UTF-16 encodes 0x10000-0x10FFFF by
  700. // subtracting 0x10000 and splitting the
  701. // 20 bits of 0x0-0xFFFFF into two halves
  702. charcode = 0x10000 + (((charcode & 0x3ff)<<10)
  703. | (str.charCodeAt(i) & 0x3ff));
  704. utf8.push(0xf0 | (charcode >>18),
  705. 0x80 | ((charcode>>12) & 0x3f),
  706. 0x80 | ((charcode>>6) & 0x3f),
  707. 0x80 | (charcode & 0x3f));
  708. }
  709. }
  710. return utf8;
  711. }
  712. function fromUtf8Array(array) {
  713. var out, i, len, c;
  714. var char2, char3;
  715. out = "";
  716. len = array.length;
  717. i = 0;
  718. while (i < len) {
  719. c = array[i++];
  720. switch (c >> 4)
  721. {
  722. case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  723. // 0xxxxxxx
  724. out += String.fromCharCode(c);
  725. break;
  726. case 12: case 13:
  727. // 110x xxxx 10xx xxxx
  728. char2 = array[i++];
  729. out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
  730. break;
  731. case 14:
  732. // 1110 xxxx 10xx xxxx 10xx xxxx
  733. char2 = array[i++];
  734. char3 = array[i++];
  735. out += String.fromCharCode(((c & 0x0F) << 12) |
  736. ((char2 & 0x3F) << 6) |
  737. ((char3 & 0x3F) << 0));
  738. break;
  739. }
  740. }
  741. return out;
  742. }
  743. function isDataView(obj) {
  744. return obj && DataView.prototype.isPrototypeOf(obj)
  745. }
  746. function bufferClone(buf) {
  747. var view = new Array(buf.byteLength)
  748. var array = new Uint8Array(buf)
  749. var i = view.length
  750. while(i--) {
  751. view[i] = array[i]
  752. }
  753. return view
  754. }
  755. function encodeByteArray(input) {
  756. var byteToCharMap = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  757. var output = [];
  758. for (var i = 0; i < input.length; i += 3) {
  759. var byte1 = input[i];
  760. var haveByte2 = i + 1 < input.length;
  761. var byte2 = haveByte2 ? input[i + 1] : 0;
  762. var haveByte3 = i + 2 < input.length;
  763. var byte3 = haveByte3 ? input[i + 2] : 0;
  764. var outByte1 = byte1 >> 2;
  765. var outByte2 = ((byte1 & 0x03) << 4) | (byte2 >> 4);
  766. var outByte3 = ((byte2 & 0x0F) << 2) | (byte3 >> 6);
  767. var outByte4 = byte3 & 0x3F;
  768. if (!haveByte3) {
  769. outByte4 = 64;
  770. if (!haveByte2) {
  771. outByte3 = 64;
  772. }
  773. }
  774. output.push(
  775. byteToCharMap[outByte1], byteToCharMap[outByte2],
  776. byteToCharMap[outByte3], byteToCharMap[outByte4])
  777. }
  778. return output.join('')
  779. }
  780. var create = Object.create || function (a) {
  781. function c() {}
  782. c.prototype = a;
  783. return new c
  784. }
  785. if (arrayBufferSupported) {
  786. var viewClasses = [
  787. '[object Int8Array]',
  788. '[object Uint8Array]',
  789. '[object Uint8ClampedArray]',
  790. '[object Int16Array]',
  791. '[object Uint16Array]',
  792. '[object Int32Array]',
  793. '[object Uint32Array]',
  794. '[object Float32Array]',
  795. '[object Float64Array]'
  796. ]
  797. var isArrayBufferView = ArrayBuffer.isView || function(obj) {
  798. return obj && viewClasses.indexOf(Object.prototype.toString.call(obj)) > -1
  799. }
  800. }
  801. /********************************************************/
  802. /* Blob constructor */
  803. /********************************************************/
  804. function Blob(chunks, opts) {
  805. chunks = chunks || []
  806. for (var i = 0, len = chunks.length; i < len; i++) {
  807. var chunk = chunks[i]
  808. if (chunk instanceof Blob) {
  809. chunks[i] = chunk._buffer
  810. } else if (typeof chunk === 'string') {
  811. chunks[i] = toUTF8Array(chunk)
  812. } else if (arrayBufferSupported && (ArrayBuffer.prototype.isPrototypeOf(chunk) || isArrayBufferView(chunk))) {
  813. chunks[i] = bufferClone(chunk)
  814. } else if (arrayBufferSupported && isDataView(chunk)) {
  815. chunks[i] = bufferClone(chunk.buffer)
  816. } else {
  817. chunks[i] = toUTF8Array(String(chunk))
  818. }
  819. }
  820. this._buffer = [].concat.apply([], chunks)
  821. this.size = this._buffer.length
  822. this.type = opts ? opts.type || '' : ''
  823. }
  824. Blob.prototype.slice = function(start, end, type) {
  825. var slice = this._buffer.slice(start || 0, end || this._buffer.length)
  826. return new Blob([slice], {type: type})
  827. }
  828. Blob.prototype.toString = function() {
  829. return '[object Blob]'
  830. }
  831. /********************************************************/
  832. /* File constructor */
  833. /********************************************************/
  834. function File(chunks, name, opts) {
  835. opts = opts || {}
  836. var a = Blob.call(this, chunks, opts) || this
  837. a.name = name
  838. a.lastModifiedDate = opts.lastModified ? new Date(opts.lastModified) : new Date
  839. a.lastModified = +a.lastModifiedDate
  840. return a
  841. }
  842. File.prototype = create(Blob.prototype);
  843. File.prototype.constructor = File;
  844. if (Object.setPrototypeOf)
  845. Object.setPrototypeOf(File, Blob);
  846. else {
  847. try {File.__proto__ = Blob} catch (e) {}
  848. }
  849. File.prototype.toString = function() {
  850. return '[object File]'
  851. }
  852. /********************************************************/
  853. /* FileReader constructor */
  854. /********************************************************/
  855. function FileReader() {
  856. if (!(this instanceof FileReader))
  857. throw new TypeError("Failed to construct 'FileReader': Please use the 'new' operator, this DOM object constructor cannot be called as a function.")
  858. var delegate = document.createDocumentFragment()
  859. this.addEventListener = delegate.addEventListener
  860. this.dispatchEvent = function(evt) {
  861. var local = this['on' + evt.type]
  862. if (typeof local === 'function') local(evt)
  863. delegate.dispatchEvent(evt)
  864. }
  865. this.removeEventListener = delegate.removeEventListener
  866. }
  867. function _read(fr, blob, kind) {
  868. if (!(blob instanceof Blob))
  869. throw new TypeError("Failed to execute '" + kind + "' on 'FileReader': parameter 1 is not of type 'Blob'.")
  870. fr.result = ''
  871. setTimeout(function(){
  872. this.readyState = FileReader.LOADING
  873. fr.dispatchEvent(new Event('load'))
  874. fr.dispatchEvent(new Event('loadend'))
  875. })
  876. }
  877. FileReader.EMPTY = 0
  878. FileReader.LOADING = 1
  879. FileReader.DONE = 2
  880. FileReader.prototype.error = null
  881. FileReader.prototype.onabort = null
  882. FileReader.prototype.onerror = null
  883. FileReader.prototype.onload = null
  884. FileReader.prototype.onloadend = null
  885. FileReader.prototype.onloadstart = null
  886. FileReader.prototype.onprogress = null
  887. FileReader.prototype.readAsDataURL = function(blob) {
  888. _read(this, blob, 'readAsDataURL')
  889. this.result = 'data:' + blob.type + ';base64,' + encodeByteArray(blob._buffer)
  890. }
  891. FileReader.prototype.readAsText = function(blob) {
  892. _read(this, blob, 'readAsText')
  893. this.result = fromUtf8Array(blob._buffer)
  894. }
  895. FileReader.prototype.readAsArrayBuffer = function(blob) {
  896. _read(this, blob, 'readAsText')
  897. this.result = blob._buffer.slice()
  898. }
  899. FileReader.prototype.abort = function() {}
  900. /********************************************************/
  901. /* URL */
  902. /********************************************************/
  903. URL.createObjectURL = function(blob) {
  904. return blob instanceof Blob
  905. ? 'data:' + blob.type + ';base64,' + encodeByteArray(blob._buffer)
  906. : createObjectURL.call(URL, blob)
  907. }
  908. URL.revokeObjectURL = function(url) {
  909. revokeObjectURL && revokeObjectURL.call(URL, url)
  910. }
  911. /********************************************************/
  912. /* XHR */
  913. /********************************************************/
  914. var _send = global.XMLHttpRequest && global.XMLHttpRequest.prototype.send
  915. if (_send) {
  916. XMLHttpRequest.prototype.send = function(data) {
  917. if (data instanceof Blob) {
  918. this.setRequestHeader('Content-Type', data.type)
  919. _send.call(this, fromUtf8Array(data._buffer))
  920. } else {
  921. _send.call(this, data)
  922. }
  923. }
  924. }
  925. global.FileReader = FileReader
  926. global.File = File
  927. global.Blob = Blob
  928. }
  929. if (strTag) {
  930. File.prototype[strTag] = 'File'
  931. Blob.prototype[strTag] = 'Blob'
  932. FileReader.prototype[strTag] = 'FileReader'
  933. }
  934. function fixFileAndXHR() {
  935. var isIE = !!global.ActiveXObject || (
  936. '-ms-scroll-limit' in document.documentElement.style &&
  937. '-ms-ime-align' in document.documentElement.style
  938. )
  939. // Monkey patched
  940. // IE don't set Content-Type header on XHR whose body is a typed Blob
  941. // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/6047383
  942. var _send = global.XMLHttpRequest && global.XMLHttpRequest.prototype.send
  943. if (isIE && _send) {
  944. XMLHttpRequest.prototype.send = function(data) {
  945. if (data instanceof Blob) {
  946. this.setRequestHeader('Content-Type', data.type)
  947. _send.call(this, data)
  948. } else {
  949. _send.call(this, data)
  950. }
  951. }
  952. }
  953. try {
  954. new File([], '')
  955. } catch(e) {
  956. try {
  957. var klass = new Function('class File extends Blob {' +
  958. 'constructor(chunks, name, opts) {' +
  959. 'opts = opts || {};' +
  960. 'super(chunks, opts || {});' +
  961. 'this.name = name;' +
  962. 'this.lastModifiedDate = opts.lastModified ? new Date(opts.lastModified) : new Date;' +
  963. 'this.lastModified = +this.lastModifiedDate;' +
  964. '}};' +
  965. 'return new File([], ""), File'
  966. )()
  967. global.File = klass
  968. } catch(e) {
  969. var klass = function(b, d, c) {
  970. var blob = new Blob(b, c)
  971. var t = c && void 0 !== c.lastModified ? new Date(c.lastModified) : new Date
  972. blob.name = d
  973. blob.lastModifiedDate = t
  974. blob.lastModified = +t
  975. blob.toString = function() {
  976. return '[object File]'
  977. }
  978. if (strTag)
  979. blob[strTag] = 'File'
  980. return blob
  981. }
  982. global.File = klass
  983. }
  984. }
  985. }
  986. if (blobSupported) {
  987. fixFileAndXHR()
  988. global.Blob = blobSupportsArrayBufferView ? global.Blob : BlobConstructor
  989. } else if (blobBuilderSupported) {
  990. fixFileAndXHR()
  991. global.Blob = BlobBuilderConstructor;
  992. } else {
  993. FakeBlobBuilder()
  994. }
  995. })();
  996. /*---------split--------*/(function(a,b){if("function"==typeof define&&define.amd)define([],b);else if("undefined"!=typeof exports)b();else{b(),a.FileSaver={exports:{}}.exports}})(this,function(){"use strict";function b(a,b){return"undefined"==typeof b?b={autoBom:!1}:"object"!=typeof b&&(console.warn("Depricated: Expected third argument to be a object"),b={autoBom:!b}),b.autoBom&&/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(a.type)?new Blob(["\uFEFF",a],{type:a.type}):a}function c(b,c,d){var e=new XMLHttpRequest;e.open("GET",b),e.responseType="blob",e.onload=function(){a(e.response,c,d)},e.onerror=function(){console.error("could not download file")},e.send()}function d(a){var b=new XMLHttpRequest;return b.open("HEAD",a,!1),b.send(),200<=b.status&&299>=b.status}function e(a){try{a.dispatchEvent(new MouseEvent("click"))}catch(c){var b=document.createEvent("MouseEvents");b.initMouseEvent("click",!0,!0,window,0,0,0,80,20,!1,!1,!1,!1,0,null),a.dispatchEvent(b)}}var f="object"==typeof window&&window.window===window?window:"object"==typeof self&&self.self===self?self:"object"==typeof global&&global.global===global?global:void 0,a=f.saveAs||"object"!=typeof window||window!==f?function(){}:"download"in HTMLAnchorElement.prototype?function(b,g,h){var i=f.URL||f.webkitURL,j=document.createElement("a");g=g||b.name||"download",j.download=g,j.rel="noopener","string"==typeof b?(j.href=b,j.origin===location.origin?e(j):d(j.href)?c(b,g,h):e(j,j.target="_blank")):(j.href=i.createObjectURL(b),setTimeout(function(){i.revokeObjectURL(j.href)},4E4),setTimeout(function(){e(j)},0))}:"msSaveOrOpenBlob"in navigator?function(f,g,h){if(g=g||f.name||"download","string"!=typeof f)navigator.msSaveOrOpenBlob(b(f,h),g);else if(d(f))c(f,g,h);else{var i=document.createElement("a");i.href=f,i.target="_blank",setTimeout(function(){e(i)})}}:function(a,b,d,e){if(e=e||open("","_blank"),e&&(e.document.title=e.document.body.innerText="downloading..."),"string"==typeof a)return c(a,b,d);var g="application/octet-stream"===a.type,h=/constructor/i.test(f.HTMLElement)||f.safari,i=/CriOS\/[\d]+/.test(navigator.userAgent);if((i||g&&h)&&"object"==typeof FileReader){var j=new FileReader;j.onloadend=function(){var a=j.result;a=i?a:a.replace(/^data:[^;]*;/,"data:attachment/file;"),e?e.location.href=a:location=a,e=null},j.readAsDataURL(a)}else{var k=f.URL||f.webkitURL,l=k.createObjectURL(a);e?e.location=l:location.href=l,e=null,setTimeout(function(){k.revokeObjectURL(l)},4E4)}};f.saveAs=a.saveAs=a,"undefined"!=typeof module&&(module.exports=a)});
  997. /*---------split--------*//*
  998. JSZip - A Javascript class for generating and reading zip files
  999. <http://stuartk.com/jszip>
  1000. (c) 2009-2014 Stuart Knightley <stuart [at] stuartk.com>
  1001. Dual licenced under the MIT license or GPLv3. See https://raw.github.com/Stuk/jszip/master/LICENSE.markdown.
  1002. JSZip uses the library pako released under the MIT license :
  1003. https://github.com/nodeca/pako/blob/master/LICENSE
  1004. Note: since JSZip 3 removed critical functionality, this version assigns to the
  1005. `JSZipSync` variable. Another JSZip version can be loaded in parallel.
  1006. */
  1007. (function(e){
  1008. if("object"==typeof exports&&"undefined"!=typeof module&&"undefined"==typeof DO_NOT_EXPORT_JSZIP)module.exports=e();
  1009. else if("function"==typeof define&&define.amd&&"undefined"==typeof DO_NOT_EXPORT_JSZIP){JSZipSync=e();define([],e);}
  1010. else{
  1011. var f;
  1012. "undefined"!=typeof window?f=window:
  1013. "undefined"!=typeof global?f=global:
  1014. "undefined"!=typeof $ && $.global?f=$.global:
  1015. "undefined"!=typeof self&&(f=self),f.JSZipSync=e()
  1016. }
  1017. }(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
  1018. 'use strict';
  1019. // private property
  1020. var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  1021. // public method for encoding
  1022. exports.encode = function(input, utf8) {
  1023. var output = "";
  1024. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  1025. var i = 0;
  1026. while (i < input.length) {
  1027. chr1 = input.charCodeAt(i++);
  1028. chr2 = input.charCodeAt(i++);
  1029. chr3 = input.charCodeAt(i++);
  1030. enc1 = chr1 >> 2;
  1031. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  1032. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  1033. enc4 = chr3 & 63;
  1034. if (isNaN(chr2)) {
  1035. enc3 = enc4 = 64;
  1036. }
  1037. else if (isNaN(chr3)) {
  1038. enc4 = 64;
  1039. }
  1040. output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
  1041. }
  1042. return output;
  1043. };
  1044. // public method for decoding
  1045. exports.decode = function(input, utf8) {
  1046. var output = "";
  1047. var chr1, chr2, chr3;
  1048. var enc1, enc2, enc3, enc4;
  1049. var i = 0;
  1050. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  1051. while (i < input.length) {
  1052. enc1 = _keyStr.indexOf(input.charAt(i++));
  1053. enc2 = _keyStr.indexOf(input.charAt(i++));
  1054. enc3 = _keyStr.indexOf(input.charAt(i++));
  1055. enc4 = _keyStr.indexOf(input.charAt(i++));
  1056. chr1 = (enc1 << 2) | (enc2 >> 4);
  1057. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  1058. chr3 = ((enc3 & 3) << 6) | enc4;
  1059. output = output + String.fromCharCode(chr1);
  1060. if (enc3 != 64) {
  1061. output = output + String.fromCharCode(chr2);
  1062. }
  1063. if (enc4 != 64) {
  1064. output = output + String.fromCharCode(chr3);
  1065. }
  1066. }
  1067. return output;
  1068. };
  1069. },{}],2:[function(_dereq_,module,exports){
  1070. 'use strict';
  1071. function CompressedObject() {
  1072. this.compressedSize = 0;
  1073. this.uncompressedSize = 0;
  1074. this.crc32 = 0;
  1075. this.compressionMethod = null;
  1076. this.compressedContent = null;
  1077. }
  1078. CompressedObject.prototype = {
  1079. /**
  1080. * Return the decompressed content in an unspecified format.
  1081. * The format will depend on the decompressor.
  1082. * @return {Object} the decompressed content.
  1083. */
  1084. getContent: function() {
  1085. return null; // see implementation
  1086. },
  1087. /**
  1088. * Return the compressed content in an unspecified format.
  1089. * The format will depend on the compressed conten source.
  1090. * @return {Object} the compressed content.
  1091. */
  1092. getCompressedContent: function() {
  1093. return null; // see implementation
  1094. }
  1095. };
  1096. module.exports = CompressedObject;
  1097. },{}],3:[function(_dereq_,module,exports){
  1098. 'use strict';
  1099. exports.STORE = {
  1100. magic: "\x00\x00",
  1101. compress: function(content) {
  1102. return content; // no compression
  1103. },
  1104. uncompress: function(content) {
  1105. return content; // no compression
  1106. },
  1107. compressInputType: null,
  1108. uncompressInputType: null
  1109. };
  1110. exports.DEFLATE = _dereq_('./flate');
  1111. },{"./flate":8}],4:[function(_dereq_,module,exports){
  1112. 'use strict';
  1113. var utils = _dereq_('./utils');
  1114. var table = [
  1115. 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
  1116. 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
  1117. 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
  1118. 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
  1119. 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
  1120. 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
  1121. 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
  1122. 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
  1123. 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
  1124. 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
  1125. 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940,
  1126. 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
  1127. 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116,
  1128. 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
  1129. 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
  1130. 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
  1131. 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A,
  1132. 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
  1133. 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818,
  1134. 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
  1135. 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
  1136. 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
  1137. 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C,
  1138. 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
  1139. 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
  1140. 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
  1141. 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
  1142. 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
  1143. 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086,
  1144. 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
  1145. 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
  1146. 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
  1147. 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
  1148. 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
  1149. 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
  1150. 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
  1151. 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE,
  1152. 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
  1153. 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
  1154. 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
  1155. 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252,
  1156. 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
  1157. 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60,
  1158. 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
  1159. 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
  1160. 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
  1161. 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
  1162. 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
  1163. 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
  1164. 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
  1165. 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
  1166. 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
  1167. 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E,
  1168. 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
  1169. 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
  1170. 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
  1171. 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
  1172. 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
  1173. 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0,
  1174. 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
  1175. 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6,
  1176. 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
  1177. 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
  1178. 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
  1179. ];
  1180. /**
  1181. *
  1182. * Javascript crc32
  1183. * http://www.webtoolkit.info/
  1184. *
  1185. */
  1186. module.exports = function crc32(input, crc) {
  1187. if (typeof input === "undefined" || !input.length) {
  1188. return 0;
  1189. }
  1190. var isArray = utils.getTypeOf(input) !== "string";
  1191. if (typeof(crc) == "undefined") {
  1192. crc = 0;
  1193. }
  1194. var x = 0;
  1195. var y = 0;
  1196. var b = 0;
  1197. crc = crc ^ (-1);
  1198. for (var i = 0, iTop = input.length; i < iTop; i++) {
  1199. b = isArray ? input[i] : input.charCodeAt(i);
  1200. y = (crc ^ b) & 0xFF;
  1201. x = table[y];
  1202. crc = (crc >>> 8) ^ x;
  1203. }
  1204. return crc ^ (-1);
  1205. };
  1206. // vim: set shiftwidth=4 softtabstop=4:
  1207. },{"./utils":21}],5:[function(_dereq_,module,exports){
  1208. 'use strict';
  1209. var utils = _dereq_('./utils');
  1210. function DataReader(data) {
  1211. this.data = null; // type : see implementation
  1212. this.length = 0;
  1213. this.index = 0;
  1214. }
  1215. DataReader.prototype = {
  1216. /**
  1217. * Check that the offset will not go too far.
  1218. * @param {string} offset the additional offset to check.
  1219. * @throws {Error} an Error if the offset is out of bounds.
  1220. */
  1221. checkOffset: function(offset) {
  1222. this.checkIndex(this.index + offset);
  1223. },
  1224. /**
  1225. * Check that the specifed index will not be too far.
  1226. * @param {string} newIndex the index to check.
  1227. * @throws {Error} an Error if the index is out of bounds.
  1228. */
  1229. checkIndex: function(newIndex) {
  1230. if (this.length < newIndex || newIndex < 0) {
  1231. throw new Error("End of data reached (data length = " + this.length + ", asked index = " + (newIndex) + "). Corrupted zip ?");
  1232. }
  1233. },
  1234. /**
  1235. * Change the index.
  1236. * @param {number} newIndex The new index.
  1237. * @throws {Error} if the new index is out of the data.
  1238. */
  1239. setIndex: function(newIndex) {
  1240. this.checkIndex(newIndex);
  1241. this.index = newIndex;
  1242. },
  1243. /**
  1244. * Skip the next n bytes.
  1245. * @param {number} n the number of bytes to skip.
  1246. * @throws {Error} if the new index is out of the data.
  1247. */
  1248. skip: function(n) {
  1249. this.setIndex(this.index + n);
  1250. },
  1251. /**
  1252. * Get the byte at the specified index.
  1253. * @param {number} i the index to use.
  1254. * @return {number} a byte.
  1255. */
  1256. byteAt: function(i) {
  1257. // see implementations
  1258. },
  1259. /**
  1260. * Get the next number with a given byte size.
  1261. * @param {number} size the number of bytes to read.
  1262. * @return {number} the corresponding number.
  1263. */
  1264. readInt: function(size) {
  1265. var result = 0,
  1266. i;
  1267. this.checkOffset(size);
  1268. for (i = this.index + size - 1; i >= this.index; i--) {
  1269. result = (result << 8) + this.byteAt(i);
  1270. }
  1271. this.index += size;
  1272. return result;
  1273. },
  1274. /**
  1275. * Get the next string with a given byte size.
  1276. * @param {number} size the number of bytes to read.
  1277. * @return {string} the corresponding string.
  1278. */
  1279. readString: function(size) {
  1280. return utils.transformTo("string", this.readData(size));
  1281. },
  1282. /**
  1283. * Get raw data without conversion, <size> bytes.
  1284. * @param {number} size the number of bytes to read.
  1285. * @return {Object} the raw data, implementation specific.
  1286. */
  1287. readData: function(size) {
  1288. // see implementations
  1289. },
  1290. /**
  1291. * Find the last occurence of a zip signature (4 bytes).
  1292. * @param {string} sig the signature to find.
  1293. * @return {number} the index of the last occurence, -1 if not found.
  1294. */
  1295. lastIndexOfSignature: function(sig) {
  1296. // see implementations
  1297. },
  1298. /**
  1299. * Get the next date.
  1300. * @return {Date} the date.
  1301. */
  1302. readDate: function() {
  1303. var dostime = this.readInt(4);
  1304. return new Date(
  1305. ((dostime >> 25) & 0x7f) + 1980, // year
  1306. ((dostime >> 21) & 0x0f) - 1, // month
  1307. (dostime >> 16) & 0x1f, // day
  1308. (dostime >> 11) & 0x1f, // hour
  1309. (dostime >> 5) & 0x3f, // minute
  1310. (dostime & 0x1f) << 1); // second
  1311. }
  1312. };
  1313. module.exports = DataReader;
  1314. },{"./utils":21}],6:[function(_dereq_,module,exports){
  1315. 'use strict';
  1316. exports.base64 = false;
  1317. exports.binary = false;
  1318. exports.dir = false;
  1319. exports.createFolders = false;
  1320. exports.date = null;
  1321. exports.compression = null;
  1322. exports.comment = null;
  1323. },{}],7:[function(_dereq_,module,exports){
  1324. 'use strict';
  1325. var utils = _dereq_('./utils');
  1326. /**
  1327. * @deprecated
  1328. * This function will be removed in a future version without replacement.
  1329. */
  1330. exports.string2binary = function(str) {
  1331. return utils.string2binary(str);
  1332. };
  1333. /**
  1334. * @deprecated
  1335. * This function will be removed in a future version without replacement.
  1336. */
  1337. exports.string2Uint8Array = function(str) {
  1338. return utils.transformTo("uint8array", str);
  1339. };
  1340. /**
  1341. * @deprecated
  1342. * This function will be removed in a future version without replacement.
  1343. */
  1344. exports.uint8Array2String = function(array) {
  1345. return utils.transformTo("string", array);
  1346. };
  1347. /**
  1348. * @deprecated
  1349. * This function will be removed in a future version without replacement.
  1350. */
  1351. exports.string2Blob = function(str) {
  1352. var buffer = utils.transformTo("arraybuffer", str);
  1353. return utils.arrayBuffer2Blob(buffer);
  1354. };
  1355. /**
  1356. * @deprecated
  1357. * This function will be removed in a future version without replacement.
  1358. */
  1359. exports.arrayBuffer2Blob = function(buffer) {
  1360. return utils.arrayBuffer2Blob(buffer);
  1361. };
  1362. /**
  1363. * @deprecated
  1364. * This function will be removed in a future version without replacement.
  1365. */
  1366. exports.transformTo = function(outputType, input) {
  1367. return utils.transformTo(outputType, input);
  1368. };
  1369. /**
  1370. * @deprecated
  1371. * This function will be removed in a future version without replacement.
  1372. */
  1373. exports.getTypeOf = function(input) {
  1374. return utils.getTypeOf(input);
  1375. };
  1376. /**
  1377. * @deprecated
  1378. * This function will be removed in a future version without replacement.
  1379. */
  1380. exports.checkSupport = function(type) {
  1381. return utils.checkSupport(type);
  1382. };
  1383. /**
  1384. * @deprecated
  1385. * This value will be removed in a future version without replacement.
  1386. */
  1387. exports.MAX_VALUE_16BITS = utils.MAX_VALUE_16BITS;
  1388. /**
  1389. * @deprecated
  1390. * This value will be removed in a future version without replacement.
  1391. */
  1392. exports.MAX_VALUE_32BITS = utils.MAX_VALUE_32BITS;
  1393. /**
  1394. * @deprecated
  1395. * This function will be removed in a future version without replacement.
  1396. */
  1397. exports.pretty = function(str) {
  1398. return utils.pretty(str);
  1399. };
  1400. /**
  1401. * @deprecated
  1402. * This function will be removed in a future version without replacement.
  1403. */
  1404. exports.findCompression = function(compressionMethod) {
  1405. return utils.findCompression(compressionMethod);
  1406. };
  1407. /**
  1408. * @deprecated
  1409. * This function will be removed in a future version without replacement.
  1410. */
  1411. exports.isRegExp = function (object) {
  1412. return utils.isRegExp(object);
  1413. };
  1414. },{"./utils":21}],8:[function(_dereq_,module,exports){
  1415. 'use strict';
  1416. var USE_TYPEDARRAY = (typeof Uint8Array !== 'undefined') && (typeof Uint16Array !== 'undefined') && (typeof Uint32Array !== 'undefined');
  1417. var pako = _dereq_("pako");
  1418. exports.uncompressInputType = USE_TYPEDARRAY ? "uint8array" : "array";
  1419. exports.compressInputType = USE_TYPEDARRAY ? "uint8array" : "array";
  1420. exports.magic = "\x08\x00";
  1421. exports.compress = function(input) {
  1422. return pako.deflateRaw(input);
  1423. };
  1424. exports.uncompress = function(input) {
  1425. return pako.inflateRaw(input);
  1426. };
  1427. },{"pako":24}],9:[function(_dereq_,module,exports){
  1428. 'use strict';
  1429. var base64 = _dereq_('./base64');
  1430. /**
  1431. Usage:
  1432. zip = new JSZip();
  1433. zip.file("hello.txt", "Hello, World!").file("tempfile", "nothing");
  1434. zip.folder("images").file("smile.gif", base64Data, {base64: true});
  1435. zip.file("Xmas.txt", "Ho ho ho !", {date : new Date("December 25, 2007 00:00:01")});
  1436. zip.remove("tempfile");
  1437. base64zip = zip.generate();
  1438. **/
  1439. /**
  1440. * Representation a of zip file in js
  1441. * @constructor
  1442. * @param {String=|ArrayBuffer=|Uint8Array=} data the data to load, if any (optional).
  1443. * @param {Object=} options the options for creating this objects (optional).
  1444. */
  1445. function JSZipSync(data, options) {
  1446. // if this constructor is used without `new`, it adds `new` before itself:
  1447. if(!(this instanceof JSZipSync)) return new JSZipSync(data, options);
  1448. // object containing the files :
  1449. // {
  1450. // "folder/" : {...},
  1451. // "folder/data.txt" : {...}
  1452. // }
  1453. this.files = {};
  1454. this.comment = null;
  1455. // Where we are in the hierarchy
  1456. this.root = "";
  1457. if (data) {
  1458. this.load(data, options);
  1459. }
  1460. this.clone = function() {
  1461. var newObj = new JSZipSync();
  1462. for (var i in this) {
  1463. if (typeof this[i] !== "function") {
  1464. newObj[i] = this[i];
  1465. }
  1466. }
  1467. return newObj;
  1468. };
  1469. }
  1470. JSZipSync.prototype = _dereq_('./object');
  1471. JSZipSync.prototype.load = _dereq_('./load');
  1472. JSZipSync.support = _dereq_('./support');
  1473. JSZipSync.defaults = _dereq_('./defaults');
  1474. /**
  1475. * @deprecated
  1476. * This namespace will be removed in a future version without replacement.
  1477. */
  1478. JSZipSync.utils = _dereq_('./deprecatedPublicUtils');
  1479. JSZipSync.base64 = {
  1480. /**
  1481. * @deprecated
  1482. * This method will be removed in a future version without replacement.
  1483. */
  1484. encode : function(input) {
  1485. return base64.encode(input);
  1486. },
  1487. /**
  1488. * @deprecated
  1489. * This method will be removed in a future version without replacement.
  1490. */
  1491. decode : function(input) {
  1492. return base64.decode(input);
  1493. }
  1494. };
  1495. JSZipSync.compressions = _dereq_('./compressions');
  1496. module.exports = JSZipSync;
  1497. },{"./base64":1,"./compressions":3,"./defaults":6,"./deprecatedPublicUtils":7,"./load":10,"./object":13,"./support":17}],10:[function(_dereq_,module,exports){
  1498. 'use strict';
  1499. var base64 = _dereq_('./base64');
  1500. var ZipEntries = _dereq_('./zipEntries');
  1501. module.exports = function(data, options) {
  1502. var files, zipEntries, i, input;
  1503. options = options || {};
  1504. if (options.base64) {
  1505. data = base64.decode(data);
  1506. }
  1507. zipEntries = new ZipEntries(data, options);
  1508. files = zipEntries.files;
  1509. for (i = 0; i < files.length; i++) {
  1510. input = files[i];
  1511. this.file(input.fileName, input.decompressed, {
  1512. binary: true,
  1513. optimizedBinaryString: true,
  1514. date: input.date,
  1515. dir: input.dir,
  1516. comment : input.fileComment.length ? input.fileComment : null,
  1517. createFolders: options.createFolders
  1518. });
  1519. }
  1520. if (zipEntries.zipComment.length) {
  1521. this.comment = zipEntries.zipComment;
  1522. }
  1523. return this;
  1524. };
  1525. },{"./base64":1,"./zipEntries":22}],11:[function(_dereq_,module,exports){
  1526. (function (Buffer){
  1527. 'use strict';
  1528. var Buffer_from = /*::(*/function(){}/*:: :any)*/;
  1529. if(typeof Buffer !== 'undefined') {
  1530. var nbfs = !Buffer.from;
  1531. if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; }
  1532. Buffer_from = nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer);
  1533. // $FlowIgnore
  1534. if(!Buffer.alloc) Buffer.alloc = function(n) { return new Buffer(n); };
  1535. }
  1536. module.exports = function(data, encoding){
  1537. return typeof data == 'number' ? Buffer.alloc(data) : Buffer_from(data, encoding);
  1538. };
  1539. module.exports.test = function(b){
  1540. return Buffer.isBuffer(b);
  1541. };
  1542. }).call(this,(typeof Buffer !== "undefined" ? Buffer : undefined))
  1543. },{}],12:[function(_dereq_,module,exports){
  1544. 'use strict';
  1545. var Uint8ArrayReader = _dereq_('./uint8ArrayReader');
  1546. function NodeBufferReader(data) {
  1547. this.data = data;
  1548. this.length = this.data.length;
  1549. this.index = 0;
  1550. }
  1551. NodeBufferReader.prototype = new Uint8ArrayReader();
  1552. /**
  1553. * @see DataReader.readData
  1554. */
  1555. NodeBufferReader.prototype.readData = function(size) {
  1556. this.checkOffset(size);
  1557. var result = this.data.slice(this.index, this.index + size);
  1558. this.index += size;
  1559. return result;
  1560. };
  1561. module.exports = NodeBufferReader;
  1562. },{"./uint8ArrayReader":18}],13:[function(_dereq_,module,exports){
  1563. 'use strict';
  1564. var support = _dereq_('./support');
  1565. var utils = _dereq_('./utils');
  1566. var crc32 = _dereq_('./crc32');
  1567. var signature = _dereq_('./signature');
  1568. var defaults = _dereq_('./defaults');
  1569. var base64 = _dereq_('./base64');
  1570. var compressions = _dereq_('./compressions');
  1571. var CompressedObject = _dereq_('./compressedObject');
  1572. var nodeBuffer = _dereq_('./nodeBuffer');
  1573. var utf8 = _dereq_('./utf8');
  1574. var StringWriter = _dereq_('./stringWriter');
  1575. var Uint8ArrayWriter = _dereq_('./uint8ArrayWriter');
  1576. /**
  1577. * Returns the raw data of a ZipObject, decompress the content if necessary.
  1578. * @param {ZipObject} file the file to use.
  1579. * @return {String|ArrayBuffer|Uint8Array|Buffer} the data.
  1580. */
  1581. var getRawData = function(file) {
  1582. if (file._data instanceof CompressedObject) {
  1583. file._data = file._data.getContent();
  1584. file.options.binary = true;
  1585. file.options.base64 = false;
  1586. if (utils.getTypeOf(file._data) === "uint8array") {
  1587. var copy = file._data;
  1588. // when reading an arraybuffer, the CompressedObject mechanism will keep it and subarray() a Uint8Array.
  1589. // if we request a file in the same format, we might get the same Uint8Array or its ArrayBuffer (the original zip file).
  1590. file._data = new Uint8Array(copy.length);
  1591. // with an empty Uint8Array, Opera fails with a "Offset larger than array size"
  1592. if (copy.length !== 0) {
  1593. file._data.set(copy, 0);
  1594. }
  1595. }
  1596. }
  1597. return file._data;
  1598. };
  1599. /**
  1600. * Returns the data of a ZipObject in a binary form. If the content is an unicode string, encode it.
  1601. * @param {ZipObject} file the file to use.
  1602. * @return {String|ArrayBuffer|Uint8Array|Buffer} the data.
  1603. */
  1604. var getBinaryData = function(file) {
  1605. var result = getRawData(file),
  1606. type = utils.getTypeOf(result);
  1607. if (type === "string") {
  1608. if (!file.options.binary) {
  1609. // unicode text !
  1610. // unicode string => binary string is a painful process, check if we can avoid it.
  1611. if (support.nodebuffer) {
  1612. return nodeBuffer(result, "utf-8");
  1613. }
  1614. }
  1615. return file.asBinary();
  1616. }
  1617. return result;
  1618. };
  1619. /**
  1620. * Transform this._data into a string.
  1621. * @param {function} filter a function String -> String, applied if not null on the result.
  1622. * @return {String} the string representing this._data.
  1623. */
  1624. var dataToString = function(asUTF8) {
  1625. var result = getRawData(this);
  1626. if (result === null || typeof result === "undefined") {
  1627. return "";
  1628. }
  1629. // if the data is a base64 string, we decode it before checking the encoding !
  1630. if (this.options.base64) {
  1631. result = base64.decode(result);
  1632. }
  1633. if (asUTF8 && this.options.binary) {
  1634. // JSZip.prototype.utf8decode supports arrays as input
  1635. // skip to array => string step, utf8decode will do it.
  1636. result = out.utf8decode(result);
  1637. }
  1638. else {
  1639. // no utf8 transformation, do the array => string step.
  1640. result = utils.transformTo("string", result);
  1641. }
  1642. if (!asUTF8 && !this.options.binary) {
  1643. result = utils.transformTo("string", out.utf8encode(result));
  1644. }
  1645. return result;
  1646. };
  1647. /**
  1648. * A simple object representing a file in the zip file.
  1649. * @constructor
  1650. * @param {string} name the name of the file
  1651. * @param {String|ArrayBuffer|Uint8Array|Buffer} data the data
  1652. * @param {Object} options the options of the file
  1653. */
  1654. var ZipObject = function(name, data, options) {
  1655. this.name = name;
  1656. this.dir = options.dir;
  1657. this.date = options.date;
  1658. this.comment = options.comment;
  1659. this._data = data;
  1660. this.options = options;
  1661. /*
  1662. * This object contains initial values for dir and date.
  1663. * With them, we can check if the user changed the deprecated metadata in
  1664. * `ZipObject#options` or not.
  1665. */
  1666. this._initialMetadata = {
  1667. dir : options.dir,
  1668. date : options.date
  1669. };
  1670. };
  1671. ZipObject.prototype = {
  1672. /**
  1673. * Return the content as UTF8 string.
  1674. * @return {string} the UTF8 string.
  1675. */
  1676. asText: function() {
  1677. return dataToString.call(this, true);
  1678. },
  1679. /**
  1680. * Returns the binary content.
  1681. * @return {string} the content as binary.
  1682. */
  1683. asBinary: function() {
  1684. return dataToString.call(this, false);
  1685. },
  1686. /**
  1687. * Returns the content as a nodejs Buffer.
  1688. * @return {Buffer} the content as a Buffer.
  1689. */
  1690. asNodeBuffer: function() {
  1691. var result = getBinaryData(this);
  1692. return utils.transformTo("nodebuffer", result);
  1693. },
  1694. /**
  1695. * Returns the content as an Uint8Array.
  1696. * @return {Uint8Array} the content as an Uint8Array.
  1697. */
  1698. asUint8Array: function() {
  1699. var result = getBinaryData(this);
  1700. return utils.transformTo("uint8array", result);
  1701. },
  1702. /**
  1703. * Returns the content as an ArrayBuffer.
  1704. * @return {ArrayBuffer} the content as an ArrayBufer.
  1705. */
  1706. asArrayBuffer: function() {
  1707. return this.asUint8Array().buffer;
  1708. }
  1709. };
  1710. /**
  1711. * Transform an integer into a string in hexadecimal.
  1712. * @private
  1713. * @param {number} dec the number to convert.
  1714. * @param {number} bytes the number of bytes to generate.
  1715. * @returns {string} the result.
  1716. */
  1717. var decToHex = function(dec, bytes) {
  1718. var hex = "",
  1719. i;
  1720. for (i = 0; i < bytes; i++) {
  1721. hex += String.fromCharCode(dec & 0xff);
  1722. dec = dec >>> 8;
  1723. }
  1724. return hex;
  1725. };
  1726. /**
  1727. * Merge the objects passed as parameters into a new one.
  1728. * @private
  1729. * @param {...Object} var_args All objects to merge.
  1730. * @return {Object} a new object with the data of the others.
  1731. */
  1732. var extend = function() {
  1733. var result = {}, i, attr;
  1734. for (i = 0; i < arguments.length; i++) { // arguments is not enumerable in some browsers
  1735. for (attr in arguments[i]) {
  1736. if (arguments[i].hasOwnProperty(attr) && typeof result[attr] === "undefined") {
  1737. result[attr] = arguments[i][attr];
  1738. }
  1739. }
  1740. }
  1741. return result;
  1742. };
  1743. /**
  1744. * Transforms the (incomplete) options from the user into the complete
  1745. * set of options to create a file.
  1746. * @private
  1747. * @param {Object} o the options from the user.
  1748. * @return {Object} the complete set of options.
  1749. */
  1750. var prepareFileAttrs = function(o) {
  1751. o = o || {};
  1752. if (o.base64 === true && (o.binary === null || o.binary === undefined)) {
  1753. o.binary = true;
  1754. }
  1755. o = extend(o, defaults);
  1756. o.date = o.date || new Date();
  1757. if (o.compression !== null) o.compression = o.compression.toUpperCase();
  1758. return o;
  1759. };
  1760. /**
  1761. * Add a file in the current folder.
  1762. * @private
  1763. * @param {string} name the name of the file
  1764. * @param {String|ArrayBuffer|Uint8Array|Buffer} data the data of the file
  1765. * @param {Object} o the options of the file
  1766. * @return {Object} the new file.
  1767. */
  1768. var fileAdd = function(name, data, o) {
  1769. // be sure sub folders exist
  1770. var dataType = utils.getTypeOf(data),
  1771. parent;
  1772. o = prepareFileAttrs(o);
  1773. if (o.createFolders && (parent = parentFolder(name))) {
  1774. folderAdd.call(this, parent, true);
  1775. }
  1776. if (o.dir || data === null || typeof data === "undefined") {
  1777. o.base64 = false;
  1778. o.binary = false;
  1779. data = null;
  1780. }
  1781. else if (dataType === "string") {
  1782. if (o.binary && !o.base64) {
  1783. // optimizedBinaryString == true means that the file has already been filtered with a 0xFF mask
  1784. if (o.optimizedBinaryString !== true) {
  1785. // this is a string, not in a base64 format.
  1786. // Be sure that this is a correct "binary string"
  1787. data = utils.string2binary(data);
  1788. }
  1789. }
  1790. }
  1791. else { // arraybuffer, uint8array, ...
  1792. o.base64 = false;
  1793. o.binary = true;
  1794. if (!dataType && !(data instanceof CompressedObject)) {
  1795. throw new Error("The data of '" + name + "' is in an unsupported format !");
  1796. }
  1797. // special case : it's way easier to work with Uint8Array than with ArrayBuffer
  1798. if (dataType === "arraybuffer") {
  1799. data = utils.transformTo("uint8array", data);
  1800. }
  1801. }
  1802. var object = new ZipObject(name, data, o);
  1803. this.files[name] = object;
  1804. return object;
  1805. };
  1806. /**
  1807. * Find the parent folder of the path.
  1808. * @private
  1809. * @param {string} path the path to use
  1810. * @return {string} the parent folder, or ""
  1811. */
  1812. var parentFolder = function (path) {
  1813. if (path.slice(-1) == '/') {
  1814. path = path.substring(0, path.length - 1);
  1815. }
  1816. var lastSlash = path.lastIndexOf('/');
  1817. return (lastSlash > 0) ? path.substring(0, lastSlash) : "";
  1818. };
  1819. /**
  1820. * Add a (sub) folder in the current folder.
  1821. * @private
  1822. * @param {string} name the folder's name
  1823. * @param {boolean=} [createFolders] If true, automatically create sub
  1824. * folders. Defaults to false.
  1825. * @return {Object} the new folder.
  1826. */
  1827. var folderAdd = function(name, createFolders) {
  1828. // Check the name ends with a /
  1829. if (name.slice(-1) != "/") {
  1830. name += "/"; // IE doesn't like substr(-1)
  1831. }
  1832. createFolders = (typeof createFolders !== 'undefined') ? createFolders : false;
  1833. // Does this folder already exist?
  1834. if (!this.files[name]) {
  1835. fileAdd.call(this, name, null, {
  1836. dir: true,
  1837. createFolders: createFolders
  1838. });
  1839. }
  1840. return this.files[name];
  1841. };
  1842. /**
  1843. * Generate a JSZip.CompressedObject for a given zipOject.
  1844. * @param {ZipObject} file the object to read.
  1845. * @param {JSZip.compression} compression the compression to use.
  1846. * @return {JSZip.CompressedObject} the compressed result.
  1847. */
  1848. var generateCompressedObjectFrom = function(file, compression) {
  1849. var result = new CompressedObject(),
  1850. content;
  1851. // the data has not been decompressed, we might reuse things !
  1852. if (file._data instanceof CompressedObject) {
  1853. result.uncompressedSize = file._data.uncompressedSize;
  1854. result.crc32 = file._data.crc32;
  1855. if (result.uncompressedSize === 0 || file.dir) {
  1856. compression = compressions['STORE'];
  1857. result.compressedContent = "";
  1858. result.crc32 = 0;
  1859. }
  1860. else if (file._data.compressionMethod === compression.magic) {
  1861. result.compressedContent = file._data.getCompressedContent();
  1862. }
  1863. else {
  1864. content = file._data.getContent();
  1865. // need to decompress / recompress
  1866. result.compressedContent = compression.compress(utils.transformTo(compression.compressInputType, content));
  1867. }
  1868. }
  1869. else {
  1870. // have uncompressed data
  1871. content = getBinaryData(file);
  1872. if (!content || content.length === 0 || file.dir) {
  1873. compression = compressions['STORE'];
  1874. content = "";
  1875. }
  1876. result.uncompressedSize = content.length;
  1877. result.crc32 = crc32(content);
  1878. result.compressedContent = compression.compress(utils.transformTo(compression.compressInputType, content));
  1879. }
  1880. result.compressedSize = result.compressedContent.length;
  1881. result.compressionMethod = compression.magic;
  1882. return result;
  1883. };
  1884. /**
  1885. * Generate the various parts used in the construction of the final zip file.
  1886. * @param {string} name the file name.
  1887. * @param {ZipObject} file the file content.
  1888. * @param {JSZip.CompressedObject} compressedObject the compressed object.
  1889. * @param {number} offset the current offset from the start of the zip file.
  1890. * @return {object} the zip parts.
  1891. */
  1892. var generateZipParts = function(name, file, compressedObject, offset) {
  1893. var data = compressedObject.compressedContent,
  1894. utfEncodedFileName = utils.transformTo("string", utf8.utf8encode(file.name)),
  1895. comment = file.comment || "",
  1896. utfEncodedComment = utils.transformTo("string", utf8.utf8encode(comment)),
  1897. useUTF8ForFileName = utfEncodedFileName.length !== file.name.length,
  1898. useUTF8ForComment = utfEncodedComment.length !== comment.length,
  1899. o = file.options,
  1900. dosTime,
  1901. dosDate,
  1902. extraFields = "",
  1903. unicodePathExtraField = "",
  1904. unicodeCommentExtraField = "",
  1905. dir, date;
  1906. // handle the deprecated options.dir
  1907. if (file._initialMetadata.dir !== file.dir) {
  1908. dir = file.dir;
  1909. } else {
  1910. dir = o.dir;
  1911. }
  1912. // handle the deprecated options.date
  1913. if(file._initialMetadata.date !== file.date) {
  1914. date = file.date;
  1915. } else {
  1916. date = o.date;
  1917. }
  1918. dosTime = date.getHours();
  1919. dosTime = dosTime << 6;
  1920. dosTime = dosTime | date.getMinutes();
  1921. dosTime = dosTime << 5;
  1922. dosTime = dosTime | date.getSeconds() / 2;
  1923. dosDate = date.getFullYear() - 1980;
  1924. dosDate = dosDate << 4;
  1925. dosDate = dosDate | (date.getMonth() + 1);
  1926. dosDate = dosDate << 5;
  1927. dosDate = dosDate | date.getDate();
  1928. if (useUTF8ForFileName) {
  1929. // set the unicode path extra field. unzip needs at least one extra
  1930. // field to correctly handle unicode path, so using the path is as good
  1931. // as any other information. This could improve the situation with
  1932. // other archive managers too.
  1933. // This field is usually used without the utf8 flag, with a non
  1934. // unicode path in the header (winrar, winzip). This helps (a bit)
  1935. // with the messy Windows' default compressed folders feature but
  1936. // breaks on p7zip which doesn't seek the unicode path extra field.
  1937. // So for now, UTF-8 everywhere !
  1938. unicodePathExtraField =
  1939. // Version
  1940. decToHex(1, 1) +
  1941. // NameCRC32
  1942. decToHex(crc32(utfEncodedFileName), 4) +
  1943. // UnicodeName
  1944. utfEncodedFileName;
  1945. extraFields +=
  1946. // Info-ZIP Unicode Path Extra Field
  1947. "\x75\x70" +
  1948. // size
  1949. decToHex(unicodePathExtraField.length, 2) +
  1950. // content
  1951. unicodePathExtraField;
  1952. }
  1953. if(useUTF8ForComment) {
  1954. unicodeCommentExtraField =
  1955. // Version
  1956. decToHex(1, 1) +
  1957. // CommentCRC32
  1958. decToHex(this.crc32(utfEncodedComment), 4) +
  1959. // UnicodeName
  1960. utfEncodedComment;
  1961. extraFields +=
  1962. // Info-ZIP Unicode Path Extra Field
  1963. "\x75\x63" +
  1964. // size
  1965. decToHex(unicodeCommentExtraField.length, 2) +
  1966. // content
  1967. unicodeCommentExtraField;
  1968. }
  1969. var header = "";
  1970. // version needed to extract
  1971. header += "\x0A\x00";
  1972. // general purpose bit flag
  1973. // set bit 11 if utf8
  1974. header += (useUTF8ForFileName || useUTF8ForComment) ? "\x00\x08" : "\x00\x00";
  1975. // compression method
  1976. header += compressedObject.compressionMethod;
  1977. // last mod file time
  1978. header += decToHex(dosTime, 2);
  1979. // last mod file date
  1980. header += decToHex(dosDate, 2);
  1981. // crc-32
  1982. header += decToHex(compressedObject.crc32, 4);
  1983. // compressed size
  1984. header += decToHex(compressedObject.compressedSize, 4);
  1985. // uncompressed size
  1986. header += decToHex(compressedObject.uncompressedSize, 4);
  1987. // file name length
  1988. header += decToHex(utfEncodedFileName.length, 2);
  1989. // extra field length
  1990. header += decToHex(extraFields.length, 2);
  1991. var fileRecord = signature.LOCAL_FILE_HEADER + header + utfEncodedFileName + extraFields;
  1992. var dirRecord = signature.CENTRAL_FILE_HEADER +
  1993. // version made by (00: DOS)
  1994. "\x14\x00" +
  1995. // file header (common to file and central directory)
  1996. header +
  1997. // file comment length
  1998. decToHex(utfEncodedComment.length, 2) +
  1999. // disk number start
  2000. "\x00\x00" +
  2001. // internal file attributes TODO
  2002. "\x00\x00" +
  2003. // external file attributes
  2004. (dir === true ? "\x10\x00\x00\x00" : "\x00\x00\x00\x00") +
  2005. // relative offset of local header
  2006. decToHex(offset, 4) +
  2007. // file name
  2008. utfEncodedFileName +
  2009. // extra field
  2010. extraFields +
  2011. // file comment
  2012. utfEncodedComment;
  2013. return {
  2014. fileRecord: fileRecord,
  2015. dirRecord: dirRecord,
  2016. compressedObject: compressedObject
  2017. };
  2018. };
  2019. // return the actual prototype of JSZip
  2020. var out = {
  2021. /**
  2022. * Read an existing zip and merge the data in the current JSZip object.
  2023. * The implementation is in jszip-load.js, don't forget to include it.
  2024. * @param {String|ArrayBuffer|Uint8Array|Buffer} stream The stream to load
  2025. * @param {Object} options Options for loading the stream.
  2026. * options.base64 : is the stream in base64 ? default : false
  2027. * @return {JSZip} the current JSZip object
  2028. */
  2029. load: function(stream, options) {
  2030. throw new Error("Load method is not defined. Is the file jszip-load.js included ?");
  2031. },
  2032. /**
  2033. * Filter nested files/folders with the specified function.
  2034. * @param {Function} search the predicate to use :
  2035. * function (relativePath, file) {...}
  2036. * It takes 2 arguments : the relative path and the file.
  2037. * @return {Array} An array of matching elements.
  2038. */
  2039. filter: function(search) {
  2040. var result = [],
  2041. filename, relativePath, file, fileClone;
  2042. for (filename in this.files) {
  2043. if (!this.files.hasOwnProperty(filename)) {
  2044. continue;
  2045. }
  2046. file = this.files[filename];
  2047. // return a new object, don't let the user mess with our internal objects :)
  2048. fileClone = new ZipObject(file.name, file._data, extend(file.options));
  2049. relativePath = filename.slice(this.root.length, filename.length);
  2050. if (filename.slice(0, this.root.length) === this.root && // the file is in the current root
  2051. search(relativePath, fileClone)) { // and the file matches the function
  2052. result.push(fileClone);
  2053. }
  2054. }
  2055. return result;
  2056. },
  2057. /**
  2058. * Add a file to the zip file, or search a file.
  2059. * @param {string|RegExp} name The name of the file to add (if data is defined),
  2060. * the name of the file to find (if no data) or a regex to match files.
  2061. * @param {String|ArrayBuffer|Uint8Array|Buffer} data The file data, either raw or base64 encoded
  2062. * @param {Object} o File options
  2063. * @return {JSZip|Object|Array} this JSZip object (when adding a file),
  2064. * a file (when searching by string) or an array of files (when searching by regex).
  2065. */
  2066. file: function(name, data, o) {
  2067. if (arguments.length === 1) {
  2068. if (utils.isRegExp(name)) {
  2069. var regexp = name;
  2070. return this.filter(function(relativePath, file) {
  2071. return !file.dir && regexp.test(relativePath);
  2072. });
  2073. }
  2074. else { // text
  2075. return this.filter(function(relativePath, file) {
  2076. return !file.dir && relativePath === name;
  2077. })[0] || null;
  2078. }
  2079. }
  2080. else { // more than one argument : we have data !
  2081. name = this.root + name;
  2082. fileAdd.call(this, name, data, o);
  2083. }
  2084. return this;
  2085. },
  2086. /**
  2087. * Add a directory to the zip file, or search.
  2088. * @param {String|RegExp} arg The name of the directory to add, or a regex to search folders.
  2089. * @return {JSZip} an object with the new directory as the root, or an array containing matching folders.
  2090. */
  2091. folder: function(arg) {
  2092. if (!arg) {
  2093. return this;
  2094. }
  2095. if (utils.isRegExp(arg)) {
  2096. return this.filter(function(relativePath, file) {
  2097. return file.dir && arg.test(relativePath);
  2098. });
  2099. }
  2100. // else, name is a new folder
  2101. var name = this.root + arg;
  2102. var newFolder = folderAdd.call(this, name);
  2103. // Allow chaining by returning a new object with this folder as the root
  2104. var ret = this.clone();
  2105. ret.root = newFolder.name;
  2106. return ret;
  2107. },
  2108. /**
  2109. * Delete a file, or a directory and all sub-files, from the zip
  2110. * @param {string} name the name of the file to delete
  2111. * @return {JSZip} this JSZip object
  2112. */
  2113. remove: function(name) {
  2114. name = this.root + name;
  2115. var file = this.files[name];
  2116. if (!file) {
  2117. // Look for any folders
  2118. if (name.slice(-1) != "/") {
  2119. name += "/";
  2120. }
  2121. file = this.files[name];
  2122. }
  2123. if (file && !file.dir) {
  2124. // file
  2125. delete this.files[name];
  2126. } else {
  2127. // maybe a folder, delete recursively
  2128. var kids = this.filter(function(relativePath, file) {
  2129. return file.name.slice(0, name.length) === name;
  2130. });
  2131. for (var i = 0; i < kids.length; i++) {
  2132. delete this.files[kids[i].name];
  2133. }
  2134. }
  2135. return this;
  2136. },
  2137. /**
  2138. * Generate the complete zip file
  2139. * @param {Object} options the options to generate the zip file :
  2140. * - base64, (deprecated, use type instead) true to generate base64.
  2141. * - compression, "STORE" by default.
  2142. * - type, "base64" by default. Values are : string, base64, uint8array, arraybuffer, blob.
  2143. * @return {String|Uint8Array|ArrayBuffer|Buffer|Blob} the zip file
  2144. */
  2145. generate: function(options) {
  2146. options = extend(options || {}, {
  2147. base64: true,
  2148. compression: "STORE",
  2149. type: "base64",
  2150. comment: null
  2151. });
  2152. utils.checkSupport(options.type);
  2153. var zipData = [],
  2154. localDirLength = 0,
  2155. centralDirLength = 0,
  2156. writer, i,
  2157. utfEncodedComment = utils.transformTo("string", this.utf8encode(options.comment || this.comment || ""));
  2158. // first, generate all the zip parts.
  2159. for (var name in this.files) {
  2160. if (!this.files.hasOwnProperty(name)) {
  2161. continue;
  2162. }
  2163. var file = this.files[name];
  2164. var compressionName = file.options.compression || options.compression.toUpperCase();
  2165. var compression = compressions[compressionName];
  2166. if (!compression) {
  2167. throw new Error(compressionName + " is not a valid compression method !");
  2168. }
  2169. var compressedObject = generateCompressedObjectFrom.call(this, file, compression);
  2170. var zipPart = generateZipParts.call(this, name, file, compressedObject, localDirLength);
  2171. localDirLength += zipPart.fileRecord.length + compressedObject.compressedSize;
  2172. centralDirLength += zipPart.dirRecord.length;
  2173. zipData.push(zipPart);
  2174. }
  2175. var dirEnd = "";
  2176. // end of central dir signature
  2177. dirEnd = signature.CENTRAL_DIRECTORY_END +
  2178. // number of this disk
  2179. "\x00\x00" +
  2180. // number of the disk with the start of the central directory
  2181. "\x00\x00" +
  2182. // total number of entries in the central directory on this disk
  2183. decToHex(zipData.length, 2) +
  2184. // total number of entries in the central directory
  2185. decToHex(zipData.length, 2) +
  2186. // size of the central directory 4 bytes
  2187. decToHex(centralDirLength, 4) +
  2188. // offset of start of central directory with respect to the starting disk number
  2189. decToHex(localDirLength, 4) +
  2190. // .ZIP file comment length
  2191. decToHex(utfEncodedComment.length, 2) +
  2192. // .ZIP file comment
  2193. utfEncodedComment;
  2194. // we have all the parts (and the total length)
  2195. // time to create a writer !
  2196. var typeName = options.type.toLowerCase();
  2197. if(typeName==="uint8array"||typeName==="arraybuffer"||typeName==="blob"||typeName==="nodebuffer") {
  2198. writer = new Uint8ArrayWriter(localDirLength + centralDirLength + dirEnd.length);
  2199. }else{
  2200. writer = new StringWriter(localDirLength + centralDirLength + dirEnd.length);
  2201. }
  2202. for (i = 0; i < zipData.length; i++) {
  2203. writer.append(zipData[i].fileRecord);
  2204. writer.append(zipData[i].compressedObject.compressedContent);
  2205. }
  2206. for (i = 0; i < zipData.length; i++) {
  2207. writer.append(zipData[i].dirRecord);
  2208. }
  2209. writer.append(dirEnd);
  2210. var zip = writer.finalize();
  2211. switch(options.type.toLowerCase()) {
  2212. // case "zip is an Uint8Array"
  2213. case "uint8array" :
  2214. case "arraybuffer" :
  2215. case "nodebuffer" :
  2216. return utils.transformTo(options.type.toLowerCase(), zip);
  2217. case "blob" :
  2218. return utils.arrayBuffer2Blob(utils.transformTo("arraybuffer", zip));
  2219. // case "zip is a string"
  2220. case "base64" :
  2221. return (options.base64) ? base64.encode(zip) : zip;
  2222. default : // case "string" :
  2223. return zip;
  2224. }
  2225. },
  2226. /**
  2227. * @deprecated
  2228. * This method will be removed in a future version without replacement.
  2229. */
  2230. crc32: function (input, crc) {
  2231. return crc32(input, crc);
  2232. },
  2233. /**
  2234. * @deprecated
  2235. * This method will be removed in a future version without replacement.
  2236. */
  2237. utf8encode: function (string) {
  2238. return utils.transformTo("string", utf8.utf8encode(string));
  2239. },
  2240. /**
  2241. * @deprecated
  2242. * This method will be removed in a future version without replacement.
  2243. */
  2244. utf8decode: function (input) {
  2245. return utf8.utf8decode(input);
  2246. }
  2247. };
  2248. module.exports = out;
  2249. },{"./base64":1,"./compressedObject":2,"./compressions":3,"./crc32":4,"./defaults":6,"./nodeBuffer":11,"./signature":14,"./stringWriter":16,"./support":17,"./uint8ArrayWriter":19,"./utf8":20,"./utils":21}],14:[function(_dereq_,module,exports){
  2250. 'use strict';
  2251. exports.LOCAL_FILE_HEADER = "PK\x03\x04";
  2252. exports.CENTRAL_FILE_HEADER = "PK\x01\x02";
  2253. exports.CENTRAL_DIRECTORY_END = "PK\x05\x06";
  2254. exports.ZIP64_CENTRAL_DIRECTORY_LOCATOR = "PK\x06\x07";
  2255. exports.ZIP64_CENTRAL_DIRECTORY_END = "PK\x06\x06";
  2256. exports.DATA_DESCRIPTOR = "PK\x07\x08";
  2257. },{}],15:[function(_dereq_,module,exports){
  2258. 'use strict';
  2259. var DataReader = _dereq_('./dataReader');
  2260. var utils = _dereq_('./utils');
  2261. function StringReader(data, optimizedBinaryString) {
  2262. this.data = data;
  2263. if (!optimizedBinaryString) {
  2264. this.data = utils.string2binary(this.data);
  2265. }
  2266. this.length = this.data.length;
  2267. this.index = 0;
  2268. }
  2269. StringReader.prototype = new DataReader();
  2270. /**
  2271. * @see DataReader.byteAt
  2272. */
  2273. StringReader.prototype.byteAt = function(i) {
  2274. return this.data.charCodeAt(i);
  2275. };
  2276. /**
  2277. * @see DataReader.lastIndexOfSignature
  2278. */
  2279. StringReader.prototype.lastIndexOfSignature = function(sig) {
  2280. return this.data.lastIndexOf(sig);
  2281. };
  2282. /**
  2283. * @see DataReader.readData
  2284. */
  2285. StringReader.prototype.readData = function(size) {
  2286. this.checkOffset(size);
  2287. // this will work because the constructor applied the "& 0xff" mask.
  2288. var result = this.data.slice(this.index, this.index + size);
  2289. this.index += size;
  2290. return result;
  2291. };
  2292. module.exports = StringReader;
  2293. },{"./dataReader":5,"./utils":21}],16:[function(_dereq_,module,exports){
  2294. 'use strict';
  2295. var utils = _dereq_('./utils');
  2296. /**
  2297. * An object to write any content to a string.
  2298. * @constructor
  2299. */
  2300. var StringWriter = function() {
  2301. this.data = [];
  2302. };
  2303. StringWriter.prototype = {
  2304. /**
  2305. * Append any content to the current string.
  2306. * @param {Object} input the content to add.
  2307. */
  2308. append: function(input) {
  2309. input = utils.transformTo("string", input);
  2310. this.data.push(input);
  2311. },
  2312. /**
  2313. * Finalize the construction an return the result.
  2314. * @return {string} the generated string.
  2315. */
  2316. finalize: function() {
  2317. return this.data.join("");
  2318. }
  2319. };
  2320. module.exports = StringWriter;
  2321. },{"./utils":21}],17:[function(_dereq_,module,exports){
  2322. (function (Buffer){
  2323. 'use strict';
  2324. exports.base64 = true;
  2325. exports.array = true;
  2326. exports.string = true;
  2327. exports.arraybuffer = typeof ArrayBuffer !== "undefined" && typeof Uint8Array !== "undefined";
  2328. // contains true if JSZip can read/generate nodejs Buffer, false otherwise.
  2329. // Browserify will provide a Buffer implementation for browsers, which is
  2330. // an augmented Uint8Array (i.e., can be used as either Buffer or U8).
  2331. exports.nodebuffer = typeof Buffer !== "undefined";
  2332. // contains true if JSZip can read/generate Uint8Array, false otherwise.
  2333. exports.uint8array = typeof Uint8Array !== "undefined";
  2334. if (typeof ArrayBuffer === "undefined") {
  2335. exports.blob = false;
  2336. }
  2337. else {
  2338. var buffer = new ArrayBuffer(0);
  2339. try {
  2340. exports.blob = new Blob([buffer], {
  2341. type: "application/zip"
  2342. }).size === 0;
  2343. }
  2344. catch (e) {
  2345. try {
  2346. var Builder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;
  2347. var builder = new Builder();
  2348. builder.append(buffer);
  2349. exports.blob = builder.getBlob('application/zip').size === 0;
  2350. }
  2351. catch (e) {
  2352. exports.blob = false;
  2353. }
  2354. }
  2355. }
  2356. }).call(this,(typeof Buffer !== "undefined" ? Buffer : undefined))
  2357. },{}],18:[function(_dereq_,module,exports){
  2358. 'use strict';
  2359. var DataReader = _dereq_('./dataReader');
  2360. function Uint8ArrayReader(data) {
  2361. if (data) {
  2362. this.data = data;
  2363. this.length = this.data.length;
  2364. this.index = 0;
  2365. }
  2366. }
  2367. Uint8ArrayReader.prototype = new DataReader();
  2368. /**
  2369. * @see DataReader.byteAt
  2370. */
  2371. Uint8ArrayReader.prototype.byteAt = function(i) {
  2372. return this.data[i];
  2373. };
  2374. /**
  2375. * @see DataReader.lastIndexOfSignature
  2376. */
  2377. Uint8ArrayReader.prototype.lastIndexOfSignature = function(sig) {
  2378. var sig0 = sig.charCodeAt(0),
  2379. sig1 = sig.charCodeAt(1),
  2380. sig2 = sig.charCodeAt(2),
  2381. sig3 = sig.charCodeAt(3);
  2382. for (var i = this.length - 4; i >= 0; --i) {
  2383. if (this.data[i] === sig0 && this.data[i + 1] === sig1 && this.data[i + 2] === sig2 && this.data[i + 3] === sig3) {
  2384. return i;
  2385. }
  2386. }
  2387. return -1;
  2388. };
  2389. /**
  2390. * @see DataReader.readData
  2391. */
  2392. Uint8ArrayReader.prototype.readData = function(size) {
  2393. this.checkOffset(size);
  2394. if(size === 0) {
  2395. // in IE10, when using subarray(idx, idx), we get the array [0x00] instead of [].
  2396. return new Uint8Array(0);
  2397. }
  2398. var result = this.data.subarray(this.index, this.index + size);
  2399. this.index += size;
  2400. return result;
  2401. };
  2402. module.exports = Uint8ArrayReader;
  2403. },{"./dataReader":5}],19:[function(_dereq_,module,exports){
  2404. 'use strict';
  2405. var utils = _dereq_('./utils');
  2406. /**
  2407. * An object to write any content to an Uint8Array.
  2408. * @constructor
  2409. * @param {number} length The length of the array.
  2410. */
  2411. var Uint8ArrayWriter = function(length) {
  2412. this.data = new Uint8Array(length);
  2413. this.index = 0;
  2414. };
  2415. Uint8ArrayWriter.prototype = {
  2416. /**
  2417. * Append any content to the current array.
  2418. * @param {Object} input the content to add.
  2419. */
  2420. append: function(input) {
  2421. if (input.length !== 0) {
  2422. // with an empty Uint8Array, Opera fails with a "Offset larger than array size"
  2423. input = utils.transformTo("uint8array", input);
  2424. this.data.set(input, this.index);
  2425. this.index += input.length;
  2426. }
  2427. },
  2428. /**
  2429. * Finalize the construction an return the result.
  2430. * @return {Uint8Array} the generated array.
  2431. */
  2432. finalize: function() {
  2433. return this.data;
  2434. }
  2435. };
  2436. module.exports = Uint8ArrayWriter;
  2437. },{"./utils":21}],20:[function(_dereq_,module,exports){
  2438. 'use strict';
  2439. var utils = _dereq_('./utils');
  2440. var support = _dereq_('./support');
  2441. var nodeBuffer = _dereq_('./nodeBuffer');
  2442. /**
  2443. * The following functions come from pako, from pako/lib/utils/strings
  2444. * released under the MIT license, see pako https://github.com/nodeca/pako/
  2445. */
  2446. // Table with utf8 lengths (calculated by first byte of sequence)
  2447. // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
  2448. // because max possible codepoint is 0x10ffff
  2449. var _utf8len = new Array(256);
  2450. for (var i=0; i<256; i++) {
  2451. _utf8len[i] = (i >= 252 ? 6 : i >= 248 ? 5 : i >= 240 ? 4 : i >= 224 ? 3 : i >= 192 ? 2 : 1);
  2452. }
  2453. _utf8len[254]=_utf8len[254]=1; // Invalid sequence start
  2454. // convert string to array (typed, when possible)
  2455. var string2buf = function (str) {
  2456. var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
  2457. // count binary size
  2458. for (m_pos = 0; m_pos < str_len; m_pos++) {
  2459. c = str.charCodeAt(m_pos);
  2460. if (((c & 0xfc00) === 0xd800) && (m_pos+1 < str_len)) {
  2461. c2 = str.charCodeAt(m_pos+1);
  2462. if ((c2 & 0xfc00) === 0xdc00) {
  2463. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  2464. m_pos++;
  2465. }
  2466. }
  2467. buf_len += (c < 0x80) ? 1 : ((c < 0x800) ? 2 : ((c < 0x10000) ? 3 : 4));
  2468. }
  2469. // allocate buffer
  2470. if (support.uint8array) {
  2471. buf = new Uint8Array(buf_len);
  2472. } else {
  2473. buf = new Array(buf_len);
  2474. }
  2475. // convert
  2476. for (i=0, m_pos = 0; i < buf_len; m_pos++) {
  2477. c = str.charCodeAt(m_pos);
  2478. if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
  2479. c2 = str.charCodeAt(m_pos+1);
  2480. if ((c2 & 0xfc00) === 0xdc00) {
  2481. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  2482. m_pos++;
  2483. }
  2484. }
  2485. if (c < 0x80) {
  2486. /* one byte */
  2487. buf[i++] = c;
  2488. } else if (c < 0x800) {
  2489. /* two bytes */
  2490. buf[i++] = 0xC0 | (c >>> 6);
  2491. buf[i++] = 0x80 | (c & 0x3f);
  2492. } else if (c < 0x10000) {
  2493. /* three bytes */
  2494. buf[i++] = 0xE0 | (c >>> 12);
  2495. buf[i++] = 0x80 | ((c >>> 6) & 0x3f);
  2496. buf[i++] = 0x80 | (c & 0x3f);
  2497. } else {
  2498. /* four bytes */
  2499. buf[i++] = 0xf0 | (c >>> 18);
  2500. buf[i++] = 0x80 | ((c >>> 12) & 0x3f);
  2501. buf[i++] = 0x80 | ((c >>> 6) & 0x3f);
  2502. buf[i++] = 0x80 | (c & 0x3f);
  2503. }
  2504. }
  2505. return buf;
  2506. };
  2507. // Calculate max possible position in utf8 buffer,
  2508. // that will not break sequence. If that's not possible
  2509. // - (very small limits) return max size as is.
  2510. //
  2511. // buf[] - utf8 bytes array
  2512. // max - length limit (mandatory);
  2513. var utf8border = function(buf, max) {
  2514. var pos;
  2515. max = max || buf.length;
  2516. if (max > buf.length) { max = buf.length; }
  2517. // go back from last position, until start of sequence found
  2518. pos = max-1;
  2519. while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
  2520. // Fuckup - very small and broken sequence,
  2521. // return max, because we should return something anyway.
  2522. if (pos < 0) { return max; }
  2523. // If we came to start of buffer - that means vuffer is too small,
  2524. // return max too.
  2525. if (pos === 0) { return max; }
  2526. return (pos + _utf8len[buf[pos]] > max) ? pos : max;
  2527. };
  2528. // convert array to string
  2529. var buf2string = function (buf) {
  2530. var str, i, out, c, c_len;
  2531. var len = buf.length;
  2532. // Reserve max possible length (2 words per char)
  2533. // NB: by unknown reasons, Array is significantly faster for
  2534. // String.fromCharCode.apply than Uint16Array.
  2535. var utf16buf = new Array(len*2);
  2536. for (out=0, i=0; i<len;) {
  2537. c = buf[i++];
  2538. // quick process ascii
  2539. if (c < 0x80) { utf16buf[out++] = c; continue; }
  2540. c_len = _utf8len[c];
  2541. // skip 5 & 6 byte codes
  2542. if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len-1; continue; }
  2543. // apply mask on first byte
  2544. c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
  2545. // join the rest
  2546. while (c_len > 1 && i < len) {
  2547. c = (c << 6) | (buf[i++] & 0x3f);
  2548. c_len--;
  2549. }
  2550. // terminated by end of string?
  2551. if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
  2552. if (c < 0x10000) {
  2553. utf16buf[out++] = c;
  2554. } else {
  2555. c -= 0x10000;
  2556. utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
  2557. utf16buf[out++] = 0xdc00 | (c & 0x3ff);
  2558. }
  2559. }
  2560. // shrinkBuf(utf16buf, out)
  2561. if (utf16buf.length !== out) {
  2562. if(utf16buf.subarray) {
  2563. utf16buf = utf16buf.subarray(0, out);
  2564. } else {
  2565. utf16buf.length = out;
  2566. }
  2567. }
  2568. // return String.fromCharCode.apply(null, utf16buf);
  2569. return utils.applyFromCharCode(utf16buf);
  2570. };
  2571. // That's all for the pako functions.
  2572. /**
  2573. * Transform a javascript string into an array (typed if possible) of bytes,
  2574. * UTF-8 encoded.
  2575. * @param {String} str the string to encode
  2576. * @return {Array|Uint8Array|Buffer} the UTF-8 encoded string.
  2577. */
  2578. exports.utf8encode = function utf8encode(str) {
  2579. if (support.nodebuffer) {
  2580. return nodeBuffer(str, "utf-8");
  2581. }
  2582. return string2buf(str);
  2583. };
  2584. /**
  2585. * Transform a bytes array (or a representation) representing an UTF-8 encoded
  2586. * string into a javascript string.
  2587. * @param {Array|Uint8Array|Buffer} buf the data de decode
  2588. * @return {String} the decoded string.
  2589. */
  2590. exports.utf8decode = function utf8decode(buf) {
  2591. if (support.nodebuffer) {
  2592. return utils.transformTo("nodebuffer", buf).toString("utf-8");
  2593. }
  2594. buf = utils.transformTo(support.uint8array ? "uint8array" : "array", buf);
  2595. // return buf2string(buf);
  2596. // Chrome prefers to work with "small" chunks of data
  2597. // for the method buf2string.
  2598. // Firefox and Chrome has their own shortcut, IE doesn't seem to really care.
  2599. var result = [], k = 0, len = buf.length, chunk = 65536;
  2600. while (k < len) {
  2601. var nextBoundary = utf8border(buf, Math.min(k + chunk, len));
  2602. if (support.uint8array) {
  2603. result.push(buf2string(buf.subarray(k, nextBoundary)));
  2604. } else {
  2605. result.push(buf2string(buf.slice(k, nextBoundary)));
  2606. }
  2607. k = nextBoundary;
  2608. }
  2609. return result.join("");
  2610. };
  2611. // vim: set shiftwidth=4 softtabstop=4:
  2612. },{"./nodeBuffer":11,"./support":17,"./utils":21}],21:[function(_dereq_,module,exports){
  2613. 'use strict';
  2614. var support = _dereq_('./support');
  2615. var compressions = _dereq_('./compressions');
  2616. var nodeBuffer = _dereq_('./nodeBuffer');
  2617. /**
  2618. * Convert a string to a "binary string" : a string containing only char codes between 0 and 255.
  2619. * @param {string} str the string to transform.
  2620. * @return {String} the binary string.
  2621. */
  2622. exports.string2binary = function(str) {
  2623. var result = "";
  2624. for (var i = 0; i < str.length; i++) {
  2625. result += String.fromCharCode(str.charCodeAt(i) & 0xff);
  2626. }
  2627. return result;
  2628. };
  2629. exports.arrayBuffer2Blob = function(buffer) {
  2630. exports.checkSupport("blob");
  2631. try {
  2632. // Blob constructor
  2633. return new Blob([buffer], {
  2634. type: "application/zip"
  2635. });
  2636. }
  2637. catch (e) {
  2638. try {
  2639. // deprecated, browser only, old way
  2640. var Builder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;
  2641. var builder = new Builder();
  2642. builder.append(buffer);
  2643. return builder.getBlob('application/zip');
  2644. }
  2645. catch (e) {
  2646. // well, fuck ?!
  2647. throw new Error("Bug : can't construct the Blob.");
  2648. }
  2649. }
  2650. };
  2651. /**
  2652. * The identity function.
  2653. * @param {Object} input the input.
  2654. * @return {Object} the same input.
  2655. */
  2656. function identity(input) {
  2657. return input;
  2658. }
  2659. /**
  2660. * Fill in an array with a string.
  2661. * @param {String} str the string to use.
  2662. * @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to fill in (will be mutated).
  2663. * @return {Array|ArrayBuffer|Uint8Array|Buffer} the updated array.
  2664. */
  2665. function stringToArrayLike(str, array) {
  2666. for (var i = 0; i < str.length; ++i) {
  2667. array[i] = str.charCodeAt(i) & 0xFF;
  2668. }
  2669. return array;
  2670. }
  2671. /**
  2672. * Transform an array-like object to a string.
  2673. * @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to transform.
  2674. * @return {String} the result.
  2675. */
  2676. function arrayLikeToString(array) {
  2677. // Performances notes :
  2678. // --------------------
  2679. // String.fromCharCode.apply(null, array) is the fastest, see
  2680. // see http://jsperf.com/converting-a-uint8array-to-a-string/2
  2681. // but the stack is limited (and we can get huge arrays !).
  2682. //
  2683. // result += String.fromCharCode(array[i]); generate too many strings !
  2684. //
  2685. // This code is inspired by http://jsperf.com/arraybuffer-to-string-apply-performance/2
  2686. var chunk = 65536;
  2687. var result = [],
  2688. len = array.length,
  2689. type = exports.getTypeOf(array),
  2690. k = 0,
  2691. canUseApply = true;
  2692. try {
  2693. switch(type) {
  2694. case "uint8array":
  2695. String.fromCharCode.apply(null, new Uint8Array(0));
  2696. break;
  2697. case "nodebuffer":
  2698. String.fromCharCode.apply(null, nodeBuffer(0));
  2699. break;
  2700. }
  2701. } catch(e) {
  2702. canUseApply = false;
  2703. }
  2704. // no apply : slow and painful algorithm
  2705. // default browser on android 4.*
  2706. if (!canUseApply) {
  2707. var resultStr = "";
  2708. for(var i = 0; i < array.length;i++) {
  2709. resultStr += String.fromCharCode(array[i]);
  2710. }
  2711. return resultStr;
  2712. }
  2713. while (k < len && chunk > 1) {
  2714. try {
  2715. if (type === "array" || type === "nodebuffer") {
  2716. result.push(String.fromCharCode.apply(null, array.slice(k, Math.min(k + chunk, len))));
  2717. }
  2718. else {
  2719. result.push(String.fromCharCode.apply(null, array.subarray(k, Math.min(k + chunk, len))));
  2720. }
  2721. k += chunk;
  2722. }
  2723. catch (e) {
  2724. chunk = Math.floor(chunk / 2);
  2725. }
  2726. }
  2727. return result.join("");
  2728. }
  2729. exports.applyFromCharCode = arrayLikeToString;
  2730. /**
  2731. * Copy the data from an array-like to an other array-like.
  2732. * @param {Array|ArrayBuffer|Uint8Array|Buffer} arrayFrom the origin array.
  2733. * @param {Array|ArrayBuffer|Uint8Array|Buffer} arrayTo the destination array which will be mutated.
  2734. * @return {Array|ArrayBuffer|Uint8Array|Buffer} the updated destination array.
  2735. */
  2736. function arrayLikeToArrayLike(arrayFrom, arrayTo) {
  2737. for (var i = 0; i < arrayFrom.length; i++) {
  2738. arrayTo[i] = arrayFrom[i];
  2739. }
  2740. return arrayTo;
  2741. }
  2742. // a matrix containing functions to transform everything into everything.
  2743. var transform = {};
  2744. // string to ?
  2745. transform["string"] = {
  2746. "string": identity,
  2747. "array": function(input) {
  2748. return stringToArrayLike(input, new Array(input.length));
  2749. },
  2750. "arraybuffer": function(input) {
  2751. return transform["string"]["uint8array"](input).buffer;
  2752. },
  2753. "uint8array": function(input) {
  2754. return stringToArrayLike(input, new Uint8Array(input.length));
  2755. },
  2756. "nodebuffer": function(input) {
  2757. return stringToArrayLike(input, nodeBuffer(input.length));
  2758. }
  2759. };
  2760. // array to ?
  2761. transform["array"] = {
  2762. "string": arrayLikeToString,
  2763. "array": identity,
  2764. "arraybuffer": function(input) {
  2765. return (new Uint8Array(input)).buffer;
  2766. },
  2767. "uint8array": function(input) {
  2768. return new Uint8Array(input);
  2769. },
  2770. "nodebuffer": function(input) {
  2771. return nodeBuffer(input);
  2772. }
  2773. };
  2774. // arraybuffer to ?
  2775. transform["arraybuffer"] = {
  2776. "string": function(input) {
  2777. return arrayLikeToString(new Uint8Array(input));
  2778. },
  2779. "array": function(input) {
  2780. return arrayLikeToArrayLike(new Uint8Array(input), new Array(input.byteLength));
  2781. },
  2782. "arraybuffer": identity,
  2783. "uint8array": function(input) {
  2784. return new Uint8Array(input);
  2785. },
  2786. "nodebuffer": function(input) {
  2787. return nodeBuffer(new Uint8Array(input));
  2788. }
  2789. };
  2790. // uint8array to ?
  2791. transform["uint8array"] = {
  2792. "string": arrayLikeToString,
  2793. "array": function(input) {
  2794. return arrayLikeToArrayLike(input, new Array(input.length));
  2795. },
  2796. "arraybuffer": function(input) {
  2797. return input.buffer;
  2798. },
  2799. "uint8array": identity,
  2800. "nodebuffer": function(input) {
  2801. return nodeBuffer(input);
  2802. }
  2803. };
  2804. // nodebuffer to ?
  2805. transform["nodebuffer"] = {
  2806. "string": arrayLikeToString,
  2807. "array": function(input) {
  2808. return arrayLikeToArrayLike(input, new Array(input.length));
  2809. },
  2810. "arraybuffer": function(input) {
  2811. return transform["nodebuffer"]["uint8array"](input).buffer;
  2812. },
  2813. "uint8array": function(input) {
  2814. return arrayLikeToArrayLike(input, new Uint8Array(input.length));
  2815. },
  2816. "nodebuffer": identity
  2817. };
  2818. /**
  2819. * Transform an input into any type.
  2820. * The supported output type are : string, array, uint8array, arraybuffer, nodebuffer.
  2821. * If no output type is specified, the unmodified input will be returned.
  2822. * @param {String} outputType the output type.
  2823. * @param {String|Array|ArrayBuffer|Uint8Array|Buffer} input the input to convert.
  2824. * @throws {Error} an Error if the browser doesn't support the requested output type.
  2825. */
  2826. exports.transformTo = function(outputType, input) {
  2827. if (!input) {
  2828. // undefined, null, etc
  2829. // an empty string won't harm.
  2830. input = "";
  2831. }
  2832. if (!outputType) {
  2833. return input;
  2834. }
  2835. exports.checkSupport(outputType);
  2836. var inputType = exports.getTypeOf(input);
  2837. var result = transform[inputType][outputType](input);
  2838. return result;
  2839. };
  2840. /**
  2841. * Return the type of the input.
  2842. * The type will be in a format valid for JSZip.utils.transformTo : string, array, uint8array, arraybuffer.
  2843. * @param {Object} input the input to identify.
  2844. * @return {String} the (lowercase) type of the input.
  2845. */
  2846. exports.getTypeOf = function(input) {
  2847. if (typeof input === "string") {
  2848. return "string";
  2849. }
  2850. if (Object.prototype.toString.call(input) === "[object Array]") {
  2851. return "array";
  2852. }
  2853. if (support.nodebuffer && nodeBuffer.test(input)) {
  2854. return "nodebuffer";
  2855. }
  2856. if (support.uint8array && input instanceof Uint8Array) {
  2857. return "uint8array";
  2858. }
  2859. if (support.arraybuffer && input instanceof ArrayBuffer) {
  2860. return "arraybuffer";
  2861. }
  2862. };
  2863. /**
  2864. * Throw an exception if the type is not supported.
  2865. * @param {String} type the type to check.
  2866. * @throws {Error} an Error if the browser doesn't support the requested type.
  2867. */
  2868. exports.checkSupport = function(type) {
  2869. var supported = support[type.toLowerCase()];
  2870. if (!supported) {
  2871. throw new Error(type + " is not supported by this browser");
  2872. }
  2873. };
  2874. exports.MAX_VALUE_16BITS = 65535;
  2875. exports.MAX_VALUE_32BITS = -1; // well, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" is parsed as -1
  2876. /**
  2877. * Prettify a string read as binary.
  2878. * @param {string} str the string to prettify.
  2879. * @return {string} a pretty string.
  2880. */
  2881. exports.pretty = function(str) {
  2882. var res = '',
  2883. code, i;
  2884. for (i = 0; i < (str || "").length; i++) {
  2885. code = str.charCodeAt(i);
  2886. res += '\\x' + (code < 16 ? "0" : "") + code.toString(16).toUpperCase();
  2887. }
  2888. return res;
  2889. };
  2890. /**
  2891. * Find a compression registered in JSZip.
  2892. * @param {string} compressionMethod the method magic to find.
  2893. * @return {Object|null} the JSZip compression object, null if none found.
  2894. */
  2895. exports.findCompression = function(compressionMethod) {
  2896. for (var method in compressions) {
  2897. if (!compressions.hasOwnProperty(method)) {
  2898. continue;
  2899. }
  2900. if (compressions[method].magic === compressionMethod) {
  2901. return compressions[method];
  2902. }
  2903. }
  2904. return null;
  2905. };
  2906. /**
  2907. * Cross-window, cross-Node-context regular expression detection
  2908. * @param {Object} object Anything
  2909. * @return {Boolean} true if the object is a regular expression,
  2910. * false otherwise
  2911. */
  2912. exports.isRegExp = function (object) {
  2913. return Object.prototype.toString.call(object) === "[object RegExp]";
  2914. };
  2915. },{"./compressions":3,"./nodeBuffer":11,"./support":17}],22:[function(_dereq_,module,exports){
  2916. 'use strict';
  2917. var StringReader = _dereq_('./stringReader');
  2918. var NodeBufferReader = _dereq_('./nodeBufferReader');
  2919. var Uint8ArrayReader = _dereq_('./uint8ArrayReader');
  2920. var utils = _dereq_('./utils');
  2921. var sig = _dereq_('./signature');
  2922. var ZipEntry = _dereq_('./zipEntry');
  2923. var support = _dereq_('./support');
  2924. var jszipProto = _dereq_('./object');
  2925. // class ZipEntries {{{
  2926. /**
  2927. * All the entries in the zip file.
  2928. * @constructor
  2929. * @param {String|ArrayBuffer|Uint8Array} data the binary stream to load.
  2930. * @param {Object} loadOptions Options for loading the stream.
  2931. */
  2932. function ZipEntries(data, loadOptions) {
  2933. this.files = [];
  2934. this.loadOptions = loadOptions;
  2935. if (data) {
  2936. this.load(data);
  2937. }
  2938. }
  2939. ZipEntries.prototype = {
  2940. /**
  2941. * Check that the reader is on the speficied signature.
  2942. * @param {string} expectedSignature the expected signature.
  2943. * @throws {Error} if it is an other signature.
  2944. */
  2945. checkSignature: function(expectedSignature) {
  2946. var signature = this.reader.readString(4);
  2947. if (signature !== expectedSignature) {
  2948. throw new Error("Corrupted zip or bug : unexpected signature " + "(" + utils.pretty(signature) + ", expected " + utils.pretty(expectedSignature) + ")");
  2949. }
  2950. },
  2951. /**
  2952. * Read the end of the central directory.
  2953. */
  2954. readBlockEndOfCentral: function() {
  2955. this.diskNumber = this.reader.readInt(2);
  2956. this.diskWithCentralDirStart = this.reader.readInt(2);
  2957. this.centralDirRecordsOnThisDisk = this.reader.readInt(2);
  2958. this.centralDirRecords = this.reader.readInt(2);
  2959. this.centralDirSize = this.reader.readInt(4);
  2960. this.centralDirOffset = this.reader.readInt(4);
  2961. this.zipCommentLength = this.reader.readInt(2);
  2962. // warning : the encoding depends of the system locale
  2963. // On a linux machine with LANG=en_US.utf8, this field is utf8 encoded.
  2964. // On a windows machine, this field is encoded with the localized windows code page.
  2965. this.zipComment = this.reader.readString(this.zipCommentLength);
  2966. // To get consistent behavior with the generation part, we will assume that
  2967. // this is utf8 encoded.
  2968. this.zipComment = jszipProto.utf8decode(this.zipComment);
  2969. },
  2970. /**
  2971. * Read the end of the Zip 64 central directory.
  2972. * Not merged with the method readEndOfCentral :
  2973. * The end of central can coexist with its Zip64 brother,
  2974. * I don't want to read the wrong number of bytes !
  2975. */
  2976. readBlockZip64EndOfCentral: function() {
  2977. this.zip64EndOfCentralSize = this.reader.readInt(8);
  2978. this.versionMadeBy = this.reader.readString(2);
  2979. this.versionNeeded = this.reader.readInt(2);
  2980. this.diskNumber = this.reader.readInt(4);
  2981. this.diskWithCentralDirStart = this.reader.readInt(4);
  2982. this.centralDirRecordsOnThisDisk = this.reader.readInt(8);
  2983. this.centralDirRecords = this.reader.readInt(8);
  2984. this.centralDirSize = this.reader.readInt(8);
  2985. this.centralDirOffset = this.reader.readInt(8);
  2986. this.zip64ExtensibleData = {};
  2987. var extraDataSize = this.zip64EndOfCentralSize - 44,
  2988. index = 0,
  2989. extraFieldId,
  2990. extraFieldLength,
  2991. extraFieldValue;
  2992. while (index < extraDataSize) {
  2993. extraFieldId = this.reader.readInt(2);
  2994. extraFieldLength = this.reader.readInt(4);
  2995. extraFieldValue = this.reader.readString(extraFieldLength);
  2996. this.zip64ExtensibleData[extraFieldId] = {
  2997. id: extraFieldId,
  2998. length: extraFieldLength,
  2999. value: extraFieldValue
  3000. };
  3001. }
  3002. },
  3003. /**
  3004. * Read the end of the Zip 64 central directory locator.
  3005. */
  3006. readBlockZip64EndOfCentralLocator: function() {
  3007. this.diskWithZip64CentralDirStart = this.reader.readInt(4);
  3008. this.relativeOffsetEndOfZip64CentralDir = this.reader.readInt(8);
  3009. this.disksCount = this.reader.readInt(4);
  3010. if (this.disksCount > 1) {
  3011. throw new Error("Multi-volumes zip are not supported");
  3012. }
  3013. },
  3014. /**
  3015. * Read the local files, based on the offset read in the central part.
  3016. */
  3017. readLocalFiles: function() {
  3018. var i, file;
  3019. for (i = 0; i < this.files.length; i++) {
  3020. file = this.files[i];
  3021. this.reader.setIndex(file.localHeaderOffset);
  3022. this.checkSignature(sig.LOCAL_FILE_HEADER);
  3023. file.readLocalPart(this.reader);
  3024. file.handleUTF8();
  3025. }
  3026. },
  3027. /**
  3028. * Read the central directory.
  3029. */
  3030. readCentralDir: function() {
  3031. var file;
  3032. this.reader.setIndex(this.centralDirOffset);
  3033. while (this.reader.readString(4) === sig.CENTRAL_FILE_HEADER) {
  3034. file = new ZipEntry({
  3035. zip64: this.zip64
  3036. }, this.loadOptions);
  3037. file.readCentralPart(this.reader);
  3038. this.files.push(file);
  3039. }
  3040. },
  3041. /**
  3042. * Read the end of central directory.
  3043. */
  3044. readEndOfCentral: function() {
  3045. var offset = this.reader.lastIndexOfSignature(sig.CENTRAL_DIRECTORY_END);
  3046. if (offset === -1) {
  3047. throw new Error("Corrupted zip : can't find end of central directory");
  3048. }
  3049. this.reader.setIndex(offset);
  3050. this.checkSignature(sig.CENTRAL_DIRECTORY_END);
  3051. this.readBlockEndOfCentral();
  3052. /* extract from the zip spec :
  3053. 4) If one of the fields in the end of central directory
  3054. record is too small to hold required data, the field
  3055. should be set to -1 (0xFFFF or 0xFFFFFFFF) and the
  3056. ZIP64 format record should be created.
  3057. 5) The end of central directory record and the
  3058. Zip64 end of central directory locator record must
  3059. reside on the same disk when splitting or spanning
  3060. an archive.
  3061. */
  3062. if (this.diskNumber === utils.MAX_VALUE_16BITS || this.diskWithCentralDirStart === utils.MAX_VALUE_16BITS || this.centralDirRecordsOnThisDisk === utils.MAX_VALUE_16BITS || this.centralDirRecords === utils.MAX_VALUE_16BITS || this.centralDirSize === utils.MAX_VALUE_32BITS || this.centralDirOffset === utils.MAX_VALUE_32BITS) {
  3063. this.zip64 = true;
  3064. /*
  3065. Warning : the zip64 extension is supported, but ONLY if the 64bits integer read from
  3066. the zip file can fit into a 32bits integer. This cannot be solved : Javascript represents
  3067. all numbers as 64-bit double precision IEEE 754 floating point numbers.
  3068. So, we have 53bits for integers and bitwise operations treat everything as 32bits.
  3069. see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Bitwise_Operators
  3070. and http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf section 8.5
  3071. */
  3072. // should look for a zip64 EOCD locator
  3073. offset = this.reader.lastIndexOfSignature(sig.ZIP64_CENTRAL_DIRECTORY_LOCATOR);
  3074. if (offset === -1) {
  3075. throw new Error("Corrupted zip : can't find the ZIP64 end of central directory locator");
  3076. }
  3077. this.reader.setIndex(offset);
  3078. this.checkSignature(sig.ZIP64_CENTRAL_DIRECTORY_LOCATOR);
  3079. this.readBlockZip64EndOfCentralLocator();
  3080. // now the zip64 EOCD record
  3081. this.reader.setIndex(this.relativeOffsetEndOfZip64CentralDir);
  3082. this.checkSignature(sig.ZIP64_CENTRAL_DIRECTORY_END);
  3083. this.readBlockZip64EndOfCentral();
  3084. }
  3085. },
  3086. prepareReader: function(data) {
  3087. var type = utils.getTypeOf(data);
  3088. if (type === "string" && !support.uint8array) {
  3089. this.reader = new StringReader(data, this.loadOptions.optimizedBinaryString);
  3090. }
  3091. else if (type === "nodebuffer") {
  3092. this.reader = new NodeBufferReader(data);
  3093. }
  3094. else {
  3095. this.reader = new Uint8ArrayReader(utils.transformTo("uint8array", data));
  3096. }
  3097. },
  3098. /**
  3099. * Read a zip file and create ZipEntries.
  3100. * @param {String|ArrayBuffer|Uint8Array|Buffer} data the binary string representing a zip file.
  3101. */
  3102. load: function(data) {
  3103. this.prepareReader(data);
  3104. this.readEndOfCentral();
  3105. this.readCentralDir();
  3106. this.readLocalFiles();
  3107. }
  3108. };
  3109. // }}} end of ZipEntries
  3110. module.exports = ZipEntries;
  3111. },{"./nodeBufferReader":12,"./object":13,"./signature":14,"./stringReader":15,"./support":17,"./uint8ArrayReader":18,"./utils":21,"./zipEntry":23}],23:[function(_dereq_,module,exports){
  3112. 'use strict';
  3113. var StringReader = _dereq_('./stringReader');
  3114. var utils = _dereq_('./utils');
  3115. var CompressedObject = _dereq_('./compressedObject');
  3116. var jszipProto = _dereq_('./object');
  3117. // class ZipEntry {{{
  3118. /**
  3119. * An entry in the zip file.
  3120. * @constructor
  3121. * @param {Object} options Options of the current file.
  3122. * @param {Object} loadOptions Options for loading the stream.
  3123. */
  3124. function ZipEntry(options, loadOptions) {
  3125. this.options = options;
  3126. this.loadOptions = loadOptions;
  3127. }
  3128. ZipEntry.prototype = {
  3129. /**
  3130. * say if the file is encrypted.
  3131. * @return {boolean} true if the file is encrypted, false otherwise.
  3132. */
  3133. isEncrypted: function() {
  3134. // bit 1 is set
  3135. return (this.bitFlag & 0x0001) === 0x0001;
  3136. },
  3137. /**
  3138. * say if the file has utf-8 filename/comment.
  3139. * @return {boolean} true if the filename/comment is in utf-8, false otherwise.
  3140. */
  3141. useUTF8: function() {
  3142. // bit 11 is set
  3143. return (this.bitFlag & 0x0800) === 0x0800;
  3144. },
  3145. /**
  3146. * Prepare the function used to generate the compressed content from this ZipFile.
  3147. * @param {DataReader} reader the reader to use.
  3148. * @param {number} from the offset from where we should read the data.
  3149. * @param {number} length the length of the data to read.
  3150. * @return {Function} the callback to get the compressed content (the type depends of the DataReader class).
  3151. */
  3152. prepareCompressedContent: function(reader, from, length) {
  3153. return function() {
  3154. var previousIndex = reader.index;
  3155. reader.setIndex(from);
  3156. var compressedFileData = reader.readData(length);
  3157. reader.setIndex(previousIndex);
  3158. return compressedFileData;
  3159. };
  3160. },
  3161. /**
  3162. * Prepare the function used to generate the uncompressed content from this ZipFile.
  3163. * @param {DataReader} reader the reader to use.
  3164. * @param {number} from the offset from where we should read the data.
  3165. * @param {number} length the length of the data to read.
  3166. * @param {JSZip.compression} compression the compression used on this file.
  3167. * @param {number} uncompressedSize the uncompressed size to expect.
  3168. * @return {Function} the callback to get the uncompressed content (the type depends of the DataReader class).
  3169. */
  3170. prepareContent: function(reader, from, length, compression, uncompressedSize) {
  3171. return function() {
  3172. var compressedFileData = utils.transformTo(compression.uncompressInputType, this.getCompressedContent());
  3173. var uncompressedFileData = compression.uncompress(compressedFileData);
  3174. if (uncompressedFileData.length !== uncompressedSize) {
  3175. throw new Error("Bug : uncompressed data size mismatch");
  3176. }
  3177. return uncompressedFileData;
  3178. };
  3179. },
  3180. /**
  3181. * Read the local part of a zip file and add the info in this object.
  3182. * @param {DataReader} reader the reader to use.
  3183. */
  3184. readLocalPart: function(reader) {
  3185. var compression, localExtraFieldsLength;
  3186. // we already know everything from the central dir !
  3187. // If the central dir data are false, we are doomed.
  3188. // On the bright side, the local part is scary : zip64, data descriptors, both, etc.
  3189. // The less data we get here, the more reliable this should be.
  3190. // Let's skip the whole header and dash to the data !
  3191. reader.skip(22);
  3192. // in some zip created on windows, the filename stored in the central dir contains \ instead of /.
  3193. // Strangely, the filename here is OK.
  3194. // I would love to treat these zip files as corrupted (see http://www.info-zip.org/FAQ.html#backslashes
  3195. // or APPNOTE#4.4.17.1, "All slashes MUST be forward slashes '/'") but there are a lot of bad zip generators...
  3196. // Search "unzip mismatching "local" filename continuing with "central" filename version" on
  3197. // the internet.
  3198. //
  3199. // I think I see the logic here : the central directory is used to display
  3200. // content and the local directory is used to extract the files. Mixing / and \
  3201. // may be used to display \ to windows users and use / when extracting the files.
  3202. // Unfortunately, this lead also to some issues : http://seclists.org/fulldisclosure/2009/Sep/394
  3203. this.fileNameLength = reader.readInt(2);
  3204. localExtraFieldsLength = reader.readInt(2); // can't be sure this will be the same as the central dir
  3205. this.fileName = reader.readString(this.fileNameLength);
  3206. reader.skip(localExtraFieldsLength);
  3207. if (this.compressedSize == -1 || this.uncompressedSize == -1) {
  3208. throw new Error("Bug or corrupted zip : didn't get enough informations from the central directory " + "(compressedSize == -1 || uncompressedSize == -1)");
  3209. }
  3210. compression = utils.findCompression(this.compressionMethod);
  3211. if (compression === null) { // no compression found
  3212. throw new Error("Corrupted zip : compression " + utils.pretty(this.compressionMethod) + " unknown (inner file : " + this.fileName + ")");
  3213. }
  3214. this.decompressed = new CompressedObject();
  3215. this.decompressed.compressedSize = this.compressedSize;
  3216. this.decompressed.uncompressedSize = this.uncompressedSize;
  3217. this.decompressed.crc32 = this.crc32;
  3218. this.decompressed.compressionMethod = this.compressionMethod;
  3219. this.decompressed.getCompressedContent = this.prepareCompressedContent(reader, reader.index, this.compressedSize, compression);
  3220. this.decompressed.getContent = this.prepareContent(reader, reader.index, this.compressedSize, compression, this.uncompressedSize);
  3221. // we need to compute the crc32...
  3222. if (this.loadOptions.checkCRC32) {
  3223. this.decompressed = utils.transformTo("string", this.decompressed.getContent());
  3224. if (jszipProto.crc32(this.decompressed) !== this.crc32) {
  3225. throw new Error("Corrupted zip : CRC32 mismatch");
  3226. }
  3227. }
  3228. },
  3229. /**
  3230. * Read the central part of a zip file and add the info in this object.
  3231. * @param {DataReader} reader the reader to use.
  3232. */
  3233. readCentralPart: function(reader) {
  3234. this.versionMadeBy = reader.readString(2);
  3235. this.versionNeeded = reader.readInt(2);
  3236. this.bitFlag = reader.readInt(2);
  3237. this.compressionMethod = reader.readString(2);
  3238. this.date = reader.readDate();
  3239. this.crc32 = reader.readInt(4);
  3240. this.compressedSize = reader.readInt(4);
  3241. this.uncompressedSize = reader.readInt(4);
  3242. this.fileNameLength = reader.readInt(2);
  3243. this.extraFieldsLength = reader.readInt(2);
  3244. this.fileCommentLength = reader.readInt(2);
  3245. this.diskNumberStart = reader.readInt(2);
  3246. this.internalFileAttributes = reader.readInt(2);
  3247. this.externalFileAttributes = reader.readInt(4);
  3248. this.localHeaderOffset = reader.readInt(4);
  3249. if (this.isEncrypted()) {
  3250. throw new Error("Encrypted zip are not supported");
  3251. }
  3252. this.fileName = reader.readString(this.fileNameLength);
  3253. this.readExtraFields(reader);
  3254. this.parseZIP64ExtraField(reader);
  3255. this.fileComment = reader.readString(this.fileCommentLength);
  3256. // warning, this is true only for zip with madeBy == DOS (plateform dependent feature)
  3257. this.dir = this.externalFileAttributes & 0x00000010 ? true : false;
  3258. },
  3259. /**
  3260. * Parse the ZIP64 extra field and merge the info in the current ZipEntry.
  3261. * @param {DataReader} reader the reader to use.
  3262. */
  3263. parseZIP64ExtraField: function(reader) {
  3264. if (!this.extraFields[0x0001]) {
  3265. return;
  3266. }
  3267. // should be something, preparing the extra reader
  3268. var extraReader = new StringReader(this.extraFields[0x0001].value);
  3269. // I really hope that these 64bits integer can fit in 32 bits integer, because js
  3270. // won't let us have more.
  3271. if (this.uncompressedSize === utils.MAX_VALUE_32BITS) {
  3272. this.uncompressedSize = extraReader.readInt(8);
  3273. }
  3274. if (this.compressedSize === utils.MAX_VALUE_32BITS) {
  3275. this.compressedSize = extraReader.readInt(8);
  3276. }
  3277. if (this.localHeaderOffset === utils.MAX_VALUE_32BITS) {
  3278. this.localHeaderOffset = extraReader.readInt(8);
  3279. }
  3280. if (this.diskNumberStart === utils.MAX_VALUE_32BITS) {
  3281. this.diskNumberStart = extraReader.readInt(4);
  3282. }
  3283. },
  3284. /**
  3285. * Read the central part of a zip file and add the info in this object.
  3286. * @param {DataReader} reader the reader to use.
  3287. */
  3288. readExtraFields: function(reader) {
  3289. var start = reader.index,
  3290. extraFieldId,
  3291. extraFieldLength,
  3292. extraFieldValue;
  3293. this.extraFields = this.extraFields || {};
  3294. while (reader.index < start + this.extraFieldsLength) {
  3295. extraFieldId = reader.readInt(2);
  3296. extraFieldLength = reader.readInt(2);
  3297. extraFieldValue = reader.readString(extraFieldLength);
  3298. this.extraFields[extraFieldId] = {
  3299. id: extraFieldId,
  3300. length: extraFieldLength,
  3301. value: extraFieldValue
  3302. };
  3303. }
  3304. },
  3305. /**
  3306. * Apply an UTF8 transformation if needed.
  3307. */
  3308. handleUTF8: function() {
  3309. if (this.useUTF8()) {
  3310. this.fileName = jszipProto.utf8decode(this.fileName);
  3311. this.fileComment = jszipProto.utf8decode(this.fileComment);
  3312. } else {
  3313. var upath = this.findExtraFieldUnicodePath();
  3314. if (upath !== null) {
  3315. this.fileName = upath;
  3316. }
  3317. var ucomment = this.findExtraFieldUnicodeComment();
  3318. if (ucomment !== null) {
  3319. this.fileComment = ucomment;
  3320. }
  3321. }
  3322. },
  3323. /**
  3324. * Find the unicode path declared in the extra field, if any.
  3325. * @return {String} the unicode path, null otherwise.
  3326. */
  3327. findExtraFieldUnicodePath: function() {
  3328. var upathField = this.extraFields[0x7075];
  3329. if (upathField) {
  3330. var extraReader = new StringReader(upathField.value);
  3331. // wrong version
  3332. if (extraReader.readInt(1) !== 1) {
  3333. return null;
  3334. }
  3335. // the crc of the filename changed, this field is out of date.
  3336. if (jszipProto.crc32(this.fileName) !== extraReader.readInt(4)) {
  3337. return null;
  3338. }
  3339. return jszipProto.utf8decode(extraReader.readString(upathField.length - 5));
  3340. }
  3341. return null;
  3342. },
  3343. /**
  3344. * Find the unicode comment declared in the extra field, if any.
  3345. * @return {String} the unicode comment, null otherwise.
  3346. */
  3347. findExtraFieldUnicodeComment: function() {
  3348. var ucommentField = this.extraFields[0x6375];
  3349. if (ucommentField) {
  3350. var extraReader = new StringReader(ucommentField.value);
  3351. // wrong version
  3352. if (extraReader.readInt(1) !== 1) {
  3353. return null;
  3354. }
  3355. // the crc of the comment changed, this field is out of date.
  3356. if (jszipProto.crc32(this.fileComment) !== extraReader.readInt(4)) {
  3357. return null;
  3358. }
  3359. return jszipProto.utf8decode(extraReader.readString(ucommentField.length - 5));
  3360. }
  3361. return null;
  3362. }
  3363. };
  3364. module.exports = ZipEntry;
  3365. },{"./compressedObject":2,"./object":13,"./stringReader":15,"./utils":21}],24:[function(_dereq_,module,exports){
  3366. // Top level file is just a mixin of submodules & constants
  3367. 'use strict';
  3368. var assign = _dereq_('./lib/utils/common').assign;
  3369. var deflate = _dereq_('./lib/deflate');
  3370. var inflate = _dereq_('./lib/inflate');
  3371. var constants = _dereq_('./lib/zlib/constants');
  3372. var pako = {};
  3373. assign(pako, deflate, inflate, constants);
  3374. module.exports = pako;
  3375. },{"./lib/deflate":25,"./lib/inflate":26,"./lib/utils/common":27,"./lib/zlib/constants":30}],25:[function(_dereq_,module,exports){
  3376. 'use strict';
  3377. var zlib_deflate = _dereq_('./zlib/deflate.js');
  3378. var utils = _dereq_('./utils/common');
  3379. var strings = _dereq_('./utils/strings');
  3380. var msg = _dereq_('./zlib/messages');
  3381. var zstream = _dereq_('./zlib/zstream');
  3382. /* Public constants ==========================================================*/
  3383. /* ===========================================================================*/
  3384. var Z_NO_FLUSH = 0;
  3385. var Z_FINISH = 4;
  3386. var Z_OK = 0;
  3387. var Z_STREAM_END = 1;
  3388. var Z_DEFAULT_COMPRESSION = -1;
  3389. var Z_DEFAULT_STRATEGY = 0;
  3390. var Z_DEFLATED = 8;
  3391. /* ===========================================================================*/
  3392. /**
  3393. * class Deflate
  3394. *
  3395. * Generic JS-style wrapper for zlib calls. If you don't need
  3396. * streaming behaviour - use more simple functions: [[deflate]],
  3397. * [[deflateRaw]] and [[gzip]].
  3398. **/
  3399. /* internal
  3400. * Deflate.chunks -> Array
  3401. *
  3402. * Chunks of output data, if [[Deflate#onData]] not overriden.
  3403. **/
  3404. /**
  3405. * Deflate.result -> Uint8Array|Array
  3406. *
  3407. * Compressed result, generated by default [[Deflate#onData]]
  3408. * and [[Deflate#onEnd]] handlers. Filled after you push last chunk
  3409. * (call [[Deflate#push]] with `Z_FINISH` / `true` param).
  3410. **/
  3411. /**
  3412. * Deflate.err -> Number
  3413. *
  3414. * Error code after deflate finished. 0 (Z_OK) on success.
  3415. * You will not need it in real life, because deflate errors
  3416. * are possible only on wrong options or bad `onData` / `onEnd`
  3417. * custom handlers.
  3418. **/
  3419. /**
  3420. * Deflate.msg -> String
  3421. *
  3422. * Error message, if [[Deflate.err]] != 0
  3423. **/
  3424. /**
  3425. * new Deflate(options)
  3426. * - options (Object): zlib deflate options.
  3427. *
  3428. * Creates new deflator instance with specified params. Throws exception
  3429. * on bad params. Supported options:
  3430. *
  3431. * - `level`
  3432. * - `windowBits`
  3433. * - `memLevel`
  3434. * - `strategy`
  3435. *
  3436. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  3437. * for more information on these.
  3438. *
  3439. * Additional options, for internal needs:
  3440. *
  3441. * - `chunkSize` - size of generated data chunks (16K by default)
  3442. * - `raw` (Boolean) - do raw deflate
  3443. * - `gzip` (Boolean) - create gzip wrapper
  3444. * - `to` (String) - if equal to 'string', then result will be "binary string"
  3445. * (each char code [0..255])
  3446. * - `header` (Object) - custom header for gzip
  3447. * - `text` (Boolean) - true if compressed data believed to be text
  3448. * - `time` (Number) - modification time, unix timestamp
  3449. * - `os` (Number) - operation system code
  3450. * - `extra` (Array) - array of bytes with extra data (max 65536)
  3451. * - `name` (String) - file name (binary string)
  3452. * - `comment` (String) - comment (binary string)
  3453. * - `hcrc` (Boolean) - true if header crc should be added
  3454. *
  3455. * ##### Example:
  3456. *
  3457. * ```javascript
  3458. * var pako = require('pako')
  3459. * , chunk1 = Uint8Array([1,2,3,4,5,6,7,8,9])
  3460. * , chunk2 = Uint8Array([10,11,12,13,14,15,16,17,18,19]);
  3461. *
  3462. * var deflate = new pako.Deflate({ level: 3});
  3463. *
  3464. * deflate.push(chunk1, false);
  3465. * deflate.push(chunk2, true); // true -> last chunk
  3466. *
  3467. * if (deflate.err) { throw new Error(deflate.err); }
  3468. *
  3469. * console.log(deflate.result);
  3470. * ```
  3471. **/
  3472. var Deflate = function(options) {
  3473. this.options = utils.assign({
  3474. level: Z_DEFAULT_COMPRESSION,
  3475. method: Z_DEFLATED,
  3476. chunkSize: 16384,
  3477. windowBits: 15,
  3478. memLevel: 8,
  3479. strategy: Z_DEFAULT_STRATEGY,
  3480. to: ''
  3481. }, options || {});
  3482. var opt = this.options;
  3483. if (opt.raw && (opt.windowBits > 0)) {
  3484. opt.windowBits = -opt.windowBits;
  3485. }
  3486. else if (opt.gzip && (opt.windowBits > 0) && (opt.windowBits < 16)) {
  3487. opt.windowBits += 16;
  3488. }
  3489. this.err = 0; // error code, if happens (0 = Z_OK)
  3490. this.msg = ''; // error message
  3491. this.ended = false; // used to avoid multiple onEnd() calls
  3492. this.chunks = []; // chunks of compressed data
  3493. this.strm = new zstream();
  3494. this.strm.avail_out = 0;
  3495. var status = zlib_deflate.deflateInit2(
  3496. this.strm,
  3497. opt.level,
  3498. opt.method,
  3499. opt.windowBits,
  3500. opt.memLevel,
  3501. opt.strategy
  3502. );
  3503. if (status !== Z_OK) {
  3504. throw new Error(msg[status]);
  3505. }
  3506. if (opt.header) {
  3507. zlib_deflate.deflateSetHeader(this.strm, opt.header);
  3508. }
  3509. };
  3510. /**
  3511. * Deflate#push(data[, mode]) -> Boolean
  3512. * - data (Uint8Array|Array|String): input data. Strings will be converted to
  3513. * utf8 byte sequence.
  3514. * - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
  3515. * See constants. Skipped or `false` means Z_NO_FLUSH, `true` meansh Z_FINISH.
  3516. *
  3517. * Sends input data to deflate pipe, generating [[Deflate#onData]] calls with
  3518. * new compressed chunks. Returns `true` on success. The last data block must have
  3519. * mode Z_FINISH (or `true`). That flush internal pending buffers and call
  3520. * [[Deflate#onEnd]].
  3521. *
  3522. * On fail call [[Deflate#onEnd]] with error code and return false.
  3523. *
  3524. * We strongly recommend to use `Uint8Array` on input for best speed (output
  3525. * array format is detected automatically). Also, don't skip last param and always
  3526. * use the same type in your code (boolean or number). That will improve JS speed.
  3527. *
  3528. * For regular `Array`-s make sure all elements are [0..255].
  3529. *
  3530. * ##### Example
  3531. *
  3532. * ```javascript
  3533. * push(chunk, false); // push one of data chunks
  3534. * ...
  3535. * push(chunk, true); // push last chunk
  3536. * ```
  3537. **/
  3538. Deflate.prototype.push = function(data, mode) {
  3539. var strm = this.strm;
  3540. var chunkSize = this.options.chunkSize;
  3541. var status, _mode;
  3542. if (this.ended) { return false; }
  3543. _mode = (mode === ~~mode) ? mode : ((mode === true) ? Z_FINISH : Z_NO_FLUSH);
  3544. // Convert data if needed
  3545. if (typeof data === 'string') {
  3546. // If we need to compress text, change encoding to utf8.
  3547. strm.input = strings.string2buf(data);
  3548. } else {
  3549. strm.input = data;
  3550. }
  3551. strm.next_in = 0;
  3552. strm.avail_in = strm.input.length;
  3553. do {
  3554. if (strm.avail_out === 0) {
  3555. strm.output = new utils.Buf8(chunkSize);
  3556. strm.next_out = 0;
  3557. strm.avail_out = chunkSize;
  3558. }
  3559. status = zlib_deflate.deflate(strm, _mode); /* no bad return value */
  3560. if (status !== Z_STREAM_END && status !== Z_OK) {
  3561. this.onEnd(status);
  3562. this.ended = true;
  3563. return false;
  3564. }
  3565. if (strm.avail_out === 0 || (strm.avail_in === 0 && _mode === Z_FINISH)) {
  3566. if (this.options.to === 'string') {
  3567. this.onData(strings.buf2binstring(utils.shrinkBuf(strm.output, strm.next_out)));
  3568. } else {
  3569. this.onData(utils.shrinkBuf(strm.output, strm.next_out));
  3570. }
  3571. }
  3572. } while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== Z_STREAM_END);
  3573. // Finalize on the last chunk.
  3574. if (_mode === Z_FINISH) {
  3575. status = zlib_deflate.deflateEnd(this.strm);
  3576. this.onEnd(status);
  3577. this.ended = true;
  3578. return status === Z_OK;
  3579. }
  3580. return true;
  3581. };
  3582. /**
  3583. * Deflate#onData(chunk) -> Void
  3584. * - chunk (Uint8Array|Array|String): ouput data. Type of array depends
  3585. * on js engine support. When string output requested, each chunk
  3586. * will be string.
  3587. *
  3588. * By default, stores data blocks in `chunks[]` property and glue
  3589. * those in `onEnd`. Override this handler, if you need another behaviour.
  3590. **/
  3591. Deflate.prototype.onData = function(chunk) {
  3592. this.chunks.push(chunk);
  3593. };
  3594. /**
  3595. * Deflate#onEnd(status) -> Void
  3596. * - status (Number): deflate status. 0 (Z_OK) on success,
  3597. * other if not.
  3598. *
  3599. * Called once after you tell deflate that input stream complete
  3600. * or error happenned. By default - join collected chunks,
  3601. * free memory and fill `results` / `err` properties.
  3602. **/
  3603. Deflate.prototype.onEnd = function(status) {
  3604. // On success - join
  3605. if (status === Z_OK) {
  3606. if (this.options.to === 'string') {
  3607. this.result = this.chunks.join('');
  3608. } else {
  3609. this.result = utils.flattenChunks(this.chunks);
  3610. }
  3611. }
  3612. this.chunks = [];
  3613. this.err = status;
  3614. this.msg = this.strm.msg;
  3615. };
  3616. /**
  3617. * deflate(data[, options]) -> Uint8Array|Array|String
  3618. * - data (Uint8Array|Array|String): input data to compress.
  3619. * - options (Object): zlib deflate options.
  3620. *
  3621. * Compress `data` with deflate alrorythm and `options`.
  3622. *
  3623. * Supported options are:
  3624. *
  3625. * - level
  3626. * - windowBits
  3627. * - memLevel
  3628. * - strategy
  3629. *
  3630. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  3631. * for more information on these.
  3632. *
  3633. * Sugar (options):
  3634. *
  3635. * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
  3636. * negative windowBits implicitly.
  3637. * - `to` (String) - if equal to 'string', then result will be "binary string"
  3638. * (each char code [0..255])
  3639. *
  3640. * ##### Example:
  3641. *
  3642. * ```javascript
  3643. * var pako = require('pako')
  3644. * , data = Uint8Array([1,2,3,4,5,6,7,8,9]);
  3645. *
  3646. * console.log(pako.deflate(data));
  3647. * ```
  3648. **/
  3649. function deflate(input, options) {
  3650. var deflator = new Deflate(options);
  3651. deflator.push(input, true);
  3652. // That will never happens, if you don't cheat with options :)
  3653. if (deflator.err) { throw deflator.msg; }
  3654. return deflator.result;
  3655. }
  3656. /**
  3657. * deflateRaw(data[, options]) -> Uint8Array|Array|String
  3658. * - data (Uint8Array|Array|String): input data to compress.
  3659. * - options (Object): zlib deflate options.
  3660. *
  3661. * The same as [[deflate]], but creates raw data, without wrapper
  3662. * (header and adler32 crc).
  3663. **/
  3664. function deflateRaw(input, options) {
  3665. options = options || {};
  3666. options.raw = true;
  3667. return deflate(input, options);
  3668. }
  3669. /**
  3670. * gzip(data[, options]) -> Uint8Array|Array|String
  3671. * - data (Uint8Array|Array|String): input data to compress.
  3672. * - options (Object): zlib deflate options.
  3673. *
  3674. * The same as [[deflate]], but create gzip wrapper instead of
  3675. * deflate one.
  3676. **/
  3677. function gzip(input, options) {
  3678. options = options || {};
  3679. options.gzip = true;
  3680. return deflate(input, options);
  3681. }
  3682. exports.Deflate = Deflate;
  3683. exports.deflate = deflate;
  3684. exports.deflateRaw = deflateRaw;
  3685. exports.gzip = gzip;
  3686. },{"./utils/common":27,"./utils/strings":28,"./zlib/deflate.js":32,"./zlib/messages":37,"./zlib/zstream":39}],26:[function(_dereq_,module,exports){
  3687. 'use strict';
  3688. var zlib_inflate = _dereq_('./zlib/inflate.js');
  3689. var utils = _dereq_('./utils/common');
  3690. var strings = _dereq_('./utils/strings');
  3691. var c = _dereq_('./zlib/constants');
  3692. var msg = _dereq_('./zlib/messages');
  3693. var zstream = _dereq_('./zlib/zstream');
  3694. var gzheader = _dereq_('./zlib/gzheader');
  3695. /**
  3696. * class Inflate
  3697. *
  3698. * Generic JS-style wrapper for zlib calls. If you don't need
  3699. * streaming behaviour - use more simple functions: [[inflate]]
  3700. * and [[inflateRaw]].
  3701. **/
  3702. /* internal
  3703. * inflate.chunks -> Array
  3704. *
  3705. * Chunks of output data, if [[Inflate#onData]] not overriden.
  3706. **/
  3707. /**
  3708. * Inflate.result -> Uint8Array|Array|String
  3709. *
  3710. * Uncompressed result, generated by default [[Inflate#onData]]
  3711. * and [[Inflate#onEnd]] handlers. Filled after you push last chunk
  3712. * (call [[Inflate#push]] with `Z_FINISH` / `true` param).
  3713. **/
  3714. /**
  3715. * Inflate.err -> Number
  3716. *
  3717. * Error code after inflate finished. 0 (Z_OK) on success.
  3718. * Should be checked if broken data possible.
  3719. **/
  3720. /**
  3721. * Inflate.msg -> String
  3722. *
  3723. * Error message, if [[Inflate.err]] != 0
  3724. **/
  3725. /**
  3726. * new Inflate(options)
  3727. * - options (Object): zlib inflate options.
  3728. *
  3729. * Creates new inflator instance with specified params. Throws exception
  3730. * on bad params. Supported options:
  3731. *
  3732. * - `windowBits`
  3733. *
  3734. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  3735. * for more information on these.
  3736. *
  3737. * Additional options, for internal needs:
  3738. *
  3739. * - `chunkSize` - size of generated data chunks (16K by default)
  3740. * - `raw` (Boolean) - do raw inflate
  3741. * - `to` (String) - if equal to 'string', then result will be converted
  3742. * from utf8 to utf16 (javascript) string. When string output requested,
  3743. * chunk length can differ from `chunkSize`, depending on content.
  3744. *
  3745. * By default, when no options set, autodetect deflate/gzip data format via
  3746. * wrapper header.
  3747. *
  3748. * ##### Example:
  3749. *
  3750. * ```javascript
  3751. * var pako = require('pako')
  3752. * , chunk1 = Uint8Array([1,2,3,4,5,6,7,8,9])
  3753. * , chunk2 = Uint8Array([10,11,12,13,14,15,16,17,18,19]);
  3754. *
  3755. * var inflate = new pako.Inflate({ level: 3});
  3756. *
  3757. * inflate.push(chunk1, false);
  3758. * inflate.push(chunk2, true); // true -> last chunk
  3759. *
  3760. * if (inflate.err) { throw new Error(inflate.err); }
  3761. *
  3762. * console.log(inflate.result);
  3763. * ```
  3764. **/
  3765. var Inflate = function(options) {
  3766. this.options = utils.assign({
  3767. chunkSize: 16384,
  3768. windowBits: 0,
  3769. to: ''
  3770. }, options || {});
  3771. var opt = this.options;
  3772. // Force window size for `raw` data, if not set directly,
  3773. // because we have no header for autodetect.
  3774. if (opt.raw && (opt.windowBits >= 0) && (opt.windowBits < 16)) {
  3775. opt.windowBits = -opt.windowBits;
  3776. if (opt.windowBits === 0) { opt.windowBits = -15; }
  3777. }
  3778. // If `windowBits` not defined (and mode not raw) - set autodetect flag for gzip/deflate
  3779. if ((opt.windowBits >= 0) && (opt.windowBits < 16) &&
  3780. !(options && options.windowBits)) {
  3781. opt.windowBits += 32;
  3782. }
  3783. // Gzip header has no info about windows size, we can do autodetect only
  3784. // for deflate. So, if window size not set, force it to max when gzip possible
  3785. if ((opt.windowBits > 15) && (opt.windowBits < 48)) {
  3786. // bit 3 (16) -> gzipped data
  3787. // bit 4 (32) -> autodetect gzip/deflate
  3788. if ((opt.windowBits & 15) === 0) {
  3789. opt.windowBits |= 15;
  3790. }
  3791. }
  3792. this.err = 0; // error code, if happens (0 = Z_OK)
  3793. this.msg = ''; // error message
  3794. this.ended = false; // used to avoid multiple onEnd() calls
  3795. this.chunks = []; // chunks of compressed data
  3796. this.strm = new zstream();
  3797. this.strm.avail_out = 0;
  3798. var status = zlib_inflate.inflateInit2(
  3799. this.strm,
  3800. opt.windowBits
  3801. );
  3802. if (status !== c.Z_OK) {
  3803. throw new Error(msg[status]);
  3804. }
  3805. this.header = new gzheader();
  3806. zlib_inflate.inflateGetHeader(this.strm, this.header);
  3807. };
  3808. /**
  3809. * Inflate#push(data[, mode]) -> Boolean
  3810. * - data (Uint8Array|Array|String): input data
  3811. * - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
  3812. * See constants. Skipped or `false` means Z_NO_FLUSH, `true` meansh Z_FINISH.
  3813. *
  3814. * Sends input data to inflate pipe, generating [[Inflate#onData]] calls with
  3815. * new output chunks. Returns `true` on success. The last data block must have
  3816. * mode Z_FINISH (or `true`). That flush internal pending buffers and call
  3817. * [[Inflate#onEnd]].
  3818. *
  3819. * On fail call [[Inflate#onEnd]] with error code and return false.
  3820. *
  3821. * We strongly recommend to use `Uint8Array` on input for best speed (output
  3822. * format is detected automatically). Also, don't skip last param and always
  3823. * use the same type in your code (boolean or number). That will improve JS speed.
  3824. *
  3825. * For regular `Array`-s make sure all elements are [0..255].
  3826. *
  3827. * ##### Example
  3828. *
  3829. * ```javascript
  3830. * push(chunk, false); // push one of data chunks
  3831. * ...
  3832. * push(chunk, true); // push last chunk
  3833. * ```
  3834. **/
  3835. Inflate.prototype.push = function(data, mode) {
  3836. var strm = this.strm;
  3837. var chunkSize = this.options.chunkSize;
  3838. var status, _mode;
  3839. var next_out_utf8, tail, utf8str;
  3840. if (this.ended) { return false; }
  3841. _mode = (mode === ~~mode) ? mode : ((mode === true) ? c.Z_FINISH : c.Z_NO_FLUSH);
  3842. // Convert data if needed
  3843. if (typeof data === 'string') {
  3844. // Only binary strings can be decompressed on practice
  3845. strm.input = strings.binstring2buf(data);
  3846. } else {
  3847. strm.input = data;
  3848. }
  3849. strm.next_in = 0;
  3850. strm.avail_in = strm.input.length;
  3851. do {
  3852. if (strm.avail_out === 0) {
  3853. strm.output = new utils.Buf8(chunkSize);
  3854. strm.next_out = 0;
  3855. strm.avail_out = chunkSize;
  3856. }
  3857. status = zlib_inflate.inflate(strm, c.Z_NO_FLUSH); /* no bad return value */
  3858. if (status !== c.Z_STREAM_END && status !== c.Z_OK) {
  3859. this.onEnd(status);
  3860. this.ended = true;
  3861. return false;
  3862. }
  3863. if (strm.next_out) {
  3864. if (strm.avail_out === 0 || status === c.Z_STREAM_END || (strm.avail_in === 0 && _mode === c.Z_FINISH)) {
  3865. if (this.options.to === 'string') {
  3866. next_out_utf8 = strings.utf8border(strm.output, strm.next_out);
  3867. tail = strm.next_out - next_out_utf8;
  3868. utf8str = strings.buf2string(strm.output, next_out_utf8);
  3869. // move tail
  3870. strm.next_out = tail;
  3871. strm.avail_out = chunkSize - tail;
  3872. if (tail) { utils.arraySet(strm.output, strm.output, next_out_utf8, tail, 0); }
  3873. this.onData(utf8str);
  3874. } else {
  3875. this.onData(utils.shrinkBuf(strm.output, strm.next_out));
  3876. }
  3877. }
  3878. }
  3879. } while ((strm.avail_in > 0) && status !== c.Z_STREAM_END);
  3880. if (status === c.Z_STREAM_END) {
  3881. _mode = c.Z_FINISH;
  3882. }
  3883. // Finalize on the last chunk.
  3884. if (_mode === c.Z_FINISH) {
  3885. status = zlib_inflate.inflateEnd(this.strm);
  3886. this.onEnd(status);
  3887. this.ended = true;
  3888. return status === c.Z_OK;
  3889. }
  3890. return true;
  3891. };
  3892. /**
  3893. * Inflate#onData(chunk) -> Void
  3894. * - chunk (Uint8Array|Array|String): ouput data. Type of array depends
  3895. * on js engine support. When string output requested, each chunk
  3896. * will be string.
  3897. *
  3898. * By default, stores data blocks in `chunks[]` property and glue
  3899. * those in `onEnd`. Override this handler, if you need another behaviour.
  3900. **/
  3901. Inflate.prototype.onData = function(chunk) {
  3902. this.chunks.push(chunk);
  3903. };
  3904. /**
  3905. * Inflate#onEnd(status) -> Void
  3906. * - status (Number): inflate status. 0 (Z_OK) on success,
  3907. * other if not.
  3908. *
  3909. * Called once after you tell inflate that input stream complete
  3910. * or error happenned. By default - join collected chunks,
  3911. * free memory and fill `results` / `err` properties.
  3912. **/
  3913. Inflate.prototype.onEnd = function(status) {
  3914. // On success - join
  3915. if (status === c.Z_OK) {
  3916. if (this.options.to === 'string') {
  3917. // Glue & convert here, until we teach pako to send
  3918. // utf8 alligned strings to onData
  3919. this.result = this.chunks.join('');
  3920. } else {
  3921. this.result = utils.flattenChunks(this.chunks);
  3922. }
  3923. }
  3924. this.chunks = [];
  3925. this.err = status;
  3926. this.msg = this.strm.msg;
  3927. };
  3928. /**
  3929. * inflate(data[, options]) -> Uint8Array|Array|String
  3930. * - data (Uint8Array|Array|String): input data to decompress.
  3931. * - options (Object): zlib inflate options.
  3932. *
  3933. * Decompress `data` with inflate/ungzip and `options`. Autodetect
  3934. * format via wrapper header by default. That's why we don't provide
  3935. * separate `ungzip` method.
  3936. *
  3937. * Supported options are:
  3938. *
  3939. * - windowBits
  3940. *
  3941. * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
  3942. * for more information.
  3943. *
  3944. * Sugar (options):
  3945. *
  3946. * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
  3947. * negative windowBits implicitly.
  3948. * - `to` (String) - if equal to 'string', then result will be converted
  3949. * from utf8 to utf16 (javascript) string. When string output requested,
  3950. * chunk length can differ from `chunkSize`, depending on content.
  3951. *
  3952. *
  3953. * ##### Example:
  3954. *
  3955. * ```javascript
  3956. * var pako = require('pako')
  3957. * , input = pako.deflate([1,2,3,4,5,6,7,8,9])
  3958. * , output;
  3959. *
  3960. * try {
  3961. * output = pako.inflate(input);
  3962. * } catch (err)
  3963. * console.log(err);
  3964. * }
  3965. * ```
  3966. **/
  3967. function inflate(input, options) {
  3968. var inflator = new Inflate(options);
  3969. inflator.push(input, true);
  3970. // That will never happens, if you don't cheat with options :)
  3971. if (inflator.err) { throw inflator.msg; }
  3972. return inflator.result;
  3973. }
  3974. /**
  3975. * inflateRaw(data[, options]) -> Uint8Array|Array|String
  3976. * - data (Uint8Array|Array|String): input data to decompress.
  3977. * - options (Object): zlib inflate options.
  3978. *
  3979. * The same as [[inflate]], but creates raw data, without wrapper
  3980. * (header and adler32 crc).
  3981. **/
  3982. function inflateRaw(input, options) {
  3983. options = options || {};
  3984. options.raw = true;
  3985. return inflate(input, options);
  3986. }
  3987. /**
  3988. * ungzip(data[, options]) -> Uint8Array|Array|String
  3989. * - data (Uint8Array|Array|String): input data to decompress.
  3990. * - options (Object): zlib inflate options.
  3991. *
  3992. * Just shortcut to [[inflate]], because it autodetects format
  3993. * by header.content. Done for convenience.
  3994. **/
  3995. exports.Inflate = Inflate;
  3996. exports.inflate = inflate;
  3997. exports.inflateRaw = inflateRaw;
  3998. exports.ungzip = inflate;
  3999. },{"./utils/common":27,"./utils/strings":28,"./zlib/constants":30,"./zlib/gzheader":33,"./zlib/inflate.js":35,"./zlib/messages":37,"./zlib/zstream":39}],27:[function(_dereq_,module,exports){
  4000. 'use strict';
  4001. var TYPED_OK = (typeof Uint8Array !== 'undefined') &&
  4002. (typeof Uint16Array !== 'undefined') &&
  4003. (typeof Int32Array !== 'undefined');
  4004. exports.assign = function (obj /*from1, from2, from3, ...*/) {
  4005. var sources = Array.prototype.slice.call(arguments, 1);
  4006. while (sources.length) {
  4007. var source = sources.shift();
  4008. if (!source) { continue; }
  4009. if (typeof(source) !== 'object') {
  4010. throw new TypeError(source + 'must be non-object');
  4011. }
  4012. for (var p in source) {
  4013. if (source.hasOwnProperty(p)) {
  4014. obj[p] = source[p];
  4015. }
  4016. }
  4017. }
  4018. return obj;
  4019. };
  4020. // reduce buffer size, avoiding mem copy
  4021. exports.shrinkBuf = function (buf, size) {
  4022. if (buf.length === size) { return buf; }
  4023. if (buf.subarray) { return buf.subarray(0, size); }
  4024. buf.length = size;
  4025. return buf;
  4026. };
  4027. var fnTyped = {
  4028. arraySet: function (dest, src, src_offs, len, dest_offs) {
  4029. if (src.subarray && dest.subarray) {
  4030. dest.set(src.subarray(src_offs, src_offs+len), dest_offs);
  4031. return;
  4032. }
  4033. // Fallback to ordinary array
  4034. for(var i=0; i<len; i++) {
  4035. dest[dest_offs + i] = src[src_offs + i];
  4036. }
  4037. },
  4038. // Join array of chunks to single array.
  4039. flattenChunks: function(chunks) {
  4040. var i, l, len, pos, chunk, result;
  4041. // calculate data length
  4042. len = 0;
  4043. for (i=0, l=chunks.length; i<l; i++) {
  4044. len += chunks[i].length;
  4045. }
  4046. // join chunks
  4047. result = new Uint8Array(len);
  4048. pos = 0;
  4049. for (i=0, l=chunks.length; i<l; i++) {
  4050. chunk = chunks[i];
  4051. result.set(chunk, pos);
  4052. pos += chunk.length;
  4053. }
  4054. return result;
  4055. }
  4056. };
  4057. var fnUntyped = {
  4058. arraySet: function (dest, src, src_offs, len, dest_offs) {
  4059. for(var i=0; i<len; i++) {
  4060. dest[dest_offs + i] = src[src_offs + i];
  4061. }
  4062. },
  4063. // Join array of chunks to single array.
  4064. flattenChunks: function(chunks) {
  4065. return [].concat.apply([], chunks);
  4066. }
  4067. };
  4068. // Enable/Disable typed arrays use, for testing
  4069. //
  4070. exports.setTyped = function (on) {
  4071. if (on) {
  4072. exports.Buf8 = Uint8Array;
  4073. exports.Buf16 = Uint16Array;
  4074. exports.Buf32 = Int32Array;
  4075. exports.assign(exports, fnTyped);
  4076. } else {
  4077. exports.Buf8 = Array;
  4078. exports.Buf16 = Array;
  4079. exports.Buf32 = Array;
  4080. exports.assign(exports, fnUntyped);
  4081. }
  4082. };
  4083. exports.setTyped(TYPED_OK);
  4084. },{}],28:[function(_dereq_,module,exports){
  4085. // String encode/decode helpers
  4086. 'use strict';
  4087. var utils = _dereq_('./common');
  4088. // Quick check if we can use fast array to bin string conversion
  4089. //
  4090. // - apply(Array) can fail on Android 2.2
  4091. // - apply(Uint8Array) can fail on iOS 5.1 Safary
  4092. //
  4093. var STR_APPLY_OK = true;
  4094. var STR_APPLY_UIA_OK = true;
  4095. try { String.fromCharCode.apply(null, [0]); } catch(__) { STR_APPLY_OK = false; }
  4096. try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch(__) { STR_APPLY_UIA_OK = false; }
  4097. // Table with utf8 lengths (calculated by first byte of sequence)
  4098. // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
  4099. // because max possible codepoint is 0x10ffff
  4100. var _utf8len = new utils.Buf8(256);
  4101. for (var i=0; i<256; i++) {
  4102. _utf8len[i] = (i >= 252 ? 6 : i >= 248 ? 5 : i >= 240 ? 4 : i >= 224 ? 3 : i >= 192 ? 2 : 1);
  4103. }
  4104. _utf8len[254]=_utf8len[254]=1; // Invalid sequence start
  4105. // convert string to array (typed, when possible)
  4106. exports.string2buf = function (str) {
  4107. var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
  4108. // count binary size
  4109. for (m_pos = 0; m_pos < str_len; m_pos++) {
  4110. c = str.charCodeAt(m_pos);
  4111. if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
  4112. c2 = str.charCodeAt(m_pos+1);
  4113. if ((c2 & 0xfc00) === 0xdc00) {
  4114. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  4115. m_pos++;
  4116. }
  4117. }
  4118. buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
  4119. }
  4120. // allocate buffer
  4121. buf = new utils.Buf8(buf_len);
  4122. // convert
  4123. for (i=0, m_pos = 0; i < buf_len; m_pos++) {
  4124. c = str.charCodeAt(m_pos);
  4125. if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
  4126. c2 = str.charCodeAt(m_pos+1);
  4127. if ((c2 & 0xfc00) === 0xdc00) {
  4128. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  4129. m_pos++;
  4130. }
  4131. }
  4132. if (c < 0x80) {
  4133. /* one byte */
  4134. buf[i++] = c;
  4135. } else if (c < 0x800) {
  4136. /* two bytes */
  4137. buf[i++] = 0xC0 | (c >>> 6);
  4138. buf[i++] = 0x80 | (c & 0x3f);
  4139. } else if (c < 0x10000) {
  4140. /* three bytes */
  4141. buf[i++] = 0xE0 | (c >>> 12);
  4142. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  4143. buf[i++] = 0x80 | (c & 0x3f);
  4144. } else {
  4145. /* four bytes */
  4146. buf[i++] = 0xf0 | (c >>> 18);
  4147. buf[i++] = 0x80 | (c >>> 12 & 0x3f);
  4148. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  4149. buf[i++] = 0x80 | (c & 0x3f);
  4150. }
  4151. }
  4152. return buf;
  4153. };
  4154. // Helper (used in 2 places)
  4155. function buf2binstring(buf, len) {
  4156. // use fallback for big arrays to avoid stack overflow
  4157. if (len < 65537) {
  4158. if ((buf.subarray && STR_APPLY_UIA_OK) || (!buf.subarray && STR_APPLY_OK)) {
  4159. return String.fromCharCode.apply(null, utils.shrinkBuf(buf, len));
  4160. }
  4161. }
  4162. var result = '';
  4163. for(var i=0; i < len; i++) {
  4164. result += String.fromCharCode(buf[i]);
  4165. }
  4166. return result;
  4167. }
  4168. // Convert byte array to binary string
  4169. exports.buf2binstring = function(buf) {
  4170. return buf2binstring(buf, buf.length);
  4171. };
  4172. // Convert binary string (typed, when possible)
  4173. exports.binstring2buf = function(str) {
  4174. var buf = new utils.Buf8(str.length);
  4175. for(var i=0, len=buf.length; i < len; i++) {
  4176. buf[i] = str.charCodeAt(i);
  4177. }
  4178. return buf;
  4179. };
  4180. // convert array to string
  4181. exports.buf2string = function (buf, max) {
  4182. var i, out, c, c_len;
  4183. var len = max || buf.length;
  4184. // Reserve max possible length (2 words per char)
  4185. // NB: by unknown reasons, Array is significantly faster for
  4186. // String.fromCharCode.apply than Uint16Array.
  4187. var utf16buf = new Array(len*2);
  4188. for (out=0, i=0; i<len;) {
  4189. c = buf[i++];
  4190. // quick process ascii
  4191. if (c < 0x80) { utf16buf[out++] = c; continue; }
  4192. c_len = _utf8len[c];
  4193. // skip 5 & 6 byte codes
  4194. if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len-1; continue; }
  4195. // apply mask on first byte
  4196. c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
  4197. // join the rest
  4198. while (c_len > 1 && i < len) {
  4199. c = (c << 6) | (buf[i++] & 0x3f);
  4200. c_len--;
  4201. }
  4202. // terminated by end of string?
  4203. if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
  4204. if (c < 0x10000) {
  4205. utf16buf[out++] = c;
  4206. } else {
  4207. c -= 0x10000;
  4208. utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
  4209. utf16buf[out++] = 0xdc00 | (c & 0x3ff);
  4210. }
  4211. }
  4212. return buf2binstring(utf16buf, out);
  4213. };
  4214. // Calculate max possible position in utf8 buffer,
  4215. // that will not break sequence. If that's not possible
  4216. // - (very small limits) return max size as is.
  4217. //
  4218. // buf[] - utf8 bytes array
  4219. // max - length limit (mandatory);
  4220. exports.utf8border = function(buf, max) {
  4221. var pos;
  4222. max = max || buf.length;
  4223. if (max > buf.length) { max = buf.length; }
  4224. // go back from last position, until start of sequence found
  4225. pos = max-1;
  4226. while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
  4227. // Fuckup - very small and broken sequence,
  4228. // return max, because we should return something anyway.
  4229. if (pos < 0) { return max; }
  4230. // If we came to start of buffer - that means vuffer is too small,
  4231. // return max too.
  4232. if (pos === 0) { return max; }
  4233. return (pos + _utf8len[buf[pos]] > max) ? pos : max;
  4234. };
  4235. },{"./common":27}],29:[function(_dereq_,module,exports){
  4236. 'use strict';
  4237. // Note: adler32 takes 12% for level 0 and 2% for level 6.
  4238. // It doesn't worth to make additional optimizationa as in original.
  4239. // Small size is preferable.
  4240. function adler32(adler, buf, len, pos) {
  4241. var s1 = (adler & 0xffff) |0
  4242. , s2 = ((adler >>> 16) & 0xffff) |0
  4243. , n = 0;
  4244. while (len !== 0) {
  4245. // Set limit ~ twice less than 5552, to keep
  4246. // s2 in 31-bits, because we force signed ints.
  4247. // in other case %= will fail.
  4248. n = len > 2000 ? 2000 : len;
  4249. len -= n;
  4250. do {
  4251. s1 = (s1 + buf[pos++]) |0;
  4252. s2 = (s2 + s1) |0;
  4253. } while (--n);
  4254. s1 %= 65521;
  4255. s2 %= 65521;
  4256. }
  4257. return (s1 | (s2 << 16)) |0;
  4258. }
  4259. module.exports = adler32;
  4260. },{}],30:[function(_dereq_,module,exports){
  4261. module.exports = {
  4262. /* Allowed flush values; see deflate() and inflate() below for details */
  4263. Z_NO_FLUSH: 0,
  4264. Z_PARTIAL_FLUSH: 1,
  4265. Z_SYNC_FLUSH: 2,
  4266. Z_FULL_FLUSH: 3,
  4267. Z_FINISH: 4,
  4268. Z_BLOCK: 5,
  4269. Z_TREES: 6,
  4270. /* Return codes for the compression/decompression functions. Negative values
  4271. * are errors, positive values are used for special but normal events.
  4272. */
  4273. Z_OK: 0,
  4274. Z_STREAM_END: 1,
  4275. Z_NEED_DICT: 2,
  4276. Z_ERRNO: -1,
  4277. Z_STREAM_ERROR: -2,
  4278. Z_DATA_ERROR: -3,
  4279. //Z_MEM_ERROR: -4,
  4280. Z_BUF_ERROR: -5,
  4281. //Z_VERSION_ERROR: -6,
  4282. /* compression levels */
  4283. Z_NO_COMPRESSION: 0,
  4284. Z_BEST_SPEED: 1,
  4285. Z_BEST_COMPRESSION: 9,
  4286. Z_DEFAULT_COMPRESSION: -1,
  4287. Z_FILTERED: 1,
  4288. Z_HUFFMAN_ONLY: 2,
  4289. Z_RLE: 3,
  4290. Z_FIXED: 4,
  4291. Z_DEFAULT_STRATEGY: 0,
  4292. /* Possible values of the data_type field (though see inflate()) */
  4293. Z_BINARY: 0,
  4294. Z_TEXT: 1,
  4295. //Z_ASCII: 1, // = Z_TEXT (deprecated)
  4296. Z_UNKNOWN: 2,
  4297. /* The deflate compression method */
  4298. Z_DEFLATED: 8
  4299. //Z_NULL: null // Use -1 or null inline, depending on var type
  4300. };
  4301. },{}],31:[function(_dereq_,module,exports){
  4302. 'use strict';
  4303. // Note: we can't get significant speed boost here.
  4304. // So write code to minimize size - no pregenerated tables
  4305. // and array tools dependencies.
  4306. // Use ordinary array, since untyped makes no boost here
  4307. function makeTable() {
  4308. var c, table = [];
  4309. for(var n =0; n < 256; n++){
  4310. c = n;
  4311. for(var k =0; k < 8; k++){
  4312. c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
  4313. }
  4314. table[n] = c;
  4315. }
  4316. return table;
  4317. }
  4318. // Create table on load. Just 255 signed longs. Not a problem.
  4319. var crcTable = makeTable();
  4320. function crc32(crc, buf, len, pos) {
  4321. var t = crcTable
  4322. , end = pos + len;
  4323. crc = crc ^ (-1);
  4324. for (var i = pos; i < end; i++ ) {
  4325. crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF];
  4326. }
  4327. return (crc ^ (-1)); // >>> 0;
  4328. }
  4329. module.exports = crc32;
  4330. },{}],32:[function(_dereq_,module,exports){
  4331. 'use strict';
  4332. var utils = _dereq_('../utils/common');
  4333. var trees = _dereq_('./trees');
  4334. var adler32 = _dereq_('./adler32');
  4335. var crc32 = _dereq_('./crc32');
  4336. var msg = _dereq_('./messages');
  4337. /* Public constants ==========================================================*/
  4338. /* ===========================================================================*/
  4339. /* Allowed flush values; see deflate() and inflate() below for details */
  4340. var Z_NO_FLUSH = 0;
  4341. var Z_PARTIAL_FLUSH = 1;
  4342. //var Z_SYNC_FLUSH = 2;
  4343. var Z_FULL_FLUSH = 3;
  4344. var Z_FINISH = 4;
  4345. var Z_BLOCK = 5;
  4346. //var Z_TREES = 6;
  4347. /* Return codes for the compression/decompression functions. Negative values
  4348. * are errors, positive values are used for special but normal events.
  4349. */
  4350. var Z_OK = 0;
  4351. var Z_STREAM_END = 1;
  4352. //var Z_NEED_DICT = 2;
  4353. //var Z_ERRNO = -1;
  4354. var Z_STREAM_ERROR = -2;
  4355. var Z_DATA_ERROR = -3;
  4356. //var Z_MEM_ERROR = -4;
  4357. var Z_BUF_ERROR = -5;
  4358. //var Z_VERSION_ERROR = -6;
  4359. /* compression levels */
  4360. //var Z_NO_COMPRESSION = 0;
  4361. //var Z_BEST_SPEED = 1;
  4362. //var Z_BEST_COMPRESSION = 9;
  4363. var Z_DEFAULT_COMPRESSION = -1;
  4364. var Z_FILTERED = 1;
  4365. var Z_HUFFMAN_ONLY = 2;
  4366. var Z_RLE = 3;
  4367. var Z_FIXED = 4;
  4368. var Z_DEFAULT_STRATEGY = 0;
  4369. /* Possible values of the data_type field (though see inflate()) */
  4370. //var Z_BINARY = 0;
  4371. //var Z_TEXT = 1;
  4372. //var Z_ASCII = 1; // = Z_TEXT
  4373. var Z_UNKNOWN = 2;
  4374. /* The deflate compression method */
  4375. var Z_DEFLATED = 8;
  4376. /*============================================================================*/
  4377. var MAX_MEM_LEVEL = 9;
  4378. /* Maximum value for memLevel in deflateInit2 */
  4379. var MAX_WBITS = 15;
  4380. /* 32K LZ77 window */
  4381. var DEF_MEM_LEVEL = 8;
  4382. var LENGTH_CODES = 29;
  4383. /* number of length codes, not counting the special END_BLOCK code */
  4384. var LITERALS = 256;
  4385. /* number of literal bytes 0..255 */
  4386. var L_CODES = LITERALS + 1 + LENGTH_CODES;
  4387. /* number of Literal or Length codes, including the END_BLOCK code */
  4388. var D_CODES = 30;
  4389. /* number of distance codes */
  4390. var BL_CODES = 19;
  4391. /* number of codes used to transfer the bit lengths */
  4392. var HEAP_SIZE = 2*L_CODES + 1;
  4393. /* maximum heap size */
  4394. var MAX_BITS = 15;
  4395. /* All codes must not exceed MAX_BITS bits */
  4396. var MIN_MATCH = 3;
  4397. var MAX_MATCH = 258;
  4398. var MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1);
  4399. var PRESET_DICT = 0x20;
  4400. var INIT_STATE = 42;
  4401. var EXTRA_STATE = 69;
  4402. var NAME_STATE = 73;
  4403. var COMMENT_STATE = 91;
  4404. var HCRC_STATE = 103;
  4405. var BUSY_STATE = 113;
  4406. var FINISH_STATE = 666;
  4407. var BS_NEED_MORE = 1; /* block not completed, need more input or more output */
  4408. var BS_BLOCK_DONE = 2; /* block flush performed */
  4409. var BS_FINISH_STARTED = 3; /* finish started, need only more output at next deflate */
  4410. var BS_FINISH_DONE = 4; /* finish done, accept no more input or output */
  4411. var OS_CODE = 0x03; // Unix :) . Don't detect, use this default.
  4412. function err(strm, errorCode) {
  4413. strm.msg = msg[errorCode];
  4414. return errorCode;
  4415. }
  4416. function rank(f) {
  4417. return ((f) << 1) - ((f) > 4 ? 9 : 0);
  4418. }
  4419. function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }
  4420. /* =========================================================================
  4421. * Flush as much pending output as possible. All deflate() output goes
  4422. * through this function so some applications may wish to modify it
  4423. * to avoid allocating a large strm->output buffer and copying into it.
  4424. * (See also read_buf()).
  4425. */
  4426. function flush_pending(strm) {
  4427. var s = strm.state;
  4428. //_tr_flush_bits(s);
  4429. var len = s.pending;
  4430. if (len > strm.avail_out) {
  4431. len = strm.avail_out;
  4432. }
  4433. if (len === 0) { return; }
  4434. utils.arraySet(strm.output, s.pending_buf, s.pending_out, len, strm.next_out);
  4435. strm.next_out += len;
  4436. s.pending_out += len;
  4437. strm.total_out += len;
  4438. strm.avail_out -= len;
  4439. s.pending -= len;
  4440. if (s.pending === 0) {
  4441. s.pending_out = 0;
  4442. }
  4443. }
  4444. function flush_block_only (s, last) {
  4445. trees._tr_flush_block(s, (s.block_start >= 0 ? s.block_start : -1), s.strstart - s.block_start, last);
  4446. s.block_start = s.strstart;
  4447. flush_pending(s.strm);
  4448. }
  4449. function put_byte(s, b) {
  4450. s.pending_buf[s.pending++] = b;
  4451. }
  4452. /* =========================================================================
  4453. * Put a short in the pending buffer. The 16-bit value is put in MSB order.
  4454. * IN assertion: the stream state is correct and there is enough room in
  4455. * pending_buf.
  4456. */
  4457. function putShortMSB(s, b) {
  4458. // put_byte(s, (Byte)(b >> 8));
  4459. // put_byte(s, (Byte)(b & 0xff));
  4460. s.pending_buf[s.pending++] = (b >>> 8) & 0xff;
  4461. s.pending_buf[s.pending++] = b & 0xff;
  4462. }
  4463. /* ===========================================================================
  4464. * Read a new buffer from the current input stream, update the adler32
  4465. * and total number of bytes read. All deflate() input goes through
  4466. * this function so some applications may wish to modify it to avoid
  4467. * allocating a large strm->input buffer and copying from it.
  4468. * (See also flush_pending()).
  4469. */
  4470. function read_buf(strm, buf, start, size) {
  4471. var len = strm.avail_in;
  4472. if (len > size) { len = size; }
  4473. if (len === 0) { return 0; }
  4474. strm.avail_in -= len;
  4475. utils.arraySet(buf, strm.input, strm.next_in, len, start);
  4476. if (strm.state.wrap === 1) {
  4477. strm.adler = adler32(strm.adler, buf, len, start);
  4478. }
  4479. else if (strm.state.wrap === 2) {
  4480. strm.adler = crc32(strm.adler, buf, len, start);
  4481. }
  4482. strm.next_in += len;
  4483. strm.total_in += len;
  4484. return len;
  4485. }
  4486. /* ===========================================================================
  4487. * Set match_start to the longest match starting at the given string and
  4488. * return its length. Matches shorter or equal to prev_length are discarded,
  4489. * in which case the result is equal to prev_length and match_start is
  4490. * garbage.
  4491. * IN assertions: cur_match is the head of the hash chain for the current
  4492. * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  4493. * OUT assertion: the match length is not greater than s->lookahead.
  4494. */
  4495. function longest_match(s, cur_match) {
  4496. var chain_length = s.max_chain_length; /* max hash chain length */
  4497. var scan = s.strstart; /* current string */
  4498. var match; /* matched string */
  4499. var len; /* length of current match */
  4500. var best_len = s.prev_length; /* best match length so far */
  4501. var nice_match = s.nice_match; /* stop if match long enough */
  4502. var limit = (s.strstart > (s.w_size - MIN_LOOKAHEAD)) ?
  4503. s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0/*NIL*/;
  4504. var _win = s.window; // shortcut
  4505. var wmask = s.w_mask;
  4506. var prev = s.prev;
  4507. /* Stop when cur_match becomes <= limit. To simplify the code,
  4508. * we prevent matches with the string of window index 0.
  4509. */
  4510. var strend = s.strstart + MAX_MATCH;
  4511. var scan_end1 = _win[scan + best_len - 1];
  4512. var scan_end = _win[scan + best_len];
  4513. /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  4514. * It is easy to get rid of this optimization if necessary.
  4515. */
  4516. // Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  4517. /* Do not waste too much time if we already have a good match: */
  4518. if (s.prev_length >= s.good_match) {
  4519. chain_length >>= 2;
  4520. }
  4521. /* Do not look for matches beyond the end of the input. This is necessary
  4522. * to make deflate deterministic.
  4523. */
  4524. if (nice_match > s.lookahead) { nice_match = s.lookahead; }
  4525. // Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  4526. do {
  4527. // Assert(cur_match < s->strstart, "no future");
  4528. match = cur_match;
  4529. /* Skip to next match if the match length cannot increase
  4530. * or if the match length is less than 2. Note that the checks below
  4531. * for insufficient lookahead only occur occasionally for performance
  4532. * reasons. Therefore uninitialized memory will be accessed, and
  4533. * conditional jumps will be made that depend on those values.
  4534. * However the length of the match is limited to the lookahead, so
  4535. * the output of deflate is not affected by the uninitialized values.
  4536. */
  4537. if (_win[match + best_len] !== scan_end ||
  4538. _win[match + best_len - 1] !== scan_end1 ||
  4539. _win[match] !== _win[scan] ||
  4540. _win[++match] !== _win[scan + 1]) {
  4541. continue;
  4542. }
  4543. /* The check at best_len-1 can be removed because it will be made
  4544. * again later. (This heuristic is not always a win.)
  4545. * It is not necessary to compare scan[2] and match[2] since they
  4546. * are always equal when the other bytes match, given that
  4547. * the hash keys are equal and that HASH_BITS >= 8.
  4548. */
  4549. scan += 2;
  4550. match++;
  4551. // Assert(*scan == *match, "match[2]?");
  4552. /* We check for insufficient lookahead only every 8th comparison;
  4553. * the 256th check will be made at strstart+258.
  4554. */
  4555. do {
  4556. /*jshint noempty:false*/
  4557. } while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  4558. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  4559. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  4560. _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
  4561. scan < strend);
  4562. // Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  4563. len = MAX_MATCH - (strend - scan);
  4564. scan = strend - MAX_MATCH;
  4565. if (len > best_len) {
  4566. s.match_start = cur_match;
  4567. best_len = len;
  4568. if (len >= nice_match) {
  4569. break;
  4570. }
  4571. scan_end1 = _win[scan + best_len - 1];
  4572. scan_end = _win[scan + best_len];
  4573. }
  4574. } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0);
  4575. if (best_len <= s.lookahead) {
  4576. return best_len;
  4577. }
  4578. return s.lookahead;
  4579. }
  4580. /* ===========================================================================
  4581. * Fill the window when the lookahead becomes insufficient.
  4582. * Updates strstart and lookahead.
  4583. *
  4584. * IN assertion: lookahead < MIN_LOOKAHEAD
  4585. * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  4586. * At least one byte has been read, or avail_in == 0; reads are
  4587. * performed for at least two bytes (required for the zip translate_eol
  4588. * option -- not supported here).
  4589. */
  4590. function fill_window(s) {
  4591. var _w_size = s.w_size;
  4592. var p, n, m, more, str;
  4593. //Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
  4594. do {
  4595. more = s.window_size - s.lookahead - s.strstart;
  4596. // JS ints have 32 bit, block below not needed
  4597. /* Deal with !@#$% 64K limit: */
  4598. //if (sizeof(int) <= 2) {
  4599. // if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
  4600. // more = wsize;
  4601. //
  4602. // } else if (more == (unsigned)(-1)) {
  4603. // /* Very unlikely, but possible on 16 bit machine if
  4604. // * strstart == 0 && lookahead == 1 (input done a byte at time)
  4605. // */
  4606. // more--;
  4607. // }
  4608. //}
  4609. /* If the window is almost full and there is insufficient lookahead,
  4610. * move the upper half to the lower one to make room in the upper half.
  4611. */
  4612. if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {
  4613. utils.arraySet(s.window, s.window, _w_size, _w_size, 0);
  4614. s.match_start -= _w_size;
  4615. s.strstart -= _w_size;
  4616. /* we now have strstart >= MAX_DIST */
  4617. s.block_start -= _w_size;
  4618. /* Slide the hash table (could be avoided with 32 bit values
  4619. at the expense of memory usage). We slide even when level == 0
  4620. to keep the hash table consistent if we switch back to level > 0
  4621. later. (Using level 0 permanently is not an optimal usage of
  4622. zlib, so we don't care about this pathological case.)
  4623. */
  4624. n = s.hash_size;
  4625. p = n;
  4626. do {
  4627. m = s.head[--p];
  4628. s.head[p] = (m >= _w_size ? m - _w_size : 0);
  4629. } while (--n);
  4630. n = _w_size;
  4631. p = n;
  4632. do {
  4633. m = s.prev[--p];
  4634. s.prev[p] = (m >= _w_size ? m - _w_size : 0);
  4635. /* If n is not on any hash chain, prev[n] is garbage but
  4636. * its value will never be used.
  4637. */
  4638. } while (--n);
  4639. more += _w_size;
  4640. }
  4641. if (s.strm.avail_in === 0) {
  4642. break;
  4643. }
  4644. /* If there was no sliding:
  4645. * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  4646. * more == window_size - lookahead - strstart
  4647. * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  4648. * => more >= window_size - 2*WSIZE + 2
  4649. * In the BIG_MEM or MMAP case (not yet supported),
  4650. * window_size == input_size + MIN_LOOKAHEAD &&
  4651. * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  4652. * Otherwise, window_size == 2*WSIZE so more >= 2.
  4653. * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  4654. */
  4655. //Assert(more >= 2, "more < 2");
  4656. n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more);
  4657. s.lookahead += n;
  4658. /* Initialize the hash value now that we have some input: */
  4659. if (s.lookahead + s.insert >= MIN_MATCH) {
  4660. str = s.strstart - s.insert;
  4661. s.ins_h = s.window[str];
  4662. /* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */
  4663. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + 1]) & s.hash_mask;
  4664. //#if MIN_MATCH != 3
  4665. // Call update_hash() MIN_MATCH-3 more times
  4666. //#endif
  4667. while (s.insert) {
  4668. /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
  4669. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH-1]) & s.hash_mask;
  4670. s.prev[str & s.w_mask] = s.head[s.ins_h];
  4671. s.head[s.ins_h] = str;
  4672. str++;
  4673. s.insert--;
  4674. if (s.lookahead + s.insert < MIN_MATCH) {
  4675. break;
  4676. }
  4677. }
  4678. }
  4679. /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  4680. * but this is not important since only literal bytes will be emitted.
  4681. */
  4682. } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0);
  4683. /* If the WIN_INIT bytes after the end of the current data have never been
  4684. * written, then zero those bytes in order to avoid memory check reports of
  4685. * the use of uninitialized (or uninitialised as Julian writes) bytes by
  4686. * the longest match routines. Update the high water mark for the next
  4687. * time through here. WIN_INIT is set to MAX_MATCH since the longest match
  4688. * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
  4689. */
  4690. // if (s.high_water < s.window_size) {
  4691. // var curr = s.strstart + s.lookahead;
  4692. // var init = 0;
  4693. //
  4694. // if (s.high_water < curr) {
  4695. // /* Previous high water mark below current data -- zero WIN_INIT
  4696. // * bytes or up to end of window, whichever is less.
  4697. // */
  4698. // init = s.window_size - curr;
  4699. // if (init > WIN_INIT)
  4700. // init = WIN_INIT;
  4701. // zmemzero(s->window + curr, (unsigned)init);
  4702. // s->high_water = curr + init;
  4703. // }
  4704. // else if (s->high_water < (ulg)curr + WIN_INIT) {
  4705. // /* High water mark at or above current data, but below current data
  4706. // * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
  4707. // * to end of window, whichever is less.
  4708. // */
  4709. // init = (ulg)curr + WIN_INIT - s->high_water;
  4710. // if (init > s->window_size - s->high_water)
  4711. // init = s->window_size - s->high_water;
  4712. // zmemzero(s->window + s->high_water, (unsigned)init);
  4713. // s->high_water += init;
  4714. // }
  4715. // }
  4716. //
  4717. // Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
  4718. // "not enough room for search");
  4719. }
  4720. /* ===========================================================================
  4721. * Copy without compression as much as possible from the input stream, return
  4722. * the current block state.
  4723. * This function does not insert new strings in the dictionary since
  4724. * uncompressible data is probably not useful. This function is used
  4725. * only for the level=0 compression option.
  4726. * NOTE: this function should be optimized to avoid extra copying from
  4727. * window to pending_buf.
  4728. */
  4729. function deflate_stored(s, flush) {
  4730. /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
  4731. * to pending_buf_size, and each stored block has a 5 byte header:
  4732. */
  4733. var max_block_size = 0xffff;
  4734. if (max_block_size > s.pending_buf_size - 5) {
  4735. max_block_size = s.pending_buf_size - 5;
  4736. }
  4737. /* Copy as much as possible from input to output: */
  4738. for (;;) {
  4739. /* Fill the window as much as possible: */
  4740. if (s.lookahead <= 1) {
  4741. //Assert(s->strstart < s->w_size+MAX_DIST(s) ||
  4742. // s->block_start >= (long)s->w_size, "slide too late");
  4743. // if (!(s.strstart < s.w_size + (s.w_size - MIN_LOOKAHEAD) ||
  4744. // s.block_start >= s.w_size)) {
  4745. // throw new Error("slide too late");
  4746. // }
  4747. fill_window(s);
  4748. if (s.lookahead === 0 && flush === Z_NO_FLUSH) {
  4749. return BS_NEED_MORE;
  4750. }
  4751. if (s.lookahead === 0) {
  4752. break;
  4753. }
  4754. /* flush the current block */
  4755. }
  4756. //Assert(s->block_start >= 0L, "block gone");
  4757. // if (s.block_start < 0) throw new Error("block gone");
  4758. s.strstart += s.lookahead;
  4759. s.lookahead = 0;
  4760. /* Emit a stored block if pending_buf will be full: */
  4761. var max_start = s.block_start + max_block_size;
  4762. if (s.strstart === 0 || s.strstart >= max_start) {
  4763. /* strstart == 0 is possible when wraparound on 16-bit machine */
  4764. s.lookahead = s.strstart - max_start;
  4765. s.strstart = max_start;
  4766. /*** FLUSH_BLOCK(s, 0); ***/
  4767. flush_block_only(s, false);
  4768. if (s.strm.avail_out === 0) {
  4769. return BS_NEED_MORE;
  4770. }
  4771. /***/
  4772. }
  4773. /* Flush if we may have to slide, otherwise block_start may become
  4774. * negative and the data will be gone:
  4775. */
  4776. if (s.strstart - s.block_start >= (s.w_size - MIN_LOOKAHEAD)) {
  4777. /*** FLUSH_BLOCK(s, 0); ***/
  4778. flush_block_only(s, false);
  4779. if (s.strm.avail_out === 0) {
  4780. return BS_NEED_MORE;
  4781. }
  4782. /***/
  4783. }
  4784. }
  4785. s.insert = 0;
  4786. if (flush === Z_FINISH) {
  4787. /*** FLUSH_BLOCK(s, 1); ***/
  4788. flush_block_only(s, true);
  4789. if (s.strm.avail_out === 0) {
  4790. return BS_FINISH_STARTED;
  4791. }
  4792. /***/
  4793. return BS_FINISH_DONE;
  4794. }
  4795. if (s.strstart > s.block_start) {
  4796. /*** FLUSH_BLOCK(s, 0); ***/
  4797. flush_block_only(s, false);
  4798. if (s.strm.avail_out === 0) {
  4799. return BS_NEED_MORE;
  4800. }
  4801. /***/
  4802. }
  4803. return BS_NEED_MORE;
  4804. }
  4805. /* ===========================================================================
  4806. * Compress as much as possible from the input stream, return the current
  4807. * block state.
  4808. * This function does not perform lazy evaluation of matches and inserts
  4809. * new strings in the dictionary only for unmatched strings or for short
  4810. * matches. It is used only for the fast compression options.
  4811. */
  4812. function deflate_fast(s, flush) {
  4813. var hash_head; /* head of the hash chain */
  4814. var bflush; /* set if current block must be flushed */
  4815. for (;;) {
  4816. /* Make sure that we always have enough lookahead, except
  4817. * at the end of the input file. We need MAX_MATCH bytes
  4818. * for the next match, plus MIN_MATCH bytes to insert the
  4819. * string following the next match.
  4820. */
  4821. if (s.lookahead < MIN_LOOKAHEAD) {
  4822. fill_window(s);
  4823. if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
  4824. return BS_NEED_MORE;
  4825. }
  4826. if (s.lookahead === 0) {
  4827. break; /* flush the current block */
  4828. }
  4829. }
  4830. /* Insert the string window[strstart .. strstart+2] in the
  4831. * dictionary, and set hash_head to the head of the hash chain:
  4832. */
  4833. hash_head = 0/*NIL*/;
  4834. if (s.lookahead >= MIN_MATCH) {
  4835. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  4836. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  4837. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  4838. s.head[s.ins_h] = s.strstart;
  4839. /***/
  4840. }
  4841. /* Find the longest match, discarding those <= prev_length.
  4842. * At this point we have always match_length < MIN_MATCH
  4843. */
  4844. if (hash_head !== 0/*NIL*/ && ((s.strstart - hash_head) <= (s.w_size - MIN_LOOKAHEAD))) {
  4845. /* To simplify the code, we prevent matches with the string
  4846. * of window index 0 (in particular we have to avoid a match
  4847. * of the string with itself at the start of the input file).
  4848. */
  4849. s.match_length = longest_match(s, hash_head);
  4850. /* longest_match() sets match_start */
  4851. }
  4852. if (s.match_length >= MIN_MATCH) {
  4853. // check_match(s, s.strstart, s.match_start, s.match_length); // for debug only
  4854. /*** _tr_tally_dist(s, s.strstart - s.match_start,
  4855. s.match_length - MIN_MATCH, bflush); ***/
  4856. bflush = trees._tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH);
  4857. s.lookahead -= s.match_length;
  4858. /* Insert new strings in the hash table only if the match length
  4859. * is not too large. This saves time but degrades compression.
  4860. */
  4861. if (s.match_length <= s.max_lazy_match/*max_insert_length*/ && s.lookahead >= MIN_MATCH) {
  4862. s.match_length--; /* string at strstart already in table */
  4863. do {
  4864. s.strstart++;
  4865. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  4866. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  4867. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  4868. s.head[s.ins_h] = s.strstart;
  4869. /***/
  4870. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  4871. * always MIN_MATCH bytes ahead.
  4872. */
  4873. } while (--s.match_length !== 0);
  4874. s.strstart++;
  4875. } else
  4876. {
  4877. s.strstart += s.match_length;
  4878. s.match_length = 0;
  4879. s.ins_h = s.window[s.strstart];
  4880. /* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */
  4881. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + 1]) & s.hash_mask;
  4882. //#if MIN_MATCH != 3
  4883. // Call UPDATE_HASH() MIN_MATCH-3 more times
  4884. //#endif
  4885. /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  4886. * matter since it will be recomputed at next deflate call.
  4887. */
  4888. }
  4889. } else {
  4890. /* No match, output a literal byte */
  4891. //Tracevv((stderr,"%c", s.window[s.strstart]));
  4892. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  4893. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  4894. s.lookahead--;
  4895. s.strstart++;
  4896. }
  4897. if (bflush) {
  4898. /*** FLUSH_BLOCK(s, 0); ***/
  4899. flush_block_only(s, false);
  4900. if (s.strm.avail_out === 0) {
  4901. return BS_NEED_MORE;
  4902. }
  4903. /***/
  4904. }
  4905. }
  4906. s.insert = ((s.strstart < (MIN_MATCH-1)) ? s.strstart : MIN_MATCH-1);
  4907. if (flush === Z_FINISH) {
  4908. /*** FLUSH_BLOCK(s, 1); ***/
  4909. flush_block_only(s, true);
  4910. if (s.strm.avail_out === 0) {
  4911. return BS_FINISH_STARTED;
  4912. }
  4913. /***/
  4914. return BS_FINISH_DONE;
  4915. }
  4916. if (s.last_lit) {
  4917. /*** FLUSH_BLOCK(s, 0); ***/
  4918. flush_block_only(s, false);
  4919. if (s.strm.avail_out === 0) {
  4920. return BS_NEED_MORE;
  4921. }
  4922. /***/
  4923. }
  4924. return BS_BLOCK_DONE;
  4925. }
  4926. /* ===========================================================================
  4927. * Same as above, but achieves better compression. We use a lazy
  4928. * evaluation for matches: a match is finally adopted only if there is
  4929. * no better match at the next window position.
  4930. */
  4931. function deflate_slow(s, flush) {
  4932. var hash_head; /* head of hash chain */
  4933. var bflush; /* set if current block must be flushed */
  4934. var max_insert;
  4935. /* Process the input block. */
  4936. for (;;) {
  4937. /* Make sure that we always have enough lookahead, except
  4938. * at the end of the input file. We need MAX_MATCH bytes
  4939. * for the next match, plus MIN_MATCH bytes to insert the
  4940. * string following the next match.
  4941. */
  4942. if (s.lookahead < MIN_LOOKAHEAD) {
  4943. fill_window(s);
  4944. if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
  4945. return BS_NEED_MORE;
  4946. }
  4947. if (s.lookahead === 0) { break; } /* flush the current block */
  4948. }
  4949. /* Insert the string window[strstart .. strstart+2] in the
  4950. * dictionary, and set hash_head to the head of the hash chain:
  4951. */
  4952. hash_head = 0/*NIL*/;
  4953. if (s.lookahead >= MIN_MATCH) {
  4954. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  4955. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  4956. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  4957. s.head[s.ins_h] = s.strstart;
  4958. /***/
  4959. }
  4960. /* Find the longest match, discarding those <= prev_length.
  4961. */
  4962. s.prev_length = s.match_length;
  4963. s.prev_match = s.match_start;
  4964. s.match_length = MIN_MATCH-1;
  4965. if (hash_head !== 0/*NIL*/ && s.prev_length < s.max_lazy_match &&
  4966. s.strstart - hash_head <= (s.w_size-MIN_LOOKAHEAD)/*MAX_DIST(s)*/) {
  4967. /* To simplify the code, we prevent matches with the string
  4968. * of window index 0 (in particular we have to avoid a match
  4969. * of the string with itself at the start of the input file).
  4970. */
  4971. s.match_length = longest_match(s, hash_head);
  4972. /* longest_match() sets match_start */
  4973. if (s.match_length <= 5 &&
  4974. (s.strategy === Z_FILTERED || (s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096/*TOO_FAR*/))) {
  4975. /* If prev_match is also MIN_MATCH, match_start is garbage
  4976. * but we will ignore the current match anyway.
  4977. */
  4978. s.match_length = MIN_MATCH-1;
  4979. }
  4980. }
  4981. /* If there was a match at the previous step and the current
  4982. * match is not better, output the previous match:
  4983. */
  4984. if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) {
  4985. max_insert = s.strstart + s.lookahead - MIN_MATCH;
  4986. /* Do not insert strings in hash table beyond this. */
  4987. //check_match(s, s.strstart-1, s.prev_match, s.prev_length);
  4988. /***_tr_tally_dist(s, s.strstart - 1 - s.prev_match,
  4989. s.prev_length - MIN_MATCH, bflush);***/
  4990. bflush = trees._tr_tally(s, s.strstart - 1- s.prev_match, s.prev_length - MIN_MATCH);
  4991. /* Insert in hash table all strings up to the end of the match.
  4992. * strstart-1 and strstart are already inserted. If there is not
  4993. * enough lookahead, the last two strings are not inserted in
  4994. * the hash table.
  4995. */
  4996. s.lookahead -= s.prev_length-1;
  4997. s.prev_length -= 2;
  4998. do {
  4999. if (++s.strstart <= max_insert) {
  5000. /*** INSERT_STRING(s, s.strstart, hash_head); ***/
  5001. s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH - 1]) & s.hash_mask;
  5002. hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
  5003. s.head[s.ins_h] = s.strstart;
  5004. /***/
  5005. }
  5006. } while (--s.prev_length !== 0);
  5007. s.match_available = 0;
  5008. s.match_length = MIN_MATCH-1;
  5009. s.strstart++;
  5010. if (bflush) {
  5011. /*** FLUSH_BLOCK(s, 0); ***/
  5012. flush_block_only(s, false);
  5013. if (s.strm.avail_out === 0) {
  5014. return BS_NEED_MORE;
  5015. }
  5016. /***/
  5017. }
  5018. } else if (s.match_available) {
  5019. /* If there was no match at the previous position, output a
  5020. * single literal. If there was a match but the current match
  5021. * is longer, truncate the previous match to a single literal.
  5022. */
  5023. //Tracevv((stderr,"%c", s->window[s->strstart-1]));
  5024. /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
  5025. bflush = trees._tr_tally(s, 0, s.window[s.strstart-1]);
  5026. if (bflush) {
  5027. /*** FLUSH_BLOCK_ONLY(s, 0) ***/
  5028. flush_block_only(s, false);
  5029. /***/
  5030. }
  5031. s.strstart++;
  5032. s.lookahead--;
  5033. if (s.strm.avail_out === 0) {
  5034. return BS_NEED_MORE;
  5035. }
  5036. } else {
  5037. /* There is no previous match to compare with, wait for
  5038. * the next step to decide.
  5039. */
  5040. s.match_available = 1;
  5041. s.strstart++;
  5042. s.lookahead--;
  5043. }
  5044. }
  5045. //Assert (flush != Z_NO_FLUSH, "no flush?");
  5046. if (s.match_available) {
  5047. //Tracevv((stderr,"%c", s->window[s->strstart-1]));
  5048. /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/
  5049. bflush = trees._tr_tally(s, 0, s.window[s.strstart-1]);
  5050. s.match_available = 0;
  5051. }
  5052. s.insert = s.strstart < MIN_MATCH-1 ? s.strstart : MIN_MATCH-1;
  5053. if (flush === Z_FINISH) {
  5054. /*** FLUSH_BLOCK(s, 1); ***/
  5055. flush_block_only(s, true);
  5056. if (s.strm.avail_out === 0) {
  5057. return BS_FINISH_STARTED;
  5058. }
  5059. /***/
  5060. return BS_FINISH_DONE;
  5061. }
  5062. if (s.last_lit) {
  5063. /*** FLUSH_BLOCK(s, 0); ***/
  5064. flush_block_only(s, false);
  5065. if (s.strm.avail_out === 0) {
  5066. return BS_NEED_MORE;
  5067. }
  5068. /***/
  5069. }
  5070. return BS_BLOCK_DONE;
  5071. }
  5072. /* ===========================================================================
  5073. * For Z_RLE, simply look for runs of bytes, generate matches only of distance
  5074. * one. Do not maintain a hash table. (It will be regenerated if this run of
  5075. * deflate switches away from Z_RLE.)
  5076. */
  5077. function deflate_rle(s, flush) {
  5078. var bflush; /* set if current block must be flushed */
  5079. var prev; /* byte at distance one to match */
  5080. var scan, strend; /* scan goes up to strend for length of run */
  5081. var _win = s.window;
  5082. for (;;) {
  5083. /* Make sure that we always have enough lookahead, except
  5084. * at the end of the input file. We need MAX_MATCH bytes
  5085. * for the longest run, plus one for the unrolled loop.
  5086. */
  5087. if (s.lookahead <= MAX_MATCH) {
  5088. fill_window(s);
  5089. if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH) {
  5090. return BS_NEED_MORE;
  5091. }
  5092. if (s.lookahead === 0) { break; } /* flush the current block */
  5093. }
  5094. /* See how many times the previous byte repeats */
  5095. s.match_length = 0;
  5096. if (s.lookahead >= MIN_MATCH && s.strstart > 0) {
  5097. scan = s.strstart - 1;
  5098. prev = _win[scan];
  5099. if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) {
  5100. strend = s.strstart + MAX_MATCH;
  5101. do {
  5102. /*jshint noempty:false*/
  5103. } while (prev === _win[++scan] && prev === _win[++scan] &&
  5104. prev === _win[++scan] && prev === _win[++scan] &&
  5105. prev === _win[++scan] && prev === _win[++scan] &&
  5106. prev === _win[++scan] && prev === _win[++scan] &&
  5107. scan < strend);
  5108. s.match_length = MAX_MATCH - (strend - scan);
  5109. if (s.match_length > s.lookahead) {
  5110. s.match_length = s.lookahead;
  5111. }
  5112. }
  5113. //Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
  5114. }
  5115. /* Emit match if have run of MIN_MATCH or longer, else emit literal */
  5116. if (s.match_length >= MIN_MATCH) {
  5117. //check_match(s, s.strstart, s.strstart - 1, s.match_length);
  5118. /*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/
  5119. bflush = trees._tr_tally(s, 1, s.match_length - MIN_MATCH);
  5120. s.lookahead -= s.match_length;
  5121. s.strstart += s.match_length;
  5122. s.match_length = 0;
  5123. } else {
  5124. /* No match, output a literal byte */
  5125. //Tracevv((stderr,"%c", s->window[s->strstart]));
  5126. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  5127. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  5128. s.lookahead--;
  5129. s.strstart++;
  5130. }
  5131. if (bflush) {
  5132. /*** FLUSH_BLOCK(s, 0); ***/
  5133. flush_block_only(s, false);
  5134. if (s.strm.avail_out === 0) {
  5135. return BS_NEED_MORE;
  5136. }
  5137. /***/
  5138. }
  5139. }
  5140. s.insert = 0;
  5141. if (flush === Z_FINISH) {
  5142. /*** FLUSH_BLOCK(s, 1); ***/
  5143. flush_block_only(s, true);
  5144. if (s.strm.avail_out === 0) {
  5145. return BS_FINISH_STARTED;
  5146. }
  5147. /***/
  5148. return BS_FINISH_DONE;
  5149. }
  5150. if (s.last_lit) {
  5151. /*** FLUSH_BLOCK(s, 0); ***/
  5152. flush_block_only(s, false);
  5153. if (s.strm.avail_out === 0) {
  5154. return BS_NEED_MORE;
  5155. }
  5156. /***/
  5157. }
  5158. return BS_BLOCK_DONE;
  5159. }
  5160. /* ===========================================================================
  5161. * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
  5162. * (It will be regenerated if this run of deflate switches away from Huffman.)
  5163. */
  5164. function deflate_huff(s, flush) {
  5165. var bflush; /* set if current block must be flushed */
  5166. for (;;) {
  5167. /* Make sure that we have a literal to write. */
  5168. if (s.lookahead === 0) {
  5169. fill_window(s);
  5170. if (s.lookahead === 0) {
  5171. if (flush === Z_NO_FLUSH) {
  5172. return BS_NEED_MORE;
  5173. }
  5174. break; /* flush the current block */
  5175. }
  5176. }
  5177. /* Output a literal byte */
  5178. s.match_length = 0;
  5179. //Tracevv((stderr,"%c", s->window[s->strstart]));
  5180. /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/
  5181. bflush = trees._tr_tally(s, 0, s.window[s.strstart]);
  5182. s.lookahead--;
  5183. s.strstart++;
  5184. if (bflush) {
  5185. /*** FLUSH_BLOCK(s, 0); ***/
  5186. flush_block_only(s, false);
  5187. if (s.strm.avail_out === 0) {
  5188. return BS_NEED_MORE;
  5189. }
  5190. /***/
  5191. }
  5192. }
  5193. s.insert = 0;
  5194. if (flush === Z_FINISH) {
  5195. /*** FLUSH_BLOCK(s, 1); ***/
  5196. flush_block_only(s, true);
  5197. if (s.strm.avail_out === 0) {
  5198. return BS_FINISH_STARTED;
  5199. }
  5200. /***/
  5201. return BS_FINISH_DONE;
  5202. }
  5203. if (s.last_lit) {
  5204. /*** FLUSH_BLOCK(s, 0); ***/
  5205. flush_block_only(s, false);
  5206. if (s.strm.avail_out === 0) {
  5207. return BS_NEED_MORE;
  5208. }
  5209. /***/
  5210. }
  5211. return BS_BLOCK_DONE;
  5212. }
  5213. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  5214. * the desired pack level (0..9). The values given below have been tuned to
  5215. * exclude worst case performance for pathological files. Better values may be
  5216. * found for specific files.
  5217. */
  5218. var Config = function (good_length, max_lazy, nice_length, max_chain, func) {
  5219. this.good_length = good_length;
  5220. this.max_lazy = max_lazy;
  5221. this.nice_length = nice_length;
  5222. this.max_chain = max_chain;
  5223. this.func = func;
  5224. };
  5225. var configuration_table;
  5226. configuration_table = [
  5227. /* good lazy nice chain */
  5228. new Config(0, 0, 0, 0, deflate_stored), /* 0 store only */
  5229. new Config(4, 4, 8, 4, deflate_fast), /* 1 max speed, no lazy matches */
  5230. new Config(4, 5, 16, 8, deflate_fast), /* 2 */
  5231. new Config(4, 6, 32, 32, deflate_fast), /* 3 */
  5232. new Config(4, 4, 16, 16, deflate_slow), /* 4 lazy matches */
  5233. new Config(8, 16, 32, 32, deflate_slow), /* 5 */
  5234. new Config(8, 16, 128, 128, deflate_slow), /* 6 */
  5235. new Config(8, 32, 128, 256, deflate_slow), /* 7 */
  5236. new Config(32, 128, 258, 1024, deflate_slow), /* 8 */
  5237. new Config(32, 258, 258, 4096, deflate_slow) /* 9 max compression */
  5238. ];
  5239. /* ===========================================================================
  5240. * Initialize the "longest match" routines for a new zlib stream
  5241. */
  5242. function lm_init(s) {
  5243. s.window_size = 2 * s.w_size;
  5244. /*** CLEAR_HASH(s); ***/
  5245. zero(s.head); // Fill with NIL (= 0);
  5246. /* Set the default configuration parameters:
  5247. */
  5248. s.max_lazy_match = configuration_table[s.level].max_lazy;
  5249. s.good_match = configuration_table[s.level].good_length;
  5250. s.nice_match = configuration_table[s.level].nice_length;
  5251. s.max_chain_length = configuration_table[s.level].max_chain;
  5252. s.strstart = 0;
  5253. s.block_start = 0;
  5254. s.lookahead = 0;
  5255. s.insert = 0;
  5256. s.match_length = s.prev_length = MIN_MATCH - 1;
  5257. s.match_available = 0;
  5258. s.ins_h = 0;
  5259. }
  5260. function DeflateState() {
  5261. this.strm = null; /* pointer back to this zlib stream */
  5262. this.status = 0; /* as the name implies */
  5263. this.pending_buf = null; /* output still pending */
  5264. this.pending_buf_size = 0; /* size of pending_buf */
  5265. this.pending_out = 0; /* next pending byte to output to the stream */
  5266. this.pending = 0; /* nb of bytes in the pending buffer */
  5267. this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
  5268. this.gzhead = null; /* gzip header information to write */
  5269. this.gzindex = 0; /* where in extra, name, or comment */
  5270. this.method = Z_DEFLATED; /* can only be DEFLATED */
  5271. this.last_flush = -1; /* value of flush param for previous deflate call */
  5272. this.w_size = 0; /* LZ77 window size (32K by default) */
  5273. this.w_bits = 0; /* log2(w_size) (8..16) */
  5274. this.w_mask = 0; /* w_size - 1 */
  5275. this.window = null;
  5276. /* Sliding window. Input bytes are read into the second half of the window,
  5277. * and move to the first half later to keep a dictionary of at least wSize
  5278. * bytes. With this organization, matches are limited to a distance of
  5279. * wSize-MAX_MATCH bytes, but this ensures that IO is always
  5280. * performed with a length multiple of the block size.
  5281. */
  5282. this.window_size = 0;
  5283. /* Actual size of window: 2*wSize, except when the user input buffer
  5284. * is directly used as sliding window.
  5285. */
  5286. this.prev = null;
  5287. /* Link to older string with same hash index. To limit the size of this
  5288. * array to 64K, this link is maintained only for the last 32K strings.
  5289. * An index in this array is thus a window index modulo 32K.
  5290. */
  5291. this.head = null; /* Heads of the hash chains or NIL. */
  5292. this.ins_h = 0; /* hash index of string to be inserted */
  5293. this.hash_size = 0; /* number of elements in hash table */
  5294. this.hash_bits = 0; /* log2(hash_size) */
  5295. this.hash_mask = 0; /* hash_size-1 */
  5296. this.hash_shift = 0;
  5297. /* Number of bits by which ins_h must be shifted at each input
  5298. * step. It must be such that after MIN_MATCH steps, the oldest
  5299. * byte no longer takes part in the hash key, that is:
  5300. * hash_shift * MIN_MATCH >= hash_bits
  5301. */
  5302. this.block_start = 0;
  5303. /* Window position at the beginning of the current output block. Gets
  5304. * negative when the window is moved backwards.
  5305. */
  5306. this.match_length = 0; /* length of best match */
  5307. this.prev_match = 0; /* previous match */
  5308. this.match_available = 0; /* set if previous match exists */
  5309. this.strstart = 0; /* start of string to insert */
  5310. this.match_start = 0; /* start of matching string */
  5311. this.lookahead = 0; /* number of valid bytes ahead in window */
  5312. this.prev_length = 0;
  5313. /* Length of the best match at previous step. Matches not greater than this
  5314. * are discarded. This is used in the lazy match evaluation.
  5315. */
  5316. this.max_chain_length = 0;
  5317. /* To speed up deflation, hash chains are never searched beyond this
  5318. * length. A higher limit improves compression ratio but degrades the
  5319. * speed.
  5320. */
  5321. this.max_lazy_match = 0;
  5322. /* Attempt to find a better match only when the current match is strictly
  5323. * smaller than this value. This mechanism is used only for compression
  5324. * levels >= 4.
  5325. */
  5326. // That's alias to max_lazy_match, don't use directly
  5327. //this.max_insert_length = 0;
  5328. /* Insert new strings in the hash table only if the match length is not
  5329. * greater than this length. This saves time but degrades compression.
  5330. * max_insert_length is used only for compression levels <= 3.
  5331. */
  5332. this.level = 0; /* compression level (1..9) */
  5333. this.strategy = 0; /* favor or force Huffman coding*/
  5334. this.good_match = 0;
  5335. /* Use a faster search when the previous match is longer than this */
  5336. this.nice_match = 0; /* Stop searching when current match exceeds this */
  5337. /* used by trees.c: */
  5338. /* Didn't use ct_data typedef below to suppress compiler warning */
  5339. // struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
  5340. // struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
  5341. // struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
  5342. // Use flat array of DOUBLE size, with interleaved fata,
  5343. // because JS does not support effective
  5344. this.dyn_ltree = new utils.Buf16(HEAP_SIZE * 2);
  5345. this.dyn_dtree = new utils.Buf16((2*D_CODES+1) * 2);
  5346. this.bl_tree = new utils.Buf16((2*BL_CODES+1) * 2);
  5347. zero(this.dyn_ltree);
  5348. zero(this.dyn_dtree);
  5349. zero(this.bl_tree);
  5350. this.l_desc = null; /* desc. for literal tree */
  5351. this.d_desc = null; /* desc. for distance tree */
  5352. this.bl_desc = null; /* desc. for bit length tree */
  5353. //ush bl_count[MAX_BITS+1];
  5354. this.bl_count = new utils.Buf16(MAX_BITS+1);
  5355. /* number of codes at each bit length for an optimal tree */
  5356. //int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
  5357. this.heap = new utils.Buf16(2*L_CODES+1); /* heap used to build the Huffman trees */
  5358. zero(this.heap);
  5359. this.heap_len = 0; /* number of elements in the heap */
  5360. this.heap_max = 0; /* element of largest frequency */
  5361. /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  5362. * The same heap array is used to build all trees.
  5363. */
  5364. this.depth = new utils.Buf16(2*L_CODES+1); //uch depth[2*L_CODES+1];
  5365. zero(this.depth);
  5366. /* Depth of each subtree used as tie breaker for trees of equal frequency
  5367. */
  5368. this.l_buf = 0; /* buffer index for literals or lengths */
  5369. this.lit_bufsize = 0;
  5370. /* Size of match buffer for literals/lengths. There are 4 reasons for
  5371. * limiting lit_bufsize to 64K:
  5372. * - frequencies can be kept in 16 bit counters
  5373. * - if compression is not successful for the first block, all input
  5374. * data is still in the window so we can still emit a stored block even
  5375. * when input comes from standard input. (This can also be done for
  5376. * all blocks if lit_bufsize is not greater than 32K.)
  5377. * - if compression is not successful for a file smaller than 64K, we can
  5378. * even emit a stored file instead of a stored block (saving 5 bytes).
  5379. * This is applicable only for zip (not gzip or zlib).
  5380. * - creating new Huffman trees less frequently may not provide fast
  5381. * adaptation to changes in the input data statistics. (Take for
  5382. * example a binary file with poorly compressible code followed by
  5383. * a highly compressible string table.) Smaller buffer sizes give
  5384. * fast adaptation but have of course the overhead of transmitting
  5385. * trees more frequently.
  5386. * - I can't count above 4
  5387. */
  5388. this.last_lit = 0; /* running index in l_buf */
  5389. this.d_buf = 0;
  5390. /* Buffer index for distances. To simplify the code, d_buf and l_buf have
  5391. * the same number of elements. To use different lengths, an extra flag
  5392. * array would be necessary.
  5393. */
  5394. this.opt_len = 0; /* bit length of current block with optimal trees */
  5395. this.static_len = 0; /* bit length of current block with static trees */
  5396. this.matches = 0; /* number of string matches in current block */
  5397. this.insert = 0; /* bytes at end of window left to insert */
  5398. this.bi_buf = 0;
  5399. /* Output buffer. bits are inserted starting at the bottom (least
  5400. * significant bits).
  5401. */
  5402. this.bi_valid = 0;
  5403. /* Number of valid bits in bi_buf. All bits above the last valid bit
  5404. * are always zero.
  5405. */
  5406. // Used for window memory init. We safely ignore it for JS. That makes
  5407. // sense only for pointers and memory check tools.
  5408. //this.high_water = 0;
  5409. /* High water mark offset in window for initialized bytes -- bytes above
  5410. * this are set to zero in order to avoid memory check warnings when
  5411. * longest match routines access bytes past the input. This is then
  5412. * updated to the new high water mark.
  5413. */
  5414. }
  5415. function deflateResetKeep(strm) {
  5416. var s;
  5417. if (!strm || !strm.state) {
  5418. return err(strm, Z_STREAM_ERROR);
  5419. }
  5420. strm.total_in = strm.total_out = 0;
  5421. strm.data_type = Z_UNKNOWN;
  5422. s = strm.state;
  5423. s.pending = 0;
  5424. s.pending_out = 0;
  5425. if (s.wrap < 0) {
  5426. s.wrap = -s.wrap;
  5427. /* was made negative by deflate(..., Z_FINISH); */
  5428. }
  5429. s.status = (s.wrap ? INIT_STATE : BUSY_STATE);
  5430. strm.adler = (s.wrap === 2) ?
  5431. 0 // crc32(0, Z_NULL, 0)
  5432. :
  5433. 1; // adler32(0, Z_NULL, 0)
  5434. s.last_flush = Z_NO_FLUSH;
  5435. trees._tr_init(s);
  5436. return Z_OK;
  5437. }
  5438. function deflateReset(strm) {
  5439. var ret = deflateResetKeep(strm);
  5440. if (ret === Z_OK) {
  5441. lm_init(strm.state);
  5442. }
  5443. return ret;
  5444. }
  5445. function deflateSetHeader(strm, head) {
  5446. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  5447. if (strm.state.wrap !== 2) { return Z_STREAM_ERROR; }
  5448. strm.state.gzhead = head;
  5449. return Z_OK;
  5450. }
  5451. function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {
  5452. if (!strm) { // === Z_NULL
  5453. return Z_STREAM_ERROR;
  5454. }
  5455. var wrap = 1;
  5456. if (level === Z_DEFAULT_COMPRESSION) {
  5457. level = 6;
  5458. }
  5459. if (windowBits < 0) { /* suppress zlib wrapper */
  5460. wrap = 0;
  5461. windowBits = -windowBits;
  5462. }
  5463. else if (windowBits > 15) {
  5464. wrap = 2; /* write gzip wrapper instead */
  5465. windowBits -= 16;
  5466. }
  5467. if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED ||
  5468. windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
  5469. strategy < 0 || strategy > Z_FIXED) {
  5470. return err(strm, Z_STREAM_ERROR);
  5471. }
  5472. if (windowBits === 8) {
  5473. windowBits = 9;
  5474. }
  5475. /* until 256-byte window bug fixed */
  5476. var s = new DeflateState();
  5477. strm.state = s;
  5478. s.strm = strm;
  5479. s.wrap = wrap;
  5480. s.gzhead = null;
  5481. s.w_bits = windowBits;
  5482. s.w_size = 1 << s.w_bits;
  5483. s.w_mask = s.w_size - 1;
  5484. s.hash_bits = memLevel + 7;
  5485. s.hash_size = 1 << s.hash_bits;
  5486. s.hash_mask = s.hash_size - 1;
  5487. s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);
  5488. s.window = new utils.Buf8(s.w_size * 2);
  5489. s.head = new utils.Buf16(s.hash_size);
  5490. s.prev = new utils.Buf16(s.w_size);
  5491. // Don't need mem init magic for JS.
  5492. //s.high_water = 0; /* nothing written to s->window yet */
  5493. s.lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
  5494. s.pending_buf_size = s.lit_bufsize * 4;
  5495. s.pending_buf = new utils.Buf8(s.pending_buf_size);
  5496. s.d_buf = s.lit_bufsize >> 1;
  5497. s.l_buf = (1 + 2) * s.lit_bufsize;
  5498. s.level = level;
  5499. s.strategy = strategy;
  5500. s.method = method;
  5501. return deflateReset(strm);
  5502. }
  5503. function deflateInit(strm, level) {
  5504. return deflateInit2(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
  5505. }
  5506. function deflate(strm, flush) {
  5507. var old_flush, s;
  5508. var beg, val; // for gzip header write only
  5509. if (!strm || !strm.state ||
  5510. flush > Z_BLOCK || flush < 0) {
  5511. return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR;
  5512. }
  5513. s = strm.state;
  5514. if (!strm.output ||
  5515. (!strm.input && strm.avail_in !== 0) ||
  5516. (s.status === FINISH_STATE && flush !== Z_FINISH)) {
  5517. return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR);
  5518. }
  5519. s.strm = strm; /* just in case */
  5520. old_flush = s.last_flush;
  5521. s.last_flush = flush;
  5522. /* Write the header */
  5523. if (s.status === INIT_STATE) {
  5524. if (s.wrap === 2) { // GZIP header
  5525. strm.adler = 0; //crc32(0L, Z_NULL, 0);
  5526. put_byte(s, 31);
  5527. put_byte(s, 139);
  5528. put_byte(s, 8);
  5529. if (!s.gzhead) { // s->gzhead == Z_NULL
  5530. put_byte(s, 0);
  5531. put_byte(s, 0);
  5532. put_byte(s, 0);
  5533. put_byte(s, 0);
  5534. put_byte(s, 0);
  5535. put_byte(s, s.level === 9 ? 2 :
  5536. (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
  5537. 4 : 0));
  5538. put_byte(s, OS_CODE);
  5539. s.status = BUSY_STATE;
  5540. }
  5541. else {
  5542. put_byte(s, (s.gzhead.text ? 1 : 0) +
  5543. (s.gzhead.hcrc ? 2 : 0) +
  5544. (!s.gzhead.extra ? 0 : 4) +
  5545. (!s.gzhead.name ? 0 : 8) +
  5546. (!s.gzhead.comment ? 0 : 16)
  5547. );
  5548. put_byte(s, s.gzhead.time & 0xff);
  5549. put_byte(s, (s.gzhead.time >> 8) & 0xff);
  5550. put_byte(s, (s.gzhead.time >> 16) & 0xff);
  5551. put_byte(s, (s.gzhead.time >> 24) & 0xff);
  5552. put_byte(s, s.level === 9 ? 2 :
  5553. (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
  5554. 4 : 0));
  5555. put_byte(s, s.gzhead.os & 0xff);
  5556. if (s.gzhead.extra && s.gzhead.extra.length) {
  5557. put_byte(s, s.gzhead.extra.length & 0xff);
  5558. put_byte(s, (s.gzhead.extra.length >> 8) & 0xff);
  5559. }
  5560. if (s.gzhead.hcrc) {
  5561. strm.adler = crc32(strm.adler, s.pending_buf, s.pending, 0);
  5562. }
  5563. s.gzindex = 0;
  5564. s.status = EXTRA_STATE;
  5565. }
  5566. }
  5567. else // DEFLATE header
  5568. {
  5569. var header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8;
  5570. var level_flags = -1;
  5571. if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
  5572. level_flags = 0;
  5573. } else if (s.level < 6) {
  5574. level_flags = 1;
  5575. } else if (s.level === 6) {
  5576. level_flags = 2;
  5577. } else {
  5578. level_flags = 3;
  5579. }
  5580. header |= (level_flags << 6);
  5581. if (s.strstart !== 0) { header |= PRESET_DICT; }
  5582. header += 31 - (header % 31);
  5583. s.status = BUSY_STATE;
  5584. putShortMSB(s, header);
  5585. /* Save the adler32 of the preset dictionary: */
  5586. if (s.strstart !== 0) {
  5587. putShortMSB(s, strm.adler >>> 16);
  5588. putShortMSB(s, strm.adler & 0xffff);
  5589. }
  5590. strm.adler = 1; // adler32(0L, Z_NULL, 0);
  5591. }
  5592. }
  5593. //#ifdef GZIP
  5594. if (s.status === EXTRA_STATE) {
  5595. if (s.gzhead.extra/* != Z_NULL*/) {
  5596. beg = s.pending; /* start of bytes to update crc */
  5597. while (s.gzindex < (s.gzhead.extra.length & 0xffff)) {
  5598. if (s.pending === s.pending_buf_size) {
  5599. if (s.gzhead.hcrc && s.pending > beg) {
  5600. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  5601. }
  5602. flush_pending(strm);
  5603. beg = s.pending;
  5604. if (s.pending === s.pending_buf_size) {
  5605. break;
  5606. }
  5607. }
  5608. put_byte(s, s.gzhead.extra[s.gzindex] & 0xff);
  5609. s.gzindex++;
  5610. }
  5611. if (s.gzhead.hcrc && s.pending > beg) {
  5612. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  5613. }
  5614. if (s.gzindex === s.gzhead.extra.length) {
  5615. s.gzindex = 0;
  5616. s.status = NAME_STATE;
  5617. }
  5618. }
  5619. else {
  5620. s.status = NAME_STATE;
  5621. }
  5622. }
  5623. if (s.status === NAME_STATE) {
  5624. if (s.gzhead.name/* != Z_NULL*/) {
  5625. beg = s.pending; /* start of bytes to update crc */
  5626. //int val;
  5627. do {
  5628. if (s.pending === s.pending_buf_size) {
  5629. if (s.gzhead.hcrc && s.pending > beg) {
  5630. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  5631. }
  5632. flush_pending(strm);
  5633. beg = s.pending;
  5634. if (s.pending === s.pending_buf_size) {
  5635. val = 1;
  5636. break;
  5637. }
  5638. }
  5639. // JS specific: little magic to add zero terminator to end of string
  5640. if (s.gzindex < s.gzhead.name.length) {
  5641. val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff;
  5642. } else {
  5643. val = 0;
  5644. }
  5645. put_byte(s, val);
  5646. } while (val !== 0);
  5647. if (s.gzhead.hcrc && s.pending > beg){
  5648. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  5649. }
  5650. if (val === 0) {
  5651. s.gzindex = 0;
  5652. s.status = COMMENT_STATE;
  5653. }
  5654. }
  5655. else {
  5656. s.status = COMMENT_STATE;
  5657. }
  5658. }
  5659. if (s.status === COMMENT_STATE) {
  5660. if (s.gzhead.comment/* != Z_NULL*/) {
  5661. beg = s.pending; /* start of bytes to update crc */
  5662. //int val;
  5663. do {
  5664. if (s.pending === s.pending_buf_size) {
  5665. if (s.gzhead.hcrc && s.pending > beg) {
  5666. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  5667. }
  5668. flush_pending(strm);
  5669. beg = s.pending;
  5670. if (s.pending === s.pending_buf_size) {
  5671. val = 1;
  5672. break;
  5673. }
  5674. }
  5675. // JS specific: little magic to add zero terminator to end of string
  5676. if (s.gzindex < s.gzhead.comment.length) {
  5677. val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff;
  5678. } else {
  5679. val = 0;
  5680. }
  5681. put_byte(s, val);
  5682. } while (val !== 0);
  5683. if (s.gzhead.hcrc && s.pending > beg) {
  5684. strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
  5685. }
  5686. if (val === 0) {
  5687. s.status = HCRC_STATE;
  5688. }
  5689. }
  5690. else {
  5691. s.status = HCRC_STATE;
  5692. }
  5693. }
  5694. if (s.status === HCRC_STATE) {
  5695. if (s.gzhead.hcrc) {
  5696. if (s.pending + 2 > s.pending_buf_size) {
  5697. flush_pending(strm);
  5698. }
  5699. if (s.pending + 2 <= s.pending_buf_size) {
  5700. put_byte(s, strm.adler & 0xff);
  5701. put_byte(s, (strm.adler >> 8) & 0xff);
  5702. strm.adler = 0; //crc32(0L, Z_NULL, 0);
  5703. s.status = BUSY_STATE;
  5704. }
  5705. }
  5706. else {
  5707. s.status = BUSY_STATE;
  5708. }
  5709. }
  5710. //#endif
  5711. /* Flush as much pending output as possible */
  5712. if (s.pending !== 0) {
  5713. flush_pending(strm);
  5714. if (strm.avail_out === 0) {
  5715. /* Since avail_out is 0, deflate will be called again with
  5716. * more output space, but possibly with both pending and
  5717. * avail_in equal to zero. There won't be anything to do,
  5718. * but this is not an error situation so make sure we
  5719. * return OK instead of BUF_ERROR at next call of deflate:
  5720. */
  5721. s.last_flush = -1;
  5722. return Z_OK;
  5723. }
  5724. /* Make sure there is something to do and avoid duplicate consecutive
  5725. * flushes. For repeated and useless calls with Z_FINISH, we keep
  5726. * returning Z_STREAM_END instead of Z_BUF_ERROR.
  5727. */
  5728. } else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) &&
  5729. flush !== Z_FINISH) {
  5730. return err(strm, Z_BUF_ERROR);
  5731. }
  5732. /* User must not provide more input after the first FINISH: */
  5733. if (s.status === FINISH_STATE && strm.avail_in !== 0) {
  5734. return err(strm, Z_BUF_ERROR);
  5735. }
  5736. /* Start a new block or continue the current one.
  5737. */
  5738. if (strm.avail_in !== 0 || s.lookahead !== 0 ||
  5739. (flush !== Z_NO_FLUSH && s.status !== FINISH_STATE)) {
  5740. var bstate = (s.strategy === Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) :
  5741. (s.strategy === Z_RLE ? deflate_rle(s, flush) :
  5742. configuration_table[s.level].func(s, flush));
  5743. if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {
  5744. s.status = FINISH_STATE;
  5745. }
  5746. if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {
  5747. if (strm.avail_out === 0) {
  5748. s.last_flush = -1;
  5749. /* avoid BUF_ERROR next call, see above */
  5750. }
  5751. return Z_OK;
  5752. /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
  5753. * of deflate should use the same flush parameter to make sure
  5754. * that the flush is complete. So we don't have to output an
  5755. * empty block here, this will be done at next call. This also
  5756. * ensures that for a very small output buffer, we emit at most
  5757. * one empty block.
  5758. */
  5759. }
  5760. if (bstate === BS_BLOCK_DONE) {
  5761. if (flush === Z_PARTIAL_FLUSH) {
  5762. trees._tr_align(s);
  5763. }
  5764. else if (flush !== Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
  5765. trees._tr_stored_block(s, 0, 0, false);
  5766. /* For a full flush, this empty block will be recognized
  5767. * as a special marker by inflate_sync().
  5768. */
  5769. if (flush === Z_FULL_FLUSH) {
  5770. /*** CLEAR_HASH(s); ***/ /* forget history */
  5771. zero(s.head); // Fill with NIL (= 0);
  5772. if (s.lookahead === 0) {
  5773. s.strstart = 0;
  5774. s.block_start = 0;
  5775. s.insert = 0;
  5776. }
  5777. }
  5778. }
  5779. flush_pending(strm);
  5780. if (strm.avail_out === 0) {
  5781. s.last_flush = -1; /* avoid BUF_ERROR at next call, see above */
  5782. return Z_OK;
  5783. }
  5784. }
  5785. }
  5786. //Assert(strm->avail_out > 0, "bug2");
  5787. //if (strm.avail_out <= 0) { throw new Error("bug2");}
  5788. if (flush !== Z_FINISH) { return Z_OK; }
  5789. if (s.wrap <= 0) { return Z_STREAM_END; }
  5790. /* Write the trailer */
  5791. if (s.wrap === 2) {
  5792. put_byte(s, strm.adler & 0xff);
  5793. put_byte(s, (strm.adler >> 8) & 0xff);
  5794. put_byte(s, (strm.adler >> 16) & 0xff);
  5795. put_byte(s, (strm.adler >> 24) & 0xff);
  5796. put_byte(s, strm.total_in & 0xff);
  5797. put_byte(s, (strm.total_in >> 8) & 0xff);
  5798. put_byte(s, (strm.total_in >> 16) & 0xff);
  5799. put_byte(s, (strm.total_in >> 24) & 0xff);
  5800. }
  5801. else
  5802. {
  5803. putShortMSB(s, strm.adler >>> 16);
  5804. putShortMSB(s, strm.adler & 0xffff);
  5805. }
  5806. flush_pending(strm);
  5807. /* If avail_out is zero, the application will call deflate again
  5808. * to flush the rest.
  5809. */
  5810. if (s.wrap > 0) { s.wrap = -s.wrap; }
  5811. /* write the trailer only once! */
  5812. return s.pending !== 0 ? Z_OK : Z_STREAM_END;
  5813. }
  5814. function deflateEnd(strm) {
  5815. var status;
  5816. if (!strm/*== Z_NULL*/ || !strm.state/*== Z_NULL*/) {
  5817. return Z_STREAM_ERROR;
  5818. }
  5819. status = strm.state.status;
  5820. if (status !== INIT_STATE &&
  5821. status !== EXTRA_STATE &&
  5822. status !== NAME_STATE &&
  5823. status !== COMMENT_STATE &&
  5824. status !== HCRC_STATE &&
  5825. status !== BUSY_STATE &&
  5826. status !== FINISH_STATE
  5827. ) {
  5828. return err(strm, Z_STREAM_ERROR);
  5829. }
  5830. strm.state = null;
  5831. return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK;
  5832. }
  5833. /* =========================================================================
  5834. * Copy the source state to the destination state
  5835. */
  5836. //function deflateCopy(dest, source) {
  5837. //
  5838. //}
  5839. exports.deflateInit = deflateInit;
  5840. exports.deflateInit2 = deflateInit2;
  5841. exports.deflateReset = deflateReset;
  5842. exports.deflateResetKeep = deflateResetKeep;
  5843. exports.deflateSetHeader = deflateSetHeader;
  5844. exports.deflate = deflate;
  5845. exports.deflateEnd = deflateEnd;
  5846. exports.deflateInfo = 'pako deflate (from Nodeca project)';
  5847. /* Not implemented
  5848. exports.deflateBound = deflateBound;
  5849. exports.deflateCopy = deflateCopy;
  5850. exports.deflateSetDictionary = deflateSetDictionary;
  5851. exports.deflateParams = deflateParams;
  5852. exports.deflatePending = deflatePending;
  5853. exports.deflatePrime = deflatePrime;
  5854. exports.deflateTune = deflateTune;
  5855. */
  5856. },{"../utils/common":27,"./adler32":29,"./crc32":31,"./messages":37,"./trees":38}],33:[function(_dereq_,module,exports){
  5857. 'use strict';
  5858. function GZheader() {
  5859. /* true if compressed data believed to be text */
  5860. this.text = 0;
  5861. /* modification time */
  5862. this.time = 0;
  5863. /* extra flags (not used when writing a gzip file) */
  5864. this.xflags = 0;
  5865. /* operating system */
  5866. this.os = 0;
  5867. /* pointer to extra field or Z_NULL if none */
  5868. this.extra = null;
  5869. /* extra field length (valid if extra != Z_NULL) */
  5870. this.extra_len = 0; // Actually, we don't need it in JS,
  5871. // but leave for few code modifications
  5872. //
  5873. // Setup limits is not necessary because in js we should not preallocate memory
  5874. // for inflate use constant limit in 65536 bytes
  5875. //
  5876. /* space at extra (only when reading header) */
  5877. // this.extra_max = 0;
  5878. /* pointer to zero-terminated file name or Z_NULL */
  5879. this.name = '';
  5880. /* space at name (only when reading header) */
  5881. // this.name_max = 0;
  5882. /* pointer to zero-terminated comment or Z_NULL */
  5883. this.comment = '';
  5884. /* space at comment (only when reading header) */
  5885. // this.comm_max = 0;
  5886. /* true if there was or will be a header crc */
  5887. this.hcrc = 0;
  5888. /* true when done reading gzip header (not used when writing a gzip file) */
  5889. this.done = false;
  5890. }
  5891. module.exports = GZheader;
  5892. },{}],34:[function(_dereq_,module,exports){
  5893. 'use strict';
  5894. // See state defs from inflate.js
  5895. var BAD = 30; /* got a data error -- remain here until reset */
  5896. var TYPE = 12; /* i: waiting for type bits, including last-flag bit */
  5897. /*
  5898. Decode literal, length, and distance codes and write out the resulting
  5899. literal and match bytes until either not enough input or output is
  5900. available, an end-of-block is encountered, or a data error is encountered.
  5901. When large enough input and output buffers are supplied to inflate(), for
  5902. example, a 16K input buffer and a 64K output buffer, more than 95% of the
  5903. inflate execution time is spent in this routine.
  5904. Entry assumptions:
  5905. state.mode === LEN
  5906. strm.avail_in >= 6
  5907. strm.avail_out >= 258
  5908. start >= strm.avail_out
  5909. state.bits < 8
  5910. On return, state.mode is one of:
  5911. LEN -- ran out of enough output space or enough available input
  5912. TYPE -- reached end of block code, inflate() to interpret next block
  5913. BAD -- error in block data
  5914. Notes:
  5915. - The maximum input bits used by a length/distance pair is 15 bits for the
  5916. length code, 5 bits for the length extra, 15 bits for the distance code,
  5917. and 13 bits for the distance extra. This totals 48 bits, or six bytes.
  5918. Therefore if strm.avail_in >= 6, then there is enough input to avoid
  5919. checking for available input while decoding.
  5920. - The maximum bytes that a single length/distance pair can output is 258
  5921. bytes, which is the maximum length that can be coded. inflate_fast()
  5922. requires strm.avail_out >= 258 for each loop to avoid checking for
  5923. output space.
  5924. */
  5925. module.exports = function inflate_fast(strm, start) {
  5926. var state;
  5927. var _in; /* local strm.input */
  5928. var last; /* have enough input while in < last */
  5929. var _out; /* local strm.output */
  5930. var beg; /* inflate()'s initial strm.output */
  5931. var end; /* while out < end, enough space available */
  5932. //#ifdef INFLATE_STRICT
  5933. var dmax; /* maximum distance from zlib header */
  5934. //#endif
  5935. var wsize; /* window size or zero if not using window */
  5936. var whave; /* valid bytes in the window */
  5937. var wnext; /* window write index */
  5938. var window; /* allocated sliding window, if wsize != 0 */
  5939. var hold; /* local strm.hold */
  5940. var bits; /* local strm.bits */
  5941. var lcode; /* local strm.lencode */
  5942. var dcode; /* local strm.distcode */
  5943. var lmask; /* mask for first level of length codes */
  5944. var dmask; /* mask for first level of distance codes */
  5945. var here; /* retrieved table entry */
  5946. var op; /* code bits, operation, extra bits, or */
  5947. /* window position, window bytes to copy */
  5948. var len; /* match length, unused bytes */
  5949. var dist; /* match distance */
  5950. var from; /* where to copy match from */
  5951. var from_source;
  5952. var input, output; // JS specific, because we have no pointers
  5953. /* copy state to local variables */
  5954. state = strm.state;
  5955. //here = state.here;
  5956. _in = strm.next_in;
  5957. input = strm.input;
  5958. last = _in + (strm.avail_in - 5);
  5959. _out = strm.next_out;
  5960. output = strm.output;
  5961. beg = _out - (start - strm.avail_out);
  5962. end = _out + (strm.avail_out - 257);
  5963. //#ifdef INFLATE_STRICT
  5964. dmax = state.dmax;
  5965. //#endif
  5966. wsize = state.wsize;
  5967. whave = state.whave;
  5968. wnext = state.wnext;
  5969. window = state.window;
  5970. hold = state.hold;
  5971. bits = state.bits;
  5972. lcode = state.lencode;
  5973. dcode = state.distcode;
  5974. lmask = (1 << state.lenbits) - 1;
  5975. dmask = (1 << state.distbits) - 1;
  5976. /* decode literals and length/distances until end-of-block or not enough
  5977. input data or output space */
  5978. top:
  5979. do {
  5980. if (bits < 15) {
  5981. hold += input[_in++] << bits;
  5982. bits += 8;
  5983. hold += input[_in++] << bits;
  5984. bits += 8;
  5985. }
  5986. here = lcode[hold & lmask];
  5987. dolen:
  5988. for (;;) { // Goto emulation
  5989. op = here >>> 24/*here.bits*/;
  5990. hold >>>= op;
  5991. bits -= op;
  5992. op = (here >>> 16) & 0xff/*here.op*/;
  5993. if (op === 0) { /* literal */
  5994. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  5995. // "inflate: literal '%c'\n" :
  5996. // "inflate: literal 0x%02x\n", here.val));
  5997. output[_out++] = here & 0xffff/*here.val*/;
  5998. }
  5999. else if (op & 16) { /* length base */
  6000. len = here & 0xffff/*here.val*/;
  6001. op &= 15; /* number of extra bits */
  6002. if (op) {
  6003. if (bits < op) {
  6004. hold += input[_in++] << bits;
  6005. bits += 8;
  6006. }
  6007. len += hold & ((1 << op) - 1);
  6008. hold >>>= op;
  6009. bits -= op;
  6010. }
  6011. //Tracevv((stderr, "inflate: length %u\n", len));
  6012. if (bits < 15) {
  6013. hold += input[_in++] << bits;
  6014. bits += 8;
  6015. hold += input[_in++] << bits;
  6016. bits += 8;
  6017. }
  6018. here = dcode[hold & dmask];
  6019. dodist:
  6020. for (;;) { // goto emulation
  6021. op = here >>> 24/*here.bits*/;
  6022. hold >>>= op;
  6023. bits -= op;
  6024. op = (here >>> 16) & 0xff/*here.op*/;
  6025. if (op & 16) { /* distance base */
  6026. dist = here & 0xffff/*here.val*/;
  6027. op &= 15; /* number of extra bits */
  6028. if (bits < op) {
  6029. hold += input[_in++] << bits;
  6030. bits += 8;
  6031. if (bits < op) {
  6032. hold += input[_in++] << bits;
  6033. bits += 8;
  6034. }
  6035. }
  6036. dist += hold & ((1 << op) - 1);
  6037. //#ifdef INFLATE_STRICT
  6038. if (dist > dmax) {
  6039. strm.msg = 'invalid distance too far back';
  6040. state.mode = BAD;
  6041. break top;
  6042. }
  6043. //#endif
  6044. hold >>>= op;
  6045. bits -= op;
  6046. //Tracevv((stderr, "inflate: distance %u\n", dist));
  6047. op = _out - beg; /* max distance in output */
  6048. if (dist > op) { /* see if copy from window */
  6049. op = dist - op; /* distance back in window */
  6050. if (op > whave) {
  6051. if (state.sane) {
  6052. strm.msg = 'invalid distance too far back';
  6053. state.mode = BAD;
  6054. break top;
  6055. }
  6056. // (!) This block is disabled in zlib defailts,
  6057. // don't enable it for binary compatibility
  6058. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  6059. // if (len <= op - whave) {
  6060. // do {
  6061. // output[_out++] = 0;
  6062. // } while (--len);
  6063. // continue top;
  6064. // }
  6065. // len -= op - whave;
  6066. // do {
  6067. // output[_out++] = 0;
  6068. // } while (--op > whave);
  6069. // if (op === 0) {
  6070. // from = _out - dist;
  6071. // do {
  6072. // output[_out++] = output[from++];
  6073. // } while (--len);
  6074. // continue top;
  6075. // }
  6076. //#endif
  6077. }
  6078. from = 0; // window index
  6079. from_source = window;
  6080. if (wnext === 0) { /* very common case */
  6081. from += wsize - op;
  6082. if (op < len) { /* some from window */
  6083. len -= op;
  6084. do {
  6085. output[_out++] = window[from++];
  6086. } while (--op);
  6087. from = _out - dist; /* rest from output */
  6088. from_source = output;
  6089. }
  6090. }
  6091. else if (wnext < op) { /* wrap around window */
  6092. from += wsize + wnext - op;
  6093. op -= wnext;
  6094. if (op < len) { /* some from end of window */
  6095. len -= op;
  6096. do {
  6097. output[_out++] = window[from++];
  6098. } while (--op);
  6099. from = 0;
  6100. if (wnext < len) { /* some from start of window */
  6101. op = wnext;
  6102. len -= op;
  6103. do {
  6104. output[_out++] = window[from++];
  6105. } while (--op);
  6106. from = _out - dist; /* rest from output */
  6107. from_source = output;
  6108. }
  6109. }
  6110. }
  6111. else { /* contiguous in window */
  6112. from += wnext - op;
  6113. if (op < len) { /* some from window */
  6114. len -= op;
  6115. do {
  6116. output[_out++] = window[from++];
  6117. } while (--op);
  6118. from = _out - dist; /* rest from output */
  6119. from_source = output;
  6120. }
  6121. }
  6122. while (len > 2) {
  6123. output[_out++] = from_source[from++];
  6124. output[_out++] = from_source[from++];
  6125. output[_out++] = from_source[from++];
  6126. len -= 3;
  6127. }
  6128. if (len) {
  6129. output[_out++] = from_source[from++];
  6130. if (len > 1) {
  6131. output[_out++] = from_source[from++];
  6132. }
  6133. }
  6134. }
  6135. else {
  6136. from = _out - dist; /* copy direct from output */
  6137. do { /* minimum length is three */
  6138. output[_out++] = output[from++];
  6139. output[_out++] = output[from++];
  6140. output[_out++] = output[from++];
  6141. len -= 3;
  6142. } while (len > 2);
  6143. if (len) {
  6144. output[_out++] = output[from++];
  6145. if (len > 1) {
  6146. output[_out++] = output[from++];
  6147. }
  6148. }
  6149. }
  6150. }
  6151. else if ((op & 64) === 0) { /* 2nd level distance code */
  6152. here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  6153. continue dodist;
  6154. }
  6155. else {
  6156. strm.msg = 'invalid distance code';
  6157. state.mode = BAD;
  6158. break top;
  6159. }
  6160. break; // need to emulate goto via "continue"
  6161. }
  6162. }
  6163. else if ((op & 64) === 0) { /* 2nd level length code */
  6164. here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
  6165. continue dolen;
  6166. }
  6167. else if (op & 32) { /* end-of-block */
  6168. //Tracevv((stderr, "inflate: end of block\n"));
  6169. state.mode = TYPE;
  6170. break top;
  6171. }
  6172. else {
  6173. strm.msg = 'invalid literal/length code';
  6174. state.mode = BAD;
  6175. break top;
  6176. }
  6177. break; // need to emulate goto via "continue"
  6178. }
  6179. } while (_in < last && _out < end);
  6180. /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
  6181. len = bits >> 3;
  6182. _in -= len;
  6183. bits -= len << 3;
  6184. hold &= (1 << bits) - 1;
  6185. /* update state and return */
  6186. strm.next_in = _in;
  6187. strm.next_out = _out;
  6188. strm.avail_in = (_in < last ? 5 + (last - _in) : 5 - (_in - last));
  6189. strm.avail_out = (_out < end ? 257 + (end - _out) : 257 - (_out - end));
  6190. state.hold = hold;
  6191. state.bits = bits;
  6192. return;
  6193. };
  6194. },{}],35:[function(_dereq_,module,exports){
  6195. 'use strict';
  6196. var utils = _dereq_('../utils/common');
  6197. var adler32 = _dereq_('./adler32');
  6198. var crc32 = _dereq_('./crc32');
  6199. var inflate_fast = _dereq_('./inffast');
  6200. var inflate_table = _dereq_('./inftrees');
  6201. var CODES = 0;
  6202. var LENS = 1;
  6203. var DISTS = 2;
  6204. /* Public constants ==========================================================*/
  6205. /* ===========================================================================*/
  6206. /* Allowed flush values; see deflate() and inflate() below for details */
  6207. //var Z_NO_FLUSH = 0;
  6208. //var Z_PARTIAL_FLUSH = 1;
  6209. //var Z_SYNC_FLUSH = 2;
  6210. //var Z_FULL_FLUSH = 3;
  6211. var Z_FINISH = 4;
  6212. var Z_BLOCK = 5;
  6213. var Z_TREES = 6;
  6214. /* Return codes for the compression/decompression functions. Negative values
  6215. * are errors, positive values are used for special but normal events.
  6216. */
  6217. var Z_OK = 0;
  6218. var Z_STREAM_END = 1;
  6219. var Z_NEED_DICT = 2;
  6220. //var Z_ERRNO = -1;
  6221. var Z_STREAM_ERROR = -2;
  6222. var Z_DATA_ERROR = -3;
  6223. var Z_MEM_ERROR = -4;
  6224. var Z_BUF_ERROR = -5;
  6225. //var Z_VERSION_ERROR = -6;
  6226. /* The deflate compression method */
  6227. var Z_DEFLATED = 8;
  6228. /* STATES ====================================================================*/
  6229. /* ===========================================================================*/
  6230. var HEAD = 1; /* i: waiting for magic header */
  6231. var FLAGS = 2; /* i: waiting for method and flags (gzip) */
  6232. var TIME = 3; /* i: waiting for modification time (gzip) */
  6233. var OS = 4; /* i: waiting for extra flags and operating system (gzip) */
  6234. var EXLEN = 5; /* i: waiting for extra length (gzip) */
  6235. var EXTRA = 6; /* i: waiting for extra bytes (gzip) */
  6236. var NAME = 7; /* i: waiting for end of file name (gzip) */
  6237. var COMMENT = 8; /* i: waiting for end of comment (gzip) */
  6238. var HCRC = 9; /* i: waiting for header crc (gzip) */
  6239. var DICTID = 10; /* i: waiting for dictionary check value */
  6240. var DICT = 11; /* waiting for inflateSetDictionary() call */
  6241. var TYPE = 12; /* i: waiting for type bits, including last-flag bit */
  6242. var TYPEDO = 13; /* i: same, but skip check to exit inflate on new block */
  6243. var STORED = 14; /* i: waiting for stored size (length and complement) */
  6244. var COPY_ = 15; /* i/o: same as COPY below, but only first time in */
  6245. var COPY = 16; /* i/o: waiting for input or output to copy stored block */
  6246. var TABLE = 17; /* i: waiting for dynamic block table lengths */
  6247. var LENLENS = 18; /* i: waiting for code length code lengths */
  6248. var CODELENS = 19; /* i: waiting for length/lit and distance code lengths */
  6249. var LEN_ = 20; /* i: same as LEN below, but only first time in */
  6250. var LEN = 21; /* i: waiting for length/lit/eob code */
  6251. var LENEXT = 22; /* i: waiting for length extra bits */
  6252. var DIST = 23; /* i: waiting for distance code */
  6253. var DISTEXT = 24; /* i: waiting for distance extra bits */
  6254. var MATCH = 25; /* o: waiting for output space to copy string */
  6255. var LIT = 26; /* o: waiting for output space to write literal */
  6256. var CHECK = 27; /* i: waiting for 32-bit check value */
  6257. var LENGTH = 28; /* i: waiting for 32-bit length (gzip) */
  6258. var DONE = 29; /* finished check, done -- remain here until reset */
  6259. var BAD = 30; /* got a data error -- remain here until reset */
  6260. var MEM = 31; /* got an inflate() memory error -- remain here until reset */
  6261. var SYNC = 32; /* looking for synchronization bytes to restart inflate() */
  6262. /* ===========================================================================*/
  6263. var ENOUGH_LENS = 852;
  6264. var ENOUGH_DISTS = 592;
  6265. //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  6266. var MAX_WBITS = 15;
  6267. /* 32K LZ77 window */
  6268. var DEF_WBITS = MAX_WBITS;
  6269. function ZSWAP32(q) {
  6270. return (((q >>> 24) & 0xff) +
  6271. ((q >>> 8) & 0xff00) +
  6272. ((q & 0xff00) << 8) +
  6273. ((q & 0xff) << 24));
  6274. }
  6275. function InflateState() {
  6276. this.mode = 0; /* current inflate mode */
  6277. this.last = false; /* true if processing last block */
  6278. this.wrap = 0; /* bit 0 true for zlib, bit 1 true for gzip */
  6279. this.havedict = false; /* true if dictionary provided */
  6280. this.flags = 0; /* gzip header method and flags (0 if zlib) */
  6281. this.dmax = 0; /* zlib header max distance (INFLATE_STRICT) */
  6282. this.check = 0; /* protected copy of check value */
  6283. this.total = 0; /* protected copy of output count */
  6284. // TODO: may be {}
  6285. this.head = null; /* where to save gzip header information */
  6286. /* sliding window */
  6287. this.wbits = 0; /* log base 2 of requested window size */
  6288. this.wsize = 0; /* window size or zero if not using window */
  6289. this.whave = 0; /* valid bytes in the window */
  6290. this.wnext = 0; /* window write index */
  6291. this.window = null; /* allocated sliding window, if needed */
  6292. /* bit accumulator */
  6293. this.hold = 0; /* input bit accumulator */
  6294. this.bits = 0; /* number of bits in "in" */
  6295. /* for string and stored block copying */
  6296. this.length = 0; /* literal or length of data to copy */
  6297. this.offset = 0; /* distance back to copy string from */
  6298. /* for table and code decoding */
  6299. this.extra = 0; /* extra bits needed */
  6300. /* fixed and dynamic code tables */
  6301. this.lencode = null; /* starting table for length/literal codes */
  6302. this.distcode = null; /* starting table for distance codes */
  6303. this.lenbits = 0; /* index bits for lencode */
  6304. this.distbits = 0; /* index bits for distcode */
  6305. /* dynamic table building */
  6306. this.ncode = 0; /* number of code length code lengths */
  6307. this.nlen = 0; /* number of length code lengths */
  6308. this.ndist = 0; /* number of distance code lengths */
  6309. this.have = 0; /* number of code lengths in lens[] */
  6310. this.next = null; /* next available space in codes[] */
  6311. this.lens = new utils.Buf16(320); /* temporary storage for code lengths */
  6312. this.work = new utils.Buf16(288); /* work area for code table building */
  6313. /*
  6314. because we don't have pointers in js, we use lencode and distcode directly
  6315. as buffers so we don't need codes
  6316. */
  6317. //this.codes = new utils.Buf32(ENOUGH); /* space for code tables */
  6318. this.lendyn = null; /* dynamic table for length/literal codes (JS specific) */
  6319. this.distdyn = null; /* dynamic table for distance codes (JS specific) */
  6320. this.sane = 0; /* if false, allow invalid distance too far */
  6321. this.back = 0; /* bits back of last unprocessed length/lit */
  6322. this.was = 0; /* initial length of match */
  6323. }
  6324. function inflateResetKeep(strm) {
  6325. var state;
  6326. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  6327. state = strm.state;
  6328. strm.total_in = strm.total_out = state.total = 0;
  6329. strm.msg = ''; /*Z_NULL*/
  6330. if (state.wrap) { /* to support ill-conceived Java test suite */
  6331. strm.adler = state.wrap & 1;
  6332. }
  6333. state.mode = HEAD;
  6334. state.last = 0;
  6335. state.havedict = 0;
  6336. state.dmax = 32768;
  6337. state.head = null/*Z_NULL*/;
  6338. state.hold = 0;
  6339. state.bits = 0;
  6340. //state.lencode = state.distcode = state.next = state.codes;
  6341. state.lencode = state.lendyn = new utils.Buf32(ENOUGH_LENS);
  6342. state.distcode = state.distdyn = new utils.Buf32(ENOUGH_DISTS);
  6343. state.sane = 1;
  6344. state.back = -1;
  6345. //Tracev((stderr, "inflate: reset\n"));
  6346. return Z_OK;
  6347. }
  6348. function inflateReset(strm) {
  6349. var state;
  6350. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  6351. state = strm.state;
  6352. state.wsize = 0;
  6353. state.whave = 0;
  6354. state.wnext = 0;
  6355. return inflateResetKeep(strm);
  6356. }
  6357. function inflateReset2(strm, windowBits) {
  6358. var wrap;
  6359. var state;
  6360. /* get the state */
  6361. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  6362. state = strm.state;
  6363. /* extract wrap request from windowBits parameter */
  6364. if (windowBits < 0) {
  6365. wrap = 0;
  6366. windowBits = -windowBits;
  6367. }
  6368. else {
  6369. wrap = (windowBits >> 4) + 1;
  6370. if (windowBits < 48) {
  6371. windowBits &= 15;
  6372. }
  6373. }
  6374. /* set number of window bits, free window if different */
  6375. if (windowBits && (windowBits < 8 || windowBits > 15)) {
  6376. return Z_STREAM_ERROR;
  6377. }
  6378. if (state.window !== null && state.wbits !== windowBits) {
  6379. state.window = null;
  6380. }
  6381. /* update state and reset the rest of it */
  6382. state.wrap = wrap;
  6383. state.wbits = windowBits;
  6384. return inflateReset(strm);
  6385. }
  6386. function inflateInit2(strm, windowBits) {
  6387. var ret;
  6388. var state;
  6389. if (!strm) { return Z_STREAM_ERROR; }
  6390. //strm.msg = Z_NULL; /* in case we return an error */
  6391. state = new InflateState();
  6392. //if (state === Z_NULL) return Z_MEM_ERROR;
  6393. //Tracev((stderr, "inflate: allocated\n"));
  6394. strm.state = state;
  6395. state.window = null/*Z_NULL*/;
  6396. ret = inflateReset2(strm, windowBits);
  6397. if (ret !== Z_OK) {
  6398. strm.state = null/*Z_NULL*/;
  6399. }
  6400. return ret;
  6401. }
  6402. function inflateInit(strm) {
  6403. return inflateInit2(strm, DEF_WBITS);
  6404. }
  6405. /*
  6406. Return state with length and distance decoding tables and index sizes set to
  6407. fixed code decoding. Normally this returns fixed tables from inffixed.h.
  6408. If BUILDFIXED is defined, then instead this routine builds the tables the
  6409. first time it's called, and returns those tables the first time and
  6410. thereafter. This reduces the size of the code by about 2K bytes, in
  6411. exchange for a little execution time. However, BUILDFIXED should not be
  6412. used for threaded applications, since the rewriting of the tables and virgin
  6413. may not be thread-safe.
  6414. */
  6415. var virgin = true;
  6416. var lenfix, distfix; // We have no pointers in JS, so keep tables separate
  6417. function fixedtables(state) {
  6418. /* build fixed huffman tables if first call (may not be thread safe) */
  6419. if (virgin) {
  6420. var sym;
  6421. lenfix = new utils.Buf32(512);
  6422. distfix = new utils.Buf32(32);
  6423. /* literal/length table */
  6424. sym = 0;
  6425. while (sym < 144) { state.lens[sym++] = 8; }
  6426. while (sym < 256) { state.lens[sym++] = 9; }
  6427. while (sym < 280) { state.lens[sym++] = 7; }
  6428. while (sym < 288) { state.lens[sym++] = 8; }
  6429. inflate_table(LENS, state.lens, 0, 288, lenfix, 0, state.work, {bits: 9});
  6430. /* distance table */
  6431. sym = 0;
  6432. while (sym < 32) { state.lens[sym++] = 5; }
  6433. inflate_table(DISTS, state.lens, 0, 32, distfix, 0, state.work, {bits: 5});
  6434. /* do this just once */
  6435. virgin = false;
  6436. }
  6437. state.lencode = lenfix;
  6438. state.lenbits = 9;
  6439. state.distcode = distfix;
  6440. state.distbits = 5;
  6441. }
  6442. /*
  6443. Update the window with the last wsize (normally 32K) bytes written before
  6444. returning. If window does not exist yet, create it. This is only called
  6445. when a window is already in use, or when output has been written during this
  6446. inflate call, but the end of the deflate stream has not been reached yet.
  6447. It is also called to create a window for dictionary data when a dictionary
  6448. is loaded.
  6449. Providing output buffers larger than 32K to inflate() should provide a speed
  6450. advantage, since only the last 32K of output is copied to the sliding window
  6451. upon return from inflate(), and since all distances after the first 32K of
  6452. output will fall in the output data, making match copies simpler and faster.
  6453. The advantage may be dependent on the size of the processor's data caches.
  6454. */
  6455. function updatewindow(strm, src, end, copy) {
  6456. var dist;
  6457. var state = strm.state;
  6458. /* if it hasn't been done already, allocate space for the window */
  6459. if (state.window === null) {
  6460. state.wsize = 1 << state.wbits;
  6461. state.wnext = 0;
  6462. state.whave = 0;
  6463. state.window = new utils.Buf8(state.wsize);
  6464. }
  6465. /* copy state->wsize or less output bytes into the circular window */
  6466. if (copy >= state.wsize) {
  6467. utils.arraySet(state.window,src, end - state.wsize, state.wsize, 0);
  6468. state.wnext = 0;
  6469. state.whave = state.wsize;
  6470. }
  6471. else {
  6472. dist = state.wsize - state.wnext;
  6473. if (dist > copy) {
  6474. dist = copy;
  6475. }
  6476. //zmemcpy(state->window + state->wnext, end - copy, dist);
  6477. utils.arraySet(state.window,src, end - copy, dist, state.wnext);
  6478. copy -= dist;
  6479. if (copy) {
  6480. //zmemcpy(state->window, end - copy, copy);
  6481. utils.arraySet(state.window,src, end - copy, copy, 0);
  6482. state.wnext = copy;
  6483. state.whave = state.wsize;
  6484. }
  6485. else {
  6486. state.wnext += dist;
  6487. if (state.wnext === state.wsize) { state.wnext = 0; }
  6488. if (state.whave < state.wsize) { state.whave += dist; }
  6489. }
  6490. }
  6491. return 0;
  6492. }
  6493. function inflate(strm, flush) {
  6494. var state;
  6495. var input, output; // input/output buffers
  6496. var next; /* next input INDEX */
  6497. var put; /* next output INDEX */
  6498. var have, left; /* available input and output */
  6499. var hold; /* bit buffer */
  6500. var bits; /* bits in bit buffer */
  6501. var _in, _out; /* save starting available input and output */
  6502. var copy; /* number of stored or match bytes to copy */
  6503. var from; /* where to copy match bytes from */
  6504. var from_source;
  6505. var here = 0; /* current decoding table entry */
  6506. var here_bits, here_op, here_val; // paked "here" denormalized (JS specific)
  6507. //var last; /* parent table entry */
  6508. var last_bits, last_op, last_val; // paked "last" denormalized (JS specific)
  6509. var len; /* length to copy for repeats, bits to drop */
  6510. var ret; /* return code */
  6511. var hbuf = new utils.Buf8(4); /* buffer for gzip header crc calculation */
  6512. var opts;
  6513. var n; // temporary var for NEED_BITS
  6514. var order = /* permutation of code lengths */
  6515. [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
  6516. if (!strm || !strm.state || !strm.output ||
  6517. (!strm.input && strm.avail_in !== 0)) {
  6518. return Z_STREAM_ERROR;
  6519. }
  6520. state = strm.state;
  6521. if (state.mode === TYPE) { state.mode = TYPEDO; } /* skip check */
  6522. //--- LOAD() ---
  6523. put = strm.next_out;
  6524. output = strm.output;
  6525. left = strm.avail_out;
  6526. next = strm.next_in;
  6527. input = strm.input;
  6528. have = strm.avail_in;
  6529. hold = state.hold;
  6530. bits = state.bits;
  6531. //---
  6532. _in = have;
  6533. _out = left;
  6534. ret = Z_OK;
  6535. inf_leave: // goto emulation
  6536. for (;;) {
  6537. switch (state.mode) {
  6538. case HEAD:
  6539. if (state.wrap === 0) {
  6540. state.mode = TYPEDO;
  6541. break;
  6542. }
  6543. //=== NEEDBITS(16);
  6544. while (bits < 16) {
  6545. if (have === 0) { break inf_leave; }
  6546. have--;
  6547. hold += input[next++] << bits;
  6548. bits += 8;
  6549. }
  6550. //===//
  6551. if ((state.wrap & 2) && hold === 0x8b1f) { /* gzip header */
  6552. state.check = 0/*crc32(0L, Z_NULL, 0)*/;
  6553. //=== CRC2(state.check, hold);
  6554. hbuf[0] = hold & 0xff;
  6555. hbuf[1] = (hold >>> 8) & 0xff;
  6556. state.check = crc32(state.check, hbuf, 2, 0);
  6557. //===//
  6558. //=== INITBITS();
  6559. hold = 0;
  6560. bits = 0;
  6561. //===//
  6562. state.mode = FLAGS;
  6563. break;
  6564. }
  6565. state.flags = 0; /* expect zlib header */
  6566. if (state.head) {
  6567. state.head.done = false;
  6568. }
  6569. if (!(state.wrap & 1) || /* check if zlib header allowed */
  6570. (((hold & 0xff)/*BITS(8)*/ << 8) + (hold >> 8)) % 31) {
  6571. strm.msg = 'incorrect header check';
  6572. state.mode = BAD;
  6573. break;
  6574. }
  6575. if ((hold & 0x0f)/*BITS(4)*/ !== Z_DEFLATED) {
  6576. strm.msg = 'unknown compression method';
  6577. state.mode = BAD;
  6578. break;
  6579. }
  6580. //--- DROPBITS(4) ---//
  6581. hold >>>= 4;
  6582. bits -= 4;
  6583. //---//
  6584. len = (hold & 0x0f)/*BITS(4)*/ + 8;
  6585. if (state.wbits === 0) {
  6586. state.wbits = len;
  6587. }
  6588. else if (len > state.wbits) {
  6589. strm.msg = 'invalid window size';
  6590. state.mode = BAD;
  6591. break;
  6592. }
  6593. state.dmax = 1 << len;
  6594. //Tracev((stderr, "inflate: zlib header ok\n"));
  6595. strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
  6596. state.mode = hold & 0x200 ? DICTID : TYPE;
  6597. //=== INITBITS();
  6598. hold = 0;
  6599. bits = 0;
  6600. //===//
  6601. break;
  6602. case FLAGS:
  6603. //=== NEEDBITS(16); */
  6604. while (bits < 16) {
  6605. if (have === 0) { break inf_leave; }
  6606. have--;
  6607. hold += input[next++] << bits;
  6608. bits += 8;
  6609. }
  6610. //===//
  6611. state.flags = hold;
  6612. if ((state.flags & 0xff) !== Z_DEFLATED) {
  6613. strm.msg = 'unknown compression method';
  6614. state.mode = BAD;
  6615. break;
  6616. }
  6617. if (state.flags & 0xe000) {
  6618. strm.msg = 'unknown header flags set';
  6619. state.mode = BAD;
  6620. break;
  6621. }
  6622. if (state.head) {
  6623. state.head.text = ((hold >> 8) & 1);
  6624. }
  6625. if (state.flags & 0x0200) {
  6626. //=== CRC2(state.check, hold);
  6627. hbuf[0] = hold & 0xff;
  6628. hbuf[1] = (hold >>> 8) & 0xff;
  6629. state.check = crc32(state.check, hbuf, 2, 0);
  6630. //===//
  6631. }
  6632. //=== INITBITS();
  6633. hold = 0;
  6634. bits = 0;
  6635. //===//
  6636. state.mode = TIME;
  6637. /* falls through */
  6638. case TIME:
  6639. //=== NEEDBITS(32); */
  6640. while (bits < 32) {
  6641. if (have === 0) { break inf_leave; }
  6642. have--;
  6643. hold += input[next++] << bits;
  6644. bits += 8;
  6645. }
  6646. //===//
  6647. if (state.head) {
  6648. state.head.time = hold;
  6649. }
  6650. if (state.flags & 0x0200) {
  6651. //=== CRC4(state.check, hold)
  6652. hbuf[0] = hold & 0xff;
  6653. hbuf[1] = (hold >>> 8) & 0xff;
  6654. hbuf[2] = (hold >>> 16) & 0xff;
  6655. hbuf[3] = (hold >>> 24) & 0xff;
  6656. state.check = crc32(state.check, hbuf, 4, 0);
  6657. //===
  6658. }
  6659. //=== INITBITS();
  6660. hold = 0;
  6661. bits = 0;
  6662. //===//
  6663. state.mode = OS;
  6664. /* falls through */
  6665. case OS:
  6666. //=== NEEDBITS(16); */
  6667. while (bits < 16) {
  6668. if (have === 0) { break inf_leave; }
  6669. have--;
  6670. hold += input[next++] << bits;
  6671. bits += 8;
  6672. }
  6673. //===//
  6674. if (state.head) {
  6675. state.head.xflags = (hold & 0xff);
  6676. state.head.os = (hold >> 8);
  6677. }
  6678. if (state.flags & 0x0200) {
  6679. //=== CRC2(state.check, hold);
  6680. hbuf[0] = hold & 0xff;
  6681. hbuf[1] = (hold >>> 8) & 0xff;
  6682. state.check = crc32(state.check, hbuf, 2, 0);
  6683. //===//
  6684. }
  6685. //=== INITBITS();
  6686. hold = 0;
  6687. bits = 0;
  6688. //===//
  6689. state.mode = EXLEN;
  6690. /* falls through */
  6691. case EXLEN:
  6692. if (state.flags & 0x0400) {
  6693. //=== NEEDBITS(16); */
  6694. while (bits < 16) {
  6695. if (have === 0) { break inf_leave; }
  6696. have--;
  6697. hold += input[next++] << bits;
  6698. bits += 8;
  6699. }
  6700. //===//
  6701. state.length = hold;
  6702. if (state.head) {
  6703. state.head.extra_len = hold;
  6704. }
  6705. if (state.flags & 0x0200) {
  6706. //=== CRC2(state.check, hold);
  6707. hbuf[0] = hold & 0xff;
  6708. hbuf[1] = (hold >>> 8) & 0xff;
  6709. state.check = crc32(state.check, hbuf, 2, 0);
  6710. //===//
  6711. }
  6712. //=== INITBITS();
  6713. hold = 0;
  6714. bits = 0;
  6715. //===//
  6716. }
  6717. else if (state.head) {
  6718. state.head.extra = null/*Z_NULL*/;
  6719. }
  6720. state.mode = EXTRA;
  6721. /* falls through */
  6722. case EXTRA:
  6723. if (state.flags & 0x0400) {
  6724. copy = state.length;
  6725. if (copy > have) { copy = have; }
  6726. if (copy) {
  6727. if (state.head) {
  6728. len = state.head.extra_len - state.length;
  6729. if (!state.head.extra) {
  6730. // Use untyped array for more conveniend processing later
  6731. state.head.extra = new Array(state.head.extra_len);
  6732. }
  6733. utils.arraySet(
  6734. state.head.extra,
  6735. input,
  6736. next,
  6737. // extra field is limited to 65536 bytes
  6738. // - no need for additional size check
  6739. copy,
  6740. /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/
  6741. len
  6742. );
  6743. //zmemcpy(state.head.extra + len, next,
  6744. // len + copy > state.head.extra_max ?
  6745. // state.head.extra_max - len : copy);
  6746. }
  6747. if (state.flags & 0x0200) {
  6748. state.check = crc32(state.check, input, copy, next);
  6749. }
  6750. have -= copy;
  6751. next += copy;
  6752. state.length -= copy;
  6753. }
  6754. if (state.length) { break inf_leave; }
  6755. }
  6756. state.length = 0;
  6757. state.mode = NAME;
  6758. /* falls through */
  6759. case NAME:
  6760. if (state.flags & 0x0800) {
  6761. if (have === 0) { break inf_leave; }
  6762. copy = 0;
  6763. do {
  6764. // TODO: 2 or 1 bytes?
  6765. len = input[next + copy++];
  6766. /* use constant limit because in js we should not preallocate memory */
  6767. if (state.head && len &&
  6768. (state.length < 65536 /*state.head.name_max*/)) {
  6769. state.head.name += String.fromCharCode(len);
  6770. }
  6771. } while (len && copy < have);
  6772. if (state.flags & 0x0200) {
  6773. state.check = crc32(state.check, input, copy, next);
  6774. }
  6775. have -= copy;
  6776. next += copy;
  6777. if (len) { break inf_leave; }
  6778. }
  6779. else if (state.head) {
  6780. state.head.name = null;
  6781. }
  6782. state.length = 0;
  6783. state.mode = COMMENT;
  6784. /* falls through */
  6785. case COMMENT:
  6786. if (state.flags & 0x1000) {
  6787. if (have === 0) { break inf_leave; }
  6788. copy = 0;
  6789. do {
  6790. len = input[next + copy++];
  6791. /* use constant limit because in js we should not preallocate memory */
  6792. if (state.head && len &&
  6793. (state.length < 65536 /*state.head.comm_max*/)) {
  6794. state.head.comment += String.fromCharCode(len);
  6795. }
  6796. } while (len && copy < have);
  6797. if (state.flags & 0x0200) {
  6798. state.check = crc32(state.check, input, copy, next);
  6799. }
  6800. have -= copy;
  6801. next += copy;
  6802. if (len) { break inf_leave; }
  6803. }
  6804. else if (state.head) {
  6805. state.head.comment = null;
  6806. }
  6807. state.mode = HCRC;
  6808. /* falls through */
  6809. case HCRC:
  6810. if (state.flags & 0x0200) {
  6811. //=== NEEDBITS(16); */
  6812. while (bits < 16) {
  6813. if (have === 0) { break inf_leave; }
  6814. have--;
  6815. hold += input[next++] << bits;
  6816. bits += 8;
  6817. }
  6818. //===//
  6819. if (hold !== (state.check & 0xffff)) {
  6820. strm.msg = 'header crc mismatch';
  6821. state.mode = BAD;
  6822. break;
  6823. }
  6824. //=== INITBITS();
  6825. hold = 0;
  6826. bits = 0;
  6827. //===//
  6828. }
  6829. if (state.head) {
  6830. state.head.hcrc = ((state.flags >> 9) & 1);
  6831. state.head.done = true;
  6832. }
  6833. strm.adler = state.check = 0 /*crc32(0L, Z_NULL, 0)*/;
  6834. state.mode = TYPE;
  6835. break;
  6836. case DICTID:
  6837. //=== NEEDBITS(32); */
  6838. while (bits < 32) {
  6839. if (have === 0) { break inf_leave; }
  6840. have--;
  6841. hold += input[next++] << bits;
  6842. bits += 8;
  6843. }
  6844. //===//
  6845. strm.adler = state.check = ZSWAP32(hold);
  6846. //=== INITBITS();
  6847. hold = 0;
  6848. bits = 0;
  6849. //===//
  6850. state.mode = DICT;
  6851. /* falls through */
  6852. case DICT:
  6853. if (state.havedict === 0) {
  6854. //--- RESTORE() ---
  6855. strm.next_out = put;
  6856. strm.avail_out = left;
  6857. strm.next_in = next;
  6858. strm.avail_in = have;
  6859. state.hold = hold;
  6860. state.bits = bits;
  6861. //---
  6862. return Z_NEED_DICT;
  6863. }
  6864. strm.adler = state.check = 1/*adler32(0L, Z_NULL, 0)*/;
  6865. state.mode = TYPE;
  6866. /* falls through */
  6867. case TYPE:
  6868. if (flush === Z_BLOCK || flush === Z_TREES) { break inf_leave; }
  6869. /* falls through */
  6870. case TYPEDO:
  6871. if (state.last) {
  6872. //--- BYTEBITS() ---//
  6873. hold >>>= bits & 7;
  6874. bits -= bits & 7;
  6875. //---//
  6876. state.mode = CHECK;
  6877. break;
  6878. }
  6879. //=== NEEDBITS(3); */
  6880. while (bits < 3) {
  6881. if (have === 0) { break inf_leave; }
  6882. have--;
  6883. hold += input[next++] << bits;
  6884. bits += 8;
  6885. }
  6886. //===//
  6887. state.last = (hold & 0x01)/*BITS(1)*/;
  6888. //--- DROPBITS(1) ---//
  6889. hold >>>= 1;
  6890. bits -= 1;
  6891. //---//
  6892. switch ((hold & 0x03)/*BITS(2)*/) {
  6893. case 0: /* stored block */
  6894. //Tracev((stderr, "inflate: stored block%s\n",
  6895. // state.last ? " (last)" : ""));
  6896. state.mode = STORED;
  6897. break;
  6898. case 1: /* fixed block */
  6899. fixedtables(state);
  6900. //Tracev((stderr, "inflate: fixed codes block%s\n",
  6901. // state.last ? " (last)" : ""));
  6902. state.mode = LEN_; /* decode codes */
  6903. if (flush === Z_TREES) {
  6904. //--- DROPBITS(2) ---//
  6905. hold >>>= 2;
  6906. bits -= 2;
  6907. //---//
  6908. break inf_leave;
  6909. }
  6910. break;
  6911. case 2: /* dynamic block */
  6912. //Tracev((stderr, "inflate: dynamic codes block%s\n",
  6913. // state.last ? " (last)" : ""));
  6914. state.mode = TABLE;
  6915. break;
  6916. case 3:
  6917. strm.msg = 'invalid block type';
  6918. state.mode = BAD;
  6919. }
  6920. //--- DROPBITS(2) ---//
  6921. hold >>>= 2;
  6922. bits -= 2;
  6923. //---//
  6924. break;
  6925. case STORED:
  6926. //--- BYTEBITS() ---// /* go to byte boundary */
  6927. hold >>>= bits & 7;
  6928. bits -= bits & 7;
  6929. //---//
  6930. //=== NEEDBITS(32); */
  6931. while (bits < 32) {
  6932. if (have === 0) { break inf_leave; }
  6933. have--;
  6934. hold += input[next++] << bits;
  6935. bits += 8;
  6936. }
  6937. //===//
  6938. if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) {
  6939. strm.msg = 'invalid stored block lengths';
  6940. state.mode = BAD;
  6941. break;
  6942. }
  6943. state.length = hold & 0xffff;
  6944. //Tracev((stderr, "inflate: stored length %u\n",
  6945. // state.length));
  6946. //=== INITBITS();
  6947. hold = 0;
  6948. bits = 0;
  6949. //===//
  6950. state.mode = COPY_;
  6951. if (flush === Z_TREES) { break inf_leave; }
  6952. /* falls through */
  6953. case COPY_:
  6954. state.mode = COPY;
  6955. /* falls through */
  6956. case COPY:
  6957. copy = state.length;
  6958. if (copy) {
  6959. if (copy > have) { copy = have; }
  6960. if (copy > left) { copy = left; }
  6961. if (copy === 0) { break inf_leave; }
  6962. //--- zmemcpy(put, next, copy); ---
  6963. utils.arraySet(output, input, next, copy, put);
  6964. //---//
  6965. have -= copy;
  6966. next += copy;
  6967. left -= copy;
  6968. put += copy;
  6969. state.length -= copy;
  6970. break;
  6971. }
  6972. //Tracev((stderr, "inflate: stored end\n"));
  6973. state.mode = TYPE;
  6974. break;
  6975. case TABLE:
  6976. //=== NEEDBITS(14); */
  6977. while (bits < 14) {
  6978. if (have === 0) { break inf_leave; }
  6979. have--;
  6980. hold += input[next++] << bits;
  6981. bits += 8;
  6982. }
  6983. //===//
  6984. state.nlen = (hold & 0x1f)/*BITS(5)*/ + 257;
  6985. //--- DROPBITS(5) ---//
  6986. hold >>>= 5;
  6987. bits -= 5;
  6988. //---//
  6989. state.ndist = (hold & 0x1f)/*BITS(5)*/ + 1;
  6990. //--- DROPBITS(5) ---//
  6991. hold >>>= 5;
  6992. bits -= 5;
  6993. //---//
  6994. state.ncode = (hold & 0x0f)/*BITS(4)*/ + 4;
  6995. //--- DROPBITS(4) ---//
  6996. hold >>>= 4;
  6997. bits -= 4;
  6998. //---//
  6999. //#ifndef PKZIP_BUG_WORKAROUND
  7000. if (state.nlen > 286 || state.ndist > 30) {
  7001. strm.msg = 'too many length or distance symbols';
  7002. state.mode = BAD;
  7003. break;
  7004. }
  7005. //#endif
  7006. //Tracev((stderr, "inflate: table sizes ok\n"));
  7007. state.have = 0;
  7008. state.mode = LENLENS;
  7009. /* falls through */
  7010. case LENLENS:
  7011. while (state.have < state.ncode) {
  7012. //=== NEEDBITS(3);
  7013. while (bits < 3) {
  7014. if (have === 0) { break inf_leave; }
  7015. have--;
  7016. hold += input[next++] << bits;
  7017. bits += 8;
  7018. }
  7019. //===//
  7020. state.lens[order[state.have++]] = (hold & 0x07);//BITS(3);
  7021. //--- DROPBITS(3) ---//
  7022. hold >>>= 3;
  7023. bits -= 3;
  7024. //---//
  7025. }
  7026. while (state.have < 19) {
  7027. state.lens[order[state.have++]] = 0;
  7028. }
  7029. // We have separate tables & no pointers. 2 commented lines below not needed.
  7030. //state.next = state.codes;
  7031. //state.lencode = state.next;
  7032. // Switch to use dynamic table
  7033. state.lencode = state.lendyn;
  7034. state.lenbits = 7;
  7035. opts = {bits: state.lenbits};
  7036. ret = inflate_table(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts);
  7037. state.lenbits = opts.bits;
  7038. if (ret) {
  7039. strm.msg = 'invalid code lengths set';
  7040. state.mode = BAD;
  7041. break;
  7042. }
  7043. //Tracev((stderr, "inflate: code lengths ok\n"));
  7044. state.have = 0;
  7045. state.mode = CODELENS;
  7046. /* falls through */
  7047. case CODELENS:
  7048. while (state.have < state.nlen + state.ndist) {
  7049. for (;;) {
  7050. here = state.lencode[hold & ((1 << state.lenbits) - 1)];/*BITS(state.lenbits)*/
  7051. here_bits = here >>> 24;
  7052. here_op = (here >>> 16) & 0xff;
  7053. here_val = here & 0xffff;
  7054. if ((here_bits) <= bits) { break; }
  7055. //--- PULLBYTE() ---//
  7056. if (have === 0) { break inf_leave; }
  7057. have--;
  7058. hold += input[next++] << bits;
  7059. bits += 8;
  7060. //---//
  7061. }
  7062. if (here_val < 16) {
  7063. //--- DROPBITS(here.bits) ---//
  7064. hold >>>= here_bits;
  7065. bits -= here_bits;
  7066. //---//
  7067. state.lens[state.have++] = here_val;
  7068. }
  7069. else {
  7070. if (here_val === 16) {
  7071. //=== NEEDBITS(here.bits + 2);
  7072. n = here_bits + 2;
  7073. while (bits < n) {
  7074. if (have === 0) { break inf_leave; }
  7075. have--;
  7076. hold += input[next++] << bits;
  7077. bits += 8;
  7078. }
  7079. //===//
  7080. //--- DROPBITS(here.bits) ---//
  7081. hold >>>= here_bits;
  7082. bits -= here_bits;
  7083. //---//
  7084. if (state.have === 0) {
  7085. strm.msg = 'invalid bit length repeat';
  7086. state.mode = BAD;
  7087. break;
  7088. }
  7089. len = state.lens[state.have - 1];
  7090. copy = 3 + (hold & 0x03);//BITS(2);
  7091. //--- DROPBITS(2) ---//
  7092. hold >>>= 2;
  7093. bits -= 2;
  7094. //---//
  7095. }
  7096. else if (here_val === 17) {
  7097. //=== NEEDBITS(here.bits + 3);
  7098. n = here_bits + 3;
  7099. while (bits < n) {
  7100. if (have === 0) { break inf_leave; }
  7101. have--;
  7102. hold += input[next++] << bits;
  7103. bits += 8;
  7104. }
  7105. //===//
  7106. //--- DROPBITS(here.bits) ---//
  7107. hold >>>= here_bits;
  7108. bits -= here_bits;
  7109. //---//
  7110. len = 0;
  7111. copy = 3 + (hold & 0x07);//BITS(3);
  7112. //--- DROPBITS(3) ---//
  7113. hold >>>= 3;
  7114. bits -= 3;
  7115. //---//
  7116. }
  7117. else {
  7118. //=== NEEDBITS(here.bits + 7);
  7119. n = here_bits + 7;
  7120. while (bits < n) {
  7121. if (have === 0) { break inf_leave; }
  7122. have--;
  7123. hold += input[next++] << bits;
  7124. bits += 8;
  7125. }
  7126. //===//
  7127. //--- DROPBITS(here.bits) ---//
  7128. hold >>>= here_bits;
  7129. bits -= here_bits;
  7130. //---//
  7131. len = 0;
  7132. copy = 11 + (hold & 0x7f);//BITS(7);
  7133. //--- DROPBITS(7) ---//
  7134. hold >>>= 7;
  7135. bits -= 7;
  7136. //---//
  7137. }
  7138. if (state.have + copy > state.nlen + state.ndist) {
  7139. strm.msg = 'invalid bit length repeat';
  7140. state.mode = BAD;
  7141. break;
  7142. }
  7143. while (copy--) {
  7144. state.lens[state.have++] = len;
  7145. }
  7146. }
  7147. }
  7148. /* handle error breaks in while */
  7149. if (state.mode === BAD) { break; }
  7150. /* check for end-of-block code (better have one) */
  7151. if (state.lens[256] === 0) {
  7152. strm.msg = 'invalid code -- missing end-of-block';
  7153. state.mode = BAD;
  7154. break;
  7155. }
  7156. /* build code tables -- note: do not change the lenbits or distbits
  7157. values here (9 and 6) without reading the comments in inftrees.h
  7158. concerning the ENOUGH constants, which depend on those values */
  7159. state.lenbits = 9;
  7160. opts = {bits: state.lenbits};
  7161. ret = inflate_table(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts);
  7162. // We have separate tables & no pointers. 2 commented lines below not needed.
  7163. // state.next_index = opts.table_index;
  7164. state.lenbits = opts.bits;
  7165. // state.lencode = state.next;
  7166. if (ret) {
  7167. strm.msg = 'invalid literal/lengths set';
  7168. state.mode = BAD;
  7169. break;
  7170. }
  7171. state.distbits = 6;
  7172. //state.distcode.copy(state.codes);
  7173. // Switch to use dynamic table
  7174. state.distcode = state.distdyn;
  7175. opts = {bits: state.distbits};
  7176. ret = inflate_table(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts);
  7177. // We have separate tables & no pointers. 2 commented lines below not needed.
  7178. // state.next_index = opts.table_index;
  7179. state.distbits = opts.bits;
  7180. // state.distcode = state.next;
  7181. if (ret) {
  7182. strm.msg = 'invalid distances set';
  7183. state.mode = BAD;
  7184. break;
  7185. }
  7186. //Tracev((stderr, 'inflate: codes ok\n'));
  7187. state.mode = LEN_;
  7188. if (flush === Z_TREES) { break inf_leave; }
  7189. /* falls through */
  7190. case LEN_:
  7191. state.mode = LEN;
  7192. /* falls through */
  7193. case LEN:
  7194. if (have >= 6 && left >= 258) {
  7195. //--- RESTORE() ---
  7196. strm.next_out = put;
  7197. strm.avail_out = left;
  7198. strm.next_in = next;
  7199. strm.avail_in = have;
  7200. state.hold = hold;
  7201. state.bits = bits;
  7202. //---
  7203. inflate_fast(strm, _out);
  7204. //--- LOAD() ---
  7205. put = strm.next_out;
  7206. output = strm.output;
  7207. left = strm.avail_out;
  7208. next = strm.next_in;
  7209. input = strm.input;
  7210. have = strm.avail_in;
  7211. hold = state.hold;
  7212. bits = state.bits;
  7213. //---
  7214. if (state.mode === TYPE) {
  7215. state.back = -1;
  7216. }
  7217. break;
  7218. }
  7219. state.back = 0;
  7220. for (;;) {
  7221. here = state.lencode[hold & ((1 << state.lenbits) -1)]; /*BITS(state.lenbits)*/
  7222. here_bits = here >>> 24;
  7223. here_op = (here >>> 16) & 0xff;
  7224. here_val = here & 0xffff;
  7225. if (here_bits <= bits) { break; }
  7226. //--- PULLBYTE() ---//
  7227. if (have === 0) { break inf_leave; }
  7228. have--;
  7229. hold += input[next++] << bits;
  7230. bits += 8;
  7231. //---//
  7232. }
  7233. if (here_op && (here_op & 0xf0) === 0) {
  7234. last_bits = here_bits;
  7235. last_op = here_op;
  7236. last_val = here_val;
  7237. for (;;) {
  7238. here = state.lencode[last_val +
  7239. ((hold & ((1 << (last_bits + last_op)) -1))/*BITS(last.bits + last.op)*/ >> last_bits)];
  7240. here_bits = here >>> 24;
  7241. here_op = (here >>> 16) & 0xff;
  7242. here_val = here & 0xffff;
  7243. if ((last_bits + here_bits) <= bits) { break; }
  7244. //--- PULLBYTE() ---//
  7245. if (have === 0) { break inf_leave; }
  7246. have--;
  7247. hold += input[next++] << bits;
  7248. bits += 8;
  7249. //---//
  7250. }
  7251. //--- DROPBITS(last.bits) ---//
  7252. hold >>>= last_bits;
  7253. bits -= last_bits;
  7254. //---//
  7255. state.back += last_bits;
  7256. }
  7257. //--- DROPBITS(here.bits) ---//
  7258. hold >>>= here_bits;
  7259. bits -= here_bits;
  7260. //---//
  7261. state.back += here_bits;
  7262. state.length = here_val;
  7263. if (here_op === 0) {
  7264. //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  7265. // "inflate: literal '%c'\n" :
  7266. // "inflate: literal 0x%02x\n", here.val));
  7267. state.mode = LIT;
  7268. break;
  7269. }
  7270. if (here_op & 32) {
  7271. //Tracevv((stderr, "inflate: end of block\n"));
  7272. state.back = -1;
  7273. state.mode = TYPE;
  7274. break;
  7275. }
  7276. if (here_op & 64) {
  7277. strm.msg = 'invalid literal/length code';
  7278. state.mode = BAD;
  7279. break;
  7280. }
  7281. state.extra = here_op & 15;
  7282. state.mode = LENEXT;
  7283. /* falls through */
  7284. case LENEXT:
  7285. if (state.extra) {
  7286. //=== NEEDBITS(state.extra);
  7287. n = state.extra;
  7288. while (bits < n) {
  7289. if (have === 0) { break inf_leave; }
  7290. have--;
  7291. hold += input[next++] << bits;
  7292. bits += 8;
  7293. }
  7294. //===//
  7295. state.length += hold & ((1 << state.extra) -1)/*BITS(state.extra)*/;
  7296. //--- DROPBITS(state.extra) ---//
  7297. hold >>>= state.extra;
  7298. bits -= state.extra;
  7299. //---//
  7300. state.back += state.extra;
  7301. }
  7302. //Tracevv((stderr, "inflate: length %u\n", state.length));
  7303. state.was = state.length;
  7304. state.mode = DIST;
  7305. /* falls through */
  7306. case DIST:
  7307. for (;;) {
  7308. here = state.distcode[hold & ((1 << state.distbits) -1)];/*BITS(state.distbits)*/
  7309. here_bits = here >>> 24;
  7310. here_op = (here >>> 16) & 0xff;
  7311. here_val = here & 0xffff;
  7312. if ((here_bits) <= bits) { break; }
  7313. //--- PULLBYTE() ---//
  7314. if (have === 0) { break inf_leave; }
  7315. have--;
  7316. hold += input[next++] << bits;
  7317. bits += 8;
  7318. //---//
  7319. }
  7320. if ((here_op & 0xf0) === 0) {
  7321. last_bits = here_bits;
  7322. last_op = here_op;
  7323. last_val = here_val;
  7324. for (;;) {
  7325. here = state.distcode[last_val +
  7326. ((hold & ((1 << (last_bits + last_op)) -1))/*BITS(last.bits + last.op)*/ >> last_bits)];
  7327. here_bits = here >>> 24;
  7328. here_op = (here >>> 16) & 0xff;
  7329. here_val = here & 0xffff;
  7330. if ((last_bits + here_bits) <= bits) { break; }
  7331. //--- PULLBYTE() ---//
  7332. if (have === 0) { break inf_leave; }
  7333. have--;
  7334. hold += input[next++] << bits;
  7335. bits += 8;
  7336. //---//
  7337. }
  7338. //--- DROPBITS(last.bits) ---//
  7339. hold >>>= last_bits;
  7340. bits -= last_bits;
  7341. //---//
  7342. state.back += last_bits;
  7343. }
  7344. //--- DROPBITS(here.bits) ---//
  7345. hold >>>= here_bits;
  7346. bits -= here_bits;
  7347. //---//
  7348. state.back += here_bits;
  7349. if (here_op & 64) {
  7350. strm.msg = 'invalid distance code';
  7351. state.mode = BAD;
  7352. break;
  7353. }
  7354. state.offset = here_val;
  7355. state.extra = (here_op) & 15;
  7356. state.mode = DISTEXT;
  7357. /* falls through */
  7358. case DISTEXT:
  7359. if (state.extra) {
  7360. //=== NEEDBITS(state.extra);
  7361. n = state.extra;
  7362. while (bits < n) {
  7363. if (have === 0) { break inf_leave; }
  7364. have--;
  7365. hold += input[next++] << bits;
  7366. bits += 8;
  7367. }
  7368. //===//
  7369. state.offset += hold & ((1 << state.extra) -1)/*BITS(state.extra)*/;
  7370. //--- DROPBITS(state.extra) ---//
  7371. hold >>>= state.extra;
  7372. bits -= state.extra;
  7373. //---//
  7374. state.back += state.extra;
  7375. }
  7376. //#ifdef INFLATE_STRICT
  7377. if (state.offset > state.dmax) {
  7378. strm.msg = 'invalid distance too far back';
  7379. state.mode = BAD;
  7380. break;
  7381. }
  7382. //#endif
  7383. //Tracevv((stderr, "inflate: distance %u\n", state.offset));
  7384. state.mode = MATCH;
  7385. /* falls through */
  7386. case MATCH:
  7387. if (left === 0) { break inf_leave; }
  7388. copy = _out - left;
  7389. if (state.offset > copy) { /* copy from window */
  7390. copy = state.offset - copy;
  7391. if (copy > state.whave) {
  7392. if (state.sane) {
  7393. strm.msg = 'invalid distance too far back';
  7394. state.mode = BAD;
  7395. break;
  7396. }
  7397. // (!) This block is disabled in zlib defailts,
  7398. // don't enable it for binary compatibility
  7399. //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  7400. // Trace((stderr, "inflate.c too far\n"));
  7401. // copy -= state.whave;
  7402. // if (copy > state.length) { copy = state.length; }
  7403. // if (copy > left) { copy = left; }
  7404. // left -= copy;
  7405. // state.length -= copy;
  7406. // do {
  7407. // output[put++] = 0;
  7408. // } while (--copy);
  7409. // if (state.length === 0) { state.mode = LEN; }
  7410. // break;
  7411. //#endif
  7412. }
  7413. if (copy > state.wnext) {
  7414. copy -= state.wnext;
  7415. from = state.wsize - copy;
  7416. }
  7417. else {
  7418. from = state.wnext - copy;
  7419. }
  7420. if (copy > state.length) { copy = state.length; }
  7421. from_source = state.window;
  7422. }
  7423. else { /* copy from output */
  7424. from_source = output;
  7425. from = put - state.offset;
  7426. copy = state.length;
  7427. }
  7428. if (copy > left) { copy = left; }
  7429. left -= copy;
  7430. state.length -= copy;
  7431. do {
  7432. output[put++] = from_source[from++];
  7433. } while (--copy);
  7434. if (state.length === 0) { state.mode = LEN; }
  7435. break;
  7436. case LIT:
  7437. if (left === 0) { break inf_leave; }
  7438. output[put++] = state.length;
  7439. left--;
  7440. state.mode = LEN;
  7441. break;
  7442. case CHECK:
  7443. if (state.wrap) {
  7444. //=== NEEDBITS(32);
  7445. while (bits < 32) {
  7446. if (have === 0) { break inf_leave; }
  7447. have--;
  7448. // Use '|' insdead of '+' to make sure that result is signed
  7449. hold |= input[next++] << bits;
  7450. bits += 8;
  7451. }
  7452. //===//
  7453. _out -= left;
  7454. strm.total_out += _out;
  7455. state.total += _out;
  7456. if (_out) {
  7457. strm.adler = state.check =
  7458. /*UPDATE(state.check, put - _out, _out);*/
  7459. (state.flags ? crc32(state.check, output, _out, put - _out) : adler32(state.check, output, _out, put - _out));
  7460. }
  7461. _out = left;
  7462. // NB: crc32 stored as signed 32-bit int, ZSWAP32 returns signed too
  7463. if ((state.flags ? hold : ZSWAP32(hold)) !== state.check) {
  7464. strm.msg = 'incorrect data check';
  7465. state.mode = BAD;
  7466. break;
  7467. }
  7468. //=== INITBITS();
  7469. hold = 0;
  7470. bits = 0;
  7471. //===//
  7472. //Tracev((stderr, "inflate: check matches trailer\n"));
  7473. }
  7474. state.mode = LENGTH;
  7475. /* falls through */
  7476. case LENGTH:
  7477. if (state.wrap && state.flags) {
  7478. //=== NEEDBITS(32);
  7479. while (bits < 32) {
  7480. if (have === 0) { break inf_leave; }
  7481. have--;
  7482. hold += input[next++] << bits;
  7483. bits += 8;
  7484. }
  7485. //===//
  7486. if (hold !== (state.total & 0xffffffff)) {
  7487. strm.msg = 'incorrect length check';
  7488. state.mode = BAD;
  7489. break;
  7490. }
  7491. //=== INITBITS();
  7492. hold = 0;
  7493. bits = 0;
  7494. //===//
  7495. //Tracev((stderr, "inflate: length matches trailer\n"));
  7496. }
  7497. state.mode = DONE;
  7498. /* falls through */
  7499. case DONE:
  7500. ret = Z_STREAM_END;
  7501. break inf_leave;
  7502. case BAD:
  7503. ret = Z_DATA_ERROR;
  7504. break inf_leave;
  7505. case MEM:
  7506. return Z_MEM_ERROR;
  7507. case SYNC:
  7508. /* falls through */
  7509. default:
  7510. return Z_STREAM_ERROR;
  7511. }
  7512. }
  7513. // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave"
  7514. /*
  7515. Return from inflate(), updating the total counts and the check value.
  7516. If there was no progress during the inflate() call, return a buffer
  7517. error. Call updatewindow() to create and/or update the window state.
  7518. Note: a memory error from inflate() is non-recoverable.
  7519. */
  7520. //--- RESTORE() ---
  7521. strm.next_out = put;
  7522. strm.avail_out = left;
  7523. strm.next_in = next;
  7524. strm.avail_in = have;
  7525. state.hold = hold;
  7526. state.bits = bits;
  7527. //---
  7528. if (state.wsize || (_out !== strm.avail_out && state.mode < BAD &&
  7529. (state.mode < CHECK || flush !== Z_FINISH))) {
  7530. if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) {
  7531. state.mode = MEM;
  7532. return Z_MEM_ERROR;
  7533. }
  7534. }
  7535. _in -= strm.avail_in;
  7536. _out -= strm.avail_out;
  7537. strm.total_in += _in;
  7538. strm.total_out += _out;
  7539. state.total += _out;
  7540. if (state.wrap && _out) {
  7541. strm.adler = state.check = /*UPDATE(state.check, strm.next_out - _out, _out);*/
  7542. (state.flags ? crc32(state.check, output, _out, strm.next_out - _out) : adler32(state.check, output, _out, strm.next_out - _out));
  7543. }
  7544. strm.data_type = state.bits + (state.last ? 64 : 0) +
  7545. (state.mode === TYPE ? 128 : 0) +
  7546. (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0);
  7547. if (((_in === 0 && _out === 0) || flush === Z_FINISH) && ret === Z_OK) {
  7548. ret = Z_BUF_ERROR;
  7549. }
  7550. return ret;
  7551. }
  7552. function inflateEnd(strm) {
  7553. if (!strm || !strm.state /*|| strm->zfree == (free_func)0*/) {
  7554. return Z_STREAM_ERROR;
  7555. }
  7556. var state = strm.state;
  7557. if (state.window) {
  7558. state.window = null;
  7559. }
  7560. strm.state = null;
  7561. return Z_OK;
  7562. }
  7563. function inflateGetHeader(strm, head) {
  7564. var state;
  7565. /* check state */
  7566. if (!strm || !strm.state) { return Z_STREAM_ERROR; }
  7567. state = strm.state;
  7568. if ((state.wrap & 2) === 0) { return Z_STREAM_ERROR; }
  7569. /* save header structure */
  7570. state.head = head;
  7571. head.done = false;
  7572. return Z_OK;
  7573. }
  7574. exports.inflateReset = inflateReset;
  7575. exports.inflateReset2 = inflateReset2;
  7576. exports.inflateResetKeep = inflateResetKeep;
  7577. exports.inflateInit = inflateInit;
  7578. exports.inflateInit2 = inflateInit2;
  7579. exports.inflate = inflate;
  7580. exports.inflateEnd = inflateEnd;
  7581. exports.inflateGetHeader = inflateGetHeader;
  7582. exports.inflateInfo = 'pako inflate (from Nodeca project)';
  7583. /* Not implemented
  7584. exports.inflateCopy = inflateCopy;
  7585. exports.inflateGetDictionary = inflateGetDictionary;
  7586. exports.inflateMark = inflateMark;
  7587. exports.inflatePrime = inflatePrime;
  7588. exports.inflateSetDictionary = inflateSetDictionary;
  7589. exports.inflateSync = inflateSync;
  7590. exports.inflateSyncPoint = inflateSyncPoint;
  7591. exports.inflateUndermine = inflateUndermine;
  7592. */
  7593. },{"../utils/common":27,"./adler32":29,"./crc32":31,"./inffast":34,"./inftrees":36}],36:[function(_dereq_,module,exports){
  7594. 'use strict';
  7595. var utils = _dereq_('../utils/common');
  7596. var MAXBITS = 15;
  7597. var ENOUGH_LENS = 852;
  7598. var ENOUGH_DISTS = 592;
  7599. //var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);
  7600. var CODES = 0;
  7601. var LENS = 1;
  7602. var DISTS = 2;
  7603. var lbase = [ /* Length codes 257..285 base */
  7604. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  7605. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
  7606. ];
  7607. var lext = [ /* Length codes 257..285 extra */
  7608. 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
  7609. 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78
  7610. ];
  7611. var dbase = [ /* Distance codes 0..29 base */
  7612. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  7613. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  7614. 8193, 12289, 16385, 24577, 0, 0
  7615. ];
  7616. var dext = [ /* Distance codes 0..29 extra */
  7617. 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
  7618. 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
  7619. 28, 28, 29, 29, 64, 64
  7620. ];
  7621. module.exports = function inflate_table(type, lens, lens_index, codes, table, table_index, work, opts)
  7622. {
  7623. var bits = opts.bits;
  7624. //here = opts.here; /* table entry for duplication */
  7625. var len = 0; /* a code's length in bits */
  7626. var sym = 0; /* index of code symbols */
  7627. var min = 0, max = 0; /* minimum and maximum code lengths */
  7628. var root = 0; /* number of index bits for root table */
  7629. var curr = 0; /* number of index bits for current table */
  7630. var drop = 0; /* code bits to drop for sub-table */
  7631. var left = 0; /* number of prefix codes available */
  7632. var used = 0; /* code entries in table used */
  7633. var huff = 0; /* Huffman code */
  7634. var incr; /* for incrementing code, index */
  7635. var fill; /* index for replicating entries */
  7636. var low; /* low bits for current root entry */
  7637. var mask; /* mask for low root bits */
  7638. var next; /* next available space in table */
  7639. var base = null; /* base value table to use */
  7640. var base_index = 0;
  7641. // var shoextra; /* extra bits table to use */
  7642. var end; /* use base and extra for symbol > end */
  7643. var count = new utils.Buf16(MAXBITS+1); //[MAXBITS+1]; /* number of codes of each length */
  7644. var offs = new utils.Buf16(MAXBITS+1); //[MAXBITS+1]; /* offsets in table for each length */
  7645. var extra = null;
  7646. var extra_index = 0;
  7647. var here_bits, here_op, here_val;
  7648. /*
  7649. Process a set of code lengths to create a canonical Huffman code. The
  7650. code lengths are lens[0..codes-1]. Each length corresponds to the
  7651. symbols 0..codes-1. The Huffman code is generated by first sorting the
  7652. symbols by length from short to long, and retaining the symbol order
  7653. for codes with equal lengths. Then the code starts with all zero bits
  7654. for the first code of the shortest length, and the codes are integer
  7655. increments for the same length, and zeros are appended as the length
  7656. increases. For the deflate format, these bits are stored backwards
  7657. from their more natural integer increment ordering, and so when the
  7658. decoding tables are built in the large loop below, the integer codes
  7659. are incremented backwards.
  7660. This routine assumes, but does not check, that all of the entries in
  7661. lens[] are in the range 0..MAXBITS. The caller must assure this.
  7662. 1..MAXBITS is interpreted as that code length. zero means that that
  7663. symbol does not occur in this code.
  7664. The codes are sorted by computing a count of codes for each length,
  7665. creating from that a table of starting indices for each length in the
  7666. sorted table, and then entering the symbols in order in the sorted
  7667. table. The sorted table is work[], with that space being provided by
  7668. the caller.
  7669. The length counts are used for other purposes as well, i.e. finding
  7670. the minimum and maximum length codes, determining if there are any
  7671. codes at all, checking for a valid set of lengths, and looking ahead
  7672. at length counts to determine sub-table sizes when building the
  7673. decoding tables.
  7674. */
  7675. /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
  7676. for (len = 0; len <= MAXBITS; len++) {
  7677. count[len] = 0;
  7678. }
  7679. for (sym = 0; sym < codes; sym++) {
  7680. count[lens[lens_index + sym]]++;
  7681. }
  7682. /* bound code lengths, force root to be within code lengths */
  7683. root = bits;
  7684. for (max = MAXBITS; max >= 1; max--) {
  7685. if (count[max] !== 0) { break; }
  7686. }
  7687. if (root > max) {
  7688. root = max;
  7689. }
  7690. if (max === 0) { /* no symbols to code at all */
  7691. //table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */
  7692. //table.bits[opts.table_index] = 1; //here.bits = (var char)1;
  7693. //table.val[opts.table_index++] = 0; //here.val = (var short)0;
  7694. table[table_index++] = (1 << 24) | (64 << 16) | 0;
  7695. //table.op[opts.table_index] = 64;
  7696. //table.bits[opts.table_index] = 1;
  7697. //table.val[opts.table_index++] = 0;
  7698. table[table_index++] = (1 << 24) | (64 << 16) | 0;
  7699. opts.bits = 1;
  7700. return 0; /* no symbols, but wait for decoding to report error */
  7701. }
  7702. for (min = 1; min < max; min++) {
  7703. if (count[min] !== 0) { break; }
  7704. }
  7705. if (root < min) {
  7706. root = min;
  7707. }
  7708. /* check for an over-subscribed or incomplete set of lengths */
  7709. left = 1;
  7710. for (len = 1; len <= MAXBITS; len++) {
  7711. left <<= 1;
  7712. left -= count[len];
  7713. if (left < 0) {
  7714. return -1;
  7715. } /* over-subscribed */
  7716. }
  7717. if (left > 0 && (type === CODES || max !== 1)) {
  7718. return -1; /* incomplete set */
  7719. }
  7720. /* generate offsets into symbol table for each length for sorting */
  7721. offs[1] = 0;
  7722. for (len = 1; len < MAXBITS; len++) {
  7723. offs[len + 1] = offs[len] + count[len];
  7724. }
  7725. /* sort symbols by length, by symbol order within each length */
  7726. for (sym = 0; sym < codes; sym++) {
  7727. if (lens[lens_index + sym] !== 0) {
  7728. work[offs[lens[lens_index + sym]]++] = sym;
  7729. }
  7730. }
  7731. /*
  7732. Create and fill in decoding tables. In this loop, the table being
  7733. filled is at next and has curr index bits. The code being used is huff
  7734. with length len. That code is converted to an index by dropping drop
  7735. bits off of the bottom. For codes where len is less than drop + curr,
  7736. those top drop + curr - len bits are incremented through all values to
  7737. fill the table with replicated entries.
  7738. root is the number of index bits for the root table. When len exceeds
  7739. root, sub-tables are created pointed to by the root entry with an index
  7740. of the low root bits of huff. This is saved in low to check for when a
  7741. new sub-table should be started. drop is zero when the root table is
  7742. being filled, and drop is root when sub-tables are being filled.
  7743. When a new sub-table is needed, it is necessary to look ahead in the
  7744. code lengths to determine what size sub-table is needed. The length
  7745. counts are used for this, and so count[] is decremented as codes are
  7746. entered in the tables.
  7747. used keeps track of how many table entries have been allocated from the
  7748. provided *table space. It is checked for LENS and DIST tables against
  7749. the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
  7750. the initial root table size constants. See the comments in inftrees.h
  7751. for more information.
  7752. sym increments through all symbols, and the loop terminates when
  7753. all codes of length max, i.e. all codes, have been processed. This
  7754. routine permits incomplete codes, so another loop after this one fills
  7755. in the rest of the decoding tables with invalid code markers.
  7756. */
  7757. /* set up for code type */
  7758. // poor man optimization - use if-else instead of switch,
  7759. // to avoid deopts in old v8
  7760. if (type === CODES) {
  7761. base = extra = work; /* dummy value--not used */
  7762. end = 19;
  7763. } else if (type === LENS) {
  7764. base = lbase;
  7765. base_index -= 257;
  7766. extra = lext;
  7767. extra_index -= 257;
  7768. end = 256;
  7769. } else { /* DISTS */
  7770. base = dbase;
  7771. extra = dext;
  7772. end = -1;
  7773. }
  7774. /* initialize opts for loop */
  7775. huff = 0; /* starting code */
  7776. sym = 0; /* starting code symbol */
  7777. len = min; /* starting code length */
  7778. next = table_index; /* current table to fill in */
  7779. curr = root; /* current table index bits */
  7780. drop = 0; /* current bits to drop from code for index */
  7781. low = -1; /* trigger new sub-table when len > root */
  7782. used = 1 << root; /* use root table entries */
  7783. mask = used - 1; /* mask for comparing low */
  7784. /* check available table space */
  7785. if ((type === LENS && used > ENOUGH_LENS) ||
  7786. (type === DISTS && used > ENOUGH_DISTS)) {
  7787. return 1;
  7788. }
  7789. var i=0;
  7790. /* process all codes and make table entries */
  7791. for (;;) {
  7792. i++;
  7793. /* create table entry */
  7794. here_bits = len - drop;
  7795. if (work[sym] < end) {
  7796. here_op = 0;
  7797. here_val = work[sym];
  7798. }
  7799. else if (work[sym] > end) {
  7800. here_op = extra[extra_index + work[sym]];
  7801. here_val = base[base_index + work[sym]];
  7802. }
  7803. else {
  7804. here_op = 32 + 64; /* end of block */
  7805. here_val = 0;
  7806. }
  7807. /* replicate for those indices with low len bits equal to huff */
  7808. incr = 1 << (len - drop);
  7809. fill = 1 << curr;
  7810. min = fill; /* save offset to next table */
  7811. do {
  7812. fill -= incr;
  7813. table[next + (huff >> drop) + fill] = (here_bits << 24) | (here_op << 16) | here_val |0;
  7814. } while (fill !== 0);
  7815. /* backwards increment the len-bit code huff */
  7816. incr = 1 << (len - 1);
  7817. while (huff & incr) {
  7818. incr >>= 1;
  7819. }
  7820. if (incr !== 0) {
  7821. huff &= incr - 1;
  7822. huff += incr;
  7823. } else {
  7824. huff = 0;
  7825. }
  7826. /* go to next symbol, update count, len */
  7827. sym++;
  7828. if (--count[len] === 0) {
  7829. if (len === max) { break; }
  7830. len = lens[lens_index + work[sym]];
  7831. }
  7832. /* create new sub-table if needed */
  7833. if (len > root && (huff & mask) !== low) {
  7834. /* if first time, transition to sub-tables */
  7835. if (drop === 0) {
  7836. drop = root;
  7837. }
  7838. /* increment past last table */
  7839. next += min; /* here min is 1 << curr */
  7840. /* determine length of next table */
  7841. curr = len - drop;
  7842. left = 1 << curr;
  7843. while (curr + drop < max) {
  7844. left -= count[curr + drop];
  7845. if (left <= 0) { break; }
  7846. curr++;
  7847. left <<= 1;
  7848. }
  7849. /* check for enough space */
  7850. used += 1 << curr;
  7851. if ((type === LENS && used > ENOUGH_LENS) ||
  7852. (type === DISTS && used > ENOUGH_DISTS)) {
  7853. return 1;
  7854. }
  7855. /* point entry in root table to sub-table */
  7856. low = huff & mask;
  7857. /*table.op[low] = curr;
  7858. table.bits[low] = root;
  7859. table.val[low] = next - opts.table_index;*/
  7860. table[low] = (root << 24) | (curr << 16) | (next - table_index) |0;
  7861. }
  7862. }
  7863. /* fill in remaining table entry if code is incomplete (guaranteed to have
  7864. at most one remaining entry, since if the code is incomplete, the
  7865. maximum code length that was allowed to get this far is one bit) */
  7866. if (huff !== 0) {
  7867. //table.op[next + huff] = 64; /* invalid code marker */
  7868. //table.bits[next + huff] = len - drop;
  7869. //table.val[next + huff] = 0;
  7870. table[next + huff] = ((len - drop) << 24) | (64 << 16) |0;
  7871. }
  7872. /* set return parameters */
  7873. //opts.table_index += used;
  7874. opts.bits = root;
  7875. return 0;
  7876. };
  7877. },{"../utils/common":27}],37:[function(_dereq_,module,exports){
  7878. 'use strict';
  7879. module.exports = {
  7880. '2': 'need dictionary', /* Z_NEED_DICT 2 */
  7881. '1': 'stream end', /* Z_STREAM_END 1 */
  7882. '0': '', /* Z_OK 0 */
  7883. '-1': 'file error', /* Z_ERRNO (-1) */
  7884. '-2': 'stream error', /* Z_STREAM_ERROR (-2) */
  7885. '-3': 'data error', /* Z_DATA_ERROR (-3) */
  7886. '-4': 'insufficient memory', /* Z_MEM_ERROR (-4) */
  7887. '-5': 'buffer error', /* Z_BUF_ERROR (-5) */
  7888. '-6': 'incompatible version' /* Z_VERSION_ERROR (-6) */
  7889. };
  7890. },{}],38:[function(_dereq_,module,exports){
  7891. 'use strict';
  7892. var utils = _dereq_('../utils/common');
  7893. /* Public constants ==========================================================*/
  7894. /* ===========================================================================*/
  7895. //var Z_FILTERED = 1;
  7896. //var Z_HUFFMAN_ONLY = 2;
  7897. //var Z_RLE = 3;
  7898. var Z_FIXED = 4;
  7899. //var Z_DEFAULT_STRATEGY = 0;
  7900. /* Possible values of the data_type field (though see inflate()) */
  7901. var Z_BINARY = 0;
  7902. var Z_TEXT = 1;
  7903. //var Z_ASCII = 1; // = Z_TEXT
  7904. var Z_UNKNOWN = 2;
  7905. /*============================================================================*/
  7906. function zero(buf) { var len = buf.length; while (--len >= 0) { buf[len] = 0; } }
  7907. // From zutil.h
  7908. var STORED_BLOCK = 0;
  7909. var STATIC_TREES = 1;
  7910. var DYN_TREES = 2;
  7911. /* The three kinds of block type */
  7912. var MIN_MATCH = 3;
  7913. var MAX_MATCH = 258;
  7914. /* The minimum and maximum match lengths */
  7915. // From deflate.h
  7916. /* ===========================================================================
  7917. * Internal compression state.
  7918. */
  7919. var LENGTH_CODES = 29;
  7920. /* number of length codes, not counting the special END_BLOCK code */
  7921. var LITERALS = 256;
  7922. /* number of literal bytes 0..255 */
  7923. var L_CODES = LITERALS + 1 + LENGTH_CODES;
  7924. /* number of Literal or Length codes, including the END_BLOCK code */
  7925. var D_CODES = 30;
  7926. /* number of distance codes */
  7927. var BL_CODES = 19;
  7928. /* number of codes used to transfer the bit lengths */
  7929. var HEAP_SIZE = 2*L_CODES + 1;
  7930. /* maximum heap size */
  7931. var MAX_BITS = 15;
  7932. /* All codes must not exceed MAX_BITS bits */
  7933. var Buf_size = 16;
  7934. /* size of bit buffer in bi_buf */
  7935. /* ===========================================================================
  7936. * Constants
  7937. */
  7938. var MAX_BL_BITS = 7;
  7939. /* Bit length codes must not exceed MAX_BL_BITS bits */
  7940. var END_BLOCK = 256;
  7941. /* end of block literal code */
  7942. var REP_3_6 = 16;
  7943. /* repeat previous bit length 3-6 times (2 bits of repeat count) */
  7944. var REPZ_3_10 = 17;
  7945. /* repeat a zero length 3-10 times (3 bits of repeat count) */
  7946. var REPZ_11_138 = 18;
  7947. /* repeat a zero length 11-138 times (7 bits of repeat count) */
  7948. var extra_lbits = /* extra bits for each length code */
  7949. [0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0];
  7950. var extra_dbits = /* extra bits for each distance code */
  7951. [0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13];
  7952. var extra_blbits = /* extra bits for each bit length code */
  7953. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7];
  7954. var bl_order =
  7955. [16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15];
  7956. /* The lengths of the bit length codes are sent in order of decreasing
  7957. * probability, to avoid transmitting the lengths for unused bit length codes.
  7958. */
  7959. /* ===========================================================================
  7960. * Local data. These are initialized only once.
  7961. */
  7962. // We pre-fill arrays with 0 to avoid uninitialized gaps
  7963. var DIST_CODE_LEN = 512; /* see definition of array dist_code below */
  7964. // !!!! Use flat array insdead of structure, Freq = i*2, Len = i*2+1
  7965. var static_ltree = new Array((L_CODES+2) * 2);
  7966. zero(static_ltree);
  7967. /* The static literal tree. Since the bit lengths are imposed, there is no
  7968. * need for the L_CODES extra codes used during heap construction. However
  7969. * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
  7970. * below).
  7971. */
  7972. var static_dtree = new Array(D_CODES * 2);
  7973. zero(static_dtree);
  7974. /* The static distance tree. (Actually a trivial tree since all codes use
  7975. * 5 bits.)
  7976. */
  7977. var _dist_code = new Array(DIST_CODE_LEN);
  7978. zero(_dist_code);
  7979. /* Distance codes. The first 256 values correspond to the distances
  7980. * 3 .. 258, the last 256 values correspond to the top 8 bits of
  7981. * the 15 bit distances.
  7982. */
  7983. var _length_code = new Array(MAX_MATCH-MIN_MATCH+1);
  7984. zero(_length_code);
  7985. /* length code for each normalized match length (0 == MIN_MATCH) */
  7986. var base_length = new Array(LENGTH_CODES);
  7987. zero(base_length);
  7988. /* First normalized length for each code (0 = MIN_MATCH) */
  7989. var base_dist = new Array(D_CODES);
  7990. zero(base_dist);
  7991. /* First normalized distance for each code (0 = distance of 1) */
  7992. var StaticTreeDesc = function (static_tree, extra_bits, extra_base, elems, max_length) {
  7993. this.static_tree = static_tree; /* static tree or NULL */
  7994. this.extra_bits = extra_bits; /* extra bits for each code or NULL */
  7995. this.extra_base = extra_base; /* base index for extra_bits */
  7996. this.elems = elems; /* max number of elements in the tree */
  7997. this.max_length = max_length; /* max bit length for the codes */
  7998. // show if `static_tree` has data or dummy - needed for monomorphic objects
  7999. this.has_stree = static_tree && static_tree.length;
  8000. };
  8001. var static_l_desc;
  8002. var static_d_desc;
  8003. var static_bl_desc;
  8004. var TreeDesc = function(dyn_tree, stat_desc) {
  8005. this.dyn_tree = dyn_tree; /* the dynamic tree */
  8006. this.max_code = 0; /* largest code with non zero frequency */
  8007. this.stat_desc = stat_desc; /* the corresponding static tree */
  8008. };
  8009. function d_code(dist) {
  8010. return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)];
  8011. }
  8012. /* ===========================================================================
  8013. * Output a short LSB first on the stream.
  8014. * IN assertion: there is enough room in pendingBuf.
  8015. */
  8016. function put_short (s, w) {
  8017. // put_byte(s, (uch)((w) & 0xff));
  8018. // put_byte(s, (uch)((ush)(w) >> 8));
  8019. s.pending_buf[s.pending++] = (w) & 0xff;
  8020. s.pending_buf[s.pending++] = (w >>> 8) & 0xff;
  8021. }
  8022. /* ===========================================================================
  8023. * Send a value on a given number of bits.
  8024. * IN assertion: length <= 16 and value fits in length bits.
  8025. */
  8026. function send_bits(s, value, length) {
  8027. if (s.bi_valid > (Buf_size - length)) {
  8028. s.bi_buf |= (value << s.bi_valid) & 0xffff;
  8029. put_short(s, s.bi_buf);
  8030. s.bi_buf = value >> (Buf_size - s.bi_valid);
  8031. s.bi_valid += length - Buf_size;
  8032. } else {
  8033. s.bi_buf |= (value << s.bi_valid) & 0xffff;
  8034. s.bi_valid += length;
  8035. }
  8036. }
  8037. function send_code(s, c, tree) {
  8038. send_bits(s, tree[c*2]/*.Code*/, tree[c*2 + 1]/*.Len*/);
  8039. }
  8040. /* ===========================================================================
  8041. * Reverse the first len bits of a code, using straightforward code (a faster
  8042. * method would use a table)
  8043. * IN assertion: 1 <= len <= 15
  8044. */
  8045. function bi_reverse(code, len) {
  8046. var res = 0;
  8047. do {
  8048. res |= code & 1;
  8049. code >>>= 1;
  8050. res <<= 1;
  8051. } while (--len > 0);
  8052. return res >>> 1;
  8053. }
  8054. /* ===========================================================================
  8055. * Flush the bit buffer, keeping at most 7 bits in it.
  8056. */
  8057. function bi_flush(s) {
  8058. if (s.bi_valid === 16) {
  8059. put_short(s, s.bi_buf);
  8060. s.bi_buf = 0;
  8061. s.bi_valid = 0;
  8062. } else if (s.bi_valid >= 8) {
  8063. s.pending_buf[s.pending++] = s.bi_buf & 0xff;
  8064. s.bi_buf >>= 8;
  8065. s.bi_valid -= 8;
  8066. }
  8067. }
  8068. /* ===========================================================================
  8069. * Compute the optimal bit lengths for a tree and update the total bit length
  8070. * for the current block.
  8071. * IN assertion: the fields freq and dad are set, heap[heap_max] and
  8072. * above are the tree nodes sorted by increasing frequency.
  8073. * OUT assertions: the field len is set to the optimal bit length, the
  8074. * array bl_count contains the frequencies for each bit length.
  8075. * The length opt_len is updated; static_len is also updated if stree is
  8076. * not null.
  8077. */
  8078. function gen_bitlen(s, desc)
  8079. // deflate_state *s;
  8080. // tree_desc *desc; /* the tree descriptor */
  8081. {
  8082. var tree = desc.dyn_tree;
  8083. var max_code = desc.max_code;
  8084. var stree = desc.stat_desc.static_tree;
  8085. var has_stree = desc.stat_desc.has_stree;
  8086. var extra = desc.stat_desc.extra_bits;
  8087. var base = desc.stat_desc.extra_base;
  8088. var max_length = desc.stat_desc.max_length;
  8089. var h; /* heap index */
  8090. var n, m; /* iterate over the tree elements */
  8091. var bits; /* bit length */
  8092. var xbits; /* extra bits */
  8093. var f; /* frequency */
  8094. var overflow = 0; /* number of elements with bit length too large */
  8095. for (bits = 0; bits <= MAX_BITS; bits++) {
  8096. s.bl_count[bits] = 0;
  8097. }
  8098. /* In a first pass, compute the optimal bit lengths (which may
  8099. * overflow in the case of the bit length tree).
  8100. */
  8101. tree[s.heap[s.heap_max]*2 + 1]/*.Len*/ = 0; /* root of the heap */
  8102. for (h = s.heap_max+1; h < HEAP_SIZE; h++) {
  8103. n = s.heap[h];
  8104. bits = tree[tree[n*2 +1]/*.Dad*/ * 2 + 1]/*.Len*/ + 1;
  8105. if (bits > max_length) {
  8106. bits = max_length;
  8107. overflow++;
  8108. }
  8109. tree[n*2 + 1]/*.Len*/ = bits;
  8110. /* We overwrite tree[n].Dad which is no longer needed */
  8111. if (n > max_code) { continue; } /* not a leaf node */
  8112. s.bl_count[bits]++;
  8113. xbits = 0;
  8114. if (n >= base) {
  8115. xbits = extra[n-base];
  8116. }
  8117. f = tree[n * 2]/*.Freq*/;
  8118. s.opt_len += f * (bits + xbits);
  8119. if (has_stree) {
  8120. s.static_len += f * (stree[n*2 + 1]/*.Len*/ + xbits);
  8121. }
  8122. }
  8123. if (overflow === 0) { return; }
  8124. // Trace((stderr,"\nbit length overflow\n"));
  8125. /* This happens for example on obj2 and pic of the Calgary corpus */
  8126. /* Find the first bit length which could increase: */
  8127. do {
  8128. bits = max_length-1;
  8129. while (s.bl_count[bits] === 0) { bits--; }
  8130. s.bl_count[bits]--; /* move one leaf down the tree */
  8131. s.bl_count[bits+1] += 2; /* move one overflow item as its brother */
  8132. s.bl_count[max_length]--;
  8133. /* The brother of the overflow item also moves one step up,
  8134. * but this does not affect bl_count[max_length]
  8135. */
  8136. overflow -= 2;
  8137. } while (overflow > 0);
  8138. /* Now recompute all bit lengths, scanning in increasing frequency.
  8139. * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  8140. * lengths instead of fixing only the wrong ones. This idea is taken
  8141. * from 'ar' written by Haruhiko Okumura.)
  8142. */
  8143. for (bits = max_length; bits !== 0; bits--) {
  8144. n = s.bl_count[bits];
  8145. while (n !== 0) {
  8146. m = s.heap[--h];
  8147. if (m > max_code) { continue; }
  8148. if (tree[m*2 + 1]/*.Len*/ !== bits) {
  8149. // Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
  8150. s.opt_len += (bits - tree[m*2 + 1]/*.Len*/)*tree[m*2]/*.Freq*/;
  8151. tree[m*2 + 1]/*.Len*/ = bits;
  8152. }
  8153. n--;
  8154. }
  8155. }
  8156. }
  8157. /* ===========================================================================
  8158. * Generate the codes for a given tree and bit counts (which need not be
  8159. * optimal).
  8160. * IN assertion: the array bl_count contains the bit length statistics for
  8161. * the given tree and the field len is set for all tree elements.
  8162. * OUT assertion: the field code is set for all tree elements of non
  8163. * zero code length.
  8164. */
  8165. function gen_codes(tree, max_code, bl_count)
  8166. // ct_data *tree; /* the tree to decorate */
  8167. // int max_code; /* largest code with non zero frequency */
  8168. // ushf *bl_count; /* number of codes at each bit length */
  8169. {
  8170. var next_code = new Array(MAX_BITS+1); /* next code value for each bit length */
  8171. var code = 0; /* running code value */
  8172. var bits; /* bit index */
  8173. var n; /* code index */
  8174. /* The distribution counts are first used to generate the code values
  8175. * without bit reversal.
  8176. */
  8177. for (bits = 1; bits <= MAX_BITS; bits++) {
  8178. next_code[bits] = code = (code + bl_count[bits-1]) << 1;
  8179. }
  8180. /* Check that the bit counts in bl_count are consistent. The last code
  8181. * must be all ones.
  8182. */
  8183. //Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  8184. // "inconsistent bit counts");
  8185. //Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  8186. for (n = 0; n <= max_code; n++) {
  8187. var len = tree[n*2 + 1]/*.Len*/;
  8188. if (len === 0) { continue; }
  8189. /* Now reverse the bits */
  8190. tree[n*2]/*.Code*/ = bi_reverse(next_code[len]++, len);
  8191. //Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
  8192. // n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
  8193. }
  8194. }
  8195. /* ===========================================================================
  8196. * Initialize the various 'constant' tables.
  8197. */
  8198. function tr_static_init() {
  8199. var n; /* iterates over tree elements */
  8200. var bits; /* bit counter */
  8201. var length; /* length value */
  8202. var code; /* code value */
  8203. var dist; /* distance index */
  8204. var bl_count = new Array(MAX_BITS+1);
  8205. /* number of codes at each bit length for an optimal tree */
  8206. // do check in _tr_init()
  8207. //if (static_init_done) return;
  8208. /* For some embedded targets, global variables are not initialized: */
  8209. /*#ifdef NO_INIT_GLOBAL_POINTERS
  8210. static_l_desc.static_tree = static_ltree;
  8211. static_l_desc.extra_bits = extra_lbits;
  8212. static_d_desc.static_tree = static_dtree;
  8213. static_d_desc.extra_bits = extra_dbits;
  8214. static_bl_desc.extra_bits = extra_blbits;
  8215. #endif*/
  8216. /* Initialize the mapping length (0..255) -> length code (0..28) */
  8217. length = 0;
  8218. for (code = 0; code < LENGTH_CODES-1; code++) {
  8219. base_length[code] = length;
  8220. for (n = 0; n < (1<<extra_lbits[code]); n++) {
  8221. _length_code[length++] = code;
  8222. }
  8223. }
  8224. //Assert (length == 256, "tr_static_init: length != 256");
  8225. /* Note that the length 255 (match length 258) can be represented
  8226. * in two different ways: code 284 + 5 bits or code 285, so we
  8227. * overwrite length_code[255] to use the best encoding:
  8228. */
  8229. _length_code[length-1] = code;
  8230. /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
  8231. dist = 0;
  8232. for (code = 0 ; code < 16; code++) {
  8233. base_dist[code] = dist;
  8234. for (n = 0; n < (1<<extra_dbits[code]); n++) {
  8235. _dist_code[dist++] = code;
  8236. }
  8237. }
  8238. //Assert (dist == 256, "tr_static_init: dist != 256");
  8239. dist >>= 7; /* from now on, all distances are divided by 128 */
  8240. for ( ; code < D_CODES; code++) {
  8241. base_dist[code] = dist << 7;
  8242. for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
  8243. _dist_code[256 + dist++] = code;
  8244. }
  8245. }
  8246. //Assert (dist == 256, "tr_static_init: 256+dist != 512");
  8247. /* Construct the codes of the static literal tree */
  8248. for (bits = 0; bits <= MAX_BITS; bits++) {
  8249. bl_count[bits] = 0;
  8250. }
  8251. n = 0;
  8252. while (n <= 143) {
  8253. static_ltree[n*2 + 1]/*.Len*/ = 8;
  8254. n++;
  8255. bl_count[8]++;
  8256. }
  8257. while (n <= 255) {
  8258. static_ltree[n*2 + 1]/*.Len*/ = 9;
  8259. n++;
  8260. bl_count[9]++;
  8261. }
  8262. while (n <= 279) {
  8263. static_ltree[n*2 + 1]/*.Len*/ = 7;
  8264. n++;
  8265. bl_count[7]++;
  8266. }
  8267. while (n <= 287) {
  8268. static_ltree[n*2 + 1]/*.Len*/ = 8;
  8269. n++;
  8270. bl_count[8]++;
  8271. }
  8272. /* Codes 286 and 287 do not exist, but we must include them in the
  8273. * tree construction to get a canonical Huffman tree (longest code
  8274. * all ones)
  8275. */
  8276. gen_codes(static_ltree, L_CODES+1, bl_count);
  8277. /* The static distance tree is trivial: */
  8278. for (n = 0; n < D_CODES; n++) {
  8279. static_dtree[n*2 + 1]/*.Len*/ = 5;
  8280. static_dtree[n*2]/*.Code*/ = bi_reverse(n, 5);
  8281. }
  8282. // Now data ready and we can init static trees
  8283. static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS);
  8284. static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES, MAX_BITS);
  8285. static_bl_desc =new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES, MAX_BL_BITS);
  8286. //static_init_done = true;
  8287. }
  8288. /* ===========================================================================
  8289. * Initialize a new block.
  8290. */
  8291. function init_block(s) {
  8292. var n; /* iterates over tree elements */
  8293. /* Initialize the trees. */
  8294. for (n = 0; n < L_CODES; n++) { s.dyn_ltree[n*2]/*.Freq*/ = 0; }
  8295. for (n = 0; n < D_CODES; n++) { s.dyn_dtree[n*2]/*.Freq*/ = 0; }
  8296. for (n = 0; n < BL_CODES; n++) { s.bl_tree[n*2]/*.Freq*/ = 0; }
  8297. s.dyn_ltree[END_BLOCK*2]/*.Freq*/ = 1;
  8298. s.opt_len = s.static_len = 0;
  8299. s.last_lit = s.matches = 0;
  8300. }
  8301. /* ===========================================================================
  8302. * Flush the bit buffer and align the output on a byte boundary
  8303. */
  8304. function bi_windup(s)
  8305. {
  8306. if (s.bi_valid > 8) {
  8307. put_short(s, s.bi_buf);
  8308. } else if (s.bi_valid > 0) {
  8309. //put_byte(s, (Byte)s->bi_buf);
  8310. s.pending_buf[s.pending++] = s.bi_buf;
  8311. }
  8312. s.bi_buf = 0;
  8313. s.bi_valid = 0;
  8314. }
  8315. /* ===========================================================================
  8316. * Copy a stored block, storing first the length and its
  8317. * one's complement if requested.
  8318. */
  8319. function copy_block(s, buf, len, header)
  8320. //DeflateState *s;
  8321. //charf *buf; /* the input data */
  8322. //unsigned len; /* its length */
  8323. //int header; /* true if block header must be written */
  8324. {
  8325. bi_windup(s); /* align on byte boundary */
  8326. if (header) {
  8327. put_short(s, len);
  8328. put_short(s, ~len);
  8329. }
  8330. // while (len--) {
  8331. // put_byte(s, *buf++);
  8332. // }
  8333. utils.arraySet(s.pending_buf, s.window, buf, len, s.pending);
  8334. s.pending += len;
  8335. }
  8336. /* ===========================================================================
  8337. * Compares to subtrees, using the tree depth as tie breaker when
  8338. * the subtrees have equal frequency. This minimizes the worst case length.
  8339. */
  8340. function smaller(tree, n, m, depth) {
  8341. var _n2 = n*2;
  8342. var _m2 = m*2;
  8343. return (tree[_n2]/*.Freq*/ < tree[_m2]/*.Freq*/ ||
  8344. (tree[_n2]/*.Freq*/ === tree[_m2]/*.Freq*/ && depth[n] <= depth[m]));
  8345. }
  8346. /* ===========================================================================
  8347. * Restore the heap property by moving down the tree starting at node k,
  8348. * exchanging a node with the smallest of its two sons if necessary, stopping
  8349. * when the heap property is re-established (each father smaller than its
  8350. * two sons).
  8351. */
  8352. function pqdownheap(s, tree, k)
  8353. // deflate_state *s;
  8354. // ct_data *tree; /* the tree to restore */
  8355. // int k; /* node to move down */
  8356. {
  8357. var v = s.heap[k];
  8358. var j = k << 1; /* left son of k */
  8359. while (j <= s.heap_len) {
  8360. /* Set j to the smallest of the two sons: */
  8361. if (j < s.heap_len &&
  8362. smaller(tree, s.heap[j+1], s.heap[j], s.depth)) {
  8363. j++;
  8364. }
  8365. /* Exit if v is smaller than both sons */
  8366. if (smaller(tree, v, s.heap[j], s.depth)) { break; }
  8367. /* Exchange v with the smallest son */
  8368. s.heap[k] = s.heap[j];
  8369. k = j;
  8370. /* And continue down the tree, setting j to the left son of k */
  8371. j <<= 1;
  8372. }
  8373. s.heap[k] = v;
  8374. }
  8375. // inlined manually
  8376. // var SMALLEST = 1;
  8377. /* ===========================================================================
  8378. * Send the block data compressed using the given Huffman trees
  8379. */
  8380. function compress_block(s, ltree, dtree)
  8381. // deflate_state *s;
  8382. // const ct_data *ltree; /* literal tree */
  8383. // const ct_data *dtree; /* distance tree */
  8384. {
  8385. var dist; /* distance of matched string */
  8386. var lc; /* match length or unmatched char (if dist == 0) */
  8387. var lx = 0; /* running index in l_buf */
  8388. var code; /* the code to send */
  8389. var extra; /* number of extra bits to send */
  8390. if (s.last_lit !== 0) {
  8391. do {
  8392. dist = (s.pending_buf[s.d_buf + lx*2] << 8) | (s.pending_buf[s.d_buf + lx*2 + 1]);
  8393. lc = s.pending_buf[s.l_buf + lx];
  8394. lx++;
  8395. if (dist === 0) {
  8396. send_code(s, lc, ltree); /* send a literal byte */
  8397. //Tracecv(isgraph(lc), (stderr," '%c' ", lc));
  8398. } else {
  8399. /* Here, lc is the match length - MIN_MATCH */
  8400. code = _length_code[lc];
  8401. send_code(s, code+LITERALS+1, ltree); /* send the length code */
  8402. extra = extra_lbits[code];
  8403. if (extra !== 0) {
  8404. lc -= base_length[code];
  8405. send_bits(s, lc, extra); /* send the extra length bits */
  8406. }
  8407. dist--; /* dist is now the match distance - 1 */
  8408. code = d_code(dist);
  8409. //Assert (code < D_CODES, "bad d_code");
  8410. send_code(s, code, dtree); /* send the distance code */
  8411. extra = extra_dbits[code];
  8412. if (extra !== 0) {
  8413. dist -= base_dist[code];
  8414. send_bits(s, dist, extra); /* send the extra distance bits */
  8415. }
  8416. } /* literal or match pair ? */
  8417. /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
  8418. //Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
  8419. // "pendingBuf overflow");
  8420. } while (lx < s.last_lit);
  8421. }
  8422. send_code(s, END_BLOCK, ltree);
  8423. }
  8424. /* ===========================================================================
  8425. * Construct one Huffman tree and assigns the code bit strings and lengths.
  8426. * Update the total bit length for the current block.
  8427. * IN assertion: the field freq is set for all tree elements.
  8428. * OUT assertions: the fields len and code are set to the optimal bit length
  8429. * and corresponding code. The length opt_len is updated; static_len is
  8430. * also updated if stree is not null. The field max_code is set.
  8431. */
  8432. function build_tree(s, desc)
  8433. // deflate_state *s;
  8434. // tree_desc *desc; /* the tree descriptor */
  8435. {
  8436. var tree = desc.dyn_tree;
  8437. var stree = desc.stat_desc.static_tree;
  8438. var has_stree = desc.stat_desc.has_stree;
  8439. var elems = desc.stat_desc.elems;
  8440. var n, m; /* iterate over heap elements */
  8441. var max_code = -1; /* largest code with non zero frequency */
  8442. var node; /* new node being created */
  8443. /* Construct the initial heap, with least frequent element in
  8444. * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  8445. * heap[0] is not used.
  8446. */
  8447. s.heap_len = 0;
  8448. s.heap_max = HEAP_SIZE;
  8449. for (n = 0; n < elems; n++) {
  8450. if (tree[n * 2]/*.Freq*/ !== 0) {
  8451. s.heap[++s.heap_len] = max_code = n;
  8452. s.depth[n] = 0;
  8453. } else {
  8454. tree[n*2 + 1]/*.Len*/ = 0;
  8455. }
  8456. }
  8457. /* The pkzip format requires that at least one distance code exists,
  8458. * and that at least one bit should be sent even if there is only one
  8459. * possible code. So to avoid special checks later on we force at least
  8460. * two codes of non zero frequency.
  8461. */
  8462. while (s.heap_len < 2) {
  8463. node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0);
  8464. tree[node * 2]/*.Freq*/ = 1;
  8465. s.depth[node] = 0;
  8466. s.opt_len--;
  8467. if (has_stree) {
  8468. s.static_len -= stree[node*2 + 1]/*.Len*/;
  8469. }
  8470. /* node is 0 or 1 so it does not have extra bits */
  8471. }
  8472. desc.max_code = max_code;
  8473. /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  8474. * establish sub-heaps of increasing lengths:
  8475. */
  8476. for (n = (s.heap_len >> 1/*int /2*/); n >= 1; n--) { pqdownheap(s, tree, n); }
  8477. /* Construct the Huffman tree by repeatedly combining the least two
  8478. * frequent nodes.
  8479. */
  8480. node = elems; /* next internal node of the tree */
  8481. do {
  8482. //pqremove(s, tree, n); /* n = node of least frequency */
  8483. /*** pqremove ***/
  8484. n = s.heap[1/*SMALLEST*/];
  8485. s.heap[1/*SMALLEST*/] = s.heap[s.heap_len--];
  8486. pqdownheap(s, tree, 1/*SMALLEST*/);
  8487. /***/
  8488. m = s.heap[1/*SMALLEST*/]; /* m = node of next least frequency */
  8489. s.heap[--s.heap_max] = n; /* keep the nodes sorted by frequency */
  8490. s.heap[--s.heap_max] = m;
  8491. /* Create a new node father of n and m */
  8492. tree[node * 2]/*.Freq*/ = tree[n * 2]/*.Freq*/ + tree[m * 2]/*.Freq*/;
  8493. s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1;
  8494. tree[n*2 + 1]/*.Dad*/ = tree[m*2 + 1]/*.Dad*/ = node;
  8495. /* and insert the new node in the heap */
  8496. s.heap[1/*SMALLEST*/] = node++;
  8497. pqdownheap(s, tree, 1/*SMALLEST*/);
  8498. } while (s.heap_len >= 2);
  8499. s.heap[--s.heap_max] = s.heap[1/*SMALLEST*/];
  8500. /* At this point, the fields freq and dad are set. We can now
  8501. * generate the bit lengths.
  8502. */
  8503. gen_bitlen(s, desc);
  8504. /* The field len is now set, we can generate the bit codes */
  8505. gen_codes(tree, max_code, s.bl_count);
  8506. }
  8507. /* ===========================================================================
  8508. * Scan a literal or distance tree to determine the frequencies of the codes
  8509. * in the bit length tree.
  8510. */
  8511. function scan_tree(s, tree, max_code)
  8512. // deflate_state *s;
  8513. // ct_data *tree; /* the tree to be scanned */
  8514. // int max_code; /* and its largest code of non zero frequency */
  8515. {
  8516. var n; /* iterates over all tree elements */
  8517. var prevlen = -1; /* last emitted length */
  8518. var curlen; /* length of current code */
  8519. var nextlen = tree[0*2 + 1]/*.Len*/; /* length of next code */
  8520. var count = 0; /* repeat count of the current code */
  8521. var max_count = 7; /* max repeat count */
  8522. var min_count = 4; /* min repeat count */
  8523. if (nextlen === 0) {
  8524. max_count = 138;
  8525. min_count = 3;
  8526. }
  8527. tree[(max_code+1)*2 + 1]/*.Len*/ = 0xffff; /* guard */
  8528. for (n = 0; n <= max_code; n++) {
  8529. curlen = nextlen;
  8530. nextlen = tree[(n+1)*2 + 1]/*.Len*/;
  8531. if (++count < max_count && curlen === nextlen) {
  8532. continue;
  8533. } else if (count < min_count) {
  8534. s.bl_tree[curlen * 2]/*.Freq*/ += count;
  8535. } else if (curlen !== 0) {
  8536. if (curlen !== prevlen) { s.bl_tree[curlen * 2]/*.Freq*/++; }
  8537. s.bl_tree[REP_3_6*2]/*.Freq*/++;
  8538. } else if (count <= 10) {
  8539. s.bl_tree[REPZ_3_10*2]/*.Freq*/++;
  8540. } else {
  8541. s.bl_tree[REPZ_11_138*2]/*.Freq*/++;
  8542. }
  8543. count = 0;
  8544. prevlen = curlen;
  8545. if (nextlen === 0) {
  8546. max_count = 138;
  8547. min_count = 3;
  8548. } else if (curlen === nextlen) {
  8549. max_count = 6;
  8550. min_count = 3;
  8551. } else {
  8552. max_count = 7;
  8553. min_count = 4;
  8554. }
  8555. }
  8556. }
  8557. /* ===========================================================================
  8558. * Send a literal or distance tree in compressed form, using the codes in
  8559. * bl_tree.
  8560. */
  8561. function send_tree(s, tree, max_code)
  8562. // deflate_state *s;
  8563. // ct_data *tree; /* the tree to be scanned */
  8564. // int max_code; /* and its largest code of non zero frequency */
  8565. {
  8566. var n; /* iterates over all tree elements */
  8567. var prevlen = -1; /* last emitted length */
  8568. var curlen; /* length of current code */
  8569. var nextlen = tree[0*2 + 1]/*.Len*/; /* length of next code */
  8570. var count = 0; /* repeat count of the current code */
  8571. var max_count = 7; /* max repeat count */
  8572. var min_count = 4; /* min repeat count */
  8573. /* tree[max_code+1].Len = -1; */ /* guard already set */
  8574. if (nextlen === 0) {
  8575. max_count = 138;
  8576. min_count = 3;
  8577. }
  8578. for (n = 0; n <= max_code; n++) {
  8579. curlen = nextlen;
  8580. nextlen = tree[(n+1)*2 + 1]/*.Len*/;
  8581. if (++count < max_count && curlen === nextlen) {
  8582. continue;
  8583. } else if (count < min_count) {
  8584. do { send_code(s, curlen, s.bl_tree); } while (--count !== 0);
  8585. } else if (curlen !== 0) {
  8586. if (curlen !== prevlen) {
  8587. send_code(s, curlen, s.bl_tree);
  8588. count--;
  8589. }
  8590. //Assert(count >= 3 && count <= 6, " 3_6?");
  8591. send_code(s, REP_3_6, s.bl_tree);
  8592. send_bits(s, count-3, 2);
  8593. } else if (count <= 10) {
  8594. send_code(s, REPZ_3_10, s.bl_tree);
  8595. send_bits(s, count-3, 3);
  8596. } else {
  8597. send_code(s, REPZ_11_138, s.bl_tree);
  8598. send_bits(s, count-11, 7);
  8599. }
  8600. count = 0;
  8601. prevlen = curlen;
  8602. if (nextlen === 0) {
  8603. max_count = 138;
  8604. min_count = 3;
  8605. } else if (curlen === nextlen) {
  8606. max_count = 6;
  8607. min_count = 3;
  8608. } else {
  8609. max_count = 7;
  8610. min_count = 4;
  8611. }
  8612. }
  8613. }
  8614. /* ===========================================================================
  8615. * Construct the Huffman tree for the bit lengths and return the index in
  8616. * bl_order of the last bit length code to send.
  8617. */
  8618. function build_bl_tree(s) {
  8619. var max_blindex; /* index of last bit length code of non zero freq */
  8620. /* Determine the bit length frequencies for literal and distance trees */
  8621. scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
  8622. scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
  8623. /* Build the bit length tree: */
  8624. build_tree(s, s.bl_desc);
  8625. /* opt_len now includes the length of the tree representations, except
  8626. * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
  8627. */
  8628. /* Determine the number of bit length codes to send. The pkzip format
  8629. * requires that at least 4 bit length codes be sent. (appnote.txt says
  8630. * 3 but the actual value used is 4.)
  8631. */
  8632. for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
  8633. if (s.bl_tree[bl_order[max_blindex]*2 + 1]/*.Len*/ !== 0) {
  8634. break;
  8635. }
  8636. }
  8637. /* Update opt_len to include the bit length tree and counts */
  8638. s.opt_len += 3*(max_blindex+1) + 5+5+4;
  8639. //Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
  8640. // s->opt_len, s->static_len));
  8641. return max_blindex;
  8642. }
  8643. /* ===========================================================================
  8644. * Send the header for a block using dynamic Huffman trees: the counts, the
  8645. * lengths of the bit length codes, the literal tree and the distance tree.
  8646. * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  8647. */
  8648. function send_all_trees(s, lcodes, dcodes, blcodes)
  8649. // deflate_state *s;
  8650. // int lcodes, dcodes, blcodes; /* number of codes for each tree */
  8651. {
  8652. var rank; /* index in bl_order */
  8653. //Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
  8654. //Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
  8655. // "too many codes");
  8656. //Tracev((stderr, "\nbl counts: "));
  8657. send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
  8658. send_bits(s, dcodes-1, 5);
  8659. send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
  8660. for (rank = 0; rank < blcodes; rank++) {
  8661. //Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
  8662. send_bits(s, s.bl_tree[bl_order[rank]*2 + 1]/*.Len*/, 3);
  8663. }
  8664. //Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
  8665. send_tree(s, s.dyn_ltree, lcodes-1); /* literal tree */
  8666. //Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
  8667. send_tree(s, s.dyn_dtree, dcodes-1); /* distance tree */
  8668. //Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
  8669. }
  8670. /* ===========================================================================
  8671. * Check if the data type is TEXT or BINARY, using the following algorithm:
  8672. * - TEXT if the two conditions below are satisfied:
  8673. * a) There are no non-portable control characters belonging to the
  8674. * "black list" (0..6, 14..25, 28..31).
  8675. * b) There is at least one printable character belonging to the
  8676. * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
  8677. * - BINARY otherwise.
  8678. * - The following partially-portable control characters form a
  8679. * "gray list" that is ignored in this detection algorithm:
  8680. * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
  8681. * IN assertion: the fields Freq of dyn_ltree are set.
  8682. */
  8683. function detect_data_type(s) {
  8684. /* black_mask is the bit mask of black-listed bytes
  8685. * set bits 0..6, 14..25, and 28..31
  8686. * 0xf3ffc07f = binary 11110011111111111100000001111111
  8687. */
  8688. var black_mask = 0xf3ffc07f;
  8689. var n;
  8690. /* Check for non-textual ("black-listed") bytes. */
  8691. for (n = 0; n <= 31; n++, black_mask >>>= 1) {
  8692. if ((black_mask & 1) && (s.dyn_ltree[n*2]/*.Freq*/ !== 0)) {
  8693. return Z_BINARY;
  8694. }
  8695. }
  8696. /* Check for textual ("white-listed") bytes. */
  8697. if (s.dyn_ltree[9 * 2]/*.Freq*/ !== 0 || s.dyn_ltree[10 * 2]/*.Freq*/ !== 0 ||
  8698. s.dyn_ltree[13 * 2]/*.Freq*/ !== 0) {
  8699. return Z_TEXT;
  8700. }
  8701. for (n = 32; n < LITERALS; n++) {
  8702. if (s.dyn_ltree[n * 2]/*.Freq*/ !== 0) {
  8703. return Z_TEXT;
  8704. }
  8705. }
  8706. /* There are no "black-listed" or "white-listed" bytes:
  8707. * this stream either is empty or has tolerated ("gray-listed") bytes only.
  8708. */
  8709. return Z_BINARY;
  8710. }
  8711. var static_init_done = false;
  8712. /* ===========================================================================
  8713. * Initialize the tree data structures for a new zlib stream.
  8714. */
  8715. function _tr_init(s)
  8716. {
  8717. if (!static_init_done) {
  8718. tr_static_init();
  8719. static_init_done = true;
  8720. }
  8721. s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc);
  8722. s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc);
  8723. s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc);
  8724. s.bi_buf = 0;
  8725. s.bi_valid = 0;
  8726. /* Initialize the first block of the first file: */
  8727. init_block(s);
  8728. }
  8729. /* ===========================================================================
  8730. * Send a stored block
  8731. */
  8732. function _tr_stored_block(s, buf, stored_len, last)
  8733. //DeflateState *s;
  8734. //charf *buf; /* input block */
  8735. //ulg stored_len; /* length of input block */
  8736. //int last; /* one if this is the last block for a file */
  8737. {
  8738. send_bits(s, (STORED_BLOCK<<1)+(last ? 1 : 0), 3); /* send block type */
  8739. copy_block(s, buf, stored_len, true); /* with header */
  8740. }
  8741. /* ===========================================================================
  8742. * Send one empty static block to give enough lookahead for inflate.
  8743. * This takes 10 bits, of which 7 may remain in the bit buffer.
  8744. */
  8745. function _tr_align(s) {
  8746. send_bits(s, STATIC_TREES<<1, 3);
  8747. send_code(s, END_BLOCK, static_ltree);
  8748. bi_flush(s);
  8749. }
  8750. /* ===========================================================================
  8751. * Determine the best encoding for the current block: dynamic trees, static
  8752. * trees or store, and output the encoded block to the zip file.
  8753. */
  8754. function _tr_flush_block(s, buf, stored_len, last)
  8755. //DeflateState *s;
  8756. //charf *buf; /* input block, or NULL if too old */
  8757. //ulg stored_len; /* length of input block */
  8758. //int last; /* one if this is the last block for a file */
  8759. {
  8760. var opt_lenb, static_lenb; /* opt_len and static_len in bytes */
  8761. var max_blindex = 0; /* index of last bit length code of non zero freq */
  8762. /* Build the Huffman trees unless a stored block is forced */
  8763. if (s.level > 0) {
  8764. /* Check if the file is binary or text */
  8765. if (s.strm.data_type === Z_UNKNOWN) {
  8766. s.strm.data_type = detect_data_type(s);
  8767. }
  8768. /* Construct the literal and distance trees */
  8769. build_tree(s, s.l_desc);
  8770. // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
  8771. // s->static_len));
  8772. build_tree(s, s.d_desc);
  8773. // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
  8774. // s->static_len));
  8775. /* At this point, opt_len and static_len are the total bit lengths of
  8776. * the compressed block data, excluding the tree representations.
  8777. */
  8778. /* Build the bit length tree for the above two trees, and get the index
  8779. * in bl_order of the last bit length code to send.
  8780. */
  8781. max_blindex = build_bl_tree(s);
  8782. /* Determine the best encoding. Compute the block lengths in bytes. */
  8783. opt_lenb = (s.opt_len+3+7) >>> 3;
  8784. static_lenb = (s.static_len+3+7) >>> 3;
  8785. // Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
  8786. // opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
  8787. // s->last_lit));
  8788. if (static_lenb <= opt_lenb) { opt_lenb = static_lenb; }
  8789. } else {
  8790. // Assert(buf != (char*)0, "lost buf");
  8791. opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
  8792. }
  8793. if ((stored_len+4 <= opt_lenb) && (buf !== -1)) {
  8794. /* 4: two words for the lengths */
  8795. /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  8796. * Otherwise we can't have processed more than WSIZE input bytes since
  8797. * the last block flush, because compression would have been
  8798. * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  8799. * transform a block into a stored block.
  8800. */
  8801. _tr_stored_block(s, buf, stored_len, last);
  8802. } else if (s.strategy === Z_FIXED || static_lenb === opt_lenb) {
  8803. send_bits(s, (STATIC_TREES<<1) + (last ? 1 : 0), 3);
  8804. compress_block(s, static_ltree, static_dtree);
  8805. } else {
  8806. send_bits(s, (DYN_TREES<<1) + (last ? 1 : 0), 3);
  8807. send_all_trees(s, s.l_desc.max_code+1, s.d_desc.max_code+1, max_blindex+1);
  8808. compress_block(s, s.dyn_ltree, s.dyn_dtree);
  8809. }
  8810. // Assert (s->compressed_len == s->bits_sent, "bad compressed size");
  8811. /* The above check is made mod 2^32, for files larger than 512 MB
  8812. * and uLong implemented on 32 bits.
  8813. */
  8814. init_block(s);
  8815. if (last) {
  8816. bi_windup(s);
  8817. }
  8818. // Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
  8819. // s->compressed_len-7*last));
  8820. }
  8821. /* ===========================================================================
  8822. * Save the match info and tally the frequency counts. Return true if
  8823. * the current block must be flushed.
  8824. */
  8825. function _tr_tally(s, dist, lc)
  8826. // deflate_state *s;
  8827. // unsigned dist; /* distance of matched string */
  8828. // unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
  8829. {
  8830. //var out_length, in_length, dcode;
  8831. s.pending_buf[s.d_buf + s.last_lit * 2] = (dist >>> 8) & 0xff;
  8832. s.pending_buf[s.d_buf + s.last_lit * 2 + 1] = dist & 0xff;
  8833. s.pending_buf[s.l_buf + s.last_lit] = lc & 0xff;
  8834. s.last_lit++;
  8835. if (dist === 0) {
  8836. /* lc is the unmatched char */
  8837. s.dyn_ltree[lc*2]/*.Freq*/++;
  8838. } else {
  8839. s.matches++;
  8840. /* Here, lc is the match length - MIN_MATCH */
  8841. dist--; /* dist = match distance - 1 */
  8842. //Assert((ush)dist < (ush)MAX_DIST(s) &&
  8843. // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
  8844. // (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
  8845. s.dyn_ltree[(_length_code[lc]+LITERALS+1) * 2]/*.Freq*/++;
  8846. s.dyn_dtree[d_code(dist) * 2]/*.Freq*/++;
  8847. }
  8848. // (!) This block is disabled in zlib defailts,
  8849. // don't enable it for binary compatibility
  8850. //#ifdef TRUNCATE_BLOCK
  8851. // /* Try to guess if it is profitable to stop the current block here */
  8852. // if ((s.last_lit & 0x1fff) === 0 && s.level > 2) {
  8853. // /* Compute an upper bound for the compressed length */
  8854. // out_length = s.last_lit*8;
  8855. // in_length = s.strstart - s.block_start;
  8856. //
  8857. // for (dcode = 0; dcode < D_CODES; dcode++) {
  8858. // out_length += s.dyn_dtree[dcode*2]/*.Freq*/ * (5 + extra_dbits[dcode]);
  8859. // }
  8860. // out_length >>>= 3;
  8861. // //Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
  8862. // // s->last_lit, in_length, out_length,
  8863. // // 100L - out_length*100L/in_length));
  8864. // if (s.matches < (s.last_lit>>1)/*int /2*/ && out_length < (in_length>>1)/*int /2*/) {
  8865. // return true;
  8866. // }
  8867. // }
  8868. //#endif
  8869. return (s.last_lit === s.lit_bufsize-1);
  8870. /* We avoid equality with lit_bufsize because of wraparound at 64K
  8871. * on 16 bit machines and because stored blocks are restricted to
  8872. * 64K-1 bytes.
  8873. */
  8874. }
  8875. exports._tr_init = _tr_init;
  8876. exports._tr_stored_block = _tr_stored_block;
  8877. exports._tr_flush_block = _tr_flush_block;
  8878. exports._tr_tally = _tr_tally;
  8879. exports._tr_align = _tr_align;
  8880. },{"../utils/common":27}],39:[function(_dereq_,module,exports){
  8881. 'use strict';
  8882. function ZStream() {
  8883. /* next input byte */
  8884. this.input = null; // JS specific, because we have no pointers
  8885. this.next_in = 0;
  8886. /* number of bytes available at input */
  8887. this.avail_in = 0;
  8888. /* total number of input bytes read so far */
  8889. this.total_in = 0;
  8890. /* next output byte should be put there */
  8891. this.output = null; // JS specific, because we have no pointers
  8892. this.next_out = 0;
  8893. /* remaining free space at output */
  8894. this.avail_out = 0;
  8895. /* total number of bytes output so far */
  8896. this.total_out = 0;
  8897. /* last error message, NULL if no error */
  8898. this.msg = ''/*Z_NULL*/;
  8899. /* not visible by applications */
  8900. this.state = null;
  8901. /* best guess about the data type: binary or text */
  8902. this.data_type = 2/*Z_UNKNOWN*/;
  8903. /* adler32 value of the uncompressed data */
  8904. this.adler = 0;
  8905. }
  8906. module.exports = ZStream;
  8907. },{}]},{},[9])
  8908. (9)
  8909. }));/*---------split--------*//*! xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
  8910. /* vim: set ts=2: */
  8911. /*exported XLSX */
  8912. /*global global, exports, module, require:false, process:false, Buffer:false, ArrayBuffer:false */
  8913. var XLSX = {};
  8914. function make_xlsx_lib(XLSX){
  8915. XLSX.version = '0.14.3';
  8916. var current_codepage = 1200, current_ansi = 1252;
  8917. /*global cptable:true, window */
  8918. if(typeof module !== "undefined" && typeof require !== 'undefined') {
  8919. if(typeof cptable === 'undefined') {
  8920. if(typeof global !== 'undefined') global.cptable = undefined;
  8921. else if(typeof window !== 'undefined') window.cptable = undefined;
  8922. }
  8923. }
  8924. var VALID_ANSI = [ 874, 932, 936, 949, 950 ];
  8925. for(var i = 0; i <= 8; ++i) VALID_ANSI.push(1250 + i);
  8926. /* ECMA-376 Part I 18.4.1 charset to codepage mapping */
  8927. var CS2CP = ({
  8928. 0: 1252, /* ANSI */
  8929. 1: 65001, /* DEFAULT */
  8930. 2: 65001, /* SYMBOL */
  8931. 77: 10000, /* MAC */
  8932. 128: 932, /* SHIFTJIS */
  8933. 129: 949, /* HANGUL */
  8934. 130: 1361, /* JOHAB */
  8935. 134: 936, /* GB2312 */
  8936. 136: 950, /* CHINESEBIG5 */
  8937. 161: 1253, /* GREEK */
  8938. 162: 1254, /* TURKISH */
  8939. 163: 1258, /* VIETNAMESE */
  8940. 177: 1255, /* HEBREW */
  8941. 178: 1256, /* ARABIC */
  8942. 186: 1257, /* BALTIC */
  8943. 204: 1251, /* RUSSIAN */
  8944. 222: 874, /* THAI */
  8945. 238: 1250, /* EASTEUROPE */
  8946. 255: 1252, /* OEM */
  8947. 69: 6969 /* MISC */
  8948. });
  8949. var set_ansi = function(cp) { if(VALID_ANSI.indexOf(cp) == -1) return; current_ansi = CS2CP[0] = cp; };
  8950. function reset_ansi() { set_ansi(1252); }
  8951. var set_cp = function(cp) { current_codepage = cp; set_ansi(cp); };
  8952. function reset_cp() { set_cp(1200); reset_ansi(); }
  8953. function char_codes(data) { var o = []; for(var i = 0, len = data.length; i < len; ++i) o[i] = data.charCodeAt(i); return o; }
  8954. function utf16leread(data) {
  8955. var o = [];
  8956. for(var i = 0; i < (data.length>>1); ++i) o[i] = String.fromCharCode(data.charCodeAt(2*i) + (data.charCodeAt(2*i+1)<<8));
  8957. return o.join("");
  8958. }
  8959. function utf16beread(data) {
  8960. var o = [];
  8961. for(var i = 0; i < (data.length>>1); ++i) o[i] = String.fromCharCode(data.charCodeAt(2*i+1) + (data.charCodeAt(2*i)<<8));
  8962. return o.join("");
  8963. }
  8964. var debom = function(data) {
  8965. var c1 = data.charCodeAt(0), c2 = data.charCodeAt(1);
  8966. if(c1 == 0xFF && c2 == 0xFE) return utf16leread(data.slice(2));
  8967. if(c1 == 0xFE && c2 == 0xFF) return utf16beread(data.slice(2));
  8968. if(c1 == 0xFEFF) return data.slice(1);
  8969. return data;
  8970. };
  8971. var _getchar = function _gc1(x) { return String.fromCharCode(x); };
  8972. if(typeof cptable !== 'undefined') {
  8973. set_cp = function(cp) { current_codepage = cp; };
  8974. debom = function(data) {
  8975. if(data.charCodeAt(0) === 0xFF && data.charCodeAt(1) === 0xFE) { return cptable.utils.decode(1200, char_codes(data.slice(2))); }
  8976. return data;
  8977. };
  8978. _getchar = function _gc2(x) {
  8979. if(current_codepage === 1200) return String.fromCharCode(x);
  8980. return cptable.utils.decode(current_codepage, [x&255,x>>8])[0];
  8981. };
  8982. }
  8983. var DENSE = null;
  8984. var DIF_XL = true;
  8985. var Base64 = (function make_b64(){
  8986. var map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  8987. return {
  8988. encode: function(input) {
  8989. var o = "";
  8990. var c1=0, c2=0, c3=0, e1=0, e2=0, e3=0, e4=0;
  8991. for(var i = 0; i < input.length; ) {
  8992. c1 = input.charCodeAt(i++);
  8993. e1 = (c1 >> 2);
  8994. c2 = input.charCodeAt(i++);
  8995. e2 = ((c1 & 3) << 4) | (c2 >> 4);
  8996. c3 = input.charCodeAt(i++);
  8997. e3 = ((c2 & 15) << 2) | (c3 >> 6);
  8998. e4 = (c3 & 63);
  8999. if (isNaN(c2)) { e3 = e4 = 64; }
  9000. else if (isNaN(c3)) { e4 = 64; }
  9001. o += map.charAt(e1) + map.charAt(e2) + map.charAt(e3) + map.charAt(e4);
  9002. }
  9003. return o;
  9004. },
  9005. decode: function b64_decode(input) {
  9006. var o = "";
  9007. var c1=0, c2=0, c3=0, e1=0, e2=0, e3=0, e4=0;
  9008. input = input.replace(/[^\w\+\/\=]/g, "");
  9009. for(var i = 0; i < input.length;) {
  9010. e1 = map.indexOf(input.charAt(i++));
  9011. e2 = map.indexOf(input.charAt(i++));
  9012. c1 = (e1 << 2) | (e2 >> 4);
  9013. o += String.fromCharCode(c1);
  9014. e3 = map.indexOf(input.charAt(i++));
  9015. c2 = ((e2 & 15) << 4) | (e3 >> 2);
  9016. if (e3 !== 64) { o += String.fromCharCode(c2); }
  9017. e4 = map.indexOf(input.charAt(i++));
  9018. c3 = ((e3 & 3) << 6) | e4;
  9019. if (e4 !== 64) { o += String.fromCharCode(c3); }
  9020. }
  9021. return o;
  9022. }
  9023. };
  9024. })();
  9025. var has_buf = (typeof Buffer !== 'undefined' && typeof process !== 'undefined' && typeof process.versions !== 'undefined' && !!process.versions.node);
  9026. var Buffer_from = function(){};
  9027. if(typeof Buffer !== 'undefined') {
  9028. var nbfs = !Buffer.from;
  9029. if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; }
  9030. Buffer_from = nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer);
  9031. // $FlowIgnore
  9032. if(!Buffer.alloc) Buffer.alloc = function(n) { return new Buffer(n); };
  9033. // $FlowIgnore
  9034. if(!Buffer.allocUnsafe) Buffer.allocUnsafe = function(n) { return new Buffer(n); };
  9035. }
  9036. function new_raw_buf(len) {
  9037. /* jshint -W056 */
  9038. return has_buf ? Buffer.alloc(len) : new Array(len);
  9039. /* jshint +W056 */
  9040. }
  9041. function new_unsafe_buf(len) {
  9042. /* jshint -W056 */
  9043. return has_buf ? Buffer.allocUnsafe(len) : new Array(len);
  9044. /* jshint +W056 */
  9045. }
  9046. var s2a = function s2a(s) {
  9047. // $FlowIgnore
  9048. if(has_buf) return Buffer_from(s, "binary");
  9049. return s.split("").map(function(x){ return x.charCodeAt(0) & 0xff; });
  9050. };
  9051. function s2ab(s) {
  9052. if(typeof ArrayBuffer === 'undefined') return s2a(s);
  9053. var buf = new ArrayBuffer(s.length), view = new Uint8Array(buf);
  9054. for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
  9055. return buf;
  9056. }
  9057. function a2s(data) {
  9058. if(Array.isArray(data)) return data.map(_chr).join("");
  9059. var o = []; for(var i = 0; i < data.length; ++i) o[i] = _chr(data[i]); return o.join("");
  9060. }
  9061. function a2u(data) {
  9062. if(typeof Uint8Array === 'undefined') throw new Error("Unsupported");
  9063. return new Uint8Array(data);
  9064. }
  9065. function ab2a(data) {
  9066. if(typeof ArrayBuffer == 'undefined') throw new Error("Unsupported");
  9067. if(data instanceof ArrayBuffer) return ab2a(new Uint8Array(data));
  9068. var o = new Array(data.length);
  9069. for(var i = 0; i < data.length; ++i) o[i] = data[i];
  9070. return o;
  9071. }
  9072. var bconcat = function(bufs) { return [].concat.apply([], bufs); };
  9073. var chr0 = /\u0000/g, chr1 = /[\u0001-\u0006]/g;
  9074. /* ssf.js (C) 2013-present SheetJS -- http://sheetjs.com */
  9075. /*jshint -W041 */
  9076. var SSF = ({});
  9077. var make_ssf = function make_ssf(SSF){
  9078. SSF.version = '0.10.2';
  9079. function _strrev(x) { var o = "", i = x.length-1; while(i>=0) o += x.charAt(i--); return o; }
  9080. function fill(c,l) { var o = ""; while(o.length < l) o+=c; return o; }
  9081. function pad0(v,d){var t=""+v; return t.length>=d?t:fill('0',d-t.length)+t;}
  9082. function pad_(v,d){var t=""+v;return t.length>=d?t:fill(' ',d-t.length)+t;}
  9083. function rpad_(v,d){var t=""+v; return t.length>=d?t:t+fill(' ',d-t.length);}
  9084. function pad0r1(v,d){var t=""+Math.round(v); return t.length>=d?t:fill('0',d-t.length)+t;}
  9085. function pad0r2(v,d){var t=""+v; return t.length>=d?t:fill('0',d-t.length)+t;}
  9086. var p2_32 = Math.pow(2,32);
  9087. function pad0r(v,d){if(v>p2_32||v<-p2_32) return pad0r1(v,d); var i = Math.round(v); return pad0r2(i,d); }
  9088. function isgeneral(s, i) { i = i || 0; return s.length >= 7 + i && (s.charCodeAt(i)|32) === 103 && (s.charCodeAt(i+1)|32) === 101 && (s.charCodeAt(i+2)|32) === 110 && (s.charCodeAt(i+3)|32) === 101 && (s.charCodeAt(i+4)|32) === 114 && (s.charCodeAt(i+5)|32) === 97 && (s.charCodeAt(i+6)|32) === 108; }
  9089. var days = [
  9090. ['Sun', 'Sunday'],
  9091. ['Mon', 'Monday'],
  9092. ['Tue', 'Tuesday'],
  9093. ['Wed', 'Wednesday'],
  9094. ['Thu', 'Thursday'],
  9095. ['Fri', 'Friday'],
  9096. ['Sat', 'Saturday']
  9097. ];
  9098. var months = [
  9099. ['J', 'Jan', 'January'],
  9100. ['F', 'Feb', 'February'],
  9101. ['M', 'Mar', 'March'],
  9102. ['A', 'Apr', 'April'],
  9103. ['M', 'May', 'May'],
  9104. ['J', 'Jun', 'June'],
  9105. ['J', 'Jul', 'July'],
  9106. ['A', 'Aug', 'August'],
  9107. ['S', 'Sep', 'September'],
  9108. ['O', 'Oct', 'October'],
  9109. ['N', 'Nov', 'November'],
  9110. ['D', 'Dec', 'December']
  9111. ];
  9112. function init_table(t) {
  9113. t[0]= 'General';
  9114. t[1]= '0';
  9115. t[2]= '0.00';
  9116. t[3]= '#,##0';
  9117. t[4]= '#,##0.00';
  9118. t[9]= '0%';
  9119. t[10]= '0.00%';
  9120. t[11]= '0.00E+00';
  9121. t[12]= '# ?/?';
  9122. t[13]= '# ??/??';
  9123. t[14]= 'm/d/yy';
  9124. t[15]= 'd-mmm-yy';
  9125. t[16]= 'd-mmm';
  9126. t[17]= 'mmm-yy';
  9127. t[18]= 'h:mm AM/PM';
  9128. t[19]= 'h:mm:ss AM/PM';
  9129. t[20]= 'h:mm';
  9130. t[21]= 'h:mm:ss';
  9131. t[22]= 'm/d/yy h:mm';
  9132. t[37]= '#,##0 ;(#,##0)';
  9133. t[38]= '#,##0 ;[Red](#,##0)';
  9134. t[39]= '#,##0.00;(#,##0.00)';
  9135. t[40]= '#,##0.00;[Red](#,##0.00)';
  9136. t[45]= 'mm:ss';
  9137. t[46]= '[h]:mm:ss';
  9138. t[47]= 'mmss.0';
  9139. t[48]= '##0.0E+0';
  9140. t[49]= '@';
  9141. t[56]= '"上午/下午 "hh"時"mm"分"ss"秒 "';
  9142. t[65535]= 'General';
  9143. }
  9144. var table_fmt = {};
  9145. init_table(table_fmt);
  9146. function frac(x, D, mixed) {
  9147. var sgn = x < 0 ? -1 : 1;
  9148. var B = x * sgn;
  9149. var P_2 = 0, P_1 = 1, P = 0;
  9150. var Q_2 = 1, Q_1 = 0, Q = 0;
  9151. var A = Math.floor(B);
  9152. while(Q_1 < D) {
  9153. A = Math.floor(B);
  9154. P = A * P_1 + P_2;
  9155. Q = A * Q_1 + Q_2;
  9156. if((B - A) < 0.00000005) break;
  9157. B = 1 / (B - A);
  9158. P_2 = P_1; P_1 = P;
  9159. Q_2 = Q_1; Q_1 = Q;
  9160. }
  9161. if(Q > D) { if(Q_1 > D) { Q = Q_2; P = P_2; } else { Q = Q_1; P = P_1; } }
  9162. if(!mixed) return [0, sgn * P, Q];
  9163. var q = Math.floor(sgn * P/Q);
  9164. return [q, sgn*P - q*Q, Q];
  9165. }
  9166. function parse_date_code(v,opts,b2) {
  9167. if(v > 2958465 || v < 0) return null;
  9168. var date = (v|0), time = Math.floor(86400 * (v - date)), dow=0;
  9169. var dout=[];
  9170. var out={D:date, T:time, u:86400*(v-date)-time,y:0,m:0,d:0,H:0,M:0,S:0,q:0};
  9171. if(Math.abs(out.u) < 1e-6) out.u = 0;
  9172. if(opts && opts.date1904) date += 1462;
  9173. if(out.u > 0.9999) {
  9174. out.u = 0;
  9175. if(++time == 86400) { out.T = time = 0; ++date; ++out.D; }
  9176. }
  9177. if(date === 60) {dout = b2 ? [1317,10,29] : [1900,2,29]; dow=3;}
  9178. else if(date === 0) {dout = b2 ? [1317,8,29] : [1900,1,0]; dow=6;}
  9179. else {
  9180. if(date > 60) --date;
  9181. /* 1 = Jan 1 1900 in Gregorian */
  9182. var d = new Date(1900, 0, 1);
  9183. d.setDate(d.getDate() + date - 1);
  9184. dout = [d.getFullYear(), d.getMonth()+1,d.getDate()];
  9185. dow = d.getDay();
  9186. if(date < 60) dow = (dow + 6) % 7;
  9187. if(b2) dow = fix_hijri(d, dout);
  9188. }
  9189. out.y = dout[0]; out.m = dout[1]; out.d = dout[2];
  9190. out.S = time % 60; time = Math.floor(time / 60);
  9191. out.M = time % 60; time = Math.floor(time / 60);
  9192. out.H = time;
  9193. out.q = dow;
  9194. return out;
  9195. }
  9196. SSF.parse_date_code = parse_date_code;
  9197. var basedate = new Date(1899, 11, 31, 0, 0, 0);
  9198. var dnthresh = basedate.getTime();
  9199. var base1904 = new Date(1900, 2, 1, 0, 0, 0);
  9200. function datenum_local(v, date1904) {
  9201. var epoch = v.getTime();
  9202. if(date1904) epoch -= 1461*24*60*60*1000;
  9203. else if(v >= base1904) epoch += 24*60*60*1000;
  9204. return (epoch - (dnthresh + (v.getTimezoneOffset() - basedate.getTimezoneOffset()) * 60000)) / (24 * 60 * 60 * 1000);
  9205. }
  9206. function general_fmt_int(v) { return v.toString(10); }
  9207. SSF._general_int = general_fmt_int;
  9208. var general_fmt_num = (function make_general_fmt_num() {
  9209. var gnr1 = /\.(\d*[1-9])0+$/, gnr2 = /\.0*$/, gnr4 = /\.(\d*[1-9])0+/, gnr5 = /\.0*[Ee]/, gnr6 = /(E[+-])(\d)$/;
  9210. function gfn2(v) {
  9211. var w = (v<0?12:11);
  9212. var o = gfn5(v.toFixed(12)); if(o.length <= w) return o;
  9213. o = v.toPrecision(10); if(o.length <= w) return o;
  9214. return v.toExponential(5);
  9215. }
  9216. function gfn3(v) {
  9217. var o = v.toFixed(11).replace(gnr1,".$1");
  9218. if(o.length > (v<0?12:11)) o = v.toPrecision(6);
  9219. return o;
  9220. }
  9221. function gfn4(o) {
  9222. for(var i = 0; i != o.length; ++i) if((o.charCodeAt(i) | 0x20) === 101) return o.replace(gnr4,".$1").replace(gnr5,"E").replace("e","E").replace(gnr6,"$10$2");
  9223. return o;
  9224. }
  9225. function gfn5(o) {
  9226. return o.indexOf(".") > -1 ? o.replace(gnr2,"").replace(gnr1,".$1") : o;
  9227. }
  9228. return function general_fmt_num(v) {
  9229. var V = Math.floor(Math.log(Math.abs(v))*Math.LOG10E), o;
  9230. if(V >= -4 && V <= -1) o = v.toPrecision(10+V);
  9231. else if(Math.abs(V) <= 9) o = gfn2(v);
  9232. else if(V === 10) o = v.toFixed(10).substr(0,12);
  9233. else o = gfn3(v);
  9234. return gfn5(gfn4(o));
  9235. };})();
  9236. SSF._general_num = general_fmt_num;
  9237. function general_fmt(v, opts) {
  9238. switch(typeof v) {
  9239. case 'string': return v;
  9240. case 'boolean': return v ? "TRUE" : "FALSE";
  9241. case 'number': return (v|0) === v ? general_fmt_int(v) : general_fmt_num(v);
  9242. case 'undefined': return "";
  9243. case 'object':
  9244. if(v == null) return "";
  9245. if(v instanceof Date) return format(14, datenum_local(v, opts && opts.date1904), opts);
  9246. }
  9247. throw new Error("unsupported value in General format: " + v);
  9248. }
  9249. SSF._general = general_fmt;
  9250. function fix_hijri() { return 0; }
  9251. /*jshint -W086 */
  9252. function write_date(type, fmt, val, ss0) {
  9253. var o="", ss=0, tt=0, y = val.y, out, outl = 0;
  9254. switch(type) {
  9255. case 98: /* 'b' buddhist year */
  9256. y = val.y + 543;
  9257. /* falls through */
  9258. case 121: /* 'y' year */
  9259. switch(fmt.length) {
  9260. case 1: case 2: out = y % 100; outl = 2; break;
  9261. default: out = y % 10000; outl = 4; break;
  9262. } break;
  9263. case 109: /* 'm' month */
  9264. switch(fmt.length) {
  9265. case 1: case 2: out = val.m; outl = fmt.length; break;
  9266. case 3: return months[val.m-1][1];
  9267. case 5: return months[val.m-1][0];
  9268. default: return months[val.m-1][2];
  9269. } break;
  9270. case 100: /* 'd' day */
  9271. switch(fmt.length) {
  9272. case 1: case 2: out = val.d; outl = fmt.length; break;
  9273. case 3: return days[val.q][0];
  9274. default: return days[val.q][1];
  9275. } break;
  9276. case 104: /* 'h' 12-hour */
  9277. switch(fmt.length) {
  9278. case 1: case 2: out = 1+(val.H+11)%12; outl = fmt.length; break;
  9279. default: throw 'bad hour format: ' + fmt;
  9280. } break;
  9281. case 72: /* 'H' 24-hour */
  9282. switch(fmt.length) {
  9283. case 1: case 2: out = val.H; outl = fmt.length; break;
  9284. default: throw 'bad hour format: ' + fmt;
  9285. } break;
  9286. case 77: /* 'M' minutes */
  9287. switch(fmt.length) {
  9288. case 1: case 2: out = val.M; outl = fmt.length; break;
  9289. default: throw 'bad minute format: ' + fmt;
  9290. } break;
  9291. case 115: /* 's' seconds */
  9292. if(fmt != 's' && fmt != 'ss' && fmt != '.0' && fmt != '.00' && fmt != '.000') throw 'bad second format: ' + fmt;
  9293. if(val.u === 0 && (fmt == "s" || fmt == "ss")) return pad0(val.S, fmt.length);
  9294. if(ss0 >= 2) tt = ss0 === 3 ? 1000 : 100;
  9295. else tt = ss0 === 1 ? 10 : 1;
  9296. ss = Math.round((tt)*(val.S + val.u));
  9297. if(ss >= 60*tt) ss = 0;
  9298. if(fmt === 's') return ss === 0 ? "0" : ""+ss/tt;
  9299. o = pad0(ss,2 + ss0);
  9300. if(fmt === 'ss') return o.substr(0,2);
  9301. return "." + o.substr(2,fmt.length-1);
  9302. case 90: /* 'Z' absolute time */
  9303. switch(fmt) {
  9304. case '[h]': case '[hh]': out = val.D*24+val.H; break;
  9305. case '[m]': case '[mm]': out = (val.D*24+val.H)*60+val.M; break;
  9306. case '[s]': case '[ss]': out = ((val.D*24+val.H)*60+val.M)*60+Math.round(val.S+val.u); break;
  9307. default: throw 'bad abstime format: ' + fmt;
  9308. } outl = fmt.length === 3 ? 1 : 2; break;
  9309. case 101: /* 'e' era */
  9310. out = y; outl = 1;
  9311. }
  9312. if(outl > 0) return pad0(out, outl); else return "";
  9313. }
  9314. /*jshint +W086 */
  9315. function commaify(s) {
  9316. var w = 3;
  9317. if(s.length <= w) return s;
  9318. var j = (s.length % w), o = s.substr(0,j);
  9319. for(; j!=s.length; j+=w) o+=(o.length > 0 ? "," : "") + s.substr(j,w);
  9320. return o;
  9321. }
  9322. var write_num = (function make_write_num(){
  9323. var pct1 = /%/g;
  9324. function write_num_pct(type, fmt, val){
  9325. var sfmt = fmt.replace(pct1,""), mul = fmt.length - sfmt.length;
  9326. return write_num(type, sfmt, val * Math.pow(10,2*mul)) + fill("%",mul);
  9327. }
  9328. function write_num_cm(type, fmt, val){
  9329. var idx = fmt.length - 1;
  9330. while(fmt.charCodeAt(idx-1) === 44) --idx;
  9331. return write_num(type, fmt.substr(0,idx), val / Math.pow(10,3*(fmt.length-idx)));
  9332. }
  9333. function write_num_exp(fmt, val){
  9334. var o;
  9335. var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1;
  9336. if(fmt.match(/^#+0.0E\+0$/)) {
  9337. if(val == 0) return "0.0E+0";
  9338. else if(val < 0) return "-" + write_num_exp(fmt, -val);
  9339. var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E');
  9340. var ee = Math.floor(Math.log(val)*Math.LOG10E)%period;
  9341. if(ee < 0) ee += period;
  9342. o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period);
  9343. if(o.indexOf("e") === -1) {
  9344. var fakee = Math.floor(Math.log(val)*Math.LOG10E);
  9345. if(o.indexOf(".") === -1) o = o.charAt(0) + "." + o.substr(1) + "E+" + (fakee - o.length+ee);
  9346. else o += "E+" + (fakee - ee);
  9347. while(o.substr(0,2) === "0.") {
  9348. o = o.charAt(0) + o.substr(2,period) + "." + o.substr(2+period);
  9349. o = o.replace(/^0+([1-9])/,"$1").replace(/^0+\./,"0.");
  9350. }
  9351. o = o.replace(/\+-/,"-");
  9352. }
  9353. o = o.replace(/^([+-]?)(\d*)\.(\d*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; });
  9354. } else o = val.toExponential(idx);
  9355. if(fmt.match(/E\+00$/) && o.match(/e[+-]\d$/)) o = o.substr(0,o.length-1) + "0" + o.charAt(o.length-1);
  9356. if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e");
  9357. return o.replace("e","E");
  9358. }
  9359. var frac1 = /# (\?+)( ?)\/( ?)(\d+)/;
  9360. function write_num_f1(r, aval, sign) {
  9361. var den = parseInt(r[4],10), rr = Math.round(aval * den), base = Math.floor(rr/den);
  9362. var myn = (rr - base*den), myd = den;
  9363. return sign + (base === 0 ? "" : ""+base) + " " + (myn === 0 ? fill(" ", r[1].length + 1 + r[4].length) : pad_(myn,r[1].length) + r[2] + "/" + r[3] + pad0(myd,r[4].length));
  9364. }
  9365. function write_num_f2(r, aval, sign) {
  9366. return sign + (aval === 0 ? "" : ""+aval) + fill(" ", r[1].length + 2 + r[4].length);
  9367. }
  9368. var dec1 = /^#*0*\.([0#]+)/;
  9369. var closeparen = /\).*[0#]/;
  9370. var phone = /\(###\) ###\\?-####/;
  9371. function hashq(str) {
  9372. var o = "", cc;
  9373. for(var i = 0; i != str.length; ++i) switch((cc=str.charCodeAt(i))) {
  9374. case 35: break;
  9375. case 63: o+= " "; break;
  9376. case 48: o+= "0"; break;
  9377. default: o+= String.fromCharCode(cc);
  9378. }
  9379. return o;
  9380. }
  9381. function rnd(val, d) { var dd = Math.pow(10,d); return ""+(Math.round(val * dd)/dd); }
  9382. function dec(val, d) {
  9383. if (d < ('' + Math.round((val-Math.floor(val))*Math.pow(10,d))).length) {
  9384. return 0;
  9385. }
  9386. return Math.round((val-Math.floor(val))*Math.pow(10,d));
  9387. }
  9388. function carry(val, d) {
  9389. if (d < ('' + Math.round((val-Math.floor(val))*Math.pow(10,d))).length) {
  9390. return 1;
  9391. }
  9392. return 0;
  9393. }
  9394. function flr(val) { if(val < 2147483647 && val > -2147483648) return ""+(val >= 0 ? (val|0) : (val-1|0)); return ""+Math.floor(val); }
  9395. function write_num_flt(type, fmt, val) {
  9396. if(type.charCodeAt(0) === 40 && !fmt.match(closeparen)) {
  9397. var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,"");
  9398. if(val >= 0) return write_num_flt('n', ffmt, val);
  9399. return '(' + write_num_flt('n', ffmt, -val) + ')';
  9400. }
  9401. if(fmt.charCodeAt(fmt.length - 1) === 44) return write_num_cm(type, fmt, val);
  9402. if(fmt.indexOf('%') !== -1) return write_num_pct(type, fmt, val);
  9403. if(fmt.indexOf('E') !== -1) return write_num_exp(fmt, val);
  9404. if(fmt.charCodeAt(0) === 36) return "$"+write_num_flt(type,fmt.substr(fmt.charAt(1)==' '?2:1),val);
  9405. var o;
  9406. var r, ri, ff, aval = Math.abs(val), sign = val < 0 ? "-" : "";
  9407. if(fmt.match(/^00+$/)) return sign + pad0r(aval,fmt.length);
  9408. if(fmt.match(/^[#?]+$/)) {
  9409. o = pad0r(val,0); if(o === "0") o = "";
  9410. return o.length > fmt.length ? o : hashq(fmt.substr(0,fmt.length-o.length)) + o;
  9411. }
  9412. if((r = fmt.match(frac1))) return write_num_f1(r, aval, sign);
  9413. if(fmt.match(/^#+0+$/)) return sign + pad0r(aval,fmt.length - fmt.indexOf("0"));
  9414. if((r = fmt.match(dec1))) {
  9415. o = rnd(val, r[1].length).replace(/^([^\.]+)$/,"$1."+hashq(r[1])).replace(/\.$/,"."+hashq(r[1])).replace(/\.(\d*)$/,function($$, $1) { return "." + $1 + fill("0", hashq(r[1]).length-$1.length); });
  9416. return fmt.indexOf("0.") !== -1 ? o : o.replace(/^0\./,".");
  9417. }
  9418. fmt = fmt.replace(/^#+([0.])/, "$1");
  9419. if((r = fmt.match(/^(0*)\.(#*)$/))) {
  9420. return sign + rnd(aval, r[2].length).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^(-?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":".");
  9421. }
  9422. if((r = fmt.match(/^#{1,3},##0(\.?)$/))) return sign + commaify(pad0r(aval,0));
  9423. if((r = fmt.match(/^#,##0\.([#0]*0)$/))) {
  9424. return val < 0 ? "-" + write_num_flt(type, fmt, -val) : commaify(""+(Math.floor(val) + carry(val, r[1].length))) + "." + pad0(dec(val, r[1].length),r[1].length);
  9425. }
  9426. if((r = fmt.match(/^#,#*,#0/))) return write_num_flt(type,fmt.replace(/^#,#*,/,""),val);
  9427. if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/))) {
  9428. o = _strrev(write_num_flt(type, fmt.replace(/[\\-]/g,""), val));
  9429. ri = 0;
  9430. return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return ri<o.length?o.charAt(ri++):x==='0'?'0':"";}));
  9431. }
  9432. if(fmt.match(phone)) {
  9433. o = write_num_flt(type, "##########", val);
  9434. return "(" + o.substr(0,3) + ") " + o.substr(3, 3) + "-" + o.substr(6);
  9435. }
  9436. var oa = "";
  9437. if((r = fmt.match(/^([#0?]+)( ?)\/( ?)([#0?]+)/))) {
  9438. ri = Math.min(r[4].length,7);
  9439. ff = frac(aval, Math.pow(10,ri)-1, false);
  9440. o = "" + sign;
  9441. oa = write_num("n", r[1], ff[1]);
  9442. if(oa.charAt(oa.length-1) == " ") oa = oa.substr(0,oa.length-1) + "0";
  9443. o += oa + r[2] + "/" + r[3];
  9444. oa = rpad_(ff[2],ri);
  9445. if(oa.length < r[4].length) oa = hashq(r[4].substr(r[4].length-oa.length)) + oa;
  9446. o += oa;
  9447. return o;
  9448. }
  9449. if((r = fmt.match(/^# ([#0?]+)( ?)\/( ?)([#0?]+)/))) {
  9450. ri = Math.min(Math.max(r[1].length, r[4].length),7);
  9451. ff = frac(aval, Math.pow(10,ri)-1, true);
  9452. return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad_(ff[1],ri) + r[2] + "/" + r[3] + rpad_(ff[2],ri): fill(" ", 2*ri+1 + r[2].length + r[3].length));
  9453. }
  9454. if((r = fmt.match(/^[#0?]+$/))) {
  9455. o = pad0r(val, 0);
  9456. if(fmt.length <= o.length) return o;
  9457. return hashq(fmt.substr(0,fmt.length-o.length)) + o;
  9458. }
  9459. if((r = fmt.match(/^([#0?]+)\.([#0]+)$/))) {
  9460. o = "" + val.toFixed(Math.min(r[2].length,10)).replace(/([^0])0+$/,"$1");
  9461. ri = o.indexOf(".");
  9462. var lres = fmt.indexOf(".") - ri, rres = fmt.length - o.length - lres;
  9463. return hashq(fmt.substr(0,lres) + o + fmt.substr(fmt.length-rres));
  9464. }
  9465. if((r = fmt.match(/^00,000\.([#0]*0)$/))) {
  9466. ri = dec(val, r[1].length);
  9467. return val < 0 ? "-" + write_num_flt(type, fmt, -val) : commaify(flr(val)).replace(/^\d,\d{3}$/,"0$&").replace(/^\d*$/,function($$) { return "00," + ($$.length < 3 ? pad0(0,3-$$.length) : "") + $$; }) + "." + pad0(ri,r[1].length);
  9468. }
  9469. switch(fmt) {
  9470. case "###,##0.00": return write_num_flt(type, "#,##0.00", val);
  9471. case "###,###":
  9472. case "##,###":
  9473. case "#,###": var x = commaify(pad0r(aval,0)); return x !== "0" ? sign + x : "";
  9474. case "###,###.00": return write_num_flt(type, "###,##0.00",val).replace(/^0\./,".");
  9475. case "#,###.00": return write_num_flt(type, "#,##0.00",val).replace(/^0\./,".");
  9476. default:
  9477. }
  9478. throw new Error("unsupported format |" + fmt + "|");
  9479. }
  9480. function write_num_cm2(type, fmt, val){
  9481. var idx = fmt.length - 1;
  9482. while(fmt.charCodeAt(idx-1) === 44) --idx;
  9483. return write_num(type, fmt.substr(0,idx), val / Math.pow(10,3*(fmt.length-idx)));
  9484. }
  9485. function write_num_pct2(type, fmt, val){
  9486. var sfmt = fmt.replace(pct1,""), mul = fmt.length - sfmt.length;
  9487. return write_num(type, sfmt, val * Math.pow(10,2*mul)) + fill("%",mul);
  9488. }
  9489. function write_num_exp2(fmt, val){
  9490. var o;
  9491. var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1;
  9492. if(fmt.match(/^#+0.0E\+0$/)) {
  9493. if(val == 0) return "0.0E+0";
  9494. else if(val < 0) return "-" + write_num_exp2(fmt, -val);
  9495. var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E');
  9496. var ee = Math.floor(Math.log(val)*Math.LOG10E)%period;
  9497. if(ee < 0) ee += period;
  9498. o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period);
  9499. if(!o.match(/[Ee]/)) {
  9500. var fakee = Math.floor(Math.log(val)*Math.LOG10E);
  9501. if(o.indexOf(".") === -1) o = o.charAt(0) + "." + o.substr(1) + "E+" + (fakee - o.length+ee);
  9502. else o += "E+" + (fakee - ee);
  9503. o = o.replace(/\+-/,"-");
  9504. }
  9505. o = o.replace(/^([+-]?)(\d*)\.(\d*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; });
  9506. } else o = val.toExponential(idx);
  9507. if(fmt.match(/E\+00$/) && o.match(/e[+-]\d$/)) o = o.substr(0,o.length-1) + "0" + o.charAt(o.length-1);
  9508. if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e");
  9509. return o.replace("e","E");
  9510. }
  9511. function write_num_int(type, fmt, val) {
  9512. if(type.charCodeAt(0) === 40 && !fmt.match(closeparen)) {
  9513. var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,"");
  9514. if(val >= 0) return write_num_int('n', ffmt, val);
  9515. return '(' + write_num_int('n', ffmt, -val) + ')';
  9516. }
  9517. if(fmt.charCodeAt(fmt.length - 1) === 44) return write_num_cm2(type, fmt, val);
  9518. if(fmt.indexOf('%') !== -1) return write_num_pct2(type, fmt, val);
  9519. if(fmt.indexOf('E') !== -1) return write_num_exp2(fmt, val);
  9520. if(fmt.charCodeAt(0) === 36) return "$"+write_num_int(type,fmt.substr(fmt.charAt(1)==' '?2:1),val);
  9521. var o;
  9522. var r, ri, ff, aval = Math.abs(val), sign = val < 0 ? "-" : "";
  9523. if(fmt.match(/^00+$/)) return sign + pad0(aval,fmt.length);
  9524. if(fmt.match(/^[#?]+$/)) {
  9525. o = (""+val); if(val === 0) o = "";
  9526. return o.length > fmt.length ? o : hashq(fmt.substr(0,fmt.length-o.length)) + o;
  9527. }
  9528. if((r = fmt.match(frac1))) return write_num_f2(r, aval, sign);
  9529. if(fmt.match(/^#+0+$/)) return sign + pad0(aval,fmt.length - fmt.indexOf("0"));
  9530. if((r = fmt.match(dec1))) {
  9531. o = (""+val).replace(/^([^\.]+)$/,"$1."+hashq(r[1])).replace(/\.$/,"."+hashq(r[1]));
  9532. o = o.replace(/\.(\d*)$/,function($$, $1) {
  9533. return "." + $1 + fill("0", hashq(r[1]).length-$1.length); });
  9534. return fmt.indexOf("0.") !== -1 ? o : o.replace(/^0\./,".");
  9535. }
  9536. fmt = fmt.replace(/^#+([0.])/, "$1");
  9537. if((r = fmt.match(/^(0*)\.(#*)$/))) {
  9538. return sign + (""+aval).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^(-?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":".");
  9539. }
  9540. if((r = fmt.match(/^#{1,3},##0(\.?)$/))) return sign + commaify((""+aval));
  9541. if((r = fmt.match(/^#,##0\.([#0]*0)$/))) {
  9542. return val < 0 ? "-" + write_num_int(type, fmt, -val) : commaify((""+val)) + "." + fill('0',r[1].length);
  9543. }
  9544. if((r = fmt.match(/^#,#*,#0/))) return write_num_int(type,fmt.replace(/^#,#*,/,""),val);
  9545. if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/))) {
  9546. o = _strrev(write_num_int(type, fmt.replace(/[\\-]/g,""), val));
  9547. ri = 0;
  9548. return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return ri<o.length?o.charAt(ri++):x==='0'?'0':"";}));
  9549. }
  9550. if(fmt.match(phone)) {
  9551. o = write_num_int(type, "##########", val);
  9552. return "(" + o.substr(0,3) + ") " + o.substr(3, 3) + "-" + o.substr(6);
  9553. }
  9554. var oa = "";
  9555. if((r = fmt.match(/^([#0?]+)( ?)\/( ?)([#0?]+)/))) {
  9556. ri = Math.min(r[4].length,7);
  9557. ff = frac(aval, Math.pow(10,ri)-1, false);
  9558. o = "" + sign;
  9559. oa = write_num("n", r[1], ff[1]);
  9560. if(oa.charAt(oa.length-1) == " ") oa = oa.substr(0,oa.length-1) + "0";
  9561. o += oa + r[2] + "/" + r[3];
  9562. oa = rpad_(ff[2],ri);
  9563. if(oa.length < r[4].length) oa = hashq(r[4].substr(r[4].length-oa.length)) + oa;
  9564. o += oa;
  9565. return o;
  9566. }
  9567. if((r = fmt.match(/^# ([#0?]+)( ?)\/( ?)([#0?]+)/))) {
  9568. ri = Math.min(Math.max(r[1].length, r[4].length),7);
  9569. ff = frac(aval, Math.pow(10,ri)-1, true);
  9570. return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad_(ff[1],ri) + r[2] + "/" + r[3] + rpad_(ff[2],ri): fill(" ", 2*ri+1 + r[2].length + r[3].length));
  9571. }
  9572. if((r = fmt.match(/^[#0?]+$/))) {
  9573. o = "" + val;
  9574. if(fmt.length <= o.length) return o;
  9575. return hashq(fmt.substr(0,fmt.length-o.length)) + o;
  9576. }
  9577. if((r = fmt.match(/^([#0]+)\.([#0]+)$/))) {
  9578. o = "" + val.toFixed(Math.min(r[2].length,10)).replace(/([^0])0+$/,"$1");
  9579. ri = o.indexOf(".");
  9580. var lres = fmt.indexOf(".") - ri, rres = fmt.length - o.length - lres;
  9581. return hashq(fmt.substr(0,lres) + o + fmt.substr(fmt.length-rres));
  9582. }
  9583. if((r = fmt.match(/^00,000\.([#0]*0)$/))) {
  9584. return val < 0 ? "-" + write_num_int(type, fmt, -val) : commaify(""+val).replace(/^\d,\d{3}$/,"0$&").replace(/^\d*$/,function($$) { return "00," + ($$.length < 3 ? pad0(0,3-$$.length) : "") + $$; }) + "." + pad0(0,r[1].length);
  9585. }
  9586. switch(fmt) {
  9587. case "###,###":
  9588. case "##,###":
  9589. case "#,###": var x = commaify(""+aval); return x !== "0" ? sign + x : "";
  9590. default:
  9591. if(fmt.match(/\.[0#?]*$/)) return write_num_int(type, fmt.slice(0,fmt.lastIndexOf(".")), val) + hashq(fmt.slice(fmt.lastIndexOf(".")));
  9592. }
  9593. throw new Error("unsupported format |" + fmt + "|");
  9594. }
  9595. return function write_num(type, fmt, val) {
  9596. return (val|0) === val ? write_num_int(type, fmt, val) : write_num_flt(type, fmt, val);
  9597. };})();
  9598. function split_fmt(fmt) {
  9599. var out = [];
  9600. var in_str = false/*, cc*/;
  9601. for(var i = 0, j = 0; i < fmt.length; ++i) switch((/*cc=*/fmt.charCodeAt(i))) {
  9602. case 34: /* '"' */
  9603. in_str = !in_str; break;
  9604. case 95: case 42: case 92: /* '_' '*' '\\' */
  9605. ++i; break;
  9606. case 59: /* ';' */
  9607. out[out.length] = fmt.substr(j,i-j);
  9608. j = i+1;
  9609. }
  9610. out[out.length] = fmt.substr(j);
  9611. if(in_str === true) throw new Error("Format |" + fmt + "| unterminated string ");
  9612. return out;
  9613. }
  9614. SSF._split = split_fmt;
  9615. var abstime = /\[[HhMmSs]*\]/;
  9616. function fmt_is_date(fmt) {
  9617. var i = 0, /*cc = 0,*/ c = "", o = "";
  9618. while(i < fmt.length) {
  9619. switch((c = fmt.charAt(i))) {
  9620. case 'G': if(isgeneral(fmt, i)) i+= 6; i++; break;
  9621. case '"': for(;(/*cc=*/fmt.charCodeAt(++i)) !== 34 && i < fmt.length;) ++i; ++i; break;
  9622. case '\\': i+=2; break;
  9623. case '_': i+=2; break;
  9624. case '@': ++i; break;
  9625. case 'B': case 'b':
  9626. if(fmt.charAt(i+1) === "1" || fmt.charAt(i+1) === "2") return true;
  9627. /* falls through */
  9628. case 'M': case 'D': case 'Y': case 'H': case 'S': case 'E':
  9629. /* falls through */
  9630. case 'm': case 'd': case 'y': case 'h': case 's': case 'e': case 'g': return true;
  9631. case 'A': case 'a':
  9632. if(fmt.substr(i, 3).toUpperCase() === "A/P") return true;
  9633. if(fmt.substr(i, 5).toUpperCase() === "AM/PM") return true;
  9634. ++i; break;
  9635. case '[':
  9636. o = c;
  9637. while(fmt.charAt(i++) !== ']' && i < fmt.length) o += fmt.charAt(i);
  9638. if(o.match(abstime)) return true;
  9639. break;
  9640. case '.':
  9641. /* falls through */
  9642. case '0': case '#':
  9643. while(i < fmt.length && ("0#?.,E+-%".indexOf(c=fmt.charAt(++i)) > -1 || (c=='\\' && fmt.charAt(i+1) == "-" && "0#".indexOf(fmt.charAt(i+2))>-1))){/* empty */}
  9644. break;
  9645. case '?': while(fmt.charAt(++i) === c){/* empty */} break;
  9646. case '*': ++i; if(fmt.charAt(i) == ' ' || fmt.charAt(i) == '*') ++i; break;
  9647. case '(': case ')': ++i; break;
  9648. case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
  9649. while(i < fmt.length && "0123456789".indexOf(fmt.charAt(++i)) > -1){/* empty */} break;
  9650. case ' ': ++i; break;
  9651. default: ++i; break;
  9652. }
  9653. }
  9654. return false;
  9655. }
  9656. SSF.is_date = fmt_is_date;
  9657. function eval_fmt(fmt, v, opts, flen) {
  9658. var out = [], o = "", i = 0, c = "", lst='t', dt, j, cc;
  9659. var hr='H';
  9660. /* Tokenize */
  9661. while(i < fmt.length) {
  9662. switch((c = fmt.charAt(i))) {
  9663. case 'G': /* General */
  9664. if(!isgeneral(fmt, i)) throw new Error('unrecognized character ' + c + ' in ' +fmt);
  9665. out[out.length] = {t:'G', v:'General'}; i+=7; break;
  9666. case '"': /* Literal text */
  9667. for(o="";(cc=fmt.charCodeAt(++i)) !== 34 && i < fmt.length;) o += String.fromCharCode(cc);
  9668. out[out.length] = {t:'t', v:o}; ++i; break;
  9669. case '\\': var w = fmt.charAt(++i), t = (w === "(" || w === ")") ? w : 't';
  9670. out[out.length] = {t:t, v:w}; ++i; break;
  9671. case '_': out[out.length] = {t:'t', v:" "}; i+=2; break;
  9672. case '@': /* Text Placeholder */
  9673. out[out.length] = {t:'T', v:v}; ++i; break;
  9674. case 'B': case 'b':
  9675. if(fmt.charAt(i+1) === "1" || fmt.charAt(i+1) === "2") {
  9676. if(dt==null) { dt=parse_date_code(v, opts, fmt.charAt(i+1) === "2"); if(dt==null) return ""; }
  9677. out[out.length] = {t:'X', v:fmt.substr(i,2)}; lst = c; i+=2; break;
  9678. }
  9679. /* falls through */
  9680. case 'M': case 'D': case 'Y': case 'H': case 'S': case 'E':
  9681. c = c.toLowerCase();
  9682. /* falls through */
  9683. case 'm': case 'd': case 'y': case 'h': case 's': case 'e': case 'g':
  9684. if(v < 0) return "";
  9685. if(dt==null) { dt=parse_date_code(v, opts); if(dt==null) return ""; }
  9686. o = c; while(++i < fmt.length && fmt.charAt(i).toLowerCase() === c) o+=c;
  9687. if(c === 'm' && lst.toLowerCase() === 'h') c = 'M';
  9688. if(c === 'h') c = hr;
  9689. out[out.length] = {t:c, v:o}; lst = c; break;
  9690. case 'A': case 'a':
  9691. var q={t:c, v:c};
  9692. if(dt==null) dt=parse_date_code(v, opts);
  9693. if(fmt.substr(i, 3).toUpperCase() === "A/P") { if(dt!=null) q.v = dt.H >= 12 ? "P" : "A"; q.t = 'T'; hr='h';i+=3;}
  9694. else if(fmt.substr(i,5).toUpperCase() === "AM/PM") { if(dt!=null) q.v = dt.H >= 12 ? "PM" : "AM"; q.t = 'T'; i+=5; hr='h'; }
  9695. else { q.t = "t"; ++i; }
  9696. if(dt==null && q.t === 'T') return "";
  9697. out[out.length] = q; lst = c; break;
  9698. case '[':
  9699. o = c;
  9700. while(fmt.charAt(i++) !== ']' && i < fmt.length) o += fmt.charAt(i);
  9701. if(o.slice(-1) !== ']') throw 'unterminated "[" block: |' + o + '|';
  9702. if(o.match(abstime)) {
  9703. if(dt==null) { dt=parse_date_code(v, opts); if(dt==null) return ""; }
  9704. out[out.length] = {t:'Z', v:o.toLowerCase()};
  9705. lst = o.charAt(1);
  9706. } else if(o.indexOf("$") > -1) {
  9707. o = (o.match(/\$([^-\[\]]*)/)||[])[1]||"$";
  9708. if(!fmt_is_date(fmt)) out[out.length] = {t:'t',v:o};
  9709. }
  9710. break;
  9711. /* Numbers */
  9712. case '.':
  9713. if(dt != null) {
  9714. o = c; while(++i < fmt.length && (c=fmt.charAt(i)) === "0") o += c;
  9715. out[out.length] = {t:'s', v:o}; break;
  9716. }
  9717. /* falls through */
  9718. case '0': case '#':
  9719. o = c; while((++i < fmt.length && "0#?.,E+-%".indexOf(c=fmt.charAt(i)) > -1) || (c=='\\' && fmt.charAt(i+1) == "-" && i < fmt.length - 2 && "0#".indexOf(fmt.charAt(i+2))>-1)) o += c;
  9720. out[out.length] = {t:'n', v:o}; break;
  9721. case '?':
  9722. o = c; while(fmt.charAt(++i) === c) o+=c;
  9723. out[out.length] = {t:c, v:o}; lst = c; break;
  9724. case '*': ++i; if(fmt.charAt(i) == ' ' || fmt.charAt(i) == '*') ++i; break; // **
  9725. case '(': case ')': out[out.length] = {t:(flen===1?'t':c), v:c}; ++i; break;
  9726. case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
  9727. o = c; while(i < fmt.length && "0123456789".indexOf(fmt.charAt(++i)) > -1) o+=fmt.charAt(i);
  9728. out[out.length] = {t:'D', v:o}; break;
  9729. case ' ': out[out.length] = {t:c, v:c}; ++i; break;
  9730. default:
  9731. if(",$-+/():!^&'~{}<>=€acfijklopqrtuvwxzP".indexOf(c) === -1) throw new Error('unrecognized character ' + c + ' in ' + fmt);
  9732. out[out.length] = {t:'t', v:c}; ++i; break;
  9733. }
  9734. }
  9735. var bt = 0, ss0 = 0, ssm;
  9736. for(i=out.length-1, lst='t'; i >= 0; --i) {
  9737. switch(out[i].t) {
  9738. case 'h': case 'H': out[i].t = hr; lst='h'; if(bt < 1) bt = 1; break;
  9739. case 's':
  9740. if((ssm=out[i].v.match(/\.0+$/))) ss0=Math.max(ss0,ssm[0].length-1);
  9741. if(bt < 3) bt = 3;
  9742. /* falls through */
  9743. case 'd': case 'y': case 'M': case 'e': lst=out[i].t; break;
  9744. case 'm': if(lst === 's') { out[i].t = 'M'; if(bt < 2) bt = 2; } break;
  9745. case 'X': /*if(out[i].v === "B2");*/
  9746. break;
  9747. case 'Z':
  9748. if(bt < 1 && out[i].v.match(/[Hh]/)) bt = 1;
  9749. if(bt < 2 && out[i].v.match(/[Mm]/)) bt = 2;
  9750. if(bt < 3 && out[i].v.match(/[Ss]/)) bt = 3;
  9751. }
  9752. }
  9753. switch(bt) {
  9754. case 0: break;
  9755. case 1:
  9756. if(dt.u >= 0.5) { dt.u = 0; ++dt.S; }
  9757. if(dt.S >= 60) { dt.S = 0; ++dt.M; }
  9758. if(dt.M >= 60) { dt.M = 0; ++dt.H; }
  9759. break;
  9760. case 2:
  9761. if(dt.u >= 0.5) { dt.u = 0; ++dt.S; }
  9762. if(dt.S >= 60) { dt.S = 0; ++dt.M; }
  9763. break;
  9764. }
  9765. /* replace fields */
  9766. var nstr = "", jj;
  9767. for(i=0; i < out.length; ++i) {
  9768. switch(out[i].t) {
  9769. case 't': case 'T': case ' ': case 'D': break;
  9770. case 'X': out[i].v = ""; out[i].t = ";"; break;
  9771. case 'd': case 'm': case 'y': case 'h': case 'H': case 'M': case 's': case 'e': case 'b': case 'Z':
  9772. out[i].v = write_date(out[i].t.charCodeAt(0), out[i].v, dt, ss0);
  9773. out[i].t = 't'; break;
  9774. case 'n': case '(': case '?':
  9775. jj = i+1;
  9776. while(out[jj] != null && (
  9777. (c=out[jj].t) === "?" || c === "D" ||
  9778. ((c === " " || c === "t") && out[jj+1] != null && (out[jj+1].t === '?' || out[jj+1].t === "t" && out[jj+1].v === '/')) ||
  9779. (out[i].t === '(' && (c === ' ' || c === 'n' || c === ')')) ||
  9780. (c === 't' && (out[jj].v === '/' || out[jj].v === ' ' && out[jj+1] != null && out[jj+1].t == '?'))
  9781. )) {
  9782. out[i].v += out[jj].v;
  9783. out[jj] = {v:"", t:";"}; ++jj;
  9784. }
  9785. nstr += out[i].v;
  9786. i = jj-1; break;
  9787. case 'G': out[i].t = 't'; out[i].v = general_fmt(v,opts); break;
  9788. }
  9789. }
  9790. var vv = "", myv, ostr;
  9791. if(nstr.length > 0) {
  9792. if(nstr.charCodeAt(0) == 40) /* '(' */ {
  9793. myv = (v<0&&nstr.charCodeAt(0) === 45 ? -v : v);
  9794. ostr = write_num('(', nstr, myv);
  9795. } else {
  9796. myv = (v<0 && flen > 1 ? -v : v);
  9797. ostr = write_num('n', nstr, myv);
  9798. if(myv < 0 && out[0] && out[0].t == 't') {
  9799. ostr = ostr.substr(1);
  9800. out[0].v = "-" + out[0].v;
  9801. }
  9802. }
  9803. jj=ostr.length-1;
  9804. var decpt = out.length;
  9805. for(i=0; i < out.length; ++i) if(out[i] != null && out[i].t != 't' && out[i].v.indexOf(".") > -1) { decpt = i; break; }
  9806. var lasti=out.length;
  9807. if(decpt === out.length && ostr.indexOf("E") === -1) {
  9808. for(i=out.length-1; i>= 0;--i) {
  9809. if(out[i] == null || 'n?('.indexOf(out[i].t) === -1) continue;
  9810. if(jj>=out[i].v.length-1) { jj -= out[i].v.length; out[i].v = ostr.substr(jj+1, out[i].v.length); }
  9811. else if(jj < 0) out[i].v = "";
  9812. else { out[i].v = ostr.substr(0, jj+1); jj = -1; }
  9813. out[i].t = 't';
  9814. lasti = i;
  9815. }
  9816. if(jj>=0 && lasti<out.length) out[lasti].v = ostr.substr(0,jj+1) + out[lasti].v;
  9817. }
  9818. else if(decpt !== out.length && ostr.indexOf("E") === -1) {
  9819. jj = ostr.indexOf(".")-1;
  9820. for(i=decpt; i>= 0; --i) {
  9821. if(out[i] == null || 'n?('.indexOf(out[i].t) === -1) continue;
  9822. j=out[i].v.indexOf(".")>-1&&i===decpt?out[i].v.indexOf(".")-1:out[i].v.length-1;
  9823. vv = out[i].v.substr(j+1);
  9824. for(; j>=0; --j) {
  9825. if(jj>=0 && (out[i].v.charAt(j) === "0" || out[i].v.charAt(j) === "#")) vv = ostr.charAt(jj--) + vv;
  9826. }
  9827. out[i].v = vv;
  9828. out[i].t = 't';
  9829. lasti = i;
  9830. }
  9831. if(jj>=0 && lasti<out.length) out[lasti].v = ostr.substr(0,jj+1) + out[lasti].v;
  9832. jj = ostr.indexOf(".")+1;
  9833. for(i=decpt; i<out.length; ++i) {
  9834. if(out[i] == null || ('n?('.indexOf(out[i].t) === -1 && i !== decpt)) continue;
  9835. j=out[i].v.indexOf(".")>-1&&i===decpt?out[i].v.indexOf(".")+1:0;
  9836. vv = out[i].v.substr(0,j);
  9837. for(; j<out[i].v.length; ++j) {
  9838. if(jj<ostr.length) vv += ostr.charAt(jj++);
  9839. }
  9840. out[i].v = vv;
  9841. out[i].t = 't';
  9842. lasti = i;
  9843. }
  9844. }
  9845. }
  9846. for(i=0; i<out.length; ++i) if(out[i] != null && 'n(?'.indexOf(out[i].t)>-1) {
  9847. myv = (flen >1 && v < 0 && i>0 && out[i-1].v === "-" ? -v:v);
  9848. out[i].v = write_num(out[i].t, out[i].v, myv);
  9849. out[i].t = 't';
  9850. }
  9851. var retval = "";
  9852. for(i=0; i !== out.length; ++i) if(out[i] != null) retval += out[i].v;
  9853. return retval;
  9854. }
  9855. SSF._eval = eval_fmt;
  9856. var cfregex = /\[[=<>]/;
  9857. var cfregex2 = /\[(=|>[=]?|<[>=]?)(-?\d+(?:\.\d*)?)\]/;
  9858. function chkcond(v, rr) {
  9859. if(rr == null) return false;
  9860. var thresh = parseFloat(rr[2]);
  9861. switch(rr[1]) {
  9862. case "=": if(v == thresh) return true; break;
  9863. case ">": if(v > thresh) return true; break;
  9864. case "<": if(v < thresh) return true; break;
  9865. case "<>": if(v != thresh) return true; break;
  9866. case ">=": if(v >= thresh) return true; break;
  9867. case "<=": if(v <= thresh) return true; break;
  9868. }
  9869. return false;
  9870. }
  9871. function choose_fmt(f, v) {
  9872. var fmt = split_fmt(f);
  9873. var l = fmt.length, lat = fmt[l-1].indexOf("@");
  9874. if(l<4 && lat>-1) --l;
  9875. if(fmt.length > 4) throw new Error("cannot find right format for |" + fmt.join("|") + "|");
  9876. if(typeof v !== "number") return [4, fmt.length === 4 || lat>-1?fmt[fmt.length-1]:"@"];
  9877. switch(fmt.length) {
  9878. case 1: fmt = lat>-1 ? ["General", "General", "General", fmt[0]] : [fmt[0], fmt[0], fmt[0], "@"]; break;
  9879. case 2: fmt = lat>-1 ? [fmt[0], fmt[0], fmt[0], fmt[1]] : [fmt[0], fmt[1], fmt[0], "@"]; break;
  9880. case 3: fmt = lat>-1 ? [fmt[0], fmt[1], fmt[0], fmt[2]] : [fmt[0], fmt[1], fmt[2], "@"]; break;
  9881. case 4: break;
  9882. }
  9883. var ff = v > 0 ? fmt[0] : v < 0 ? fmt[1] : fmt[2];
  9884. if(fmt[0].indexOf("[") === -1 && fmt[1].indexOf("[") === -1) return [l, ff];
  9885. if(fmt[0].match(cfregex) != null || fmt[1].match(cfregex) != null) {
  9886. var m1 = fmt[0].match(cfregex2);
  9887. var m2 = fmt[1].match(cfregex2);
  9888. return chkcond(v, m1) ? [l, fmt[0]] : chkcond(v, m2) ? [l, fmt[1]] : [l, fmt[m1 != null && m2 != null ? 2 : 1]];
  9889. }
  9890. return [l, ff];
  9891. }
  9892. function format(fmt,v,o) {
  9893. if(o == null) o = {};
  9894. var sfmt = "";
  9895. switch(typeof fmt) {
  9896. case "string":
  9897. if(fmt == "m/d/yy" && o.dateNF) sfmt = o.dateNF;
  9898. else sfmt = fmt;
  9899. break;
  9900. case "number":
  9901. if(fmt == 14 && o.dateNF) sfmt = o.dateNF;
  9902. else sfmt = (o.table != null ? (o.table) : table_fmt)[fmt];
  9903. break;
  9904. }
  9905. if(isgeneral(sfmt,0)) return general_fmt(v, o);
  9906. if(v instanceof Date) v = datenum_local(v, o.date1904);
  9907. var f = choose_fmt(sfmt, v);
  9908. if(isgeneral(f[1])) return general_fmt(v, o);
  9909. if(v === true) v = "TRUE"; else if(v === false) v = "FALSE";
  9910. else if(v === "" || v == null) return "";
  9911. return eval_fmt(f[1], v, o, f[0]);
  9912. }
  9913. function load_entry(fmt, idx) {
  9914. if(typeof idx != 'number') {
  9915. idx = +idx || -1;
  9916. for(var i = 0; i < 0x0188; ++i) {
  9917. if(table_fmt[i] == undefined) { if(idx < 0) idx = i; continue; }
  9918. if(table_fmt[i] == fmt) { idx = i; break; }
  9919. }
  9920. if(idx < 0) idx = 0x187;
  9921. }
  9922. table_fmt[idx] = fmt;
  9923. return idx;
  9924. }
  9925. SSF.load = load_entry;
  9926. SSF._table = table_fmt;
  9927. SSF.get_table = function get_table() { return table_fmt; };
  9928. SSF.load_table = function load_table(tbl) {
  9929. for(var i=0; i!=0x0188; ++i)
  9930. if(tbl[i] !== undefined) load_entry(tbl[i], i);
  9931. };
  9932. SSF.init_table = init_table;
  9933. SSF.format = format;
  9934. };
  9935. make_ssf(SSF);
  9936. /* map from xlml named formats to SSF TODO: localize */
  9937. var XLMLFormatMap/*{[string]:string}*/ = ({
  9938. "General Number": "General",
  9939. "General Date": SSF._table[22],
  9940. "Long Date": "dddd, mmmm dd, yyyy",
  9941. "Medium Date": SSF._table[15],
  9942. "Short Date": SSF._table[14],
  9943. "Long Time": SSF._table[19],
  9944. "Medium Time": SSF._table[18],
  9945. "Short Time": SSF._table[20],
  9946. "Currency": '"$"#,##0.00_);[Red]\\("$"#,##0.00\\)',
  9947. "Fixed": SSF._table[2],
  9948. "Standard": SSF._table[4],
  9949. "Percent": SSF._table[10],
  9950. "Scientific": SSF._table[11],
  9951. "Yes/No": '"Yes";"Yes";"No";@',
  9952. "True/False": '"True";"True";"False";@',
  9953. "On/Off": '"Yes";"Yes";"No";@'
  9954. });
  9955. var SSFImplicit/*{[number]:string}*/ = ({
  9956. "5": '"$"#,##0_);\\("$"#,##0\\)',
  9957. "6": '"$"#,##0_);[Red]\\("$"#,##0\\)',
  9958. "7": '"$"#,##0.00_);\\("$"#,##0.00\\)',
  9959. "8": '"$"#,##0.00_);[Red]\\("$"#,##0.00\\)',
  9960. "23": 'General', "24": 'General', "25": 'General', "26": 'General',
  9961. "27": 'm/d/yy', "28": 'm/d/yy', "29": 'm/d/yy', "30": 'm/d/yy', "31": 'm/d/yy',
  9962. "32": 'h:mm:ss', "33": 'h:mm:ss', "34": 'h:mm:ss', "35": 'h:mm:ss',
  9963. "36": 'm/d/yy',
  9964. "41": '_(* #,##0_);_(* \(#,##0\);_(* "-"_);_(@_)',
  9965. "42": '_("$"* #,##0_);_("$"* \(#,##0\);_("$"* "-"_);_(@_)',
  9966. "43": '_(* #,##0.00_);_(* \(#,##0.00\);_(* "-"??_);_(@_)',
  9967. "44": '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)',
  9968. "50": 'm/d/yy', "51": 'm/d/yy', "52": 'm/d/yy', "53": 'm/d/yy', "54": 'm/d/yy',
  9969. "55": 'm/d/yy', "56": 'm/d/yy', "57": 'm/d/yy', "58": 'm/d/yy',
  9970. "59": '0',
  9971. "60": '0.00',
  9972. "61": '#,##0',
  9973. "62": '#,##0.00',
  9974. "63": '"$"#,##0_);\\("$"#,##0\\)',
  9975. "64": '"$"#,##0_);[Red]\\("$"#,##0\\)',
  9976. "65": '"$"#,##0.00_);\\("$"#,##0.00\\)',
  9977. "66": '"$"#,##0.00_);[Red]\\("$"#,##0.00\\)',
  9978. "67": '0%',
  9979. "68": '0.00%',
  9980. "69": '# ?/?',
  9981. "70": '# ??/??',
  9982. "71": 'm/d/yy',
  9983. "72": 'm/d/yy',
  9984. "73": 'd-mmm-yy',
  9985. "74": 'd-mmm',
  9986. "75": 'mmm-yy',
  9987. "76": 'h:mm',
  9988. "77": 'h:mm:ss',
  9989. "78": 'm/d/yy h:mm',
  9990. "79": 'mm:ss',
  9991. "80": '[h]:mm:ss',
  9992. "81": 'mmss.0'
  9993. });
  9994. /* dateNF parse TODO: move to SSF */
  9995. var dateNFregex = /[dD]+|[mM]+|[yYeE]+|[Hh]+|[Ss]+/g;
  9996. function dateNF_regex(dateNF) {
  9997. var fmt = typeof dateNF == "number" ? SSF._table[dateNF] : dateNF;
  9998. fmt = fmt.replace(dateNFregex, "(\\d+)");
  9999. return new RegExp("^" + fmt + "$");
  10000. }
  10001. function dateNF_fix(str, dateNF, match) {
  10002. var Y = -1, m = -1, d = -1, H = -1, M = -1, S = -1;
  10003. (dateNF.match(dateNFregex)||[]).forEach(function(n, i) {
  10004. var v = parseInt(match[i+1], 10);
  10005. switch(n.toLowerCase().charAt(0)) {
  10006. case 'y': Y = v; break; case 'd': d = v; break;
  10007. case 'h': H = v; break; case 's': S = v; break;
  10008. case 'm': if(H >= 0) M = v; else m = v; break;
  10009. }
  10010. });
  10011. if(S >= 0 && M == -1 && m >= 0) { M = m; m = -1; }
  10012. var datestr = (("" + (Y>=0?Y: new Date().getFullYear())).slice(-4) + "-" + ("00" + (m>=1?m:1)).slice(-2) + "-" + ("00" + (d>=1?d:1)).slice(-2));
  10013. if(datestr.length == 7) datestr = "0" + datestr;
  10014. if(datestr.length == 8) datestr = "20" + datestr;
  10015. var timestr = (("00" + (H>=0?H:0)).slice(-2) + ":" + ("00" + (M>=0?M:0)).slice(-2) + ":" + ("00" + (S>=0?S:0)).slice(-2));
  10016. if(H == -1 && M == -1 && S == -1) return datestr;
  10017. if(Y == -1 && m == -1 && d == -1) return timestr;
  10018. return datestr + "T" + timestr;
  10019. }
  10020. var DO_NOT_EXPORT_CFB = true;
  10021. /* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */
  10022. /* vim: set ts=2: */
  10023. /*jshint eqnull:true */
  10024. /*exported CFB */
  10025. /*global module, require:false, process:false, Buffer:false, Uint8Array:false, Uint16Array:false */
  10026. /* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */
  10027. /* vim: set ts=2: */
  10028. /*exported CRC32 */
  10029. var CRC32;
  10030. (function (factory) {
  10031. /*jshint ignore:start */
  10032. /*eslint-disable */
  10033. factory(CRC32 = {});
  10034. /*eslint-enable */
  10035. /*jshint ignore:end */
  10036. }(function(CRC32) {
  10037. CRC32.version = '1.2.0';
  10038. /* see perf/crc32table.js */
  10039. /*global Int32Array */
  10040. function signed_crc_table() {
  10041. var c = 0, table = new Array(256);
  10042. for(var n =0; n != 256; ++n){
  10043. c = n;
  10044. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  10045. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  10046. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  10047. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  10048. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  10049. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  10050. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  10051. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  10052. table[n] = c;
  10053. }
  10054. return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table;
  10055. }
  10056. var T = signed_crc_table();
  10057. function crc32_bstr(bstr, seed) {
  10058. var C = seed ^ -1, L = bstr.length - 1;
  10059. for(var i = 0; i < L;) {
  10060. C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF];
  10061. C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF];
  10062. }
  10063. if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF];
  10064. return C ^ -1;
  10065. }
  10066. function crc32_buf(buf, seed) {
  10067. if(buf.length > 10000) return crc32_buf_8(buf, seed);
  10068. var C = seed ^ -1, L = buf.length - 3;
  10069. for(var i = 0; i < L;) {
  10070. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  10071. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  10072. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  10073. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  10074. }
  10075. while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  10076. return C ^ -1;
  10077. }
  10078. function crc32_buf_8(buf, seed) {
  10079. var C = seed ^ -1, L = buf.length - 7;
  10080. for(var i = 0; i < L;) {
  10081. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  10082. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  10083. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  10084. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  10085. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  10086. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  10087. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  10088. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  10089. }
  10090. while(i < L+7) C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  10091. return C ^ -1;
  10092. }
  10093. function crc32_str(str, seed) {
  10094. var C = seed ^ -1;
  10095. for(var i = 0, L=str.length, c, d; i < L;) {
  10096. c = str.charCodeAt(i++);
  10097. if(c < 0x80) {
  10098. C = (C>>>8) ^ T[(C ^ c)&0xFF];
  10099. } else if(c < 0x800) {
  10100. C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF];
  10101. C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
  10102. } else if(c >= 0xD800 && c < 0xE000) {
  10103. c = (c&1023)+64; d = str.charCodeAt(i++)&1023;
  10104. C = (C>>>8) ^ T[(C ^ (240|((c>>8)&7)))&0xFF];
  10105. C = (C>>>8) ^ T[(C ^ (128|((c>>2)&63)))&0xFF];
  10106. C = (C>>>8) ^ T[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF];
  10107. C = (C>>>8) ^ T[(C ^ (128|(d&63)))&0xFF];
  10108. } else {
  10109. C = (C>>>8) ^ T[(C ^ (224|((c>>12)&15)))&0xFF];
  10110. C = (C>>>8) ^ T[(C ^ (128|((c>>6)&63)))&0xFF];
  10111. C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
  10112. }
  10113. }
  10114. return C ^ -1;
  10115. }
  10116. CRC32.table = T;
  10117. CRC32.bstr = crc32_bstr;
  10118. CRC32.buf = crc32_buf;
  10119. CRC32.str = crc32_str;
  10120. }));
  10121. /* [MS-CFB] v20171201 */
  10122. var CFB = (function _CFB(){
  10123. var exports = {};
  10124. exports.version = '1.1.0';
  10125. /* [MS-CFB] 2.6.4 */
  10126. function namecmp(l, r) {
  10127. var L = l.split("/"), R = r.split("/");
  10128. for(var i = 0, c = 0, Z = Math.min(L.length, R.length); i < Z; ++i) {
  10129. if((c = L[i].length - R[i].length)) return c;
  10130. if(L[i] != R[i]) return L[i] < R[i] ? -1 : 1;
  10131. }
  10132. return L.length - R.length;
  10133. }
  10134. function dirname(p) {
  10135. if(p.charAt(p.length - 1) == "/") return (p.slice(0,-1).indexOf("/") === -1) ? p : dirname(p.slice(0, -1));
  10136. var c = p.lastIndexOf("/");
  10137. return (c === -1) ? p : p.slice(0, c+1);
  10138. }
  10139. function filename(p) {
  10140. if(p.charAt(p.length - 1) == "/") return filename(p.slice(0, -1));
  10141. var c = p.lastIndexOf("/");
  10142. return (c === -1) ? p : p.slice(c+1);
  10143. }
  10144. /* -------------------------------------------------------------------------- */
  10145. /* DOS Date format:
  10146. high|YYYYYYYm.mmmddddd.HHHHHMMM.MMMSSSSS|low
  10147. add 1980 to stored year
  10148. stored second should be doubled
  10149. */
  10150. /* write JS date to buf as a DOS date */
  10151. function write_dos_date(buf, date) {
  10152. if(typeof date === "string") date = new Date(date);
  10153. var hms = date.getHours();
  10154. hms = hms << 6 | date.getMinutes();
  10155. hms = hms << 5 | (date.getSeconds()>>>1);
  10156. buf.write_shift(2, hms);
  10157. var ymd = (date.getFullYear() - 1980);
  10158. ymd = ymd << 4 | (date.getMonth()+1);
  10159. ymd = ymd << 5 | date.getDate();
  10160. buf.write_shift(2, ymd);
  10161. }
  10162. /* read four bytes from buf and interpret as a DOS date */
  10163. function parse_dos_date(buf) {
  10164. var hms = buf.read_shift(2) & 0xFFFF;
  10165. var ymd = buf.read_shift(2) & 0xFFFF;
  10166. var val = new Date();
  10167. var d = ymd & 0x1F; ymd >>>= 5;
  10168. var m = ymd & 0x0F; ymd >>>= 4;
  10169. val.setMilliseconds(0);
  10170. val.setFullYear(ymd + 1980);
  10171. val.setMonth(m-1);
  10172. val.setDate(d);
  10173. var S = hms & 0x1F; hms >>>= 5;
  10174. var M = hms & 0x3F; hms >>>= 6;
  10175. val.setHours(hms);
  10176. val.setMinutes(M);
  10177. val.setSeconds(S<<1);
  10178. return val;
  10179. }
  10180. function parse_extra_field(blob) {
  10181. prep_blob(blob, 0);
  10182. var o = {};
  10183. var flags = 0;
  10184. while(blob.l <= blob.length - 4) {
  10185. var type = blob.read_shift(2);
  10186. var sz = blob.read_shift(2), tgt = blob.l + sz;
  10187. var p = {};
  10188. switch(type) {
  10189. /* UNIX-style Timestamps */
  10190. case 0x5455: {
  10191. flags = blob.read_shift(1);
  10192. if(flags & 1) p.mtime = blob.read_shift(4);
  10193. /* for some reason, CD flag corresponds to LFH */
  10194. if(sz > 5) {
  10195. if(flags & 2) p.atime = blob.read_shift(4);
  10196. if(flags & 4) p.ctime = blob.read_shift(4);
  10197. }
  10198. if(p.mtime) p.mt = new Date(p.mtime*1000);
  10199. }
  10200. break;
  10201. }
  10202. blob.l = tgt;
  10203. o[type] = p;
  10204. }
  10205. return o;
  10206. }
  10207. var fs;
  10208. function get_fs() { return fs || (fs = require('fs')); }
  10209. function parse(file, options) {
  10210. if(file[0] == 0x50 && file[1] == 0x4b) return parse_zip(file, options);
  10211. if(file.length < 512) throw new Error("CFB file size " + file.length + " < 512");
  10212. var mver = 3;
  10213. var ssz = 512;
  10214. var nmfs = 0; // number of mini FAT sectors
  10215. var difat_sec_cnt = 0;
  10216. var dir_start = 0;
  10217. var minifat_start = 0;
  10218. var difat_start = 0;
  10219. var fat_addrs = []; // locations of FAT sectors
  10220. /* [MS-CFB] 2.2 Compound File Header */
  10221. var blob = file.slice(0,512);
  10222. prep_blob(blob, 0);
  10223. /* major version */
  10224. var mv = check_get_mver(blob);
  10225. mver = mv[0];
  10226. switch(mver) {
  10227. case 3: ssz = 512; break; case 4: ssz = 4096; break;
  10228. case 0: if(mv[1] == 0) return parse_zip(file, options);
  10229. /* falls through */
  10230. default: throw new Error("Major Version: Expected 3 or 4 saw " + mver);
  10231. }
  10232. /* reprocess header */
  10233. if(ssz !== 512) { blob = file.slice(0,ssz); prep_blob(blob, 28 /* blob.l */); }
  10234. /* Save header for final object */
  10235. var header = file.slice(0,ssz);
  10236. check_shifts(blob, mver);
  10237. // Number of Directory Sectors
  10238. var dir_cnt = blob.read_shift(4, 'i');
  10239. if(mver === 3 && dir_cnt !== 0) throw new Error('# Directory Sectors: Expected 0 saw ' + dir_cnt);
  10240. // Number of FAT Sectors
  10241. blob.l += 4;
  10242. // First Directory Sector Location
  10243. dir_start = blob.read_shift(4, 'i');
  10244. // Transaction Signature
  10245. blob.l += 4;
  10246. // Mini Stream Cutoff Size
  10247. blob.chk('00100000', 'Mini Stream Cutoff Size: ');
  10248. // First Mini FAT Sector Location
  10249. minifat_start = blob.read_shift(4, 'i');
  10250. // Number of Mini FAT Sectors
  10251. nmfs = blob.read_shift(4, 'i');
  10252. // First DIFAT sector location
  10253. difat_start = blob.read_shift(4, 'i');
  10254. // Number of DIFAT Sectors
  10255. difat_sec_cnt = blob.read_shift(4, 'i');
  10256. // Grab FAT Sector Locations
  10257. for(var q = -1, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
  10258. q = blob.read_shift(4, 'i');
  10259. if(q<0) break;
  10260. fat_addrs[j] = q;
  10261. }
  10262. /** Break the file up into sectors */
  10263. var sectors = sectorify(file, ssz);
  10264. sleuth_fat(difat_start, difat_sec_cnt, sectors, ssz, fat_addrs);
  10265. /** Chains */
  10266. var sector_list = make_sector_list(sectors, dir_start, fat_addrs, ssz);
  10267. sector_list[dir_start].name = "!Directory";
  10268. if(nmfs > 0 && minifat_start !== ENDOFCHAIN) sector_list[minifat_start].name = "!MiniFAT";
  10269. sector_list[fat_addrs[0]].name = "!FAT";
  10270. sector_list.fat_addrs = fat_addrs;
  10271. sector_list.ssz = ssz;
  10272. /* [MS-CFB] 2.6.1 Compound File Directory Entry */
  10273. var files = {}, Paths = [], FileIndex = [], FullPaths = [];
  10274. read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex, minifat_start);
  10275. build_full_paths(FileIndex, FullPaths, Paths);
  10276. Paths.shift();
  10277. var o = {
  10278. FileIndex: FileIndex,
  10279. FullPaths: FullPaths
  10280. };
  10281. // $FlowIgnore
  10282. if(options && options.raw) o.raw = {header: header, sectors: sectors};
  10283. return o;
  10284. } // parse
  10285. /* [MS-CFB] 2.2 Compound File Header -- read up to major version */
  10286. function check_get_mver(blob) {
  10287. if(blob[blob.l] == 0x50 && blob[blob.l + 1] == 0x4b) return [0, 0];
  10288. // header signature 8
  10289. blob.chk(HEADER_SIGNATURE, 'Header Signature: ');
  10290. // clsid 16
  10291. blob.chk(HEADER_CLSID, 'CLSID: ');
  10292. // minor version 2
  10293. var mver = blob.read_shift(2, 'u');
  10294. return [blob.read_shift(2,'u'), mver];
  10295. }
  10296. function check_shifts(blob, mver) {
  10297. var shift = 0x09;
  10298. // Byte Order
  10299. //blob.chk('feff', 'Byte Order: '); // note: some writers put 0xffff
  10300. blob.l += 2;
  10301. // Sector Shift
  10302. switch((shift = blob.read_shift(2))) {
  10303. case 0x09: if(mver != 3) throw new Error('Sector Shift: Expected 9 saw ' + shift); break;
  10304. case 0x0c: if(mver != 4) throw new Error('Sector Shift: Expected 12 saw ' + shift); break;
  10305. default: throw new Error('Sector Shift: Expected 9 or 12 saw ' + shift);
  10306. }
  10307. // Mini Sector Shift
  10308. blob.chk('0600', 'Mini Sector Shift: ');
  10309. // Reserved
  10310. blob.chk('000000000000', 'Reserved: ');
  10311. }
  10312. /** Break the file up into sectors */
  10313. function sectorify(file, ssz) {
  10314. var nsectors = Math.ceil(file.length/ssz)-1;
  10315. var sectors = [];
  10316. for(var i=1; i < nsectors; ++i) sectors[i-1] = file.slice(i*ssz,(i+1)*ssz);
  10317. sectors[nsectors-1] = file.slice(nsectors*ssz);
  10318. return sectors;
  10319. }
  10320. /* [MS-CFB] 2.6.4 Red-Black Tree */
  10321. function build_full_paths(FI, FP, Paths) {
  10322. var i = 0, L = 0, R = 0, C = 0, j = 0, pl = Paths.length;
  10323. var dad = [], q = [];
  10324. for(; i < pl; ++i) { dad[i]=q[i]=i; FP[i]=Paths[i]; }
  10325. for(; j < q.length; ++j) {
  10326. i = q[j];
  10327. L = FI[i].L; R = FI[i].R; C = FI[i].C;
  10328. if(dad[i] === i) {
  10329. if(L !== -1 /*NOSTREAM*/ && dad[L] !== L) dad[i] = dad[L];
  10330. if(R !== -1 && dad[R] !== R) dad[i] = dad[R];
  10331. }
  10332. if(C !== -1 /*NOSTREAM*/) dad[C] = i;
  10333. if(L !== -1) { dad[L] = dad[i]; if(q.lastIndexOf(L) < j) q.push(L); }
  10334. if(R !== -1) { dad[R] = dad[i]; if(q.lastIndexOf(R) < j) q.push(R); }
  10335. }
  10336. for(i=1; i < pl; ++i) if(dad[i] === i) {
  10337. if(R !== -1 /*NOSTREAM*/ && dad[R] !== R) dad[i] = dad[R];
  10338. else if(L !== -1 && dad[L] !== L) dad[i] = dad[L];
  10339. }
  10340. for(i=1; i < pl; ++i) {
  10341. if(FI[i].type === 0 /* unknown */) continue;
  10342. j = dad[i];
  10343. if(j === 0) FP[i] = FP[0] + "/" + FP[i];
  10344. else while(j !== 0 && j !== dad[j]) {
  10345. FP[i] = FP[j] + "/" + FP[i];
  10346. j = dad[j];
  10347. }
  10348. dad[i] = 0;
  10349. }
  10350. FP[0] += "/";
  10351. for(i=1; i < pl; ++i) {
  10352. if(FI[i].type !== 2 /* stream */) FP[i] += "/";
  10353. }
  10354. }
  10355. function get_mfat_entry(entry, payload, mini) {
  10356. var start = entry.start, size = entry.size;
  10357. //return (payload.slice(start*MSSZ, start*MSSZ + size));
  10358. var o = [];
  10359. var idx = start;
  10360. while(mini && size > 0 && idx >= 0) {
  10361. o.push(payload.slice(idx * MSSZ, idx * MSSZ + MSSZ));
  10362. size -= MSSZ;
  10363. idx = __readInt32LE(mini, idx * 4);
  10364. }
  10365. if(o.length === 0) return (new_buf(0));
  10366. return (bconcat(o).slice(0, entry.size));
  10367. }
  10368. /** Chase down the rest of the DIFAT chain to build a comprehensive list
  10369. DIFAT chains by storing the next sector number as the last 32 bits */
  10370. function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
  10371. var q = ENDOFCHAIN;
  10372. if(idx === ENDOFCHAIN) {
  10373. if(cnt !== 0) throw new Error("DIFAT chain shorter than expected");
  10374. } else if(idx !== -1 /*FREESECT*/) {
  10375. var sector = sectors[idx], m = (ssz>>>2)-1;
  10376. if(!sector) return;
  10377. for(var i = 0; i < m; ++i) {
  10378. if((q = __readInt32LE(sector,i*4)) === ENDOFCHAIN) break;
  10379. fat_addrs.push(q);
  10380. }
  10381. sleuth_fat(__readInt32LE(sector,ssz-4),cnt - 1, sectors, ssz, fat_addrs);
  10382. }
  10383. }
  10384. /** Follow the linked list of sectors for a given starting point */
  10385. function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
  10386. var buf = [], buf_chain = [];
  10387. if(!chkd) chkd = [];
  10388. var modulus = ssz - 1, j = 0, jj = 0;
  10389. for(j=start; j>=0;) {
  10390. chkd[j] = true;
  10391. buf[buf.length] = j;
  10392. buf_chain.push(sectors[j]);
  10393. var addr = fat_addrs[Math.floor(j*4/ssz)];
  10394. jj = ((j*4) & modulus);
  10395. if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz);
  10396. if(!sectors[addr]) break;
  10397. j = __readInt32LE(sectors[addr], jj);
  10398. }
  10399. return {nodes: buf, data:__toBuffer([buf_chain])};
  10400. }
  10401. /** Chase down the sector linked lists */
  10402. function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
  10403. var sl = sectors.length, sector_list = ([]);
  10404. var chkd = [], buf = [], buf_chain = [];
  10405. var modulus = ssz - 1, i=0, j=0, k=0, jj=0;
  10406. for(i=0; i < sl; ++i) {
  10407. buf = ([]);
  10408. k = (i + dir_start); if(k >= sl) k-=sl;
  10409. if(chkd[k]) continue;
  10410. buf_chain = [];
  10411. for(j=k; j>=0;) {
  10412. chkd[j] = true;
  10413. buf[buf.length] = j;
  10414. buf_chain.push(sectors[j]);
  10415. var addr = fat_addrs[Math.floor(j*4/ssz)];
  10416. jj = ((j*4) & modulus);
  10417. if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz);
  10418. if(!sectors[addr]) break;
  10419. j = __readInt32LE(sectors[addr], jj);
  10420. }
  10421. sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])});
  10422. }
  10423. return sector_list;
  10424. }
  10425. /* [MS-CFB] 2.6.1 Compound File Directory Entry */
  10426. function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex, mini) {
  10427. var minifat_store = 0, pl = (Paths.length?2:0);
  10428. var sector = sector_list[dir_start].data;
  10429. var i = 0, namelen = 0, name;
  10430. for(; i < sector.length; i+= 128) {
  10431. var blob = sector.slice(i, i+128);
  10432. prep_blob(blob, 64);
  10433. namelen = blob.read_shift(2);
  10434. name = __utf16le(blob,0,namelen-pl);
  10435. Paths.push(name);
  10436. var o = ({
  10437. name: name,
  10438. type: blob.read_shift(1),
  10439. color: blob.read_shift(1),
  10440. L: blob.read_shift(4, 'i'),
  10441. R: blob.read_shift(4, 'i'),
  10442. C: blob.read_shift(4, 'i'),
  10443. clsid: blob.read_shift(16),
  10444. state: blob.read_shift(4, 'i'),
  10445. start: 0,
  10446. size: 0
  10447. });
  10448. var ctime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
  10449. if(ctime !== 0) o.ct = read_date(blob, blob.l-8);
  10450. var mtime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
  10451. if(mtime !== 0) o.mt = read_date(blob, blob.l-8);
  10452. o.start = blob.read_shift(4, 'i');
  10453. o.size = blob.read_shift(4, 'i');
  10454. if(o.size < 0 && o.start < 0) { o.size = o.type = 0; o.start = ENDOFCHAIN; o.name = ""; }
  10455. if(o.type === 5) { /* root */
  10456. minifat_store = o.start;
  10457. if(nmfs > 0 && minifat_store !== ENDOFCHAIN) sector_list[minifat_store].name = "!StreamData";
  10458. /*minifat_size = o.size;*/
  10459. } else if(o.size >= 4096 /* MSCSZ */) {
  10460. o.storage = 'fat';
  10461. if(sector_list[o.start] === undefined) sector_list[o.start] = get_sector_list(sectors, o.start, sector_list.fat_addrs, sector_list.ssz);
  10462. sector_list[o.start].name = o.name;
  10463. o.content = (sector_list[o.start].data.slice(0,o.size));
  10464. } else {
  10465. o.storage = 'minifat';
  10466. if(o.size < 0) o.size = 0;
  10467. else if(minifat_store !== ENDOFCHAIN && o.start !== ENDOFCHAIN && sector_list[minifat_store]) {
  10468. o.content = get_mfat_entry(o, sector_list[minifat_store].data, (sector_list[mini]||{}).data);
  10469. }
  10470. }
  10471. if(o.content) prep_blob(o.content, 0);
  10472. files[name] = o;
  10473. FileIndex.push(o);
  10474. }
  10475. }
  10476. function read_date(blob, offset) {
  10477. return new Date(( ( (__readUInt32LE(blob,offset+4)/1e7)*Math.pow(2,32)+__readUInt32LE(blob,offset)/1e7 ) - 11644473600)*1000);
  10478. }
  10479. function read_file(filename, options) {
  10480. get_fs();
  10481. return parse(fs.readFileSync(filename), options);
  10482. }
  10483. function read(blob, options) {
  10484. switch(options && options.type || "base64") {
  10485. case "file": return read_file(blob, options);
  10486. case "base64": return parse(s2a(Base64.decode(blob)), options);
  10487. case "binary": return parse(s2a(blob), options);
  10488. }
  10489. return parse(blob, options);
  10490. }
  10491. function init_cfb(cfb, opts) {
  10492. var o = opts || {}, root = o.root || "Root Entry";
  10493. if(!cfb.FullPaths) cfb.FullPaths = [];
  10494. if(!cfb.FileIndex) cfb.FileIndex = [];
  10495. if(cfb.FullPaths.length !== cfb.FileIndex.length) throw new Error("inconsistent CFB structure");
  10496. if(cfb.FullPaths.length === 0) {
  10497. cfb.FullPaths[0] = root + "/";
  10498. cfb.FileIndex[0] = ({ name: root, type: 5 });
  10499. }
  10500. if(o.CLSID) cfb.FileIndex[0].clsid = o.CLSID;
  10501. seed_cfb(cfb);
  10502. }
  10503. function seed_cfb(cfb) {
  10504. var nm = "\u0001Sh33tJ5";
  10505. if(CFB.find(cfb, "/" + nm)) return;
  10506. var p = new_buf(4); p[0] = 55; p[1] = p[3] = 50; p[2] = 54;
  10507. cfb.FileIndex.push(({ name: nm, type: 2, content:p, size:4, L:69, R:69, C:69 }));
  10508. cfb.FullPaths.push(cfb.FullPaths[0] + nm);
  10509. rebuild_cfb(cfb);
  10510. }
  10511. function rebuild_cfb(cfb, f) {
  10512. init_cfb(cfb);
  10513. var gc = false, s = false;
  10514. for(var i = cfb.FullPaths.length - 1; i >= 0; --i) {
  10515. var _file = cfb.FileIndex[i];
  10516. switch(_file.type) {
  10517. case 0:
  10518. if(s) gc = true;
  10519. else { cfb.FileIndex.pop(); cfb.FullPaths.pop(); }
  10520. break;
  10521. case 1: case 2: case 5:
  10522. s = true;
  10523. if(isNaN(_file.R * _file.L * _file.C)) gc = true;
  10524. if(_file.R > -1 && _file.L > -1 && _file.R == _file.L) gc = true;
  10525. break;
  10526. default: gc = true; break;
  10527. }
  10528. }
  10529. if(!gc && !f) return;
  10530. var now = new Date(1987, 1, 19), j = 0;
  10531. var data = [];
  10532. for(i = 0; i < cfb.FullPaths.length; ++i) {
  10533. if(cfb.FileIndex[i].type === 0) continue;
  10534. data.push([cfb.FullPaths[i], cfb.FileIndex[i]]);
  10535. }
  10536. for(i = 0; i < data.length; ++i) {
  10537. var dad = dirname(data[i][0]);
  10538. s = false;
  10539. for(j = 0; j < data.length; ++j) if(data[j][0] === dad) s = true;
  10540. if(!s) data.push([dad, ({
  10541. name: filename(dad).replace("/",""),
  10542. type: 1,
  10543. clsid: HEADER_CLSID,
  10544. ct: now, mt: now,
  10545. content: null
  10546. })]);
  10547. }
  10548. data.sort(function(x,y) { return namecmp(x[0], y[0]); });
  10549. cfb.FullPaths = []; cfb.FileIndex = [];
  10550. for(i = 0; i < data.length; ++i) { cfb.FullPaths[i] = data[i][0]; cfb.FileIndex[i] = data[i][1]; }
  10551. for(i = 0; i < data.length; ++i) {
  10552. var elt = cfb.FileIndex[i];
  10553. var nm = cfb.FullPaths[i];
  10554. elt.name = filename(nm).replace("/","");
  10555. elt.L = elt.R = elt.C = -(elt.color = 1);
  10556. elt.size = elt.content ? elt.content.length : 0;
  10557. elt.start = 0;
  10558. elt.clsid = (elt.clsid || HEADER_CLSID);
  10559. if(i === 0) {
  10560. elt.C = data.length > 1 ? 1 : -1;
  10561. elt.size = 0;
  10562. elt.type = 5;
  10563. } else if(nm.slice(-1) == "/") {
  10564. for(j=i+1;j < data.length; ++j) if(dirname(cfb.FullPaths[j])==nm) break;
  10565. elt.C = j >= data.length ? -1 : j;
  10566. for(j=i+1;j < data.length; ++j) if(dirname(cfb.FullPaths[j])==dirname(nm)) break;
  10567. elt.R = j >= data.length ? -1 : j;
  10568. elt.type = 1;
  10569. } else {
  10570. if(dirname(cfb.FullPaths[i+1]||"") == dirname(nm)) elt.R = i + 1;
  10571. elt.type = 2;
  10572. }
  10573. }
  10574. }
  10575. function _write(cfb, options) {
  10576. var _opts = options || {};
  10577. rebuild_cfb(cfb);
  10578. if(_opts.fileType == 'zip') return write_zip(cfb, _opts);
  10579. var L = (function(cfb){
  10580. var mini_size = 0, fat_size = 0;
  10581. for(var i = 0; i < cfb.FileIndex.length; ++i) {
  10582. var file = cfb.FileIndex[i];
  10583. if(!file.content) continue;
  10584. var flen = file.content.length;
  10585. if(flen > 0){
  10586. if(flen < 0x1000) mini_size += (flen + 0x3F) >> 6;
  10587. else fat_size += (flen + 0x01FF) >> 9;
  10588. }
  10589. }
  10590. var dir_cnt = (cfb.FullPaths.length +3) >> 2;
  10591. var mini_cnt = (mini_size + 7) >> 3;
  10592. var mfat_cnt = (mini_size + 0x7F) >> 7;
  10593. var fat_base = mini_cnt + fat_size + dir_cnt + mfat_cnt;
  10594. var fat_cnt = (fat_base + 0x7F) >> 7;
  10595. var difat_cnt = fat_cnt <= 109 ? 0 : Math.ceil((fat_cnt-109)/0x7F);
  10596. while(((fat_base + fat_cnt + difat_cnt + 0x7F) >> 7) > fat_cnt) difat_cnt = ++fat_cnt <= 109 ? 0 : Math.ceil((fat_cnt-109)/0x7F);
  10597. var L = [1, difat_cnt, fat_cnt, mfat_cnt, dir_cnt, fat_size, mini_size, 0];
  10598. cfb.FileIndex[0].size = mini_size << 6;
  10599. L[7] = (cfb.FileIndex[0].start=L[0]+L[1]+L[2]+L[3]+L[4]+L[5])+((L[6]+7) >> 3);
  10600. return L;
  10601. })(cfb);
  10602. var o = new_buf(L[7] << 9);
  10603. var i = 0, T = 0;
  10604. {
  10605. for(i = 0; i < 8; ++i) o.write_shift(1, HEADER_SIG[i]);
  10606. for(i = 0; i < 8; ++i) o.write_shift(2, 0);
  10607. o.write_shift(2, 0x003E);
  10608. o.write_shift(2, 0x0003);
  10609. o.write_shift(2, 0xFFFE);
  10610. o.write_shift(2, 0x0009);
  10611. o.write_shift(2, 0x0006);
  10612. for(i = 0; i < 3; ++i) o.write_shift(2, 0);
  10613. o.write_shift(4, 0);
  10614. o.write_shift(4, L[2]);
  10615. o.write_shift(4, L[0] + L[1] + L[2] + L[3] - 1);
  10616. o.write_shift(4, 0);
  10617. o.write_shift(4, 1<<12);
  10618. o.write_shift(4, L[3] ? L[0] + L[1] + L[2] - 1: ENDOFCHAIN);
  10619. o.write_shift(4, L[3]);
  10620. o.write_shift(-4, L[1] ? L[0] - 1: ENDOFCHAIN);
  10621. o.write_shift(4, L[1]);
  10622. for(i = 0; i < 109; ++i) o.write_shift(-4, i < L[2] ? L[1] + i : -1);
  10623. }
  10624. if(L[1]) {
  10625. for(T = 0; T < L[1]; ++T) {
  10626. for(; i < 236 + T * 127; ++i) o.write_shift(-4, i < L[2] ? L[1] + i : -1);
  10627. o.write_shift(-4, T === L[1] - 1 ? ENDOFCHAIN : T + 1);
  10628. }
  10629. }
  10630. var chainit = function(w) {
  10631. for(T += w; i<T-1; ++i) o.write_shift(-4, i+1);
  10632. if(w) { ++i; o.write_shift(-4, ENDOFCHAIN); }
  10633. };
  10634. T = i = 0;
  10635. for(T+=L[1]; i<T; ++i) o.write_shift(-4, consts.DIFSECT);
  10636. for(T+=L[2]; i<T; ++i) o.write_shift(-4, consts.FATSECT);
  10637. chainit(L[3]);
  10638. chainit(L[4]);
  10639. var j = 0, flen = 0;
  10640. var file = cfb.FileIndex[0];
  10641. for(; j < cfb.FileIndex.length; ++j) {
  10642. file = cfb.FileIndex[j];
  10643. if(!file.content) continue;
  10644. flen = file.content.length;
  10645. if(flen < 0x1000) continue;
  10646. file.start = T;
  10647. chainit((flen + 0x01FF) >> 9);
  10648. }
  10649. chainit((L[6] + 7) >> 3);
  10650. while(o.l & 0x1FF) o.write_shift(-4, consts.ENDOFCHAIN);
  10651. T = i = 0;
  10652. for(j = 0; j < cfb.FileIndex.length; ++j) {
  10653. file = cfb.FileIndex[j];
  10654. if(!file.content) continue;
  10655. flen = file.content.length;
  10656. if(!flen || flen >= 0x1000) continue;
  10657. file.start = T;
  10658. chainit((flen + 0x3F) >> 6);
  10659. }
  10660. while(o.l & 0x1FF) o.write_shift(-4, consts.ENDOFCHAIN);
  10661. for(i = 0; i < L[4]<<2; ++i) {
  10662. var nm = cfb.FullPaths[i];
  10663. if(!nm || nm.length === 0) {
  10664. for(j = 0; j < 17; ++j) o.write_shift(4, 0);
  10665. for(j = 0; j < 3; ++j) o.write_shift(4, -1);
  10666. for(j = 0; j < 12; ++j) o.write_shift(4, 0);
  10667. continue;
  10668. }
  10669. file = cfb.FileIndex[i];
  10670. if(i === 0) file.start = file.size ? file.start - 1 : ENDOFCHAIN;
  10671. var _nm = (i === 0 && _opts.root) || file.name;
  10672. flen = 2*(_nm.length+1);
  10673. o.write_shift(64, _nm, "utf16le");
  10674. o.write_shift(2, flen);
  10675. o.write_shift(1, file.type);
  10676. o.write_shift(1, file.color);
  10677. o.write_shift(-4, file.L);
  10678. o.write_shift(-4, file.R);
  10679. o.write_shift(-4, file.C);
  10680. if(!file.clsid) for(j = 0; j < 4; ++j) o.write_shift(4, 0);
  10681. else o.write_shift(16, file.clsid, "hex");
  10682. o.write_shift(4, file.state || 0);
  10683. o.write_shift(4, 0); o.write_shift(4, 0);
  10684. o.write_shift(4, 0); o.write_shift(4, 0);
  10685. o.write_shift(4, file.start);
  10686. o.write_shift(4, file.size); o.write_shift(4, 0);
  10687. }
  10688. for(i = 1; i < cfb.FileIndex.length; ++i) {
  10689. file = cfb.FileIndex[i];
  10690. if(file.size >= 0x1000) {
  10691. o.l = (file.start+1) << 9;
  10692. for(j = 0; j < file.size; ++j) o.write_shift(1, file.content[j]);
  10693. for(; j & 0x1FF; ++j) o.write_shift(1, 0);
  10694. }
  10695. }
  10696. for(i = 1; i < cfb.FileIndex.length; ++i) {
  10697. file = cfb.FileIndex[i];
  10698. if(file.size > 0 && file.size < 0x1000) {
  10699. for(j = 0; j < file.size; ++j) o.write_shift(1, file.content[j]);
  10700. for(; j & 0x3F; ++j) o.write_shift(1, 0);
  10701. }
  10702. }
  10703. while(o.l < o.length) o.write_shift(1, 0);
  10704. return o;
  10705. }
  10706. /* [MS-CFB] 2.6.4 (Unicode 3.0.1 case conversion) */
  10707. function find(cfb, path) {
  10708. var UCFullPaths = cfb.FullPaths.map(function(x) { return x.toUpperCase(); });
  10709. var UCPaths = UCFullPaths.map(function(x) { var y = x.split("/"); return y[y.length - (x.slice(-1) == "/" ? 2 : 1)]; });
  10710. var k = false;
  10711. if(path.charCodeAt(0) === 47 /* "/" */) { k = true; path = UCFullPaths[0].slice(0, -1) + path; }
  10712. else k = path.indexOf("/") !== -1;
  10713. var UCPath = path.toUpperCase();
  10714. var w = k === true ? UCFullPaths.indexOf(UCPath) : UCPaths.indexOf(UCPath);
  10715. if(w !== -1) return cfb.FileIndex[w];
  10716. var m = !UCPath.match(chr1);
  10717. UCPath = UCPath.replace(chr0,'');
  10718. if(m) UCPath = UCPath.replace(chr1,'!');
  10719. for(w = 0; w < UCFullPaths.length; ++w) {
  10720. if((m ? UCFullPaths[w].replace(chr1,'!') : UCFullPaths[w]).replace(chr0,'') == UCPath) return cfb.FileIndex[w];
  10721. if((m ? UCPaths[w].replace(chr1,'!') : UCPaths[w]).replace(chr0,'') == UCPath) return cfb.FileIndex[w];
  10722. }
  10723. return null;
  10724. }
  10725. /** CFB Constants */
  10726. var MSSZ = 64; /* Mini Sector Size = 1<<6 */
  10727. //var MSCSZ = 4096; /* Mini Stream Cutoff Size */
  10728. /* 2.1 Compound File Sector Numbers and Types */
  10729. var ENDOFCHAIN = -2;
  10730. /* 2.2 Compound File Header */
  10731. var HEADER_SIGNATURE = 'd0cf11e0a1b11ae1';
  10732. var HEADER_SIG = [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1];
  10733. var HEADER_CLSID = '00000000000000000000000000000000';
  10734. var consts = {
  10735. /* 2.1 Compund File Sector Numbers and Types */
  10736. MAXREGSECT: -6,
  10737. DIFSECT: -4,
  10738. FATSECT: -3,
  10739. ENDOFCHAIN: ENDOFCHAIN,
  10740. FREESECT: -1,
  10741. /* 2.2 Compound File Header */
  10742. HEADER_SIGNATURE: HEADER_SIGNATURE,
  10743. HEADER_MINOR_VERSION: '3e00',
  10744. MAXREGSID: -6,
  10745. NOSTREAM: -1,
  10746. HEADER_CLSID: HEADER_CLSID,
  10747. /* 2.6.1 Compound File Directory Entry */
  10748. EntryTypes: ['unknown','storage','stream','lockbytes','property','root']
  10749. };
  10750. function write_file(cfb, filename, options) {
  10751. get_fs();
  10752. var o = _write(cfb, options);
  10753. fs.writeFileSync(filename, o);
  10754. }
  10755. function a2s(o) {
  10756. var out = new Array(o.length);
  10757. for(var i = 0; i < o.length; ++i) out[i] = String.fromCharCode(o[i]);
  10758. return out.join("");
  10759. }
  10760. function write(cfb, options) {
  10761. var o = _write(cfb, options);
  10762. switch(options && options.type) {
  10763. case "file": get_fs(); fs.writeFileSync(options.filename, (o)); return o;
  10764. case "binary": return a2s(o);
  10765. case "base64": return Base64.encode(a2s(o));
  10766. }
  10767. return o;
  10768. }
  10769. /* node < 8.1 zlib does not expose bytesRead, so default to pure JS */
  10770. var _zlib;
  10771. function use_zlib(zlib) { try {
  10772. var InflateRaw = zlib.InflateRaw;
  10773. var InflRaw = new InflateRaw();
  10774. InflRaw._processChunk(new Uint8Array([3, 0]), InflRaw._finishFlushFlag);
  10775. if(InflRaw.bytesRead) _zlib = zlib;
  10776. else throw new Error("zlib does not expose bytesRead");
  10777. } catch(e) {console.error("cannot use native zlib: " + (e.message || e)); } }
  10778. function _inflateRawSync(payload, usz) {
  10779. if(!_zlib) return _inflate(payload, usz);
  10780. var InflateRaw = _zlib.InflateRaw;
  10781. var InflRaw = new InflateRaw();
  10782. var out = InflRaw._processChunk(payload.slice(payload.l), InflRaw._finishFlushFlag);
  10783. payload.l += InflRaw.bytesRead;
  10784. return out;
  10785. }
  10786. function _deflateRawSync(payload) {
  10787. return _zlib ? _zlib.deflateRawSync(payload) : _deflate(payload);
  10788. }
  10789. var CLEN_ORDER = [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ];
  10790. /* LEN_ID = [ 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285 ]; */
  10791. var LEN_LN = [ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13 , 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258 ];
  10792. /* DST_ID = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 ]; */
  10793. var DST_LN = [ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 ];
  10794. function bit_swap_8(n) { var t = (((((n<<1)|(n<<11)) & 0x22110) | (((n<<5)|(n<<15)) & 0x88440))); return ((t>>16) | (t>>8) |t)&0xFF; }
  10795. var use_typed_arrays = typeof Uint8Array !== 'undefined';
  10796. var bitswap8 = use_typed_arrays ? new Uint8Array(1<<8) : [];
  10797. for(var q = 0; q < (1<<8); ++q) bitswap8[q] = bit_swap_8(q);
  10798. function bit_swap_n(n, b) {
  10799. var rev = bitswap8[n & 0xFF];
  10800. if(b <= 8) return rev >>> (8-b);
  10801. rev = (rev << 8) | bitswap8[(n>>8)&0xFF];
  10802. if(b <= 16) return rev >>> (16-b);
  10803. rev = (rev << 8) | bitswap8[(n>>16)&0xFF];
  10804. return rev >>> (24-b);
  10805. }
  10806. /* helpers for unaligned bit reads */
  10807. function read_bits_2(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 6 ? 0 : buf[h+1]<<8))>>>w)& 0x03; }
  10808. function read_bits_3(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 5 ? 0 : buf[h+1]<<8))>>>w)& 0x07; }
  10809. function read_bits_4(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 4 ? 0 : buf[h+1]<<8))>>>w)& 0x0F; }
  10810. function read_bits_5(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 3 ? 0 : buf[h+1]<<8))>>>w)& 0x1F; }
  10811. function read_bits_7(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 1 ? 0 : buf[h+1]<<8))>>>w)& 0x7F; }
  10812. /* works up to n = 3 * 8 + 1 = 25 */
  10813. function read_bits_n(buf, bl, n) {
  10814. var w = (bl&7), h = (bl>>>3), f = ((1<<n)-1);
  10815. var v = buf[h] >>> w;
  10816. if(n < 8 - w) return v & f;
  10817. v |= buf[h+1]<<(8-w);
  10818. if(n < 16 - w) return v & f;
  10819. v |= buf[h+2]<<(16-w);
  10820. if(n < 24 - w) return v & f;
  10821. v |= buf[h+3]<<(24-w);
  10822. return v & f;
  10823. }
  10824. /* until ArrayBuffer#realloc is a thing, fake a realloc */
  10825. function realloc(b, sz) {
  10826. var L = b.length, M = 2*L > sz ? 2*L : sz + 5, i = 0;
  10827. if(L >= sz) return b;
  10828. if(has_buf) {
  10829. var o = new_unsafe_buf(M);
  10830. // $FlowIgnore
  10831. if(b.copy) b.copy(o);
  10832. else for(; i < b.length; ++i) o[i] = b[i];
  10833. return o;
  10834. } else if(use_typed_arrays) {
  10835. var a = new Uint8Array(M);
  10836. if(a.set) a.set(b);
  10837. else for(; i < b.length; ++i) a[i] = b[i];
  10838. return a;
  10839. }
  10840. b.length = M;
  10841. return b;
  10842. }
  10843. /* zero-filled arrays for older browsers */
  10844. function zero_fill_array(n) {
  10845. var o = new Array(n);
  10846. for(var i = 0; i < n; ++i) o[i] = 0;
  10847. return o;
  10848. }var _deflate = (function() {
  10849. var _deflateRaw = (function() {
  10850. return function deflateRaw(data, out) {
  10851. var boff = 0;
  10852. while(boff < data.length) {
  10853. var L = Math.min(0xFFFF, data.length - boff);
  10854. var h = boff + L == data.length;
  10855. /* TODO: this is only type 0 stored */
  10856. out.write_shift(1, +h);
  10857. out.write_shift(2, L);
  10858. out.write_shift(2, (~L) & 0xFFFF);
  10859. while(L-- > 0) out[out.l++] = data[boff++];
  10860. }
  10861. return out.l;
  10862. };
  10863. })();
  10864. return function(data) {
  10865. var buf = new_buf(50+Math.floor(data.length*1.1));
  10866. var off = _deflateRaw(data, buf);
  10867. return buf.slice(0, off);
  10868. };
  10869. })();
  10870. /* modified inflate function also moves original read head */
  10871. /* build tree (used for literals and lengths) */
  10872. function build_tree(clens, cmap, MAX) {
  10873. var maxlen = 1, w = 0, i = 0, j = 0, ccode = 0, L = clens.length;
  10874. var bl_count = use_typed_arrays ? new Uint16Array(32) : zero_fill_array(32);
  10875. for(i = 0; i < 32; ++i) bl_count[i] = 0;
  10876. for(i = L; i < MAX; ++i) clens[i] = 0;
  10877. L = clens.length;
  10878. var ctree = use_typed_arrays ? new Uint16Array(L) : zero_fill_array(L); // []
  10879. /* build code tree */
  10880. for(i = 0; i < L; ++i) {
  10881. bl_count[(w = clens[i])]++;
  10882. if(maxlen < w) maxlen = w;
  10883. ctree[i] = 0;
  10884. }
  10885. bl_count[0] = 0;
  10886. for(i = 1; i <= maxlen; ++i) bl_count[i+16] = (ccode = (ccode + bl_count[i-1])<<1);
  10887. for(i = 0; i < L; ++i) {
  10888. ccode = clens[i];
  10889. if(ccode != 0) ctree[i] = bl_count[ccode+16]++;
  10890. }
  10891. /* cmap[maxlen + 4 bits] = (off&15) + (lit<<4) reverse mapping */
  10892. var cleni = 0;
  10893. for(i = 0; i < L; ++i) {
  10894. cleni = clens[i];
  10895. if(cleni != 0) {
  10896. ccode = bit_swap_n(ctree[i], maxlen)>>(maxlen-cleni);
  10897. for(j = (1<<(maxlen + 4 - cleni)) - 1; j>=0; --j)
  10898. cmap[ccode|(j<<cleni)] = (cleni&15) | (i<<4);
  10899. }
  10900. }
  10901. return maxlen;
  10902. }
  10903. var fix_lmap = use_typed_arrays ? new Uint16Array(512) : zero_fill_array(512);
  10904. var fix_dmap = use_typed_arrays ? new Uint16Array(32) : zero_fill_array(32);
  10905. if(!use_typed_arrays) {
  10906. for(var i = 0; i < 512; ++i) fix_lmap[i] = 0;
  10907. for(i = 0; i < 32; ++i) fix_dmap[i] = 0;
  10908. }
  10909. (function() {
  10910. var dlens = [];
  10911. var i = 0;
  10912. for(;i<32; i++) dlens.push(5);
  10913. build_tree(dlens, fix_dmap, 32);
  10914. var clens = [];
  10915. i = 0;
  10916. for(; i<=143; i++) clens.push(8);
  10917. for(; i<=255; i++) clens.push(9);
  10918. for(; i<=279; i++) clens.push(7);
  10919. for(; i<=287; i++) clens.push(8);
  10920. build_tree(clens, fix_lmap, 288);
  10921. })();
  10922. var dyn_lmap = use_typed_arrays ? new Uint16Array(32768) : zero_fill_array(32768);
  10923. var dyn_dmap = use_typed_arrays ? new Uint16Array(32768) : zero_fill_array(32768);
  10924. var dyn_cmap = use_typed_arrays ? new Uint16Array(128) : zero_fill_array(128);
  10925. var dyn_len_1 = 1, dyn_len_2 = 1;
  10926. /* 5.5.3 Expanding Huffman Codes */
  10927. function dyn(data, boff) {
  10928. /* nomenclature from RFC1951 refers to bit values; these are offset by the implicit constant */
  10929. var _HLIT = read_bits_5(data, boff) + 257; boff += 5;
  10930. var _HDIST = read_bits_5(data, boff) + 1; boff += 5;
  10931. var _HCLEN = read_bits_4(data, boff) + 4; boff += 4;
  10932. var w = 0;
  10933. /* grab and store code lengths */
  10934. var clens = use_typed_arrays ? new Uint8Array(19) : zero_fill_array(19);
  10935. var ctree = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
  10936. var maxlen = 1;
  10937. var bl_count = use_typed_arrays ? new Uint8Array(8) : zero_fill_array(8);
  10938. var next_code = use_typed_arrays ? new Uint8Array(8) : zero_fill_array(8);
  10939. var L = clens.length; /* 19 */
  10940. for(var i = 0; i < _HCLEN; ++i) {
  10941. clens[CLEN_ORDER[i]] = w = read_bits_3(data, boff);
  10942. if(maxlen < w) maxlen = w;
  10943. bl_count[w]++;
  10944. boff += 3;
  10945. }
  10946. /* build code tree */
  10947. var ccode = 0;
  10948. bl_count[0] = 0;
  10949. for(i = 1; i <= maxlen; ++i) next_code[i] = ccode = (ccode + bl_count[i-1])<<1;
  10950. for(i = 0; i < L; ++i) if((ccode = clens[i]) != 0) ctree[i] = next_code[ccode]++;
  10951. /* cmap[7 bits from stream] = (off&7) + (lit<<3) */
  10952. var cleni = 0;
  10953. for(i = 0; i < L; ++i) {
  10954. cleni = clens[i];
  10955. if(cleni != 0) {
  10956. ccode = bitswap8[ctree[i]]>>(8-cleni);
  10957. for(var j = (1<<(7-cleni))-1; j>=0; --j) dyn_cmap[ccode|(j<<cleni)] = (cleni&7) | (i<<3);
  10958. }
  10959. }
  10960. /* read literal and dist codes at once */
  10961. var hcodes = [];
  10962. maxlen = 1;
  10963. for(; hcodes.length < _HLIT + _HDIST;) {
  10964. ccode = dyn_cmap[read_bits_7(data, boff)];
  10965. boff += ccode & 7;
  10966. switch((ccode >>>= 3)) {
  10967. case 16:
  10968. w = 3 + read_bits_2(data, boff); boff += 2;
  10969. ccode = hcodes[hcodes.length - 1];
  10970. while(w-- > 0) hcodes.push(ccode);
  10971. break;
  10972. case 17:
  10973. w = 3 + read_bits_3(data, boff); boff += 3;
  10974. while(w-- > 0) hcodes.push(0);
  10975. break;
  10976. case 18:
  10977. w = 11 + read_bits_7(data, boff); boff += 7;
  10978. while(w -- > 0) hcodes.push(0);
  10979. break;
  10980. default:
  10981. hcodes.push(ccode);
  10982. if(maxlen < ccode) maxlen = ccode;
  10983. break;
  10984. }
  10985. }
  10986. /* build literal / length trees */
  10987. var h1 = hcodes.slice(0, _HLIT), h2 = hcodes.slice(_HLIT);
  10988. for(i = _HLIT; i < 286; ++i) h1[i] = 0;
  10989. for(i = _HDIST; i < 30; ++i) h2[i] = 0;
  10990. dyn_len_1 = build_tree(h1, dyn_lmap, 286);
  10991. dyn_len_2 = build_tree(h2, dyn_dmap, 30);
  10992. return boff;
  10993. }
  10994. /* return [ data, bytesRead ] */
  10995. function inflate(data, usz) {
  10996. /* shortcircuit for empty buffer [0x03, 0x00] */
  10997. if(data[0] == 3 && !(data[1] & 0x3)) { return [new_raw_buf(usz), 2]; }
  10998. /* bit offset */
  10999. var boff = 0;
  11000. /* header includes final bit and type bits */
  11001. var header = 0;
  11002. var outbuf = new_unsafe_buf(usz ? usz : (1<<18));
  11003. var woff = 0;
  11004. var OL = outbuf.length>>>0;
  11005. var max_len_1 = 0, max_len_2 = 0;
  11006. while((header&1) == 0) {
  11007. header = read_bits_3(data, boff); boff += 3;
  11008. if((header >>> 1) == 0) {
  11009. /* Stored block */
  11010. if(boff & 7) boff += 8 - (boff&7);
  11011. /* 2 bytes sz, 2 bytes bit inverse */
  11012. var sz = data[boff>>>3] | data[(boff>>>3)+1]<<8;
  11013. boff += 32;
  11014. /* push sz bytes */
  11015. if(!usz && OL < woff + sz) { outbuf = realloc(outbuf, woff + sz); OL = outbuf.length; }
  11016. if(typeof data.copy === 'function') {
  11017. // $FlowIgnore
  11018. data.copy(outbuf, woff, boff>>>3, (boff>>>3)+sz);
  11019. woff += sz; boff += 8*sz;
  11020. } else while(sz-- > 0) { outbuf[woff++] = data[boff>>>3]; boff += 8; }
  11021. continue;
  11022. } else if((header >>> 1) == 1) {
  11023. /* Fixed Huffman */
  11024. max_len_1 = 9; max_len_2 = 5;
  11025. } else {
  11026. /* Dynamic Huffman */
  11027. boff = dyn(data, boff);
  11028. max_len_1 = dyn_len_1; max_len_2 = dyn_len_2;
  11029. }
  11030. if(!usz && (OL < woff + 32767)) { outbuf = realloc(outbuf, woff + 32767); OL = outbuf.length; }
  11031. for(;;) { // while(true) is apparently out of vogue in modern JS circles
  11032. /* ingest code and move read head */
  11033. var bits = read_bits_n(data, boff, max_len_1);
  11034. var code = (header>>>1) == 1 ? fix_lmap[bits] : dyn_lmap[bits];
  11035. boff += code & 15; code >>>= 4;
  11036. /* 0-255 are literals, 256 is end of block token, 257+ are copy tokens */
  11037. if(((code>>>8)&0xFF) === 0) outbuf[woff++] = code;
  11038. else if(code == 256) break;
  11039. else {
  11040. code -= 257;
  11041. var len_eb = (code < 8) ? 0 : ((code-4)>>2); if(len_eb > 5) len_eb = 0;
  11042. var tgt = woff + LEN_LN[code];
  11043. /* length extra bits */
  11044. if(len_eb > 0) {
  11045. tgt += read_bits_n(data, boff, len_eb);
  11046. boff += len_eb;
  11047. }
  11048. /* dist code */
  11049. bits = read_bits_n(data, boff, max_len_2);
  11050. code = (header>>>1) == 1 ? fix_dmap[bits] : dyn_dmap[bits];
  11051. boff += code & 15; code >>>= 4;
  11052. var dst_eb = (code < 4 ? 0 : (code-2)>>1);
  11053. var dst = DST_LN[code];
  11054. /* dist extra bits */
  11055. if(dst_eb > 0) {
  11056. dst += read_bits_n(data, boff, dst_eb);
  11057. boff += dst_eb;
  11058. }
  11059. /* in the common case, manual byte copy is faster than TA set / Buffer copy */
  11060. if(!usz && OL < tgt) { outbuf = realloc(outbuf, tgt); OL = outbuf.length; }
  11061. while(woff < tgt) { outbuf[woff] = outbuf[woff - dst]; ++woff; }
  11062. }
  11063. }
  11064. }
  11065. return [usz ? outbuf : outbuf.slice(0, woff), (boff+7)>>>3];
  11066. }
  11067. function _inflate(payload, usz) {
  11068. var data = payload.slice(payload.l||0);
  11069. var out = inflate(data, usz);
  11070. payload.l += out[1];
  11071. return out[0];
  11072. }
  11073. function warn_or_throw(wrn, msg) {
  11074. if(wrn) { if(typeof console !== 'undefined') console.error(msg); }
  11075. else throw new Error(msg);
  11076. }
  11077. function parse_zip(file, options) {
  11078. var blob = file;
  11079. prep_blob(blob, 0);
  11080. var FileIndex = [], FullPaths = [];
  11081. var o = {
  11082. FileIndex: FileIndex,
  11083. FullPaths: FullPaths
  11084. };
  11085. init_cfb(o, { root: options.root });
  11086. /* find end of central directory, start just after signature */
  11087. var i = blob.length - 4;
  11088. while((blob[i] != 0x50 || blob[i+1] != 0x4b || blob[i+2] != 0x05 || blob[i+3] != 0x06) && i >= 0) --i;
  11089. blob.l = i + 4;
  11090. /* parse end of central directory */
  11091. blob.l += 4;
  11092. var fcnt = blob.read_shift(2);
  11093. blob.l += 6;
  11094. var start_cd = blob.read_shift(4);
  11095. /* parse central directory */
  11096. blob.l = start_cd;
  11097. for(i = 0; i < fcnt; ++i) {
  11098. /* trust local file header instead of CD entry */
  11099. blob.l += 20;
  11100. var csz = blob.read_shift(4);
  11101. var usz = blob.read_shift(4);
  11102. var namelen = blob.read_shift(2);
  11103. var efsz = blob.read_shift(2);
  11104. var fcsz = blob.read_shift(2);
  11105. blob.l += 8;
  11106. var offset = blob.read_shift(4);
  11107. var EF = parse_extra_field(blob.slice(blob.l+namelen, blob.l+namelen+efsz));
  11108. blob.l += namelen + efsz + fcsz;
  11109. var L = blob.l;
  11110. blob.l = offset + 4;
  11111. parse_local_file(blob, csz, usz, o, EF);
  11112. blob.l = L;
  11113. }
  11114. return o;
  11115. }
  11116. /* head starts just after local file header signature */
  11117. function parse_local_file(blob, csz, usz, o, EF) {
  11118. /* [local file header] */
  11119. blob.l += 2;
  11120. var flags = blob.read_shift(2);
  11121. var meth = blob.read_shift(2);
  11122. var date = parse_dos_date(blob);
  11123. if(flags & 0x2041) throw new Error("Unsupported ZIP encryption");
  11124. var crc32 = blob.read_shift(4);
  11125. var _csz = blob.read_shift(4);
  11126. var _usz = blob.read_shift(4);
  11127. var namelen = blob.read_shift(2);
  11128. var efsz = blob.read_shift(2);
  11129. // TODO: flags & (1<<11) // UTF8
  11130. var name = ""; for(var i = 0; i < namelen; ++i) name += String.fromCharCode(blob[blob.l++]);
  11131. if(efsz) {
  11132. var ef = parse_extra_field(blob.slice(blob.l, blob.l + efsz));
  11133. if((ef[0x5455]||{}).mt) date = ef[0x5455].mt;
  11134. if(((EF||{})[0x5455]||{}).mt) date = EF[0x5455].mt;
  11135. }
  11136. blob.l += efsz;
  11137. /* [encryption header] */
  11138. /* [file data] */
  11139. var data = blob.slice(blob.l, blob.l + _csz);
  11140. switch(meth) {
  11141. case 8: data = _inflateRawSync(blob, _usz); break;
  11142. case 0: break;
  11143. default: throw new Error("Unsupported ZIP Compression method " + meth);
  11144. }
  11145. /* [data descriptor] */
  11146. var wrn = false;
  11147. if(flags & 8) {
  11148. crc32 = blob.read_shift(4);
  11149. if(crc32 == 0x08074b50) { crc32 = blob.read_shift(4); wrn = true; }
  11150. _csz = blob.read_shift(4);
  11151. _usz = blob.read_shift(4);
  11152. }
  11153. if(_csz != csz) warn_or_throw(wrn, "Bad compressed size: " + csz + " != " + _csz);
  11154. if(_usz != usz) warn_or_throw(wrn, "Bad uncompressed size: " + usz + " != " + _usz);
  11155. var _crc32 = CRC32.buf(data, 0);
  11156. if(crc32 != _crc32) warn_or_throw(wrn, "Bad CRC32 checksum: " + crc32 + " != " + _crc32);
  11157. cfb_add(o, name, data, {unsafe: true, mt: date});
  11158. }
  11159. function write_zip(cfb, options) {
  11160. var _opts = options || {};
  11161. var out = [], cdirs = [];
  11162. var o = new_buf(1);
  11163. var method = (_opts.compression ? 8 : 0), flags = 0;
  11164. var desc = false;
  11165. if(desc) flags |= 8;
  11166. var i = 0, j = 0;
  11167. var start_cd = 0, fcnt = 0;
  11168. var root = cfb.FullPaths[0], fp = root, fi = cfb.FileIndex[0];
  11169. var crcs = [];
  11170. var sz_cd = 0;
  11171. for(i = 1; i < cfb.FullPaths.length; ++i) {
  11172. fp = cfb.FullPaths[i].slice(root.length); fi = cfb.FileIndex[i];
  11173. if(!fi.size || !fi.content || fp == "\u0001Sh33tJ5") continue;
  11174. var start = start_cd;
  11175. /* TODO: CP437 filename */
  11176. var namebuf = new_buf(fp.length);
  11177. for(j = 0; j < fp.length; ++j) namebuf.write_shift(1, fp.charCodeAt(j) & 0x7F);
  11178. namebuf = namebuf.slice(0, namebuf.l);
  11179. crcs[fcnt] = CRC32.buf(fi.content, 0);
  11180. var outbuf = fi.content;
  11181. if(method == 8) outbuf = _deflateRawSync(outbuf);
  11182. /* local file header */
  11183. o = new_buf(30);
  11184. o.write_shift(4, 0x04034b50);
  11185. o.write_shift(2, 20);
  11186. o.write_shift(2, flags);
  11187. o.write_shift(2, method);
  11188. /* TODO: last mod file time/date */
  11189. if(fi.mt) write_dos_date(o, fi.mt);
  11190. else o.write_shift(4, 0);
  11191. o.write_shift(-4, (flags & 8) ? 0 : crcs[fcnt]);
  11192. o.write_shift(4, (flags & 8) ? 0 : outbuf.length);
  11193. o.write_shift(4, (flags & 8) ? 0 : fi.content.length);
  11194. o.write_shift(2, namebuf.length);
  11195. o.write_shift(2, 0);
  11196. start_cd += o.length;
  11197. out.push(o);
  11198. start_cd += namebuf.length;
  11199. out.push(namebuf);
  11200. /* TODO: encryption header ? */
  11201. start_cd += outbuf.length;
  11202. out.push(outbuf);
  11203. /* data descriptor */
  11204. if(flags & 8) {
  11205. o = new_buf(12);
  11206. o.write_shift(-4, crcs[fcnt]);
  11207. o.write_shift(4, outbuf.length);
  11208. o.write_shift(4, fi.content.length);
  11209. start_cd += o.l;
  11210. out.push(o);
  11211. }
  11212. /* central directory */
  11213. o = new_buf(46);
  11214. o.write_shift(4, 0x02014b50);
  11215. o.write_shift(2, 0);
  11216. o.write_shift(2, 20);
  11217. o.write_shift(2, flags);
  11218. o.write_shift(2, method);
  11219. o.write_shift(4, 0); /* TODO: last mod file time/date */
  11220. o.write_shift(-4, crcs[fcnt]);
  11221. o.write_shift(4, outbuf.length);
  11222. o.write_shift(4, fi.content.length);
  11223. o.write_shift(2, namebuf.length);
  11224. o.write_shift(2, 0);
  11225. o.write_shift(2, 0);
  11226. o.write_shift(2, 0);
  11227. o.write_shift(2, 0);
  11228. o.write_shift(4, 0);
  11229. o.write_shift(4, start);
  11230. sz_cd += o.l;
  11231. cdirs.push(o);
  11232. sz_cd += namebuf.length;
  11233. cdirs.push(namebuf);
  11234. ++fcnt;
  11235. }
  11236. /* end of central directory */
  11237. o = new_buf(22);
  11238. o.write_shift(4, 0x06054b50);
  11239. o.write_shift(2, 0);
  11240. o.write_shift(2, 0);
  11241. o.write_shift(2, fcnt);
  11242. o.write_shift(2, fcnt);
  11243. o.write_shift(4, sz_cd);
  11244. o.write_shift(4, start_cd);
  11245. o.write_shift(2, 0);
  11246. return bconcat(([bconcat((out)), bconcat(cdirs), o]));
  11247. }
  11248. function cfb_new(opts) {
  11249. var o = ({});
  11250. init_cfb(o, opts);
  11251. return o;
  11252. }
  11253. function cfb_add(cfb, name, content, opts) {
  11254. var unsafe = opts && opts.unsafe;
  11255. if(!unsafe) init_cfb(cfb);
  11256. var file = !unsafe && CFB.find(cfb, name);
  11257. if(!file) {
  11258. var fpath = cfb.FullPaths[0];
  11259. if(name.slice(0, fpath.length) == fpath) fpath = name;
  11260. else {
  11261. if(fpath.slice(-1) != "/") fpath += "/";
  11262. fpath = (fpath + name).replace("//","/");
  11263. }
  11264. file = ({name: filename(name), type: 2});
  11265. cfb.FileIndex.push(file);
  11266. cfb.FullPaths.push(fpath);
  11267. if(!unsafe) CFB.utils.cfb_gc(cfb);
  11268. }
  11269. file.content = (content);
  11270. file.size = content ? content.length : 0;
  11271. if(opts) {
  11272. if(opts.CLSID) file.clsid = opts.CLSID;
  11273. if(opts.mt) file.mt = opts.mt;
  11274. if(opts.ct) file.ct = opts.ct;
  11275. }
  11276. return file;
  11277. }
  11278. function cfb_del(cfb, name) {
  11279. init_cfb(cfb);
  11280. var file = CFB.find(cfb, name);
  11281. if(file) for(var j = 0; j < cfb.FileIndex.length; ++j) if(cfb.FileIndex[j] == file) {
  11282. cfb.FileIndex.splice(j, 1);
  11283. cfb.FullPaths.splice(j, 1);
  11284. return true;
  11285. }
  11286. return false;
  11287. }
  11288. function cfb_mov(cfb, old_name, new_name) {
  11289. init_cfb(cfb);
  11290. var file = CFB.find(cfb, old_name);
  11291. if(file) for(var j = 0; j < cfb.FileIndex.length; ++j) if(cfb.FileIndex[j] == file) {
  11292. cfb.FileIndex[j].name = filename(new_name);
  11293. cfb.FullPaths[j] = new_name;
  11294. return true;
  11295. }
  11296. return false;
  11297. }
  11298. function cfb_gc(cfb) { rebuild_cfb(cfb, true); }
  11299. exports.find = find;
  11300. exports.read = read;
  11301. exports.parse = parse;
  11302. exports.write = write;
  11303. exports.writeFile = write_file;
  11304. exports.utils = {
  11305. cfb_new: cfb_new,
  11306. cfb_add: cfb_add,
  11307. cfb_del: cfb_del,
  11308. cfb_mov: cfb_mov,
  11309. cfb_gc: cfb_gc,
  11310. ReadShift: ReadShift,
  11311. CheckField: CheckField,
  11312. prep_blob: prep_blob,
  11313. bconcat: bconcat,
  11314. use_zlib: use_zlib,
  11315. _deflateRaw: _deflate,
  11316. _inflateRaw: _inflate,
  11317. consts: consts
  11318. };
  11319. return exports;
  11320. })();
  11321. if(typeof require !== 'undefined' && typeof module !== 'undefined' && typeof DO_NOT_EXPORT_CFB === 'undefined') { module.exports = CFB; }
  11322. var _fs;
  11323. if(typeof require !== 'undefined') try { _fs = require('fs'); } catch(e) {}
  11324. /* normalize data for blob ctor */
  11325. function blobify(data) {
  11326. if(typeof data === "string") return s2ab(data);
  11327. if(Array.isArray(data)) return a2u(data);
  11328. return data;
  11329. }
  11330. /* write or download file */
  11331. function write_dl(fname, payload, enc) {
  11332. /*global IE_SaveFile, Blob, navigator, saveAs, URL, document, File, chrome */
  11333. if(typeof _fs !== 'undefined' && _fs.writeFileSync) return enc ? _fs.writeFileSync(fname, payload, enc) : _fs.writeFileSync(fname, payload);
  11334. var data = (enc == "utf8") ? utf8write(payload) : payload;
  11335. if(typeof IE_SaveFile !== 'undefined') return IE_SaveFile(data, fname);
  11336. if(typeof Blob !== 'undefined') {
  11337. var blob = new Blob([blobify(data)], {type:"application/octet-stream"});
  11338. if(typeof navigator !== 'undefined' && navigator.msSaveBlob) return navigator.msSaveBlob(blob, fname);
  11339. if(typeof saveAs !== 'undefined') return saveAs(blob, fname);
  11340. if(typeof URL !== 'undefined' && typeof document !== 'undefined' && document.createElement && URL.createObjectURL) {
  11341. var url = URL.createObjectURL(blob);
  11342. if(typeof chrome === 'object' && typeof (chrome.downloads||{}).download == "function") {
  11343. if(URL.revokeObjectURL && typeof setTimeout !== 'undefined') setTimeout(function() { URL.revokeObjectURL(url); }, 60000);
  11344. return chrome.downloads.download({ url: url, filename: fname, saveAs: true});
  11345. }
  11346. var a = document.createElement("a");
  11347. if(a.download != null) {
  11348. a.download = fname; a.href = url; document.body.appendChild(a); a.click();
  11349. document.body.removeChild(a);
  11350. if(URL.revokeObjectURL && typeof setTimeout !== 'undefined') setTimeout(function() { URL.revokeObjectURL(url); }, 60000);
  11351. return url;
  11352. }
  11353. }
  11354. }
  11355. // $FlowIgnore
  11356. if(typeof $ !== 'undefined' && typeof File !== 'undefined' && typeof Folder !== 'undefined') try { // extendscript
  11357. // $FlowIgnore
  11358. var out = File(fname); out.open("w"); out.encoding = "binary";
  11359. if(Array.isArray(payload)) payload = a2s(payload);
  11360. out.write(payload); out.close(); return payload;
  11361. } catch(e) { if(!e.message || !e.message.match(/onstruct/)) throw e; }
  11362. throw new Error("cannot save file " + fname);
  11363. }
  11364. /* read binary data from file */
  11365. function read_binary(path) {
  11366. if(typeof _fs !== 'undefined') return _fs.readFileSync(path);
  11367. // $FlowIgnore
  11368. if(typeof $ !== 'undefined' && typeof File !== 'undefined' && typeof Folder !== 'undefined') try { // extendscript
  11369. // $FlowIgnore
  11370. var infile = File(path); infile.open("r"); infile.encoding = "binary";
  11371. var data = infile.read(); infile.close();
  11372. return data;
  11373. } catch(e) { if(!e.message || !e.message.match(/onstruct/)) throw e; }
  11374. throw new Error("Cannot access file " + path);
  11375. }
  11376. function keys(o) {
  11377. var ks = Object.keys(o), o2 = [];
  11378. for(var i = 0; i < ks.length; ++i) if(o.hasOwnProperty(ks[i])) o2.push(ks[i]);
  11379. return o2;
  11380. }
  11381. function evert_key(obj, key) {
  11382. var o = ([]), K = keys(obj);
  11383. for(var i = 0; i !== K.length; ++i) if(o[obj[K[i]][key]] == null) o[obj[K[i]][key]] = K[i];
  11384. return o;
  11385. }
  11386. function evert(obj) {
  11387. var o = ([]), K = keys(obj);
  11388. for(var i = 0; i !== K.length; ++i) o[obj[K[i]]] = K[i];
  11389. return o;
  11390. }
  11391. function evert_num(obj) {
  11392. var o = ([]), K = keys(obj);
  11393. for(var i = 0; i !== K.length; ++i) o[obj[K[i]]] = parseInt(K[i],10);
  11394. return o;
  11395. }
  11396. function evert_arr(obj) {
  11397. var o = ([]), K = keys(obj);
  11398. for(var i = 0; i !== K.length; ++i) {
  11399. if(o[obj[K[i]]] == null) o[obj[K[i]]] = [];
  11400. o[obj[K[i]]].push(K[i]);
  11401. }
  11402. return o;
  11403. }
  11404. var basedate = new Date(1899, 11, 30, 0, 0, 0); // 2209161600000
  11405. var dnthresh = basedate.getTime() + (new Date().getTimezoneOffset() - basedate.getTimezoneOffset()) * 60000;
  11406. function datenum(v, date1904) {
  11407. var epoch = v.getTime();
  11408. if(date1904) epoch -= 1462*24*60*60*1000;
  11409. return (epoch - dnthresh) / (24 * 60 * 60 * 1000);
  11410. }
  11411. function numdate(v) {
  11412. var out = new Date();
  11413. out.setTime(v * 24 * 60 * 60 * 1000 + dnthresh);
  11414. return out;
  11415. }
  11416. /* ISO 8601 Duration */
  11417. function parse_isodur(s) {
  11418. var sec = 0, mt = 0, time = false;
  11419. var m = s.match(/P([0-9\.]+Y)?([0-9\.]+M)?([0-9\.]+D)?T([0-9\.]+H)?([0-9\.]+M)?([0-9\.]+S)?/);
  11420. if(!m) throw new Error("|" + s + "| is not an ISO8601 Duration");
  11421. for(var i = 1; i != m.length; ++i) {
  11422. if(!m[i]) continue;
  11423. mt = 1;
  11424. if(i > 3) time = true;
  11425. switch(m[i].slice(m[i].length-1)) {
  11426. case 'Y':
  11427. throw new Error("Unsupported ISO Duration Field: " + m[i].slice(m[i].length-1));
  11428. case 'D': mt *= 24;
  11429. /* falls through */
  11430. case 'H': mt *= 60;
  11431. /* falls through */
  11432. case 'M':
  11433. if(!time) throw new Error("Unsupported ISO Duration Field: M");
  11434. else mt *= 60;
  11435. /* falls through */
  11436. case 'S': break;
  11437. }
  11438. sec += mt * parseInt(m[i], 10);
  11439. }
  11440. return sec;
  11441. }
  11442. var good_pd_date = new Date('2017-02-19T19:06:09.000Z');
  11443. if(isNaN(good_pd_date.getFullYear())) good_pd_date = new Date('2/19/17');
  11444. var good_pd = good_pd_date.getFullYear() == 2017;
  11445. /* parses a date as a local date */
  11446. function parseDate(str, fixdate) {
  11447. var d = new Date(str);
  11448. if(good_pd) {
  11449. if(fixdate > 0) d.setTime(d.getTime() + d.getTimezoneOffset() * 60 * 1000);
  11450. else if(fixdate < 0) d.setTime(d.getTime() - d.getTimezoneOffset() * 60 * 1000);
  11451. return d;
  11452. }
  11453. if(str instanceof Date) return str;
  11454. if(good_pd_date.getFullYear() == 1917 && !isNaN(d.getFullYear())) {
  11455. var s = d.getFullYear();
  11456. if(str.indexOf("" + s) > -1) return d;
  11457. d.setFullYear(d.getFullYear() + 100); return d;
  11458. }
  11459. var n = str.match(/\d+/g)||["2017","2","19","0","0","0"];
  11460. var out = new Date(+n[0], +n[1] - 1, +n[2], (+n[3]||0), (+n[4]||0), (+n[5]||0));
  11461. if(str.indexOf("Z") > -1) out = new Date(out.getTime() - out.getTimezoneOffset() * 60 * 1000);
  11462. return out;
  11463. }
  11464. function cc2str(arr) {
  11465. var o = "";
  11466. for(var i = 0; i != arr.length; ++i) o += String.fromCharCode(arr[i]);
  11467. return o;
  11468. }
  11469. function dup(o) {
  11470. if(typeof JSON != 'undefined' && !Array.isArray(o)) return JSON.parse(JSON.stringify(o));
  11471. if(typeof o != 'object' || o == null) return o;
  11472. if(o instanceof Date) return new Date(o.getTime());
  11473. var out = {};
  11474. for(var k in o) if(o.hasOwnProperty(k)) out[k] = dup(o[k]);
  11475. return out;
  11476. }
  11477. function fill(c,l) { var o = ""; while(o.length < l) o+=c; return o; }
  11478. /* TODO: stress test */
  11479. function fuzzynum(s) {
  11480. var v = Number(s);
  11481. if(!isNaN(v)) return v;
  11482. var wt = 1;
  11483. var ss = s.replace(/([\d]),([\d])/g,"$1$2").replace(/[$]/g,"").replace(/[%]/g, function() { wt *= 100; return "";});
  11484. if(!isNaN(v = Number(ss))) return v / wt;
  11485. ss = ss.replace(/[(](.*)[)]/,function($$, $1) { wt = -wt; return $1;});
  11486. if(!isNaN(v = Number(ss))) return v / wt;
  11487. return v;
  11488. }
  11489. function fuzzydate(s) {
  11490. var o = new Date(s), n = new Date(NaN);
  11491. var y = o.getYear(), m = o.getMonth(), d = o.getDate();
  11492. if(isNaN(d)) return n;
  11493. if(y < 0 || y > 8099) return n;
  11494. if((m > 0 || d > 1) && y != 101) return o;
  11495. if(s.toLowerCase().match(/jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec/)) return o;
  11496. if(s.match(/[^-0-9:,\/\\]/)) return n;
  11497. return o;
  11498. }
  11499. var safe_split_regex = "abacaba".split(/(:?b)/i).length == 5;
  11500. function split_regex(str, re, def) {
  11501. if(safe_split_regex || typeof re == "string") return str.split(re);
  11502. var p = str.split(re), o = [p[0]];
  11503. for(var i = 1; i < p.length; ++i) { o.push(def); o.push(p[i]); }
  11504. return o;
  11505. }
  11506. function getdatastr(data) {
  11507. if(!data) return null;
  11508. if(data.data) return debom(data.data);
  11509. if(data.asNodeBuffer && has_buf) return debom(data.asNodeBuffer().toString('binary'));
  11510. if(data.asBinary) return debom(data.asBinary());
  11511. if(data._data && data._data.getContent) return debom(cc2str(Array.prototype.slice.call(data._data.getContent(),0)));
  11512. return null;
  11513. }
  11514. function getdatabin(data) {
  11515. if(!data) return null;
  11516. if(data.data) return char_codes(data.data);
  11517. if(data.asNodeBuffer && has_buf) return data.asNodeBuffer();
  11518. if(data._data && data._data.getContent) {
  11519. var o = data._data.getContent();
  11520. if(typeof o == "string") return char_codes(o);
  11521. return Array.prototype.slice.call(o);
  11522. }
  11523. return null;
  11524. }
  11525. function getdata(data) { return (data && data.name.slice(-4) === ".bin") ? getdatabin(data) : getdatastr(data); }
  11526. /* Part 2 Section 10.1.2 "Mapping Content Types" Names are case-insensitive */
  11527. /* OASIS does not comment on filename case sensitivity */
  11528. function safegetzipfile(zip, file) {
  11529. var k = keys(zip.files);
  11530. var f = file.toLowerCase(), g = f.replace(/\//g,'\\');
  11531. for(var i=0; i<k.length; ++i) {
  11532. var n = k[i].toLowerCase();
  11533. if(f == n || g == n) return zip.files[k[i]];
  11534. }
  11535. return null;
  11536. }
  11537. function getzipfile(zip, file) {
  11538. var o = safegetzipfile(zip, file);
  11539. if(o == null) throw new Error("Cannot find file " + file + " in zip");
  11540. return o;
  11541. }
  11542. function getzipdata(zip, file, safe) {
  11543. if(!safe) return getdata(getzipfile(zip, file));
  11544. if(!file) return null;
  11545. try { return getzipdata(zip, file); } catch(e) { return null; }
  11546. }
  11547. function getzipstr(zip, file, safe) {
  11548. if(!safe) return getdatastr(getzipfile(zip, file));
  11549. if(!file) return null;
  11550. try { return getzipstr(zip, file); } catch(e) { return null; }
  11551. }
  11552. function zipentries(zip) {
  11553. var k = keys(zip.files), o = [];
  11554. for(var i = 0; i < k.length; ++i) if(k[i].slice(-1) != '/') o.push(k[i]);
  11555. return o.sort();
  11556. }
  11557. var jszip;
  11558. /*global JSZipSync:true */
  11559. if(typeof JSZipSync !== 'undefined') jszip = JSZipSync;
  11560. if(typeof exports !== 'undefined') {
  11561. if(typeof module !== 'undefined' && module.exports) {
  11562. if(typeof jszip === 'undefined') jszip = undefined;
  11563. }
  11564. }
  11565. function resolve_path(path, base) {
  11566. var result = base.split('/');
  11567. if(base.slice(-1) != "/") result.pop(); // folder path
  11568. var target = path.split('/');
  11569. while (target.length !== 0) {
  11570. var step = target.shift();
  11571. if (step === '..') result.pop();
  11572. else if (step !== '.') result.push(step);
  11573. }
  11574. return result.join('/');
  11575. }
  11576. var XML_HEADER = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n';
  11577. var attregexg=/([^"\s?>\/]+)\s*=\s*((?:")([^"]*)(?:")|(?:')([^']*)(?:')|([^'">\s]+))/g;
  11578. var tagregex=/<[\/\?]?[a-zA-Z0-9:]+(?:\s+[^"\s?>\/]+\s*=\s*(?:"[^"]*"|'[^']*'|[^'">\s=]+))*\s?[\/\?]?>/g;
  11579. if(!(XML_HEADER.match(tagregex))) tagregex = /<[^>]*>/g;
  11580. var nsregex=/<\w*:/, nsregex2 = /<(\/?)\w+:/;
  11581. function parsexmltag(tag, skip_root) {
  11582. var z = ({});
  11583. var eq = 0, c = 0;
  11584. for(; eq !== tag.length; ++eq) if((c = tag.charCodeAt(eq)) === 32 || c === 10 || c === 13) break;
  11585. if(!skip_root) z[0] = tag.slice(0, eq);
  11586. if(eq === tag.length) return z;
  11587. var m = tag.match(attregexg), j=0, v="", i=0, q="", cc="", quot = 1;
  11588. if(m) for(i = 0; i != m.length; ++i) {
  11589. cc = m[i];
  11590. for(c=0; c != cc.length; ++c) if(cc.charCodeAt(c) === 61) break;
  11591. q = cc.slice(0,c).trim();
  11592. while(cc.charCodeAt(c+1) == 32) ++c;
  11593. quot = ((eq=cc.charCodeAt(c+1)) == 34 || eq == 39) ? 1 : 0;
  11594. v = cc.slice(c+1+quot, cc.length-quot);
  11595. for(j=0;j!=q.length;++j) if(q.charCodeAt(j) === 58) break;
  11596. if(j===q.length) {
  11597. if(q.indexOf("_") > 0) q = q.slice(0, q.indexOf("_")); // from ods
  11598. z[q] = v;
  11599. z[q.toLowerCase()] = v;
  11600. }
  11601. else {
  11602. var k = (j===5 && q.slice(0,5)==="xmlns"?"xmlns":"")+q.slice(j+1);
  11603. if(z[k] && q.slice(j-3,j) == "ext") continue; // from ods
  11604. z[k] = v;
  11605. z[k.toLowerCase()] = v;
  11606. }
  11607. }
  11608. return z;
  11609. }
  11610. function strip_ns(x) { return x.replace(nsregex2, "<$1"); }
  11611. var encodings = {
  11612. '&quot;': '"',
  11613. '&apos;': "'",
  11614. '&gt;': '>',
  11615. '&lt;': '<',
  11616. '&amp;': '&'
  11617. };
  11618. var rencoding = evert(encodings);
  11619. //var rencstr = "&<>'\"".split("");
  11620. // TODO: CP remap (need to read file version to determine OS)
  11621. var unescapexml = (function() {
  11622. /* 22.4.2.4 bstr (Basic String) */
  11623. var encregex = /&(?:quot|apos|gt|lt|amp|#x?([\da-fA-F]+));/g, coderegex = /_x([\da-fA-F]{4})_/g;
  11624. return function unescapexml(text) {
  11625. var s = text + '', i = s.indexOf("<![CDATA[");
  11626. if(i == -1) return s.replace(encregex, function($$, $1) { return encodings[$$]||String.fromCharCode(parseInt($1,$$.indexOf("x")>-1?16:10))||$$; }).replace(coderegex,function(m,c) {return String.fromCharCode(parseInt(c,16));});
  11627. var j = s.indexOf("]]>");
  11628. return unescapexml(s.slice(0, i)) + s.slice(i+9,j) + unescapexml(s.slice(j+3));
  11629. };
  11630. })();
  11631. var decregex=/[&<>'"]/g, charegex = /[\u0000-\u0008\u000b-\u001f]/g;
  11632. function escapexml(text){
  11633. var s = text + '';
  11634. return s.replace(decregex, function(y) { return rencoding[y]; }).replace(charegex,function(s) { return "_x" + ("000"+s.charCodeAt(0).toString(16)).slice(-4) + "_";});
  11635. }
  11636. function escapexmltag(text){ return escapexml(text).replace(/ /g,"_x0020_"); }
  11637. var htmlcharegex = /[\u0000-\u001f]/g;
  11638. function escapehtml(text){
  11639. var s = text + '';
  11640. return s.replace(decregex, function(y) { return rencoding[y]; }).replace(/\n/g, "<br/>").replace(htmlcharegex,function(s) { return "&#x" + ("000"+s.charCodeAt(0).toString(16)).slice(-4) + ";"; });
  11641. }
  11642. function escapexlml(text){
  11643. var s = text + '';
  11644. return s.replace(decregex, function(y) { return rencoding[y]; }).replace(htmlcharegex,function(s) { return "&#x" + (s.charCodeAt(0).toString(16)).toUpperCase() + ";"; });
  11645. }
  11646. /* TODO: handle codepages */
  11647. var xlml_fixstr = (function() {
  11648. var entregex = /&#(\d+);/g;
  11649. function entrepl($$,$1) { return String.fromCharCode(parseInt($1,10)); }
  11650. return function xlml_fixstr(str) { return str.replace(entregex,entrepl); };
  11651. })();
  11652. var xlml_unfixstr = (function() {
  11653. return function xlml_unfixstr(str) { return str.replace(/(\r\n|[\r\n])/g,"\&#10;"); };
  11654. })();
  11655. function parsexmlbool(value) {
  11656. switch(value) {
  11657. case 1: case true: case '1': case 'true': case 'TRUE': return true;
  11658. /* case '0': case 'false': case 'FALSE':*/
  11659. default: return false;
  11660. }
  11661. }
  11662. var utf8read = function utf8reada(orig) {
  11663. var out = "", i = 0, c = 0, d = 0, e = 0, f = 0, w = 0;
  11664. while (i < orig.length) {
  11665. c = orig.charCodeAt(i++);
  11666. if (c < 128) { out += String.fromCharCode(c); continue; }
  11667. d = orig.charCodeAt(i++);
  11668. if (c>191 && c<224) { f = ((c & 31) << 6); f |= (d & 63); out += String.fromCharCode(f); continue; }
  11669. e = orig.charCodeAt(i++);
  11670. if (c < 240) { out += String.fromCharCode(((c & 15) << 12) | ((d & 63) << 6) | (e & 63)); continue; }
  11671. f = orig.charCodeAt(i++);
  11672. w = (((c & 7) << 18) | ((d & 63) << 12) | ((e & 63) << 6) | (f & 63))-65536;
  11673. out += String.fromCharCode(0xD800 + ((w>>>10)&1023));
  11674. out += String.fromCharCode(0xDC00 + (w&1023));
  11675. }
  11676. return out;
  11677. };
  11678. var utf8write = function(orig) {
  11679. var out = [], i = 0, c = 0, d = 0;
  11680. while(i < orig.length) {
  11681. c = orig.charCodeAt(i++);
  11682. switch(true) {
  11683. case c < 128: out.push(String.fromCharCode(c)); break;
  11684. case c < 2048:
  11685. out.push(String.fromCharCode(192 + (c >> 6)));
  11686. out.push(String.fromCharCode(128 + (c & 63)));
  11687. break;
  11688. case c >= 55296 && c < 57344:
  11689. c -= 55296; d = orig.charCodeAt(i++) - 56320 + (c<<10);
  11690. out.push(String.fromCharCode(240 + ((d >>18) & 7)));
  11691. out.push(String.fromCharCode(144 + ((d >>12) & 63)));
  11692. out.push(String.fromCharCode(128 + ((d >> 6) & 63)));
  11693. out.push(String.fromCharCode(128 + (d & 63)));
  11694. break;
  11695. default:
  11696. out.push(String.fromCharCode(224 + (c >> 12)));
  11697. out.push(String.fromCharCode(128 + ((c >> 6) & 63)));
  11698. out.push(String.fromCharCode(128 + (c & 63)));
  11699. }
  11700. }
  11701. return out.join("");
  11702. };
  11703. if(has_buf) {
  11704. var utf8readb = function utf8readb(data) {
  11705. var out = Buffer.alloc(2*data.length), w, i, j = 1, k = 0, ww=0, c;
  11706. for(i = 0; i < data.length; i+=j) {
  11707. j = 1;
  11708. if((c=data.charCodeAt(i)) < 128) w = c;
  11709. else if(c < 224) { w = (c&31)*64+(data.charCodeAt(i+1)&63); j=2; }
  11710. else if(c < 240) { w=(c&15)*4096+(data.charCodeAt(i+1)&63)*64+(data.charCodeAt(i+2)&63); j=3; }
  11711. else { j = 4;
  11712. w = (c & 7)*262144+(data.charCodeAt(i+1)&63)*4096+(data.charCodeAt(i+2)&63)*64+(data.charCodeAt(i+3)&63);
  11713. w -= 65536; ww = 0xD800 + ((w>>>10)&1023); w = 0xDC00 + (w&1023);
  11714. }
  11715. if(ww !== 0) { out[k++] = ww&255; out[k++] = ww>>>8; ww = 0; }
  11716. out[k++] = w%256; out[k++] = w>>>8;
  11717. }
  11718. return out.slice(0,k).toString('ucs2');
  11719. };
  11720. var corpus = "foo bar baz\u00e2\u0098\u0083\u00f0\u009f\u008d\u00a3";
  11721. if(utf8read(corpus) == utf8readb(corpus)) utf8read = utf8readb;
  11722. // $FlowIgnore
  11723. var utf8readc = function utf8readc(data) { return Buffer_from(data, 'binary').toString('utf8'); };
  11724. if(utf8read(corpus) == utf8readc(corpus)) utf8read = utf8readc;
  11725. // $FlowIgnore
  11726. utf8write = function(data) { return Buffer_from(data, 'utf8').toString("binary"); };
  11727. }
  11728. // matches <foo>...</foo> extracts content
  11729. var matchtag = (function() {
  11730. var mtcache = ({});
  11731. return function matchtag(f,g) {
  11732. var t = f+"|"+(g||"");
  11733. if(mtcache[t]) return mtcache[t];
  11734. return (mtcache[t] = new RegExp('<(?:\\w+:)?'+f+'(?: xml:space="preserve")?(?:[^>]*)>([\\s\\S]*?)</(?:\\w+:)?'+f+'>',((g||""))));
  11735. };
  11736. })();
  11737. var htmldecode = (function() {
  11738. var entities = [
  11739. ['nbsp', ' '], ['middot', '·'],
  11740. ['quot', '"'], ['apos', "'"], ['gt', '>'], ['lt', '<'], ['amp', '&']
  11741. ].map(function(x) { return [new RegExp('&' + x[0] + ';', "g"), x[1]]; });
  11742. return function htmldecode(str) {
  11743. var o = str.replace(/^[\t\n\r ]+/, "").replace(/[\t\n\r ]+$/,"").replace(/[\t\n\r ]+/g, " ").replace(/<\s*[bB][rR]\s*\/?>/g,"\n").replace(/<[^>]*>/g,"");
  11744. for(var i = 0; i < entities.length; ++i) o = o.replace(entities[i][0], entities[i][1]);
  11745. return o;
  11746. };
  11747. })();
  11748. var vtregex = (function(){ var vt_cache = {};
  11749. return function vt_regex(bt) {
  11750. if(vt_cache[bt] !== undefined) return vt_cache[bt];
  11751. return (vt_cache[bt] = new RegExp("<(?:vt:)?" + bt + ">([\\s\\S]*?)</(?:vt:)?" + bt + ">", 'g') );
  11752. };})();
  11753. var vtvregex = /<\/?(?:vt:)?variant>/g, vtmregex = /<(?:vt:)([^>]*)>([\s\S]*)</;
  11754. function parseVector(data, opts) {
  11755. var h = parsexmltag(data);
  11756. var matches = data.match(vtregex(h.baseType))||[];
  11757. var res = [];
  11758. if(matches.length != h.size) {
  11759. if(opts.WTF) throw new Error("unexpected vector length " + matches.length + " != " + h.size);
  11760. return res;
  11761. }
  11762. matches.forEach(function(x) {
  11763. var v = x.replace(vtvregex,"").match(vtmregex);
  11764. if(v) res.push({v:utf8read(v[2]), t:v[1]});
  11765. });
  11766. return res;
  11767. }
  11768. var wtregex = /(^\s|\s$|\n)/;
  11769. function writetag(f,g) { return '<' + f + (g.match(wtregex)?' xml:space="preserve"' : "") + '>' + g + '</' + f + '>'; }
  11770. function wxt_helper(h) { return keys(h).map(function(k) { return " " + k + '="' + h[k] + '"';}).join(""); }
  11771. function writextag(f,g,h) { return '<' + f + ((h != null) ? wxt_helper(h) : "") + ((g != null) ? (g.match(wtregex)?' xml:space="preserve"' : "") + '>' + g + '</' + f : "/") + '>';}
  11772. function write_w3cdtf(d, t) { try { return d.toISOString().replace(/\.\d*/,""); } catch(e) { if(t) throw e; } return ""; }
  11773. function write_vt(s) {
  11774. switch(typeof s) {
  11775. case 'string': return writextag('vt:lpwstr', s);
  11776. case 'number': return writextag((s|0)==s?'vt:i4':'vt:r8', String(s));
  11777. case 'boolean': return writextag('vt:bool',s?'true':'false');
  11778. }
  11779. if(s instanceof Date) return writextag('vt:filetime', write_w3cdtf(s));
  11780. throw new Error("Unable to serialize " + s);
  11781. }
  11782. var XMLNS = ({
  11783. 'dc': 'http://purl.org/dc/elements/1.1/',
  11784. 'dcterms': 'http://purl.org/dc/terms/',
  11785. 'dcmitype': 'http://purl.org/dc/dcmitype/',
  11786. 'mx': 'http://schemas.microsoft.com/office/mac/excel/2008/main',
  11787. 'r': 'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
  11788. 'sjs': 'http://schemas.openxmlformats.org/package/2006/sheetjs/core-properties',
  11789. 'vt': 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes',
  11790. 'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
  11791. 'xsd': 'http://www.w3.org/2001/XMLSchema'
  11792. });
  11793. XMLNS.main = [
  11794. 'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
  11795. 'http://purl.oclc.org/ooxml/spreadsheetml/main',
  11796. 'http://schemas.microsoft.com/office/excel/2006/main',
  11797. 'http://schemas.microsoft.com/office/excel/2006/2'
  11798. ];
  11799. var XLMLNS = ({
  11800. 'o': 'urn:schemas-microsoft-com:office:office',
  11801. 'x': 'urn:schemas-microsoft-com:office:excel',
  11802. 'ss': 'urn:schemas-microsoft-com:office:spreadsheet',
  11803. 'dt': 'uuid:C2F41010-65B3-11d1-A29F-00AA00C14882',
  11804. 'mv': 'http://macVmlSchemaUri',
  11805. 'v': 'urn:schemas-microsoft-com:vml',
  11806. 'html': 'http://www.w3.org/TR/REC-html40'
  11807. });
  11808. function read_double_le(b, idx) {
  11809. var s = 1 - 2 * (b[idx + 7] >>> 7);
  11810. var e = ((b[idx + 7] & 0x7f) << 4) + ((b[idx + 6] >>> 4) & 0x0f);
  11811. var m = (b[idx+6]&0x0f);
  11812. for(var i = 5; i >= 0; --i) m = m * 256 + b[idx + i];
  11813. if(e == 0x7ff) return m == 0 ? (s * Infinity) : NaN;
  11814. if(e == 0) e = -1022;
  11815. else { e -= 1023; m += Math.pow(2,52); }
  11816. return s * Math.pow(2, e - 52) * m;
  11817. }
  11818. function write_double_le(b, v, idx) {
  11819. var bs = ((((v < 0) || (1/v == -Infinity)) ? 1 : 0) << 7), e = 0, m = 0;
  11820. var av = bs ? (-v) : v;
  11821. if(!isFinite(av)) { e = 0x7ff; m = isNaN(v) ? 0x6969 : 0; }
  11822. else if(av == 0) e = m = 0;
  11823. else {
  11824. e = Math.floor(Math.log(av) / Math.LN2);
  11825. m = av * Math.pow(2, 52 - e);
  11826. if((e <= -1023) && (!isFinite(m) || (m < Math.pow(2,52)))) { e = -1022; }
  11827. else { m -= Math.pow(2,52); e+=1023; }
  11828. }
  11829. for(var i = 0; i <= 5; ++i, m/=256) b[idx + i] = m & 0xff;
  11830. b[idx + 6] = ((e & 0x0f) << 4) | (m & 0xf);
  11831. b[idx + 7] = (e >> 4) | bs;
  11832. }
  11833. var __toBuffer = function(bufs) { var x=[],w=10240; for(var i=0;i<bufs[0].length;++i) if(bufs[0][i]) for(var j=0,L=bufs[0][i].length;j<L;j+=w) x.push.apply(x, bufs[0][i].slice(j,j+w)); return x; };
  11834. var ___toBuffer = __toBuffer;
  11835. var __utf16le = function(b,s,e) { var ss=[]; for(var i=s; i<e; i+=2) ss.push(String.fromCharCode(__readUInt16LE(b,i))); return ss.join("").replace(chr0,''); };
  11836. var ___utf16le = __utf16le;
  11837. var __hexlify = function(b,s,l) { var ss=[]; for(var i=s; i<s+l; ++i) ss.push(("0" + b[i].toString(16)).slice(-2)); return ss.join(""); };
  11838. var ___hexlify = __hexlify;
  11839. var __utf8 = function(b,s,e) { var ss=[]; for(var i=s; i<e; i++) ss.push(String.fromCharCode(__readUInt8(b,i))); return ss.join(""); };
  11840. var ___utf8 = __utf8;
  11841. var __lpstr = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? __utf8(b, i+4,i+4+len-1) : "";};
  11842. var ___lpstr = __lpstr;
  11843. var __cpstr = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? __utf8(b, i+4,i+4+len-1) : "";};
  11844. var ___cpstr = __cpstr;
  11845. var __lpwstr = function(b,i) { var len = 2*__readUInt32LE(b,i); return len > 0 ? __utf8(b, i+4,i+4+len-1) : "";};
  11846. var ___lpwstr = __lpwstr;
  11847. var __lpp4, ___lpp4;
  11848. __lpp4 = ___lpp4 = function lpp4_(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? __utf16le(b, i+4,i+4+len) : "";};
  11849. var __8lpp4 = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? __utf8(b, i+4,i+4+len) : "";};
  11850. var ___8lpp4 = __8lpp4;
  11851. var __double, ___double;
  11852. __double = ___double = function(b, idx) { return read_double_le(b, idx);};
  11853. var is_buf = function is_buf_a(a) { return Array.isArray(a); };
  11854. if(has_buf) {
  11855. __utf16le = function(b,s,e) { if(!Buffer.isBuffer(b)) return ___utf16le(b,s,e); return b.toString('utf16le',s,e).replace(chr0,'')/*.replace(chr1,'!')*/; };
  11856. __hexlify = function(b,s,l) { return Buffer.isBuffer(b) ? b.toString('hex',s,s+l) : ___hexlify(b,s,l); };
  11857. __lpstr = function lpstr_b(b, i) { if(!Buffer.isBuffer(b)) return ___lpstr(b, i); var len = b.readUInt32LE(i); return len > 0 ? b.toString('utf8',i+4,i+4+len-1) : "";};
  11858. __cpstr = function cpstr_b(b, i) { if(!Buffer.isBuffer(b)) return ___cpstr(b, i); var len = b.readUInt32LE(i); return len > 0 ? b.toString('utf8',i+4,i+4+len-1) : "";};
  11859. __lpwstr = function lpwstr_b(b, i) { if(!Buffer.isBuffer(b)) return ___lpwstr(b, i); var len = 2*b.readUInt32LE(i); return b.toString('utf16le',i+4,i+4+len-1);};
  11860. __lpp4 = function lpp4_b(b, i) { if(!Buffer.isBuffer(b)) return ___lpp4(b, i); var len = b.readUInt32LE(i); return b.toString('utf16le',i+4,i+4+len);};
  11861. __8lpp4 = function lpp4_8b(b, i) { if(!Buffer.isBuffer(b)) return ___8lpp4(b, i); var len = b.readUInt32LE(i); return b.toString('utf8',i+4,i+4+len);};
  11862. __utf8 = function utf8_b(b, s, e) { return (Buffer.isBuffer(b)) ? b.toString('utf8',s,e) : ___utf8(b,s,e); };
  11863. __toBuffer = function(bufs) { return (bufs[0].length > 0 && Buffer.isBuffer(bufs[0][0])) ? Buffer.concat(bufs[0]) : ___toBuffer(bufs);};
  11864. bconcat = function(bufs) { return Buffer.isBuffer(bufs[0]) ? Buffer.concat(bufs) : [].concat.apply([], bufs); };
  11865. __double = function double_(b, i) { if(Buffer.isBuffer(b)) return b.readDoubleLE(i); return ___double(b,i); };
  11866. is_buf = function is_buf_b(a) { return Buffer.isBuffer(a) || Array.isArray(a); };
  11867. }
  11868. /* from js-xls */
  11869. if(typeof cptable !== 'undefined') {
  11870. __utf16le = function(b,s,e) { return cptable.utils.decode(1200, b.slice(s,e)).replace(chr0, ''); };
  11871. __utf8 = function(b,s,e) { return cptable.utils.decode(65001, b.slice(s,e)); };
  11872. __lpstr = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? cptable.utils.decode(current_ansi, b.slice(i+4, i+4+len-1)) : "";};
  11873. __cpstr = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? cptable.utils.decode(current_codepage, b.slice(i+4, i+4+len-1)) : "";};
  11874. __lpwstr = function(b,i) { var len = 2*__readUInt32LE(b,i); return len > 0 ? cptable.utils.decode(1200, b.slice(i+4,i+4+len-1)) : "";};
  11875. __lpp4 = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? cptable.utils.decode(1200, b.slice(i+4,i+4+len)) : "";};
  11876. __8lpp4 = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? cptable.utils.decode(65001, b.slice(i+4,i+4+len)) : "";};
  11877. }
  11878. var __readUInt8 = function(b, idx) { return b[idx]; };
  11879. var __readUInt16LE = function(b, idx) { return (b[idx+1]*(1<<8))+b[idx]; };
  11880. var __readInt16LE = function(b, idx) { var u = (b[idx+1]*(1<<8))+b[idx]; return (u < 0x8000) ? u : ((0xffff - u + 1) * -1); };
  11881. var __readUInt32LE = function(b, idx) { return b[idx+3]*(1<<24)+(b[idx+2]<<16)+(b[idx+1]<<8)+b[idx]; };
  11882. var __readInt32LE = function(b, idx) { return (b[idx+3]<<24)|(b[idx+2]<<16)|(b[idx+1]<<8)|b[idx]; };
  11883. var __readInt32BE = function(b, idx) { return (b[idx]<<24)|(b[idx+1]<<16)|(b[idx+2]<<8)|b[idx+3]; };
  11884. function ReadShift(size, t) {
  11885. var o="", oI, oR, oo=[], w, vv, i, loc;
  11886. switch(t) {
  11887. case 'dbcs':
  11888. loc = this.l;
  11889. if(has_buf && Buffer.isBuffer(this)) o = this.slice(this.l, this.l+2*size).toString("utf16le");
  11890. else for(i = 0; i < size; ++i) { o+=String.fromCharCode(__readUInt16LE(this, loc)); loc+=2; }
  11891. size *= 2;
  11892. break;
  11893. case 'utf8': o = __utf8(this, this.l, this.l + size); break;
  11894. case 'utf16le': size *= 2; o = __utf16le(this, this.l, this.l + size); break;
  11895. case 'wstr':
  11896. if(typeof cptable !== 'undefined') o = cptable.utils.decode(current_codepage, this.slice(this.l, this.l+2*size));
  11897. else return ReadShift.call(this, size, 'dbcs');
  11898. size = 2 * size; break;
  11899. /* [MS-OLEDS] 2.1.4 LengthPrefixedAnsiString */
  11900. case 'lpstr-ansi': o = __lpstr(this, this.l); size = 4 + __readUInt32LE(this, this.l); break;
  11901. case 'lpstr-cp': o = __cpstr(this, this.l); size = 4 + __readUInt32LE(this, this.l); break;
  11902. /* [MS-OLEDS] 2.1.5 LengthPrefixedUnicodeString */
  11903. case 'lpwstr': o = __lpwstr(this, this.l); size = 4 + 2 * __readUInt32LE(this, this.l); break;
  11904. /* [MS-OFFCRYPTO] 2.1.2 Length-Prefixed Padded Unicode String (UNICODE-LP-P4) */
  11905. case 'lpp4': size = 4 + __readUInt32LE(this, this.l); o = __lpp4(this, this.l); if(size & 0x02) size += 2; break;
  11906. /* [MS-OFFCRYPTO] 2.1.3 Length-Prefixed UTF-8 String (UTF-8-LP-P4) */
  11907. case '8lpp4': size = 4 + __readUInt32LE(this, this.l); o = __8lpp4(this, this.l); if(size & 0x03) size += 4 - (size & 0x03); break;
  11908. case 'cstr': size = 0; o = "";
  11909. while((w=__readUInt8(this, this.l + size++))!==0) oo.push(_getchar(w));
  11910. o = oo.join(""); break;
  11911. case '_wstr': size = 0; o = "";
  11912. while((w=__readUInt16LE(this,this.l +size))!==0){oo.push(_getchar(w));size+=2;}
  11913. size+=2; o = oo.join(""); break;
  11914. /* sbcs and dbcs support continue records in the SST way TODO codepages */
  11915. case 'dbcs-cont': o = ""; loc = this.l;
  11916. for(i = 0; i < size; ++i) {
  11917. if(this.lens && this.lens.indexOf(loc) !== -1) {
  11918. w = __readUInt8(this, loc);
  11919. this.l = loc + 1;
  11920. vv = ReadShift.call(this, size-i, w ? 'dbcs-cont' : 'sbcs-cont');
  11921. return oo.join("") + vv;
  11922. }
  11923. oo.push(_getchar(__readUInt16LE(this, loc)));
  11924. loc+=2;
  11925. } o = oo.join(""); size *= 2; break;
  11926. case 'cpstr':
  11927. if(typeof cptable !== 'undefined') {
  11928. o = cptable.utils.decode(current_codepage, this.slice(this.l, this.l + size));
  11929. break;
  11930. }
  11931. /* falls through */
  11932. case 'sbcs-cont': o = ""; loc = this.l;
  11933. for(i = 0; i != size; ++i) {
  11934. if(this.lens && this.lens.indexOf(loc) !== -1) {
  11935. w = __readUInt8(this, loc);
  11936. this.l = loc + 1;
  11937. vv = ReadShift.call(this, size-i, w ? 'dbcs-cont' : 'sbcs-cont');
  11938. return oo.join("") + vv;
  11939. }
  11940. oo.push(_getchar(__readUInt8(this, loc)));
  11941. loc+=1;
  11942. } o = oo.join(""); break;
  11943. default:
  11944. switch(size) {
  11945. case 1: oI = __readUInt8(this, this.l); this.l++; return oI;
  11946. case 2: oI = (t === 'i' ? __readInt16LE : __readUInt16LE)(this, this.l); this.l += 2; return oI;
  11947. case 4: case -4:
  11948. if(t === 'i' || ((this[this.l+3] & 0x80)===0)) { oI = ((size > 0) ? __readInt32LE : __readInt32BE)(this, this.l); this.l += 4; return oI; }
  11949. else { oR = __readUInt32LE(this, this.l); this.l += 4; } return oR;
  11950. case 8: case -8:
  11951. if(t === 'f') {
  11952. if(size == 8) oR = __double(this, this.l);
  11953. else oR = __double([this[this.l+7],this[this.l+6],this[this.l+5],this[this.l+4],this[this.l+3],this[this.l+2],this[this.l+1],this[this.l+0]], 0);
  11954. this.l += 8; return oR;
  11955. } else size = 8;
  11956. /* falls through */
  11957. case 16: o = __hexlify(this, this.l, size); break;
  11958. }}
  11959. this.l+=size; return o;
  11960. }
  11961. var __writeUInt32LE = function(b, val, idx) { b[idx] = (val & 0xFF); b[idx+1] = ((val >>> 8) & 0xFF); b[idx+2] = ((val >>> 16) & 0xFF); b[idx+3] = ((val >>> 24) & 0xFF); };
  11962. var __writeInt32LE = function(b, val, idx) { b[idx] = (val & 0xFF); b[idx+1] = ((val >> 8) & 0xFF); b[idx+2] = ((val >> 16) & 0xFF); b[idx+3] = ((val >> 24) & 0xFF); };
  11963. var __writeUInt16LE = function(b, val, idx) { b[idx] = (val & 0xFF); b[idx+1] = ((val >>> 8) & 0xFF); };
  11964. function WriteShift(t, val, f) {
  11965. var size = 0, i = 0;
  11966. if(f === 'dbcs') {
  11967. for(i = 0; i != val.length; ++i) __writeUInt16LE(this, val.charCodeAt(i), this.l + 2 * i);
  11968. size = 2 * val.length;
  11969. } else if(f === 'sbcs') {
  11970. /* TODO: codepage */
  11971. val = val.replace(/[^\x00-\x7F]/g, "_");
  11972. for(i = 0; i != val.length; ++i) this[this.l + i] = (val.charCodeAt(i) & 0xFF);
  11973. size = val.length;
  11974. } else if(f === 'hex') {
  11975. for(; i < t; ++i) {
  11976. this[this.l++] = (parseInt(val.slice(2*i, 2*i+2), 16)||0);
  11977. } return this;
  11978. } else if(f === 'utf16le') {
  11979. var end = Math.min(this.l + t, this.length);
  11980. for(i = 0; i < Math.min(val.length, t); ++i) {
  11981. var cc = val.charCodeAt(i);
  11982. this[this.l++] = (cc & 0xff);
  11983. this[this.l++] = (cc >> 8);
  11984. }
  11985. while(this.l < end) this[this.l++] = 0;
  11986. return this;
  11987. } else switch(t) {
  11988. case 1: size = 1; this[this.l] = val&0xFF; break;
  11989. case 2: size = 2; this[this.l] = val&0xFF; val >>>= 8; this[this.l+1] = val&0xFF; break;
  11990. case 3: size = 3; this[this.l] = val&0xFF; val >>>= 8; this[this.l+1] = val&0xFF; val >>>= 8; this[this.l+2] = val&0xFF; break;
  11991. case 4: size = 4; __writeUInt32LE(this, val, this.l); break;
  11992. case 8: size = 8; if(f === 'f') { write_double_le(this, val, this.l); break; }
  11993. /* falls through */
  11994. case 16: break;
  11995. case -4: size = 4; __writeInt32LE(this, val, this.l); break;
  11996. }
  11997. this.l += size; return this;
  11998. }
  11999. function CheckField(hexstr, fld) {
  12000. var m = __hexlify(this,this.l,hexstr.length>>1);
  12001. if(m !== hexstr) throw new Error(fld + 'Expected ' + hexstr + ' saw ' + m);
  12002. this.l += hexstr.length>>1;
  12003. }
  12004. function prep_blob(blob, pos) {
  12005. blob.l = pos;
  12006. blob.read_shift = ReadShift;
  12007. blob.chk = CheckField;
  12008. blob.write_shift = WriteShift;
  12009. }
  12010. function parsenoop(blob, length) { blob.l += length; }
  12011. function new_buf(sz) {
  12012. var o = new_raw_buf(sz);
  12013. prep_blob(o, 0);
  12014. return o;
  12015. }
  12016. /* [MS-XLSB] 2.1.4 Record */
  12017. function recordhopper(data, cb, opts) {
  12018. if(!data) return;
  12019. var tmpbyte, cntbyte, length;
  12020. prep_blob(data, data.l || 0);
  12021. var L = data.length, RT = 0, tgt = 0;
  12022. while(data.l < L) {
  12023. RT = data.read_shift(1);
  12024. if(RT & 0x80) RT = (RT & 0x7F) + ((data.read_shift(1) & 0x7F)<<7);
  12025. var R = XLSBRecordEnum[RT] || XLSBRecordEnum[0xFFFF];
  12026. tmpbyte = data.read_shift(1);
  12027. length = tmpbyte & 0x7F;
  12028. for(cntbyte = 1; cntbyte <4 && (tmpbyte & 0x80); ++cntbyte) length += ((tmpbyte = data.read_shift(1)) & 0x7F)<<(7*cntbyte);
  12029. tgt = data.l + length;
  12030. var d = (R.f||parsenoop)(data, length, opts);
  12031. data.l = tgt;
  12032. if(cb(d, R.n, RT)) return;
  12033. }
  12034. }
  12035. /* control buffer usage for fixed-length buffers */
  12036. function buf_array() {
  12037. var bufs = [], blksz = has_buf ? 256 : 2048;
  12038. var newblk = function ba_newblk(sz) {
  12039. var o = (new_buf(sz));
  12040. prep_blob(o, 0);
  12041. return o;
  12042. };
  12043. var curbuf = newblk(blksz);
  12044. var endbuf = function ba_endbuf() {
  12045. if(!curbuf) return;
  12046. if(curbuf.length > curbuf.l) { curbuf = curbuf.slice(0, curbuf.l); curbuf.l = curbuf.length; }
  12047. if(curbuf.length > 0) bufs.push(curbuf);
  12048. curbuf = null;
  12049. };
  12050. var next = function ba_next(sz) {
  12051. if(curbuf && (sz < (curbuf.length - curbuf.l))) return curbuf;
  12052. endbuf();
  12053. return (curbuf = newblk(Math.max(sz+1, blksz)));
  12054. };
  12055. var end = function ba_end() {
  12056. endbuf();
  12057. return __toBuffer([bufs]);
  12058. };
  12059. var push = function ba_push(buf) { endbuf(); curbuf = buf; if(curbuf.l == null) curbuf.l = curbuf.length; next(blksz); };
  12060. return ({ next:next, push:push, end:end, _bufs:bufs });
  12061. }
  12062. function write_record(ba, type, payload, length) {
  12063. var t = +XLSBRE[type], l;
  12064. if(isNaN(t)) return; // TODO: throw something here?
  12065. if(!length) length = XLSBRecordEnum[t].p || (payload||[]).length || 0;
  12066. l = 1 + (t >= 0x80 ? 1 : 0) + 1/* + length*/;
  12067. if(length >= 0x80) ++l; if(length >= 0x4000) ++l; if(length >= 0x200000) ++l;
  12068. var o = ba.next(l);
  12069. if(t <= 0x7F) o.write_shift(1, t);
  12070. else {
  12071. o.write_shift(1, (t & 0x7F) + 0x80);
  12072. o.write_shift(1, (t >> 7));
  12073. }
  12074. for(var i = 0; i != 4; ++i) {
  12075. if(length >= 0x80) { o.write_shift(1, (length & 0x7F)+0x80); length >>= 7; }
  12076. else { o.write_shift(1, length); break; }
  12077. }
  12078. if(length > 0 && is_buf(payload)) ba.push(payload);
  12079. }
  12080. /* XLS ranges enforced */
  12081. function shift_cell_xls(cell, tgt, opts) {
  12082. var out = dup(cell);
  12083. if(tgt.s) {
  12084. if(out.cRel) out.c += tgt.s.c;
  12085. if(out.rRel) out.r += tgt.s.r;
  12086. } else {
  12087. if(out.cRel) out.c += tgt.c;
  12088. if(out.rRel) out.r += tgt.r;
  12089. }
  12090. if(!opts || opts.biff < 12) {
  12091. while(out.c >= 0x100) out.c -= 0x100;
  12092. while(out.r >= 0x10000) out.r -= 0x10000;
  12093. }
  12094. return out;
  12095. }
  12096. function shift_range_xls(cell, range, opts) {
  12097. var out = dup(cell);
  12098. out.s = shift_cell_xls(out.s, range.s, opts);
  12099. out.e = shift_cell_xls(out.e, range.s, opts);
  12100. return out;
  12101. }
  12102. function encode_cell_xls(c, biff) {
  12103. if(c.cRel && c.c < 0) { c = dup(c); c.c += (biff > 8) ? 0x4000 : 0x100; }
  12104. if(c.rRel && c.r < 0) { c = dup(c); c.r += (biff > 8) ? 0x100000 : ((biff > 5) ? 0x10000 : 0x4000); }
  12105. var s = encode_cell(c);
  12106. if(c.cRel === 0) s = fix_col(s);
  12107. if(c.rRel === 0) s = fix_row(s);
  12108. return s;
  12109. }
  12110. function encode_range_xls(r, opts) {
  12111. if(r.s.r == 0 && !r.s.rRel) {
  12112. if(r.e.r == (opts.biff >= 12 ? 0xFFFFF : (opts.biff >= 8 ? 0x10000 : 0x4000)) && !r.e.rRel) {
  12113. return (r.s.cRel ? "" : "$") + encode_col(r.s.c) + ":" + (r.e.cRel ? "" : "$") + encode_col(r.e.c);
  12114. }
  12115. }
  12116. if(r.s.c == 0 && !r.s.cRel) {
  12117. if(r.e.c == (opts.biff >= 12 ? 0xFFFF : 0xFF) && !r.e.cRel) {
  12118. return (r.s.rRel ? "" : "$") + encode_row(r.s.r) + ":" + (r.e.rRel ? "" : "$") + encode_row(r.e.r);
  12119. }
  12120. }
  12121. return encode_cell_xls(r.s, opts.biff) + ":" + encode_cell_xls(r.e, opts.biff);
  12122. }
  12123. var OFFCRYPTO = {};
  12124. var make_offcrypto = function(O, _crypto) {
  12125. var crypto;
  12126. if(typeof _crypto !== 'undefined') crypto = _crypto;
  12127. else if(typeof require !== 'undefined') {
  12128. try { crypto = undefined; }
  12129. catch(e) { crypto = null; }
  12130. }
  12131. O.rc4 = function(key, data) {
  12132. var S = new Array(256);
  12133. var c = 0, i = 0, j = 0, t = 0;
  12134. for(i = 0; i != 256; ++i) S[i] = i;
  12135. for(i = 0; i != 256; ++i) {
  12136. j = (j + S[i] + (key[i%key.length]).charCodeAt(0))&255;
  12137. t = S[i]; S[i] = S[j]; S[j] = t;
  12138. }
  12139. // $FlowIgnore
  12140. i = j = 0; var out = Buffer(data.length);
  12141. for(c = 0; c != data.length; ++c) {
  12142. i = (i + 1)&255;
  12143. j = (j + S[i])%256;
  12144. t = S[i]; S[i] = S[j]; S[j] = t;
  12145. out[c] = (data[c] ^ S[(S[i]+S[j])&255]);
  12146. }
  12147. return out;
  12148. };
  12149. O.md5 = function(hex) {
  12150. if(!crypto) throw new Error("Unsupported crypto");
  12151. return crypto.createHash('md5').update(hex).digest('hex');
  12152. };
  12153. };
  12154. /*global crypto:true */
  12155. make_offcrypto(OFFCRYPTO, typeof crypto !== "undefined" ? crypto : undefined);
  12156. function decode_row(rowstr) { return parseInt(unfix_row(rowstr),10) - 1; }
  12157. function encode_row(row) { return "" + (row + 1); }
  12158. function fix_row(cstr) { return cstr.replace(/([A-Z]|^)(\d+)$/,"$1$$$2"); }
  12159. function unfix_row(cstr) { return cstr.replace(/\$(\d+)$/,"$1"); }
  12160. function decode_col(colstr) { var c = unfix_col(colstr), d = 0, i = 0; for(; i !== c.length; ++i) d = 26*d + c.charCodeAt(i) - 64; return d - 1; }
  12161. function encode_col(col) { var s=""; for(++col; col; col=Math.floor((col-1)/26)) s = String.fromCharCode(((col-1)%26) + 65) + s; return s; }
  12162. function fix_col(cstr) { return cstr.replace(/^([A-Z])/,"$$$1"); }
  12163. function unfix_col(cstr) { return cstr.replace(/^\$([A-Z])/,"$1"); }
  12164. function split_cell(cstr) { return cstr.replace(/(\$?[A-Z]*)(\$?\d*)/,"$1,$2").split(","); }
  12165. function decode_cell(cstr) { var splt = split_cell(cstr); return { c:decode_col(splt[0]), r:decode_row(splt[1]) }; }
  12166. function encode_cell(cell) { return encode_col(cell.c) + encode_row(cell.r); }
  12167. function decode_range(range) { var x =range.split(":").map(decode_cell); return {s:x[0],e:x[x.length-1]}; }
  12168. function encode_range(cs,ce) {
  12169. if(typeof ce === 'undefined' || typeof ce === 'number') {
  12170. return encode_range(cs.s, cs.e);
  12171. }
  12172. if(typeof cs !== 'string') cs = encode_cell((cs));
  12173. if(typeof ce !== 'string') ce = encode_cell((ce));
  12174. return cs == ce ? cs : cs + ":" + ce;
  12175. }
  12176. function safe_decode_range(range) {
  12177. var o = {s:{c:0,r:0},e:{c:0,r:0}};
  12178. var idx = 0, i = 0, cc = 0;
  12179. var len = range.length;
  12180. for(idx = 0; i < len; ++i) {
  12181. if((cc=range.charCodeAt(i)-64) < 1 || cc > 26) break;
  12182. idx = 26*idx + cc;
  12183. }
  12184. o.s.c = --idx;
  12185. for(idx = 0; i < len; ++i) {
  12186. if((cc=range.charCodeAt(i)-48) < 0 || cc > 9) break;
  12187. idx = 10*idx + cc;
  12188. }
  12189. o.s.r = --idx;
  12190. if(i === len || range.charCodeAt(++i) === 58) { o.e.c=o.s.c; o.e.r=o.s.r; return o; }
  12191. for(idx = 0; i != len; ++i) {
  12192. if((cc=range.charCodeAt(i)-64) < 1 || cc > 26) break;
  12193. idx = 26*idx + cc;
  12194. }
  12195. o.e.c = --idx;
  12196. for(idx = 0; i != len; ++i) {
  12197. if((cc=range.charCodeAt(i)-48) < 0 || cc > 9) break;
  12198. idx = 10*idx + cc;
  12199. }
  12200. o.e.r = --idx;
  12201. return o;
  12202. }
  12203. function safe_format_cell(cell, v) {
  12204. var q = (cell.t == 'd' && v instanceof Date);
  12205. if(cell.z != null) try { return (cell.w = SSF.format(cell.z, q ? datenum(v) : v)); } catch(e) { }
  12206. try { return (cell.w = SSF.format((cell.XF||{}).numFmtId||(q ? 14 : 0), q ? datenum(v) : v)); } catch(e) { return ''+v; }
  12207. }
  12208. function format_cell(cell, v, o) {
  12209. if(cell == null || cell.t == null || cell.t == 'z') return "";
  12210. if(cell.w !== undefined) return cell.w;
  12211. if(cell.t == 'd' && !cell.z && o && o.dateNF) cell.z = o.dateNF;
  12212. if(v == undefined) return safe_format_cell(cell, cell.v);
  12213. return safe_format_cell(cell, v);
  12214. }
  12215. function sheet_to_workbook(sheet, opts) {
  12216. var n = opts && opts.sheet ? opts.sheet : "Sheet1";
  12217. var sheets = {}; sheets[n] = sheet;
  12218. return { SheetNames: [n], Sheets: sheets };
  12219. }
  12220. function sheet_add_aoa(_ws, data, opts) {
  12221. var o = opts || {};
  12222. var dense = _ws ? Array.isArray(_ws) : o.dense;
  12223. if(DENSE != null && dense == null) dense = DENSE;
  12224. var ws = _ws || (dense ? ([]) : ({}));
  12225. var _R = 0, _C = 0;
  12226. if(ws && o.origin != null) {
  12227. if(typeof o.origin == 'number') _R = o.origin;
  12228. else {
  12229. var _origin = typeof o.origin == "string" ? decode_cell(o.origin) : o.origin;
  12230. _R = _origin.r; _C = _origin.c;
  12231. }
  12232. }
  12233. var range = ({s: {c:10000000, r:10000000}, e: {c:0, r:0}});
  12234. if(ws['!ref']) {
  12235. var _range = safe_decode_range(ws['!ref']);
  12236. range.s.c = _range.s.c;
  12237. range.s.r = _range.s.r;
  12238. range.e.c = Math.max(range.e.c, _range.e.c);
  12239. range.e.r = Math.max(range.e.r, _range.e.r);
  12240. if(_R == -1) range.e.r = _R = _range.e.r + 1;
  12241. }
  12242. for(var R = 0; R != data.length; ++R) {
  12243. if(!data[R]) continue;
  12244. if(!Array.isArray(data[R])) throw new Error("aoa_to_sheet expects an array of arrays");
  12245. for(var C = 0; C != data[R].length; ++C) {
  12246. if(typeof data[R][C] === 'undefined') continue;
  12247. var cell = ({v: data[R][C] });
  12248. var __R = _R + R, __C = _C + C;
  12249. if(range.s.r > __R) range.s.r = __R;
  12250. if(range.s.c > __C) range.s.c = __C;
  12251. if(range.e.r < __R) range.e.r = __R;
  12252. if(range.e.c < __C) range.e.c = __C;
  12253. if(data[R][C] && typeof data[R][C] === 'object' && !Array.isArray(data[R][C]) && !(data[R][C] instanceof Date)) cell = data[R][C];
  12254. else {
  12255. if(Array.isArray(cell.v)) { cell.f = data[R][C][1]; cell.v = cell.v[0]; }
  12256. if(cell.v === null) { if(cell.f) cell.t = 'n'; else if(!o.sheetStubs) continue; else cell.t = 'z'; }
  12257. else if(typeof cell.v === 'number') cell.t = 'n';
  12258. else if(typeof cell.v === 'boolean') cell.t = 'b';
  12259. else if(cell.v instanceof Date) {
  12260. cell.z = o.dateNF || SSF._table[14];
  12261. if(o.cellDates) { cell.t = 'd'; cell.w = SSF.format(cell.z, datenum(cell.v)); }
  12262. else { cell.t = 'n'; cell.v = datenum(cell.v); cell.w = SSF.format(cell.z, cell.v); }
  12263. }
  12264. else cell.t = 's';
  12265. }
  12266. if(dense) {
  12267. if(!ws[__R]) ws[__R] = [];
  12268. ws[__R][__C] = cell;
  12269. } else {
  12270. var cell_ref = encode_cell(({c:__C,r:__R}));
  12271. ws[cell_ref] = cell;
  12272. }
  12273. }
  12274. }
  12275. if(range.s.c < 10000000) ws['!ref'] = encode_range(range);
  12276. return ws;
  12277. }
  12278. function aoa_to_sheet(data, opts) { return sheet_add_aoa(null, data, opts); }
  12279. function write_UInt32LE(x, o) {
  12280. if(!o) o = new_buf(4);
  12281. o.write_shift(4, x);
  12282. return o;
  12283. }
  12284. /* [MS-XLSB] 2.5.168 */
  12285. function parse_XLWideString(data) {
  12286. var cchCharacters = data.read_shift(4);
  12287. return cchCharacters === 0 ? "" : data.read_shift(cchCharacters, 'dbcs');
  12288. }
  12289. function write_XLWideString(data, o) {
  12290. var _null = false; if(o == null) { _null = true; o = new_buf(4+2*data.length); }
  12291. o.write_shift(4, data.length);
  12292. if(data.length > 0) o.write_shift(0, data, 'dbcs');
  12293. return _null ? o.slice(0, o.l) : o;
  12294. }
  12295. /* [MS-XLSB] 2.5.143 */
  12296. function parse_StrRun(data) {
  12297. return { ich: data.read_shift(2), ifnt: data.read_shift(2) };
  12298. }
  12299. function write_StrRun(run, o) {
  12300. if(!o) o = new_buf(4);
  12301. o.write_shift(2, run.ich || 0);
  12302. o.write_shift(2, run.ifnt || 0);
  12303. return o;
  12304. }
  12305. /* [MS-XLSB] 2.5.121 */
  12306. function parse_RichStr(data, length) {
  12307. var start = data.l;
  12308. var flags = data.read_shift(1);
  12309. var str = parse_XLWideString(data);
  12310. var rgsStrRun = [];
  12311. var z = ({ t: str, h: str });
  12312. if((flags & 1) !== 0) { /* fRichStr */
  12313. /* TODO: formatted string */
  12314. var dwSizeStrRun = data.read_shift(4);
  12315. for(var i = 0; i != dwSizeStrRun; ++i) rgsStrRun.push(parse_StrRun(data));
  12316. z.r = rgsStrRun;
  12317. }
  12318. else z.r = [{ich:0, ifnt:0}];
  12319. //if((flags & 2) !== 0) { /* fExtStr */
  12320. // /* TODO: phonetic string */
  12321. //}
  12322. data.l = start + length;
  12323. return z;
  12324. }
  12325. function write_RichStr(str, o) {
  12326. /* TODO: formatted string */
  12327. var _null = false; if(o == null) { _null = true; o = new_buf(15+4*str.t.length); }
  12328. o.write_shift(1,0);
  12329. write_XLWideString(str.t, o);
  12330. return _null ? o.slice(0, o.l) : o;
  12331. }
  12332. /* [MS-XLSB] 2.4.328 BrtCommentText (RichStr w/1 run) */
  12333. var parse_BrtCommentText = parse_RichStr;
  12334. function write_BrtCommentText(str, o) {
  12335. /* TODO: formatted string */
  12336. var _null = false; if(o == null) { _null = true; o = new_buf(23+4*str.t.length); }
  12337. o.write_shift(1,1);
  12338. write_XLWideString(str.t, o);
  12339. o.write_shift(4,1);
  12340. write_StrRun({ich:0,ifnt:0}, o);
  12341. return _null ? o.slice(0, o.l) : o;
  12342. }
  12343. /* [MS-XLSB] 2.5.9 */
  12344. function parse_XLSBCell(data) {
  12345. var col = data.read_shift(4);
  12346. var iStyleRef = data.read_shift(2);
  12347. iStyleRef += data.read_shift(1) <<16;
  12348. data.l++; //var fPhShow = data.read_shift(1);
  12349. return { c:col, iStyleRef: iStyleRef };
  12350. }
  12351. function write_XLSBCell(cell, o) {
  12352. if(o == null) o = new_buf(8);
  12353. o.write_shift(-4, cell.c);
  12354. o.write_shift(3, cell.iStyleRef || cell.s);
  12355. o.write_shift(1, 0); /* fPhShow */
  12356. return o;
  12357. }
  12358. /* [MS-XLSB] 2.5.21 */
  12359. var parse_XLSBCodeName = parse_XLWideString;
  12360. var write_XLSBCodeName = write_XLWideString;
  12361. /* [MS-XLSB] 2.5.166 */
  12362. function parse_XLNullableWideString(data) {
  12363. var cchCharacters = data.read_shift(4);
  12364. return cchCharacters === 0 || cchCharacters === 0xFFFFFFFF ? "" : data.read_shift(cchCharacters, 'dbcs');
  12365. }
  12366. function write_XLNullableWideString(data, o) {
  12367. var _null = false; if(o == null) { _null = true; o = new_buf(127); }
  12368. o.write_shift(4, data.length > 0 ? data.length : 0xFFFFFFFF);
  12369. if(data.length > 0) o.write_shift(0, data, 'dbcs');
  12370. return _null ? o.slice(0, o.l) : o;
  12371. }
  12372. /* [MS-XLSB] 2.5.165 */
  12373. var parse_XLNameWideString = parse_XLWideString;
  12374. //var write_XLNameWideString = write_XLWideString;
  12375. /* [MS-XLSB] 2.5.114 */
  12376. var parse_RelID = parse_XLNullableWideString;
  12377. var write_RelID = write_XLNullableWideString;
  12378. /* [MS-XLS] 2.5.217 ; [MS-XLSB] 2.5.122 */
  12379. function parse_RkNumber(data) {
  12380. var b = data.slice(data.l, data.l+4);
  12381. var fX100 = (b[0] & 1), fInt = (b[0] & 2);
  12382. data.l+=4;
  12383. b[0] &= 0xFC; // b[0] &= ~3;
  12384. var RK = fInt === 0 ? __double([0,0,0,0,b[0],b[1],b[2],b[3]],0) : __readInt32LE(b,0)>>2;
  12385. return fX100 ? (RK/100) : RK;
  12386. }
  12387. function write_RkNumber(data, o) {
  12388. if(o == null) o = new_buf(4);
  12389. var fX100 = 0, fInt = 0, d100 = data * 100;
  12390. if((data == (data | 0)) && (data >= -(1<<29)) && (data < (1 << 29))) { fInt = 1; }
  12391. else if((d100 == (d100 | 0)) && (d100 >= -(1<<29)) && (d100 < (1 << 29))) { fInt = 1; fX100 = 1; }
  12392. if(fInt) o.write_shift(-4, ((fX100 ? d100 : data) << 2) + (fX100 + 2));
  12393. else throw new Error("unsupported RkNumber " + data); // TODO
  12394. }
  12395. /* [MS-XLSB] 2.5.117 RfX */
  12396. function parse_RfX(data ) {
  12397. var cell = ({s: {}, e: {}});
  12398. cell.s.r = data.read_shift(4);
  12399. cell.e.r = data.read_shift(4);
  12400. cell.s.c = data.read_shift(4);
  12401. cell.e.c = data.read_shift(4);
  12402. return cell;
  12403. }
  12404. function write_RfX(r, o) {
  12405. if(!o) o = new_buf(16);
  12406. o.write_shift(4, r.s.r);
  12407. o.write_shift(4, r.e.r);
  12408. o.write_shift(4, r.s.c);
  12409. o.write_shift(4, r.e.c);
  12410. return o;
  12411. }
  12412. /* [MS-XLSB] 2.5.153 UncheckedRfX */
  12413. var parse_UncheckedRfX = parse_RfX;
  12414. var write_UncheckedRfX = write_RfX;
  12415. /* [MS-XLS] 2.5.342 ; [MS-XLSB] 2.5.171 */
  12416. /* TODO: error checking, NaN and Infinity values are not valid Xnum */
  12417. function parse_Xnum(data) { return data.read_shift(8, 'f'); }
  12418. function write_Xnum(data, o) { return (o || new_buf(8)).write_shift(8, data, 'f'); }
  12419. /* [MS-XLSB] 2.5.97.2 */
  12420. var BErr = {
  12421. 0x00: "#NULL!",
  12422. 0x07: "#DIV/0!",
  12423. 0x0F: "#VALUE!",
  12424. 0x17: "#REF!",
  12425. 0x1D: "#NAME?",
  12426. 0x24: "#NUM!",
  12427. 0x2A: "#N/A",
  12428. 0x2B: "#GETTING_DATA",
  12429. 0xFF: "#WTF?"
  12430. };
  12431. var RBErr = evert_num(BErr);
  12432. /* [MS-XLSB] 2.4.324 BrtColor */
  12433. function parse_BrtColor(data) {
  12434. var out = {};
  12435. var d = data.read_shift(1);
  12436. //var fValidRGB = d & 1;
  12437. var xColorType = d >>> 1;
  12438. var index = data.read_shift(1);
  12439. var nTS = data.read_shift(2, 'i');
  12440. var bR = data.read_shift(1);
  12441. var bG = data.read_shift(1);
  12442. var bB = data.read_shift(1);
  12443. data.l++; //var bAlpha = data.read_shift(1);
  12444. switch(xColorType) {
  12445. case 0: out.auto = 1; break;
  12446. case 1:
  12447. out.index = index;
  12448. var icv = XLSIcv[index];
  12449. /* automatic pseudo index 81 */
  12450. if(icv) out.rgb = rgb2Hex(icv);
  12451. break;
  12452. case 2:
  12453. /* if(!fValidRGB) throw new Error("invalid"); */
  12454. out.rgb = rgb2Hex([bR, bG, bB]);
  12455. break;
  12456. case 3: out.theme = index; break;
  12457. }
  12458. if(nTS != 0) out.tint = nTS > 0 ? nTS / 32767 : nTS / 32768;
  12459. return out;
  12460. }
  12461. function write_BrtColor(color, o) {
  12462. if(!o) o = new_buf(8);
  12463. if(!color||color.auto) { o.write_shift(4, 0); o.write_shift(4, 0); return o; }
  12464. if(color.index) {
  12465. o.write_shift(1, 0x02);
  12466. o.write_shift(1, color.index);
  12467. } else if(color.theme) {
  12468. o.write_shift(1, 0x06);
  12469. o.write_shift(1, color.theme);
  12470. } else {
  12471. o.write_shift(1, 0x05);
  12472. o.write_shift(1, 0);
  12473. }
  12474. var nTS = color.tint || 0;
  12475. if(nTS > 0) nTS *= 32767;
  12476. else if(nTS < 0) nTS *= 32768;
  12477. o.write_shift(2, nTS);
  12478. if(!color.rgb) {
  12479. o.write_shift(2, 0);
  12480. o.write_shift(1, 0);
  12481. o.write_shift(1, 0);
  12482. } else {
  12483. var rgb = (color.rgb || 'FFFFFF');
  12484. o.write_shift(1, parseInt(rgb.slice(0,2),16));
  12485. o.write_shift(1, parseInt(rgb.slice(2,4),16));
  12486. o.write_shift(1, parseInt(rgb.slice(4,6),16));
  12487. o.write_shift(1, 0xFF);
  12488. }
  12489. return o;
  12490. }
  12491. /* [MS-XLSB] 2.5.52 */
  12492. function parse_FontFlags(data) {
  12493. var d = data.read_shift(1);
  12494. data.l++;
  12495. var out = {
  12496. /* fBold: d & 0x01 */
  12497. fItalic: d & 0x02,
  12498. /* fUnderline: d & 0x04 */
  12499. fStrikeout: d & 0x08,
  12500. fOutline: d & 0x10,
  12501. fShadow: d & 0x20,
  12502. fCondense: d & 0x40,
  12503. fExtend: d & 0x80
  12504. };
  12505. return out;
  12506. }
  12507. function write_FontFlags(font, o) {
  12508. if(!o) o = new_buf(2);
  12509. var grbit =
  12510. (font.italic ? 0x02 : 0) |
  12511. (font.strike ? 0x08 : 0) |
  12512. (font.outline ? 0x10 : 0) |
  12513. (font.shadow ? 0x20 : 0) |
  12514. (font.condense ? 0x40 : 0) |
  12515. (font.extend ? 0x80 : 0);
  12516. o.write_shift(1, grbit);
  12517. o.write_shift(1, 0);
  12518. return o;
  12519. }
  12520. /* [MS-OLEDS] 2.3.1 and 2.3.2 */
  12521. function parse_ClipboardFormatOrString(o, w) {
  12522. // $FlowIgnore
  12523. var ClipFmt = {2:"BITMAP",3:"METAFILEPICT",8:"DIB",14:"ENHMETAFILE"};
  12524. var m = o.read_shift(4);
  12525. switch(m) {
  12526. case 0x00000000: return "";
  12527. case 0xffffffff: case 0xfffffffe: return ClipFmt[o.read_shift(4)]||"";
  12528. }
  12529. if(m > 0x190) throw new Error("Unsupported Clipboard: " + m.toString(16));
  12530. o.l -= 4;
  12531. return o.read_shift(0, w == 1 ? "lpstr" : "lpwstr");
  12532. }
  12533. function parse_ClipboardFormatOrAnsiString(o) { return parse_ClipboardFormatOrString(o, 1); }
  12534. function parse_ClipboardFormatOrUnicodeString(o) { return parse_ClipboardFormatOrString(o, 2); }
  12535. /* [MS-OLEPS] 2.2 PropertyType */
  12536. //var VT_EMPTY = 0x0000;
  12537. //var VT_NULL = 0x0001;
  12538. var VT_I2 = 0x0002;
  12539. var VT_I4 = 0x0003;
  12540. //var VT_R4 = 0x0004;
  12541. //var VT_R8 = 0x0005;
  12542. //var VT_CY = 0x0006;
  12543. //var VT_DATE = 0x0007;
  12544. //var VT_BSTR = 0x0008;
  12545. //var VT_ERROR = 0x000A;
  12546. var VT_BOOL = 0x000B;
  12547. var VT_VARIANT = 0x000C;
  12548. //var VT_DECIMAL = 0x000E;
  12549. //var VT_I1 = 0x0010;
  12550. //var VT_UI1 = 0x0011;
  12551. //var VT_UI2 = 0x0012;
  12552. var VT_UI4 = 0x0013;
  12553. //var VT_I8 = 0x0014;
  12554. //var VT_UI8 = 0x0015;
  12555. //var VT_INT = 0x0016;
  12556. //var VT_UINT = 0x0017;
  12557. var VT_LPSTR = 0x001E;
  12558. //var VT_LPWSTR = 0x001F;
  12559. var VT_FILETIME = 0x0040;
  12560. var VT_BLOB = 0x0041;
  12561. //var VT_STREAM = 0x0042;
  12562. //var VT_STORAGE = 0x0043;
  12563. //var VT_STREAMED_Object = 0x0044;
  12564. //var VT_STORED_Object = 0x0045;
  12565. //var VT_BLOB_Object = 0x0046;
  12566. var VT_CF = 0x0047;
  12567. //var VT_CLSID = 0x0048;
  12568. //var VT_VERSIONED_STREAM = 0x0049;
  12569. var VT_VECTOR = 0x1000;
  12570. //var VT_ARRAY = 0x2000;
  12571. var VT_STRING = 0x0050; // 2.3.3.1.11 VtString
  12572. var VT_USTR = 0x0051; // 2.3.3.1.12 VtUnalignedString
  12573. var VT_CUSTOM = [VT_STRING, VT_USTR];
  12574. /* [MS-OSHARED] 2.3.3.2.2.1 Document Summary Information PIDDSI */
  12575. var DocSummaryPIDDSI = {
  12576. 0x01: { n: 'CodePage', t: VT_I2 },
  12577. 0x02: { n: 'Category', t: VT_STRING },
  12578. 0x03: { n: 'PresentationFormat', t: VT_STRING },
  12579. 0x04: { n: 'ByteCount', t: VT_I4 },
  12580. 0x05: { n: 'LineCount', t: VT_I4 },
  12581. 0x06: { n: 'ParagraphCount', t: VT_I4 },
  12582. 0x07: { n: 'SlideCount', t: VT_I4 },
  12583. 0x08: { n: 'NoteCount', t: VT_I4 },
  12584. 0x09: { n: 'HiddenCount', t: VT_I4 },
  12585. 0x0a: { n: 'MultimediaClipCount', t: VT_I4 },
  12586. 0x0b: { n: 'ScaleCrop', t: VT_BOOL },
  12587. 0x0c: { n: 'HeadingPairs', t: VT_VECTOR | VT_VARIANT },
  12588. 0x0d: { n: 'TitlesOfParts', t: VT_VECTOR | VT_LPSTR },
  12589. 0x0e: { n: 'Manager', t: VT_STRING },
  12590. 0x0f: { n: 'Company', t: VT_STRING },
  12591. 0x10: { n: 'LinksUpToDate', t: VT_BOOL },
  12592. 0x11: { n: 'CharacterCount', t: VT_I4 },
  12593. 0x13: { n: 'SharedDoc', t: VT_BOOL },
  12594. 0x16: { n: 'HyperlinksChanged', t: VT_BOOL },
  12595. 0x17: { n: 'AppVersion', t: VT_I4, p: 'version' },
  12596. 0x18: { n: 'DigSig', t: VT_BLOB },
  12597. 0x1A: { n: 'ContentType', t: VT_STRING },
  12598. 0x1B: { n: 'ContentStatus', t: VT_STRING },
  12599. 0x1C: { n: 'Language', t: VT_STRING },
  12600. 0x1D: { n: 'Version', t: VT_STRING },
  12601. 0xFF: {}
  12602. };
  12603. /* [MS-OSHARED] 2.3.3.2.1.1 Summary Information Property Set PIDSI */
  12604. var SummaryPIDSI = {
  12605. 0x01: { n: 'CodePage', t: VT_I2 },
  12606. 0x02: { n: 'Title', t: VT_STRING },
  12607. 0x03: { n: 'Subject', t: VT_STRING },
  12608. 0x04: { n: 'Author', t: VT_STRING },
  12609. 0x05: { n: 'Keywords', t: VT_STRING },
  12610. 0x06: { n: 'Comments', t: VT_STRING },
  12611. 0x07: { n: 'Template', t: VT_STRING },
  12612. 0x08: { n: 'LastAuthor', t: VT_STRING },
  12613. 0x09: { n: 'RevNumber', t: VT_STRING },
  12614. 0x0A: { n: 'EditTime', t: VT_FILETIME },
  12615. 0x0B: { n: 'LastPrinted', t: VT_FILETIME },
  12616. 0x0C: { n: 'CreatedDate', t: VT_FILETIME },
  12617. 0x0D: { n: 'ModifiedDate', t: VT_FILETIME },
  12618. 0x0E: { n: 'PageCount', t: VT_I4 },
  12619. 0x0F: { n: 'WordCount', t: VT_I4 },
  12620. 0x10: { n: 'CharCount', t: VT_I4 },
  12621. 0x11: { n: 'Thumbnail', t: VT_CF },
  12622. 0x12: { n: 'Application', t: VT_STRING },
  12623. 0x13: { n: 'DocSecurity', t: VT_I4 },
  12624. 0xFF: {}
  12625. };
  12626. /* [MS-OLEPS] 2.18 */
  12627. var SpecialProperties = {
  12628. 0x80000000: { n: 'Locale', t: VT_UI4 },
  12629. 0x80000003: { n: 'Behavior', t: VT_UI4 },
  12630. 0x72627262: {}
  12631. };
  12632. (function() {
  12633. for(var y in SpecialProperties) if(SpecialProperties.hasOwnProperty(y))
  12634. DocSummaryPIDDSI[y] = SummaryPIDSI[y] = SpecialProperties[y];
  12635. })();
  12636. var DocSummaryRE = evert_key(DocSummaryPIDDSI, "n");
  12637. var SummaryRE = evert_key(SummaryPIDSI, "n");
  12638. /* [MS-XLS] 2.4.63 Country/Region codes */
  12639. var CountryEnum = {
  12640. 0x0001: "US", // United States
  12641. 0x0002: "CA", // Canada
  12642. 0x0003: "", // Latin America (except Brazil)
  12643. 0x0007: "RU", // Russia
  12644. 0x0014: "EG", // Egypt
  12645. 0x001E: "GR", // Greece
  12646. 0x001F: "NL", // Netherlands
  12647. 0x0020: "BE", // Belgium
  12648. 0x0021: "FR", // France
  12649. 0x0022: "ES", // Spain
  12650. 0x0024: "HU", // Hungary
  12651. 0x0027: "IT", // Italy
  12652. 0x0029: "CH", // Switzerland
  12653. 0x002B: "AT", // Austria
  12654. 0x002C: "GB", // United Kingdom
  12655. 0x002D: "DK", // Denmark
  12656. 0x002E: "SE", // Sweden
  12657. 0x002F: "NO", // Norway
  12658. 0x0030: "PL", // Poland
  12659. 0x0031: "DE", // Germany
  12660. 0x0034: "MX", // Mexico
  12661. 0x0037: "BR", // Brazil
  12662. 0x003d: "AU", // Australia
  12663. 0x0040: "NZ", // New Zealand
  12664. 0x0042: "TH", // Thailand
  12665. 0x0051: "JP", // Japan
  12666. 0x0052: "KR", // Korea
  12667. 0x0054: "VN", // Viet Nam
  12668. 0x0056: "CN", // China
  12669. 0x005A: "TR", // Turkey
  12670. 0x0069: "JS", // Ramastan
  12671. 0x00D5: "DZ", // Algeria
  12672. 0x00D8: "MA", // Morocco
  12673. 0x00DA: "LY", // Libya
  12674. 0x015F: "PT", // Portugal
  12675. 0x0162: "IS", // Iceland
  12676. 0x0166: "FI", // Finland
  12677. 0x01A4: "CZ", // Czech Republic
  12678. 0x0376: "TW", // Taiwan
  12679. 0x03C1: "LB", // Lebanon
  12680. 0x03C2: "JO", // Jordan
  12681. 0x03C3: "SY", // Syria
  12682. 0x03C4: "IQ", // Iraq
  12683. 0x03C5: "KW", // Kuwait
  12684. 0x03C6: "SA", // Saudi Arabia
  12685. 0x03CB: "AE", // United Arab Emirates
  12686. 0x03CC: "IL", // Israel
  12687. 0x03CE: "QA", // Qatar
  12688. 0x03D5: "IR", // Iran
  12689. 0xFFFF: "US" // United States
  12690. };
  12691. /* [MS-XLS] 2.5.127 */
  12692. var XLSFillPattern = [
  12693. null,
  12694. 'solid',
  12695. 'mediumGray',
  12696. 'darkGray',
  12697. 'lightGray',
  12698. 'darkHorizontal',
  12699. 'darkVertical',
  12700. 'darkDown',
  12701. 'darkUp',
  12702. 'darkGrid',
  12703. 'darkTrellis',
  12704. 'lightHorizontal',
  12705. 'lightVertical',
  12706. 'lightDown',
  12707. 'lightUp',
  12708. 'lightGrid',
  12709. 'lightTrellis',
  12710. 'gray125',
  12711. 'gray0625'
  12712. ];
  12713. function rgbify(arr) { return arr.map(function(x) { return [(x>>16)&255,(x>>8)&255,x&255]; }); }
  12714. /* [MS-XLS] 2.5.161 */
  12715. /* [MS-XLSB] 2.5.75 Icv */
  12716. var XLSIcv = rgbify([
  12717. /* Color Constants */
  12718. 0x000000,
  12719. 0xFFFFFF,
  12720. 0xFF0000,
  12721. 0x00FF00,
  12722. 0x0000FF,
  12723. 0xFFFF00,
  12724. 0xFF00FF,
  12725. 0x00FFFF,
  12726. /* Overridable Defaults */
  12727. 0x000000,
  12728. 0xFFFFFF,
  12729. 0xFF0000,
  12730. 0x00FF00,
  12731. 0x0000FF,
  12732. 0xFFFF00,
  12733. 0xFF00FF,
  12734. 0x00FFFF,
  12735. 0x800000,
  12736. 0x008000,
  12737. 0x000080,
  12738. 0x808000,
  12739. 0x800080,
  12740. 0x008080,
  12741. 0xC0C0C0,
  12742. 0x808080,
  12743. 0x9999FF,
  12744. 0x993366,
  12745. 0xFFFFCC,
  12746. 0xCCFFFF,
  12747. 0x660066,
  12748. 0xFF8080,
  12749. 0x0066CC,
  12750. 0xCCCCFF,
  12751. 0x000080,
  12752. 0xFF00FF,
  12753. 0xFFFF00,
  12754. 0x00FFFF,
  12755. 0x800080,
  12756. 0x800000,
  12757. 0x008080,
  12758. 0x0000FF,
  12759. 0x00CCFF,
  12760. 0xCCFFFF,
  12761. 0xCCFFCC,
  12762. 0xFFFF99,
  12763. 0x99CCFF,
  12764. 0xFF99CC,
  12765. 0xCC99FF,
  12766. 0xFFCC99,
  12767. 0x3366FF,
  12768. 0x33CCCC,
  12769. 0x99CC00,
  12770. 0xFFCC00,
  12771. 0xFF9900,
  12772. 0xFF6600,
  12773. 0x666699,
  12774. 0x969696,
  12775. 0x003366,
  12776. 0x339966,
  12777. 0x003300,
  12778. 0x333300,
  12779. 0x993300,
  12780. 0x993366,
  12781. 0x333399,
  12782. 0x333333,
  12783. /* Other entries to appease BIFF8/12 */
  12784. 0xFFFFFF, /* 0x40 icvForeground ?? */
  12785. 0x000000, /* 0x41 icvBackground ?? */
  12786. 0x000000, /* 0x42 icvFrame ?? */
  12787. 0x000000, /* 0x43 icv3D ?? */
  12788. 0x000000, /* 0x44 icv3DText ?? */
  12789. 0x000000, /* 0x45 icv3DHilite ?? */
  12790. 0x000000, /* 0x46 icv3DShadow ?? */
  12791. 0x000000, /* 0x47 icvHilite ?? */
  12792. 0x000000, /* 0x48 icvCtlText ?? */
  12793. 0x000000, /* 0x49 icvCtlScrl ?? */
  12794. 0x000000, /* 0x4A icvCtlInv ?? */
  12795. 0x000000, /* 0x4B icvCtlBody ?? */
  12796. 0x000000, /* 0x4C icvCtlFrame ?? */
  12797. 0x000000, /* 0x4D icvCtlFore ?? */
  12798. 0x000000, /* 0x4E icvCtlBack ?? */
  12799. 0x000000, /* 0x4F icvCtlNeutral */
  12800. 0x000000, /* 0x50 icvInfoBk ?? */
  12801. 0x000000 /* 0x51 icvInfoText ?? */
  12802. ]);
  12803. /* Parts enumerated in OPC spec, MS-XLSB and MS-XLSX */
  12804. /* 12.3 Part Summary <SpreadsheetML> */
  12805. /* 14.2 Part Summary <DrawingML> */
  12806. /* [MS-XLSX] 2.1 Part Enumerations ; [MS-XLSB] 2.1.7 Part Enumeration */
  12807. var ct2type/*{[string]:string}*/ = ({
  12808. /* Workbook */
  12809. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml": "workbooks",
  12810. /* Worksheet */
  12811. "application/vnd.ms-excel.binIndexWs": "TODO", /* Binary Index */
  12812. /* Macrosheet */
  12813. "application/vnd.ms-excel.intlmacrosheet": "TODO",
  12814. "application/vnd.ms-excel.binIndexMs": "TODO", /* Binary Index */
  12815. /* File Properties */
  12816. "application/vnd.openxmlformats-package.core-properties+xml": "coreprops",
  12817. "application/vnd.openxmlformats-officedocument.custom-properties+xml": "custprops",
  12818. "application/vnd.openxmlformats-officedocument.extended-properties+xml": "extprops",
  12819. /* Custom Data Properties */
  12820. "application/vnd.openxmlformats-officedocument.customXmlProperties+xml": "TODO",
  12821. "application/vnd.openxmlformats-officedocument.spreadsheetml.customProperty": "TODO",
  12822. /* PivotTable */
  12823. "application/vnd.ms-excel.pivotTable": "TODO",
  12824. "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotTable+xml": "TODO",
  12825. /* Chart Colors */
  12826. "application/vnd.ms-office.chartcolorstyle+xml": "TODO",
  12827. /* Chart Style */
  12828. "application/vnd.ms-office.chartstyle+xml": "TODO",
  12829. /* Calculation Chain */
  12830. "application/vnd.ms-excel.calcChain": "calcchains",
  12831. "application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml": "calcchains",
  12832. /* Printer Settings */
  12833. "application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings": "TODO",
  12834. /* ActiveX */
  12835. "application/vnd.ms-office.activeX": "TODO",
  12836. "application/vnd.ms-office.activeX+xml": "TODO",
  12837. /* Custom Toolbars */
  12838. "application/vnd.ms-excel.attachedToolbars": "TODO",
  12839. /* External Data Connections */
  12840. "application/vnd.ms-excel.connections": "TODO",
  12841. "application/vnd.openxmlformats-officedocument.spreadsheetml.connections+xml": "TODO",
  12842. /* External Links */
  12843. "application/vnd.ms-excel.externalLink": "links",
  12844. "application/vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml": "links",
  12845. /* Metadata */
  12846. "application/vnd.ms-excel.sheetMetadata": "TODO",
  12847. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheetMetadata+xml": "TODO",
  12848. /* PivotCache */
  12849. "application/vnd.ms-excel.pivotCacheDefinition": "TODO",
  12850. "application/vnd.ms-excel.pivotCacheRecords": "TODO",
  12851. "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheDefinition+xml": "TODO",
  12852. "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheRecords+xml": "TODO",
  12853. /* Query Table */
  12854. "application/vnd.ms-excel.queryTable": "TODO",
  12855. "application/vnd.openxmlformats-officedocument.spreadsheetml.queryTable+xml": "TODO",
  12856. /* Shared Workbook */
  12857. "application/vnd.ms-excel.userNames": "TODO",
  12858. "application/vnd.ms-excel.revisionHeaders": "TODO",
  12859. "application/vnd.ms-excel.revisionLog": "TODO",
  12860. "application/vnd.openxmlformats-officedocument.spreadsheetml.revisionHeaders+xml": "TODO",
  12861. "application/vnd.openxmlformats-officedocument.spreadsheetml.revisionLog+xml": "TODO",
  12862. "application/vnd.openxmlformats-officedocument.spreadsheetml.userNames+xml": "TODO",
  12863. /* Single Cell Table */
  12864. "application/vnd.ms-excel.tableSingleCells": "TODO",
  12865. "application/vnd.openxmlformats-officedocument.spreadsheetml.tableSingleCells+xml": "TODO",
  12866. /* Slicer */
  12867. "application/vnd.ms-excel.slicer": "TODO",
  12868. "application/vnd.ms-excel.slicerCache": "TODO",
  12869. "application/vnd.ms-excel.slicer+xml": "TODO",
  12870. "application/vnd.ms-excel.slicerCache+xml": "TODO",
  12871. /* Sort Map */
  12872. "application/vnd.ms-excel.wsSortMap": "TODO",
  12873. /* Table */
  12874. "application/vnd.ms-excel.table": "TODO",
  12875. "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml": "TODO",
  12876. /* Themes */
  12877. "application/vnd.openxmlformats-officedocument.theme+xml": "themes",
  12878. /* Theme Override */
  12879. "application/vnd.openxmlformats-officedocument.themeOverride+xml": "TODO",
  12880. /* Timeline */
  12881. "application/vnd.ms-excel.Timeline+xml": "TODO", /* verify */
  12882. "application/vnd.ms-excel.TimelineCache+xml": "TODO", /* verify */
  12883. /* VBA */
  12884. "application/vnd.ms-office.vbaProject": "vba",
  12885. "application/vnd.ms-office.vbaProjectSignature": "vba",
  12886. /* Volatile Dependencies */
  12887. "application/vnd.ms-office.volatileDependencies": "TODO",
  12888. "application/vnd.openxmlformats-officedocument.spreadsheetml.volatileDependencies+xml": "TODO",
  12889. /* Control Properties */
  12890. "application/vnd.ms-excel.controlproperties+xml": "TODO",
  12891. /* Data Model */
  12892. "application/vnd.openxmlformats-officedocument.model+data": "TODO",
  12893. /* Survey */
  12894. "application/vnd.ms-excel.Survey+xml": "TODO",
  12895. /* Drawing */
  12896. "application/vnd.openxmlformats-officedocument.drawing+xml": "drawings",
  12897. "application/vnd.openxmlformats-officedocument.drawingml.chart+xml": "TODO",
  12898. "application/vnd.openxmlformats-officedocument.drawingml.chartshapes+xml": "TODO",
  12899. "application/vnd.openxmlformats-officedocument.drawingml.diagramColors+xml": "TODO",
  12900. "application/vnd.openxmlformats-officedocument.drawingml.diagramData+xml": "TODO",
  12901. "application/vnd.openxmlformats-officedocument.drawingml.diagramLayout+xml": "TODO",
  12902. "application/vnd.openxmlformats-officedocument.drawingml.diagramStyle+xml": "TODO",
  12903. /* VML */
  12904. "application/vnd.openxmlformats-officedocument.vmlDrawing": "TODO",
  12905. "application/vnd.openxmlformats-package.relationships+xml": "rels",
  12906. "application/vnd.openxmlformats-officedocument.oleObject": "TODO",
  12907. /* Image */
  12908. "image/png": "TODO",
  12909. "sheet": "js"
  12910. });
  12911. var CT_LIST = (function(){
  12912. var o = {
  12913. workbooks: {
  12914. xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
  12915. xlsm: "application/vnd.ms-excel.sheet.macroEnabled.main+xml",
  12916. xlsb: "application/vnd.ms-excel.sheet.binary.macroEnabled.main",
  12917. xlam: "application/vnd.ms-excel.addin.macroEnabled.main+xml",
  12918. xltx: "application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml"
  12919. },
  12920. strs: { /* Shared Strings */
  12921. xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
  12922. xlsb: "application/vnd.ms-excel.sharedStrings"
  12923. },
  12924. comments: { /* Comments */
  12925. xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml",
  12926. xlsb: "application/vnd.ms-excel.comments"
  12927. },
  12928. sheets: { /* Worksheet */
  12929. xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
  12930. xlsb: "application/vnd.ms-excel.worksheet"
  12931. },
  12932. charts: { /* Chartsheet */
  12933. xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml",
  12934. xlsb: "application/vnd.ms-excel.chartsheet"
  12935. },
  12936. dialogs: { /* Dialogsheet */
  12937. xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.dialogsheet+xml",
  12938. xlsb: "application/vnd.ms-excel.dialogsheet"
  12939. },
  12940. macros: { /* Macrosheet (Excel 4.0 Macros) */
  12941. xlsx: "application/vnd.ms-excel.macrosheet+xml",
  12942. xlsb: "application/vnd.ms-excel.macrosheet"
  12943. },
  12944. styles: { /* Styles */
  12945. xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
  12946. xlsb: "application/vnd.ms-excel.styles"
  12947. }
  12948. };
  12949. keys(o).forEach(function(k) { ["xlsm", "xlam"].forEach(function(v) { if(!o[k][v]) o[k][v] = o[k].xlsx; }); });
  12950. keys(o).forEach(function(k){ keys(o[k]).forEach(function(v) { ct2type[o[k][v]] = k; }); });
  12951. return o;
  12952. })();
  12953. var type2ct/*{[string]:Array<string>}*/ = evert_arr(ct2type);
  12954. XMLNS.CT = 'http://schemas.openxmlformats.org/package/2006/content-types';
  12955. function new_ct() {
  12956. return ({
  12957. workbooks:[], sheets:[], charts:[], dialogs:[], macros:[],
  12958. rels:[], strs:[], comments:[], links:[],
  12959. coreprops:[], extprops:[], custprops:[], themes:[], styles:[],
  12960. calcchains:[], vba: [], drawings: [],
  12961. TODO:[], xmlns: "" });
  12962. }
  12963. function parse_ct(data) {
  12964. var ct = new_ct();
  12965. if(!data || !data.match) return ct;
  12966. var ctext = {};
  12967. (data.match(tagregex)||[]).forEach(function(x) {
  12968. var y = parsexmltag(x);
  12969. switch(y[0].replace(nsregex,"<")) {
  12970. case '<?xml': break;
  12971. case '<Types': ct.xmlns = y['xmlns' + (y[0].match(/<(\w+):/)||["",""])[1] ]; break;
  12972. case '<Default': ctext[y.Extension] = y.ContentType; break;
  12973. case '<Override':
  12974. if(ct[ct2type[y.ContentType]] !== undefined) ct[ct2type[y.ContentType]].push(y.PartName);
  12975. break;
  12976. }
  12977. });
  12978. if(ct.xmlns !== XMLNS.CT) throw new Error("Unknown Namespace: " + ct.xmlns);
  12979. ct.calcchain = ct.calcchains.length > 0 ? ct.calcchains[0] : "";
  12980. ct.sst = ct.strs.length > 0 ? ct.strs[0] : "";
  12981. ct.style = ct.styles.length > 0 ? ct.styles[0] : "";
  12982. ct.defaults = ctext;
  12983. delete ct.calcchains;
  12984. return ct;
  12985. }
  12986. var CTYPE_XML_ROOT = writextag('Types', null, {
  12987. 'xmlns': XMLNS.CT,
  12988. 'xmlns:xsd': XMLNS.xsd,
  12989. 'xmlns:xsi': XMLNS.xsi
  12990. });
  12991. var CTYPE_DEFAULTS = [
  12992. ['xml', 'application/xml'],
  12993. ['bin', 'application/vnd.ms-excel.sheet.binary.macroEnabled.main'],
  12994. ['vml', 'application/vnd.openxmlformats-officedocument.vmlDrawing'],
  12995. /* from test files */
  12996. ['bmp', 'image/bmp'],
  12997. ['png', 'image/png'],
  12998. ['gif', 'image/gif'],
  12999. ['emf', 'image/x-emf'],
  13000. ['wmf', 'image/x-wmf'],
  13001. ['jpg', 'image/jpeg'], ['jpeg', 'image/jpeg'],
  13002. ['tif', 'image/tiff'], ['tiff', 'image/tiff'],
  13003. ['pdf', 'application/pdf'],
  13004. ['rels', type2ct.rels[0]]
  13005. ].map(function(x) {
  13006. return writextag('Default', null, {'Extension':x[0], 'ContentType': x[1]});
  13007. });
  13008. function write_ct(ct, opts) {
  13009. var o = [], v;
  13010. o[o.length] = (XML_HEADER);
  13011. o[o.length] = (CTYPE_XML_ROOT);
  13012. o = o.concat(CTYPE_DEFAULTS);
  13013. var f1 = function(w) {
  13014. if(ct[w] && ct[w].length > 0) {
  13015. v = ct[w][0];
  13016. o[o.length] = (writextag('Override', null, {
  13017. 'PartName': (v[0] == '/' ? "":"/") + v,
  13018. 'ContentType': CT_LIST[w][opts.bookType || 'xlsx']
  13019. }));
  13020. }
  13021. };
  13022. var f2 = function(w) {
  13023. (ct[w]||[]).forEach(function(v) {
  13024. o[o.length] = (writextag('Override', null, {
  13025. 'PartName': (v[0] == '/' ? "":"/") + v,
  13026. 'ContentType': CT_LIST[w][opts.bookType || 'xlsx']
  13027. }));
  13028. });
  13029. };
  13030. var f3 = function(t) {
  13031. (ct[t]||[]).forEach(function(v) {
  13032. o[o.length] = (writextag('Override', null, {
  13033. 'PartName': (v[0] == '/' ? "":"/") + v,
  13034. 'ContentType': type2ct[t][0]
  13035. }));
  13036. });
  13037. };
  13038. f1('workbooks');
  13039. f2('sheets');
  13040. f2('charts');
  13041. f3('themes');
  13042. ['strs', 'styles'].forEach(f1);
  13043. ['coreprops', 'extprops', 'custprops'].forEach(f3);
  13044. f3('vba');
  13045. f3('comments');
  13046. f3('drawings');
  13047. if(o.length>2){ o[o.length] = ('</Types>'); o[1]=o[1].replace("/>",">"); }
  13048. return o.join("");
  13049. }
  13050. /* 9.3 Relationships */
  13051. var RELS = ({
  13052. WB: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
  13053. SHEET: "http://sheetjs.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
  13054. HLINK: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
  13055. VML: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing",
  13056. VBA: "http://schemas.microsoft.com/office/2006/relationships/vbaProject"
  13057. });
  13058. /* 9.3.3 Representing Relationships */
  13059. function get_rels_path(file) {
  13060. var n = file.lastIndexOf("/");
  13061. return file.slice(0,n+1) + '_rels/' + file.slice(n+1) + ".rels";
  13062. }
  13063. function parse_rels(data, currentFilePath) {
  13064. if (!data) return data;
  13065. if (currentFilePath.charAt(0) !== '/') {
  13066. currentFilePath = '/'+currentFilePath;
  13067. }
  13068. var rels = {};
  13069. var hash = {};
  13070. (data.match(tagregex)||[]).forEach(function(x) {
  13071. var y = parsexmltag(x);
  13072. /* 9.3.2.2 OPC_Relationships */
  13073. if (y[0] === '<Relationship') {
  13074. var rel = {}; rel.Type = y.Type; rel.Target = y.Target; rel.Id = y.Id; rel.TargetMode = y.TargetMode;
  13075. var canonictarget = y.TargetMode === 'External' ? y.Target : resolve_path(y.Target, currentFilePath);
  13076. rels[canonictarget] = rel;
  13077. hash[y.Id] = rel;
  13078. }
  13079. });
  13080. rels["!id"] = hash;
  13081. return rels;
  13082. }
  13083. XMLNS.RELS = 'http://schemas.openxmlformats.org/package/2006/relationships';
  13084. var RELS_ROOT = writextag('Relationships', null, {
  13085. //'xmlns:ns0': XMLNS.RELS,
  13086. 'xmlns': XMLNS.RELS
  13087. });
  13088. /* TODO */
  13089. function write_rels(rels) {
  13090. var o = [XML_HEADER, RELS_ROOT];
  13091. keys(rels['!id']).forEach(function(rid) {
  13092. o[o.length] = (writextag('Relationship', null, rels['!id'][rid]));
  13093. });
  13094. if(o.length>2){ o[o.length] = ('</Relationships>'); o[1]=o[1].replace("/>",">"); }
  13095. return o.join("");
  13096. }
  13097. function add_rels(rels, rId, f, type, relobj) {
  13098. if(!relobj) relobj = {};
  13099. if(!rels['!id']) rels['!id'] = {};
  13100. if(rId < 0) for(rId = 1; rels['!id']['rId' + rId]; ++rId){/* empty */}
  13101. relobj.Id = 'rId' + rId;
  13102. relobj.Type = type;
  13103. relobj.Target = f;
  13104. if(relobj.Type == RELS.HLINK) relobj.TargetMode = "External";
  13105. if(rels['!id'][relobj.Id]) throw new Error("Cannot rewrite rId " + rId);
  13106. rels['!id'][relobj.Id] = relobj;
  13107. rels[('/' + relobj.Target).replace("//","/")] = relobj;
  13108. return rId;
  13109. }
  13110. /* Open Document Format for Office Applications (OpenDocument) Version 1.2 */
  13111. /* Part 3 Section 4 Manifest File */
  13112. var CT_ODS = "application/vnd.oasis.opendocument.spreadsheet";
  13113. function parse_manifest(d, opts) {
  13114. var str = xlml_normalize(d);
  13115. var Rn;
  13116. var FEtag;
  13117. while((Rn = xlmlregex.exec(str))) switch(Rn[3]) {
  13118. case 'manifest': break; // 4.2 <manifest:manifest>
  13119. case 'file-entry': // 4.3 <manifest:file-entry>
  13120. FEtag = parsexmltag(Rn[0], false);
  13121. if(FEtag.path == '/' && FEtag.type !== CT_ODS) throw new Error("This OpenDocument is not a spreadsheet");
  13122. break;
  13123. case 'encryption-data': // 4.4 <manifest:encryption-data>
  13124. case 'algorithm': // 4.5 <manifest:algorithm>
  13125. case 'start-key-generation': // 4.6 <manifest:start-key-generation>
  13126. case 'key-derivation': // 4.7 <manifest:key-derivation>
  13127. throw new Error("Unsupported ODS Encryption");
  13128. default: if(opts && opts.WTF) throw Rn;
  13129. }
  13130. }
  13131. function write_manifest(manifest) {
  13132. var o = [XML_HEADER];
  13133. o.push('<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2">\n');
  13134. o.push(' <manifest:file-entry manifest:full-path="/" manifest:version="1.2" manifest:media-type="application/vnd.oasis.opendocument.spreadsheet"/>\n');
  13135. for(var i = 0; i < manifest.length; ++i) o.push(' <manifest:file-entry manifest:full-path="' + manifest[i][0] + '" manifest:media-type="' + manifest[i][1] + '"/>\n');
  13136. o.push('</manifest:manifest>');
  13137. return o.join("");
  13138. }
  13139. /* Part 3 Section 6 Metadata Manifest File */
  13140. function write_rdf_type(file, res, tag) {
  13141. return [
  13142. ' <rdf:Description rdf:about="' + file + '">\n',
  13143. ' <rdf:type rdf:resource="http://docs.oasis-open.org/ns/office/1.2/meta/' + (tag || "odf") + '#' + res + '"/>\n',
  13144. ' </rdf:Description>\n'
  13145. ].join("");
  13146. }
  13147. function write_rdf_has(base, file) {
  13148. return [
  13149. ' <rdf:Description rdf:about="' + base + '">\n',
  13150. ' <ns0:hasPart xmlns:ns0="http://docs.oasis-open.org/ns/office/1.2/meta/pkg#" rdf:resource="' + file + '"/>\n',
  13151. ' </rdf:Description>\n'
  13152. ].join("");
  13153. }
  13154. function write_rdf(rdf) {
  13155. var o = [XML_HEADER];
  13156. o.push('<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">\n');
  13157. for(var i = 0; i != rdf.length; ++i) {
  13158. o.push(write_rdf_type(rdf[i][0], rdf[i][1]));
  13159. o.push(write_rdf_has("",rdf[i][0]));
  13160. }
  13161. o.push(write_rdf_type("","Document", "pkg"));
  13162. o.push('</rdf:RDF>');
  13163. return o.join("");
  13164. }
  13165. /* TODO: pull properties */
  13166. var write_meta_ods = (function() {
  13167. var payload = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><office:document-meta xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xlink="http://www.w3.org/1999/xlink" office:version="1.2"><office:meta><meta:generator>Sheet' + 'JS ' + XLSX.version + '</meta:generator></office:meta></office:document-meta>';
  13168. return function wmo() {
  13169. return payload;
  13170. };
  13171. })();
  13172. /* ECMA-376 Part II 11.1 Core Properties Part */
  13173. /* [MS-OSHARED] 2.3.3.2.[1-2].1 (PIDSI/PIDDSI) */
  13174. var CORE_PROPS = [
  13175. ["cp:category", "Category"],
  13176. ["cp:contentStatus", "ContentStatus"],
  13177. ["cp:keywords", "Keywords"],
  13178. ["cp:lastModifiedBy", "LastAuthor"],
  13179. ["cp:lastPrinted", "LastPrinted"],
  13180. ["cp:revision", "RevNumber"],
  13181. ["cp:version", "Version"],
  13182. ["dc:creator", "Author"],
  13183. ["dc:description", "Comments"],
  13184. ["dc:identifier", "Identifier"],
  13185. ["dc:language", "Language"],
  13186. ["dc:subject", "Subject"],
  13187. ["dc:title", "Title"],
  13188. ["dcterms:created", "CreatedDate", 'date'],
  13189. ["dcterms:modified", "ModifiedDate", 'date']
  13190. ];
  13191. XMLNS.CORE_PROPS = "http://schemas.openxmlformats.org/package/2006/metadata/core-properties";
  13192. RELS.CORE_PROPS = 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties';
  13193. var CORE_PROPS_REGEX = (function() {
  13194. var r = new Array(CORE_PROPS.length);
  13195. for(var i = 0; i < CORE_PROPS.length; ++i) {
  13196. var f = CORE_PROPS[i];
  13197. var g = "(?:"+ f[0].slice(0,f[0].indexOf(":")) +":)"+ f[0].slice(f[0].indexOf(":")+1);
  13198. r[i] = new RegExp("<" + g + "[^>]*>([\\s\\S]*?)<\/" + g + ">");
  13199. }
  13200. return r;
  13201. })();
  13202. function parse_core_props(data) {
  13203. var p = {};
  13204. data = utf8read(data);
  13205. for(var i = 0; i < CORE_PROPS.length; ++i) {
  13206. var f = CORE_PROPS[i], cur = data.match(CORE_PROPS_REGEX[i]);
  13207. if(cur != null && cur.length > 0) p[f[1]] = cur[1];
  13208. if(f[2] === 'date' && p[f[1]]) p[f[1]] = parseDate(p[f[1]]);
  13209. }
  13210. return p;
  13211. }
  13212. var CORE_PROPS_XML_ROOT = writextag('cp:coreProperties', null, {
  13213. //'xmlns': XMLNS.CORE_PROPS,
  13214. 'xmlns:cp': XMLNS.CORE_PROPS,
  13215. 'xmlns:dc': XMLNS.dc,
  13216. 'xmlns:dcterms': XMLNS.dcterms,
  13217. 'xmlns:dcmitype': XMLNS.dcmitype,
  13218. 'xmlns:xsi': XMLNS.xsi
  13219. });
  13220. function cp_doit(f, g, h, o, p) {
  13221. if(p[f] != null || g == null || g === "") return;
  13222. p[f] = g;
  13223. o[o.length] = (h ? writextag(f,g,h) : writetag(f,g));
  13224. }
  13225. function write_core_props(cp, _opts) {
  13226. var opts = _opts || {};
  13227. var o = [XML_HEADER, CORE_PROPS_XML_ROOT], p = {};
  13228. if(!cp && !opts.Props) return o.join("");
  13229. if(cp) {
  13230. if(cp.CreatedDate != null) cp_doit("dcterms:created", typeof cp.CreatedDate === "string" ? cp.CreatedDate : write_w3cdtf(cp.CreatedDate, opts.WTF), {"xsi:type":"dcterms:W3CDTF"}, o, p);
  13231. if(cp.ModifiedDate != null) cp_doit("dcterms:modified", typeof cp.ModifiedDate === "string" ? cp.ModifiedDate : write_w3cdtf(cp.ModifiedDate, opts.WTF), {"xsi:type":"dcterms:W3CDTF"}, o, p);
  13232. }
  13233. for(var i = 0; i != CORE_PROPS.length; ++i) {
  13234. var f = CORE_PROPS[i];
  13235. var v = opts.Props && opts.Props[f[1]] != null ? opts.Props[f[1]] : cp ? cp[f[1]] : null;
  13236. if(v === true) v = "1";
  13237. else if(v === false) v = "0";
  13238. else if(typeof v == "number") v = String(v);
  13239. if(v != null) cp_doit(f[0], v, null, o, p);
  13240. }
  13241. if(o.length>2){ o[o.length] = ('</cp:coreProperties>'); o[1]=o[1].replace("/>",">"); }
  13242. return o.join("");
  13243. }
  13244. /* 15.2.12.3 Extended File Properties Part */
  13245. /* [MS-OSHARED] 2.3.3.2.[1-2].1 (PIDSI/PIDDSI) */
  13246. var EXT_PROPS = [
  13247. ["Application", "Application", "string"],
  13248. ["AppVersion", "AppVersion", "string"],
  13249. ["Company", "Company", "string"],
  13250. ["DocSecurity", "DocSecurity", "string"],
  13251. ["Manager", "Manager", "string"],
  13252. ["HyperlinksChanged", "HyperlinksChanged", "bool"],
  13253. ["SharedDoc", "SharedDoc", "bool"],
  13254. ["LinksUpToDate", "LinksUpToDate", "bool"],
  13255. ["ScaleCrop", "ScaleCrop", "bool"],
  13256. ["HeadingPairs", "HeadingPairs", "raw"],
  13257. ["TitlesOfParts", "TitlesOfParts", "raw"]
  13258. ];
  13259. XMLNS.EXT_PROPS = "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties";
  13260. RELS.EXT_PROPS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties';
  13261. var PseudoPropsPairs = [
  13262. "Worksheets", "SheetNames",
  13263. "NamedRanges", "DefinedNames",
  13264. "Chartsheets", "ChartNames"
  13265. ];
  13266. function load_props_pairs(HP, TOP, props, opts) {
  13267. var v = [];
  13268. if(typeof HP == "string") v = parseVector(HP, opts);
  13269. else for(var j = 0; j < HP.length; ++j) v = v.concat(HP[j].map(function(hp) { return {v:hp}; }));
  13270. var parts = (typeof TOP == "string") ? parseVector(TOP, opts).map(function (x) { return x.v; }) : TOP;
  13271. var idx = 0, len = 0;
  13272. if(parts.length > 0) for(var i = 0; i !== v.length; i += 2) {
  13273. len = +(v[i+1].v);
  13274. switch(v[i].v) {
  13275. case "Worksheets":
  13276. case "工作表":
  13277. case "Листы":
  13278. case "أوراق العمل":
  13279. case "ワークシート":
  13280. case "גליונות עבודה":
  13281. case "Arbeitsblätter":
  13282. case "Çalışma Sayfaları":
  13283. case "Feuilles de calcul":
  13284. case "Fogli di lavoro":
  13285. case "Folhas de cálculo":
  13286. case "Planilhas":
  13287. case "Regneark":
  13288. case "Werkbladen":
  13289. props.Worksheets = len;
  13290. props.SheetNames = parts.slice(idx, idx + len);
  13291. break;
  13292. case "Named Ranges":
  13293. case "名前付き一覧":
  13294. case "Benannte Bereiche":
  13295. case "Navngivne områder":
  13296. props.NamedRanges = len;
  13297. props.DefinedNames = parts.slice(idx, idx + len);
  13298. break;
  13299. case "Charts":
  13300. case "Diagramme":
  13301. props.Chartsheets = len;
  13302. props.ChartNames = parts.slice(idx, idx + len);
  13303. break;
  13304. }
  13305. idx += len;
  13306. }
  13307. }
  13308. function parse_ext_props(data, p, opts) {
  13309. var q = {}; if(!p) p = {};
  13310. data = utf8read(data);
  13311. EXT_PROPS.forEach(function(f) {
  13312. switch(f[2]) {
  13313. case "string": p[f[1]] = (data.match(matchtag(f[0]))||[])[1]; break;
  13314. case "bool": p[f[1]] = (data.match(matchtag(f[0]))||[])[1] === "true"; break;
  13315. case "raw":
  13316. var cur = data.match(new RegExp("<" + f[0] + "[^>]*>([\\s\\S]*?)<\/" + f[0] + ">"));
  13317. if(cur && cur.length > 0) q[f[1]] = cur[1];
  13318. break;
  13319. }
  13320. });
  13321. if(q.HeadingPairs && q.TitlesOfParts) load_props_pairs(q.HeadingPairs, q.TitlesOfParts, p, opts);
  13322. return p;
  13323. }
  13324. var EXT_PROPS_XML_ROOT = writextag('Properties', null, {
  13325. 'xmlns': XMLNS.EXT_PROPS,
  13326. 'xmlns:vt': XMLNS.vt
  13327. });
  13328. function write_ext_props(cp) {
  13329. var o = [], W = writextag;
  13330. if(!cp) cp = {};
  13331. cp.Application = "SheetJS";
  13332. o[o.length] = (XML_HEADER);
  13333. o[o.length] = (EXT_PROPS_XML_ROOT);
  13334. EXT_PROPS.forEach(function(f) {
  13335. if(cp[f[1]] === undefined) return;
  13336. var v;
  13337. switch(f[2]) {
  13338. case 'string': v = String(cp[f[1]]); break;
  13339. case 'bool': v = cp[f[1]] ? 'true' : 'false'; break;
  13340. }
  13341. if(v !== undefined) o[o.length] = (W(f[0], v));
  13342. });
  13343. /* TODO: HeadingPairs, TitlesOfParts */
  13344. o[o.length] = (W('HeadingPairs', W('vt:vector', W('vt:variant', '<vt:lpstr>Worksheets</vt:lpstr>')+W('vt:variant', W('vt:i4', String(cp.Worksheets))), {size:2, baseType:"variant"})));
  13345. o[o.length] = (W('TitlesOfParts', W('vt:vector', cp.SheetNames.map(function(s) { return "<vt:lpstr>" + escapexml(s) + "</vt:lpstr>"; }).join(""), {size: cp.Worksheets, baseType:"lpstr"})));
  13346. if(o.length>2){ o[o.length] = ('</Properties>'); o[1]=o[1].replace("/>",">"); }
  13347. return o.join("");
  13348. }
  13349. /* 15.2.12.2 Custom File Properties Part */
  13350. XMLNS.CUST_PROPS = "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties";
  13351. RELS.CUST_PROPS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties';
  13352. var custregex = /<[^>]+>[^<]*/g;
  13353. function parse_cust_props(data, opts) {
  13354. var p = {}, name = "";
  13355. var m = data.match(custregex);
  13356. if(m) for(var i = 0; i != m.length; ++i) {
  13357. var x = m[i], y = parsexmltag(x);
  13358. switch(y[0]) {
  13359. case '<?xml': break;
  13360. case '<Properties': break;
  13361. case '<property': name = y.name; break;
  13362. case '</property>': name = null; break;
  13363. default: if (x.indexOf('<vt:') === 0) {
  13364. var toks = x.split('>');
  13365. var type = toks[0].slice(4), text = toks[1];
  13366. /* 22.4.2.32 (CT_Variant). Omit the binary types from 22.4 (Variant Types) */
  13367. switch(type) {
  13368. case 'lpstr': case 'bstr': case 'lpwstr':
  13369. p[name] = unescapexml(text);
  13370. break;
  13371. case 'bool':
  13372. p[name] = parsexmlbool(text);
  13373. break;
  13374. case 'i1': case 'i2': case 'i4': case 'i8': case 'int': case 'uint':
  13375. p[name] = parseInt(text, 10);
  13376. break;
  13377. case 'r4': case 'r8': case 'decimal':
  13378. p[name] = parseFloat(text);
  13379. break;
  13380. case 'filetime': case 'date':
  13381. p[name] = parseDate(text);
  13382. break;
  13383. case 'cy': case 'error':
  13384. p[name] = unescapexml(text);
  13385. break;
  13386. default:
  13387. if(type.slice(-1) == '/') break;
  13388. if(opts.WTF && typeof console !== 'undefined') console.warn('Unexpected', x, type, toks);
  13389. }
  13390. } else if(x.slice(0,2) === "</") {/* empty */
  13391. } else if(opts.WTF) throw new Error(x);
  13392. }
  13393. }
  13394. return p;
  13395. }
  13396. var CUST_PROPS_XML_ROOT = writextag('Properties', null, {
  13397. 'xmlns': XMLNS.CUST_PROPS,
  13398. 'xmlns:vt': XMLNS.vt
  13399. });
  13400. function write_cust_props(cp) {
  13401. var o = [XML_HEADER, CUST_PROPS_XML_ROOT];
  13402. if(!cp) return o.join("");
  13403. var pid = 1;
  13404. keys(cp).forEach(function custprop(k) { ++pid;
  13405. o[o.length] = (writextag('property', write_vt(cp[k]), {
  13406. 'fmtid': '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}',
  13407. 'pid': pid,
  13408. 'name': k
  13409. }));
  13410. });
  13411. if(o.length>2){ o[o.length] = '</Properties>'; o[1]=o[1].replace("/>",">"); }
  13412. return o.join("");
  13413. }
  13414. /* Common Name -> XLML Name */
  13415. var XLMLDocPropsMap = {
  13416. Title: 'Title',
  13417. Subject: 'Subject',
  13418. Author: 'Author',
  13419. Keywords: 'Keywords',
  13420. Comments: 'Description',
  13421. LastAuthor: 'LastAuthor',
  13422. RevNumber: 'Revision',
  13423. Application: 'AppName',
  13424. /* TotalTime: 'TotalTime', */
  13425. LastPrinted: 'LastPrinted',
  13426. CreatedDate: 'Created',
  13427. ModifiedDate: 'LastSaved',
  13428. /* Pages */
  13429. /* Words */
  13430. /* Characters */
  13431. Category: 'Category',
  13432. /* PresentationFormat */
  13433. Manager: 'Manager',
  13434. Company: 'Company',
  13435. /* Guid */
  13436. /* HyperlinkBase */
  13437. /* Bytes */
  13438. /* Lines */
  13439. /* Paragraphs */
  13440. /* CharactersWithSpaces */
  13441. AppVersion: 'Version',
  13442. ContentStatus: 'ContentStatus', /* NOTE: missing from schema */
  13443. Identifier: 'Identifier', /* NOTE: missing from schema */
  13444. Language: 'Language' /* NOTE: missing from schema */
  13445. };
  13446. var evert_XLMLDPM = evert(XLMLDocPropsMap);
  13447. function xlml_set_prop(Props, tag, val) {
  13448. tag = evert_XLMLDPM[tag] || tag;
  13449. Props[tag] = val;
  13450. }
  13451. function xlml_write_docprops(Props, opts) {
  13452. var o = [];
  13453. keys(XLMLDocPropsMap).map(function(m) {
  13454. for(var i = 0; i < CORE_PROPS.length; ++i) if(CORE_PROPS[i][1] == m) return CORE_PROPS[i];
  13455. for(i = 0; i < EXT_PROPS.length; ++i) if(EXT_PROPS[i][1] == m) return EXT_PROPS[i];
  13456. throw m;
  13457. }).forEach(function(p) {
  13458. if(Props[p[1]] == null) return;
  13459. var m = opts && opts.Props && opts.Props[p[1]] != null ? opts.Props[p[1]] : Props[p[1]];
  13460. switch(p[2]) {
  13461. case 'date': m = new Date(m).toISOString().replace(/\.\d*Z/,"Z"); break;
  13462. }
  13463. if(typeof m == 'number') m = String(m);
  13464. else if(m === true || m === false) { m = m ? "1" : "0"; }
  13465. else if(m instanceof Date) m = new Date(m).toISOString().replace(/\.\d*Z/,"");
  13466. o.push(writetag(XLMLDocPropsMap[p[1]] || p[1], m));
  13467. });
  13468. return writextag('DocumentProperties', o.join(""), {xmlns:XLMLNS.o });
  13469. }
  13470. function xlml_write_custprops(Props, Custprops) {
  13471. var BLACKLIST = ["Worksheets","SheetNames"];
  13472. var T = 'CustomDocumentProperties';
  13473. var o = [];
  13474. if(Props) keys(Props).forEach(function(k) {
  13475. if(!Props.hasOwnProperty(k)) return;
  13476. for(var i = 0; i < CORE_PROPS.length; ++i) if(k == CORE_PROPS[i][1]) return;
  13477. for(i = 0; i < EXT_PROPS.length; ++i) if(k == EXT_PROPS[i][1]) return;
  13478. for(i = 0; i < BLACKLIST.length; ++i) if(k == BLACKLIST[i]) return;
  13479. var m = Props[k];
  13480. var t = "string";
  13481. if(typeof m == 'number') { t = "float"; m = String(m); }
  13482. else if(m === true || m === false) { t = "boolean"; m = m ? "1" : "0"; }
  13483. else m = String(m);
  13484. o.push(writextag(escapexmltag(k), m, {"dt:dt":t}));
  13485. });
  13486. if(Custprops) keys(Custprops).forEach(function(k) {
  13487. if(!Custprops.hasOwnProperty(k)) return;
  13488. if(Props && Props.hasOwnProperty(k)) return;
  13489. var m = Custprops[k];
  13490. var t = "string";
  13491. if(typeof m == 'number') { t = "float"; m = String(m); }
  13492. else if(m === true || m === false) { t = "boolean"; m = m ? "1" : "0"; }
  13493. else if(m instanceof Date) { t = "dateTime.tz"; m = m.toISOString(); }
  13494. else m = String(m);
  13495. o.push(writextag(escapexmltag(k), m, {"dt:dt":t}));
  13496. });
  13497. return '<' + T + ' xmlns="' + XLMLNS.o + '">' + o.join("") + '</' + T + '>';
  13498. }
  13499. /* [MS-DTYP] 2.3.3 FILETIME */
  13500. /* [MS-OLEDS] 2.1.3 FILETIME (Packet Version) */
  13501. /* [MS-OLEPS] 2.8 FILETIME (Packet Version) */
  13502. function parse_FILETIME(blob) {
  13503. var dwLowDateTime = blob.read_shift(4), dwHighDateTime = blob.read_shift(4);
  13504. return new Date(((dwHighDateTime/1e7*Math.pow(2,32) + dwLowDateTime/1e7) - 11644473600)*1000).toISOString().replace(/\.000/,"");
  13505. }
  13506. function write_FILETIME(time) {
  13507. var date = (typeof time == "string") ? new Date(Date.parse(time)) : time;
  13508. var t = date.getTime() / 1000 + 11644473600;
  13509. var l = t % Math.pow(2,32), h = (t - l) / Math.pow(2,32);
  13510. l *= 1e7; h *= 1e7;
  13511. var w = (l / Math.pow(2,32)) | 0;
  13512. if(w > 0) { l = l % Math.pow(2,32); h += w; }
  13513. var o = new_buf(8); o.write_shift(4, l); o.write_shift(4, h); return o;
  13514. }
  13515. /* [MS-OSHARED] 2.3.3.1.4 Lpstr */
  13516. function parse_lpstr(blob, type, pad) {
  13517. var start = blob.l;
  13518. var str = blob.read_shift(0, 'lpstr-cp');
  13519. if(pad) while((blob.l - start) & 3) ++blob.l;
  13520. return str;
  13521. }
  13522. /* [MS-OSHARED] 2.3.3.1.6 Lpwstr */
  13523. function parse_lpwstr(blob, type, pad) {
  13524. var str = blob.read_shift(0, 'lpwstr');
  13525. if(pad) blob.l += (4 - ((str.length+1) & 3)) & 3;
  13526. return str;
  13527. }
  13528. /* [MS-OSHARED] 2.3.3.1.11 VtString */
  13529. /* [MS-OSHARED] 2.3.3.1.12 VtUnalignedString */
  13530. function parse_VtStringBase(blob, stringType, pad) {
  13531. if(stringType === 0x1F /*VT_LPWSTR*/) return parse_lpwstr(blob);
  13532. return parse_lpstr(blob, stringType, pad);
  13533. }
  13534. function parse_VtString(blob, t, pad) { return parse_VtStringBase(blob, t, pad === false ? 0: 4); }
  13535. function parse_VtUnalignedString(blob, t) { if(!t) throw new Error("VtUnalignedString must have positive length"); return parse_VtStringBase(blob, t, 0); }
  13536. /* [MS-OSHARED] 2.3.3.1.9 VtVecUnalignedLpstrValue */
  13537. function parse_VtVecUnalignedLpstrValue(blob) {
  13538. var length = blob.read_shift(4);
  13539. var ret = [];
  13540. for(var i = 0; i != length; ++i) ret[i] = blob.read_shift(0, 'lpstr-cp').replace(chr0,'');
  13541. return ret;
  13542. }
  13543. /* [MS-OSHARED] 2.3.3.1.10 VtVecUnalignedLpstr */
  13544. function parse_VtVecUnalignedLpstr(blob) {
  13545. return parse_VtVecUnalignedLpstrValue(blob);
  13546. }
  13547. /* [MS-OSHARED] 2.3.3.1.13 VtHeadingPair */
  13548. function parse_VtHeadingPair(blob) {
  13549. var headingString = parse_TypedPropertyValue(blob, VT_USTR);
  13550. var headerParts = parse_TypedPropertyValue(blob, VT_I4);
  13551. return [headingString, headerParts];
  13552. }
  13553. /* [MS-OSHARED] 2.3.3.1.14 VtVecHeadingPairValue */
  13554. function parse_VtVecHeadingPairValue(blob) {
  13555. var cElements = blob.read_shift(4);
  13556. var out = [];
  13557. for(var i = 0; i != cElements / 2; ++i) out.push(parse_VtHeadingPair(blob));
  13558. return out;
  13559. }
  13560. /* [MS-OSHARED] 2.3.3.1.15 VtVecHeadingPair */
  13561. function parse_VtVecHeadingPair(blob) {
  13562. // NOTE: When invoked, wType & padding were already consumed
  13563. return parse_VtVecHeadingPairValue(blob);
  13564. }
  13565. /* [MS-OLEPS] 2.18.1 Dictionary (uses 2.17, 2.16) */
  13566. function parse_dictionary(blob,CodePage) {
  13567. var cnt = blob.read_shift(4);
  13568. var dict = ({});
  13569. for(var j = 0; j != cnt; ++j) {
  13570. var pid = blob.read_shift(4);
  13571. var len = blob.read_shift(4);
  13572. dict[pid] = blob.read_shift(len, (CodePage === 0x4B0 ?'utf16le':'utf8')).replace(chr0,'').replace(chr1,'!');
  13573. if(CodePage === 0x4B0 && (len % 2)) blob.l += 2;
  13574. }
  13575. if(blob.l & 3) blob.l = (blob.l>>2+1)<<2;
  13576. return dict;
  13577. }
  13578. /* [MS-OLEPS] 2.9 BLOB */
  13579. function parse_BLOB(blob) {
  13580. var size = blob.read_shift(4);
  13581. var bytes = blob.slice(blob.l,blob.l+size);
  13582. blob.l += size;
  13583. if((size & 3) > 0) blob.l += (4 - (size & 3)) & 3;
  13584. return bytes;
  13585. }
  13586. /* [MS-OLEPS] 2.11 ClipboardData */
  13587. function parse_ClipboardData(blob) {
  13588. // TODO
  13589. var o = {};
  13590. o.Size = blob.read_shift(4);
  13591. //o.Format = blob.read_shift(4);
  13592. blob.l += o.Size + 3 - (o.Size - 1) % 4;
  13593. return o;
  13594. }
  13595. /* [MS-OLEPS] 2.15 TypedPropertyValue */
  13596. function parse_TypedPropertyValue(blob, type, _opts) {
  13597. var t = blob.read_shift(2), ret, opts = _opts||{};
  13598. blob.l += 2;
  13599. if(type !== VT_VARIANT)
  13600. if(t !== type && VT_CUSTOM.indexOf(type)===-1) throw new Error('Expected type ' + type + ' saw ' + t);
  13601. switch(type === VT_VARIANT ? t : type) {
  13602. case 0x02 /*VT_I2*/: ret = blob.read_shift(2, 'i'); if(!opts.raw) blob.l += 2; return ret;
  13603. case 0x03 /*VT_I4*/: ret = blob.read_shift(4, 'i'); return ret;
  13604. case 0x0B /*VT_BOOL*/: return blob.read_shift(4) !== 0x0;
  13605. case 0x13 /*VT_UI4*/: ret = blob.read_shift(4); return ret;
  13606. case 0x1E /*VT_LPSTR*/: return parse_lpstr(blob, t, 4).replace(chr0,'');
  13607. case 0x1F /*VT_LPWSTR*/: return parse_lpwstr(blob);
  13608. case 0x40 /*VT_FILETIME*/: return parse_FILETIME(blob);
  13609. case 0x41 /*VT_BLOB*/: return parse_BLOB(blob);
  13610. case 0x47 /*VT_CF*/: return parse_ClipboardData(blob);
  13611. case 0x50 /*VT_STRING*/: return parse_VtString(blob, t, !opts.raw).replace(chr0,'');
  13612. case 0x51 /*VT_USTR*/: return parse_VtUnalignedString(blob, t/*, 4*/).replace(chr0,'');
  13613. case 0x100C /*VT_VECTOR|VT_VARIANT*/: return parse_VtVecHeadingPair(blob);
  13614. case 0x101E /*VT_LPSTR*/: return parse_VtVecUnalignedLpstr(blob);
  13615. default: throw new Error("TypedPropertyValue unrecognized type " + type + " " + t);
  13616. }
  13617. }
  13618. function write_TypedPropertyValue(type, value) {
  13619. var o = new_buf(4), p = new_buf(4);
  13620. o.write_shift(4, type == 0x50 ? 0x1F : type);
  13621. switch(type) {
  13622. case 0x03 /*VT_I4*/: p.write_shift(-4, value); break;
  13623. case 0x05 /*VT_I4*/: p = new_buf(8); p.write_shift(8, value, 'f'); break;
  13624. case 0x0B /*VT_BOOL*/: p.write_shift(4, value ? 0x01 : 0x00); break;
  13625. case 0x40 /*VT_FILETIME*/: p = write_FILETIME(value); break;
  13626. case 0x1F /*VT_LPWSTR*/:
  13627. case 0x50 /*VT_STRING*/:
  13628. p = new_buf(4 + 2 * (value.length + 1) + (value.length % 2 ? 0 : 2));
  13629. p.write_shift(4, value.length + 1);
  13630. p.write_shift(0, value, "dbcs");
  13631. while(p.l != p.length) p.write_shift(1, 0);
  13632. break;
  13633. default: throw new Error("TypedPropertyValue unrecognized type " + type + " " + value);
  13634. }
  13635. return bconcat([o, p]);
  13636. }
  13637. /* [MS-OLEPS] 2.20 PropertySet */
  13638. function parse_PropertySet(blob, PIDSI) {
  13639. var start_addr = blob.l;
  13640. var size = blob.read_shift(4);
  13641. var NumProps = blob.read_shift(4);
  13642. var Props = [], i = 0;
  13643. var CodePage = 0;
  13644. var Dictionary = -1, DictObj = ({});
  13645. for(i = 0; i != NumProps; ++i) {
  13646. var PropID = blob.read_shift(4);
  13647. var Offset = blob.read_shift(4);
  13648. Props[i] = [PropID, Offset + start_addr];
  13649. }
  13650. Props.sort(function(x,y) { return x[1] - y[1]; });
  13651. var PropH = {};
  13652. for(i = 0; i != NumProps; ++i) {
  13653. if(blob.l !== Props[i][1]) {
  13654. var fail = true;
  13655. if(i>0 && PIDSI) switch(PIDSI[Props[i-1][0]].t) {
  13656. case 0x02 /*VT_I2*/: if(blob.l+2 === Props[i][1]) { blob.l+=2; fail = false; } break;
  13657. case 0x50 /*VT_STRING*/: if(blob.l <= Props[i][1]) { blob.l=Props[i][1]; fail = false; } break;
  13658. case 0x100C /*VT_VECTOR|VT_VARIANT*/: if(blob.l <= Props[i][1]) { blob.l=Props[i][1]; fail = false; } break;
  13659. }
  13660. if((!PIDSI||i==0) && blob.l <= Props[i][1]) { fail=false; blob.l = Props[i][1]; }
  13661. if(fail) throw new Error("Read Error: Expected address " + Props[i][1] + ' at ' + blob.l + ' :' + i);
  13662. }
  13663. if(PIDSI) {
  13664. var piddsi = PIDSI[Props[i][0]];
  13665. PropH[piddsi.n] = parse_TypedPropertyValue(blob, piddsi.t, {raw:true});
  13666. if(piddsi.p === 'version') PropH[piddsi.n] = String(PropH[piddsi.n] >> 16) + "." + ("0000" + String(PropH[piddsi.n] & 0xFFFF)).slice(-4);
  13667. if(piddsi.n == "CodePage") switch(PropH[piddsi.n]) {
  13668. case 0: PropH[piddsi.n] = 1252;
  13669. /* falls through */
  13670. case 874:
  13671. case 932:
  13672. case 936:
  13673. case 949:
  13674. case 950:
  13675. case 1250:
  13676. case 1251:
  13677. case 1253:
  13678. case 1254:
  13679. case 1255:
  13680. case 1256:
  13681. case 1257:
  13682. case 1258:
  13683. case 10000:
  13684. case 1200:
  13685. case 1201:
  13686. case 1252:
  13687. case 65000: case -536:
  13688. case 65001: case -535:
  13689. set_cp(CodePage = (PropH[piddsi.n]>>>0) & 0xFFFF); break;
  13690. default: throw new Error("Unsupported CodePage: " + PropH[piddsi.n]);
  13691. }
  13692. } else {
  13693. if(Props[i][0] === 0x1) {
  13694. CodePage = PropH.CodePage = (parse_TypedPropertyValue(blob, VT_I2));
  13695. set_cp(CodePage);
  13696. if(Dictionary !== -1) {
  13697. var oldpos = blob.l;
  13698. blob.l = Props[Dictionary][1];
  13699. DictObj = parse_dictionary(blob,CodePage);
  13700. blob.l = oldpos;
  13701. }
  13702. } else if(Props[i][0] === 0) {
  13703. if(CodePage === 0) { Dictionary = i; blob.l = Props[i+1][1]; continue; }
  13704. DictObj = parse_dictionary(blob,CodePage);
  13705. } else {
  13706. var name = DictObj[Props[i][0]];
  13707. var val;
  13708. /* [MS-OSHARED] 2.3.3.2.3.1.2 + PROPVARIANT */
  13709. switch(blob[blob.l]) {
  13710. case 0x41 /*VT_BLOB*/: blob.l += 4; val = parse_BLOB(blob); break;
  13711. case 0x1E /*VT_LPSTR*/: blob.l += 4; val = parse_VtString(blob, blob[blob.l-4]).replace(/\u0000+$/,""); break;
  13712. case 0x1F /*VT_LPWSTR*/: blob.l += 4; val = parse_VtString(blob, blob[blob.l-4]).replace(/\u0000+$/,""); break;
  13713. case 0x03 /*VT_I4*/: blob.l += 4; val = blob.read_shift(4, 'i'); break;
  13714. case 0x13 /*VT_UI4*/: blob.l += 4; val = blob.read_shift(4); break;
  13715. case 0x05 /*VT_R8*/: blob.l += 4; val = blob.read_shift(8, 'f'); break;
  13716. case 0x0B /*VT_BOOL*/: blob.l += 4; val = parsebool(blob, 4); break;
  13717. case 0x40 /*VT_FILETIME*/: blob.l += 4; val = parseDate(parse_FILETIME(blob)); break;
  13718. default: throw new Error("unparsed value: " + blob[blob.l]);
  13719. }
  13720. PropH[name] = val;
  13721. }
  13722. }
  13723. }
  13724. blob.l = start_addr + size; /* step ahead to skip padding */
  13725. return PropH;
  13726. }
  13727. var XLSPSSkip = [ "CodePage", "Thumbnail", "_PID_LINKBASE", "_PID_HLINKS", "SystemIdentifier", "FMTID" ].concat(PseudoPropsPairs);
  13728. function guess_property_type(val) {
  13729. switch(typeof val) {
  13730. case "boolean": return 0x0B;
  13731. case "number": return ((val|0)==val) ? 0x03 : 0x05;
  13732. case "string": return 0x1F;
  13733. case "object": if(val instanceof Date) return 0x40; break;
  13734. }
  13735. return -1;
  13736. }
  13737. function write_PropertySet(entries, RE, PIDSI) {
  13738. var hdr = new_buf(8), piao = [], prop = [];
  13739. var sz = 8, i = 0;
  13740. var pr = new_buf(8), pio = new_buf(8);
  13741. pr.write_shift(4, 0x0002);
  13742. pr.write_shift(4, 0x04B0);
  13743. pio.write_shift(4, 0x0001);
  13744. prop.push(pr); piao.push(pio);
  13745. sz += 8 + pr.length;
  13746. if(!RE) {
  13747. pio = new_buf(8);
  13748. pio.write_shift(4, 0);
  13749. piao.unshift(pio);
  13750. var bufs = [new_buf(4)];
  13751. bufs[0].write_shift(4, entries.length);
  13752. for(i = 0; i < entries.length; ++i) {
  13753. var value = entries[i][0];
  13754. pr = new_buf(4 + 4 + 2 * (value.length + 1) + (value.length % 2 ? 0 : 2));
  13755. pr.write_shift(4, i+2);
  13756. pr.write_shift(4, value.length + 1);
  13757. pr.write_shift(0, value, "dbcs");
  13758. while(pr.l != pr.length) pr.write_shift(1, 0);
  13759. bufs.push(pr);
  13760. }
  13761. pr = bconcat(bufs);
  13762. prop.unshift(pr);
  13763. sz += 8 + pr.length;
  13764. }
  13765. for(i = 0; i < entries.length; ++i) {
  13766. if(RE && !RE[entries[i][0]]) continue;
  13767. if(XLSPSSkip.indexOf(entries[i][0]) > -1) continue;
  13768. if(entries[i][1] == null) continue;
  13769. var val = entries[i][1], idx = 0;
  13770. if(RE) {
  13771. idx = +RE[entries[i][0]];
  13772. var pinfo = (PIDSI)[idx];
  13773. if(pinfo.p == "version" && typeof val == "string") {
  13774. var arr = val.split(".");
  13775. val = ((+arr[0])<<16) + ((+arr[1])||0);
  13776. }
  13777. pr = write_TypedPropertyValue(pinfo.t, val);
  13778. } else {
  13779. var T = guess_property_type(val);
  13780. if(T == -1) { T = 0x1F; val = String(val); }
  13781. pr = write_TypedPropertyValue(T, val);
  13782. }
  13783. prop.push(pr);
  13784. pio = new_buf(8);
  13785. pio.write_shift(4, !RE ? 2+i : idx);
  13786. piao.push(pio);
  13787. sz += 8 + pr.length;
  13788. }
  13789. var w = 8 * (prop.length + 1);
  13790. for(i = 0; i < prop.length; ++i) { piao[i].write_shift(4, w); w += prop[i].length; }
  13791. hdr.write_shift(4, sz);
  13792. hdr.write_shift(4, prop.length);
  13793. return bconcat([hdr].concat(piao).concat(prop));
  13794. }
  13795. /* [MS-OLEPS] 2.21 PropertySetStream */
  13796. function parse_PropertySetStream(file, PIDSI, clsid) {
  13797. var blob = file.content;
  13798. if(!blob) return ({});
  13799. prep_blob(blob, 0);
  13800. var NumSets, FMTID0, FMTID1, Offset0, Offset1 = 0;
  13801. blob.chk('feff', 'Byte Order: ');
  13802. /*var vers = */blob.read_shift(2); // TODO: check version
  13803. var SystemIdentifier = blob.read_shift(4);
  13804. var CLSID = blob.read_shift(16);
  13805. if(CLSID !== CFB.utils.consts.HEADER_CLSID && CLSID !== clsid) throw new Error("Bad PropertySet CLSID " + CLSID);
  13806. NumSets = blob.read_shift(4);
  13807. if(NumSets !== 1 && NumSets !== 2) throw new Error("Unrecognized #Sets: " + NumSets);
  13808. FMTID0 = blob.read_shift(16); Offset0 = blob.read_shift(4);
  13809. if(NumSets === 1 && Offset0 !== blob.l) throw new Error("Length mismatch: " + Offset0 + " !== " + blob.l);
  13810. else if(NumSets === 2) { FMTID1 = blob.read_shift(16); Offset1 = blob.read_shift(4); }
  13811. var PSet0 = parse_PropertySet(blob, PIDSI);
  13812. var rval = ({ SystemIdentifier: SystemIdentifier });
  13813. for(var y in PSet0) rval[y] = PSet0[y];
  13814. //rval.blob = blob;
  13815. rval.FMTID = FMTID0;
  13816. //rval.PSet0 = PSet0;
  13817. if(NumSets === 1) return rval;
  13818. if(Offset1 - blob.l == 2) blob.l += 2;
  13819. if(blob.l !== Offset1) throw new Error("Length mismatch 2: " + blob.l + " !== " + Offset1);
  13820. var PSet1;
  13821. try { PSet1 = parse_PropertySet(blob, null); } catch(e) {/* empty */}
  13822. for(y in PSet1) rval[y] = PSet1[y];
  13823. rval.FMTID = [FMTID0, FMTID1]; // TODO: verify FMTID0/1
  13824. return rval;
  13825. }
  13826. function write_PropertySetStream(entries, clsid, RE, PIDSI, entries2, clsid2) {
  13827. var hdr = new_buf(entries2 ? 68 : 48);
  13828. var bufs = [hdr];
  13829. hdr.write_shift(2, 0xFFFE);
  13830. hdr.write_shift(2, 0x0000); /* TODO: type 1 props */
  13831. hdr.write_shift(4, 0x32363237);
  13832. hdr.write_shift(16, CFB.utils.consts.HEADER_CLSID, "hex");
  13833. hdr.write_shift(4, (entries2 ? 2 : 1));
  13834. hdr.write_shift(16, clsid, "hex");
  13835. hdr.write_shift(4, (entries2 ? 68 : 48));
  13836. var ps0 = write_PropertySet(entries, RE, PIDSI);
  13837. bufs.push(ps0);
  13838. if(entries2) {
  13839. var ps1 = write_PropertySet(entries2, null, null);
  13840. hdr.write_shift(16, clsid2, "hex");
  13841. hdr.write_shift(4, 68 + ps0.length);
  13842. bufs.push(ps1);
  13843. }
  13844. return bconcat(bufs);
  13845. }
  13846. function parsenoop2(blob, length) { blob.read_shift(length); return null; }
  13847. function writezeroes(n, o) { if(!o) o=new_buf(n); for(var j=0; j<n; ++j) o.write_shift(1, 0); return o; }
  13848. function parslurp(blob, length, cb) {
  13849. var arr = [], target = blob.l + length;
  13850. while(blob.l < target) arr.push(cb(blob, target - blob.l));
  13851. if(target !== blob.l) throw new Error("Slurp error");
  13852. return arr;
  13853. }
  13854. function parsebool(blob, length) { return blob.read_shift(length) === 0x1; }
  13855. function writebool(v, o) { if(!o) o=new_buf(2); o.write_shift(2, +!!v); return o; }
  13856. function parseuint16(blob) { return blob.read_shift(2, 'u'); }
  13857. function writeuint16(v, o) { if(!o) o=new_buf(2); o.write_shift(2, v); return o; }
  13858. function parseuint16a(blob, length) { return parslurp(blob,length,parseuint16);}
  13859. /* --- 2.5 Structures --- */
  13860. /* [MS-XLS] 2.5.10 Bes (boolean or error) */
  13861. function parse_Bes(blob) {
  13862. var v = blob.read_shift(1), t = blob.read_shift(1);
  13863. return t === 0x01 ? v : v === 0x01;
  13864. }
  13865. function write_Bes(v, t, o) {
  13866. if(!o) o = new_buf(2);
  13867. o.write_shift(1, +v);
  13868. o.write_shift(1, ((t == 'e') ? 1 : 0));
  13869. return o;
  13870. }
  13871. /* [MS-XLS] 2.5.240 ShortXLUnicodeString */
  13872. function parse_ShortXLUnicodeString(blob, length, opts) {
  13873. var cch = blob.read_shift(opts && opts.biff >= 12 ? 2 : 1);
  13874. var encoding = 'sbcs-cont';
  13875. var cp = current_codepage;
  13876. if(opts && opts.biff >= 8) current_codepage = 1200;
  13877. if(!opts || opts.biff == 8 ) {
  13878. var fHighByte = blob.read_shift(1);
  13879. if(fHighByte) { encoding = 'dbcs-cont'; }
  13880. } else if(opts.biff == 12) {
  13881. encoding = 'wstr';
  13882. }
  13883. if(opts.biff >= 2 && opts.biff <= 5) encoding = 'cpstr';
  13884. var o = cch ? blob.read_shift(cch, encoding) : "";
  13885. current_codepage = cp;
  13886. return o;
  13887. }
  13888. /* 2.5.293 XLUnicodeRichExtendedString */
  13889. function parse_XLUnicodeRichExtendedString(blob) {
  13890. var cp = current_codepage;
  13891. current_codepage = 1200;
  13892. var cch = blob.read_shift(2), flags = blob.read_shift(1);
  13893. var /*fHighByte = flags & 0x1,*/ fExtSt = flags & 0x4, fRichSt = flags & 0x8;
  13894. var width = 1 + (flags & 0x1); // 0x0 -> utf8, 0x1 -> dbcs
  13895. var cRun = 0, cbExtRst;
  13896. var z = {};
  13897. if(fRichSt) cRun = blob.read_shift(2);
  13898. if(fExtSt) cbExtRst = blob.read_shift(4);
  13899. var encoding = width == 2 ? 'dbcs-cont' : 'sbcs-cont';
  13900. var msg = cch === 0 ? "" : blob.read_shift(cch, encoding);
  13901. if(fRichSt) blob.l += 4 * cRun; //TODO: parse this
  13902. if(fExtSt) blob.l += cbExtRst; //TODO: parse this
  13903. z.t = msg;
  13904. if(!fRichSt) { z.raw = "<t>" + z.t + "</t>"; z.r = z.t; }
  13905. current_codepage = cp;
  13906. return z;
  13907. }
  13908. /* 2.5.296 XLUnicodeStringNoCch */
  13909. function parse_XLUnicodeStringNoCch(blob, cch, opts) {
  13910. var retval;
  13911. if(opts) {
  13912. if(opts.biff >= 2 && opts.biff <= 5) return blob.read_shift(cch, 'cpstr');
  13913. if(opts.biff >= 12) return blob.read_shift(cch, 'dbcs-cont');
  13914. }
  13915. var fHighByte = blob.read_shift(1);
  13916. if(fHighByte===0) { retval = blob.read_shift(cch, 'sbcs-cont'); }
  13917. else { retval = blob.read_shift(cch, 'dbcs-cont'); }
  13918. return retval;
  13919. }
  13920. /* 2.5.294 XLUnicodeString */
  13921. function parse_XLUnicodeString(blob, length, opts) {
  13922. var cch = blob.read_shift(opts && opts.biff == 2 ? 1 : 2);
  13923. if(cch === 0) { blob.l++; return ""; }
  13924. return parse_XLUnicodeStringNoCch(blob, cch, opts);
  13925. }
  13926. /* BIFF5 override */
  13927. function parse_XLUnicodeString2(blob, length, opts) {
  13928. if(opts.biff > 5) return parse_XLUnicodeString(blob, length, opts);
  13929. var cch = blob.read_shift(1);
  13930. if(cch === 0) { blob.l++; return ""; }
  13931. return blob.read_shift(cch, (opts.biff <= 4 || !blob.lens ) ? 'cpstr' : 'sbcs-cont');
  13932. }
  13933. /* TODO: BIFF5 and lower, codepage awareness */
  13934. function write_XLUnicodeString(str, opts, o) {
  13935. if(!o) o = new_buf(3 + 2 * str.length);
  13936. o.write_shift(2, str.length);
  13937. o.write_shift(1, 1);
  13938. o.write_shift(31, str, 'utf16le');
  13939. return o;
  13940. }
  13941. /* [MS-XLS] 2.5.61 ControlInfo */
  13942. function parse_ControlInfo(blob) {
  13943. var flags = blob.read_shift(1);
  13944. blob.l++;
  13945. var accel = blob.read_shift(2);
  13946. blob.l += 2;
  13947. return [flags, accel];
  13948. }
  13949. /* [MS-OSHARED] 2.3.7.6 URLMoniker TODO: flags */
  13950. function parse_URLMoniker(blob) {
  13951. var len = blob.read_shift(4), start = blob.l;
  13952. var extra = false;
  13953. if(len > 24) {
  13954. /* look ahead */
  13955. blob.l += len - 24;
  13956. if(blob.read_shift(16) === "795881f43b1d7f48af2c825dc4852763") extra = true;
  13957. blob.l = start;
  13958. }
  13959. var url = blob.read_shift((extra?len-24:len)>>1, 'utf16le').replace(chr0,"");
  13960. if(extra) blob.l += 24;
  13961. return url;
  13962. }
  13963. /* [MS-OSHARED] 2.3.7.8 FileMoniker TODO: all fields */
  13964. function parse_FileMoniker(blob) {
  13965. blob.l += 2; //var cAnti = blob.read_shift(2);
  13966. var ansiPath = blob.read_shift(0, 'lpstr-ansi');
  13967. blob.l += 2; //var endServer = blob.read_shift(2);
  13968. if(blob.read_shift(2) != 0xDEAD) throw new Error("Bad FileMoniker");
  13969. var sz = blob.read_shift(4);
  13970. if(sz === 0) return ansiPath.replace(/\\/g,"/");
  13971. var bytes = blob.read_shift(4);
  13972. if(blob.read_shift(2) != 3) throw new Error("Bad FileMoniker");
  13973. var unicodePath = blob.read_shift(bytes>>1, 'utf16le').replace(chr0,"");
  13974. return unicodePath;
  13975. }
  13976. /* [MS-OSHARED] 2.3.7.2 HyperlinkMoniker TODO: all the monikers */
  13977. function parse_HyperlinkMoniker(blob, length) {
  13978. var clsid = blob.read_shift(16); length -= 16;
  13979. switch(clsid) {
  13980. case "e0c9ea79f9bace118c8200aa004ba90b": return parse_URLMoniker(blob, length);
  13981. case "0303000000000000c000000000000046": return parse_FileMoniker(blob, length);
  13982. default: throw new Error("Unsupported Moniker " + clsid);
  13983. }
  13984. }
  13985. /* [MS-OSHARED] 2.3.7.9 HyperlinkString */
  13986. function parse_HyperlinkString(blob) {
  13987. var len = blob.read_shift(4);
  13988. var o = len > 0 ? blob.read_shift(len, 'utf16le').replace(chr0, "") : "";
  13989. return o;
  13990. }
  13991. /* [MS-OSHARED] 2.3.7.1 Hyperlink Object */
  13992. function parse_Hyperlink(blob, length) {
  13993. var end = blob.l + length;
  13994. var sVer = blob.read_shift(4);
  13995. if(sVer !== 2) throw new Error("Unrecognized streamVersion: " + sVer);
  13996. var flags = blob.read_shift(2);
  13997. blob.l += 2;
  13998. var displayName, targetFrameName, moniker, oleMoniker, Loc="", guid, fileTime;
  13999. if(flags & 0x0010) displayName = parse_HyperlinkString(blob, end - blob.l);
  14000. if(flags & 0x0080) targetFrameName = parse_HyperlinkString(blob, end - blob.l);
  14001. if((flags & 0x0101) === 0x0101) moniker = parse_HyperlinkString(blob, end - blob.l);
  14002. if((flags & 0x0101) === 0x0001) oleMoniker = parse_HyperlinkMoniker(blob, end - blob.l);
  14003. if(flags & 0x0008) Loc = parse_HyperlinkString(blob, end - blob.l);
  14004. if(flags & 0x0020) guid = blob.read_shift(16);
  14005. if(flags & 0x0040) fileTime = parse_FILETIME(blob/*, 8*/);
  14006. blob.l = end;
  14007. var target = targetFrameName||moniker||oleMoniker||"";
  14008. if(target && Loc) target+="#"+Loc;
  14009. if(!target) target = "#" + Loc;
  14010. var out = ({Target:target});
  14011. if(guid) out.guid = guid;
  14012. if(fileTime) out.time = fileTime;
  14013. if(displayName) out.Tooltip = displayName;
  14014. return out;
  14015. }
  14016. function write_Hyperlink(hl) {
  14017. var out = new_buf(512), i = 0;
  14018. var Target = hl.Target;
  14019. var F = Target.indexOf("#") > -1 ? 0x1f : 0x17;
  14020. switch(Target.charAt(0)) { case "#": F=0x1c; break; case ".": F&=~2; break; }
  14021. out.write_shift(4,2); out.write_shift(4, F);
  14022. var data = [8,6815827,6619237,4849780,83]; for(i = 0; i < data.length; ++i) out.write_shift(4, data[i]);
  14023. if(F == 0x1C) {
  14024. Target = Target.slice(1);
  14025. out.write_shift(4, Target.length + 1);
  14026. for(i = 0; i < Target.length; ++i) out.write_shift(2, Target.charCodeAt(i));
  14027. out.write_shift(2, 0);
  14028. } else if(F & 0x02) {
  14029. data = "e0 c9 ea 79 f9 ba ce 11 8c 82 00 aa 00 4b a9 0b".split(" ");
  14030. for(i = 0; i < data.length; ++i) out.write_shift(1, parseInt(data[i], 16));
  14031. out.write_shift(4, 2*(Target.length + 1));
  14032. for(i = 0; i < Target.length; ++i) out.write_shift(2, Target.charCodeAt(i));
  14033. out.write_shift(2, 0);
  14034. } else {
  14035. data = "03 03 00 00 00 00 00 00 c0 00 00 00 00 00 00 46".split(" ");
  14036. for(i = 0; i < data.length; ++i) out.write_shift(1, parseInt(data[i], 16));
  14037. var P = 0;
  14038. while(Target.slice(P*3,P*3+3)=="../"||Target.slice(P*3,P*3+3)=="..\\") ++P;
  14039. out.write_shift(2, P);
  14040. out.write_shift(4, Target.length + 1);
  14041. for(i = 0; i < Target.length; ++i) out.write_shift(1, Target.charCodeAt(i) & 0xFF);
  14042. out.write_shift(1, 0);
  14043. out.write_shift(2, 0xFFFF);
  14044. out.write_shift(2, 0xDEAD);
  14045. for(i = 0; i < 6; ++i) out.write_shift(4, 0);
  14046. }
  14047. return out.slice(0, out.l);
  14048. }
  14049. /* 2.5.178 LongRGBA */
  14050. function parse_LongRGBA(blob) { var r = blob.read_shift(1), g = blob.read_shift(1), b = blob.read_shift(1), a = blob.read_shift(1); return [r,g,b,a]; }
  14051. /* 2.5.177 LongRGB */
  14052. function parse_LongRGB(blob, length) { var x = parse_LongRGBA(blob, length); x[3] = 0; return x; }
  14053. /* [MS-XLS] 2.5.19 */
  14054. function parse_XLSCell(blob) {
  14055. var rw = blob.read_shift(2); // 0-indexed
  14056. var col = blob.read_shift(2);
  14057. var ixfe = blob.read_shift(2);
  14058. return ({r:rw, c:col, ixfe:ixfe});
  14059. }
  14060. function write_XLSCell(R, C, ixfe, o) {
  14061. if(!o) o = new_buf(6);
  14062. o.write_shift(2, R);
  14063. o.write_shift(2, C);
  14064. o.write_shift(2, ixfe||0);
  14065. return o;
  14066. }
  14067. /* [MS-XLS] 2.5.134 */
  14068. function parse_frtHeader(blob) {
  14069. var rt = blob.read_shift(2);
  14070. var flags = blob.read_shift(2); // TODO: parse these flags
  14071. blob.l += 8;
  14072. return {type: rt, flags: flags};
  14073. }
  14074. function parse_OptXLUnicodeString(blob, length, opts) { return length === 0 ? "" : parse_XLUnicodeString2(blob, length, opts); }
  14075. /* [MS-XLS] 2.5.344 */
  14076. function parse_XTI(blob, length, opts) {
  14077. var w = opts.biff > 8 ? 4 : 2;
  14078. var iSupBook = blob.read_shift(w), itabFirst = blob.read_shift(w,'i'), itabLast = blob.read_shift(w,'i');
  14079. return [iSupBook, itabFirst, itabLast];
  14080. }
  14081. /* [MS-XLS] 2.5.218 */
  14082. function parse_RkRec(blob) {
  14083. var ixfe = blob.read_shift(2);
  14084. var RK = parse_RkNumber(blob);
  14085. return [ixfe, RK];
  14086. }
  14087. /* [MS-XLS] 2.5.1 */
  14088. function parse_AddinUdf(blob, length, opts) {
  14089. blob.l += 4; length -= 4;
  14090. var l = blob.l + length;
  14091. var udfName = parse_ShortXLUnicodeString(blob, length, opts);
  14092. var cb = blob.read_shift(2);
  14093. l -= blob.l;
  14094. if(cb !== l) throw new Error("Malformed AddinUdf: padding = " + l + " != " + cb);
  14095. blob.l += cb;
  14096. return udfName;
  14097. }
  14098. /* [MS-XLS] 2.5.209 TODO: Check sizes */
  14099. function parse_Ref8U(blob) {
  14100. var rwFirst = blob.read_shift(2);
  14101. var rwLast = blob.read_shift(2);
  14102. var colFirst = blob.read_shift(2);
  14103. var colLast = blob.read_shift(2);
  14104. return {s:{c:colFirst, r:rwFirst}, e:{c:colLast,r:rwLast}};
  14105. }
  14106. function write_Ref8U(r, o) {
  14107. if(!o) o = new_buf(8);
  14108. o.write_shift(2, r.s.r);
  14109. o.write_shift(2, r.e.r);
  14110. o.write_shift(2, r.s.c);
  14111. o.write_shift(2, r.e.c);
  14112. return o;
  14113. }
  14114. /* [MS-XLS] 2.5.211 */
  14115. function parse_RefU(blob) {
  14116. var rwFirst = blob.read_shift(2);
  14117. var rwLast = blob.read_shift(2);
  14118. var colFirst = blob.read_shift(1);
  14119. var colLast = blob.read_shift(1);
  14120. return {s:{c:colFirst, r:rwFirst}, e:{c:colLast,r:rwLast}};
  14121. }
  14122. /* [MS-XLS] 2.5.207 */
  14123. var parse_Ref = parse_RefU;
  14124. /* [MS-XLS] 2.5.143 */
  14125. function parse_FtCmo(blob) {
  14126. blob.l += 4;
  14127. var ot = blob.read_shift(2);
  14128. var id = blob.read_shift(2);
  14129. var flags = blob.read_shift(2);
  14130. blob.l+=12;
  14131. return [id, ot, flags];
  14132. }
  14133. /* [MS-XLS] 2.5.149 */
  14134. function parse_FtNts(blob) {
  14135. var out = {};
  14136. blob.l += 4;
  14137. blob.l += 16; // GUID TODO
  14138. out.fSharedNote = blob.read_shift(2);
  14139. blob.l += 4;
  14140. return out;
  14141. }
  14142. /* [MS-XLS] 2.5.142 */
  14143. function parse_FtCf(blob) {
  14144. var out = {};
  14145. blob.l += 4;
  14146. blob.cf = blob.read_shift(2);
  14147. return out;
  14148. }
  14149. /* [MS-XLS] 2.5.140 - 2.5.154 and friends */
  14150. function parse_FtSkip(blob) { blob.l += 2; blob.l += blob.read_shift(2); }
  14151. var FtTab = {
  14152. 0x00: parse_FtSkip, /* FtEnd */
  14153. 0x04: parse_FtSkip, /* FtMacro */
  14154. 0x05: parse_FtSkip, /* FtButton */
  14155. 0x06: parse_FtSkip, /* FtGmo */
  14156. 0x07: parse_FtCf, /* FtCf */
  14157. 0x08: parse_FtSkip, /* FtPioGrbit */
  14158. 0x09: parse_FtSkip, /* FtPictFmla */
  14159. 0x0A: parse_FtSkip, /* FtCbls */
  14160. 0x0B: parse_FtSkip, /* FtRbo */
  14161. 0x0C: parse_FtSkip, /* FtSbs */
  14162. 0x0D: parse_FtNts, /* FtNts */
  14163. 0x0E: parse_FtSkip, /* FtSbsFmla */
  14164. 0x0F: parse_FtSkip, /* FtGboData */
  14165. 0x10: parse_FtSkip, /* FtEdoData */
  14166. 0x11: parse_FtSkip, /* FtRboData */
  14167. 0x12: parse_FtSkip, /* FtCblsData */
  14168. 0x13: parse_FtSkip, /* FtLbsData */
  14169. 0x14: parse_FtSkip, /* FtCblsFmla */
  14170. 0x15: parse_FtCmo
  14171. };
  14172. function parse_FtArray(blob, length) {
  14173. var tgt = blob.l + length;
  14174. var fts = [];
  14175. while(blob.l < tgt) {
  14176. var ft = blob.read_shift(2);
  14177. blob.l-=2;
  14178. try {
  14179. fts.push(FtTab[ft](blob, tgt - blob.l));
  14180. } catch(e) { blob.l = tgt; return fts; }
  14181. }
  14182. if(blob.l != tgt) blob.l = tgt; //throw new Error("bad Object Ft-sequence");
  14183. return fts;
  14184. }
  14185. /* --- 2.4 Records --- */
  14186. /* [MS-XLS] 2.4.21 */
  14187. function parse_BOF(blob, length) {
  14188. var o = {BIFFVer:0, dt:0};
  14189. o.BIFFVer = blob.read_shift(2); length -= 2;
  14190. if(length >= 2) { o.dt = blob.read_shift(2); blob.l -= 2; }
  14191. switch(o.BIFFVer) {
  14192. case 0x0600: /* BIFF8 */
  14193. case 0x0500: /* BIFF5 */
  14194. case 0x0400: /* BIFF4 */
  14195. case 0x0300: /* BIFF3 */
  14196. case 0x0200: /* BIFF2 */
  14197. case 0x0002: case 0x0007: /* BIFF2 */
  14198. break;
  14199. default: if(length > 6) throw new Error("Unexpected BIFF Ver " + o.BIFFVer);
  14200. }
  14201. blob.read_shift(length);
  14202. return o;
  14203. }
  14204. function write_BOF(wb, t, o) {
  14205. var h = 0x0600, w = 16;
  14206. switch(o.bookType) {
  14207. case 'biff8': break;
  14208. case 'biff5': h = 0x0500; w = 8; break;
  14209. case 'biff4': h = 0x0004; w = 6; break;
  14210. case 'biff3': h = 0x0003; w = 6; break;
  14211. case 'biff2': h = 0x0002; w = 4; break;
  14212. case 'xla': break;
  14213. default: throw new Error("unsupported BIFF version");
  14214. }
  14215. var out = new_buf(w);
  14216. out.write_shift(2, h);
  14217. out.write_shift(2, t);
  14218. if(w > 4) out.write_shift(2, 0x7262);
  14219. if(w > 6) out.write_shift(2, 0x07CD);
  14220. if(w > 8) {
  14221. out.write_shift(2, 0xC009);
  14222. out.write_shift(2, 0x0001);
  14223. out.write_shift(2, 0x0706);
  14224. out.write_shift(2, 0x0000);
  14225. }
  14226. return out;
  14227. }
  14228. /* [MS-XLS] 2.4.146 */
  14229. function parse_InterfaceHdr(blob, length) {
  14230. if(length === 0) return 0x04b0;
  14231. if((blob.read_shift(2))!==0x04b0){/* empty */}
  14232. return 0x04b0;
  14233. }
  14234. /* [MS-XLS] 2.4.349 */
  14235. function parse_WriteAccess(blob, length, opts) {
  14236. if(opts.enc) { blob.l += length; return ""; }
  14237. var l = blob.l;
  14238. // TODO: make sure XLUnicodeString doesnt overrun
  14239. var UserName = parse_XLUnicodeString2(blob, 0, opts);
  14240. blob.read_shift(length + l - blob.l);
  14241. return UserName;
  14242. }
  14243. function write_WriteAccess(s, opts) {
  14244. var b8 = !opts || opts.biff == 8;
  14245. var o = new_buf(b8 ? 112 : 54);
  14246. o.write_shift(opts.biff == 8 ? 2 : 1, 7);
  14247. if(b8) o.write_shift(1, 0);
  14248. o.write_shift(4, 0x33336853);
  14249. o.write_shift(4, (0x00534A74 | (b8 ? 0 : 0x20000000)));
  14250. while(o.l < o.length) o.write_shift(1, (b8 ? 0 : 32));
  14251. return o;
  14252. }
  14253. /* [MS-XLS] 2.4.351 */
  14254. function parse_WsBool(blob, length, opts) {
  14255. var flags = opts && opts.biff == 8 || length == 2 ? blob.read_shift(2) : (blob.l += length, 0);
  14256. return { fDialog: flags & 0x10 };
  14257. }
  14258. /* [MS-XLS] 2.4.28 */
  14259. function parse_BoundSheet8(blob, length, opts) {
  14260. var pos = blob.read_shift(4);
  14261. var hidden = blob.read_shift(1) & 0x03;
  14262. var dt = blob.read_shift(1);
  14263. switch(dt) {
  14264. case 0: dt = 'Worksheet'; break;
  14265. case 1: dt = 'Macrosheet'; break;
  14266. case 2: dt = 'Chartsheet'; break;
  14267. case 6: dt = 'VBAModule'; break;
  14268. }
  14269. var name = parse_ShortXLUnicodeString(blob, 0, opts);
  14270. if(name.length === 0) name = "Sheet1";
  14271. return { pos:pos, hs:hidden, dt:dt, name:name };
  14272. }
  14273. function write_BoundSheet8(data, opts) {
  14274. var w = (!opts || opts.biff >= 8 ? 2 : 1);
  14275. var o = new_buf(8 + w * data.name.length);
  14276. o.write_shift(4, data.pos);
  14277. o.write_shift(1, data.hs || 0);
  14278. o.write_shift(1, data.dt);
  14279. o.write_shift(1, data.name.length);
  14280. if(opts.biff >= 8) o.write_shift(1, 1);
  14281. o.write_shift(w * data.name.length, data.name, opts.biff < 8 ? 'sbcs' : 'utf16le');
  14282. var out = o.slice(0, o.l);
  14283. out.l = o.l; return out;
  14284. }
  14285. /* [MS-XLS] 2.4.265 TODO */
  14286. function parse_SST(blob, length) {
  14287. var end = blob.l + length;
  14288. var cnt = blob.read_shift(4);
  14289. var ucnt = blob.read_shift(4);
  14290. var strs = ([]);
  14291. for(var i = 0; i != ucnt && blob.l < end; ++i) {
  14292. strs.push(parse_XLUnicodeRichExtendedString(blob));
  14293. }
  14294. strs.Count = cnt; strs.Unique = ucnt;
  14295. return strs;
  14296. }
  14297. /* [MS-XLS] 2.4.107 */
  14298. function parse_ExtSST(blob, length) {
  14299. var extsst = {};
  14300. extsst.dsst = blob.read_shift(2);
  14301. blob.l += length-2;
  14302. return extsst;
  14303. }
  14304. /* [MS-XLS] 2.4.221 TODO: check BIFF2-4 */
  14305. function parse_Row(blob) {
  14306. var z = ({});
  14307. z.r = blob.read_shift(2);
  14308. z.c = blob.read_shift(2);
  14309. z.cnt = blob.read_shift(2) - z.c;
  14310. var miyRw = blob.read_shift(2);
  14311. blob.l += 4; // reserved(2), unused(2)
  14312. var flags = blob.read_shift(1); // various flags
  14313. blob.l += 3; // reserved(8), ixfe(12), flags(4)
  14314. if(flags & 0x07) z.level = flags & 0x07;
  14315. // collapsed: flags & 0x10
  14316. if(flags & 0x20) z.hidden = true;
  14317. if(flags & 0x40) z.hpt = miyRw / 20;
  14318. return z;
  14319. }
  14320. /* [MS-XLS] 2.4.125 */
  14321. function parse_ForceFullCalculation(blob) {
  14322. var header = parse_frtHeader(blob);
  14323. if(header.type != 0x08A3) throw new Error("Invalid Future Record " + header.type);
  14324. var fullcalc = blob.read_shift(4);
  14325. return fullcalc !== 0x0;
  14326. }
  14327. /* [MS-XLS] 2.4.215 rt */
  14328. function parse_RecalcId(blob) {
  14329. blob.read_shift(2);
  14330. return blob.read_shift(4);
  14331. }
  14332. /* [MS-XLS] 2.4.87 */
  14333. function parse_DefaultRowHeight(blob, length, opts) {
  14334. var f = 0;
  14335. if(!(opts && opts.biff == 2)) {
  14336. f = blob.read_shift(2);
  14337. }
  14338. var miyRw = blob.read_shift(2);
  14339. if((opts && opts.biff == 2)) {
  14340. f = 1 - (miyRw >> 15); miyRw &= 0x7fff;
  14341. }
  14342. var fl = {Unsynced:f&1,DyZero:(f&2)>>1,ExAsc:(f&4)>>2,ExDsc:(f&8)>>3};
  14343. return [fl, miyRw];
  14344. }
  14345. /* [MS-XLS] 2.4.345 TODO */
  14346. function parse_Window1(blob) {
  14347. var xWn = blob.read_shift(2), yWn = blob.read_shift(2), dxWn = blob.read_shift(2), dyWn = blob.read_shift(2);
  14348. var flags = blob.read_shift(2), iTabCur = blob.read_shift(2), iTabFirst = blob.read_shift(2);
  14349. var ctabSel = blob.read_shift(2), wTabRatio = blob.read_shift(2);
  14350. return { Pos: [xWn, yWn], Dim: [dxWn, dyWn], Flags: flags, CurTab: iTabCur,
  14351. FirstTab: iTabFirst, Selected: ctabSel, TabRatio: wTabRatio };
  14352. }
  14353. function write_Window1() {
  14354. var o = new_buf(18);
  14355. o.write_shift(2, 0);
  14356. o.write_shift(2, 0);
  14357. o.write_shift(2, 0x7260);
  14358. o.write_shift(2, 0x44c0);
  14359. o.write_shift(2, 0x38);
  14360. o.write_shift(2, 0);
  14361. o.write_shift(2, 0);
  14362. o.write_shift(2, 1);
  14363. o.write_shift(2, 0x01f4);
  14364. return o;
  14365. }
  14366. /* [MS-XLS] 2.4.346 TODO */
  14367. function parse_Window2(blob, length, opts) {
  14368. if(opts && opts.biff >= 2 && opts.biff < 8) return {};
  14369. var f = blob.read_shift(2);
  14370. return { RTL: f & 0x40 };
  14371. }
  14372. function write_Window2(view) {
  14373. var o = new_buf(18), f = 0x6b6;
  14374. if(view && view.RTL) f |= 0x40;
  14375. o.write_shift(2, f);
  14376. o.write_shift(4, 0);
  14377. o.write_shift(4, 64);
  14378. o.write_shift(4, 0);
  14379. o.write_shift(4, 0);
  14380. return o;
  14381. }
  14382. /* [MS-XLS] 2.4.122 TODO */
  14383. function parse_Font(blob, length, opts) {
  14384. var o = {
  14385. dyHeight: blob.read_shift(2),
  14386. fl: blob.read_shift(2)
  14387. };
  14388. switch((opts && opts.biff) || 8) {
  14389. case 2: break;
  14390. case 3: case 4: blob.l += 2; break;
  14391. default: blob.l += 10; break;
  14392. }
  14393. o.name = parse_ShortXLUnicodeString(blob, 0, opts);
  14394. return o;
  14395. }
  14396. function write_Font(data, opts) {
  14397. var name = data.name || "Arial";
  14398. var b5 = (opts && (opts.biff == 5)), w = (b5 ? (15 + name.length) : (16 + 2 * name.length));
  14399. var o = new_buf(w);
  14400. o.write_shift(2, (data.sz || 12) * 20);
  14401. o.write_shift(4, 0);
  14402. o.write_shift(2, 400);
  14403. o.write_shift(4, 0);
  14404. o.write_shift(2, 0);
  14405. o.write_shift(1, name.length);
  14406. if(!b5) o.write_shift(1, 1);
  14407. o.write_shift((b5 ? 1 : 2) * name.length, name, (b5 ? "sbcs" : "utf16le"));
  14408. return o;
  14409. }
  14410. /* [MS-XLS] 2.4.149 */
  14411. function parse_LabelSst(blob) {
  14412. var cell = parse_XLSCell(blob);
  14413. cell.isst = blob.read_shift(4);
  14414. return cell;
  14415. }
  14416. /* [MS-XLS] 2.4.148 */
  14417. function parse_Label(blob, length, opts) {
  14418. var target = blob.l + length;
  14419. var cell = parse_XLSCell(blob, 6);
  14420. if(opts.biff == 2) blob.l++;
  14421. var str = parse_XLUnicodeString(blob, target - blob.l, opts);
  14422. cell.val = str;
  14423. return cell;
  14424. }
  14425. function write_Label(R, C, v, os, opts) {
  14426. var b8 = !opts || opts.biff == 8;
  14427. var o = new_buf(6 + 2 + (+b8) + (1 + b8) * v.length);
  14428. write_XLSCell(R, C, os, o);
  14429. o.write_shift(2, v.length);
  14430. if(b8) o.write_shift(1, 1);
  14431. o.write_shift((1 + b8) * v.length, v, b8 ? 'utf16le' : 'sbcs');
  14432. return o;
  14433. }
  14434. /* [MS-XLS] 2.4.126 Number Formats */
  14435. function parse_Format(blob, length, opts) {
  14436. var numFmtId = blob.read_shift(2);
  14437. var fmtstr = parse_XLUnicodeString2(blob, 0, opts);
  14438. return [numFmtId, fmtstr];
  14439. }
  14440. function write_Format(i, f, opts, o) {
  14441. var b5 = (opts && (opts.biff == 5));
  14442. if(!o) o = new_buf(b5 ? (3 + f.length) : (5 + 2 * f.length));
  14443. o.write_shift(2, i);
  14444. o.write_shift((b5 ? 1 : 2), f.length);
  14445. if(!b5) o.write_shift(1, 1);
  14446. o.write_shift((b5 ? 1 : 2) * f.length, f, (b5 ? 'sbcs' : 'utf16le'));
  14447. var out = (o.length > o.l) ? o.slice(0, o.l) : o;
  14448. if(out.l == null) out.l = out.length;
  14449. return out;
  14450. }
  14451. var parse_BIFF2Format = parse_XLUnicodeString2;
  14452. /* [MS-XLS] 2.4.90 */
  14453. function parse_Dimensions(blob, length, opts) {
  14454. var end = blob.l + length;
  14455. var w = opts.biff == 8 || !opts.biff ? 4 : 2;
  14456. var r = blob.read_shift(w), R = blob.read_shift(w);
  14457. var c = blob.read_shift(2), C = blob.read_shift(2);
  14458. blob.l = end;
  14459. return {s: {r:r, c:c}, e: {r:R, c:C}};
  14460. }
  14461. function write_Dimensions(range, opts) {
  14462. var w = opts.biff == 8 || !opts.biff ? 4 : 2;
  14463. var o = new_buf(2*w + 6);
  14464. o.write_shift(w, range.s.r);
  14465. o.write_shift(w, range.e.r + 1);
  14466. o.write_shift(2, range.s.c);
  14467. o.write_shift(2, range.e.c + 1);
  14468. o.write_shift(2, 0);
  14469. return o;
  14470. }
  14471. /* [MS-XLS] 2.4.220 */
  14472. function parse_RK(blob) {
  14473. var rw = blob.read_shift(2), col = blob.read_shift(2);
  14474. var rkrec = parse_RkRec(blob);
  14475. return {r:rw, c:col, ixfe:rkrec[0], rknum:rkrec[1]};
  14476. }
  14477. /* [MS-XLS] 2.4.175 */
  14478. function parse_MulRk(blob, length) {
  14479. var target = blob.l + length - 2;
  14480. var rw = blob.read_shift(2), col = blob.read_shift(2);
  14481. var rkrecs = [];
  14482. while(blob.l < target) rkrecs.push(parse_RkRec(blob));
  14483. if(blob.l !== target) throw new Error("MulRK read error");
  14484. var lastcol = blob.read_shift(2);
  14485. if(rkrecs.length != lastcol - col + 1) throw new Error("MulRK length mismatch");
  14486. return {r:rw, c:col, C:lastcol, rkrec:rkrecs};
  14487. }
  14488. /* [MS-XLS] 2.4.174 */
  14489. function parse_MulBlank(blob, length) {
  14490. var target = blob.l + length - 2;
  14491. var rw = blob.read_shift(2), col = blob.read_shift(2);
  14492. var ixfes = [];
  14493. while(blob.l < target) ixfes.push(blob.read_shift(2));
  14494. if(blob.l !== target) throw new Error("MulBlank read error");
  14495. var lastcol = blob.read_shift(2);
  14496. if(ixfes.length != lastcol - col + 1) throw new Error("MulBlank length mismatch");
  14497. return {r:rw, c:col, C:lastcol, ixfe:ixfes};
  14498. }
  14499. /* [MS-XLS] 2.5.20 2.5.249 TODO: interpret values here */
  14500. function parse_CellStyleXF(blob, length, style, opts) {
  14501. var o = {};
  14502. var a = blob.read_shift(4), b = blob.read_shift(4);
  14503. var c = blob.read_shift(4), d = blob.read_shift(2);
  14504. o.patternType = XLSFillPattern[c >> 26];
  14505. if(!opts.cellStyles) return o;
  14506. o.alc = a & 0x07;
  14507. o.fWrap = (a >> 3) & 0x01;
  14508. o.alcV = (a >> 4) & 0x07;
  14509. o.fJustLast = (a >> 7) & 0x01;
  14510. o.trot = (a >> 8) & 0xFF;
  14511. o.cIndent = (a >> 16) & 0x0F;
  14512. o.fShrinkToFit = (a >> 20) & 0x01;
  14513. o.iReadOrder = (a >> 22) & 0x02;
  14514. o.fAtrNum = (a >> 26) & 0x01;
  14515. o.fAtrFnt = (a >> 27) & 0x01;
  14516. o.fAtrAlc = (a >> 28) & 0x01;
  14517. o.fAtrBdr = (a >> 29) & 0x01;
  14518. o.fAtrPat = (a >> 30) & 0x01;
  14519. o.fAtrProt = (a >> 31) & 0x01;
  14520. o.dgLeft = b & 0x0F;
  14521. o.dgRight = (b >> 4) & 0x0F;
  14522. o.dgTop = (b >> 8) & 0x0F;
  14523. o.dgBottom = (b >> 12) & 0x0F;
  14524. o.icvLeft = (b >> 16) & 0x7F;
  14525. o.icvRight = (b >> 23) & 0x7F;
  14526. o.grbitDiag = (b >> 30) & 0x03;
  14527. o.icvTop = c & 0x7F;
  14528. o.icvBottom = (c >> 7) & 0x7F;
  14529. o.icvDiag = (c >> 14) & 0x7F;
  14530. o.dgDiag = (c >> 21) & 0x0F;
  14531. o.icvFore = d & 0x7F;
  14532. o.icvBack = (d >> 7) & 0x7F;
  14533. o.fsxButton = (d >> 14) & 0x01;
  14534. return o;
  14535. }
  14536. //function parse_CellXF(blob, length, opts) {return parse_CellStyleXF(blob,length,0, opts);}
  14537. //function parse_StyleXF(blob, length, opts) {return parse_CellStyleXF(blob,length,1, opts);}
  14538. /* [MS-XLS] 2.4.353 TODO: actually do this right */
  14539. function parse_XF(blob, length, opts) {
  14540. var o = {};
  14541. o.ifnt = blob.read_shift(2); o.numFmtId = blob.read_shift(2); o.flags = blob.read_shift(2);
  14542. o.fStyle = (o.flags >> 2) & 0x01;
  14543. length -= 6;
  14544. o.data = parse_CellStyleXF(blob, length, o.fStyle, opts);
  14545. return o;
  14546. }
  14547. function write_XF(data, ixfeP, opts, o) {
  14548. var b5 = (opts && (opts.biff == 5));
  14549. if(!o) o = new_buf(b5 ? 16 : 20);
  14550. o.write_shift(2, 0);
  14551. if(data.style) {
  14552. o.write_shift(2, (data.numFmtId||0));
  14553. o.write_shift(2, 0xFFF4);
  14554. } else {
  14555. o.write_shift(2, (data.numFmtId||0));
  14556. o.write_shift(2, (ixfeP<<4));
  14557. }
  14558. o.write_shift(4, 0);
  14559. o.write_shift(4, 0);
  14560. if(!b5) o.write_shift(4, 0);
  14561. o.write_shift(2, 0);
  14562. return o;
  14563. }
  14564. /* [MS-XLS] 2.4.134 */
  14565. function parse_Guts(blob) {
  14566. blob.l += 4;
  14567. var out = [blob.read_shift(2), blob.read_shift(2)];
  14568. if(out[0] !== 0) out[0]--;
  14569. if(out[1] !== 0) out[1]--;
  14570. if(out[0] > 7 || out[1] > 7) throw new Error("Bad Gutters: " + out.join("|"));
  14571. return out;
  14572. }
  14573. function write_Guts(guts) {
  14574. var o = new_buf(8);
  14575. o.write_shift(4, 0);
  14576. o.write_shift(2, guts[0] ? guts[0] + 1 : 0);
  14577. o.write_shift(2, guts[1] ? guts[1] + 1 : 0);
  14578. return o;
  14579. }
  14580. /* [MS-XLS] 2.4.24 */
  14581. function parse_BoolErr(blob, length, opts) {
  14582. var cell = parse_XLSCell(blob, 6);
  14583. if(opts.biff == 2) ++blob.l;
  14584. var val = parse_Bes(blob, 2);
  14585. cell.val = val;
  14586. cell.t = (val === true || val === false) ? 'b' : 'e';
  14587. return cell;
  14588. }
  14589. function write_BoolErr(R, C, v, os, opts, t) {
  14590. var o = new_buf(8);
  14591. write_XLSCell(R, C, os, o);
  14592. write_Bes(v, t, o);
  14593. return o;
  14594. }
  14595. /* [MS-XLS] 2.4.180 Number */
  14596. function parse_Number(blob) {
  14597. var cell = parse_XLSCell(blob, 6);
  14598. var xnum = parse_Xnum(blob, 8);
  14599. cell.val = xnum;
  14600. return cell;
  14601. }
  14602. function write_Number(R, C, v, os) {
  14603. var o = new_buf(14);
  14604. write_XLSCell(R, C, os, o);
  14605. write_Xnum(v, o);
  14606. return o;
  14607. }
  14608. var parse_XLHeaderFooter = parse_OptXLUnicodeString; // TODO: parse 2.4.136
  14609. /* [MS-XLS] 2.4.271 */
  14610. function parse_SupBook(blob, length, opts) {
  14611. var end = blob.l + length;
  14612. var ctab = blob.read_shift(2);
  14613. var cch = blob.read_shift(2);
  14614. opts.sbcch = cch;
  14615. if(cch == 0x0401 || cch == 0x3A01) return [cch, ctab];
  14616. if(cch < 0x01 || cch >0xff) throw new Error("Unexpected SupBook type: "+cch);
  14617. var virtPath = parse_XLUnicodeStringNoCch(blob, cch);
  14618. /* TODO: 2.5.277 Virtual Path */
  14619. var rgst = [];
  14620. while(end > blob.l) rgst.push(parse_XLUnicodeString(blob));
  14621. return [cch, ctab, virtPath, rgst];
  14622. }
  14623. /* [MS-XLS] 2.4.105 TODO */
  14624. function parse_ExternName(blob, length, opts) {
  14625. var flags = blob.read_shift(2);
  14626. var body;
  14627. var o = ({
  14628. fBuiltIn: flags & 0x01,
  14629. fWantAdvise: (flags >>> 1) & 0x01,
  14630. fWantPict: (flags >>> 2) & 0x01,
  14631. fOle: (flags >>> 3) & 0x01,
  14632. fOleLink: (flags >>> 4) & 0x01,
  14633. cf: (flags >>> 5) & 0x3FF,
  14634. fIcon: flags >>> 15 & 0x01
  14635. });
  14636. if(opts.sbcch === 0x3A01) body = parse_AddinUdf(blob, length-2, opts);
  14637. //else throw new Error("unsupported SupBook cch: " + opts.sbcch);
  14638. o.body = body || blob.read_shift(length-2);
  14639. if(typeof body === "string") o.Name = body;
  14640. return o;
  14641. }
  14642. /* [MS-XLS] 2.4.150 TODO */
  14643. var XLSLblBuiltIn = [
  14644. "_xlnm.Consolidate_Area",
  14645. "_xlnm.Auto_Open",
  14646. "_xlnm.Auto_Close",
  14647. "_xlnm.Extract",
  14648. "_xlnm.Database",
  14649. "_xlnm.Criteria",
  14650. "_xlnm.Print_Area",
  14651. "_xlnm.Print_Titles",
  14652. "_xlnm.Recorder",
  14653. "_xlnm.Data_Form",
  14654. "_xlnm.Auto_Activate",
  14655. "_xlnm.Auto_Deactivate",
  14656. "_xlnm.Sheet_Title",
  14657. "_xlnm._FilterDatabase"
  14658. ];
  14659. function parse_Lbl(blob, length, opts) {
  14660. var target = blob.l + length;
  14661. var flags = blob.read_shift(2);
  14662. var chKey = blob.read_shift(1);
  14663. var cch = blob.read_shift(1);
  14664. var cce = blob.read_shift(opts && opts.biff == 2 ? 1 : 2);
  14665. var itab = 0;
  14666. if(!opts || opts.biff >= 5) {
  14667. if(opts.biff != 5) blob.l += 2;
  14668. itab = blob.read_shift(2);
  14669. if(opts.biff == 5) blob.l += 2;
  14670. blob.l += 4;
  14671. }
  14672. var name = parse_XLUnicodeStringNoCch(blob, cch, opts);
  14673. if(flags & 0x20) name = XLSLblBuiltIn[name.charCodeAt(0)];
  14674. var npflen = target - blob.l; if(opts && opts.biff == 2) --npflen;
  14675. var rgce = target == blob.l || cce === 0 ? [] : parse_NameParsedFormula(blob, npflen, opts, cce);
  14676. return {
  14677. chKey: chKey,
  14678. Name: name,
  14679. itab: itab,
  14680. rgce: rgce
  14681. };
  14682. }
  14683. /* [MS-XLS] 2.4.106 TODO: verify filename encoding */
  14684. function parse_ExternSheet(blob, length, opts) {
  14685. if(opts.biff < 8) return parse_BIFF5ExternSheet(blob, length, opts);
  14686. var o = [], target = blob.l + length, len = blob.read_shift(opts.biff > 8 ? 4 : 2);
  14687. while(len-- !== 0) o.push(parse_XTI(blob, opts.biff > 8 ? 12 : 6, opts));
  14688. // [iSupBook, itabFirst, itabLast];
  14689. if(blob.l != target) throw new Error("Bad ExternSheet: " + blob.l + " != " + target);
  14690. return o;
  14691. }
  14692. function parse_BIFF5ExternSheet(blob, length, opts) {
  14693. if(blob[blob.l + 1] == 0x03) blob[blob.l]++;
  14694. var o = parse_ShortXLUnicodeString(blob, length, opts);
  14695. return o.charCodeAt(0) == 0x03 ? o.slice(1) : o;
  14696. }
  14697. /* [MS-XLS] 2.4.176 TODO: check older biff */
  14698. function parse_NameCmt(blob, length, opts) {
  14699. if(opts.biff < 8) { blob.l += length; return; }
  14700. var cchName = blob.read_shift(2);
  14701. var cchComment = blob.read_shift(2);
  14702. var name = parse_XLUnicodeStringNoCch(blob, cchName, opts);
  14703. var comment = parse_XLUnicodeStringNoCch(blob, cchComment, opts);
  14704. return [name, comment];
  14705. }
  14706. /* [MS-XLS] 2.4.260 */
  14707. function parse_ShrFmla(blob, length, opts) {
  14708. var ref = parse_RefU(blob, 6);
  14709. blob.l++;
  14710. var cUse = blob.read_shift(1);
  14711. length -= 8;
  14712. return [parse_SharedParsedFormula(blob, length, opts), cUse, ref];
  14713. }
  14714. /* [MS-XLS] 2.4.4 TODO */
  14715. function parse_Array(blob, length, opts) {
  14716. var ref = parse_Ref(blob, 6);
  14717. /* TODO: fAlwaysCalc */
  14718. switch(opts.biff) {
  14719. case 2: blob.l ++; length -= 7; break;
  14720. case 3: case 4: blob.l += 2; length -= 8; break;
  14721. default: blob.l += 6; length -= 12;
  14722. }
  14723. return [ref, parse_ArrayParsedFormula(blob, length, opts, ref)];
  14724. }
  14725. /* [MS-XLS] 2.4.173 */
  14726. function parse_MTRSettings(blob) {
  14727. var fMTREnabled = blob.read_shift(4) !== 0x00;
  14728. var fUserSetThreadCount = blob.read_shift(4) !== 0x00;
  14729. var cUserThreadCount = blob.read_shift(4);
  14730. return [fMTREnabled, fUserSetThreadCount, cUserThreadCount];
  14731. }
  14732. /* [MS-XLS] 2.5.186 TODO: BIFF5 */
  14733. function parse_NoteSh(blob, length, opts) {
  14734. if(opts.biff < 8) return;
  14735. var row = blob.read_shift(2), col = blob.read_shift(2);
  14736. var flags = blob.read_shift(2), idObj = blob.read_shift(2);
  14737. var stAuthor = parse_XLUnicodeString2(blob, 0, opts);
  14738. if(opts.biff < 8) blob.read_shift(1);
  14739. return [{r:row,c:col}, stAuthor, idObj, flags];
  14740. }
  14741. /* [MS-XLS] 2.4.179 */
  14742. function parse_Note(blob, length, opts) {
  14743. /* TODO: Support revisions */
  14744. return parse_NoteSh(blob, length, opts);
  14745. }
  14746. /* [MS-XLS] 2.4.168 */
  14747. function parse_MergeCells(blob, length) {
  14748. var merges = [];
  14749. var cmcs = blob.read_shift(2);
  14750. while (cmcs--) merges.push(parse_Ref8U(blob,length));
  14751. return merges;
  14752. }
  14753. function write_MergeCells(merges) {
  14754. var o = new_buf(2 + merges.length * 8);
  14755. o.write_shift(2, merges.length);
  14756. for(var i = 0; i < merges.length; ++i) write_Ref8U(merges[i], o);
  14757. return o;
  14758. }
  14759. /* [MS-XLS] 2.4.181 TODO: parse all the things! */
  14760. function parse_Obj(blob, length, opts) {
  14761. if(opts && opts.biff < 8) return parse_BIFF5Obj(blob, length, opts);
  14762. var cmo = parse_FtCmo(blob, 22); // id, ot, flags
  14763. var fts = parse_FtArray(blob, length-22, cmo[1]);
  14764. return { cmo: cmo, ft:fts };
  14765. }
  14766. /* from older spec */
  14767. var parse_BIFF5OT = [];
  14768. parse_BIFF5OT[0x08] = function(blob, length) {
  14769. var tgt = blob.l + length;
  14770. blob.l += 10; // todo
  14771. var cf = blob.read_shift(2);
  14772. blob.l += 4;
  14773. blob.l += 2; //var cbPictFmla = blob.read_shift(2);
  14774. blob.l += 2;
  14775. blob.l += 2; //var grbit = blob.read_shift(2);
  14776. blob.l += 4;
  14777. var cchName = blob.read_shift(1);
  14778. blob.l += cchName; // TODO: stName
  14779. blob.l = tgt; // TODO: fmla
  14780. return { fmt:cf };
  14781. };
  14782. function parse_BIFF5Obj(blob, length, opts) {
  14783. blob.l += 4; //var cnt = blob.read_shift(4);
  14784. var ot = blob.read_shift(2);
  14785. var id = blob.read_shift(2);
  14786. var grbit = blob.read_shift(2);
  14787. blob.l += 2; //var colL = blob.read_shift(2);
  14788. blob.l += 2; //var dxL = blob.read_shift(2);
  14789. blob.l += 2; //var rwT = blob.read_shift(2);
  14790. blob.l += 2; //var dyT = blob.read_shift(2);
  14791. blob.l += 2; //var colR = blob.read_shift(2);
  14792. blob.l += 2; //var dxR = blob.read_shift(2);
  14793. blob.l += 2; //var rwB = blob.read_shift(2);
  14794. blob.l += 2; //var dyB = blob.read_shift(2);
  14795. blob.l += 2; //var cbMacro = blob.read_shift(2);
  14796. blob.l += 6;
  14797. length -= 36;
  14798. var fts = [];
  14799. fts.push((parse_BIFF5OT[ot]||parsenoop)(blob, length, opts));
  14800. return { cmo: [id, ot, grbit], ft:fts };
  14801. }
  14802. /* [MS-XLS] 2.4.329 TODO: parse properly */
  14803. function parse_TxO(blob, length, opts) {
  14804. var s = blob.l;
  14805. var texts = "";
  14806. try {
  14807. blob.l += 4;
  14808. var ot = (opts.lastobj||{cmo:[0,0]}).cmo[1];
  14809. var controlInfo; // eslint-disable-line no-unused-vars
  14810. if([0,5,7,11,12,14].indexOf(ot) == -1) blob.l += 6;
  14811. else controlInfo = parse_ControlInfo(blob, 6, opts);
  14812. var cchText = blob.read_shift(2);
  14813. /*var cbRuns = */blob.read_shift(2);
  14814. /*var ifntEmpty = */parseuint16(blob, 2);
  14815. var len = blob.read_shift(2);
  14816. blob.l += len;
  14817. //var fmla = parse_ObjFmla(blob, s + length - blob.l);
  14818. for(var i = 1; i < blob.lens.length-1; ++i) {
  14819. if(blob.l-s != blob.lens[i]) throw new Error("TxO: bad continue record");
  14820. var hdr = blob[blob.l];
  14821. var t = parse_XLUnicodeStringNoCch(blob, blob.lens[i+1]-blob.lens[i]-1);
  14822. texts += t;
  14823. if(texts.length >= (hdr ? cchText : 2*cchText)) break;
  14824. }
  14825. if(texts.length !== cchText && texts.length !== cchText*2) {
  14826. throw new Error("cchText: " + cchText + " != " + texts.length);
  14827. }
  14828. blob.l = s + length;
  14829. /* [MS-XLS] 2.5.272 TxORuns */
  14830. // var rgTxoRuns = [];
  14831. // for(var j = 0; j != cbRuns/8-1; ++j) blob.l += 8;
  14832. // var cchText2 = blob.read_shift(2);
  14833. // if(cchText2 !== cchText) throw new Error("TxOLastRun mismatch: " + cchText2 + " " + cchText);
  14834. // blob.l += 6;
  14835. // if(s + length != blob.l) throw new Error("TxO " + (s + length) + ", at " + blob.l);
  14836. return { t: texts };
  14837. } catch(e) { blob.l = s + length; return { t: texts }; }
  14838. }
  14839. /* [MS-XLS] 2.4.140 */
  14840. function parse_HLink(blob, length) {
  14841. var ref = parse_Ref8U(blob, 8);
  14842. blob.l += 16; /* CLSID */
  14843. var hlink = parse_Hyperlink(blob, length-24);
  14844. return [ref, hlink];
  14845. }
  14846. function write_HLink(hl) {
  14847. var O = new_buf(24);
  14848. var ref = decode_cell(hl[0]);
  14849. O.write_shift(2, ref.r); O.write_shift(2, ref.r);
  14850. O.write_shift(2, ref.c); O.write_shift(2, ref.c);
  14851. var clsid = "d0 c9 ea 79 f9 ba ce 11 8c 82 00 aa 00 4b a9 0b".split(" ");
  14852. for(var i = 0; i < 16; ++i) O.write_shift(1, parseInt(clsid[i], 16));
  14853. return bconcat([O, write_Hyperlink(hl[1])]);
  14854. }
  14855. /* [MS-XLS] 2.4.141 */
  14856. function parse_HLinkTooltip(blob, length) {
  14857. blob.read_shift(2);
  14858. var ref = parse_Ref8U(blob, 8);
  14859. var wzTooltip = blob.read_shift((length-10)/2, 'dbcs-cont');
  14860. wzTooltip = wzTooltip.replace(chr0,"");
  14861. return [ref, wzTooltip];
  14862. }
  14863. function write_HLinkTooltip(hl) {
  14864. var TT = hl[1].Tooltip;
  14865. var O = new_buf(10 + 2 * (TT.length + 1));
  14866. O.write_shift(2, 0x0800);
  14867. var ref = decode_cell(hl[0]);
  14868. O.write_shift(2, ref.r); O.write_shift(2, ref.r);
  14869. O.write_shift(2, ref.c); O.write_shift(2, ref.c);
  14870. for(var i = 0; i < TT.length; ++i) O.write_shift(2, TT.charCodeAt(i));
  14871. O.write_shift(2, 0);
  14872. return O;
  14873. }
  14874. /* [MS-XLS] 2.4.63 */
  14875. function parse_Country(blob) {
  14876. var o = [0,0], d;
  14877. d = blob.read_shift(2); o[0] = CountryEnum[d] || d;
  14878. d = blob.read_shift(2); o[1] = CountryEnum[d] || d;
  14879. return o;
  14880. }
  14881. function write_Country(o) {
  14882. if(!o) o = new_buf(4);
  14883. o.write_shift(2, 0x01);
  14884. o.write_shift(2, 0x01);
  14885. return o;
  14886. }
  14887. /* [MS-XLS] 2.4.50 ClrtClient */
  14888. function parse_ClrtClient(blob) {
  14889. var ccv = blob.read_shift(2);
  14890. var o = [];
  14891. while(ccv-->0) o.push(parse_LongRGB(blob, 8));
  14892. return o;
  14893. }
  14894. /* [MS-XLS] 2.4.188 */
  14895. function parse_Palette(blob) {
  14896. var ccv = blob.read_shift(2);
  14897. var o = [];
  14898. while(ccv-->0) o.push(parse_LongRGB(blob, 8));
  14899. return o;
  14900. }
  14901. /* [MS-XLS] 2.4.354 */
  14902. function parse_XFCRC(blob) {
  14903. blob.l += 2;
  14904. var o = {cxfs:0, crc:0};
  14905. o.cxfs = blob.read_shift(2);
  14906. o.crc = blob.read_shift(4);
  14907. return o;
  14908. }
  14909. /* [MS-XLS] 2.4.53 TODO: parse flags */
  14910. /* [MS-XLSB] 2.4.323 TODO: parse flags */
  14911. function parse_ColInfo(blob, length, opts) {
  14912. if(!opts.cellStyles) return parsenoop(blob, length);
  14913. var w = opts && opts.biff >= 12 ? 4 : 2;
  14914. var colFirst = blob.read_shift(w);
  14915. var colLast = blob.read_shift(w);
  14916. var coldx = blob.read_shift(w);
  14917. var ixfe = blob.read_shift(w);
  14918. var flags = blob.read_shift(2);
  14919. if(w == 2) blob.l += 2;
  14920. return {s:colFirst, e:colLast, w:coldx, ixfe:ixfe, flags:flags};
  14921. }
  14922. /* [MS-XLS] 2.4.257 */
  14923. function parse_Setup(blob, length) {
  14924. var o = {};
  14925. if(length < 32) return o;
  14926. blob.l += 16;
  14927. o.header = parse_Xnum(blob, 8);
  14928. o.footer = parse_Xnum(blob, 8);
  14929. blob.l += 2;
  14930. return o;
  14931. }
  14932. /* [MS-XLS] 2.4.261 */
  14933. function parse_ShtProps(blob, length, opts) {
  14934. var def = {area:false};
  14935. if(opts.biff != 5) { blob.l += length; return def; }
  14936. var d = blob.read_shift(1); blob.l += 3;
  14937. if((d & 0x10)) def.area = true;
  14938. return def;
  14939. }
  14940. /* [MS-XLS] 2.4.241 */
  14941. function write_RRTabId(n) {
  14942. var out = new_buf(2 * n);
  14943. for(var i = 0; i < n; ++i) out.write_shift(2, i+1);
  14944. return out;
  14945. }
  14946. var parse_Blank = parse_XLSCell; /* [MS-XLS] 2.4.20 Just the cell */
  14947. var parse_Scl = parseuint16a; /* [MS-XLS] 2.4.247 num, den */
  14948. var parse_String = parse_XLUnicodeString; /* [MS-XLS] 2.4.268 */
  14949. /* --- Specific to versions before BIFF8 --- */
  14950. function parse_ImData(blob) {
  14951. var cf = blob.read_shift(2);
  14952. var env = blob.read_shift(2);
  14953. var lcb = blob.read_shift(4);
  14954. var o = {fmt:cf, env:env, len:lcb, data:blob.slice(blob.l,blob.l+lcb)};
  14955. blob.l += lcb;
  14956. return o;
  14957. }
  14958. /* BIFF2_??? where ??? is the name from [XLS] */
  14959. function parse_BIFF2STR(blob, length, opts) {
  14960. var cell = parse_XLSCell(blob, 6);
  14961. ++blob.l;
  14962. var str = parse_XLUnicodeString2(blob, length-7, opts);
  14963. cell.t = 'str';
  14964. cell.val = str;
  14965. return cell;
  14966. }
  14967. function parse_BIFF2NUM(blob) {
  14968. var cell = parse_XLSCell(blob, 6);
  14969. ++blob.l;
  14970. var num = parse_Xnum(blob, 8);
  14971. cell.t = 'n';
  14972. cell.val = num;
  14973. return cell;
  14974. }
  14975. function write_BIFF2NUM(r, c, val) {
  14976. var out = new_buf(15);
  14977. write_BIFF2Cell(out, r, c);
  14978. out.write_shift(8, val, 'f');
  14979. return out;
  14980. }
  14981. function parse_BIFF2INT(blob) {
  14982. var cell = parse_XLSCell(blob, 6);
  14983. ++blob.l;
  14984. var num = blob.read_shift(2);
  14985. cell.t = 'n';
  14986. cell.val = num;
  14987. return cell;
  14988. }
  14989. function write_BIFF2INT(r, c, val) {
  14990. var out = new_buf(9);
  14991. write_BIFF2Cell(out, r, c);
  14992. out.write_shift(2, val);
  14993. return out;
  14994. }
  14995. function parse_BIFF2STRING(blob) {
  14996. var cch = blob.read_shift(1);
  14997. if(cch === 0) { blob.l++; return ""; }
  14998. return blob.read_shift(cch, 'sbcs-cont');
  14999. }
  15000. /* TODO: convert to BIFF8 font struct */
  15001. function parse_BIFF2FONTXTRA(blob, length) {
  15002. blob.l += 6; // unknown
  15003. blob.l += 2; // font weight "bls"
  15004. blob.l += 1; // charset
  15005. blob.l += 3; // unknown
  15006. blob.l += 1; // font family
  15007. blob.l += length - 13;
  15008. }
  15009. /* TODO: parse rich text runs */
  15010. function parse_RString(blob, length, opts) {
  15011. var end = blob.l + length;
  15012. var cell = parse_XLSCell(blob, 6);
  15013. var cch = blob.read_shift(2);
  15014. var str = parse_XLUnicodeStringNoCch(blob, cch, opts);
  15015. blob.l = end;
  15016. cell.t = 'str';
  15017. cell.val = str;
  15018. return cell;
  15019. }
  15020. /* from js-harb (C) 2014-present SheetJS */
  15021. var DBF = (function() {
  15022. var dbf_codepage_map = {
  15023. /* Code Pages Supported by Visual FoxPro */
  15024. 0x01: 437, 0x02: 850,
  15025. 0x03: 1252, 0x04: 10000,
  15026. 0x64: 852, 0x65: 866,
  15027. 0x66: 865, 0x67: 861,
  15028. 0x68: 895, 0x69: 620,
  15029. 0x6A: 737, 0x6B: 857,
  15030. 0x78: 950, 0x79: 949,
  15031. 0x7A: 936, 0x7B: 932,
  15032. 0x7C: 874, 0x7D: 1255,
  15033. 0x7E: 1256, 0x96: 10007,
  15034. 0x97: 10029, 0x98: 10006,
  15035. 0xC8: 1250, 0xC9: 1251,
  15036. 0xCA: 1254, 0xCB: 1253,
  15037. /* shapefile DBF extension */
  15038. 0x00: 20127, 0x08: 865,
  15039. 0x09: 437, 0x0A: 850,
  15040. 0x0B: 437, 0x0D: 437,
  15041. 0x0E: 850, 0x0F: 437,
  15042. 0x10: 850, 0x11: 437,
  15043. 0x12: 850, 0x13: 932,
  15044. 0x14: 850, 0x15: 437,
  15045. 0x16: 850, 0x17: 865,
  15046. 0x18: 437, 0x19: 437,
  15047. 0x1A: 850, 0x1B: 437,
  15048. 0x1C: 863, 0x1D: 850,
  15049. 0x1F: 852, 0x22: 852,
  15050. 0x23: 852, 0x24: 860,
  15051. 0x25: 850, 0x26: 866,
  15052. 0x37: 850, 0x40: 852,
  15053. 0x4D: 936, 0x4E: 949,
  15054. 0x4F: 950, 0x50: 874,
  15055. 0x57: 1252, 0x58: 1252,
  15056. 0x59: 1252,
  15057. 0xFF: 16969
  15058. };
  15059. /* TODO: find an actual specification */
  15060. function dbf_to_aoa(buf, opts) {
  15061. var out = [];
  15062. /* TODO: browser based */
  15063. var d = (new_raw_buf(1));
  15064. switch(opts.type) {
  15065. case 'base64': d = s2a(Base64.decode(buf)); break;
  15066. case 'binary': d = s2a(buf); break;
  15067. case 'buffer':
  15068. case 'array': d = buf; break;
  15069. }
  15070. prep_blob(d, 0);
  15071. /* header */
  15072. var ft = d.read_shift(1);
  15073. var memo = false;
  15074. var vfp = false, l7 = false;
  15075. switch(ft) {
  15076. case 0x02: case 0x03: break;
  15077. case 0x30: vfp = true; memo = true; break;
  15078. case 0x31: vfp = true; break;
  15079. case 0x83: memo = true; break;
  15080. case 0x8B: memo = true; break;
  15081. case 0x8C: memo = true; l7 = true; break;
  15082. case 0xF5: memo = true; break;
  15083. default: throw new Error("DBF Unsupported Version: " + ft.toString(16));
  15084. }
  15085. var /*filedate = new Date(),*/ nrow = 0, fpos = 0;
  15086. if(ft == 0x02) nrow = d.read_shift(2);
  15087. /*filedate = new Date(d.read_shift(1) + 1900, d.read_shift(1) - 1, d.read_shift(1));*/d.l += 3;
  15088. if(ft != 0x02) nrow = d.read_shift(4);
  15089. if(ft != 0x02) fpos = d.read_shift(2);
  15090. var rlen = d.read_shift(2);
  15091. var /*flags = 0,*/ current_cp = 1252;
  15092. if(ft != 0x02) {
  15093. d.l+=16;
  15094. /*flags = */d.read_shift(1);
  15095. //if(memo && ((flags & 0x02) === 0)) throw new Error("DBF Flags " + flags.toString(16) + " ft " + ft.toString(16));
  15096. /* codepage present in FoxPro */
  15097. if(d[d.l] !== 0) current_cp = dbf_codepage_map[d[d.l]];
  15098. d.l+=1;
  15099. d.l+=2;
  15100. }
  15101. if(l7) d.l += 36;
  15102. var fields = [], field = ({});
  15103. var hend = fpos - 10 - (vfp ? 264 : 0), ww = l7 ? 32 : 11;
  15104. while(ft == 0x02 ? d.l < d.length && d[d.l] != 0x0d: d.l < hend) {
  15105. field = ({});
  15106. field.name = cptable.utils.decode(current_cp, d.slice(d.l, d.l+ww)).replace(/[\u0000\r\n].*$/g,"");
  15107. d.l += ww;
  15108. field.type = String.fromCharCode(d.read_shift(1));
  15109. if(ft != 0x02 && !l7) field.offset = d.read_shift(4);
  15110. field.len = d.read_shift(1);
  15111. if(ft == 0x02) field.offset = d.read_shift(2);
  15112. field.dec = d.read_shift(1);
  15113. if(field.name.length) fields.push(field);
  15114. if(ft != 0x02) d.l += l7 ? 13 : 14;
  15115. switch(field.type) {
  15116. case 'B': // VFP Double
  15117. if((!vfp || field.len != 8) && opts.WTF) console.log('Skipping ' + field.name + ':' + field.type);
  15118. break;
  15119. case 'G': // General
  15120. case 'P': // Picture
  15121. if(opts.WTF) console.log('Skipping ' + field.name + ':' + field.type);
  15122. break;
  15123. case 'C': // character
  15124. case 'D': // date
  15125. case 'F': // floating point
  15126. case 'I': // long
  15127. case 'L': // boolean
  15128. case 'M': // memo
  15129. case 'N': // number
  15130. case 'O': // double
  15131. case 'T': // datetime
  15132. case 'Y': // currency
  15133. case '0': // VFP _NullFlags
  15134. case '@': // timestamp
  15135. case '+': // autoincrement
  15136. break;
  15137. default: throw new Error('Unknown Field Type: ' + field.type);
  15138. }
  15139. }
  15140. if(d[d.l] !== 0x0D) d.l = fpos-1;
  15141. else if(ft == 0x02) d.l = 0x209;
  15142. if(ft != 0x02) {
  15143. if(d.read_shift(1) !== 0x0D) throw new Error("DBF Terminator not found " + d.l + " " + d[d.l]);
  15144. d.l = fpos;
  15145. }
  15146. /* data */
  15147. var R = 0, C = 0;
  15148. out[0] = [];
  15149. for(C = 0; C != fields.length; ++C) out[0][C] = fields[C].name;
  15150. while(nrow-- > 0) {
  15151. if(d[d.l] === 0x2A) { d.l+=rlen; continue; }
  15152. ++d.l;
  15153. out[++R] = []; C = 0;
  15154. for(C = 0; C != fields.length; ++C) {
  15155. var dd = d.slice(d.l, d.l+fields[C].len); d.l+=fields[C].len;
  15156. prep_blob(dd, 0);
  15157. var s = cptable.utils.decode(current_cp, dd);
  15158. switch(fields[C].type) {
  15159. case 'C':
  15160. out[R][C] = cptable.utils.decode(current_cp, dd);
  15161. out[R][C] = out[R][C].trim();
  15162. break;
  15163. case 'D':
  15164. if(s.length === 8) out[R][C] = new Date(+s.slice(0,4), +s.slice(4,6)-1, +s.slice(6,8));
  15165. else out[R][C] = s;
  15166. break;
  15167. case 'F': out[R][C] = parseFloat(s.trim()); break;
  15168. case '+': case 'I': out[R][C] = l7 ? dd.read_shift(-4, 'i') ^ 0x80000000 : dd.read_shift(4, 'i'); break;
  15169. case 'L': switch(s.toUpperCase()) {
  15170. case 'Y': case 'T': out[R][C] = true; break;
  15171. case 'N': case 'F': out[R][C] = false; break;
  15172. case ' ': case '?': out[R][C] = false; break; /* NOTE: technically uninitialized */
  15173. default: throw new Error("DBF Unrecognized L:|" + s + "|");
  15174. } break;
  15175. case 'M': /* TODO: handle memo files */
  15176. if(!memo) throw new Error("DBF Unexpected MEMO for type " + ft.toString(16));
  15177. out[R][C] = "##MEMO##" + (l7 ? parseInt(s.trim(), 10): dd.read_shift(4));
  15178. break;
  15179. case 'N': out[R][C] = +s.replace(/\u0000/g,"").trim(); break;
  15180. case '@': out[R][C] = new Date(dd.read_shift(-8, 'f') - 0x388317533400); break;
  15181. case 'T': out[R][C] = new Date((dd.read_shift(4) - 0x253D8C) * 0x5265C00 + dd.read_shift(4)); break;
  15182. case 'Y': out[R][C] = dd.read_shift(4,'i')/1e4; break;
  15183. case 'O': out[R][C] = -dd.read_shift(-8, 'f'); break;
  15184. case 'B': if(vfp && fields[C].len == 8) { out[R][C] = dd.read_shift(8,'f'); break; }
  15185. /* falls through */
  15186. case 'G': case 'P': dd.l += fields[C].len; break;
  15187. case '0':
  15188. if(fields[C].name === '_NullFlags') break;
  15189. /* falls through */
  15190. default: throw new Error("DBF Unsupported data type " + fields[C].type);
  15191. }
  15192. }
  15193. }
  15194. if(ft != 0x02) if(d.l < d.length && d[d.l++] != 0x1A) throw new Error("DBF EOF Marker missing " + (d.l-1) + " of " + d.length + " " + d[d.l-1].toString(16));
  15195. if(opts && opts.sheetRows) out = out.slice(0, opts.sheetRows);
  15196. return out;
  15197. }
  15198. function dbf_to_sheet(buf, opts) {
  15199. var o = opts || {};
  15200. if(!o.dateNF) o.dateNF = "yyyymmdd";
  15201. return aoa_to_sheet(dbf_to_aoa(buf, o), o);
  15202. }
  15203. function dbf_to_workbook(buf, opts) {
  15204. try { return sheet_to_workbook(dbf_to_sheet(buf, opts), opts); }
  15205. catch(e) { if(opts && opts.WTF) throw e; }
  15206. return ({SheetNames:[],Sheets:{}});
  15207. }
  15208. var _RLEN = { 'B': 8, 'C': 250, 'L': 1, 'D': 8, '?': 0, '': 0 };
  15209. function sheet_to_dbf(ws, opts) {
  15210. var o = opts || {};
  15211. if(o.type == "string") throw new Error("Cannot write DBF to JS string");
  15212. var ba = buf_array();
  15213. var aoa = sheet_to_json(ws, {header:1, cellDates:true});
  15214. var headers = aoa[0], data = aoa.slice(1);
  15215. var i = 0, j = 0, hcnt = 0, rlen = 1;
  15216. for(i = 0; i < headers.length; ++i) {
  15217. if(i == null) continue;
  15218. ++hcnt;
  15219. if(typeof headers[i] === 'number') headers[i] = headers[i].toString(10);
  15220. if(typeof headers[i] !== 'string') throw new Error("DBF Invalid column name " + headers[i] + " |" + (typeof headers[i]) + "|");
  15221. if(headers.indexOf(headers[i]) !== i) for(j=0; j<1024;++j)
  15222. if(headers.indexOf(headers[i] + "_" + j) == -1) { headers[i] += "_" + j; break; }
  15223. }
  15224. var range = safe_decode_range(ws['!ref']);
  15225. var coltypes = [];
  15226. for(i = 0; i <= range.e.c - range.s.c; ++i) {
  15227. var col = [];
  15228. for(j=0; j < data.length; ++j) {
  15229. if(data[j][i] != null) col.push(data[j][i]);
  15230. }
  15231. if(col.length == 0 || headers[i] == null) { coltypes[i] = '?'; continue; }
  15232. var guess = '', _guess = '';
  15233. for(j = 0; j < col.length; ++j) {
  15234. switch(typeof col[j]) {
  15235. /* TODO: check if L2 compat is desired */
  15236. case 'number': _guess = 'B'; break;
  15237. case 'string': _guess = 'C'; break;
  15238. case 'boolean': _guess = 'L'; break;
  15239. case 'object': _guess = col[j] instanceof Date ? 'D' : 'C'; break;
  15240. default: _guess = 'C';
  15241. }
  15242. guess = guess && guess != _guess ? 'C' : _guess;
  15243. if(guess == 'C') break;
  15244. }
  15245. rlen += _RLEN[guess] || 0;
  15246. coltypes[i] = guess;
  15247. }
  15248. var h = ba.next(32);
  15249. h.write_shift(4, 0x13021130);
  15250. h.write_shift(4, data.length);
  15251. h.write_shift(2, 296 + 32 * hcnt);
  15252. h.write_shift(2, rlen);
  15253. for(i=0; i < 4; ++i) h.write_shift(4, 0);
  15254. h.write_shift(4, 0x00000300); // TODO: CP
  15255. for(i = 0, j = 0; i < headers.length; ++i) {
  15256. if(headers[i] == null) continue;
  15257. var hf = ba.next(32);
  15258. var _f = (headers[i].slice(-10) + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00").slice(0, 11);
  15259. hf.write_shift(1, _f, "sbcs");
  15260. hf.write_shift(1, coltypes[i] == '?' ? 'C' : coltypes[i], "sbcs");
  15261. hf.write_shift(4, j);
  15262. hf.write_shift(1, _RLEN[coltypes[i]] || 0);
  15263. hf.write_shift(1, 0);
  15264. hf.write_shift(1, 0x02);
  15265. hf.write_shift(4, 0);
  15266. hf.write_shift(1, 0);
  15267. hf.write_shift(4, 0);
  15268. hf.write_shift(4, 0);
  15269. j += _RLEN[coltypes[i]] || 0;
  15270. }
  15271. var hb = ba.next(264);
  15272. hb.write_shift(4, 0x0000000D);
  15273. for(i=0; i < 65;++i) hb.write_shift(4, 0x00000000);
  15274. for(i=0; i < data.length; ++i) {
  15275. var rout = ba.next(rlen);
  15276. rout.write_shift(1, 0);
  15277. for(j=0; j<headers.length; ++j) {
  15278. if(headers[j] == null) continue;
  15279. switch(coltypes[j]) {
  15280. case 'L': rout.write_shift(1, data[i][j] == null ? 0x3F : data[i][j] ? 0x54 : 0x46); break;
  15281. case 'B': rout.write_shift(8, data[i][j]||0, 'f'); break;
  15282. case 'D':
  15283. if(!data[i][j]) rout.write_shift(8, "00000000", "sbcs");
  15284. else {
  15285. rout.write_shift(4, ("0000"+data[i][j].getFullYear()).slice(-4), "sbcs");
  15286. rout.write_shift(2, ("00"+(data[i][j].getMonth()+1)).slice(-2), "sbcs");
  15287. rout.write_shift(2, ("00"+data[i][j].getDate()).slice(-2), "sbcs");
  15288. } break;
  15289. case 'C':
  15290. var _s = String(data[i][j]||"");
  15291. rout.write_shift(1, _s, "sbcs");
  15292. for(hcnt=0; hcnt < 250-_s.length; ++hcnt) rout.write_shift(1, 0x20); break;
  15293. }
  15294. }
  15295. // data
  15296. }
  15297. ba.next(1).write_shift(1, 0x1A);
  15298. return ba.end();
  15299. }
  15300. return {
  15301. to_workbook: dbf_to_workbook,
  15302. to_sheet: dbf_to_sheet,
  15303. from_sheet: sheet_to_dbf
  15304. };
  15305. })();
  15306. var SYLK = (function() {
  15307. /* TODO: find an actual specification */
  15308. function sylk_to_aoa(d, opts) {
  15309. switch(opts.type) {
  15310. case 'base64': return sylk_to_aoa_str(Base64.decode(d), opts);
  15311. case 'binary': return sylk_to_aoa_str(d, opts);
  15312. case 'buffer': return sylk_to_aoa_str(d.toString('binary'), opts);
  15313. case 'array': return sylk_to_aoa_str(cc2str(d), opts);
  15314. }
  15315. throw new Error("Unrecognized type " + opts.type);
  15316. }
  15317. function sylk_to_aoa_str(str, opts) {
  15318. var records = str.split(/[\n\r]+/), R = -1, C = -1, ri = 0, rj = 0, arr = [];
  15319. var formats = [];
  15320. var next_cell_format = null;
  15321. var sht = {}, rowinfo = [], colinfo = [], cw = [];
  15322. var Mval = 0, j;
  15323. for (; ri !== records.length; ++ri) {
  15324. Mval = 0;
  15325. var rstr=records[ri].trim();
  15326. var record=rstr.replace(/;;/g, "\u0001").split(";").map(function(x) { return x.replace(/\u0001/g, ";"); });
  15327. var RT=record[0], val;
  15328. if(rstr.length > 0) switch(RT) {
  15329. case 'ID': break; /* header */
  15330. case 'E': break; /* EOF */
  15331. case 'B': break; /* dimensions */
  15332. case 'O': break; /* options? */
  15333. case 'P':
  15334. if(record[1].charAt(0) == 'P')
  15335. formats.push(rstr.slice(3).replace(/;;/g, ";"));
  15336. break;
  15337. case 'C':
  15338. var C_seen_K = false, C_seen_X = false;
  15339. for(rj=1; rj<record.length; ++rj) switch(record[rj].charAt(0)) {
  15340. case 'X': C = parseInt(record[rj].slice(1))-1; C_seen_X = true; break;
  15341. case 'Y':
  15342. R = parseInt(record[rj].slice(1))-1; if(!C_seen_X) C = 0;
  15343. for(j = arr.length; j <= R; ++j) arr[j] = [];
  15344. break;
  15345. case 'K':
  15346. val = record[rj].slice(1);
  15347. if(val.charAt(0) === '"') val = val.slice(1,val.length - 1);
  15348. else if(val === 'TRUE') val = true;
  15349. else if(val === 'FALSE') val = false;
  15350. else if(!isNaN(fuzzynum(val))) {
  15351. val = fuzzynum(val);
  15352. if(next_cell_format !== null && SSF.is_date(next_cell_format)) val = numdate(val);
  15353. } else if(!isNaN(fuzzydate(val).getDate())) {
  15354. val = parseDate(val);
  15355. }
  15356. if(typeof cptable !== 'undefined' && typeof val == "string" && ((opts||{}).type != "string") && (opts||{}).codepage) val = cptable.utils.decode(opts.codepage, val);
  15357. C_seen_K = true;
  15358. break;
  15359. case 'E':
  15360. var formula = rc_to_a1(record[rj].slice(1), {r:R,c:C});
  15361. arr[R][C] = [arr[R][C], formula];
  15362. break;
  15363. default: if(opts && opts.WTF) throw new Error("SYLK bad record " + rstr);
  15364. }
  15365. if(C_seen_K) { arr[R][C] = val; next_cell_format = null; }
  15366. break;
  15367. case 'F':
  15368. var F_seen = 0;
  15369. for(rj=1; rj<record.length; ++rj) switch(record[rj].charAt(0)) {
  15370. case 'X': C = parseInt(record[rj].slice(1))-1; ++F_seen; break;
  15371. case 'Y':
  15372. R = parseInt(record[rj].slice(1))-1; /*C = 0;*/
  15373. for(j = arr.length; j <= R; ++j) arr[j] = [];
  15374. break;
  15375. case 'M': Mval = parseInt(record[rj].slice(1)) / 20; break;
  15376. case 'F': break; /* ??? */
  15377. case 'G': break; /* hide grid */
  15378. case 'P':
  15379. next_cell_format = formats[parseInt(record[rj].slice(1))];
  15380. break;
  15381. case 'S': break; /* cell style */
  15382. case 'D': break; /* column */
  15383. case 'N': break; /* font */
  15384. case 'W':
  15385. cw = record[rj].slice(1).split(" ");
  15386. for(j = parseInt(cw[0], 10); j <= parseInt(cw[1], 10); ++j) {
  15387. Mval = parseInt(cw[2], 10);
  15388. colinfo[j-1] = Mval === 0 ? {hidden:true}: {wch:Mval}; process_col(colinfo[j-1]);
  15389. } break;
  15390. case 'C': /* default column format */
  15391. C = parseInt(record[rj].slice(1))-1;
  15392. if(!colinfo[C]) colinfo[C] = {};
  15393. break;
  15394. case 'R': /* row properties */
  15395. R = parseInt(record[rj].slice(1))-1;
  15396. if(!rowinfo[R]) rowinfo[R] = {};
  15397. if(Mval > 0) { rowinfo[R].hpt = Mval; rowinfo[R].hpx = pt2px(Mval); }
  15398. else if(Mval === 0) rowinfo[R].hidden = true;
  15399. break;
  15400. default: if(opts && opts.WTF) throw new Error("SYLK bad record " + rstr);
  15401. }
  15402. if(F_seen < 1) next_cell_format = null; break;
  15403. default: if(opts && opts.WTF) throw new Error("SYLK bad record " + rstr);
  15404. }
  15405. }
  15406. if(rowinfo.length > 0) sht['!rows'] = rowinfo;
  15407. if(colinfo.length > 0) sht['!cols'] = colinfo;
  15408. if(opts && opts.sheetRows) arr = arr.slice(0, opts.sheetRows);
  15409. return [arr, sht];
  15410. }
  15411. function sylk_to_sheet(d, opts) {
  15412. var aoasht = sylk_to_aoa(d, opts);
  15413. var aoa = aoasht[0], ws = aoasht[1];
  15414. var o = aoa_to_sheet(aoa, opts);
  15415. keys(ws).forEach(function(k) { o[k] = ws[k]; });
  15416. return o;
  15417. }
  15418. function sylk_to_workbook(d, opts) { return sheet_to_workbook(sylk_to_sheet(d, opts), opts); }
  15419. function write_ws_cell_sylk(cell, ws, R, C) {
  15420. var o = "C;Y" + (R+1) + ";X" + (C+1) + ";K";
  15421. switch(cell.t) {
  15422. case 'n':
  15423. o += (cell.v||0);
  15424. if(cell.f && !cell.F) o += ";E" + a1_to_rc(cell.f, {r:R, c:C}); break;
  15425. case 'b': o += cell.v ? "TRUE" : "FALSE"; break;
  15426. case 'e': o += cell.w || cell.v; break;
  15427. case 'd': o += '"' + (cell.w || cell.v) + '"'; break;
  15428. case 's': o += '"' + cell.v.replace(/"/g,"") + '"'; break;
  15429. }
  15430. return o;
  15431. }
  15432. function write_ws_cols_sylk(out, cols) {
  15433. cols.forEach(function(col, i) {
  15434. var rec = "F;W" + (i+1) + " " + (i+1) + " ";
  15435. if(col.hidden) rec += "0";
  15436. else {
  15437. if(typeof col.width == 'number') col.wpx = width2px(col.width);
  15438. if(typeof col.wpx == 'number') col.wch = px2char(col.wpx);
  15439. if(typeof col.wch == 'number') rec += Math.round(col.wch);
  15440. }
  15441. if(rec.charAt(rec.length - 1) != " ") out.push(rec);
  15442. });
  15443. }
  15444. function write_ws_rows_sylk(out, rows) {
  15445. rows.forEach(function(row, i) {
  15446. var rec = "F;";
  15447. if(row.hidden) rec += "M0;";
  15448. else if(row.hpt) rec += "M" + 20 * row.hpt + ";";
  15449. else if(row.hpx) rec += "M" + 20 * px2pt(row.hpx) + ";";
  15450. if(rec.length > 2) out.push(rec + "R" + (i+1));
  15451. });
  15452. }
  15453. function sheet_to_sylk(ws, opts) {
  15454. var preamble = ["ID;PWXL;N;E"], o = [];
  15455. var r = safe_decode_range(ws['!ref']), cell;
  15456. var dense = Array.isArray(ws);
  15457. var RS = "\r\n";
  15458. preamble.push("P;PGeneral");
  15459. preamble.push("F;P0;DG0G8;M255");
  15460. if(ws['!cols']) write_ws_cols_sylk(preamble, ws['!cols']);
  15461. if(ws['!rows']) write_ws_rows_sylk(preamble, ws['!rows']);
  15462. preamble.push("B;Y" + (r.e.r - r.s.r + 1) + ";X" + (r.e.c - r.s.c + 1) + ";D" + [r.s.c,r.s.r,r.e.c,r.e.r].join(" "));
  15463. for(var R = r.s.r; R <= r.e.r; ++R) {
  15464. for(var C = r.s.c; C <= r.e.c; ++C) {
  15465. var coord = encode_cell({r:R,c:C});
  15466. cell = dense ? (ws[R]||[])[C]: ws[coord];
  15467. if(!cell || (cell.v == null && (!cell.f || cell.F))) continue;
  15468. o.push(write_ws_cell_sylk(cell, ws, R, C, opts));
  15469. }
  15470. }
  15471. return preamble.join(RS) + RS + o.join(RS) + RS + "E" + RS;
  15472. }
  15473. return {
  15474. to_workbook: sylk_to_workbook,
  15475. to_sheet: sylk_to_sheet,
  15476. from_sheet: sheet_to_sylk
  15477. };
  15478. })();
  15479. var DIF = (function() {
  15480. function dif_to_aoa(d, opts) {
  15481. switch(opts.type) {
  15482. case 'base64': return dif_to_aoa_str(Base64.decode(d), opts);
  15483. case 'binary': return dif_to_aoa_str(d, opts);
  15484. case 'buffer': return dif_to_aoa_str(d.toString('binary'), opts);
  15485. case 'array': return dif_to_aoa_str(cc2str(d), opts);
  15486. }
  15487. throw new Error("Unrecognized type " + opts.type);
  15488. }
  15489. function dif_to_aoa_str(str, opts) {
  15490. var records = str.split('\n'), R = -1, C = -1, ri = 0, arr = [];
  15491. for (; ri !== records.length; ++ri) {
  15492. if (records[ri].trim() === 'BOT') { arr[++R] = []; C = 0; continue; }
  15493. if (R < 0) continue;
  15494. var metadata = records[ri].trim().split(",");
  15495. var type = metadata[0], value = metadata[1];
  15496. ++ri;
  15497. var data = records[ri].trim();
  15498. switch (+type) {
  15499. case -1:
  15500. if (data === 'BOT') { arr[++R] = []; C = 0; continue; }
  15501. else if (data !== 'EOD') throw new Error("Unrecognized DIF special command " + data);
  15502. break;
  15503. case 0:
  15504. if(data === 'TRUE') arr[R][C] = true;
  15505. else if(data === 'FALSE') arr[R][C] = false;
  15506. else if(!isNaN(fuzzynum(value))) arr[R][C] = fuzzynum(value);
  15507. else if(!isNaN(fuzzydate(value).getDate())) arr[R][C] = parseDate(value);
  15508. else arr[R][C] = value;
  15509. ++C; break;
  15510. case 1:
  15511. data = data.slice(1,data.length-1);
  15512. arr[R][C++] = data !== '' ? data : null;
  15513. break;
  15514. }
  15515. if (data === 'EOD') break;
  15516. }
  15517. if(opts && opts.sheetRows) arr = arr.slice(0, opts.sheetRows);
  15518. return arr;
  15519. }
  15520. function dif_to_sheet(str, opts) { return aoa_to_sheet(dif_to_aoa(str, opts), opts); }
  15521. function dif_to_workbook(str, opts) { return sheet_to_workbook(dif_to_sheet(str, opts), opts); }
  15522. var sheet_to_dif = (function() {
  15523. var push_field = function pf(o, topic, v, n, s) {
  15524. o.push(topic);
  15525. o.push(v + "," + n);
  15526. o.push('"' + s.replace(/"/g,'""') + '"');
  15527. };
  15528. var push_value = function po(o, type, v, s) {
  15529. o.push(type + "," + v);
  15530. o.push(type == 1 ? '"' + s.replace(/"/g,'""') + '"' : s);
  15531. };
  15532. return function sheet_to_dif(ws) {
  15533. var o = [];
  15534. var r = safe_decode_range(ws['!ref']), cell;
  15535. var dense = Array.isArray(ws);
  15536. push_field(o, "TABLE", 0, 1, "sheetjs");
  15537. push_field(o, "VECTORS", 0, r.e.r - r.s.r + 1,"");
  15538. push_field(o, "TUPLES", 0, r.e.c - r.s.c + 1,"");
  15539. push_field(o, "DATA", 0, 0,"");
  15540. for(var R = r.s.r; R <= r.e.r; ++R) {
  15541. push_value(o, -1, 0, "BOT");
  15542. for(var C = r.s.c; C <= r.e.c; ++C) {
  15543. var coord = encode_cell({r:R,c:C});
  15544. cell = dense ? (ws[R]||[])[C] : ws[coord];
  15545. if(!cell) { push_value(o, 1, 0, ""); continue;}
  15546. switch(cell.t) {
  15547. case 'n':
  15548. var val = DIF_XL ? cell.w : cell.v;
  15549. if(!val && cell.v != null) val = cell.v;
  15550. if(val == null) {
  15551. if(DIF_XL && cell.f && !cell.F) push_value(o, 1, 0, "=" + cell.f);
  15552. else push_value(o, 1, 0, "");
  15553. }
  15554. else push_value(o, 0, val, "V");
  15555. break;
  15556. case 'b':
  15557. push_value(o, 0, cell.v ? 1 : 0, cell.v ? "TRUE" : "FALSE");
  15558. break;
  15559. case 's':
  15560. push_value(o, 1, 0, (!DIF_XL || isNaN(cell.v)) ? cell.v : '="' + cell.v + '"');
  15561. break;
  15562. case 'd':
  15563. if(!cell.w) cell.w = SSF.format(cell.z || SSF._table[14], datenum(parseDate(cell.v)));
  15564. if(DIF_XL) push_value(o, 0, cell.w, "V");
  15565. else push_value(o, 1, 0, cell.w);
  15566. break;
  15567. default: push_value(o, 1, 0, "");
  15568. }
  15569. }
  15570. }
  15571. push_value(o, -1, 0, "EOD");
  15572. var RS = "\r\n";
  15573. var oo = o.join(RS);
  15574. //while((oo.length & 0x7F) != 0) oo += "\0";
  15575. return oo;
  15576. };
  15577. })();
  15578. return {
  15579. to_workbook: dif_to_workbook,
  15580. to_sheet: dif_to_sheet,
  15581. from_sheet: sheet_to_dif
  15582. };
  15583. })();
  15584. var ETH = (function() {
  15585. function decode(s) { return s.replace(/\\b/g,"\\").replace(/\\c/g,":").replace(/\\n/g,"\n"); }
  15586. function encode(s) { return s.replace(/\\/g, "\\b").replace(/:/g, "\\c").replace(/\n/g,"\\n"); }
  15587. function eth_to_aoa(str, opts) {
  15588. var records = str.split('\n'), R = -1, C = -1, ri = 0, arr = [];
  15589. for (; ri !== records.length; ++ri) {
  15590. var record = records[ri].trim().split(":");
  15591. if(record[0] !== 'cell') continue;
  15592. var addr = decode_cell(record[1]);
  15593. if(arr.length <= addr.r) for(R = arr.length; R <= addr.r; ++R) if(!arr[R]) arr[R] = [];
  15594. R = addr.r; C = addr.c;
  15595. switch(record[2]) {
  15596. case 't': arr[R][C] = decode(record[3]); break;
  15597. case 'v': arr[R][C] = +record[3]; break;
  15598. case 'vtf': var _f = record[record.length - 1];
  15599. /* falls through */
  15600. case 'vtc':
  15601. switch(record[3]) {
  15602. case 'nl': arr[R][C] = +record[4] ? true : false; break;
  15603. default: arr[R][C] = +record[4]; break;
  15604. }
  15605. if(record[2] == 'vtf') arr[R][C] = [arr[R][C], _f];
  15606. }
  15607. }
  15608. if(opts && opts.sheetRows) arr = arr.slice(0, opts.sheetRows);
  15609. return arr;
  15610. }
  15611. function eth_to_sheet(d, opts) { return aoa_to_sheet(eth_to_aoa(d, opts), opts); }
  15612. function eth_to_workbook(d, opts) { return sheet_to_workbook(eth_to_sheet(d, opts), opts); }
  15613. var header = [
  15614. "socialcalc:version:1.5",
  15615. "MIME-Version: 1.0",
  15616. "Content-Type: multipart/mixed; boundary=SocialCalcSpreadsheetControlSave"
  15617. ].join("\n");
  15618. var sep = [
  15619. "--SocialCalcSpreadsheetControlSave",
  15620. "Content-type: text/plain; charset=UTF-8"
  15621. ].join("\n") + "\n";
  15622. /* TODO: the other parts */
  15623. var meta = [
  15624. "# SocialCalc Spreadsheet Control Save",
  15625. "part:sheet"
  15626. ].join("\n");
  15627. var end = "--SocialCalcSpreadsheetControlSave--";
  15628. function sheet_to_eth_data(ws) {
  15629. if(!ws || !ws['!ref']) return "";
  15630. var o = [], oo = [], cell, coord = "";
  15631. var r = decode_range(ws['!ref']);
  15632. var dense = Array.isArray(ws);
  15633. for(var R = r.s.r; R <= r.e.r; ++R) {
  15634. for(var C = r.s.c; C <= r.e.c; ++C) {
  15635. coord = encode_cell({r:R,c:C});
  15636. cell = dense ? (ws[R]||[])[C] : ws[coord];
  15637. if(!cell || cell.v == null || cell.t === 'z') continue;
  15638. oo = ["cell", coord, 't'];
  15639. switch(cell.t) {
  15640. case 's': case 'str': oo.push(encode(cell.v)); break;
  15641. case 'n':
  15642. if(!cell.f) { oo[2]='v'; oo[3]=cell.v; }
  15643. else { oo[2]='vtf'; oo[3]='n'; oo[4]=cell.v; oo[5]=encode(cell.f); }
  15644. break;
  15645. case 'b':
  15646. oo[2] = 'vt'+(cell.f?'f':'c'); oo[3]='nl'; oo[4]=cell.v?"1":"0";
  15647. oo[5] = encode(cell.f||(cell.v?'TRUE':'FALSE'));
  15648. break;
  15649. case 'd':
  15650. var t = datenum(parseDate(cell.v));
  15651. oo[2] = 'vtc'; oo[3] = 'nd'; oo[4] = ""+t;
  15652. oo[5] = cell.w || SSF.format(cell.z || SSF._table[14], t);
  15653. break;
  15654. case 'e': continue;
  15655. }
  15656. o.push(oo.join(":"));
  15657. }
  15658. }
  15659. o.push("sheet:c:" + (r.e.c-r.s.c+1) + ":r:" + (r.e.r-r.s.r+1) + ":tvf:1");
  15660. o.push("valueformat:1:text-wiki");
  15661. //o.push("copiedfrom:" + ws['!ref']); // clipboard only
  15662. return o.join("\n");
  15663. }
  15664. function sheet_to_eth(ws) {
  15665. return [header, sep, meta, sep, sheet_to_eth_data(ws), end].join("\n");
  15666. // return ["version:1.5", sheet_to_eth_data(ws)].join("\n"); // clipboard form
  15667. }
  15668. return {
  15669. to_workbook: eth_to_workbook,
  15670. to_sheet: eth_to_sheet,
  15671. from_sheet: sheet_to_eth
  15672. };
  15673. })();
  15674. var PRN = (function() {
  15675. function set_text_arr(data, arr, R, C, o) {
  15676. if(o.raw) arr[R][C] = data;
  15677. else if(data === 'TRUE') arr[R][C] = true;
  15678. else if(data === 'FALSE') arr[R][C] = false;
  15679. else if(data === ""){/* empty */}
  15680. else if(!isNaN(fuzzynum(data))) arr[R][C] = fuzzynum(data);
  15681. else if(!isNaN(fuzzydate(data).getDate())) arr[R][C] = parseDate(data);
  15682. else arr[R][C] = data;
  15683. }
  15684. function prn_to_aoa_str(f, opts) {
  15685. var o = opts || {};
  15686. var arr = ([]);
  15687. if(!f || f.length === 0) return arr;
  15688. var lines = f.split(/[\r\n]/);
  15689. var L = lines.length - 1;
  15690. while(L >= 0 && lines[L].length === 0) --L;
  15691. var start = 10, idx = 0;
  15692. var R = 0;
  15693. for(; R <= L; ++R) {
  15694. idx = lines[R].indexOf(" ");
  15695. if(idx == -1) idx = lines[R].length; else idx++;
  15696. start = Math.max(start, idx);
  15697. }
  15698. for(R = 0; R <= L; ++R) {
  15699. arr[R] = [];
  15700. /* TODO: confirm that widths are always 10 */
  15701. var C = 0;
  15702. set_text_arr(lines[R].slice(0, start).trim(), arr, R, C, o);
  15703. for(C = 1; C <= (lines[R].length - start)/10 + 1; ++C)
  15704. set_text_arr(lines[R].slice(start+(C-1)*10,start+C*10).trim(),arr,R,C,o);
  15705. }
  15706. if(o.sheetRows) arr = arr.slice(0, o.sheetRows);
  15707. return arr;
  15708. }
  15709. // List of accepted CSV separators
  15710. var guess_seps = {
  15711. 0x2C: ',',
  15712. 0x09: "\t",
  15713. 0x3B: ';'
  15714. };
  15715. // CSV separator weights to be used in case of equal numbers
  15716. var guess_sep_weights = {
  15717. 0x2C: 3,
  15718. 0x09: 2,
  15719. 0x3B: 1
  15720. };
  15721. function guess_sep(str) {
  15722. var cnt = {}, instr = false, end = 0, cc = 0;
  15723. for(;end < str.length;++end) {
  15724. if((cc=str.charCodeAt(end)) == 0x22) instr = !instr;
  15725. else if(!instr && cc in guess_seps) cnt[cc] = (cnt[cc]||0)+1;
  15726. }
  15727. cc = [];
  15728. for(end in cnt) if ( cnt.hasOwnProperty(end) ) {
  15729. cc.push([ cnt[end], end ]);
  15730. }
  15731. if ( !cc.length ) {
  15732. cnt = guess_sep_weights;
  15733. for(end in cnt) if ( cnt.hasOwnProperty(end) ) {
  15734. cc.push([ cnt[end], end ]);
  15735. }
  15736. }
  15737. cc.sort(function(a, b) { return a[0] - b[0] || guess_sep_weights[a[1]] - guess_sep_weights[b[1]]; });
  15738. return guess_seps[cc.pop()[1]];
  15739. }
  15740. function dsv_to_sheet_str(str, opts) {
  15741. var o = opts || {};
  15742. var sep = "";
  15743. if(DENSE != null && o.dense == null) o.dense = DENSE;
  15744. var ws = o.dense ? ([]) : ({});
  15745. var range = ({s: {c:0, r:0}, e: {c:0, r:0}});
  15746. if(str.slice(0,4) == "sep=" && str.charCodeAt(5) == 10) { sep = str.charAt(4); str = str.slice(6); }
  15747. else sep = guess_sep(str.slice(0,1024));
  15748. var R = 0, C = 0, v = 0;
  15749. var start = 0, end = 0, sepcc = sep.charCodeAt(0), instr = false, cc=0;
  15750. str = str.replace(/\r\n/mg, "\n");
  15751. var _re = o.dateNF != null ? dateNF_regex(o.dateNF) : null;
  15752. function finish_cell() {
  15753. var s = str.slice(start, end);
  15754. var cell = ({});
  15755. if(s.charAt(0) == '"' && s.charAt(s.length - 1) == '"') s = s.slice(1,-1).replace(/""/g,'"');
  15756. if(s.length === 0) cell.t = 'z';
  15757. else if(o.raw) { cell.t = 's'; cell.v = s; }
  15758. else if(s.trim().length === 0) { cell.t = 's'; cell.v = s; }
  15759. else if(s.charCodeAt(0) == 0x3D) {
  15760. if(s.charCodeAt(1) == 0x22 && s.charCodeAt(s.length - 1) == 0x22) { cell.t = 's'; cell.v = s.slice(2,-1).replace(/""/g,'"'); }
  15761. else if(fuzzyfmla(s)) { cell.t = 'n'; cell.f = s.slice(1); }
  15762. else { cell.t = 's'; cell.v = s; } }
  15763. else if(s == "TRUE") { cell.t = 'b'; cell.v = true; }
  15764. else if(s == "FALSE") { cell.t = 'b'; cell.v = false; }
  15765. else if(!isNaN(v = fuzzynum(s))) { cell.t = 'n'; if(o.cellText !== false) cell.w = s; cell.v = v; }
  15766. else if(!isNaN(fuzzydate(s).getDate()) || _re && s.match(_re)) {
  15767. cell.z = o.dateNF || SSF._table[14];
  15768. var k = 0;
  15769. if(_re && s.match(_re)){ s=dateNF_fix(s, o.dateNF, (s.match(_re)||[])); k=1; }
  15770. if(o.cellDates) { cell.t = 'd'; cell.v = parseDate(s, k); }
  15771. else { cell.t = 'n'; cell.v = datenum(parseDate(s, k)); }
  15772. if(o.cellText !== false) cell.w = SSF.format(cell.z, cell.v instanceof Date ? datenum(cell.v):cell.v);
  15773. if(!o.cellNF) delete cell.z;
  15774. } else {
  15775. cell.t = 's';
  15776. cell.v = s;
  15777. }
  15778. if(cell.t == 'z'){}
  15779. else if(o.dense) { if(!ws[R]) ws[R] = []; ws[R][C] = cell; }
  15780. else ws[encode_cell({c:C,r:R})] = cell;
  15781. start = end+1;
  15782. if(range.e.c < C) range.e.c = C;
  15783. if(range.e.r < R) range.e.r = R;
  15784. if(cc == sepcc) ++C; else { C = 0; ++R; if(o.sheetRows && o.sheetRows <= R) return true; }
  15785. }
  15786. outer: for(;end < str.length;++end) switch((cc=str.charCodeAt(end))) {
  15787. case 0x22: instr = !instr; break;
  15788. case sepcc: case 0x0a: case 0x0d: if(!instr && finish_cell()) break outer; break;
  15789. default: break;
  15790. }
  15791. if(end - start > 0) finish_cell();
  15792. ws['!ref'] = encode_range(range);
  15793. return ws;
  15794. }
  15795. function prn_to_sheet_str(str, opts) {
  15796. if(str.slice(0,4) == "sep=") return dsv_to_sheet_str(str, opts);
  15797. if(str.indexOf("\t") >= 0 || str.indexOf(",") >= 0 || str.indexOf(";") >= 0) return dsv_to_sheet_str(str, opts);
  15798. return aoa_to_sheet(prn_to_aoa_str(str, opts), opts);
  15799. }
  15800. function prn_to_sheet(d, opts) {
  15801. var str = "", bytes = opts.type == 'string' ? [0,0,0,0] : firstbyte(d, opts);
  15802. switch(opts.type) {
  15803. case 'base64': str = Base64.decode(d); break;
  15804. case 'binary': str = d; break;
  15805. case 'buffer':
  15806. if(opts.codepage == 65001) str = d.toString('utf8');
  15807. else if(opts.codepage && typeof cptable !== 'undefined') str = cptable.utils.decode(opts.codepage, d);
  15808. else str = d.toString('binary');
  15809. break;
  15810. case 'array': str = cc2str(d); break;
  15811. case 'string': str = d; break;
  15812. default: throw new Error("Unrecognized type " + opts.type);
  15813. }
  15814. if(bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) str = utf8read(str.slice(3));
  15815. else if((opts.type == 'binary') && typeof cptable !== 'undefined' && opts.codepage) str = cptable.utils.decode(opts.codepage, cptable.utils.encode(1252,str));
  15816. if(str.slice(0,19) == "socialcalc:version:") return ETH.to_sheet(opts.type == 'string' ? str : utf8read(str), opts);
  15817. return prn_to_sheet_str(str, opts);
  15818. }
  15819. function prn_to_workbook(d, opts) { return sheet_to_workbook(prn_to_sheet(d, opts), opts); }
  15820. function sheet_to_prn(ws) {
  15821. var o = [];
  15822. var r = safe_decode_range(ws['!ref']), cell;
  15823. var dense = Array.isArray(ws);
  15824. for(var R = r.s.r; R <= r.e.r; ++R) {
  15825. var oo = [];
  15826. for(var C = r.s.c; C <= r.e.c; ++C) {
  15827. var coord = encode_cell({r:R,c:C});
  15828. cell = dense ? (ws[R]||[])[C] : ws[coord];
  15829. if(!cell || cell.v == null) { oo.push(" "); continue; }
  15830. var w = (cell.w || (format_cell(cell), cell.w) || "").slice(0,10);
  15831. while(w.length < 10) w += " ";
  15832. oo.push(w + (C === 0 ? " " : ""));
  15833. }
  15834. o.push(oo.join(""));
  15835. }
  15836. return o.join("\n");
  15837. }
  15838. return {
  15839. to_workbook: prn_to_workbook,
  15840. to_sheet: prn_to_sheet,
  15841. from_sheet: sheet_to_prn
  15842. };
  15843. })();
  15844. /* Excel defaults to SYLK but warns if data is not valid */
  15845. function read_wb_ID(d, opts) {
  15846. var o = opts || {}, OLD_WTF = !!o.WTF; o.WTF = true;
  15847. try {
  15848. var out = SYLK.to_workbook(d, o);
  15849. o.WTF = OLD_WTF;
  15850. return out;
  15851. } catch(e) {
  15852. o.WTF = OLD_WTF;
  15853. if(!e.message.match(/SYLK bad record ID/) && OLD_WTF) throw e;
  15854. return PRN.to_workbook(d, opts);
  15855. }
  15856. }
  15857. var WK_ = (function() {
  15858. function lotushopper(data, cb, opts) {
  15859. if(!data) return;
  15860. prep_blob(data, data.l || 0);
  15861. var Enum = opts.Enum || WK1Enum;
  15862. while(data.l < data.length) {
  15863. var RT = data.read_shift(2);
  15864. var R = Enum[RT] || Enum[0xFF];
  15865. var length = data.read_shift(2);
  15866. var tgt = data.l + length;
  15867. var d = (R.f||parsenoop)(data, length, opts);
  15868. data.l = tgt;
  15869. if(cb(d, R.n, RT)) return;
  15870. }
  15871. }
  15872. function lotus_to_workbook(d, opts) {
  15873. switch(opts.type) {
  15874. case 'base64': return lotus_to_workbook_buf(s2a(Base64.decode(d)), opts);
  15875. case 'binary': return lotus_to_workbook_buf(s2a(d), opts);
  15876. case 'buffer':
  15877. case 'array': return lotus_to_workbook_buf(d, opts);
  15878. }
  15879. throw "Unsupported type " + opts.type;
  15880. }
  15881. function lotus_to_workbook_buf(d, opts) {
  15882. if(!d) return d;
  15883. var o = opts || {};
  15884. if(DENSE != null && o.dense == null) o.dense = DENSE;
  15885. var s = ((o.dense ? [] : {})), n = "Sheet1", sidx = 0;
  15886. var sheets = {}, snames = [n];
  15887. var refguess = {s: {r:0, c:0}, e: {r:0, c:0} };
  15888. var sheetRows = o.sheetRows || 0;
  15889. if(d[2] == 0x02) o.Enum = WK1Enum;
  15890. else if(d[2] == 0x1a) o.Enum = WK3Enum;
  15891. else if(d[2] == 0x0e) { o.Enum = WK3Enum; o.qpro = true; d.l = 0; }
  15892. else throw new Error("Unrecognized LOTUS BOF " + d[2]);
  15893. lotushopper(d, function(val, Rn, RT) {
  15894. if(d[2] == 0x02) switch(RT) {
  15895. case 0x00:
  15896. o.vers = val;
  15897. if(val >= 0x1000) o.qpro = true;
  15898. break;
  15899. case 0x06: refguess = val; break; /* RANGE */
  15900. case 0x0F: /* LABEL */
  15901. if(!o.qpro) val[1].v = val[1].v.slice(1);
  15902. /* falls through */
  15903. case 0x0D: /* INTEGER */
  15904. case 0x0E: /* NUMBER */
  15905. case 0x10: /* FORMULA */
  15906. case 0x33: /* STRING */
  15907. /* TODO: actual translation of the format code */
  15908. if(RT == 0x0E && (val[2] & 0x70) == 0x70 && (val[2] & 0x0F) > 1 && (val[2] & 0x0F) < 15) {
  15909. val[1].z = o.dateNF || SSF._table[14];
  15910. if(o.cellDates) { val[1].t = 'd'; val[1].v = numdate(val[1].v); }
  15911. }
  15912. if(o.dense) {
  15913. if(!s[val[0].r]) s[val[0].r] = [];
  15914. s[val[0].r][val[0].c] = val[1];
  15915. } else s[encode_cell(val[0])] = val[1];
  15916. break;
  15917. } else switch(RT) {
  15918. case 0x16: /* LABEL16 */
  15919. val[1].v = val[1].v.slice(1);
  15920. /* falls through */
  15921. case 0x17: /* NUMBER17 */
  15922. case 0x18: /* NUMBER18 */
  15923. case 0x19: /* FORMULA19 */
  15924. case 0x25: /* NUMBER25 */
  15925. case 0x27: /* NUMBER27 */
  15926. case 0x28: /* FORMULA28 */
  15927. if(val[3] > sidx) {
  15928. s["!ref"] = encode_range(refguess);
  15929. sheets[n] = s;
  15930. s = (o.dense ? [] : {});
  15931. refguess = {s: {r:0, c:0}, e: {r:0, c:0} };
  15932. sidx = val[3]; n = "Sheet" + (sidx + 1);
  15933. snames.push(n);
  15934. }
  15935. if(sheetRows > 0 && val[0].r >= sheetRows) break;
  15936. if(o.dense) {
  15937. if(!s[val[0].r]) s[val[0].r] = [];
  15938. s[val[0].r][val[0].c] = val[1];
  15939. } else s[encode_cell(val[0])] = val[1];
  15940. if(refguess.e.c < val[0].c) refguess.e.c = val[0].c;
  15941. if(refguess.e.r < val[0].r) refguess.e.r = val[0].r;
  15942. break;
  15943. default: break;
  15944. }
  15945. }, o);
  15946. s["!ref"] = encode_range(refguess);
  15947. sheets[n] = s;
  15948. return { SheetNames: snames, Sheets:sheets };
  15949. }
  15950. function parse_RANGE(blob) {
  15951. var o = {s:{c:0,r:0},e:{c:0,r:0}};
  15952. o.s.c = blob.read_shift(2);
  15953. o.s.r = blob.read_shift(2);
  15954. o.e.c = blob.read_shift(2);
  15955. o.e.r = blob.read_shift(2);
  15956. if(o.s.c == 0xFFFF) o.s.c = o.e.c = o.s.r = o.e.r = 0;
  15957. return o;
  15958. }
  15959. function parse_cell(blob, length, opts) {
  15960. var o = [{c:0,r:0}, {t:'n',v:0}, 0];
  15961. if(opts.qpro && opts.vers != 0x5120) {
  15962. o[0].c = blob.read_shift(1);
  15963. blob.l++;
  15964. o[0].r = blob.read_shift(2);
  15965. blob.l+=2;
  15966. } else {
  15967. o[2] = blob.read_shift(1);
  15968. o[0].c = blob.read_shift(2); o[0].r = blob.read_shift(2);
  15969. }
  15970. return o;
  15971. }
  15972. function parse_LABEL(blob, length, opts) {
  15973. var tgt = blob.l + length;
  15974. var o = parse_cell(blob, length, opts);
  15975. o[1].t = 's';
  15976. if(opts.vers == 0x5120) {
  15977. blob.l++;
  15978. var len = blob.read_shift(1);
  15979. o[1].v = blob.read_shift(len, 'utf8');
  15980. return o;
  15981. }
  15982. if(opts.qpro) blob.l++;
  15983. o[1].v = blob.read_shift(tgt - blob.l, 'cstr');
  15984. return o;
  15985. }
  15986. function parse_INTEGER(blob, length, opts) {
  15987. var o = parse_cell(blob, length, opts);
  15988. o[1].v = blob.read_shift(2, 'i');
  15989. return o;
  15990. }
  15991. function parse_NUMBER(blob, length, opts) {
  15992. var o = parse_cell(blob, length, opts);
  15993. o[1].v = blob.read_shift(8, 'f');
  15994. return o;
  15995. }
  15996. function parse_FORMULA(blob, length, opts) {
  15997. var tgt = blob.l + length;
  15998. var o = parse_cell(blob, length, opts);
  15999. /* TODO: formula */
  16000. o[1].v = blob.read_shift(8, 'f');
  16001. if(opts.qpro) blob.l = tgt;
  16002. else {
  16003. var flen = blob.read_shift(2);
  16004. blob.l += flen;
  16005. }
  16006. return o;
  16007. }
  16008. function parse_cell_3(blob) {
  16009. var o = [{c:0,r:0}, {t:'n',v:0}, 0];
  16010. o[0].r = blob.read_shift(2); o[3] = blob[blob.l++]; o[0].c = blob[blob.l++];
  16011. return o;
  16012. }
  16013. function parse_LABEL_16(blob, length) {
  16014. var o = parse_cell_3(blob, length);
  16015. o[1].t = 's';
  16016. o[1].v = blob.read_shift(length - 4, 'cstr');
  16017. return o;
  16018. }
  16019. function parse_NUMBER_18(blob, length) {
  16020. var o = parse_cell_3(blob, length);
  16021. o[1].v = blob.read_shift(2);
  16022. var v = o[1].v >> 1;
  16023. /* TODO: figure out all of the corner cases */
  16024. if(o[1].v & 0x1) {
  16025. switch(v & 0x07) {
  16026. case 1: v = (v >> 3) * 500; break;
  16027. case 2: v = (v >> 3) / 20; break;
  16028. case 4: v = (v >> 3) / 2000; break;
  16029. case 6: v = (v >> 3) / 16; break;
  16030. case 7: v = (v >> 3) / 64; break;
  16031. default: throw "unknown NUMBER_18 encoding " + (v & 0x07);
  16032. }
  16033. }
  16034. o[1].v = v;
  16035. return o;
  16036. }
  16037. function parse_NUMBER_17(blob, length) {
  16038. var o = parse_cell_3(blob, length);
  16039. var v1 = blob.read_shift(4);
  16040. var v2 = blob.read_shift(4);
  16041. var e = blob.read_shift(2);
  16042. if(e == 0xFFFF) { o[1].v = 0; return o; }
  16043. var s = e & 0x8000; e = (e&0x7FFF) - 16446;
  16044. o[1].v = (s*2 - 1) * ((e > 0 ? (v2 << e) : (v2 >>> -e)) + (e > -32 ? (v1 << (e + 32)) : (v1 >>> -(e + 32))));
  16045. return o;
  16046. }
  16047. function parse_FORMULA_19(blob, length) {
  16048. var o = parse_NUMBER_17(blob, 14);
  16049. blob.l += length - 14; /* TODO: formula */
  16050. return o;
  16051. }
  16052. function parse_NUMBER_25(blob, length) {
  16053. var o = parse_cell_3(blob, length);
  16054. var v1 = blob.read_shift(4);
  16055. o[1].v = v1 >> 6;
  16056. return o;
  16057. }
  16058. function parse_NUMBER_27(blob, length) {
  16059. var o = parse_cell_3(blob, length);
  16060. var v1 = blob.read_shift(8,'f');
  16061. o[1].v = v1;
  16062. return o;
  16063. }
  16064. function parse_FORMULA_28(blob, length) {
  16065. var o = parse_NUMBER_27(blob, 14);
  16066. blob.l += length - 10; /* TODO: formula */
  16067. return o;
  16068. }
  16069. var WK1Enum = {
  16070. 0x0000: { n:"BOF", f:parseuint16 },
  16071. 0x0001: { n:"EOF" },
  16072. 0x0002: { n:"CALCMODE" },
  16073. 0x0003: { n:"CALCORDER" },
  16074. 0x0004: { n:"SPLIT" },
  16075. 0x0005: { n:"SYNC" },
  16076. 0x0006: { n:"RANGE", f:parse_RANGE },
  16077. 0x0007: { n:"WINDOW1" },
  16078. 0x0008: { n:"COLW1" },
  16079. 0x0009: { n:"WINTWO" },
  16080. 0x000A: { n:"COLW2" },
  16081. 0x000B: { n:"NAME" },
  16082. 0x000C: { n:"BLANK" },
  16083. 0x000D: { n:"INTEGER", f:parse_INTEGER },
  16084. 0x000E: { n:"NUMBER", f:parse_NUMBER },
  16085. 0x000F: { n:"LABEL", f:parse_LABEL },
  16086. 0x0010: { n:"FORMULA", f:parse_FORMULA },
  16087. 0x0018: { n:"TABLE" },
  16088. 0x0019: { n:"ORANGE" },
  16089. 0x001A: { n:"PRANGE" },
  16090. 0x001B: { n:"SRANGE" },
  16091. 0x001C: { n:"FRANGE" },
  16092. 0x001D: { n:"KRANGE1" },
  16093. 0x0020: { n:"HRANGE" },
  16094. 0x0023: { n:"KRANGE2" },
  16095. 0x0024: { n:"PROTEC" },
  16096. 0x0025: { n:"FOOTER" },
  16097. 0x0026: { n:"HEADER" },
  16098. 0x0027: { n:"SETUP" },
  16099. 0x0028: { n:"MARGINS" },
  16100. 0x0029: { n:"LABELFMT" },
  16101. 0x002A: { n:"TITLES" },
  16102. 0x002B: { n:"SHEETJS" },
  16103. 0x002D: { n:"GRAPH" },
  16104. 0x002E: { n:"NGRAPH" },
  16105. 0x002F: { n:"CALCCOUNT" },
  16106. 0x0030: { n:"UNFORMATTED" },
  16107. 0x0031: { n:"CURSORW12" },
  16108. 0x0032: { n:"WINDOW" },
  16109. 0x0033: { n:"STRING", f:parse_LABEL },
  16110. 0x0037: { n:"PASSWORD" },
  16111. 0x0038: { n:"LOCKED" },
  16112. 0x003C: { n:"QUERY" },
  16113. 0x003D: { n:"QUERYNAME" },
  16114. 0x003E: { n:"PRINT" },
  16115. 0x003F: { n:"PRINTNAME" },
  16116. 0x0040: { n:"GRAPH2" },
  16117. 0x0041: { n:"GRAPHNAME" },
  16118. 0x0042: { n:"ZOOM" },
  16119. 0x0043: { n:"SYMSPLIT" },
  16120. 0x0044: { n:"NSROWS" },
  16121. 0x0045: { n:"NSCOLS" },
  16122. 0x0046: { n:"RULER" },
  16123. 0x0047: { n:"NNAME" },
  16124. 0x0048: { n:"ACOMM" },
  16125. 0x0049: { n:"AMACRO" },
  16126. 0x004A: { n:"PARSE" },
  16127. 0x00FF: { n:"", f:parsenoop }
  16128. };
  16129. var WK3Enum = {
  16130. 0x0000: { n:"BOF" },
  16131. 0x0001: { n:"EOF" },
  16132. 0x0003: { n:"??" },
  16133. 0x0004: { n:"??" },
  16134. 0x0005: { n:"??" },
  16135. 0x0006: { n:"??" },
  16136. 0x0007: { n:"??" },
  16137. 0x0009: { n:"??" },
  16138. 0x000a: { n:"??" },
  16139. 0x000b: { n:"??" },
  16140. 0x000c: { n:"??" },
  16141. 0x000e: { n:"??" },
  16142. 0x000f: { n:"??" },
  16143. 0x0010: { n:"??" },
  16144. 0x0011: { n:"??" },
  16145. 0x0012: { n:"??" },
  16146. 0x0013: { n:"??" },
  16147. 0x0015: { n:"??" },
  16148. 0x0016: { n:"LABEL16", f:parse_LABEL_16},
  16149. 0x0017: { n:"NUMBER17", f:parse_NUMBER_17 },
  16150. 0x0018: { n:"NUMBER18", f:parse_NUMBER_18 },
  16151. 0x0019: { n:"FORMULA19", f:parse_FORMULA_19},
  16152. 0x001a: { n:"??" },
  16153. 0x001b: { n:"??" },
  16154. 0x001c: { n:"??" },
  16155. 0x001d: { n:"??" },
  16156. 0x001e: { n:"??" },
  16157. 0x001f: { n:"??" },
  16158. 0x0021: { n:"??" },
  16159. 0x0025: { n:"NUMBER25", f:parse_NUMBER_25 },
  16160. 0x0027: { n:"NUMBER27", f:parse_NUMBER_27 },
  16161. 0x0028: { n:"FORMULA28", f:parse_FORMULA_28 },
  16162. 0x00FF: { n:"", f:parsenoop }
  16163. };
  16164. return {
  16165. to_workbook: lotus_to_workbook
  16166. };
  16167. })();
  16168. /* Parse a list of <r> tags */
  16169. var parse_rs = (function parse_rs_factory() {
  16170. var tregex = matchtag("t"), rpregex = matchtag("rPr"), rregex = /<(?:\w+:)?r>/g, rend = /<\/(?:\w+:)?r>/, nlregex = /\r\n/g;
  16171. /* 18.4.7 rPr CT_RPrElt */
  16172. var parse_rpr = function parse_rpr(rpr, intro, outro) {
  16173. var font = {}, cp = 65001, align = "";
  16174. var pass = false;
  16175. var m = rpr.match(tagregex), i = 0;
  16176. if(m) for(;i!=m.length; ++i) {
  16177. var y = parsexmltag(m[i]);
  16178. switch(y[0].replace(/\w*:/g,"")) {
  16179. /* 18.8.12 condense CT_BooleanProperty */
  16180. /* ** not required . */
  16181. case '<condense': break;
  16182. /* 18.8.17 extend CT_BooleanProperty */
  16183. /* ** not required . */
  16184. case '<extend': break;
  16185. /* 18.8.36 shadow CT_BooleanProperty */
  16186. /* ** not required . */
  16187. case '<shadow':
  16188. if(!y.val) break;
  16189. /* falls through */
  16190. case '<shadow>':
  16191. case '<shadow/>': font.shadow = 1; break;
  16192. case '</shadow>': break;
  16193. /* 18.4.1 charset CT_IntProperty TODO */
  16194. case '<charset':
  16195. if(y.val == '1') break;
  16196. cp = CS2CP[parseInt(y.val, 10)];
  16197. break;
  16198. /* 18.4.2 outline CT_BooleanProperty TODO */
  16199. case '<outline':
  16200. if(!y.val) break;
  16201. /* falls through */
  16202. case '<outline>':
  16203. case '<outline/>': font.outline = 1; break;
  16204. case '</outline>': break;
  16205. /* 18.4.5 rFont CT_FontName */
  16206. case '<rFont': font.name = y.val; break;
  16207. /* 18.4.11 sz CT_FontSize */
  16208. case '<sz': font.sz = y.val; break;
  16209. /* 18.4.10 strike CT_BooleanProperty */
  16210. case '<strike':
  16211. if(!y.val) break;
  16212. /* falls through */
  16213. case '<strike>':
  16214. case '<strike/>': font.strike = 1; break;
  16215. case '</strike>': break;
  16216. /* 18.4.13 u CT_UnderlineProperty */
  16217. case '<u':
  16218. if(!y.val) break;
  16219. switch(y.val) {
  16220. case 'double': font.uval = "double"; break;
  16221. case 'singleAccounting': font.uval = "single-accounting"; break;
  16222. case 'doubleAccounting': font.uval = "double-accounting"; break;
  16223. }
  16224. /* falls through */
  16225. case '<u>':
  16226. case '<u/>': font.u = 1; break;
  16227. case '</u>': break;
  16228. /* 18.8.2 b */
  16229. case '<b':
  16230. if(y.val == '0') break;
  16231. /* falls through */
  16232. case '<b>':
  16233. case '<b/>': font.b = 1; break;
  16234. case '</b>': break;
  16235. /* 18.8.26 i */
  16236. case '<i':
  16237. if(y.val == '0') break;
  16238. /* falls through */
  16239. case '<i>':
  16240. case '<i/>': font.i = 1; break;
  16241. case '</i>': break;
  16242. /* 18.3.1.15 color CT_Color TODO: tint, theme, auto, indexed */
  16243. case '<color':
  16244. if(y.rgb) font.color = y.rgb.slice(2,8);
  16245. break;
  16246. /* 18.8.18 family ST_FontFamily */
  16247. case '<family': font.family = y.val; break;
  16248. /* 18.4.14 vertAlign CT_VerticalAlignFontProperty TODO */
  16249. case '<vertAlign': align = y.val; break;
  16250. /* 18.8.35 scheme CT_FontScheme TODO */
  16251. case '<scheme': break;
  16252. /* 18.2.10 extLst CT_ExtensionList ? */
  16253. case '<extLst': case '<extLst>': case '</extLst>': break;
  16254. case '<ext': pass = true; break;
  16255. case '</ext>': pass = false; break;
  16256. default:
  16257. if(y[0].charCodeAt(1) !== 47 && !pass) throw new Error('Unrecognized rich format ' + y[0]);
  16258. }
  16259. }
  16260. var style = [];
  16261. if(font.u) style.push("text-decoration: underline;");
  16262. if(font.uval) style.push("text-underline-style:" + font.uval + ";");
  16263. if(font.sz) style.push("font-size:" + font.sz + "pt;");
  16264. if(font.outline) style.push("text-effect: outline;");
  16265. if(font.shadow) style.push("text-shadow: auto;");
  16266. intro.push('<span style="' + style.join("") + '">');
  16267. if(font.b) { intro.push("<b>"); outro.push("</b>"); }
  16268. if(font.i) { intro.push("<i>"); outro.push("</i>"); }
  16269. if(font.strike) { intro.push("<s>"); outro.push("</s>"); }
  16270. if(align == "superscript") align = "sup";
  16271. else if(align == "subscript") align = "sub";
  16272. if(align != "") { intro.push("<" + align + ">"); outro.push("</" + align + ">"); }
  16273. outro.push("</span>");
  16274. return cp;
  16275. };
  16276. /* 18.4.4 r CT_RElt */
  16277. function parse_r(r) {
  16278. var terms = [[],"",[]];
  16279. /* 18.4.12 t ST_Xstring */
  16280. var t = r.match(tregex)/*, cp = 65001*/;
  16281. if(!t) return "";
  16282. terms[1] = t[1];
  16283. var rpr = r.match(rpregex);
  16284. if(rpr) /*cp = */parse_rpr(rpr[1], terms[0], terms[2]);
  16285. return terms[0].join("") + terms[1].replace(nlregex,'<br/>') + terms[2].join("");
  16286. }
  16287. return function parse_rs(rs) {
  16288. return rs.replace(rregex,"").split(rend).map(parse_r).join("");
  16289. };
  16290. })();
  16291. /* 18.4.8 si CT_Rst */
  16292. var sitregex = /<(?:\w+:)?t[^>]*>([^<]*)<\/(?:\w+:)?t>/g, sirregex = /<(?:\w+:)?r>/;
  16293. var sirphregex = /<(?:\w+:)?rPh.*?>([\s\S]*?)<\/(?:\w+:)?rPh>/g;
  16294. function parse_si(x, opts) {
  16295. var html = opts ? opts.cellHTML : true;
  16296. var z = {};
  16297. if(!x) return null;
  16298. //var y;
  16299. /* 18.4.12 t ST_Xstring (Plaintext String) */
  16300. // TODO: is whitespace actually valid here?
  16301. if(x.match(/^\s*<(?:\w+:)?t[^>]*>/)) {
  16302. z.t = unescapexml(utf8read(x.slice(x.indexOf(">")+1).split(/<\/(?:\w+:)?t>/)[0]||""));
  16303. z.r = utf8read(x);
  16304. if(html) z.h = escapehtml(z.t);
  16305. }
  16306. /* 18.4.4 r CT_RElt (Rich Text Run) */
  16307. else if((/*y = */x.match(sirregex))) {
  16308. z.r = utf8read(x);
  16309. z.t = unescapexml(utf8read((x.replace(sirphregex, '').match(sitregex)||[]).join("").replace(tagregex,"")));
  16310. if(html) z.h = parse_rs(z.r);
  16311. }
  16312. /* 18.4.3 phoneticPr CT_PhoneticPr (TODO: needed for Asian support) */
  16313. /* 18.4.6 rPh CT_PhoneticRun (TODO: needed for Asian support) */
  16314. return z;
  16315. }
  16316. /* 18.4 Shared String Table */
  16317. var sstr0 = /<(?:\w+:)?sst([^>]*)>([\s\S]*)<\/(?:\w+:)?sst>/;
  16318. var sstr1 = /<(?:\w+:)?(?:si|sstItem)>/g;
  16319. var sstr2 = /<\/(?:\w+:)?(?:si|sstItem)>/;
  16320. function parse_sst_xml(data, opts) {
  16321. var s = ([]), ss = "";
  16322. if(!data) return s;
  16323. /* 18.4.9 sst CT_Sst */
  16324. var sst = data.match(sstr0);
  16325. if(sst) {
  16326. ss = sst[2].replace(sstr1,"").split(sstr2);
  16327. for(var i = 0; i != ss.length; ++i) {
  16328. var o = parse_si(ss[i].trim(), opts);
  16329. if(o != null) s[s.length] = o;
  16330. }
  16331. sst = parsexmltag(sst[1]); s.Count = sst.count; s.Unique = sst.uniqueCount;
  16332. }
  16333. return s;
  16334. }
  16335. RELS.SST = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings";
  16336. var straywsregex = /^\s|\s$|[\t\n\r]/;
  16337. function write_sst_xml(sst, opts) {
  16338. if(!opts.bookSST) return "";
  16339. var o = [XML_HEADER];
  16340. o[o.length] = (writextag('sst', null, {
  16341. xmlns: XMLNS.main[0],
  16342. count: sst.Count,
  16343. uniqueCount: sst.Unique
  16344. }));
  16345. for(var i = 0; i != sst.length; ++i) { if(sst[i] == null) continue;
  16346. var s = sst[i];
  16347. var sitag = "<si>";
  16348. if(s.r) sitag += s.r;
  16349. else {
  16350. sitag += "<t";
  16351. if(!s.t) s.t = "";
  16352. if(s.t.match(straywsregex)) sitag += ' xml:space="preserve"';
  16353. sitag += ">" + escapexml(s.t) + "</t>";
  16354. }
  16355. sitag += "</si>";
  16356. o[o.length] = (sitag);
  16357. }
  16358. if(o.length>2){ o[o.length] = ('</sst>'); o[1]=o[1].replace("/>",">"); }
  16359. return o.join("");
  16360. }
  16361. /* [MS-XLSB] 2.4.221 BrtBeginSst */
  16362. function parse_BrtBeginSst(data) {
  16363. return [data.read_shift(4), data.read_shift(4)];
  16364. }
  16365. /* [MS-XLSB] 2.1.7.45 Shared Strings */
  16366. function parse_sst_bin(data, opts) {
  16367. var s = ([]);
  16368. var pass = false;
  16369. recordhopper(data, function hopper_sst(val, R_n, RT) {
  16370. switch(RT) {
  16371. case 0x009F: /* 'BrtBeginSst' */
  16372. s.Count = val[0]; s.Unique = val[1]; break;
  16373. case 0x0013: /* 'BrtSSTItem' */
  16374. s.push(val); break;
  16375. case 0x00A0: /* 'BrtEndSst' */
  16376. return true;
  16377. case 0x0023: /* 'BrtFRTBegin' */
  16378. pass = true; break;
  16379. case 0x0024: /* 'BrtFRTEnd' */
  16380. pass = false; break;
  16381. default:
  16382. if(R_n.indexOf("Begin") > 0){/* empty */}
  16383. else if(R_n.indexOf("End") > 0){/* empty */}
  16384. if(!pass || opts.WTF) throw new Error("Unexpected record " + RT + " " + R_n);
  16385. }
  16386. });
  16387. return s;
  16388. }
  16389. function write_BrtBeginSst(sst, o) {
  16390. if(!o) o = new_buf(8);
  16391. o.write_shift(4, sst.Count);
  16392. o.write_shift(4, sst.Unique);
  16393. return o;
  16394. }
  16395. var write_BrtSSTItem = write_RichStr;
  16396. function write_sst_bin(sst) {
  16397. var ba = buf_array();
  16398. write_record(ba, "BrtBeginSst", write_BrtBeginSst(sst));
  16399. for(var i = 0; i < sst.length; ++i) write_record(ba, "BrtSSTItem", write_BrtSSTItem(sst[i]));
  16400. /* FRTSST */
  16401. write_record(ba, "BrtEndSst");
  16402. return ba.end();
  16403. }
  16404. function _JS2ANSI(str) {
  16405. if(typeof cptable !== 'undefined') return cptable.utils.encode(current_ansi, str);
  16406. var o = [], oo = str.split("");
  16407. for(var i = 0; i < oo.length; ++i) o[i] = oo[i].charCodeAt(0);
  16408. return o;
  16409. }
  16410. /* [MS-OFFCRYPTO] 2.1.4 Version */
  16411. function parse_CRYPTOVersion(blob, length) {
  16412. var o = {};
  16413. o.Major = blob.read_shift(2);
  16414. o.Minor = blob.read_shift(2);
  16415. if(length >= 4) blob.l += length - 4;
  16416. return o;
  16417. }
  16418. /* [MS-OFFCRYPTO] 2.1.5 DataSpaceVersionInfo */
  16419. function parse_DataSpaceVersionInfo(blob) {
  16420. var o = {};
  16421. o.id = blob.read_shift(0, 'lpp4');
  16422. o.R = parse_CRYPTOVersion(blob, 4);
  16423. o.U = parse_CRYPTOVersion(blob, 4);
  16424. o.W = parse_CRYPTOVersion(blob, 4);
  16425. return o;
  16426. }
  16427. /* [MS-OFFCRYPTO] 2.1.6.1 DataSpaceMapEntry Structure */
  16428. function parse_DataSpaceMapEntry(blob) {
  16429. var len = blob.read_shift(4);
  16430. var end = blob.l + len - 4;
  16431. var o = {};
  16432. var cnt = blob.read_shift(4);
  16433. var comps = [];
  16434. /* [MS-OFFCRYPTO] 2.1.6.2 DataSpaceReferenceComponent Structure */
  16435. while(cnt-- > 0) comps.push({ t: blob.read_shift(4), v: blob.read_shift(0, 'lpp4') });
  16436. o.name = blob.read_shift(0, 'lpp4');
  16437. o.comps = comps;
  16438. if(blob.l != end) throw new Error("Bad DataSpaceMapEntry: " + blob.l + " != " + end);
  16439. return o;
  16440. }
  16441. /* [MS-OFFCRYPTO] 2.1.6 DataSpaceMap */
  16442. function parse_DataSpaceMap(blob) {
  16443. var o = [];
  16444. blob.l += 4; // must be 0x8
  16445. var cnt = blob.read_shift(4);
  16446. while(cnt-- > 0) o.push(parse_DataSpaceMapEntry(blob));
  16447. return o;
  16448. }
  16449. /* [MS-OFFCRYPTO] 2.1.7 DataSpaceDefinition */
  16450. function parse_DataSpaceDefinition(blob) {
  16451. var o = [];
  16452. blob.l += 4; // must be 0x8
  16453. var cnt = blob.read_shift(4);
  16454. while(cnt-- > 0) o.push(blob.read_shift(0, 'lpp4'));
  16455. return o;
  16456. }
  16457. /* [MS-OFFCRYPTO] 2.1.8 DataSpaceDefinition */
  16458. function parse_TransformInfoHeader(blob) {
  16459. var o = {};
  16460. /*var len = */blob.read_shift(4);
  16461. blob.l += 4; // must be 0x1
  16462. o.id = blob.read_shift(0, 'lpp4');
  16463. o.name = blob.read_shift(0, 'lpp4');
  16464. o.R = parse_CRYPTOVersion(blob, 4);
  16465. o.U = parse_CRYPTOVersion(blob, 4);
  16466. o.W = parse_CRYPTOVersion(blob, 4);
  16467. return o;
  16468. }
  16469. function parse_Primary(blob) {
  16470. /* [MS-OFFCRYPTO] 2.2.6 IRMDSTransformInfo */
  16471. var hdr = parse_TransformInfoHeader(blob);
  16472. /* [MS-OFFCRYPTO] 2.1.9 EncryptionTransformInfo */
  16473. hdr.ename = blob.read_shift(0, '8lpp4');
  16474. hdr.blksz = blob.read_shift(4);
  16475. hdr.cmode = blob.read_shift(4);
  16476. if(blob.read_shift(4) != 0x04) throw new Error("Bad !Primary record");
  16477. return hdr;
  16478. }
  16479. /* [MS-OFFCRYPTO] 2.3.2 Encryption Header */
  16480. function parse_EncryptionHeader(blob, length) {
  16481. var tgt = blob.l + length;
  16482. var o = {};
  16483. o.Flags = (blob.read_shift(4) & 0x3F);
  16484. blob.l += 4;
  16485. o.AlgID = blob.read_shift(4);
  16486. var valid = false;
  16487. switch(o.AlgID) {
  16488. case 0x660E: case 0x660F: case 0x6610: valid = (o.Flags == 0x24); break;
  16489. case 0x6801: valid = (o.Flags == 0x04); break;
  16490. case 0: valid = (o.Flags == 0x10 || o.Flags == 0x04 || o.Flags == 0x24); break;
  16491. default: throw 'Unrecognized encryption algorithm: ' + o.AlgID;
  16492. }
  16493. if(!valid) throw new Error("Encryption Flags/AlgID mismatch");
  16494. o.AlgIDHash = blob.read_shift(4);
  16495. o.KeySize = blob.read_shift(4);
  16496. o.ProviderType = blob.read_shift(4);
  16497. blob.l += 8;
  16498. o.CSPName = blob.read_shift((tgt-blob.l)>>1, 'utf16le');
  16499. blob.l = tgt;
  16500. return o;
  16501. }
  16502. /* [MS-OFFCRYPTO] 2.3.3 Encryption Verifier */
  16503. function parse_EncryptionVerifier(blob, length) {
  16504. var o = {}, tgt = blob.l + length;
  16505. blob.l += 4; // SaltSize must be 0x10
  16506. o.Salt = blob.slice(blob.l, blob.l+16); blob.l += 16;
  16507. o.Verifier = blob.slice(blob.l, blob.l+16); blob.l += 16;
  16508. /*var sz = */blob.read_shift(4);
  16509. o.VerifierHash = blob.slice(blob.l, tgt); blob.l = tgt;
  16510. return o;
  16511. }
  16512. /* [MS-OFFCRYPTO] 2.3.4.* EncryptionInfo Stream */
  16513. function parse_EncryptionInfo(blob) {
  16514. var vers = parse_CRYPTOVersion(blob);
  16515. switch(vers.Minor) {
  16516. case 0x02: return [vers.Minor, parse_EncInfoStd(blob, vers)];
  16517. case 0x03: return [vers.Minor, parse_EncInfoExt(blob, vers)];
  16518. case 0x04: return [vers.Minor, parse_EncInfoAgl(blob, vers)];
  16519. }
  16520. throw new Error("ECMA-376 Encrypted file unrecognized Version: " + vers.Minor);
  16521. }
  16522. /* [MS-OFFCRYPTO] 2.3.4.5 EncryptionInfo Stream (Standard Encryption) */
  16523. function parse_EncInfoStd(blob) {
  16524. var flags = blob.read_shift(4);
  16525. if((flags & 0x3F) != 0x24) throw new Error("EncryptionInfo mismatch");
  16526. var sz = blob.read_shift(4);
  16527. //var tgt = blob.l + sz;
  16528. var hdr = parse_EncryptionHeader(blob, sz);
  16529. var verifier = parse_EncryptionVerifier(blob, blob.length - blob.l);
  16530. return { t:"Std", h:hdr, v:verifier };
  16531. }
  16532. /* [MS-OFFCRYPTO] 2.3.4.6 EncryptionInfo Stream (Extensible Encryption) */
  16533. function parse_EncInfoExt() { throw new Error("File is password-protected: ECMA-376 Extensible"); }
  16534. /* [MS-OFFCRYPTO] 2.3.4.10 EncryptionInfo Stream (Agile Encryption) */
  16535. function parse_EncInfoAgl(blob) {
  16536. var KeyData = ["saltSize","blockSize","keyBits","hashSize","cipherAlgorithm","cipherChaining","hashAlgorithm","saltValue"];
  16537. blob.l+=4;
  16538. var xml = blob.read_shift(blob.length - blob.l, 'utf8');
  16539. var o = {};
  16540. xml.replace(tagregex, function xml_agile(x) {
  16541. var y = parsexmltag(x);
  16542. switch(strip_ns(y[0])) {
  16543. case '<?xml': break;
  16544. case '<encryption': case '</encryption>': break;
  16545. case '<keyData': KeyData.forEach(function(k) { o[k] = y[k]; }); break;
  16546. case '<dataIntegrity': o.encryptedHmacKey = y.encryptedHmacKey; o.encryptedHmacValue = y.encryptedHmacValue; break;
  16547. case '<keyEncryptors>': case '<keyEncryptors': o.encs = []; break;
  16548. case '</keyEncryptors>': break;
  16549. case '<keyEncryptor': o.uri = y.uri; break;
  16550. case '</keyEncryptor>': break;
  16551. case '<encryptedKey': o.encs.push(y); break;
  16552. default: throw y[0];
  16553. }
  16554. });
  16555. return o;
  16556. }
  16557. /* [MS-OFFCRYPTO] 2.3.5.1 RC4 CryptoAPI Encryption Header */
  16558. function parse_RC4CryptoHeader(blob, length) {
  16559. var o = {};
  16560. var vers = o.EncryptionVersionInfo = parse_CRYPTOVersion(blob, 4); length -= 4;
  16561. if(vers.Minor != 2) throw new Error('unrecognized minor version code: ' + vers.Minor);
  16562. if(vers.Major > 4 || vers.Major < 2) throw new Error('unrecognized major version code: ' + vers.Major);
  16563. o.Flags = blob.read_shift(4); length -= 4;
  16564. var sz = blob.read_shift(4); length -= 4;
  16565. o.EncryptionHeader = parse_EncryptionHeader(blob, sz); length -= sz;
  16566. o.EncryptionVerifier = parse_EncryptionVerifier(blob, length);
  16567. return o;
  16568. }
  16569. /* [MS-OFFCRYPTO] 2.3.6.1 RC4 Encryption Header */
  16570. function parse_RC4Header(blob) {
  16571. var o = {};
  16572. var vers = o.EncryptionVersionInfo = parse_CRYPTOVersion(blob, 4);
  16573. if(vers.Major != 1 || vers.Minor != 1) throw 'unrecognized version code ' + vers.Major + ' : ' + vers.Minor;
  16574. o.Salt = blob.read_shift(16);
  16575. o.EncryptedVerifier = blob.read_shift(16);
  16576. o.EncryptedVerifierHash = blob.read_shift(16);
  16577. return o;
  16578. }
  16579. /* [MS-OFFCRYPTO] 2.3.7.1 Binary Document Password Verifier Derivation */
  16580. function crypto_CreatePasswordVerifier_Method1(Password) {
  16581. var Verifier = 0x0000, PasswordArray;
  16582. var PasswordDecoded = _JS2ANSI(Password);
  16583. var len = PasswordDecoded.length + 1, i, PasswordByte;
  16584. var Intermediate1, Intermediate2, Intermediate3;
  16585. PasswordArray = new_raw_buf(len);
  16586. PasswordArray[0] = PasswordDecoded.length;
  16587. for(i = 1; i != len; ++i) PasswordArray[i] = PasswordDecoded[i-1];
  16588. for(i = len-1; i >= 0; --i) {
  16589. PasswordByte = PasswordArray[i];
  16590. Intermediate1 = ((Verifier & 0x4000) === 0x0000) ? 0 : 1;
  16591. Intermediate2 = (Verifier << 1) & 0x7FFF;
  16592. Intermediate3 = Intermediate1 | Intermediate2;
  16593. Verifier = Intermediate3 ^ PasswordByte;
  16594. }
  16595. return Verifier ^ 0xCE4B;
  16596. }
  16597. /* [MS-OFFCRYPTO] 2.3.7.2 Binary Document XOR Array Initialization */
  16598. var crypto_CreateXorArray_Method1 = (function() {
  16599. var PadArray = [0xBB, 0xFF, 0xFF, 0xBA, 0xFF, 0xFF, 0xB9, 0x80, 0x00, 0xBE, 0x0F, 0x00, 0xBF, 0x0F, 0x00];
  16600. var InitialCode = [0xE1F0, 0x1D0F, 0xCC9C, 0x84C0, 0x110C, 0x0E10, 0xF1CE, 0x313E, 0x1872, 0xE139, 0xD40F, 0x84F9, 0x280C, 0xA96A, 0x4EC3];
  16601. var XorMatrix = [0xAEFC, 0x4DD9, 0x9BB2, 0x2745, 0x4E8A, 0x9D14, 0x2A09, 0x7B61, 0xF6C2, 0xFDA5, 0xEB6B, 0xC6F7, 0x9DCF, 0x2BBF, 0x4563, 0x8AC6, 0x05AD, 0x0B5A, 0x16B4, 0x2D68, 0x5AD0, 0x0375, 0x06EA, 0x0DD4, 0x1BA8, 0x3750, 0x6EA0, 0xDD40, 0xD849, 0xA0B3, 0x5147, 0xA28E, 0x553D, 0xAA7A, 0x44D5, 0x6F45, 0xDE8A, 0xAD35, 0x4A4B, 0x9496, 0x390D, 0x721A, 0xEB23, 0xC667, 0x9CEF, 0x29FF, 0x53FE, 0xA7FC, 0x5FD9, 0x47D3, 0x8FA6, 0x0F6D, 0x1EDA, 0x3DB4, 0x7B68, 0xF6D0, 0xB861, 0x60E3, 0xC1C6, 0x93AD, 0x377B, 0x6EF6, 0xDDEC, 0x45A0, 0x8B40, 0x06A1, 0x0D42, 0x1A84, 0x3508, 0x6A10, 0xAA51, 0x4483, 0x8906, 0x022D, 0x045A, 0x08B4, 0x1168, 0x76B4, 0xED68, 0xCAF1, 0x85C3, 0x1BA7, 0x374E, 0x6E9C, 0x3730, 0x6E60, 0xDCC0, 0xA9A1, 0x4363, 0x86C6, 0x1DAD, 0x3331, 0x6662, 0xCCC4, 0x89A9, 0x0373, 0x06E6, 0x0DCC, 0x1021, 0x2042, 0x4084, 0x8108, 0x1231, 0x2462, 0x48C4];
  16602. var Ror = function(Byte) { return ((Byte/2) | (Byte*128)) & 0xFF; };
  16603. var XorRor = function(byte1, byte2) { return Ror(byte1 ^ byte2); };
  16604. var CreateXorKey_Method1 = function(Password) {
  16605. var XorKey = InitialCode[Password.length - 1];
  16606. var CurrentElement = 0x68;
  16607. for(var i = Password.length-1; i >= 0; --i) {
  16608. var Char = Password[i];
  16609. for(var j = 0; j != 7; ++j) {
  16610. if(Char & 0x40) XorKey ^= XorMatrix[CurrentElement];
  16611. Char *= 2; --CurrentElement;
  16612. }
  16613. }
  16614. return XorKey;
  16615. };
  16616. return function(password) {
  16617. var Password = _JS2ANSI(password);
  16618. var XorKey = CreateXorKey_Method1(Password);
  16619. var Index = Password.length;
  16620. var ObfuscationArray = new_raw_buf(16);
  16621. for(var i = 0; i != 16; ++i) ObfuscationArray[i] = 0x00;
  16622. var Temp, PasswordLastChar, PadIndex;
  16623. if((Index & 1) === 1) {
  16624. Temp = XorKey >> 8;
  16625. ObfuscationArray[Index] = XorRor(PadArray[0], Temp);
  16626. --Index;
  16627. Temp = XorKey & 0xFF;
  16628. PasswordLastChar = Password[Password.length - 1];
  16629. ObfuscationArray[Index] = XorRor(PasswordLastChar, Temp);
  16630. }
  16631. while(Index > 0) {
  16632. --Index;
  16633. Temp = XorKey >> 8;
  16634. ObfuscationArray[Index] = XorRor(Password[Index], Temp);
  16635. --Index;
  16636. Temp = XorKey & 0xFF;
  16637. ObfuscationArray[Index] = XorRor(Password[Index], Temp);
  16638. }
  16639. Index = 15;
  16640. PadIndex = 15 - Password.length;
  16641. while(PadIndex > 0) {
  16642. Temp = XorKey >> 8;
  16643. ObfuscationArray[Index] = XorRor(PadArray[PadIndex], Temp);
  16644. --Index;
  16645. --PadIndex;
  16646. Temp = XorKey & 0xFF;
  16647. ObfuscationArray[Index] = XorRor(Password[Index], Temp);
  16648. --Index;
  16649. --PadIndex;
  16650. }
  16651. return ObfuscationArray;
  16652. };
  16653. })();
  16654. /* [MS-OFFCRYPTO] 2.3.7.3 Binary Document XOR Data Transformation Method 1 */
  16655. var crypto_DecryptData_Method1 = function(password, Data, XorArrayIndex, XorArray, O) {
  16656. /* If XorArray is set, use it; if O is not set, make changes in-place */
  16657. if(!O) O = Data;
  16658. if(!XorArray) XorArray = crypto_CreateXorArray_Method1(password);
  16659. var Index, Value;
  16660. for(Index = 0; Index != Data.length; ++Index) {
  16661. Value = Data[Index];
  16662. Value ^= XorArray[XorArrayIndex];
  16663. Value = ((Value>>5) | (Value<<3)) & 0xFF;
  16664. O[Index] = Value;
  16665. ++XorArrayIndex;
  16666. }
  16667. return [O, XorArrayIndex, XorArray];
  16668. };
  16669. var crypto_MakeXorDecryptor = function(password) {
  16670. var XorArrayIndex = 0, XorArray = crypto_CreateXorArray_Method1(password);
  16671. return function(Data) {
  16672. var O = crypto_DecryptData_Method1("", Data, XorArrayIndex, XorArray);
  16673. XorArrayIndex = O[1];
  16674. return O[0];
  16675. };
  16676. };
  16677. /* 2.5.343 */
  16678. function parse_XORObfuscation(blob, length, opts, out) {
  16679. var o = ({ key: parseuint16(blob), verificationBytes: parseuint16(blob) });
  16680. if(opts.password) o.verifier = crypto_CreatePasswordVerifier_Method1(opts.password);
  16681. out.valid = o.verificationBytes === o.verifier;
  16682. if(out.valid) out.insitu = crypto_MakeXorDecryptor(opts.password);
  16683. return o;
  16684. }
  16685. /* 2.4.117 */
  16686. function parse_FilePassHeader(blob, length, oo) {
  16687. var o = oo || {}; o.Info = blob.read_shift(2); blob.l -= 2;
  16688. if(o.Info === 1) o.Data = parse_RC4Header(blob, length);
  16689. else o.Data = parse_RC4CryptoHeader(blob, length);
  16690. return o;
  16691. }
  16692. function parse_FilePass(blob, length, opts) {
  16693. var o = ({ Type: opts.biff >= 8 ? blob.read_shift(2) : 0 }); /* wEncryptionType */
  16694. if(o.Type) parse_FilePassHeader(blob, length-2, o);
  16695. else parse_XORObfuscation(blob, opts.biff >= 8 ? length : length - 2, opts, o);
  16696. return o;
  16697. }
  16698. var RTF = (function() {
  16699. function rtf_to_sheet(d, opts) {
  16700. switch(opts.type) {
  16701. case 'base64': return rtf_to_sheet_str(Base64.decode(d), opts);
  16702. case 'binary': return rtf_to_sheet_str(d, opts);
  16703. case 'buffer': return rtf_to_sheet_str(d.toString('binary'), opts);
  16704. case 'array': return rtf_to_sheet_str(cc2str(d), opts);
  16705. }
  16706. throw new Error("Unrecognized type " + opts.type);
  16707. }
  16708. function rtf_to_sheet_str(str, opts) {
  16709. var o = opts || {};
  16710. var ws = o.dense ? ([]) : ({});
  16711. var range = ({s: {c:0, r:0}, e: {c:0, r:0}});
  16712. // TODO: parse
  16713. if(!str.match(/\\trowd/)) throw new Error("RTF missing table");
  16714. ws['!ref'] = encode_range(range);
  16715. return ws;
  16716. }
  16717. function rtf_to_workbook(d, opts) { return sheet_to_workbook(rtf_to_sheet(d, opts), opts); }
  16718. /* TODO: this is a stub */
  16719. function sheet_to_rtf(ws) {
  16720. var o = ["{\\rtf1\\ansi"];
  16721. var r = safe_decode_range(ws['!ref']), cell;
  16722. var dense = Array.isArray(ws);
  16723. for(var R = r.s.r; R <= r.e.r; ++R) {
  16724. o.push("\\trowd\\trautofit1");
  16725. for(var C = r.s.c; C <= r.e.c; ++C) o.push("\\cellx" + (C+1));
  16726. o.push("\\pard\\intbl");
  16727. for(C = r.s.c; C <= r.e.c; ++C) {
  16728. var coord = encode_cell({r:R,c:C});
  16729. cell = dense ? (ws[R]||[])[C]: ws[coord];
  16730. if(!cell || cell.v == null && (!cell.f || cell.F)) continue;
  16731. o.push(" " + (cell.w || (format_cell(cell), cell.w)));
  16732. o.push("\\cell");
  16733. }
  16734. o.push("\\pard\\intbl\\row");
  16735. }
  16736. return o.join("") + "}";
  16737. }
  16738. return {
  16739. to_workbook: rtf_to_workbook,
  16740. to_sheet: rtf_to_sheet,
  16741. from_sheet: sheet_to_rtf
  16742. };
  16743. })();
  16744. function hex2RGB(h) {
  16745. var o = h.slice(h[0]==="#"?1:0).slice(0,6);
  16746. return [parseInt(o.slice(0,2),16),parseInt(o.slice(2,4),16),parseInt(o.slice(4,6),16)];
  16747. }
  16748. function rgb2Hex(rgb) {
  16749. for(var i=0,o=1; i!=3; ++i) o = o*256 + (rgb[i]>255?255:rgb[i]<0?0:rgb[i]);
  16750. return o.toString(16).toUpperCase().slice(1);
  16751. }
  16752. function rgb2HSL(rgb) {
  16753. var R = rgb[0]/255, G = rgb[1]/255, B=rgb[2]/255;
  16754. var M = Math.max(R, G, B), m = Math.min(R, G, B), C = M - m;
  16755. if(C === 0) return [0, 0, R];
  16756. var H6 = 0, S = 0, L2 = (M + m);
  16757. S = C / (L2 > 1 ? 2 - L2 : L2);
  16758. switch(M){
  16759. case R: H6 = ((G - B) / C + 6)%6; break;
  16760. case G: H6 = ((B - R) / C + 2); break;
  16761. case B: H6 = ((R - G) / C + 4); break;
  16762. }
  16763. return [H6 / 6, S, L2 / 2];
  16764. }
  16765. function hsl2RGB(hsl){
  16766. var H = hsl[0], S = hsl[1], L = hsl[2];
  16767. var C = S * 2 * (L < 0.5 ? L : 1 - L), m = L - C/2;
  16768. var rgb = [m,m,m], h6 = 6*H;
  16769. var X;
  16770. if(S !== 0) switch(h6|0) {
  16771. case 0: case 6: X = C * h6; rgb[0] += C; rgb[1] += X; break;
  16772. case 1: X = C * (2 - h6); rgb[0] += X; rgb[1] += C; break;
  16773. case 2: X = C * (h6 - 2); rgb[1] += C; rgb[2] += X; break;
  16774. case 3: X = C * (4 - h6); rgb[1] += X; rgb[2] += C; break;
  16775. case 4: X = C * (h6 - 4); rgb[2] += C; rgb[0] += X; break;
  16776. case 5: X = C * (6 - h6); rgb[2] += X; rgb[0] += C; break;
  16777. }
  16778. for(var i = 0; i != 3; ++i) rgb[i] = Math.round(rgb[i]*255);
  16779. return rgb;
  16780. }
  16781. /* 18.8.3 bgColor tint algorithm */
  16782. function rgb_tint(hex, tint) {
  16783. if(tint === 0) return hex;
  16784. var hsl = rgb2HSL(hex2RGB(hex));
  16785. if (tint < 0) hsl[2] = hsl[2] * (1 + tint);
  16786. else hsl[2] = 1 - (1 - hsl[2]) * (1 - tint);
  16787. return rgb2Hex(hsl2RGB(hsl));
  16788. }
  16789. /* 18.3.1.13 width calculations */
  16790. /* [MS-OI29500] 2.1.595 Column Width & Formatting */
  16791. var DEF_MDW = 6, MAX_MDW = 15, MIN_MDW = 1, MDW = DEF_MDW;
  16792. function width2px(width) { return Math.floor(( width + (Math.round(128/MDW))/256 )* MDW ); }
  16793. function px2char(px) { return (Math.floor((px - 5)/MDW * 100 + 0.5))/100; }
  16794. function char2width(chr) { return (Math.round((chr * MDW + 5)/MDW*256))/256; }
  16795. //function px2char_(px) { return (((px - 5)/MDW * 100 + 0.5))/100; }
  16796. //function char2width_(chr) { return (((chr * MDW + 5)/MDW*256))/256; }
  16797. function cycle_width(collw) { return char2width(px2char(width2px(collw))); }
  16798. /* XLSX/XLSB/XLS specify width in units of MDW */
  16799. function find_mdw_colw(collw) {
  16800. var delta = Math.abs(collw - cycle_width(collw)), _MDW = MDW;
  16801. if(delta > 0.005) for(MDW=MIN_MDW; MDW<MAX_MDW; ++MDW) if(Math.abs(collw - cycle_width(collw)) <= delta) { delta = Math.abs(collw - cycle_width(collw)); _MDW = MDW; }
  16802. MDW = _MDW;
  16803. }
  16804. /* XLML specifies width in terms of pixels */
  16805. /*function find_mdw_wpx(wpx) {
  16806. var delta = Infinity, guess = 0, _MDW = MIN_MDW;
  16807. for(MDW=MIN_MDW; MDW<MAX_MDW; ++MDW) {
  16808. guess = char2width_(px2char_(wpx))*256;
  16809. guess = (guess) % 1;
  16810. if(guess > 0.5) guess--;
  16811. if(Math.abs(guess) < delta) { delta = Math.abs(guess); _MDW = MDW; }
  16812. }
  16813. MDW = _MDW;
  16814. }*/
  16815. function process_col(coll) {
  16816. if(coll.width) {
  16817. coll.wpx = width2px(coll.width);
  16818. coll.wch = px2char(coll.wpx);
  16819. coll.MDW = MDW;
  16820. } else if(coll.wpx) {
  16821. coll.wch = px2char(coll.wpx);
  16822. coll.width = char2width(coll.wch);
  16823. coll.MDW = MDW;
  16824. } else if(typeof coll.wch == 'number') {
  16825. coll.width = char2width(coll.wch);
  16826. coll.wpx = width2px(coll.width);
  16827. coll.MDW = MDW;
  16828. }
  16829. if(coll.customWidth) delete coll.customWidth;
  16830. }
  16831. var DEF_PPI = 96, PPI = DEF_PPI;
  16832. function px2pt(px) { return px * 96 / PPI; }
  16833. function pt2px(pt) { return pt * PPI / 96; }
  16834. /* [MS-EXSPXML3] 2.4.54 ST_enmPattern */
  16835. var XLMLPatternTypeMap = {
  16836. "None": "none",
  16837. "Solid": "solid",
  16838. "Gray50": "mediumGray",
  16839. "Gray75": "darkGray",
  16840. "Gray25": "lightGray",
  16841. "HorzStripe": "darkHorizontal",
  16842. "VertStripe": "darkVertical",
  16843. "ReverseDiagStripe": "darkDown",
  16844. "DiagStripe": "darkUp",
  16845. "DiagCross": "darkGrid",
  16846. "ThickDiagCross": "darkTrellis",
  16847. "ThinHorzStripe": "lightHorizontal",
  16848. "ThinVertStripe": "lightVertical",
  16849. "ThinReverseDiagStripe": "lightDown",
  16850. "ThinHorzCross": "lightGrid"
  16851. };
  16852. /* 18.8.5 borders CT_Borders */
  16853. function parse_borders(t, styles, themes, opts) {
  16854. styles.Borders = [];
  16855. var border = {}/*, sub_border = {}*/;
  16856. var pass = false;
  16857. t[0].match(tagregex).forEach(function(x) {
  16858. var y = parsexmltag(x);
  16859. switch(strip_ns(y[0])) {
  16860. case '<borders': case '<borders>': case '</borders>': break;
  16861. /* 18.8.4 border CT_Border */
  16862. case '<border': case '<border>': case '<border/>':
  16863. border = {};
  16864. if (y.diagonalUp) { border.diagonalUp = y.diagonalUp; }
  16865. if (y.diagonalDown) { border.diagonalDown = y.diagonalDown; }
  16866. styles.Borders.push(border);
  16867. break;
  16868. case '</border>': break;
  16869. /* note: not in spec, appears to be CT_BorderPr */
  16870. case '<left/>': break;
  16871. case '<left': case '<left>': break;
  16872. case '</left>': break;
  16873. /* note: not in spec, appears to be CT_BorderPr */
  16874. case '<right/>': break;
  16875. case '<right': case '<right>': break;
  16876. case '</right>': break;
  16877. /* 18.8.43 top CT_BorderPr */
  16878. case '<top/>': break;
  16879. case '<top': case '<top>': break;
  16880. case '</top>': break;
  16881. /* 18.8.6 bottom CT_BorderPr */
  16882. case '<bottom/>': break;
  16883. case '<bottom': case '<bottom>': break;
  16884. case '</bottom>': break;
  16885. /* 18.8.13 diagonal CT_BorderPr */
  16886. case '<diagonal': case '<diagonal>': case '<diagonal/>': break;
  16887. case '</diagonal>': break;
  16888. /* 18.8.25 horizontal CT_BorderPr */
  16889. case '<horizontal': case '<horizontal>': case '<horizontal/>': break;
  16890. case '</horizontal>': break;
  16891. /* 18.8.44 vertical CT_BorderPr */
  16892. case '<vertical': case '<vertical>': case '<vertical/>': break;
  16893. case '</vertical>': break;
  16894. /* 18.8.37 start CT_BorderPr */
  16895. case '<start': case '<start>': case '<start/>': break;
  16896. case '</start>': break;
  16897. /* 18.8.16 end CT_BorderPr */
  16898. case '<end': case '<end>': case '<end/>': break;
  16899. case '</end>': break;
  16900. /* 18.8.? color CT_Color */
  16901. case '<color': case '<color>': break;
  16902. case '<color/>': case '</color>': break;
  16903. /* 18.2.10 extLst CT_ExtensionList ? */
  16904. case '<extLst': case '<extLst>': case '</extLst>': break;
  16905. case '<ext': pass = true; break;
  16906. case '</ext>': pass = false; break;
  16907. default: if(opts && opts.WTF) {
  16908. if(!pass) throw new Error('unrecognized ' + y[0] + ' in borders');
  16909. }
  16910. }
  16911. });
  16912. }
  16913. /* 18.8.21 fills CT_Fills */
  16914. function parse_fills(t, styles, themes, opts) {
  16915. styles.Fills = [];
  16916. var fill = {};
  16917. var pass = false;
  16918. t[0].match(tagregex).forEach(function(x) {
  16919. var y = parsexmltag(x);
  16920. switch(strip_ns(y[0])) {
  16921. case '<fills': case '<fills>': case '</fills>': break;
  16922. /* 18.8.20 fill CT_Fill */
  16923. case '<fill>': case '<fill': case '<fill/>':
  16924. fill = {}; styles.Fills.push(fill); break;
  16925. case '</fill>': break;
  16926. /* 18.8.24 gradientFill CT_GradientFill */
  16927. case '<gradientFill>': break;
  16928. case '<gradientFill':
  16929. case '</gradientFill>': styles.Fills.push(fill); fill = {}; break;
  16930. /* 18.8.32 patternFill CT_PatternFill */
  16931. case '<patternFill': case '<patternFill>':
  16932. if(y.patternType) fill.patternType = y.patternType;
  16933. break;
  16934. case '<patternFill/>': case '</patternFill>': break;
  16935. /* 18.8.3 bgColor CT_Color */
  16936. case '<bgColor':
  16937. if(!fill.bgColor) fill.bgColor = {};
  16938. if(y.indexed) fill.bgColor.indexed = parseInt(y.indexed, 10);
  16939. if(y.theme) fill.bgColor.theme = parseInt(y.theme, 10);
  16940. if(y.tint) fill.bgColor.tint = parseFloat(y.tint);
  16941. /* Excel uses ARGB strings */
  16942. if(y.rgb) fill.bgColor.rgb = y.rgb.slice(-6);
  16943. break;
  16944. case '<bgColor/>': case '</bgColor>': break;
  16945. /* 18.8.19 fgColor CT_Color */
  16946. case '<fgColor':
  16947. if(!fill.fgColor) fill.fgColor = {};
  16948. if(y.theme) fill.fgColor.theme = parseInt(y.theme, 10);
  16949. if(y.tint) fill.fgColor.tint = parseFloat(y.tint);
  16950. /* Excel uses ARGB strings */
  16951. if(y.rgb) fill.fgColor.rgb = y.rgb.slice(-6);
  16952. break;
  16953. case '<fgColor/>': case '</fgColor>': break;
  16954. /* 18.8.38 stop CT_GradientStop */
  16955. case '<stop': case '<stop/>': break;
  16956. case '</stop>': break;
  16957. /* 18.8.? color CT_Color */
  16958. case '<color': case '<color/>': break;
  16959. case '</color>': break;
  16960. /* 18.2.10 extLst CT_ExtensionList ? */
  16961. case '<extLst': case '<extLst>': case '</extLst>': break;
  16962. case '<ext': pass = true; break;
  16963. case '</ext>': pass = false; break;
  16964. default: if(opts && opts.WTF) {
  16965. if(!pass) throw new Error('unrecognized ' + y[0] + ' in fills');
  16966. }
  16967. }
  16968. });
  16969. }
  16970. /* 18.8.23 fonts CT_Fonts */
  16971. function parse_fonts(t, styles, themes, opts) {
  16972. styles.Fonts = [];
  16973. var font = {};
  16974. var pass = false;
  16975. t[0].match(tagregex).forEach(function(x) {
  16976. var y = parsexmltag(x);
  16977. switch(strip_ns(y[0])) {
  16978. case '<fonts': case '<fonts>': case '</fonts>': break;
  16979. /* 18.8.22 font CT_Font */
  16980. case '<font': case '<font>': break;
  16981. case '</font>': case '<font/>':
  16982. styles.Fonts.push(font);
  16983. font = {};
  16984. break;
  16985. /* 18.8.29 name CT_FontName */
  16986. case '<name': if(y.val) font.name = y.val; break;
  16987. case '<name/>': case '</name>': break;
  16988. /* 18.8.2 b CT_BooleanProperty */
  16989. case '<b': font.bold = y.val ? parsexmlbool(y.val) : 1; break;
  16990. case '<b/>': font.bold = 1; break;
  16991. /* 18.8.26 i CT_BooleanProperty */
  16992. case '<i': font.italic = y.val ? parsexmlbool(y.val) : 1; break;
  16993. case '<i/>': font.italic = 1; break;
  16994. /* 18.4.13 u CT_UnderlineProperty */
  16995. case '<u':
  16996. switch(y.val) {
  16997. case "none": font.underline = 0x00; break;
  16998. case "single": font.underline = 0x01; break;
  16999. case "double": font.underline = 0x02; break;
  17000. case "singleAccounting": font.underline = 0x21; break;
  17001. case "doubleAccounting": font.underline = 0x22; break;
  17002. } break;
  17003. case '<u/>': font.underline = 1; break;
  17004. /* 18.4.10 strike CT_BooleanProperty */
  17005. case '<strike': font.strike = y.val ? parsexmlbool(y.val) : 1; break;
  17006. case '<strike/>': font.strike = 1; break;
  17007. /* 18.4.2 outline CT_BooleanProperty */
  17008. case '<outline': font.outline = y.val ? parsexmlbool(y.val) : 1; break;
  17009. case '<outline/>': font.outline = 1; break;
  17010. /* 18.8.36 shadow CT_BooleanProperty */
  17011. case '<shadow': font.shadow = y.val ? parsexmlbool(y.val) : 1; break;
  17012. case '<shadow/>': font.shadow = 1; break;
  17013. /* 18.8.12 condense CT_BooleanProperty */
  17014. case '<condense': font.condense = y.val ? parsexmlbool(y.val) : 1; break;
  17015. case '<condense/>': font.condense = 1; break;
  17016. /* 18.8.17 extend CT_BooleanProperty */
  17017. case '<extend': font.extend = y.val ? parsexmlbool(y.val) : 1; break;
  17018. case '<extend/>': font.extend = 1; break;
  17019. /* 18.4.11 sz CT_FontSize */
  17020. case '<sz': if(y.val) font.sz = +y.val; break;
  17021. case '<sz/>': case '</sz>': break;
  17022. /* 18.4.14 vertAlign CT_VerticalAlignFontProperty */
  17023. case '<vertAlign': if(y.val) font.vertAlign = y.val; break;
  17024. case '<vertAlign/>': case '</vertAlign>': break;
  17025. /* 18.8.18 family CT_FontFamily */
  17026. case '<family': if(y.val) font.family = parseInt(y.val,10); break;
  17027. case '<family/>': case '</family>': break;
  17028. /* 18.8.35 scheme CT_FontScheme */
  17029. case '<scheme': if(y.val) font.scheme = y.val; break;
  17030. case '<scheme/>': case '</scheme>': break;
  17031. /* 18.4.1 charset CT_IntProperty */
  17032. case '<charset':
  17033. if(y.val == '1') break;
  17034. y.codepage = CS2CP[parseInt(y.val, 10)];
  17035. break;
  17036. /* 18.?.? color CT_Color */
  17037. case '<color':
  17038. if(!font.color) font.color = {};
  17039. if(y.auto) font.color.auto = parsexmlbool(y.auto);
  17040. if(y.rgb) font.color.rgb = y.rgb.slice(-6);
  17041. else if(y.indexed) {
  17042. font.color.index = parseInt(y.indexed, 10);
  17043. var icv = XLSIcv[font.color.index];
  17044. if(font.color.index == 81) icv = XLSIcv[1];
  17045. if(!icv) throw new Error(x);
  17046. font.color.rgb = icv[0].toString(16) + icv[1].toString(16) + icv[2].toString(16);
  17047. } else if(y.theme) {
  17048. font.color.theme = parseInt(y.theme, 10);
  17049. if(y.tint) font.color.tint = parseFloat(y.tint);
  17050. if(y.theme && themes.themeElements && themes.themeElements.clrScheme) {
  17051. font.color.rgb = rgb_tint(themes.themeElements.clrScheme[font.color.theme].rgb, font.color.tint || 0);
  17052. }
  17053. }
  17054. break;
  17055. case '<color/>': case '</color>': break;
  17056. /* 18.2.10 extLst CT_ExtensionList ? */
  17057. case '<extLst': case '<extLst>': case '</extLst>': break;
  17058. case '<ext': pass = true; break;
  17059. case '</ext>': pass = false; break;
  17060. default: if(opts && opts.WTF) {
  17061. if(!pass) throw new Error('unrecognized ' + y[0] + ' in fonts');
  17062. }
  17063. }
  17064. });
  17065. }
  17066. /* 18.8.31 numFmts CT_NumFmts */
  17067. function parse_numFmts(t, styles, opts) {
  17068. styles.NumberFmt = [];
  17069. var k/*Array<number>*/ = (keys(SSF._table));
  17070. for(var i=0; i < k.length; ++i) styles.NumberFmt[k[i]] = SSF._table[k[i]];
  17071. var m = t[0].match(tagregex);
  17072. if(!m) return;
  17073. for(i=0; i < m.length; ++i) {
  17074. var y = parsexmltag(m[i]);
  17075. switch(strip_ns(y[0])) {
  17076. case '<numFmts': case '</numFmts>': case '<numFmts/>': case '<numFmts>': break;
  17077. case '<numFmt': {
  17078. var f=unescapexml(utf8read(y.formatCode)), j=parseInt(y.numFmtId,10);
  17079. styles.NumberFmt[j] = f;
  17080. if(j>0) {
  17081. if(j > 0x188) {
  17082. for(j = 0x188; j > 0x3c; --j) if(styles.NumberFmt[j] == null) break;
  17083. styles.NumberFmt[j] = f;
  17084. }
  17085. SSF.load(f,j);
  17086. }
  17087. } break;
  17088. case '</numFmt>': break;
  17089. default: if(opts.WTF) throw new Error('unrecognized ' + y[0] + ' in numFmts');
  17090. }
  17091. }
  17092. }
  17093. function write_numFmts(NF) {
  17094. var o = ["<numFmts>"];
  17095. [[5,8],[23,26],[41,44],[/*63*/50,/*66],[164,*/392]].forEach(function(r) {
  17096. for(var i = r[0]; i <= r[1]; ++i) if(NF[i] != null) o[o.length] = (writextag('numFmt',null,{numFmtId:i,formatCode:escapexml(NF[i])}));
  17097. });
  17098. if(o.length === 1) return "";
  17099. o[o.length] = ("</numFmts>");
  17100. o[0] = writextag('numFmts', null, { count:o.length-2 }).replace("/>", ">");
  17101. return o.join("");
  17102. }
  17103. /* 18.8.10 cellXfs CT_CellXfs */
  17104. var cellXF_uint = [ "numFmtId", "fillId", "fontId", "borderId", "xfId" ];
  17105. var cellXF_bool = [ "applyAlignment", "applyBorder", "applyFill", "applyFont", "applyNumberFormat", "applyProtection", "pivotButton", "quotePrefix" ];
  17106. function parse_cellXfs(t, styles, opts) {
  17107. styles.CellXf = [];
  17108. var xf;
  17109. var pass = false;
  17110. t[0].match(tagregex).forEach(function(x) {
  17111. var y = parsexmltag(x), i = 0;
  17112. switch(strip_ns(y[0])) {
  17113. case '<cellXfs': case '<cellXfs>': case '<cellXfs/>': case '</cellXfs>': break;
  17114. /* 18.8.45 xf CT_Xf */
  17115. case '<xf': case '<xf/>':
  17116. xf = y;
  17117. delete xf[0];
  17118. for(i = 0; i < cellXF_uint.length; ++i) if(xf[cellXF_uint[i]])
  17119. xf[cellXF_uint[i]] = parseInt(xf[cellXF_uint[i]], 10);
  17120. for(i = 0; i < cellXF_bool.length; ++i) if(xf[cellXF_bool[i]])
  17121. xf[cellXF_bool[i]] = parsexmlbool(xf[cellXF_bool[i]]);
  17122. if(xf.numFmtId > 0x188) {
  17123. for(i = 0x188; i > 0x3c; --i) if(styles.NumberFmt[xf.numFmtId] == styles.NumberFmt[i]) { xf.numFmtId = i; break; }
  17124. }
  17125. styles.CellXf.push(xf); break;
  17126. case '</xf>': break;
  17127. /* 18.8.1 alignment CT_CellAlignment */
  17128. case '<alignment': case '<alignment/>':
  17129. var alignment = {};
  17130. if(y.vertical) alignment.vertical = y.vertical;
  17131. if(y.horizontal) alignment.horizontal = y.horizontal;
  17132. if(y.textRotation != null) alignment.textRotation = y.textRotation;
  17133. if(y.indent) alignment.indent = y.indent;
  17134. if(y.wrapText) alignment.wrapText = y.wrapText;
  17135. xf.alignment = alignment;
  17136. break;
  17137. case '</alignment>': break;
  17138. /* 18.8.33 protection CT_CellProtection */
  17139. case '<protection': case '</protection>': case '<protection/>': break;
  17140. /* 18.2.10 extLst CT_ExtensionList ? */
  17141. case '<extLst': case '<extLst>': case '</extLst>': break;
  17142. case '<ext': pass = true; break;
  17143. case '</ext>': pass = false; break;
  17144. default: if(opts && opts.WTF) {
  17145. if(!pass) throw new Error('unrecognized ' + y[0] + ' in cellXfs');
  17146. }
  17147. }
  17148. });
  17149. }
  17150. function write_cellXfs(cellXfs) {
  17151. var o = [];
  17152. o[o.length] = (writextag('cellXfs',null));
  17153. cellXfs.forEach(function(c) { o[o.length] = (writextag('xf', null, c)); });
  17154. o[o.length] = ("</cellXfs>");
  17155. if(o.length === 2) return "";
  17156. o[0] = writextag('cellXfs',null, {count:o.length-2}).replace("/>",">");
  17157. return o.join("");
  17158. }
  17159. /* 18.8 Styles CT_Stylesheet*/
  17160. var parse_sty_xml= (function make_pstyx() {
  17161. var numFmtRegex = /<(?:\w+:)?numFmts([^>]*)>[\S\s]*?<\/(?:\w+:)?numFmts>/;
  17162. var cellXfRegex = /<(?:\w+:)?cellXfs([^>]*)>[\S\s]*?<\/(?:\w+:)?cellXfs>/;
  17163. var fillsRegex = /<(?:\w+:)?fills([^>]*)>[\S\s]*?<\/(?:\w+:)?fills>/;
  17164. var fontsRegex = /<(?:\w+:)?fonts([^>]*)>[\S\s]*?<\/(?:\w+:)?fonts>/;
  17165. var bordersRegex = /<(?:\w+:)?borders([^>]*)>[\S\s]*?<\/(?:\w+:)?borders>/;
  17166. return function parse_sty_xml(data, themes, opts) {
  17167. var styles = {};
  17168. if(!data) return styles;
  17169. data = data.replace(/<!--([\s\S]*?)-->/mg,"").replace(/<!DOCTYPE[^\[]*\[[^\]]*\]>/gm,"");
  17170. /* 18.8.39 styleSheet CT_Stylesheet */
  17171. var t;
  17172. /* 18.8.31 numFmts CT_NumFmts ? */
  17173. if((t=data.match(numFmtRegex))) parse_numFmts(t, styles, opts);
  17174. /* 18.8.23 fonts CT_Fonts ? */
  17175. if((t=data.match(fontsRegex))) parse_fonts(t, styles, themes, opts);
  17176. /* 18.8.21 fills CT_Fills ? */
  17177. if((t=data.match(fillsRegex))) parse_fills(t, styles, themes, opts);
  17178. /* 18.8.5 borders CT_Borders ? */
  17179. if((t=data.match(bordersRegex))) parse_borders(t, styles, themes, opts);
  17180. /* 18.8.9 cellStyleXfs CT_CellStyleXfs ? */
  17181. /* 18.8.10 cellXfs CT_CellXfs ? */
  17182. if((t=data.match(cellXfRegex))) parse_cellXfs(t, styles, opts);
  17183. /* 18.8.8 cellStyles CT_CellStyles ? */
  17184. /* 18.8.15 dxfs CT_Dxfs ? */
  17185. /* 18.8.42 tableStyles CT_TableStyles ? */
  17186. /* 18.8.11 colors CT_Colors ? */
  17187. /* 18.2.10 extLst CT_ExtensionList ? */
  17188. return styles;
  17189. };
  17190. })();
  17191. var STYLES_XML_ROOT = writextag('styleSheet', null, {
  17192. 'xmlns': XMLNS.main[0],
  17193. 'xmlns:vt': XMLNS.vt
  17194. });
  17195. RELS.STY = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles";
  17196. function write_sty_xml(wb, opts) {
  17197. if (typeof style_builder != 'undefined' && typeof 'require' != 'undefined') {
  17198. return style_builder.toXml();
  17199. }
  17200. var o = [XML_HEADER, STYLES_XML_ROOT], w;
  17201. if(wb.SSF && (w = write_numFmts(wb.SSF)) != null) o[o.length] = w;
  17202. o[o.length] = ('<fonts count="1"><font><sz val="12"/><color theme="1"/><name val="Calibri"/><family val="2"/><scheme val="minor"/></font></fonts>');
  17203. o[o.length] = ('<fills count="2"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill></fills>');
  17204. o[o.length] = ('<borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders>');
  17205. o[o.length] = ('<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs>');
  17206. if((w = write_cellXfs(opts.cellXfs))) o[o.length] = (w);
  17207. o[o.length] = ('<cellStyles count="1"><cellStyle name="Normal" xfId="0" builtinId="0"/></cellStyles>');
  17208. o[o.length] = ('<dxfs count="0"/>');
  17209. o[o.length] = ('<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleMedium4"/>');
  17210. if(o.length>2){ o[o.length] = ('</styleSheet>'); o[1]=o[1].replace("/>",">"); }
  17211. return o.join("");
  17212. }
  17213. /* [MS-XLSB] 2.4.657 BrtFmt */
  17214. function parse_BrtFmt(data, length) {
  17215. var numFmtId = data.read_shift(2);
  17216. var stFmtCode = parse_XLWideString(data,length-2);
  17217. return [numFmtId, stFmtCode];
  17218. }
  17219. function write_BrtFmt(i, f, o) {
  17220. if(!o) o = new_buf(6 + 4 * f.length);
  17221. o.write_shift(2, i);
  17222. write_XLWideString(f, o);
  17223. var out = (o.length > o.l) ? o.slice(0, o.l) : o;
  17224. if(o.l == null) o.l = o.length;
  17225. return out;
  17226. }
  17227. /* [MS-XLSB] 2.4.659 BrtFont TODO */
  17228. function parse_BrtFont(data, length, opts) {
  17229. var out = ({});
  17230. out.sz = data.read_shift(2) / 20;
  17231. var grbit = parse_FontFlags(data, 2, opts);
  17232. if(grbit.fCondense) out.condense = 1;
  17233. if(grbit.fExtend) out.extend = 1;
  17234. if(grbit.fShadow) out.shadow = 1;
  17235. if(grbit.fOutline) out.outline = 1;
  17236. if(grbit.fStrikeout) out.strike = 1;
  17237. if(grbit.fItalic) out.italic = 1;
  17238. var bls = data.read_shift(2);
  17239. if(bls === 0x02BC) out.bold = 1;
  17240. switch(data.read_shift(2)) {
  17241. /* case 0: out.vertAlign = "baseline"; break; */
  17242. case 1: out.vertAlign = "superscript"; break;
  17243. case 2: out.vertAlign = "subscript"; break;
  17244. }
  17245. var underline = data.read_shift(1);
  17246. if(underline != 0) out.underline = underline;
  17247. var family = data.read_shift(1);
  17248. if(family > 0) out.family = family;
  17249. var bCharSet = data.read_shift(1);
  17250. if(bCharSet > 0) out.charset = bCharSet;
  17251. data.l++;
  17252. out.color = parse_BrtColor(data, 8);
  17253. switch(data.read_shift(1)) {
  17254. /* case 0: out.scheme = "none": break; */
  17255. case 1: out.scheme = "major"; break;
  17256. case 2: out.scheme = "minor"; break;
  17257. }
  17258. out.name = parse_XLWideString(data, length - 21);
  17259. return out;
  17260. }
  17261. function write_BrtFont(font, o) {
  17262. if(!o) o = new_buf(25+4*32);
  17263. o.write_shift(2, font.sz * 20);
  17264. write_FontFlags(font, o);
  17265. o.write_shift(2, font.bold ? 0x02BC : 0x0190);
  17266. var sss = 0;
  17267. if(font.vertAlign == "superscript") sss = 1;
  17268. else if(font.vertAlign == "subscript") sss = 2;
  17269. o.write_shift(2, sss);
  17270. o.write_shift(1, font.underline || 0);
  17271. o.write_shift(1, font.family || 0);
  17272. o.write_shift(1, font.charset || 0);
  17273. o.write_shift(1, 0);
  17274. write_BrtColor(font.color, o);
  17275. var scheme = 0;
  17276. if(font.scheme == "major") scheme = 1;
  17277. if(font.scheme == "minor") scheme = 2;
  17278. o.write_shift(1, scheme);
  17279. write_XLWideString(font.name, o);
  17280. return o.length > o.l ? o.slice(0, o.l) : o;
  17281. }
  17282. /* [MS-XLSB] 2.4.650 BrtFill */
  17283. var XLSBFillPTNames = [
  17284. "none",
  17285. "solid",
  17286. "mediumGray",
  17287. "darkGray",
  17288. "lightGray",
  17289. "darkHorizontal",
  17290. "darkVertical",
  17291. "darkDown",
  17292. "darkUp",
  17293. "darkGrid",
  17294. "darkTrellis",
  17295. "lightHorizontal",
  17296. "lightVertical",
  17297. "lightDown",
  17298. "lightUp",
  17299. "lightGrid",
  17300. "lightTrellis",
  17301. "gray125",
  17302. "gray0625"
  17303. ];
  17304. var rev_XLSBFillPTNames = (evert(XLSBFillPTNames));
  17305. /* TODO: gradient fill representation */
  17306. var parse_BrtFill = parsenoop;
  17307. function write_BrtFill(fill, o) {
  17308. if(!o) o = new_buf(4*3 + 8*7 + 16*1);
  17309. var fls = rev_XLSBFillPTNames[fill.patternType];
  17310. if(fls == null) fls = 0x28;
  17311. o.write_shift(4, fls);
  17312. var j = 0;
  17313. if(fls != 0x28) {
  17314. /* TODO: custom FG Color */
  17315. write_BrtColor({auto:1}, o);
  17316. /* TODO: custom BG Color */
  17317. write_BrtColor({auto:1}, o);
  17318. for(; j < 12; ++j) o.write_shift(4, 0);
  17319. } else {
  17320. for(; j < 4; ++j) o.write_shift(4, 0);
  17321. for(; j < 12; ++j) o.write_shift(4, 0); /* TODO */
  17322. /* iGradientType */
  17323. /* xnumDegree */
  17324. /* xnumFillToLeft */
  17325. /* xnumFillToRight */
  17326. /* xnumFillToTop */
  17327. /* xnumFillToBottom */
  17328. /* cNumStop */
  17329. /* xfillGradientStop */
  17330. }
  17331. return o.length > o.l ? o.slice(0, o.l) : o;
  17332. }
  17333. /* [MS-XLSB] 2.4.824 BrtXF */
  17334. function parse_BrtXF(data, length) {
  17335. var tgt = data.l + length;
  17336. var ixfeParent = data.read_shift(2);
  17337. var ifmt = data.read_shift(2);
  17338. data.l = tgt;
  17339. return {ixfe:ixfeParent, numFmtId:ifmt };
  17340. }
  17341. function write_BrtXF(data, ixfeP, o) {
  17342. if(!o) o = new_buf(16);
  17343. o.write_shift(2, ixfeP||0);
  17344. o.write_shift(2, data.numFmtId||0);
  17345. o.write_shift(2, 0); /* iFont */
  17346. o.write_shift(2, 0); /* iFill */
  17347. o.write_shift(2, 0); /* ixBorder */
  17348. o.write_shift(1, 0); /* trot */
  17349. o.write_shift(1, 0); /* indent */
  17350. o.write_shift(1, 0); /* flags */
  17351. o.write_shift(1, 0); /* flags */
  17352. o.write_shift(1, 0); /* xfGrbitAtr */
  17353. o.write_shift(1, 0);
  17354. return o;
  17355. }
  17356. /* [MS-XLSB] 2.5.4 Blxf TODO */
  17357. function write_Blxf(data, o) {
  17358. if(!o) o = new_buf(10);
  17359. o.write_shift(1, 0); /* dg */
  17360. o.write_shift(1, 0);
  17361. o.write_shift(4, 0); /* color */
  17362. o.write_shift(4, 0); /* color */
  17363. return o;
  17364. }
  17365. /* [MS-XLSB] 2.4.302 BrtBorder TODO */
  17366. var parse_BrtBorder = parsenoop;
  17367. function write_BrtBorder(border, o) {
  17368. if(!o) o = new_buf(51);
  17369. o.write_shift(1, 0); /* diagonal */
  17370. write_Blxf(null, o); /* top */
  17371. write_Blxf(null, o); /* bottom */
  17372. write_Blxf(null, o); /* left */
  17373. write_Blxf(null, o); /* right */
  17374. write_Blxf(null, o); /* diag */
  17375. return o.length > o.l ? o.slice(0, o.l) : o;
  17376. }
  17377. /* [MS-XLSB] 2.4.763 BrtStyle TODO */
  17378. function write_BrtStyle(style, o) {
  17379. if(!o) o = new_buf(12+4*10);
  17380. o.write_shift(4, style.xfId);
  17381. o.write_shift(2, 1);
  17382. o.write_shift(1, +style.builtinId);
  17383. o.write_shift(1, 0); /* iLevel */
  17384. write_XLNullableWideString(style.name || "", o);
  17385. return o.length > o.l ? o.slice(0, o.l) : o;
  17386. }
  17387. /* [MS-XLSB] 2.4.272 BrtBeginTableStyles */
  17388. function write_BrtBeginTableStyles(cnt, defTableStyle, defPivotStyle) {
  17389. var o = new_buf(4+256*2*4);
  17390. o.write_shift(4, cnt);
  17391. write_XLNullableWideString(defTableStyle, o);
  17392. write_XLNullableWideString(defPivotStyle, o);
  17393. return o.length > o.l ? o.slice(0, o.l) : o;
  17394. }
  17395. /* [MS-XLSB] 2.1.7.50 Styles */
  17396. function parse_sty_bin(data, themes, opts) {
  17397. var styles = {};
  17398. styles.NumberFmt = ([]);
  17399. for(var y in SSF._table) styles.NumberFmt[y] = SSF._table[y];
  17400. styles.CellXf = [];
  17401. styles.Fonts = [];
  17402. var state = [];
  17403. var pass = false;
  17404. recordhopper(data, function hopper_sty(val, R_n, RT) {
  17405. switch(RT) {
  17406. case 0x002C: /* 'BrtFmt' */
  17407. styles.NumberFmt[val[0]] = val[1]; SSF.load(val[1], val[0]);
  17408. break;
  17409. case 0x002B: /* 'BrtFont' */
  17410. styles.Fonts.push(val);
  17411. if(val.color.theme != null && themes && themes.themeElements && themes.themeElements.clrScheme) {
  17412. val.color.rgb = rgb_tint(themes.themeElements.clrScheme[val.color.theme].rgb, val.color.tint || 0);
  17413. }
  17414. break;
  17415. case 0x0401: /* 'BrtKnownFonts' */ break;
  17416. case 0x002D: /* 'BrtFill' */ break;
  17417. case 0x002E: /* 'BrtBorder' */ break;
  17418. case 0x002F: /* 'BrtXF' */
  17419. if(state[state.length - 1] == "BrtBeginCellXFs") {
  17420. styles.CellXf.push(val);
  17421. }
  17422. break;
  17423. case 0x0030: /* 'BrtStyle' */
  17424. case 0x01FB: /* 'BrtDXF' */
  17425. case 0x023C: /* 'BrtMRUColor' */
  17426. case 0x01DB: /* 'BrtIndexedColor': */
  17427. break;
  17428. case 0x0493: /* 'BrtDXF14' */
  17429. case 0x0836: /* 'BrtDXF15' */
  17430. case 0x046A: /* 'BrtSlicerStyleElement' */
  17431. case 0x0200: /* 'BrtTableStyleElement' */
  17432. case 0x082F: /* 'BrtTimelineStyleElement' */
  17433. case 0x0C00: /* 'BrtUid' */
  17434. break;
  17435. case 0x0023: /* 'BrtFRTBegin' */
  17436. pass = true; break;
  17437. case 0x0024: /* 'BrtFRTEnd' */
  17438. pass = false; break;
  17439. case 0x0025: /* 'BrtACBegin' */
  17440. state.push(R_n); break;
  17441. case 0x0026: /* 'BrtACEnd' */
  17442. state.pop(); break;
  17443. default:
  17444. if((R_n||"").indexOf("Begin") > 0) state.push(R_n);
  17445. else if((R_n||"").indexOf("End") > 0) state.pop();
  17446. else if(!pass || opts.WTF) throw new Error("Unexpected record " + RT + " " + R_n);
  17447. }
  17448. });
  17449. return styles;
  17450. }
  17451. function write_FMTS_bin(ba, NF) {
  17452. if(!NF) return;
  17453. var cnt = 0;
  17454. [[5,8],[23,26],[41,44],[/*63*/50,/*66],[164,*/392]].forEach(function(r) {
  17455. for(var i = r[0]; i <= r[1]; ++i) if(NF[i] != null) ++cnt;
  17456. });
  17457. if(cnt == 0) return;
  17458. write_record(ba, "BrtBeginFmts", write_UInt32LE(cnt));
  17459. [[5,8],[23,26],[41,44],[/*63*/50,/*66],[164,*/392]].forEach(function(r) {
  17460. for(var i = r[0]; i <= r[1]; ++i) if(NF[i] != null) write_record(ba, "BrtFmt", write_BrtFmt(i, NF[i]));
  17461. });
  17462. write_record(ba, "BrtEndFmts");
  17463. }
  17464. function write_FONTS_bin(ba) {
  17465. var cnt = 1;
  17466. if(cnt == 0) return;
  17467. write_record(ba, "BrtBeginFonts", write_UInt32LE(cnt));
  17468. write_record(ba, "BrtFont", write_BrtFont({
  17469. sz:12,
  17470. color: {theme:1},
  17471. name: "Calibri",
  17472. family: 2,
  17473. scheme: "minor"
  17474. }));
  17475. /* 1*65491BrtFont [ACFONTS] */
  17476. write_record(ba, "BrtEndFonts");
  17477. }
  17478. function write_FILLS_bin(ba) {
  17479. var cnt = 2;
  17480. if(cnt == 0) return;
  17481. write_record(ba, "BrtBeginFills", write_UInt32LE(cnt));
  17482. write_record(ba, "BrtFill", write_BrtFill({patternType:"none"}));
  17483. write_record(ba, "BrtFill", write_BrtFill({patternType:"gray125"}));
  17484. /* 1*65431BrtFill */
  17485. write_record(ba, "BrtEndFills");
  17486. }
  17487. function write_BORDERS_bin(ba) {
  17488. var cnt = 1;
  17489. if(cnt == 0) return;
  17490. write_record(ba, "BrtBeginBorders", write_UInt32LE(cnt));
  17491. write_record(ba, "BrtBorder", write_BrtBorder({}));
  17492. /* 1*65430BrtBorder */
  17493. write_record(ba, "BrtEndBorders");
  17494. }
  17495. function write_CELLSTYLEXFS_bin(ba) {
  17496. var cnt = 1;
  17497. write_record(ba, "BrtBeginCellStyleXFs", write_UInt32LE(cnt));
  17498. write_record(ba, "BrtXF", write_BrtXF({
  17499. numFmtId:0,
  17500. fontId:0,
  17501. fillId:0,
  17502. borderId:0
  17503. }, 0xFFFF));
  17504. /* 1*65430(BrtXF *FRT) */
  17505. write_record(ba, "BrtEndCellStyleXFs");
  17506. }
  17507. function write_CELLXFS_bin(ba, data) {
  17508. write_record(ba, "BrtBeginCellXFs", write_UInt32LE(data.length));
  17509. data.forEach(function(c) { write_record(ba, "BrtXF", write_BrtXF(c,0)); });
  17510. /* 1*65430(BrtXF *FRT) */
  17511. write_record(ba, "BrtEndCellXFs");
  17512. }
  17513. function write_STYLES_bin(ba) {
  17514. var cnt = 1;
  17515. write_record(ba, "BrtBeginStyles", write_UInt32LE(cnt));
  17516. write_record(ba, "BrtStyle", write_BrtStyle({
  17517. xfId:0,
  17518. builtinId:0,
  17519. name:"Normal"
  17520. }));
  17521. /* 1*65430(BrtStyle *FRT) */
  17522. write_record(ba, "BrtEndStyles");
  17523. }
  17524. function write_DXFS_bin(ba) {
  17525. var cnt = 0;
  17526. write_record(ba, "BrtBeginDXFs", write_UInt32LE(cnt));
  17527. /* *2147483647(BrtDXF *FRT) */
  17528. write_record(ba, "BrtEndDXFs");
  17529. }
  17530. function write_TABLESTYLES_bin(ba) {
  17531. var cnt = 0;
  17532. write_record(ba, "BrtBeginTableStyles", write_BrtBeginTableStyles(cnt, "TableStyleMedium9", "PivotStyleMedium4"));
  17533. /* *TABLESTYLE */
  17534. write_record(ba, "BrtEndTableStyles");
  17535. }
  17536. function write_COLORPALETTE_bin() {
  17537. return;
  17538. /* BrtBeginColorPalette [INDEXEDCOLORS] [MRUCOLORS] BrtEndColorPalette */
  17539. }
  17540. /* [MS-XLSB] 2.1.7.50 Styles */
  17541. function write_sty_bin(wb, opts) {
  17542. var ba = buf_array();
  17543. write_record(ba, "BrtBeginStyleSheet");
  17544. write_FMTS_bin(ba, wb.SSF);
  17545. write_FONTS_bin(ba, wb);
  17546. write_FILLS_bin(ba, wb);
  17547. write_BORDERS_bin(ba, wb);
  17548. write_CELLSTYLEXFS_bin(ba, wb);
  17549. write_CELLXFS_bin(ba, opts.cellXfs);
  17550. write_STYLES_bin(ba, wb);
  17551. write_DXFS_bin(ba, wb);
  17552. write_TABLESTYLES_bin(ba, wb);
  17553. write_COLORPALETTE_bin(ba, wb);
  17554. /* FRTSTYLESHEET*/
  17555. write_record(ba, "BrtEndStyleSheet");
  17556. return ba.end();
  17557. }
  17558. RELS.THEME = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme";
  17559. /* 20.1.6.2 clrScheme CT_ColorScheme */
  17560. function parse_clrScheme(t, themes, opts) {
  17561. themes.themeElements.clrScheme = [];
  17562. var color = {};
  17563. (t[0].match(tagregex)||[]).forEach(function(x) {
  17564. var y = parsexmltag(x);
  17565. switch(y[0]) {
  17566. /* 20.1.6.2 clrScheme (Color Scheme) CT_ColorScheme */
  17567. case '<a:clrScheme': case '</a:clrScheme>': break;
  17568. /* 20.1.2.3.32 srgbClr CT_SRgbColor */
  17569. case '<a:srgbClr':
  17570. color.rgb = y.val; break;
  17571. /* 20.1.2.3.33 sysClr CT_SystemColor */
  17572. case '<a:sysClr':
  17573. color.rgb = y.lastClr; break;
  17574. /* 20.1.4.1.1 accent1 (Accent 1) */
  17575. /* 20.1.4.1.2 accent2 (Accent 2) */
  17576. /* 20.1.4.1.3 accent3 (Accent 3) */
  17577. /* 20.1.4.1.4 accent4 (Accent 4) */
  17578. /* 20.1.4.1.5 accent5 (Accent 5) */
  17579. /* 20.1.4.1.6 accent6 (Accent 6) */
  17580. /* 20.1.4.1.9 dk1 (Dark 1) */
  17581. /* 20.1.4.1.10 dk2 (Dark 2) */
  17582. /* 20.1.4.1.15 folHlink (Followed Hyperlink) */
  17583. /* 20.1.4.1.19 hlink (Hyperlink) */
  17584. /* 20.1.4.1.22 lt1 (Light 1) */
  17585. /* 20.1.4.1.23 lt2 (Light 2) */
  17586. case '<a:dk1>': case '</a:dk1>':
  17587. case '<a:lt1>': case '</a:lt1>':
  17588. case '<a:dk2>': case '</a:dk2>':
  17589. case '<a:lt2>': case '</a:lt2>':
  17590. case '<a:accent1>': case '</a:accent1>':
  17591. case '<a:accent2>': case '</a:accent2>':
  17592. case '<a:accent3>': case '</a:accent3>':
  17593. case '<a:accent4>': case '</a:accent4>':
  17594. case '<a:accent5>': case '</a:accent5>':
  17595. case '<a:accent6>': case '</a:accent6>':
  17596. case '<a:hlink>': case '</a:hlink>':
  17597. case '<a:folHlink>': case '</a:folHlink>':
  17598. if (y[0].charAt(1) === '/') {
  17599. themes.themeElements.clrScheme.push(color);
  17600. color = {};
  17601. } else {
  17602. color.name = y[0].slice(3, y[0].length - 1);
  17603. }
  17604. break;
  17605. default: if(opts && opts.WTF) throw new Error('Unrecognized ' + y[0] + ' in clrScheme');
  17606. }
  17607. });
  17608. }
  17609. /* 20.1.4.1.18 fontScheme CT_FontScheme */
  17610. function parse_fontScheme() { }
  17611. /* 20.1.4.1.15 fmtScheme CT_StyleMatrix */
  17612. function parse_fmtScheme() { }
  17613. var clrsregex = /<a:clrScheme([^>]*)>[\s\S]*<\/a:clrScheme>/;
  17614. var fntsregex = /<a:fontScheme([^>]*)>[\s\S]*<\/a:fontScheme>/;
  17615. var fmtsregex = /<a:fmtScheme([^>]*)>[\s\S]*<\/a:fmtScheme>/;
  17616. /* 20.1.6.10 themeElements CT_BaseStyles */
  17617. function parse_themeElements(data, themes, opts) {
  17618. themes.themeElements = {};
  17619. var t;
  17620. [
  17621. /* clrScheme CT_ColorScheme */
  17622. ['clrScheme', clrsregex, parse_clrScheme],
  17623. /* fontScheme CT_FontScheme */
  17624. ['fontScheme', fntsregex, parse_fontScheme],
  17625. /* fmtScheme CT_StyleMatrix */
  17626. ['fmtScheme', fmtsregex, parse_fmtScheme]
  17627. ].forEach(function(m) {
  17628. if(!(t=data.match(m[1]))) throw new Error(m[0] + ' not found in themeElements');
  17629. m[2](t, themes, opts);
  17630. });
  17631. }
  17632. var themeltregex = /<a:themeElements([^>]*)>[\s\S]*<\/a:themeElements>/;
  17633. /* 14.2.7 Theme Part */
  17634. function parse_theme_xml(data, opts) {
  17635. /* 20.1.6.9 theme CT_OfficeStyleSheet */
  17636. if(!data || data.length === 0) return parse_theme_xml(write_theme());
  17637. var t;
  17638. var themes = {};
  17639. /* themeElements CT_BaseStyles */
  17640. if(!(t=data.match(themeltregex))) throw new Error('themeElements not found in theme');
  17641. parse_themeElements(t[0], themes, opts);
  17642. return themes;
  17643. }
  17644. function write_theme(Themes, opts) {
  17645. if(opts && opts.themeXLSX) return opts.themeXLSX;
  17646. var o = [XML_HEADER];
  17647. o[o.length] = '<a:theme xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" name="Office Theme">';
  17648. o[o.length] = '<a:themeElements>';
  17649. o[o.length] = '<a:clrScheme name="Office">';
  17650. o[o.length] = '<a:dk1><a:sysClr val="windowText" lastClr="000000"/></a:dk1>';
  17651. o[o.length] = '<a:lt1><a:sysClr val="window" lastClr="FFFFFF"/></a:lt1>';
  17652. o[o.length] = '<a:dk2><a:srgbClr val="1F497D"/></a:dk2>';
  17653. o[o.length] = '<a:lt2><a:srgbClr val="EEECE1"/></a:lt2>';
  17654. o[o.length] = '<a:accent1><a:srgbClr val="4F81BD"/></a:accent1>';
  17655. o[o.length] = '<a:accent2><a:srgbClr val="C0504D"/></a:accent2>';
  17656. o[o.length] = '<a:accent3><a:srgbClr val="9BBB59"/></a:accent3>';
  17657. o[o.length] = '<a:accent4><a:srgbClr val="8064A2"/></a:accent4>';
  17658. o[o.length] = '<a:accent5><a:srgbClr val="4BACC6"/></a:accent5>';
  17659. o[o.length] = '<a:accent6><a:srgbClr val="F79646"/></a:accent6>';
  17660. o[o.length] = '<a:hlink><a:srgbClr val="0000FF"/></a:hlink>';
  17661. o[o.length] = '<a:folHlink><a:srgbClr val="800080"/></a:folHlink>';
  17662. o[o.length] = '</a:clrScheme>';
  17663. o[o.length] = '<a:fontScheme name="Office">';
  17664. o[o.length] = '<a:majorFont>';
  17665. o[o.length] = '<a:latin typeface="Cambria"/>';
  17666. o[o.length] = '<a:ea typeface=""/>';
  17667. o[o.length] = '<a:cs typeface=""/>';
  17668. o[o.length] = '<a:font script="Jpan" typeface="MS Pゴシック"/>';
  17669. o[o.length] = '<a:font script="Hang" typeface="맑은 고딕"/>';
  17670. o[o.length] = '<a:font script="Hans" typeface="宋体"/>';
  17671. o[o.length] = '<a:font script="Hant" typeface="新細明體"/>';
  17672. o[o.length] = '<a:font script="Arab" typeface="Times New Roman"/>';
  17673. o[o.length] = '<a:font script="Hebr" typeface="Times New Roman"/>';
  17674. o[o.length] = '<a:font script="Thai" typeface="Tahoma"/>';
  17675. o[o.length] = '<a:font script="Ethi" typeface="Nyala"/>';
  17676. o[o.length] = '<a:font script="Beng" typeface="Vrinda"/>';
  17677. o[o.length] = '<a:font script="Gujr" typeface="Shruti"/>';
  17678. o[o.length] = '<a:font script="Khmr" typeface="MoolBoran"/>';
  17679. o[o.length] = '<a:font script="Knda" typeface="Tunga"/>';
  17680. o[o.length] = '<a:font script="Guru" typeface="Raavi"/>';
  17681. o[o.length] = '<a:font script="Cans" typeface="Euphemia"/>';
  17682. o[o.length] = '<a:font script="Cher" typeface="Plantagenet Cherokee"/>';
  17683. o[o.length] = '<a:font script="Yiii" typeface="Microsoft Yi Baiti"/>';
  17684. o[o.length] = '<a:font script="Tibt" typeface="Microsoft Himalaya"/>';
  17685. o[o.length] = '<a:font script="Thaa" typeface="MV Boli"/>';
  17686. o[o.length] = '<a:font script="Deva" typeface="Mangal"/>';
  17687. o[o.length] = '<a:font script="Telu" typeface="Gautami"/>';
  17688. o[o.length] = '<a:font script="Taml" typeface="Latha"/>';
  17689. o[o.length] = '<a:font script="Syrc" typeface="Estrangelo Edessa"/>';
  17690. o[o.length] = '<a:font script="Orya" typeface="Kalinga"/>';
  17691. o[o.length] = '<a:font script="Mlym" typeface="Kartika"/>';
  17692. o[o.length] = '<a:font script="Laoo" typeface="DokChampa"/>';
  17693. o[o.length] = '<a:font script="Sinh" typeface="Iskoola Pota"/>';
  17694. o[o.length] = '<a:font script="Mong" typeface="Mongolian Baiti"/>';
  17695. o[o.length] = '<a:font script="Viet" typeface="Times New Roman"/>';
  17696. o[o.length] = '<a:font script="Uigh" typeface="Microsoft Uighur"/>';
  17697. o[o.length] = '<a:font script="Geor" typeface="Sylfaen"/>';
  17698. o[o.length] = '</a:majorFont>';
  17699. o[o.length] = '<a:minorFont>';
  17700. o[o.length] = '<a:latin typeface="Calibri"/>';
  17701. o[o.length] = '<a:ea typeface=""/>';
  17702. o[o.length] = '<a:cs typeface=""/>';
  17703. o[o.length] = '<a:font script="Jpan" typeface="MS Pゴシック"/>';
  17704. o[o.length] = '<a:font script="Hang" typeface="맑은 고딕"/>';
  17705. o[o.length] = '<a:font script="Hans" typeface="宋体"/>';
  17706. o[o.length] = '<a:font script="Hant" typeface="新細明體"/>';
  17707. o[o.length] = '<a:font script="Arab" typeface="Arial"/>';
  17708. o[o.length] = '<a:font script="Hebr" typeface="Arial"/>';
  17709. o[o.length] = '<a:font script="Thai" typeface="Tahoma"/>';
  17710. o[o.length] = '<a:font script="Ethi" typeface="Nyala"/>';
  17711. o[o.length] = '<a:font script="Beng" typeface="Vrinda"/>';
  17712. o[o.length] = '<a:font script="Gujr" typeface="Shruti"/>';
  17713. o[o.length] = '<a:font script="Khmr" typeface="DaunPenh"/>';
  17714. o[o.length] = '<a:font script="Knda" typeface="Tunga"/>';
  17715. o[o.length] = '<a:font script="Guru" typeface="Raavi"/>';
  17716. o[o.length] = '<a:font script="Cans" typeface="Euphemia"/>';
  17717. o[o.length] = '<a:font script="Cher" typeface="Plantagenet Cherokee"/>';
  17718. o[o.length] = '<a:font script="Yiii" typeface="Microsoft Yi Baiti"/>';
  17719. o[o.length] = '<a:font script="Tibt" typeface="Microsoft Himalaya"/>';
  17720. o[o.length] = '<a:font script="Thaa" typeface="MV Boli"/>';
  17721. o[o.length] = '<a:font script="Deva" typeface="Mangal"/>';
  17722. o[o.length] = '<a:font script="Telu" typeface="Gautami"/>';
  17723. o[o.length] = '<a:font script="Taml" typeface="Latha"/>';
  17724. o[o.length] = '<a:font script="Syrc" typeface="Estrangelo Edessa"/>';
  17725. o[o.length] = '<a:font script="Orya" typeface="Kalinga"/>';
  17726. o[o.length] = '<a:font script="Mlym" typeface="Kartika"/>';
  17727. o[o.length] = '<a:font script="Laoo" typeface="DokChampa"/>';
  17728. o[o.length] = '<a:font script="Sinh" typeface="Iskoola Pota"/>';
  17729. o[o.length] = '<a:font script="Mong" typeface="Mongolian Baiti"/>';
  17730. o[o.length] = '<a:font script="Viet" typeface="Arial"/>';
  17731. o[o.length] = '<a:font script="Uigh" typeface="Microsoft Uighur"/>';
  17732. o[o.length] = '<a:font script="Geor" typeface="Sylfaen"/>';
  17733. o[o.length] = '</a:minorFont>';
  17734. o[o.length] = '</a:fontScheme>';
  17735. o[o.length] = '<a:fmtScheme name="Office">';
  17736. o[o.length] = '<a:fillStyleLst>';
  17737. o[o.length] = '<a:solidFill><a:schemeClr val="phClr"/></a:solidFill>';
  17738. o[o.length] = '<a:gradFill rotWithShape="1">';
  17739. o[o.length] = '<a:gsLst>';
  17740. o[o.length] = '<a:gs pos="0"><a:schemeClr val="phClr"><a:tint val="50000"/><a:satMod val="300000"/></a:schemeClr></a:gs>';
  17741. o[o.length] = '<a:gs pos="35000"><a:schemeClr val="phClr"><a:tint val="37000"/><a:satMod val="300000"/></a:schemeClr></a:gs>';
  17742. o[o.length] = '<a:gs pos="100000"><a:schemeClr val="phClr"><a:tint val="15000"/><a:satMod val="350000"/></a:schemeClr></a:gs>';
  17743. o[o.length] = '</a:gsLst>';
  17744. o[o.length] = '<a:lin ang="16200000" scaled="1"/>';
  17745. o[o.length] = '</a:gradFill>';
  17746. o[o.length] = '<a:gradFill rotWithShape="1">';
  17747. o[o.length] = '<a:gsLst>';
  17748. o[o.length] = '<a:gs pos="0"><a:schemeClr val="phClr"><a:tint val="100000"/><a:shade val="100000"/><a:satMod val="130000"/></a:schemeClr></a:gs>';
  17749. o[o.length] = '<a:gs pos="100000"><a:schemeClr val="phClr"><a:tint val="50000"/><a:shade val="100000"/><a:satMod val="350000"/></a:schemeClr></a:gs>';
  17750. o[o.length] = '</a:gsLst>';
  17751. o[o.length] = '<a:lin ang="16200000" scaled="0"/>';
  17752. o[o.length] = '</a:gradFill>';
  17753. o[o.length] = '</a:fillStyleLst>';
  17754. o[o.length] = '<a:lnStyleLst>';
  17755. o[o.length] = '<a:ln w="9525" cap="flat" cmpd="sng" algn="ctr"><a:solidFill><a:schemeClr val="phClr"><a:shade val="95000"/><a:satMod val="105000"/></a:schemeClr></a:solidFill><a:prstDash val="solid"/></a:ln>';
  17756. o[o.length] = '<a:ln w="25400" cap="flat" cmpd="sng" algn="ctr"><a:solidFill><a:schemeClr val="phClr"/></a:solidFill><a:prstDash val="solid"/></a:ln>';
  17757. o[o.length] = '<a:ln w="38100" cap="flat" cmpd="sng" algn="ctr"><a:solidFill><a:schemeClr val="phClr"/></a:solidFill><a:prstDash val="solid"/></a:ln>';
  17758. o[o.length] = '</a:lnStyleLst>';
  17759. o[o.length] = '<a:effectStyleLst>';
  17760. o[o.length] = '<a:effectStyle>';
  17761. o[o.length] = '<a:effectLst>';
  17762. o[o.length] = '<a:outerShdw blurRad="40000" dist="20000" dir="5400000" rotWithShape="0"><a:srgbClr val="000000"><a:alpha val="38000"/></a:srgbClr></a:outerShdw>';
  17763. o[o.length] = '</a:effectLst>';
  17764. o[o.length] = '</a:effectStyle>';
  17765. o[o.length] = '<a:effectStyle>';
  17766. o[o.length] = '<a:effectLst>';
  17767. o[o.length] = '<a:outerShdw blurRad="40000" dist="23000" dir="5400000" rotWithShape="0"><a:srgbClr val="000000"><a:alpha val="35000"/></a:srgbClr></a:outerShdw>';
  17768. o[o.length] = '</a:effectLst>';
  17769. o[o.length] = '</a:effectStyle>';
  17770. o[o.length] = '<a:effectStyle>';
  17771. o[o.length] = '<a:effectLst>';
  17772. o[o.length] = '<a:outerShdw blurRad="40000" dist="23000" dir="5400000" rotWithShape="0"><a:srgbClr val="000000"><a:alpha val="35000"/></a:srgbClr></a:outerShdw>';
  17773. o[o.length] = '</a:effectLst>';
  17774. o[o.length] = '<a:scene3d><a:camera prst="orthographicFront"><a:rot lat="0" lon="0" rev="0"/></a:camera><a:lightRig rig="threePt" dir="t"><a:rot lat="0" lon="0" rev="1200000"/></a:lightRig></a:scene3d>';
  17775. o[o.length] = '<a:sp3d><a:bevelT w="63500" h="25400"/></a:sp3d>';
  17776. o[o.length] = '</a:effectStyle>';
  17777. o[o.length] = '</a:effectStyleLst>';
  17778. o[o.length] = '<a:bgFillStyleLst>';
  17779. o[o.length] = '<a:solidFill><a:schemeClr val="phClr"/></a:solidFill>';
  17780. o[o.length] = '<a:gradFill rotWithShape="1">';
  17781. o[o.length] = '<a:gsLst>';
  17782. o[o.length] = '<a:gs pos="0"><a:schemeClr val="phClr"><a:tint val="40000"/><a:satMod val="350000"/></a:schemeClr></a:gs>';
  17783. o[o.length] = '<a:gs pos="40000"><a:schemeClr val="phClr"><a:tint val="45000"/><a:shade val="99000"/><a:satMod val="350000"/></a:schemeClr></a:gs>';
  17784. o[o.length] = '<a:gs pos="100000"><a:schemeClr val="phClr"><a:shade val="20000"/><a:satMod val="255000"/></a:schemeClr></a:gs>';
  17785. o[o.length] = '</a:gsLst>';
  17786. o[o.length] = '<a:path path="circle"><a:fillToRect l="50000" t="-80000" r="50000" b="180000"/></a:path>';
  17787. o[o.length] = '</a:gradFill>';
  17788. o[o.length] = '<a:gradFill rotWithShape="1">';
  17789. o[o.length] = '<a:gsLst>';
  17790. o[o.length] = '<a:gs pos="0"><a:schemeClr val="phClr"><a:tint val="80000"/><a:satMod val="300000"/></a:schemeClr></a:gs>';
  17791. o[o.length] = '<a:gs pos="100000"><a:schemeClr val="phClr"><a:shade val="30000"/><a:satMod val="200000"/></a:schemeClr></a:gs>';
  17792. o[o.length] = '</a:gsLst>';
  17793. o[o.length] = '<a:path path="circle"><a:fillToRect l="50000" t="50000" r="50000" b="50000"/></a:path>';
  17794. o[o.length] = '</a:gradFill>';
  17795. o[o.length] = '</a:bgFillStyleLst>';
  17796. o[o.length] = '</a:fmtScheme>';
  17797. o[o.length] = '</a:themeElements>';
  17798. o[o.length] = '<a:objectDefaults>';
  17799. o[o.length] = '<a:spDef>';
  17800. o[o.length] = '<a:spPr/><a:bodyPr/><a:lstStyle/><a:style><a:lnRef idx="1"><a:schemeClr val="accent1"/></a:lnRef><a:fillRef idx="3"><a:schemeClr val="accent1"/></a:fillRef><a:effectRef idx="2"><a:schemeClr val="accent1"/></a:effectRef><a:fontRef idx="minor"><a:schemeClr val="lt1"/></a:fontRef></a:style>';
  17801. o[o.length] = '</a:spDef>';
  17802. o[o.length] = '<a:lnDef>';
  17803. o[o.length] = '<a:spPr/><a:bodyPr/><a:lstStyle/><a:style><a:lnRef idx="2"><a:schemeClr val="accent1"/></a:lnRef><a:fillRef idx="0"><a:schemeClr val="accent1"/></a:fillRef><a:effectRef idx="1"><a:schemeClr val="accent1"/></a:effectRef><a:fontRef idx="minor"><a:schemeClr val="tx1"/></a:fontRef></a:style>';
  17804. o[o.length] = '</a:lnDef>';
  17805. o[o.length] = '</a:objectDefaults>';
  17806. o[o.length] = '<a:extraClrSchemeLst/>';
  17807. o[o.length] = '</a:theme>';
  17808. return o.join("");
  17809. }
  17810. /* [MS-XLS] 2.4.326 TODO: payload is a zip file */
  17811. function parse_Theme(blob, length, opts) {
  17812. var end = blob.l + length;
  17813. var dwThemeVersion = blob.read_shift(4);
  17814. if(dwThemeVersion === 124226) return;
  17815. if(!opts.cellStyles || !jszip) { blob.l = end; return; }
  17816. var data = blob.slice(blob.l);
  17817. blob.l = end;
  17818. var zip; try { zip = new jszip(data); } catch(e) { return; }
  17819. var themeXML = getzipstr(zip, "theme/theme/theme1.xml", true);
  17820. if(!themeXML) return;
  17821. return parse_theme_xml(themeXML, opts);
  17822. }
  17823. /* 2.5.49 */
  17824. function parse_ColorTheme(blob) { return blob.read_shift(4); }
  17825. /* 2.5.155 */
  17826. function parse_FullColorExt(blob) {
  17827. var o = {};
  17828. o.xclrType = blob.read_shift(2);
  17829. o.nTintShade = blob.read_shift(2);
  17830. switch(o.xclrType) {
  17831. case 0: blob.l += 4; break;
  17832. case 1: o.xclrValue = parse_IcvXF(blob, 4); break;
  17833. case 2: o.xclrValue = parse_LongRGBA(blob, 4); break;
  17834. case 3: o.xclrValue = parse_ColorTheme(blob, 4); break;
  17835. case 4: blob.l += 4; break;
  17836. }
  17837. blob.l += 8;
  17838. return o;
  17839. }
  17840. /* 2.5.164 TODO: read 7 bits*/
  17841. function parse_IcvXF(blob, length) {
  17842. return parsenoop(blob, length);
  17843. }
  17844. /* 2.5.280 */
  17845. function parse_XFExtGradient(blob, length) {
  17846. return parsenoop(blob, length);
  17847. }
  17848. /* [MS-XLS] 2.5.108 */
  17849. function parse_ExtProp(blob) {
  17850. var extType = blob.read_shift(2);
  17851. var cb = blob.read_shift(2) - 4;
  17852. var o = [extType];
  17853. switch(extType) {
  17854. case 0x04: case 0x05: case 0x07: case 0x08:
  17855. case 0x09: case 0x0A: case 0x0B: case 0x0D:
  17856. o[1] = parse_FullColorExt(blob, cb); break;
  17857. case 0x06: o[1] = parse_XFExtGradient(blob, cb); break;
  17858. case 0x0E: case 0x0F: o[1] = blob.read_shift(cb === 1 ? 1 : 2); break;
  17859. default: throw new Error("Unrecognized ExtProp type: " + extType + " " + cb);
  17860. }
  17861. return o;
  17862. }
  17863. /* 2.4.355 */
  17864. function parse_XFExt(blob, length) {
  17865. var end = blob.l + length;
  17866. blob.l += 2;
  17867. var ixfe = blob.read_shift(2);
  17868. blob.l += 2;
  17869. var cexts = blob.read_shift(2);
  17870. var ext = [];
  17871. while(cexts-- > 0) ext.push(parse_ExtProp(blob, end-blob.l));
  17872. return {ixfe:ixfe, ext:ext};
  17873. }
  17874. /* xf is an XF, see parse_XFExt for xfext */
  17875. function update_xfext(xf, xfext) {
  17876. xfext.forEach(function(xfe) {
  17877. switch(xfe[0]) { /* 2.5.108 extPropData */
  17878. case 0x04: break; /* foreground color */
  17879. case 0x05: break; /* background color */
  17880. case 0x06: break; /* gradient fill */
  17881. case 0x07: break; /* top cell border color */
  17882. case 0x08: break; /* bottom cell border color */
  17883. case 0x09: break; /* left cell border color */
  17884. case 0x0a: break; /* right cell border color */
  17885. case 0x0b: break; /* diagonal cell border color */
  17886. case 0x0d: break; /* text color */
  17887. case 0x0e: break; /* font scheme */
  17888. case 0x0f: break; /* indentation level */
  17889. }
  17890. });
  17891. }
  17892. /* 18.6 Calculation Chain */
  17893. function parse_cc_xml(data) {
  17894. var d = [];
  17895. if(!data) return d;
  17896. var i = 1;
  17897. (data.match(tagregex)||[]).forEach(function(x) {
  17898. var y = parsexmltag(x);
  17899. switch(y[0]) {
  17900. case '<?xml': break;
  17901. /* 18.6.2 calcChain CT_CalcChain 1 */
  17902. case '<calcChain': case '<calcChain>': case '</calcChain>': break;
  17903. /* 18.6.1 c CT_CalcCell 1 */
  17904. case '<c': delete y[0]; if(y.i) i = y.i; else y.i = i; d.push(y); break;
  17905. }
  17906. });
  17907. return d;
  17908. }
  17909. //function write_cc_xml(data, opts) { }
  17910. /* [MS-XLSB] 2.6.4.1 */
  17911. function parse_BrtCalcChainItem$(data) {
  17912. var out = {};
  17913. out.i = data.read_shift(4);
  17914. var cell = {};
  17915. cell.r = data.read_shift(4);
  17916. cell.c = data.read_shift(4);
  17917. out.r = encode_cell(cell);
  17918. var flags = data.read_shift(1);
  17919. if(flags & 0x2) out.l = '1';
  17920. if(flags & 0x8) out.a = '1';
  17921. return out;
  17922. }
  17923. /* 18.6 Calculation Chain */
  17924. function parse_cc_bin(data, name, opts) {
  17925. var out = [];
  17926. var pass = false;
  17927. recordhopper(data, function hopper_cc(val, R_n, RT) {
  17928. switch(RT) {
  17929. case 0x003F: /* 'BrtCalcChainItem$' */
  17930. out.push(val); break;
  17931. default:
  17932. if((R_n||"").indexOf("Begin") > 0){/* empty */}
  17933. else if((R_n||"").indexOf("End") > 0){/* empty */}
  17934. else if(!pass || opts.WTF) throw new Error("Unexpected record " + RT + " " + R_n);
  17935. }
  17936. });
  17937. return out;
  17938. }
  17939. //function write_cc_bin(data, opts) { }
  17940. /* 18.14 Supplementary Workbook Data */
  17941. function parse_xlink_xml() {
  17942. //var opts = _opts || {};
  17943. //if(opts.WTF) throw "XLSX External Link";
  17944. }
  17945. /* [MS-XLSB] 2.1.7.25 External Link */
  17946. function parse_xlink_bin(data, name, _opts) {
  17947. if(!data) return data;
  17948. var opts = _opts || {};
  17949. var pass = false, end = false;
  17950. recordhopper(data, function xlink_parse(val, R_n, RT) {
  17951. if(end) return;
  17952. switch(RT) {
  17953. case 0x0167: /* 'BrtSupTabs' */
  17954. case 0x016B: /* 'BrtExternTableStart' */
  17955. case 0x016C: /* 'BrtExternTableEnd' */
  17956. case 0x016E: /* 'BrtExternRowHdr' */
  17957. case 0x016F: /* 'BrtExternCellBlank' */
  17958. case 0x0170: /* 'BrtExternCellReal' */
  17959. case 0x0171: /* 'BrtExternCellBool' */
  17960. case 0x0172: /* 'BrtExternCellError' */
  17961. case 0x0173: /* 'BrtExternCellString' */
  17962. case 0x01D8: /* 'BrtExternValueMeta' */
  17963. case 0x0241: /* 'BrtSupNameStart' */
  17964. case 0x0242: /* 'BrtSupNameValueStart' */
  17965. case 0x0243: /* 'BrtSupNameValueEnd' */
  17966. case 0x0244: /* 'BrtSupNameNum' */
  17967. case 0x0245: /* 'BrtSupNameErr' */
  17968. case 0x0246: /* 'BrtSupNameSt' */
  17969. case 0x0247: /* 'BrtSupNameNil' */
  17970. case 0x0248: /* 'BrtSupNameBool' */
  17971. case 0x0249: /* 'BrtSupNameFmla' */
  17972. case 0x024A: /* 'BrtSupNameBits' */
  17973. case 0x024B: /* 'BrtSupNameEnd' */
  17974. break;
  17975. case 0x0023: /* 'BrtFRTBegin' */
  17976. pass = true; break;
  17977. case 0x0024: /* 'BrtFRTEnd' */
  17978. pass = false; break;
  17979. default:
  17980. if((R_n||"").indexOf("Begin") > 0){/* empty */}
  17981. else if((R_n||"").indexOf("End") > 0){/* empty */}
  17982. else if(!pass || opts.WTF) throw new Error("Unexpected record " + RT.toString(16) + " " + R_n);
  17983. }
  17984. }, opts);
  17985. }
  17986. RELS.IMG = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image";
  17987. RELS.DRAW = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing";
  17988. /* 20.5 DrawingML - SpreadsheetML Drawing */
  17989. function parse_drawing(data, rels) {
  17990. if(!data) return "??";
  17991. /*
  17992. Chartsheet Drawing:
  17993. - 20.5.2.35 wsDr CT_Drawing
  17994. - 20.5.2.1 absoluteAnchor CT_AbsoluteAnchor
  17995. - 20.5.2.16 graphicFrame CT_GraphicalObjectFrame
  17996. - 20.1.2.2.16 graphic CT_GraphicalObject
  17997. - 20.1.2.2.17 graphicData CT_GraphicalObjectData
  17998. - chart reference
  17999. the actual type is based on the URI of the graphicData
  18000. TODO: handle embedded charts and other types of graphics
  18001. */
  18002. var id = (data.match(/<c:chart [^>]*r:id="([^"]*)"/)||["",""])[1];
  18003. return rels['!id'][id].Target;
  18004. }
  18005. /* L.5.5.2 SpreadsheetML Comments + VML Schema */
  18006. var _shapeid = 1024;
  18007. function write_comments_vml(rId, comments) {
  18008. var csize = [21600, 21600];
  18009. /* L.5.2.1.2 Path Attribute */
  18010. var bbox = ["m0,0l0",csize[1],csize[0],csize[1],csize[0],"0xe"].join(",");
  18011. var o = [
  18012. writextag("xml", null, { 'xmlns:v': XLMLNS.v, 'xmlns:o': XLMLNS.o, 'xmlns:x': XLMLNS.x, 'xmlns:mv': XLMLNS.mv }).replace(/\/>/,">"),
  18013. writextag("o:shapelayout", writextag("o:idmap", null, {'v:ext':"edit", 'data':rId}), {'v:ext':"edit"}),
  18014. writextag("v:shapetype", [
  18015. writextag("v:stroke", null, {joinstyle:"miter"}),
  18016. writextag("v:path", null, {gradientshapeok:"t", 'o:connecttype':"rect"})
  18017. ].join(""), {id:"_x0000_t202", 'o:spt':202, coordsize:csize.join(","),path:bbox})
  18018. ];
  18019. while(_shapeid < rId * 1000) _shapeid += 1000;
  18020. comments.forEach(function(x) { var c = decode_cell(x[0]);
  18021. o = o.concat([
  18022. '<v:shape' + wxt_helper({
  18023. id:'_x0000_s' + (++_shapeid),
  18024. type:"#_x0000_t202",
  18025. style:"position:absolute; margin-left:80pt;margin-top:5pt;width:104pt;height:64pt;z-index:10" + (x[1].hidden ? ";visibility:hidden" : "") ,
  18026. fillcolor:"#ECFAD4",
  18027. strokecolor:"#edeaa1"
  18028. }) + '>',
  18029. writextag('v:fill', writextag("o:fill", null, {type:"gradientUnscaled", 'v:ext':"view"}), {'color2':"#BEFF82", 'angle':"-180", 'type':"gradient"}),
  18030. writextag("v:shadow", null, {on:"t", 'obscured':"t"}),
  18031. writextag("v:path", null, {'o:connecttype':"none"}),
  18032. '<v:textbox><div style="text-align:left"></div></v:textbox>',
  18033. '<x:ClientData ObjectType="Note">',
  18034. '<x:MoveWithCells/>',
  18035. '<x:SizeWithCells/>',
  18036. /* Part 4 19.4.2.3 Anchor (Anchor) */
  18037. writetag('x:Anchor', [c.c, 0, c.r, 0, c.c+3, 100, c.r+5, 100].join(",")),
  18038. writetag('x:AutoFill', "False"),
  18039. writetag('x:Row', String(c.r)),
  18040. writetag('x:Column', String(c.c)),
  18041. x[1].hidden ? '' : '<x:Visible/>',
  18042. '</x:ClientData>',
  18043. '</v:shape>'
  18044. ]); });
  18045. o.push('</xml>');
  18046. return o.join("");
  18047. }
  18048. RELS.CMNT = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments";
  18049. function parse_comments(zip, dirComments, sheets, sheetRels, opts) {
  18050. for(var i = 0; i != dirComments.length; ++i) {
  18051. var canonicalpath=dirComments[i];
  18052. var comments=parse_cmnt(getzipdata(zip, canonicalpath.replace(/^\//,''), true), canonicalpath, opts);
  18053. if(!comments || !comments.length) continue;
  18054. // find the sheets targeted by these comments
  18055. var sheetNames = keys(sheets);
  18056. for(var j = 0; j != sheetNames.length; ++j) {
  18057. var sheetName = sheetNames[j];
  18058. var rels = sheetRels[sheetName];
  18059. if(rels) {
  18060. var rel = rels[canonicalpath];
  18061. if(rel) insertCommentsIntoSheet(sheetName, sheets[sheetName], comments);
  18062. }
  18063. }
  18064. }
  18065. }
  18066. function insertCommentsIntoSheet(sheetName, sheet, comments) {
  18067. var dense = Array.isArray(sheet);
  18068. var cell;
  18069. comments.forEach(function(comment) {
  18070. var r = decode_cell(comment.ref);
  18071. if(dense) {
  18072. if(!sheet[r.r]) sheet[r.r] = [];
  18073. cell = sheet[r.r][r.c];
  18074. } else cell = sheet[comment.ref];
  18075. if (!cell) {
  18076. cell = {};
  18077. if(dense) sheet[r.r][r.c] = cell;
  18078. else sheet[comment.ref] = cell;
  18079. var range = safe_decode_range(sheet["!ref"]||"BDWGO1000001:A1");
  18080. if(range.s.r > r.r) range.s.r = r.r;
  18081. if(range.e.r < r.r) range.e.r = r.r;
  18082. if(range.s.c > r.c) range.s.c = r.c;
  18083. if(range.e.c < r.c) range.e.c = r.c;
  18084. var encoded = encode_range(range);
  18085. if (encoded !== sheet["!ref"]) sheet["!ref"] = encoded;
  18086. }
  18087. if (!cell.c) cell.c = [];
  18088. var o = ({a: comment.author, t: comment.t, r: comment.r});
  18089. if(comment.h) o.h = comment.h;
  18090. cell.c.push(o);
  18091. });
  18092. }
  18093. /* 18.7 Comments */
  18094. function parse_comments_xml(data, opts) {
  18095. /* 18.7.6 CT_Comments */
  18096. if(data.match(/<(?:\w+:)?comments *\/>/)) return [];
  18097. var authors = [];
  18098. var commentList = [];
  18099. var authtag = data.match(/<(?:\w+:)?authors>([\s\S]*)<\/(?:\w+:)?authors>/);
  18100. if(authtag && authtag[1]) authtag[1].split(/<\/\w*:?author>/).forEach(function(x) {
  18101. if(x === "" || x.trim() === "") return;
  18102. var a = x.match(/<(?:\w+:)?author[^>]*>(.*)/);
  18103. if(a) authors.push(a[1]);
  18104. });
  18105. var cmnttag = data.match(/<(?:\w+:)?commentList>([\s\S]*)<\/(?:\w+:)?commentList>/);
  18106. if(cmnttag && cmnttag[1]) cmnttag[1].split(/<\/\w*:?comment>/).forEach(function(x) {
  18107. if(x === "" || x.trim() === "") return;
  18108. var cm = x.match(/<(?:\w+:)?comment[^>]*>/);
  18109. if(!cm) return;
  18110. var y = parsexmltag(cm[0]);
  18111. var comment = ({ author: y.authorId && authors[y.authorId] || "sheetjsghost", ref: y.ref, guid: y.guid });
  18112. var cell = decode_cell(y.ref);
  18113. if(opts.sheetRows && opts.sheetRows <= cell.r) return;
  18114. var textMatch = x.match(/<(?:\w+:)?text>([\s\S]*)<\/(?:\w+:)?text>/);
  18115. var rt = !!textMatch && !!textMatch[1] && parse_si(textMatch[1]) || {r:"",t:"",h:""};
  18116. comment.r = rt.r;
  18117. if(rt.r == "<t></t>") rt.t = rt.h = "";
  18118. comment.t = rt.t.replace(/\r\n/g,"\n").replace(/\r/g,"\n");
  18119. if(opts.cellHTML) comment.h = rt.h;
  18120. commentList.push(comment);
  18121. });
  18122. return commentList;
  18123. }
  18124. var CMNT_XML_ROOT = writextag('comments', null, { 'xmlns': XMLNS.main[0] });
  18125. function write_comments_xml(data) {
  18126. var o = [XML_HEADER, CMNT_XML_ROOT];
  18127. var iauthor = [];
  18128. o.push("<authors>");
  18129. data.forEach(function(x) { x[1].forEach(function(w) { var a = escapexml(w.a);
  18130. if(iauthor.indexOf(a) > -1) return;
  18131. iauthor.push(a);
  18132. o.push("<author>" + a + "</author>");
  18133. }); });
  18134. o.push("</authors>");
  18135. o.push("<commentList>");
  18136. data.forEach(function(d) {
  18137. d[1].forEach(function(c) {
  18138. /* 18.7.3 CT_Comment */
  18139. o.push('<comment ref="' + d[0] + '" authorId="' + iauthor.indexOf(escapexml(c.a)) + '"><text>');
  18140. o.push(writetag("t", c.t == null ? "" : escapexml(c.t)));
  18141. o.push('</text></comment>');
  18142. });
  18143. });
  18144. o.push("</commentList>");
  18145. if(o.length>2) { o[o.length] = ('</comments>'); o[1]=o[1].replace("/>",">"); }
  18146. return o.join("");
  18147. }
  18148. /* [MS-XLSB] 2.4.28 BrtBeginComment */
  18149. function parse_BrtBeginComment(data) {
  18150. var out = {};
  18151. out.iauthor = data.read_shift(4);
  18152. var rfx = parse_UncheckedRfX(data, 16);
  18153. out.rfx = rfx.s;
  18154. out.ref = encode_cell(rfx.s);
  18155. data.l += 16; /*var guid = parse_GUID(data); */
  18156. return out;
  18157. }
  18158. function write_BrtBeginComment(data, o) {
  18159. if(o == null) o = new_buf(36);
  18160. o.write_shift(4, data[1].iauthor);
  18161. write_UncheckedRfX((data[0]), o);
  18162. o.write_shift(4, 0);
  18163. o.write_shift(4, 0);
  18164. o.write_shift(4, 0);
  18165. o.write_shift(4, 0);
  18166. return o;
  18167. }
  18168. /* [MS-XLSB] 2.4.327 BrtCommentAuthor */
  18169. var parse_BrtCommentAuthor = parse_XLWideString;
  18170. function write_BrtCommentAuthor(data) { return write_XLWideString(data.slice(0, 54)); }
  18171. /* [MS-XLSB] 2.1.7.8 Comments */
  18172. function parse_comments_bin(data, opts) {
  18173. var out = [];
  18174. var authors = [];
  18175. var c = {};
  18176. var pass = false;
  18177. recordhopper(data, function hopper_cmnt(val, R_n, RT) {
  18178. switch(RT) {
  18179. case 0x0278: /* 'BrtCommentAuthor' */
  18180. authors.push(val); break;
  18181. case 0x027B: /* 'BrtBeginComment' */
  18182. c = val; break;
  18183. case 0x027D: /* 'BrtCommentText' */
  18184. c.t = val.t; c.h = val.h; c.r = val.r; break;
  18185. case 0x027C: /* 'BrtEndComment' */
  18186. c.author = authors[c.iauthor];
  18187. delete c.iauthor;
  18188. if(opts.sheetRows && opts.sheetRows <= c.rfx.r) break;
  18189. if(!c.t) c.t = "";
  18190. delete c.rfx; out.push(c); break;
  18191. case 0x0C00: /* 'BrtUid' */
  18192. break;
  18193. case 0x0023: /* 'BrtFRTBegin' */
  18194. pass = true; break;
  18195. case 0x0024: /* 'BrtFRTEnd' */
  18196. pass = false; break;
  18197. case 0x0025: /* 'BrtACBegin' */ break;
  18198. case 0x0026: /* 'BrtACEnd' */ break;
  18199. default:
  18200. if((R_n||"").indexOf("Begin") > 0){/* empty */}
  18201. else if((R_n||"").indexOf("End") > 0){/* empty */}
  18202. else if(!pass || opts.WTF) throw new Error("Unexpected record " + RT + " " + R_n);
  18203. }
  18204. });
  18205. return out;
  18206. }
  18207. function write_comments_bin(data) {
  18208. var ba = buf_array();
  18209. var iauthor = [];
  18210. write_record(ba, "BrtBeginComments");
  18211. write_record(ba, "BrtBeginCommentAuthors");
  18212. data.forEach(function(comment) {
  18213. comment[1].forEach(function(c) {
  18214. if(iauthor.indexOf(c.a) > -1) return;
  18215. iauthor.push(c.a.slice(0,54));
  18216. write_record(ba, "BrtCommentAuthor", write_BrtCommentAuthor(c.a));
  18217. });
  18218. });
  18219. write_record(ba, "BrtEndCommentAuthors");
  18220. write_record(ba, "BrtBeginCommentList");
  18221. data.forEach(function(comment) {
  18222. comment[1].forEach(function(c) {
  18223. c.iauthor = iauthor.indexOf(c.a);
  18224. var range = {s:decode_cell(comment[0]),e:decode_cell(comment[0])};
  18225. write_record(ba, "BrtBeginComment", write_BrtBeginComment([range, c]));
  18226. if(c.t && c.t.length > 0) write_record(ba, "BrtCommentText", write_BrtCommentText(c));
  18227. write_record(ba, "BrtEndComment");
  18228. delete c.iauthor;
  18229. });
  18230. });
  18231. write_record(ba, "BrtEndCommentList");
  18232. write_record(ba, "BrtEndComments");
  18233. return ba.end();
  18234. }
  18235. var CT_VBA = "application/vnd.ms-office.vbaProject";
  18236. function make_vba_xls(cfb) {
  18237. var newcfb = CFB.utils.cfb_new({root:"R"});
  18238. cfb.FullPaths.forEach(function(p, i) {
  18239. if(p.slice(-1) === "/" || !p.match(/_VBA_PROJECT_CUR/)) return;
  18240. var newpath = p.replace(/^[^\/]*/,"R").replace(/\/_VBA_PROJECT_CUR\u0000*/, "");
  18241. CFB.utils.cfb_add(newcfb, newpath, cfb.FileIndex[i].content);
  18242. });
  18243. return CFB.write(newcfb);
  18244. }
  18245. function fill_vba_xls(cfb, vba) {
  18246. vba.FullPaths.forEach(function(p, i) {
  18247. if(i == 0) return;
  18248. var newpath = p.replace(/[^\/]*[\/]/, "/_VBA_PROJECT_CUR/");
  18249. if(newpath.slice(-1) !== "/") CFB.utils.cfb_add(cfb, newpath, vba.FileIndex[i].content);
  18250. });
  18251. }
  18252. var VBAFMTS = [ "xlsb", "xlsm", "xlam", "biff8", "xla" ];
  18253. RELS.DS = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/dialogsheet";
  18254. RELS.MS = "http://schemas.microsoft.com/office/2006/relationships/xlMacrosheet";
  18255. /* macro and dialog sheet stubs */
  18256. function parse_ds_bin() { return {'!type':'dialog'}; }
  18257. function parse_ds_xml() { return {'!type':'dialog'}; }
  18258. function parse_ms_bin() { return {'!type':'macro'}; }
  18259. function parse_ms_xml() { return {'!type':'macro'}; }
  18260. /* TODO: it will be useful to parse the function str */
  18261. var rc_to_a1 = (function(){
  18262. var rcregex = /(^|[^A-Za-z])R(\[?)(-?\d+|)\]?C(\[?)(-?\d+|)\]?/g;
  18263. var rcbase = ({r:0,c:0});
  18264. function rcfunc($$,$1,$2,$3,$4,$5) {
  18265. var R = $3.length>0?parseInt($3,10)|0:0, C = $5.length>0?parseInt($5,10)|0:0;
  18266. if(C<0 && $4.length === 0) C=0;
  18267. var cRel = false, rRel = false;
  18268. if($4.length > 0 || $5.length == 0) cRel = true; if(cRel) C += rcbase.c; else --C;
  18269. if($2.length > 0 || $3.length == 0) rRel = true; if(rRel) R += rcbase.r; else --R;
  18270. return $1 + (cRel ? "" : "$") + encode_col(C) + (rRel ? "" : "$") + encode_row(R);
  18271. }
  18272. return function rc_to_a1(fstr, base) {
  18273. rcbase = base;
  18274. return fstr.replace(rcregex, rcfunc);
  18275. };
  18276. })();
  18277. var crefregex = /(^|[^._A-Z0-9])([$]?)([A-Z]{1,2}|[A-W][A-Z]{2}|X[A-E][A-Z]|XF[A-D])([$]?)([1-9]\d{0,5}|10[0-3]\d{4}|104[0-7]\d{3}|1048[0-4]\d{2}|10485[0-6]\d|104857[0-6])(?![_.\(A-Za-z0-9])/g;
  18278. var a1_to_rc =(function(){
  18279. return function a1_to_rc(fstr, base) {
  18280. return fstr.replace(crefregex, function($0, $1, $2, $3, $4, $5) {
  18281. var c = decode_col($3) - ($2 ? 0 : base.c);
  18282. var r = decode_row($5) - ($4 ? 0 : base.r);
  18283. var R = (r == 0 ? "" : !$4 ? "[" + r + "]" : (r+1));
  18284. var C = (c == 0 ? "" : !$2 ? "[" + c + "]" : (c+1));
  18285. return $1 + "R" + R + "C" + C;
  18286. });
  18287. };
  18288. })();
  18289. /* no defined name can collide with a valid cell address A1:XFD1048576 ... except LOG10! */
  18290. function shift_formula_str(f, delta) {
  18291. return f.replace(crefregex, function($0, $1, $2, $3, $4, $5) {
  18292. return $1+($2=="$" ? $2+$3 : encode_col(decode_col($3)+delta.c))+($4=="$" ? $4+$5 : encode_row(decode_row($5) + delta.r));
  18293. });
  18294. }
  18295. function shift_formula_xlsx(f, range, cell) {
  18296. var r = decode_range(range), s = r.s, c = decode_cell(cell);
  18297. var delta = {r:c.r - s.r, c:c.c - s.c};
  18298. return shift_formula_str(f, delta);
  18299. }
  18300. /* TODO: parse formula */
  18301. function fuzzyfmla(f) {
  18302. if(f.length == 1) return false;
  18303. return true;
  18304. }
  18305. function _xlfn(f) {
  18306. return f.replace(/_xlfn\./g,"");
  18307. }
  18308. function parseread1(blob) { blob.l+=1; return; }
  18309. /* [MS-XLS] 2.5.51 */
  18310. function parse_ColRelU(blob, length) {
  18311. var c = blob.read_shift(length == 1 ? 1 : 2);
  18312. return [c & 0x3FFF, (c >> 14) & 1, (c >> 15) & 1];
  18313. }
  18314. /* [MS-XLS] 2.5.198.105 ; [MS-XLSB] 2.5.97.89 */
  18315. function parse_RgceArea(blob, length, opts) {
  18316. var w = 2;
  18317. if(opts) {
  18318. if(opts.biff >= 2 && opts.biff <= 5) return parse_RgceArea_BIFF2(blob, length, opts);
  18319. else if(opts.biff == 12) w = 4;
  18320. }
  18321. var r=blob.read_shift(w), R=blob.read_shift(w);
  18322. var c=parse_ColRelU(blob, 2);
  18323. var C=parse_ColRelU(blob, 2);
  18324. return { s:{r:r, c:c[0], cRel:c[1], rRel:c[2]}, e:{r:R, c:C[0], cRel:C[1], rRel:C[2]} };
  18325. }
  18326. /* BIFF 2-5 encodes flags in the row field */
  18327. function parse_RgceArea_BIFF2(blob) {
  18328. var r=parse_ColRelU(blob, 2), R=parse_ColRelU(blob, 2);
  18329. var c=blob.read_shift(1);
  18330. var C=blob.read_shift(1);
  18331. return { s:{r:r[0], c:c, cRel:r[1], rRel:r[2]}, e:{r:R[0], c:C, cRel:R[1], rRel:R[2]} };
  18332. }
  18333. /* [MS-XLS] 2.5.198.105 ; [MS-XLSB] 2.5.97.90 */
  18334. function parse_RgceAreaRel(blob, length, opts) {
  18335. if(opts.biff < 8) return parse_RgceArea_BIFF2(blob, length, opts);
  18336. var r=blob.read_shift(opts.biff == 12 ? 4 : 2), R=blob.read_shift(opts.biff == 12 ? 4 : 2);
  18337. var c=parse_ColRelU(blob, 2);
  18338. var C=parse_ColRelU(blob, 2);
  18339. return { s:{r:r, c:c[0], cRel:c[1], rRel:c[2]}, e:{r:R, c:C[0], cRel:C[1], rRel:C[2]} };
  18340. }
  18341. /* [MS-XLS] 2.5.198.109 ; [MS-XLSB] 2.5.97.91 */
  18342. function parse_RgceLoc(blob, length, opts) {
  18343. if(opts && opts.biff >= 2 && opts.biff <= 5) return parse_RgceLoc_BIFF2(blob, length, opts);
  18344. var r = blob.read_shift(opts && opts.biff == 12 ? 4 : 2);
  18345. var c = parse_ColRelU(blob, 2);
  18346. return {r:r, c:c[0], cRel:c[1], rRel:c[2]};
  18347. }
  18348. function parse_RgceLoc_BIFF2(blob) {
  18349. var r = parse_ColRelU(blob, 2);
  18350. var c = blob.read_shift(1);
  18351. return {r:r[0], c:c, cRel:r[1], rRel:r[2]};
  18352. }
  18353. /* [MS-XLS] 2.5.198.107, 2.5.47 */
  18354. function parse_RgceElfLoc(blob) {
  18355. var r = blob.read_shift(2);
  18356. var c = blob.read_shift(2);
  18357. return {r:r, c:c & 0xFF, fQuoted:!!(c & 0x4000), cRel:c>>15, rRel:c>>15 };
  18358. }
  18359. /* [MS-XLS] 2.5.198.111 ; [MS-XLSB] 2.5.97.92 TODO */
  18360. function parse_RgceLocRel(blob, length, opts) {
  18361. var biff = opts && opts.biff ? opts.biff : 8;
  18362. if(biff >= 2 && biff <= 5) return parse_RgceLocRel_BIFF2(blob, length, opts);
  18363. var r = blob.read_shift(biff >= 12 ? 4 : 2);
  18364. var cl = blob.read_shift(2);
  18365. var cRel = (cl & 0x4000) >> 14, rRel = (cl & 0x8000) >> 15;
  18366. cl &= 0x3FFF;
  18367. if(rRel == 1) while(r > 0x7FFFF) r -= 0x100000;
  18368. if(cRel == 1) while(cl > 0x1FFF) cl = cl - 0x4000;
  18369. return {r:r,c:cl,cRel:cRel,rRel:rRel};
  18370. }
  18371. function parse_RgceLocRel_BIFF2(blob) {
  18372. var rl = blob.read_shift(2);
  18373. var c = blob.read_shift(1);
  18374. var rRel = (rl & 0x8000) >> 15, cRel = (rl & 0x4000) >> 14;
  18375. rl &= 0x3FFF;
  18376. if(rRel == 1 && rl >= 0x2000) rl = rl - 0x4000;
  18377. if(cRel == 1 && c >= 0x80) c = c - 0x100;
  18378. return {r:rl,c:c,cRel:cRel,rRel:rRel};
  18379. }
  18380. /* [MS-XLS] 2.5.198.27 ; [MS-XLSB] 2.5.97.18 */
  18381. function parse_PtgArea(blob, length, opts) {
  18382. var type = (blob[blob.l++] & 0x60) >> 5;
  18383. var area = parse_RgceArea(blob, opts.biff >= 2 && opts.biff <= 5 ? 6 : 8, opts);
  18384. return [type, area];
  18385. }
  18386. /* [MS-XLS] 2.5.198.28 ; [MS-XLSB] 2.5.97.19 */
  18387. function parse_PtgArea3d(blob, length, opts) {
  18388. var type = (blob[blob.l++] & 0x60) >> 5;
  18389. var ixti = blob.read_shift(2, 'i');
  18390. var w = 8;
  18391. if(opts) switch(opts.biff) {
  18392. case 5: blob.l += 12; w = 6; break;
  18393. case 12: w = 12; break;
  18394. }
  18395. var area = parse_RgceArea(blob, w, opts);
  18396. return [type, ixti, area];
  18397. }
  18398. /* [MS-XLS] 2.5.198.29 ; [MS-XLSB] 2.5.97.20 */
  18399. function parse_PtgAreaErr(blob, length, opts) {
  18400. var type = (blob[blob.l++] & 0x60) >> 5;
  18401. blob.l += opts && (opts.biff > 8) ? 12 : (opts.biff < 8 ? 6 : 8);
  18402. return [type];
  18403. }
  18404. /* [MS-XLS] 2.5.198.30 ; [MS-XLSB] 2.5.97.21 */
  18405. function parse_PtgAreaErr3d(blob, length, opts) {
  18406. var type = (blob[blob.l++] & 0x60) >> 5;
  18407. var ixti = blob.read_shift(2);
  18408. var w = 8;
  18409. if(opts) switch(opts.biff) {
  18410. case 5: blob.l += 12; w = 6; break;
  18411. case 12: w = 12; break;
  18412. }
  18413. blob.l += w;
  18414. return [type, ixti];
  18415. }
  18416. /* [MS-XLS] 2.5.198.31 ; [MS-XLSB] 2.5.97.22 */
  18417. function parse_PtgAreaN(blob, length, opts) {
  18418. var type = (blob[blob.l++] & 0x60) >> 5;
  18419. var area = parse_RgceAreaRel(blob, length - 1, opts);
  18420. return [type, area];
  18421. }
  18422. /* [MS-XLS] 2.5.198.32 ; [MS-XLSB] 2.5.97.23 */
  18423. function parse_PtgArray(blob, length, opts) {
  18424. var type = (blob[blob.l++] & 0x60) >> 5;
  18425. blob.l += opts.biff == 2 ? 6 : opts.biff == 12 ? 14 : 7;
  18426. return [type];
  18427. }
  18428. /* [MS-XLS] 2.5.198.33 ; [MS-XLSB] 2.5.97.24 */
  18429. function parse_PtgAttrBaxcel(blob) {
  18430. var bitSemi = blob[blob.l+1] & 0x01; /* 1 = volatile */
  18431. var bitBaxcel = 1;
  18432. blob.l += 4;
  18433. return [bitSemi, bitBaxcel];
  18434. }
  18435. /* [MS-XLS] 2.5.198.34 ; [MS-XLSB] 2.5.97.25 */
  18436. function parse_PtgAttrChoose(blob, length, opts) {
  18437. blob.l +=2;
  18438. var offset = blob.read_shift(opts && opts.biff == 2 ? 1 : 2);
  18439. var o = [];
  18440. /* offset is 1 less than the number of elements */
  18441. for(var i = 0; i <= offset; ++i) o.push(blob.read_shift(opts && opts.biff == 2 ? 1 : 2));
  18442. return o;
  18443. }
  18444. /* [MS-XLS] 2.5.198.35 ; [MS-XLSB] 2.5.97.26 */
  18445. function parse_PtgAttrGoto(blob, length, opts) {
  18446. var bitGoto = (blob[blob.l+1] & 0xFF) ? 1 : 0;
  18447. blob.l += 2;
  18448. return [bitGoto, blob.read_shift(opts && opts.biff == 2 ? 1 : 2)];
  18449. }
  18450. /* [MS-XLS] 2.5.198.36 ; [MS-XLSB] 2.5.97.27 */
  18451. function parse_PtgAttrIf(blob, length, opts) {
  18452. var bitIf = (blob[blob.l+1] & 0xFF) ? 1 : 0;
  18453. blob.l += 2;
  18454. return [bitIf, blob.read_shift(opts && opts.biff == 2 ? 1 : 2)];
  18455. }
  18456. /* [MS-XLSB] 2.5.97.28 */
  18457. function parse_PtgAttrIfError(blob) {
  18458. var bitIf = (blob[blob.l+1] & 0xFF) ? 1 : 0;
  18459. blob.l += 2;
  18460. return [bitIf, blob.read_shift(2)];
  18461. }
  18462. /* [MS-XLS] 2.5.198.37 ; [MS-XLSB] 2.5.97.29 */
  18463. function parse_PtgAttrSemi(blob, length, opts) {
  18464. var bitSemi = (blob[blob.l+1] & 0xFF) ? 1 : 0;
  18465. blob.l += opts && opts.biff == 2 ? 3 : 4;
  18466. return [bitSemi];
  18467. }
  18468. /* [MS-XLS] 2.5.198.40 ; [MS-XLSB] 2.5.97.32 */
  18469. function parse_PtgAttrSpaceType(blob) {
  18470. var type = blob.read_shift(1), cch = blob.read_shift(1);
  18471. return [type, cch];
  18472. }
  18473. /* [MS-XLS] 2.5.198.38 ; [MS-XLSB] 2.5.97.30 */
  18474. function parse_PtgAttrSpace(blob) {
  18475. blob.read_shift(2);
  18476. return parse_PtgAttrSpaceType(blob, 2);
  18477. }
  18478. /* [MS-XLS] 2.5.198.39 ; [MS-XLSB] 2.5.97.31 */
  18479. function parse_PtgAttrSpaceSemi(blob) {
  18480. blob.read_shift(2);
  18481. return parse_PtgAttrSpaceType(blob, 2);
  18482. }
  18483. /* [MS-XLS] 2.5.198.84 ; [MS-XLSB] 2.5.97.68 TODO */
  18484. function parse_PtgRef(blob, length, opts) {
  18485. //var ptg = blob[blob.l] & 0x1F;
  18486. var type = (blob[blob.l] & 0x60)>>5;
  18487. blob.l += 1;
  18488. var loc = parse_RgceLoc(blob, 0, opts);
  18489. return [type, loc];
  18490. }
  18491. /* [MS-XLS] 2.5.198.88 ; [MS-XLSB] 2.5.97.72 TODO */
  18492. function parse_PtgRefN(blob, length, opts) {
  18493. var type = (blob[blob.l] & 0x60)>>5;
  18494. blob.l += 1;
  18495. var loc = parse_RgceLocRel(blob, 0, opts);
  18496. return [type, loc];
  18497. }
  18498. /* [MS-XLS] 2.5.198.85 ; [MS-XLSB] 2.5.97.69 TODO */
  18499. function parse_PtgRef3d(blob, length, opts) {
  18500. var type = (blob[blob.l] & 0x60)>>5;
  18501. blob.l += 1;
  18502. var ixti = blob.read_shift(2); // XtiIndex
  18503. if(opts && opts.biff == 5) blob.l += 12;
  18504. var loc = parse_RgceLoc(blob, 0, opts); // TODO: or RgceLocRel
  18505. return [type, ixti, loc];
  18506. }
  18507. /* [MS-XLS] 2.5.198.62 ; [MS-XLSB] 2.5.97.45 TODO */
  18508. function parse_PtgFunc(blob, length, opts) {
  18509. //var ptg = blob[blob.l] & 0x1F;
  18510. var type = (blob[blob.l] & 0x60)>>5;
  18511. blob.l += 1;
  18512. var iftab = blob.read_shift(opts && opts.biff <= 3 ? 1 : 2);
  18513. return [FtabArgc[iftab], Ftab[iftab], type];
  18514. }
  18515. /* [MS-XLS] 2.5.198.63 ; [MS-XLSB] 2.5.97.46 TODO */
  18516. function parse_PtgFuncVar(blob, length, opts) {
  18517. var type = blob[blob.l++];
  18518. var cparams = blob.read_shift(1), tab = opts && opts.biff <= 3 ? [(type == 0x58 ? -1 : 0), blob.read_shift(1)]: parsetab(blob);
  18519. return [cparams, (tab[0] === 0 ? Ftab : Cetab)[tab[1]]];
  18520. }
  18521. function parsetab(blob) {
  18522. return [blob[blob.l+1]>>7, blob.read_shift(2) & 0x7FFF];
  18523. }
  18524. /* [MS-XLS] 2.5.198.41 ; [MS-XLSB] 2.5.97.33 */
  18525. function parse_PtgAttrSum(blob, length, opts) {
  18526. blob.l += opts && opts.biff == 2 ? 3 : 4; return;
  18527. }
  18528. /* [MS-XLS] 2.5.198.58 ; [MS-XLSB] 2.5.97.40 */
  18529. function parse_PtgExp(blob, length, opts) {
  18530. blob.l++;
  18531. if(opts && opts.biff == 12) return [blob.read_shift(4, 'i'), 0];
  18532. var row = blob.read_shift(2);
  18533. var col = blob.read_shift(opts && opts.biff == 2 ? 1 : 2);
  18534. return [row, col];
  18535. }
  18536. /* [MS-XLS] 2.5.198.57 ; [MS-XLSB] 2.5.97.39 */
  18537. function parse_PtgErr(blob) { blob.l++; return BErr[blob.read_shift(1)]; }
  18538. /* [MS-XLS] 2.5.198.66 ; [MS-XLSB] 2.5.97.49 */
  18539. function parse_PtgInt(blob) { blob.l++; return blob.read_shift(2); }
  18540. /* [MS-XLS] 2.5.198.42 ; [MS-XLSB] 2.5.97.34 */
  18541. function parse_PtgBool(blob) { blob.l++; return blob.read_shift(1)!==0;}
  18542. /* [MS-XLS] 2.5.198.79 ; [MS-XLSB] 2.5.97.63 */
  18543. function parse_PtgNum(blob) { blob.l++; return parse_Xnum(blob, 8); }
  18544. /* [MS-XLS] 2.5.198.89 ; [MS-XLSB] 2.5.97.74 */
  18545. function parse_PtgStr(blob, length, opts) { blob.l++; return parse_ShortXLUnicodeString(blob, length-1, opts); }
  18546. /* [MS-XLS] 2.5.192.112 + 2.5.192.11{3,4,5,6,7} */
  18547. /* [MS-XLSB] 2.5.97.93 + 2.5.97.9{4,5,6,7} */
  18548. function parse_SerAr(blob, biff) {
  18549. var val = [blob.read_shift(1)];
  18550. if(biff == 12) switch(val[0]) {
  18551. case 0x02: val[0] = 0x04; break; /* SerBool */
  18552. case 0x04: val[0] = 0x10; break; /* SerErr */
  18553. case 0x00: val[0] = 0x01; break; /* SerNum */
  18554. case 0x01: val[0] = 0x02; break; /* SerStr */
  18555. }
  18556. switch(val[0]) {
  18557. case 0x04: /* SerBool -- boolean */
  18558. val[1] = parsebool(blob, 1) ? 'TRUE' : 'FALSE';
  18559. if(biff != 12) blob.l += 7; break;
  18560. case 0x25: /* appears to be an alias */
  18561. case 0x10: /* SerErr -- error */
  18562. val[1] = BErr[blob[blob.l]];
  18563. blob.l += ((biff == 12) ? 4 : 8); break;
  18564. case 0x00: /* SerNil -- honestly, I'm not sure how to reproduce this */
  18565. blob.l += 8; break;
  18566. case 0x01: /* SerNum -- Xnum */
  18567. val[1] = parse_Xnum(blob, 8); break;
  18568. case 0x02: /* SerStr -- XLUnicodeString (<256 chars) */
  18569. val[1] = parse_XLUnicodeString2(blob, 0, {biff:biff > 0 && biff < 8 ? 2 : biff}); break;
  18570. default: throw new Error("Bad SerAr: " + val[0]); /* Unreachable */
  18571. }
  18572. return val;
  18573. }
  18574. /* [MS-XLS] 2.5.198.61 ; [MS-XLSB] 2.5.97.44 */
  18575. function parse_PtgExtraMem(blob, cce, opts) {
  18576. var count = blob.read_shift((opts.biff == 12) ? 4 : 2);
  18577. var out = [];
  18578. for(var i = 0; i != count; ++i) out.push(((opts.biff == 12) ? parse_UncheckedRfX : parse_Ref8U)(blob, 8));
  18579. return out;
  18580. }
  18581. /* [MS-XLS] 2.5.198.59 ; [MS-XLSB] 2.5.97.41 */
  18582. function parse_PtgExtraArray(blob, length, opts) {
  18583. var rows = 0, cols = 0;
  18584. if(opts.biff == 12) {
  18585. rows = blob.read_shift(4); // DRw
  18586. cols = blob.read_shift(4); // DCol
  18587. } else {
  18588. cols = 1 + blob.read_shift(1); //DColByteU
  18589. rows = 1 + blob.read_shift(2); //DRw
  18590. }
  18591. if(opts.biff >= 2 && opts.biff < 8) { --rows; if(--cols == 0) cols = 0x100; }
  18592. // $FlowIgnore
  18593. for(var i = 0, o = []; i != rows && (o[i] = []); ++i)
  18594. for(var j = 0; j != cols; ++j) o[i][j] = parse_SerAr(blob, opts.biff);
  18595. return o;
  18596. }
  18597. /* [MS-XLS] 2.5.198.76 ; [MS-XLSB] 2.5.97.60 */
  18598. function parse_PtgName(blob, length, opts) {
  18599. var type = (blob.read_shift(1) >>> 5) & 0x03;
  18600. var w = (!opts || (opts.biff >= 8)) ? 4 : 2;
  18601. var nameindex = blob.read_shift(w);
  18602. switch(opts.biff) {
  18603. case 2: blob.l += 5; break;
  18604. case 3: case 4: blob.l += 8; break;
  18605. case 5: blob.l += 12; break;
  18606. }
  18607. return [type, 0, nameindex];
  18608. }
  18609. /* [MS-XLS] 2.5.198.77 ; [MS-XLSB] 2.5.97.61 */
  18610. function parse_PtgNameX(blob, length, opts) {
  18611. if(opts.biff == 5) return parse_PtgNameX_BIFF5(blob, length, opts);
  18612. var type = (blob.read_shift(1) >>> 5) & 0x03;
  18613. var ixti = blob.read_shift(2); // XtiIndex
  18614. var nameindex = blob.read_shift(4);
  18615. return [type, ixti, nameindex];
  18616. }
  18617. function parse_PtgNameX_BIFF5(blob) {
  18618. var type = (blob.read_shift(1) >>> 5) & 0x03;
  18619. var ixti = blob.read_shift(2, 'i'); // XtiIndex
  18620. blob.l += 8;
  18621. var nameindex = blob.read_shift(2);
  18622. blob.l += 12;
  18623. return [type, ixti, nameindex];
  18624. }
  18625. /* [MS-XLS] 2.5.198.70 ; [MS-XLSB] 2.5.97.54 */
  18626. function parse_PtgMemArea(blob, length, opts) {
  18627. var type = (blob.read_shift(1) >>> 5) & 0x03;
  18628. blob.l += (opts && opts.biff == 2 ? 3 : 4);
  18629. var cce = blob.read_shift(opts && opts.biff == 2 ? 1 : 2);
  18630. return [type, cce];
  18631. }
  18632. /* [MS-XLS] 2.5.198.72 ; [MS-XLSB] 2.5.97.56 */
  18633. function parse_PtgMemFunc(blob, length, opts) {
  18634. var type = (blob.read_shift(1) >>> 5) & 0x03;
  18635. var cce = blob.read_shift(opts && opts.biff == 2 ? 1 : 2);
  18636. return [type, cce];
  18637. }
  18638. /* [MS-XLS] 2.5.198.86 ; [MS-XLSB] 2.5.97.69 */
  18639. function parse_PtgRefErr(blob, length, opts) {
  18640. var type = (blob.read_shift(1) >>> 5) & 0x03;
  18641. blob.l += 4;
  18642. if(opts.biff < 8) blob.l--;
  18643. if(opts.biff == 12) blob.l += 2;
  18644. return [type];
  18645. }
  18646. /* [MS-XLS] 2.5.198.87 ; [MS-XLSB] 2.5.97.71 */
  18647. function parse_PtgRefErr3d(blob, length, opts) {
  18648. var type = (blob[blob.l++] & 0x60) >> 5;
  18649. var ixti = blob.read_shift(2);
  18650. var w = 4;
  18651. if(opts) switch(opts.biff) {
  18652. case 5: w = 15; break;
  18653. case 12: w = 6; break;
  18654. }
  18655. blob.l += w;
  18656. return [type, ixti];
  18657. }
  18658. /* [MS-XLS] 2.5.198.71 ; [MS-XLSB] 2.5.97.55 */
  18659. var parse_PtgMemErr = parsenoop;
  18660. /* [MS-XLS] 2.5.198.73 ; [MS-XLSB] 2.5.97.57 */
  18661. var parse_PtgMemNoMem = parsenoop;
  18662. /* [MS-XLS] 2.5.198.92 */
  18663. var parse_PtgTbl = parsenoop;
  18664. function parse_PtgElfLoc(blob, length, opts) {
  18665. blob.l += 2;
  18666. return [parse_RgceElfLoc(blob, 4, opts)];
  18667. }
  18668. function parse_PtgElfNoop(blob) {
  18669. blob.l += 6;
  18670. return [];
  18671. }
  18672. /* [MS-XLS] 2.5.198.46 */
  18673. var parse_PtgElfCol = parse_PtgElfLoc;
  18674. /* [MS-XLS] 2.5.198.47 */
  18675. var parse_PtgElfColS = parse_PtgElfNoop;
  18676. /* [MS-XLS] 2.5.198.48 */
  18677. var parse_PtgElfColSV = parse_PtgElfNoop;
  18678. /* [MS-XLS] 2.5.198.49 */
  18679. var parse_PtgElfColV = parse_PtgElfLoc;
  18680. /* [MS-XLS] 2.5.198.50 */
  18681. function parse_PtgElfLel(blob) {
  18682. blob.l += 2;
  18683. return [parseuint16(blob), blob.read_shift(2) & 0x01];
  18684. }
  18685. /* [MS-XLS] 2.5.198.51 */
  18686. var parse_PtgElfRadical = parse_PtgElfLoc;
  18687. /* [MS-XLS] 2.5.198.52 */
  18688. var parse_PtgElfRadicalLel = parse_PtgElfLel;
  18689. /* [MS-XLS] 2.5.198.53 */
  18690. var parse_PtgElfRadicalS = parse_PtgElfNoop;
  18691. /* [MS-XLS] 2.5.198.54 */
  18692. var parse_PtgElfRw = parse_PtgElfLoc;
  18693. /* [MS-XLS] 2.5.198.55 */
  18694. var parse_PtgElfRwV = parse_PtgElfLoc;
  18695. /* [MS-XLSB] 2.5.97.52 TODO */
  18696. var PtgListRT = [
  18697. "Data",
  18698. "All",
  18699. "Headers",
  18700. "??",
  18701. "?Data2",
  18702. "??",
  18703. "?DataHeaders",
  18704. "??",
  18705. "Totals",
  18706. "??",
  18707. "??",
  18708. "??",
  18709. "?DataTotals",
  18710. "??",
  18711. "??",
  18712. "??",
  18713. "?Current"
  18714. ];
  18715. function parse_PtgList(blob) {
  18716. blob.l += 2;
  18717. var ixti = blob.read_shift(2);
  18718. var flags = blob.read_shift(2);
  18719. var idx = blob.read_shift(4);
  18720. var c = blob.read_shift(2);
  18721. var C = blob.read_shift(2);
  18722. var rt = PtgListRT[(flags >> 2) & 0x1F];
  18723. return {ixti: ixti, coltype:(flags&0x3), rt:rt, idx:idx, c:c, C:C};
  18724. }
  18725. /* [MS-XLS] 2.5.198.91 ; [MS-XLSB] 2.5.97.76 */
  18726. function parse_PtgSxName(blob) {
  18727. blob.l += 2;
  18728. return [blob.read_shift(4)];
  18729. }
  18730. /* [XLS] old spec */
  18731. function parse_PtgSheet(blob, length, opts) {
  18732. blob.l += 5;
  18733. blob.l += 2;
  18734. blob.l += (opts.biff == 2 ? 1 : 4);
  18735. return ["PTGSHEET"];
  18736. }
  18737. function parse_PtgEndSheet(blob, length, opts) {
  18738. blob.l += (opts.biff == 2 ? 4 : 5);
  18739. return ["PTGENDSHEET"];
  18740. }
  18741. function parse_PtgMemAreaN(blob) {
  18742. var type = (blob.read_shift(1) >>> 5) & 0x03;
  18743. var cce = blob.read_shift(2);
  18744. return [type, cce];
  18745. }
  18746. function parse_PtgMemNoMemN(blob) {
  18747. var type = (blob.read_shift(1) >>> 5) & 0x03;
  18748. var cce = blob.read_shift(2);
  18749. return [type, cce];
  18750. }
  18751. function parse_PtgAttrNoop(blob) {
  18752. blob.l += 4;
  18753. return [0, 0];
  18754. }
  18755. /* [MS-XLS] 2.5.198.25 ; [MS-XLSB] 2.5.97.16 */
  18756. var PtgTypes = {
  18757. 0x01: { n:'PtgExp', f:parse_PtgExp },
  18758. 0x02: { n:'PtgTbl', f:parse_PtgTbl },
  18759. 0x03: { n:'PtgAdd', f:parseread1 },
  18760. 0x04: { n:'PtgSub', f:parseread1 },
  18761. 0x05: { n:'PtgMul', f:parseread1 },
  18762. 0x06: { n:'PtgDiv', f:parseread1 },
  18763. 0x07: { n:'PtgPower', f:parseread1 },
  18764. 0x08: { n:'PtgConcat', f:parseread1 },
  18765. 0x09: { n:'PtgLt', f:parseread1 },
  18766. 0x0A: { n:'PtgLe', f:parseread1 },
  18767. 0x0B: { n:'PtgEq', f:parseread1 },
  18768. 0x0C: { n:'PtgGe', f:parseread1 },
  18769. 0x0D: { n:'PtgGt', f:parseread1 },
  18770. 0x0E: { n:'PtgNe', f:parseread1 },
  18771. 0x0F: { n:'PtgIsect', f:parseread1 },
  18772. 0x10: { n:'PtgUnion', f:parseread1 },
  18773. 0x11: { n:'PtgRange', f:parseread1 },
  18774. 0x12: { n:'PtgUplus', f:parseread1 },
  18775. 0x13: { n:'PtgUminus', f:parseread1 },
  18776. 0x14: { n:'PtgPercent', f:parseread1 },
  18777. 0x15: { n:'PtgParen', f:parseread1 },
  18778. 0x16: { n:'PtgMissArg', f:parseread1 },
  18779. 0x17: { n:'PtgStr', f:parse_PtgStr },
  18780. 0x1A: { n:'PtgSheet', f:parse_PtgSheet },
  18781. 0x1B: { n:'PtgEndSheet', f:parse_PtgEndSheet },
  18782. 0x1C: { n:'PtgErr', f:parse_PtgErr },
  18783. 0x1D: { n:'PtgBool', f:parse_PtgBool },
  18784. 0x1E: { n:'PtgInt', f:parse_PtgInt },
  18785. 0x1F: { n:'PtgNum', f:parse_PtgNum },
  18786. 0x20: { n:'PtgArray', f:parse_PtgArray },
  18787. 0x21: { n:'PtgFunc', f:parse_PtgFunc },
  18788. 0x22: { n:'PtgFuncVar', f:parse_PtgFuncVar },
  18789. 0x23: { n:'PtgName', f:parse_PtgName },
  18790. 0x24: { n:'PtgRef', f:parse_PtgRef },
  18791. 0x25: { n:'PtgArea', f:parse_PtgArea },
  18792. 0x26: { n:'PtgMemArea', f:parse_PtgMemArea },
  18793. 0x27: { n:'PtgMemErr', f:parse_PtgMemErr },
  18794. 0x28: { n:'PtgMemNoMem', f:parse_PtgMemNoMem },
  18795. 0x29: { n:'PtgMemFunc', f:parse_PtgMemFunc },
  18796. 0x2A: { n:'PtgRefErr', f:parse_PtgRefErr },
  18797. 0x2B: { n:'PtgAreaErr', f:parse_PtgAreaErr },
  18798. 0x2C: { n:'PtgRefN', f:parse_PtgRefN },
  18799. 0x2D: { n:'PtgAreaN', f:parse_PtgAreaN },
  18800. 0x2E: { n:'PtgMemAreaN', f:parse_PtgMemAreaN },
  18801. 0x2F: { n:'PtgMemNoMemN', f:parse_PtgMemNoMemN },
  18802. 0x39: { n:'PtgNameX', f:parse_PtgNameX },
  18803. 0x3A: { n:'PtgRef3d', f:parse_PtgRef3d },
  18804. 0x3B: { n:'PtgArea3d', f:parse_PtgArea3d },
  18805. 0x3C: { n:'PtgRefErr3d', f:parse_PtgRefErr3d },
  18806. 0x3D: { n:'PtgAreaErr3d', f:parse_PtgAreaErr3d },
  18807. 0xFF: {}
  18808. };
  18809. /* These are duplicated in the PtgTypes table */
  18810. var PtgDupes = {
  18811. 0x40: 0x20, 0x60: 0x20,
  18812. 0x41: 0x21, 0x61: 0x21,
  18813. 0x42: 0x22, 0x62: 0x22,
  18814. 0x43: 0x23, 0x63: 0x23,
  18815. 0x44: 0x24, 0x64: 0x24,
  18816. 0x45: 0x25, 0x65: 0x25,
  18817. 0x46: 0x26, 0x66: 0x26,
  18818. 0x47: 0x27, 0x67: 0x27,
  18819. 0x48: 0x28, 0x68: 0x28,
  18820. 0x49: 0x29, 0x69: 0x29,
  18821. 0x4A: 0x2A, 0x6A: 0x2A,
  18822. 0x4B: 0x2B, 0x6B: 0x2B,
  18823. 0x4C: 0x2C, 0x6C: 0x2C,
  18824. 0x4D: 0x2D, 0x6D: 0x2D,
  18825. 0x4E: 0x2E, 0x6E: 0x2E,
  18826. 0x4F: 0x2F, 0x6F: 0x2F,
  18827. 0x58: 0x22, 0x78: 0x22,
  18828. 0x59: 0x39, 0x79: 0x39,
  18829. 0x5A: 0x3A, 0x7A: 0x3A,
  18830. 0x5B: 0x3B, 0x7B: 0x3B,
  18831. 0x5C: 0x3C, 0x7C: 0x3C,
  18832. 0x5D: 0x3D, 0x7D: 0x3D
  18833. };
  18834. (function(){for(var y in PtgDupes) PtgTypes[y] = PtgTypes[PtgDupes[y]];})();
  18835. var Ptg18 = {
  18836. 0x01: { n:'PtgElfLel', f:parse_PtgElfLel },
  18837. 0x02: { n:'PtgElfRw', f:parse_PtgElfRw },
  18838. 0x03: { n:'PtgElfCol', f:parse_PtgElfCol },
  18839. 0x06: { n:'PtgElfRwV', f:parse_PtgElfRwV },
  18840. 0x07: { n:'PtgElfColV', f:parse_PtgElfColV },
  18841. 0x0A: { n:'PtgElfRadical', f:parse_PtgElfRadical },
  18842. 0x0B: { n:'PtgElfRadicalS', f:parse_PtgElfRadicalS },
  18843. 0x0D: { n:'PtgElfColS', f:parse_PtgElfColS },
  18844. 0x0F: { n:'PtgElfColSV', f:parse_PtgElfColSV },
  18845. 0x10: { n:'PtgElfRadicalLel', f:parse_PtgElfRadicalLel },
  18846. 0x19: { n:'PtgList', f:parse_PtgList },
  18847. 0x1D: { n:'PtgSxName', f:parse_PtgSxName },
  18848. 0xFF: {}
  18849. };
  18850. var Ptg19 = {
  18851. 0x00: { n:'PtgAttrNoop', f:parse_PtgAttrNoop },
  18852. 0x01: { n:'PtgAttrSemi', f:parse_PtgAttrSemi },
  18853. 0x02: { n:'PtgAttrIf', f:parse_PtgAttrIf },
  18854. 0x04: { n:'PtgAttrChoose', f:parse_PtgAttrChoose },
  18855. 0x08: { n:'PtgAttrGoto', f:parse_PtgAttrGoto },
  18856. 0x10: { n:'PtgAttrSum', f:parse_PtgAttrSum },
  18857. 0x20: { n:'PtgAttrBaxcel', f:parse_PtgAttrBaxcel },
  18858. 0x40: { n:'PtgAttrSpace', f:parse_PtgAttrSpace },
  18859. 0x41: { n:'PtgAttrSpaceSemi', f:parse_PtgAttrSpaceSemi },
  18860. 0x80: { n:'PtgAttrIfError', f:parse_PtgAttrIfError },
  18861. 0xFF: {}
  18862. };
  18863. Ptg19[0x21] = Ptg19[0x20];
  18864. /* [MS-XLS] 2.5.198.103 ; [MS-XLSB] 2.5.97.87 */
  18865. function parse_RgbExtra(blob, length, rgce, opts) {
  18866. if(opts.biff < 8) return parsenoop(blob, length);
  18867. var target = blob.l + length;
  18868. var o = [];
  18869. for(var i = 0; i !== rgce.length; ++i) {
  18870. switch(rgce[i][0]) {
  18871. case 'PtgArray': /* PtgArray -> PtgExtraArray */
  18872. rgce[i][1] = parse_PtgExtraArray(blob, 0, opts);
  18873. o.push(rgce[i][1]);
  18874. break;
  18875. case 'PtgMemArea': /* PtgMemArea -> PtgExtraMem */
  18876. rgce[i][2] = parse_PtgExtraMem(blob, rgce[i][1], opts);
  18877. o.push(rgce[i][2]);
  18878. break;
  18879. case 'PtgExp': /* PtgExp -> PtgExtraCol */
  18880. if(opts && opts.biff == 12) {
  18881. rgce[i][1][1] = blob.read_shift(4);
  18882. o.push(rgce[i][1]);
  18883. } break;
  18884. case 'PtgList': /* TODO: PtgList -> PtgExtraList */
  18885. case 'PtgElfRadicalS': /* TODO: PtgElfRadicalS -> PtgExtraElf */
  18886. case 'PtgElfColS': /* TODO: PtgElfColS -> PtgExtraElf */
  18887. case 'PtgElfColSV': /* TODO: PtgElfColSV -> PtgExtraElf */
  18888. throw "Unsupported " + rgce[i][0];
  18889. default: break;
  18890. }
  18891. }
  18892. length = target - blob.l;
  18893. /* note: this is technically an error but Excel disregards */
  18894. //if(target !== blob.l && blob.l !== target - length) throw new Error(target + " != " + blob.l);
  18895. if(length !== 0) o.push(parsenoop(blob, length));
  18896. return o;
  18897. }
  18898. /* [MS-XLS] 2.5.198.104 ; [MS-XLSB] 2.5.97.88 */
  18899. function parse_Rgce(blob, length, opts) {
  18900. var target = blob.l + length;
  18901. var R, id, ptgs = [];
  18902. while(target != blob.l) {
  18903. length = target - blob.l;
  18904. id = blob[blob.l];
  18905. R = PtgTypes[id];
  18906. if(id === 0x18 || id === 0x19) R = (id === 0x18 ? Ptg18 : Ptg19)[blob[blob.l + 1]];
  18907. if(!R || !R.f) { /*ptgs.push*/(parsenoop(blob, length)); }
  18908. else { ptgs.push([R.n, R.f(blob, length, opts)]); }
  18909. }
  18910. return ptgs;
  18911. }
  18912. function stringify_array(f) {
  18913. var o = [];
  18914. for(var i = 0; i < f.length; ++i) {
  18915. var x = f[i], r = [];
  18916. for(var j = 0; j < x.length; ++j) {
  18917. var y = x[j];
  18918. if(y) switch(y[0]) {
  18919. // TODO: handle embedded quotes
  18920. case 0x02:
  18921. r.push('"' + y[1].replace(/"/g,'""') + '"'); break;
  18922. default: r.push(y[1]);
  18923. } else r.push("");
  18924. }
  18925. o.push(r.join(","));
  18926. }
  18927. return o.join(";");
  18928. }
  18929. /* [MS-XLS] 2.2.2 ; [MS-XLSB] 2.2.2 TODO */
  18930. var PtgBinOp = {
  18931. PtgAdd: "+",
  18932. PtgConcat: "&",
  18933. PtgDiv: "/",
  18934. PtgEq: "=",
  18935. PtgGe: ">=",
  18936. PtgGt: ">",
  18937. PtgLe: "<=",
  18938. PtgLt: "<",
  18939. PtgMul: "*",
  18940. PtgNe: "<>",
  18941. PtgPower: "^",
  18942. PtgSub: "-"
  18943. };
  18944. function formula_quote_sheet_name(sname, opts) {
  18945. if(!sname && !(opts && opts.biff <= 5 && opts.biff >= 2)) throw new Error("empty sheet name");
  18946. if(sname.indexOf(" ") > -1) return "'" + sname + "'";
  18947. return sname;
  18948. }
  18949. function get_ixti_raw(supbooks, ixti, opts) {
  18950. if(!supbooks) return "SH33TJSERR0";
  18951. if(opts.biff > 8 && (!supbooks.XTI || !supbooks.XTI[ixti])) return supbooks.SheetNames[ixti];
  18952. if(!supbooks.XTI) return "SH33TJSERR6";
  18953. var XTI = supbooks.XTI[ixti];
  18954. if(opts.biff < 8) {
  18955. if(ixti > 10000) ixti-= 65536;
  18956. if(ixti < 0) ixti = -ixti;
  18957. return ixti == 0 ? "" : supbooks.XTI[ixti - 1];
  18958. }
  18959. if(!XTI) return "SH33TJSERR1";
  18960. var o = "";
  18961. if(opts.biff > 8) switch(supbooks[XTI[0]][0]) {
  18962. case 0x0165: /* 'BrtSupSelf' */
  18963. o = XTI[1] == -1 ? "#REF" : supbooks.SheetNames[XTI[1]];
  18964. return XTI[1] == XTI[2] ? o : o + ":" + supbooks.SheetNames[XTI[2]];
  18965. case 0x0166: /* 'BrtSupSame' */
  18966. if(opts.SID != null) return supbooks.SheetNames[opts.SID];
  18967. return "SH33TJSSAME" + supbooks[XTI[0]][0];
  18968. case 0x0163: /* 'BrtSupBookSrc' */
  18969. /* falls through */
  18970. default: return "SH33TJSSRC" + supbooks[XTI[0]][0];
  18971. }
  18972. switch(supbooks[XTI[0]][0][0]) {
  18973. case 0x0401:
  18974. o = XTI[1] == -1 ? "#REF" : (supbooks.SheetNames[XTI[1]] || "SH33TJSERR3");
  18975. return XTI[1] == XTI[2] ? o : o + ":" + supbooks.SheetNames[XTI[2]];
  18976. case 0x3A01: return "SH33TJSERR8";
  18977. default:
  18978. if(!supbooks[XTI[0]][0][3]) return "SH33TJSERR2";
  18979. o = XTI[1] == -1 ? "#REF" : (supbooks[XTI[0]][0][3][XTI[1]] || "SH33TJSERR4");
  18980. return XTI[1] == XTI[2] ? o : o + ":" + supbooks[XTI[0]][0][3][XTI[2]];
  18981. }
  18982. }
  18983. function get_ixti(supbooks, ixti, opts) {
  18984. return formula_quote_sheet_name(get_ixti_raw(supbooks, ixti, opts), opts);
  18985. }
  18986. function stringify_formula(formula/*Array<any>*/, range, cell, supbooks, opts) {
  18987. var biff = (opts && opts.biff) || 8;
  18988. var _range = /*range != null ? range :*/ {s:{c:0, r:0},e:{c:0, r:0}};
  18989. var stack = [], e1, e2, c, ixti=0, nameidx=0, r, sname="";
  18990. if(!formula[0] || !formula[0][0]) return "";
  18991. var last_sp = -1, sp = "";
  18992. for(var ff = 0, fflen = formula[0].length; ff < fflen; ++ff) {
  18993. var f = formula[0][ff];
  18994. switch(f[0]) {
  18995. case 'PtgUminus': /* [MS-XLS] 2.5.198.93 */
  18996. stack.push("-" + stack.pop()); break;
  18997. case 'PtgUplus': /* [MS-XLS] 2.5.198.95 */
  18998. stack.push("+" + stack.pop()); break;
  18999. case 'PtgPercent': /* [MS-XLS] 2.5.198.81 */
  19000. stack.push(stack.pop() + "%"); break;
  19001. case 'PtgAdd': /* [MS-XLS] 2.5.198.26 */
  19002. case 'PtgConcat': /* [MS-XLS] 2.5.198.43 */
  19003. case 'PtgDiv': /* [MS-XLS] 2.5.198.45 */
  19004. case 'PtgEq': /* [MS-XLS] 2.5.198.56 */
  19005. case 'PtgGe': /* [MS-XLS] 2.5.198.64 */
  19006. case 'PtgGt': /* [MS-XLS] 2.5.198.65 */
  19007. case 'PtgLe': /* [MS-XLS] 2.5.198.68 */
  19008. case 'PtgLt': /* [MS-XLS] 2.5.198.69 */
  19009. case 'PtgMul': /* [MS-XLS] 2.5.198.75 */
  19010. case 'PtgNe': /* [MS-XLS] 2.5.198.78 */
  19011. case 'PtgPower': /* [MS-XLS] 2.5.198.82 */
  19012. case 'PtgSub': /* [MS-XLS] 2.5.198.90 */
  19013. e1 = stack.pop(); e2 = stack.pop();
  19014. if(last_sp >= 0) {
  19015. switch(formula[0][last_sp][1][0]) {
  19016. case 0:
  19017. // $FlowIgnore
  19018. sp = fill(" ", formula[0][last_sp][1][1]); break;
  19019. case 1:
  19020. // $FlowIgnore
  19021. sp = fill("\r", formula[0][last_sp][1][1]); break;
  19022. default:
  19023. sp = "";
  19024. // $FlowIgnore
  19025. if(opts.WTF) throw new Error("Unexpected PtgAttrSpaceType " + formula[0][last_sp][1][0]);
  19026. }
  19027. e2 = e2 + sp;
  19028. last_sp = -1;
  19029. }
  19030. stack.push(e2+PtgBinOp[f[0]]+e1);
  19031. break;
  19032. case 'PtgIsect': /* [MS-XLS] 2.5.198.67 */
  19033. e1 = stack.pop(); e2 = stack.pop();
  19034. stack.push(e2+" "+e1);
  19035. break;
  19036. case 'PtgUnion': /* [MS-XLS] 2.5.198.94 */
  19037. e1 = stack.pop(); e2 = stack.pop();
  19038. stack.push(e2+","+e1);
  19039. break;
  19040. case 'PtgRange': /* [MS-XLS] 2.5.198.83 */
  19041. e1 = stack.pop(); e2 = stack.pop();
  19042. stack.push(e2+":"+e1);
  19043. break;
  19044. case 'PtgAttrChoose': /* [MS-XLS] 2.5.198.34 */
  19045. break;
  19046. case 'PtgAttrGoto': /* [MS-XLS] 2.5.198.35 */
  19047. break;
  19048. case 'PtgAttrIf': /* [MS-XLS] 2.5.198.36 */
  19049. break;
  19050. case 'PtgAttrIfError': /* [MS-XLSB] 2.5.97.28 */
  19051. break;
  19052. case 'PtgRef': /* [MS-XLS] 2.5.198.84 */
  19053. c = shift_cell_xls((f[1][1]), _range, opts);
  19054. stack.push(encode_cell_xls(c, biff));
  19055. break;
  19056. case 'PtgRefN': /* [MS-XLS] 2.5.198.88 */
  19057. c = cell ? shift_cell_xls((f[1][1]), cell, opts) : (f[1][1]);
  19058. stack.push(encode_cell_xls(c, biff));
  19059. break;
  19060. case 'PtgRef3d': /* [MS-XLS] 2.5.198.85 */
  19061. ixti = f[1][1]; c = shift_cell_xls((f[1][2]), _range, opts);
  19062. sname = get_ixti(supbooks, ixti, opts);
  19063. var w = sname; /* IE9 fails on defined names */ // eslint-disable-line no-unused-vars
  19064. stack.push(sname + "!" + encode_cell_xls(c, biff));
  19065. break;
  19066. case 'PtgFunc': /* [MS-XLS] 2.5.198.62 */
  19067. case 'PtgFuncVar': /* [MS-XLS] 2.5.198.63 */
  19068. /* f[1] = [argc, func, type] */
  19069. var argc = (f[1][0]), func = (f[1][1]);
  19070. if(!argc) argc = 0;
  19071. argc &= 0x7F;
  19072. var args = argc == 0 ? [] : stack.slice(-argc);
  19073. stack.length -= argc;
  19074. if(func === 'User') func = args.shift();
  19075. stack.push(func + "(" + args.join(",") + ")");
  19076. break;
  19077. case 'PtgBool': /* [MS-XLS] 2.5.198.42 */
  19078. stack.push(f[1] ? "TRUE" : "FALSE"); break;
  19079. case 'PtgInt': /* [MS-XLS] 2.5.198.66 */
  19080. stack.push(f[1]); break;
  19081. case 'PtgNum': /* [MS-XLS] 2.5.198.79 TODO: precision? */
  19082. stack.push(String(f[1])); break;
  19083. case 'PtgStr': /* [MS-XLS] 2.5.198.89 */
  19084. // $FlowIgnore
  19085. stack.push('"' + f[1] + '"'); break;
  19086. case 'PtgErr': /* [MS-XLS] 2.5.198.57 */
  19087. stack.push(f[1]); break;
  19088. case 'PtgAreaN': /* [MS-XLS] 2.5.198.31 TODO */
  19089. r = shift_range_xls(f[1][1], cell ? {s:cell} : _range, opts);
  19090. stack.push(encode_range_xls((r), opts));
  19091. break;
  19092. case 'PtgArea': /* [MS-XLS] 2.5.198.27 TODO: fixed points */
  19093. r = shift_range_xls(f[1][1], _range, opts);
  19094. stack.push(encode_range_xls((r), opts));
  19095. break;
  19096. case 'PtgArea3d': /* [MS-XLS] 2.5.198.28 TODO */
  19097. ixti = f[1][1]; r = f[1][2];
  19098. sname = get_ixti(supbooks, ixti, opts);
  19099. stack.push(sname + "!" + encode_range_xls((r), opts));
  19100. break;
  19101. case 'PtgAttrSum': /* [MS-XLS] 2.5.198.41 */
  19102. stack.push("SUM(" + stack.pop() + ")");
  19103. break;
  19104. case 'PtgAttrBaxcel': /* [MS-XLS] 2.5.198.33 */
  19105. case 'PtgAttrSemi': /* [MS-XLS] 2.5.198.37 */
  19106. break;
  19107. case 'PtgName': /* [MS-XLS] 2.5.198.76 ; [MS-XLSB] 2.5.97.60 TODO: revisions */
  19108. /* f[1] = type, 0, nameindex */
  19109. nameidx = (f[1][2]);
  19110. var lbl = (supbooks.names||[])[nameidx-1] || (supbooks[0]||[])[nameidx];
  19111. var name = lbl ? lbl.Name : "SH33TJSNAME" + String(nameidx);
  19112. if(name in XLSXFutureFunctions) name = XLSXFutureFunctions[name];
  19113. stack.push(name);
  19114. break;
  19115. case 'PtgNameX': /* [MS-XLS] 2.5.198.77 ; [MS-XLSB] 2.5.97.61 TODO: revisions */
  19116. /* f[1] = type, ixti, nameindex */
  19117. var bookidx = (f[1][1]); nameidx = (f[1][2]); var externbook;
  19118. /* TODO: Properly handle missing values */
  19119. if(opts.biff <= 5) {
  19120. if(bookidx < 0) bookidx = -bookidx;
  19121. if(supbooks[bookidx]) externbook = supbooks[bookidx][nameidx];
  19122. } else {
  19123. var o = "";
  19124. if(((supbooks[bookidx]||[])[0]||[])[0] == 0x3A01){/* empty */}
  19125. else if(((supbooks[bookidx]||[])[0]||[])[0] == 0x0401){
  19126. if(supbooks[bookidx][nameidx] && supbooks[bookidx][nameidx].itab > 0) {
  19127. o = supbooks.SheetNames[supbooks[bookidx][nameidx].itab-1] + "!";
  19128. }
  19129. }
  19130. else o = supbooks.SheetNames[nameidx-1]+ "!";
  19131. if(supbooks[bookidx] && supbooks[bookidx][nameidx]) o += supbooks[bookidx][nameidx].Name;
  19132. else if(supbooks[0] && supbooks[0][nameidx]) o += supbooks[0][nameidx].Name;
  19133. else o += "SH33TJSERRX";
  19134. stack.push(o);
  19135. break;
  19136. }
  19137. if(!externbook) externbook = {Name: "SH33TJSERRY"};
  19138. stack.push(externbook.Name);
  19139. break;
  19140. case 'PtgParen': /* [MS-XLS] 2.5.198.80 */
  19141. var lp = '(', rp = ')';
  19142. if(last_sp >= 0) {
  19143. sp = "";
  19144. switch(formula[0][last_sp][1][0]) {
  19145. // $FlowIgnore
  19146. case 2: lp = fill(" ", formula[0][last_sp][1][1]) + lp; break;
  19147. // $FlowIgnore
  19148. case 3: lp = fill("\r", formula[0][last_sp][1][1]) + lp; break;
  19149. // $FlowIgnore
  19150. case 4: rp = fill(" ", formula[0][last_sp][1][1]) + rp; break;
  19151. // $FlowIgnore
  19152. case 5: rp = fill("\r", formula[0][last_sp][1][1]) + rp; break;
  19153. default:
  19154. // $FlowIgnore
  19155. if(opts.WTF) throw new Error("Unexpected PtgAttrSpaceType " + formula[0][last_sp][1][0]);
  19156. }
  19157. last_sp = -1;
  19158. }
  19159. stack.push(lp + stack.pop() + rp); break;
  19160. case 'PtgRefErr': /* [MS-XLS] 2.5.198.86 */
  19161. stack.push('#REF!'); break;
  19162. case 'PtgRefErr3d': /* [MS-XLS] 2.5.198.87 */
  19163. stack.push('#REF!'); break;
  19164. case 'PtgExp': /* [MS-XLS] 2.5.198.58 TODO */
  19165. c = {c:(f[1][1]),r:(f[1][0])};
  19166. var q = ({c: cell.c, r:cell.r});
  19167. if(supbooks.sharedf[encode_cell(c)]) {
  19168. var parsedf = (supbooks.sharedf[encode_cell(c)]);
  19169. stack.push(stringify_formula(parsedf, _range, q, supbooks, opts));
  19170. }
  19171. else {
  19172. var fnd = false;
  19173. for(e1=0;e1!=supbooks.arrayf.length; ++e1) {
  19174. /* TODO: should be something like range_has */
  19175. e2 = supbooks.arrayf[e1];
  19176. if(c.c < e2[0].s.c || c.c > e2[0].e.c) continue;
  19177. if(c.r < e2[0].s.r || c.r > e2[0].e.r) continue;
  19178. stack.push(stringify_formula(e2[1], _range, q, supbooks, opts));
  19179. fnd = true;
  19180. break;
  19181. }
  19182. if(!fnd) stack.push(f[1]);
  19183. }
  19184. break;
  19185. case 'PtgArray': /* [MS-XLS] 2.5.198.32 TODO */
  19186. stack.push("{" + stringify_array(f[1]) + "}");
  19187. break;
  19188. case 'PtgMemArea': /* [MS-XLS] 2.5.198.70 TODO: confirm this is a non-display */
  19189. //stack.push("(" + f[2].map(encode_range).join(",") + ")");
  19190. break;
  19191. case 'PtgAttrSpace': /* [MS-XLS] 2.5.198.38 */
  19192. case 'PtgAttrSpaceSemi': /* [MS-XLS] 2.5.198.39 */
  19193. last_sp = ff;
  19194. break;
  19195. case 'PtgTbl': /* [MS-XLS] 2.5.198.92 TODO */
  19196. break;
  19197. case 'PtgMemErr': /* [MS-XLS] 2.5.198.71 */
  19198. break;
  19199. case 'PtgMissArg': /* [MS-XLS] 2.5.198.74 */
  19200. stack.push("");
  19201. break;
  19202. case 'PtgAreaErr': /* [MS-XLS] 2.5.198.29 */
  19203. stack.push("#REF!"); break;
  19204. case 'PtgAreaErr3d': /* [MS-XLS] 2.5.198.30 */
  19205. stack.push("#REF!"); break;
  19206. case 'PtgList': /* [MS-XLSB] 2.5.97.52 */
  19207. // $FlowIgnore
  19208. stack.push("Table" + f[1].idx + "[#" + f[1].rt + "]");
  19209. break;
  19210. case 'PtgMemAreaN':
  19211. case 'PtgMemNoMemN':
  19212. case 'PtgAttrNoop':
  19213. case 'PtgSheet':
  19214. case 'PtgEndSheet':
  19215. break;
  19216. case 'PtgMemFunc': /* [MS-XLS] 2.5.198.72 TODO */
  19217. break;
  19218. case 'PtgMemNoMem': /* [MS-XLS] 2.5.198.73 TODO */
  19219. break;
  19220. case 'PtgElfCol': /* [MS-XLS] 2.5.198.46 */
  19221. case 'PtgElfColS': /* [MS-XLS] 2.5.198.47 */
  19222. case 'PtgElfColSV': /* [MS-XLS] 2.5.198.48 */
  19223. case 'PtgElfColV': /* [MS-XLS] 2.5.198.49 */
  19224. case 'PtgElfLel': /* [MS-XLS] 2.5.198.50 */
  19225. case 'PtgElfRadical': /* [MS-XLS] 2.5.198.51 */
  19226. case 'PtgElfRadicalLel': /* [MS-XLS] 2.5.198.52 */
  19227. case 'PtgElfRadicalS': /* [MS-XLS] 2.5.198.53 */
  19228. case 'PtgElfRw': /* [MS-XLS] 2.5.198.54 */
  19229. case 'PtgElfRwV': /* [MS-XLS] 2.5.198.55 */
  19230. throw new Error("Unsupported ELFs");
  19231. case 'PtgSxName': /* [MS-XLS] 2.5.198.91 TODO -- find a test case */
  19232. throw new Error('Unrecognized Formula Token: ' + String(f));
  19233. default: throw new Error('Unrecognized Formula Token: ' + String(f));
  19234. }
  19235. var PtgNonDisp = ['PtgAttrSpace', 'PtgAttrSpaceSemi', 'PtgAttrGoto'];
  19236. if(opts.biff != 3) if(last_sp >= 0 && PtgNonDisp.indexOf(formula[0][ff][0]) == -1) {
  19237. f = formula[0][last_sp];
  19238. var _left = true;
  19239. switch(f[1][0]) {
  19240. /* note: some bad XLSB files omit the PtgParen */
  19241. case 4: _left = false;
  19242. /* falls through */
  19243. case 0:
  19244. // $FlowIgnore
  19245. sp = fill(" ", f[1][1]); break;
  19246. case 5: _left = false;
  19247. /* falls through */
  19248. case 1:
  19249. // $FlowIgnore
  19250. sp = fill("\r", f[1][1]); break;
  19251. default:
  19252. sp = "";
  19253. // $FlowIgnore
  19254. if(opts.WTF) throw new Error("Unexpected PtgAttrSpaceType " + f[1][0]);
  19255. }
  19256. stack.push((_left ? sp : "") + stack.pop() + (_left ? "" : sp));
  19257. last_sp = -1;
  19258. }
  19259. }
  19260. if(stack.length > 1 && opts.WTF) throw new Error("bad formula stack");
  19261. return stack[0];
  19262. }
  19263. /* [MS-XLS] 2.5.198.1 TODO */
  19264. function parse_ArrayParsedFormula(blob, length, opts) {
  19265. var target = blob.l + length, len = opts.biff == 2 ? 1 : 2;
  19266. var rgcb, cce = blob.read_shift(len); // length of rgce
  19267. if(cce == 0xFFFF) return [[],parsenoop(blob, length-2)];
  19268. var rgce = parse_Rgce(blob, cce, opts);
  19269. if(length !== cce + len) rgcb = parse_RgbExtra(blob, length - cce - len, rgce, opts);
  19270. blob.l = target;
  19271. return [rgce, rgcb];
  19272. }
  19273. /* [MS-XLS] 2.5.198.3 TODO */
  19274. function parse_XLSCellParsedFormula(blob, length, opts) {
  19275. var target = blob.l + length, len = opts.biff == 2 ? 1 : 2;
  19276. var rgcb, cce = blob.read_shift(len); // length of rgce
  19277. if(cce == 0xFFFF) return [[],parsenoop(blob, length-2)];
  19278. var rgce = parse_Rgce(blob, cce, opts);
  19279. if(length !== cce + len) rgcb = parse_RgbExtra(blob, length - cce - len, rgce, opts);
  19280. blob.l = target;
  19281. return [rgce, rgcb];
  19282. }
  19283. /* [MS-XLS] 2.5.198.21 */
  19284. function parse_NameParsedFormula(blob, length, opts, cce) {
  19285. var target = blob.l + length;
  19286. var rgce = parse_Rgce(blob, cce, opts);
  19287. var rgcb;
  19288. if(target !== blob.l) rgcb = parse_RgbExtra(blob, target - blob.l, rgce, opts);
  19289. return [rgce, rgcb];
  19290. }
  19291. /* [MS-XLS] 2.5.198.118 TODO */
  19292. function parse_SharedParsedFormula(blob, length, opts) {
  19293. var target = blob.l + length;
  19294. var rgcb, cce = blob.read_shift(2); // length of rgce
  19295. var rgce = parse_Rgce(blob, cce, opts);
  19296. if(cce == 0xFFFF) return [[],parsenoop(blob, length-2)];
  19297. if(length !== cce + 2) rgcb = parse_RgbExtra(blob, target - cce - 2, rgce, opts);
  19298. return [rgce, rgcb];
  19299. }
  19300. /* [MS-XLS] 2.5.133 TODO: how to emit empty strings? */
  19301. function parse_FormulaValue(blob) {
  19302. var b;
  19303. if(__readUInt16LE(blob,blob.l + 6) !== 0xFFFF) return [parse_Xnum(blob),'n'];
  19304. switch(blob[blob.l]) {
  19305. case 0x00: blob.l += 8; return ["String", 's'];
  19306. case 0x01: b = blob[blob.l+2] === 0x1; blob.l += 8; return [b,'b'];
  19307. case 0x02: b = blob[blob.l+2]; blob.l += 8; return [b,'e'];
  19308. case 0x03: blob.l += 8; return ["",'s'];
  19309. }
  19310. return [];
  19311. }
  19312. /* [MS-XLS] 2.4.127 TODO */
  19313. function parse_Formula(blob, length, opts) {
  19314. var end = blob.l + length;
  19315. var cell = parse_XLSCell(blob, 6);
  19316. if(opts.biff == 2) ++blob.l;
  19317. var val = parse_FormulaValue(blob,8);
  19318. var flags = blob.read_shift(1);
  19319. if(opts.biff != 2) {
  19320. blob.read_shift(1);
  19321. if(opts.biff >= 5) {
  19322. /*var chn = */blob.read_shift(4);
  19323. }
  19324. }
  19325. var cbf = parse_XLSCellParsedFormula(blob, end - blob.l, opts);
  19326. return {cell:cell, val:val[0], formula:cbf, shared: (flags >> 3) & 1, tt:val[1]};
  19327. }
  19328. /* XLSB Parsed Formula records have the same shape */
  19329. function parse_XLSBParsedFormula(data, length, opts) {
  19330. var cce = data.read_shift(4);
  19331. var rgce = parse_Rgce(data, cce, opts);
  19332. var cb = data.read_shift(4);
  19333. var rgcb = cb > 0 ? parse_RgbExtra(data, cb, rgce, opts) : null;
  19334. return [rgce, rgcb];
  19335. }
  19336. /* [MS-XLSB] 2.5.97.1 ArrayParsedFormula */
  19337. var parse_XLSBArrayParsedFormula = parse_XLSBParsedFormula;
  19338. /* [MS-XLSB] 2.5.97.4 CellParsedFormula */
  19339. var parse_XLSBCellParsedFormula = parse_XLSBParsedFormula;
  19340. /* [MS-XLSB] 2.5.97.12 NameParsedFormula */
  19341. var parse_XLSBNameParsedFormula = parse_XLSBParsedFormula;
  19342. /* [MS-XLSB] 2.5.97.98 SharedParsedFormula */
  19343. var parse_XLSBSharedParsedFormula = parse_XLSBParsedFormula;
  19344. /* [MS-XLS] 2.5.198.4 */
  19345. var Cetab = {
  19346. 0x0000: 'BEEP',
  19347. 0x0001: 'OPEN',
  19348. 0x0002: 'OPEN.LINKS',
  19349. 0x0003: 'CLOSE.ALL',
  19350. 0x0004: 'SAVE',
  19351. 0x0005: 'SAVE.AS',
  19352. 0x0006: 'FILE.DELETE',
  19353. 0x0007: 'PAGE.SETUP',
  19354. 0x0008: 'PRINT',
  19355. 0x0009: 'PRINTER.SETUP',
  19356. 0x000A: 'QUIT',
  19357. 0x000B: 'NEW.WINDOW',
  19358. 0x000C: 'ARRANGE.ALL',
  19359. 0x000D: 'WINDOW.SIZE',
  19360. 0x000E: 'WINDOW.MOVE',
  19361. 0x000F: 'FULL',
  19362. 0x0010: 'CLOSE',
  19363. 0x0011: 'RUN',
  19364. 0x0016: 'SET.PRINT.AREA',
  19365. 0x0017: 'SET.PRINT.TITLES',
  19366. 0x0018: 'SET.PAGE.BREAK',
  19367. 0x0019: 'REMOVE.PAGE.BREAK',
  19368. 0x001A: 'FONT',
  19369. 0x001B: 'DISPLAY',
  19370. 0x001C: 'PROTECT.DOCUMENT',
  19371. 0x001D: 'PRECISION',
  19372. 0x001E: 'A1.R1C1',
  19373. 0x001F: 'CALCULATE.NOW',
  19374. 0x0020: 'CALCULATION',
  19375. 0x0022: 'DATA.FIND',
  19376. 0x0023: 'EXTRACT',
  19377. 0x0024: 'DATA.DELETE',
  19378. 0x0025: 'SET.DATABASE',
  19379. 0x0026: 'SET.CRITERIA',
  19380. 0x0027: 'SORT',
  19381. 0x0028: 'DATA.SERIES',
  19382. 0x0029: 'TABLE',
  19383. 0x002A: 'FORMAT.NUMBER',
  19384. 0x002B: 'ALIGNMENT',
  19385. 0x002C: 'STYLE',
  19386. 0x002D: 'BORDER',
  19387. 0x002E: 'CELL.PROTECTION',
  19388. 0x002F: 'COLUMN.WIDTH',
  19389. 0x0030: 'UNDO',
  19390. 0x0031: 'CUT',
  19391. 0x0032: 'COPY',
  19392. 0x0033: 'PASTE',
  19393. 0x0034: 'CLEAR',
  19394. 0x0035: 'PASTE.SPECIAL',
  19395. 0x0036: 'EDIT.DELETE',
  19396. 0x0037: 'INSERT',
  19397. 0x0038: 'FILL.RIGHT',
  19398. 0x0039: 'FILL.DOWN',
  19399. 0x003D: 'DEFINE.NAME',
  19400. 0x003E: 'CREATE.NAMES',
  19401. 0x003F: 'FORMULA.GOTO',
  19402. 0x0040: 'FORMULA.FIND',
  19403. 0x0041: 'SELECT.LAST.CELL',
  19404. 0x0042: 'SHOW.ACTIVE.CELL',
  19405. 0x0043: 'GALLERY.AREA',
  19406. 0x0044: 'GALLERY.BAR',
  19407. 0x0045: 'GALLERY.COLUMN',
  19408. 0x0046: 'GALLERY.LINE',
  19409. 0x0047: 'GALLERY.PIE',
  19410. 0x0048: 'GALLERY.SCATTER',
  19411. 0x0049: 'COMBINATION',
  19412. 0x004A: 'PREFERRED',
  19413. 0x004B: 'ADD.OVERLAY',
  19414. 0x004C: 'GRIDLINES',
  19415. 0x004D: 'SET.PREFERRED',
  19416. 0x004E: 'AXES',
  19417. 0x004F: 'LEGEND',
  19418. 0x0050: 'ATTACH.TEXT',
  19419. 0x0051: 'ADD.ARROW',
  19420. 0x0052: 'SELECT.CHART',
  19421. 0x0053: 'SELECT.PLOT.AREA',
  19422. 0x0054: 'PATTERNS',
  19423. 0x0055: 'MAIN.CHART',
  19424. 0x0056: 'OVERLAY',
  19425. 0x0057: 'SCALE',
  19426. 0x0058: 'FORMAT.LEGEND',
  19427. 0x0059: 'FORMAT.TEXT',
  19428. 0x005A: 'EDIT.REPEAT',
  19429. 0x005B: 'PARSE',
  19430. 0x005C: 'JUSTIFY',
  19431. 0x005D: 'HIDE',
  19432. 0x005E: 'UNHIDE',
  19433. 0x005F: 'WORKSPACE',
  19434. 0x0060: 'FORMULA',
  19435. 0x0061: 'FORMULA.FILL',
  19436. 0x0062: 'FORMULA.ARRAY',
  19437. 0x0063: 'DATA.FIND.NEXT',
  19438. 0x0064: 'DATA.FIND.PREV',
  19439. 0x0065: 'FORMULA.FIND.NEXT',
  19440. 0x0066: 'FORMULA.FIND.PREV',
  19441. 0x0067: 'ACTIVATE',
  19442. 0x0068: 'ACTIVATE.NEXT',
  19443. 0x0069: 'ACTIVATE.PREV',
  19444. 0x006A: 'UNLOCKED.NEXT',
  19445. 0x006B: 'UNLOCKED.PREV',
  19446. 0x006C: 'COPY.PICTURE',
  19447. 0x006D: 'SELECT',
  19448. 0x006E: 'DELETE.NAME',
  19449. 0x006F: 'DELETE.FORMAT',
  19450. 0x0070: 'VLINE',
  19451. 0x0071: 'HLINE',
  19452. 0x0072: 'VPAGE',
  19453. 0x0073: 'HPAGE',
  19454. 0x0074: 'VSCROLL',
  19455. 0x0075: 'HSCROLL',
  19456. 0x0076: 'ALERT',
  19457. 0x0077: 'NEW',
  19458. 0x0078: 'CANCEL.COPY',
  19459. 0x0079: 'SHOW.CLIPBOARD',
  19460. 0x007A: 'MESSAGE',
  19461. 0x007C: 'PASTE.LINK',
  19462. 0x007D: 'APP.ACTIVATE',
  19463. 0x007E: 'DELETE.ARROW',
  19464. 0x007F: 'ROW.HEIGHT',
  19465. 0x0080: 'FORMAT.MOVE',
  19466. 0x0081: 'FORMAT.SIZE',
  19467. 0x0082: 'FORMULA.REPLACE',
  19468. 0x0083: 'SEND.KEYS',
  19469. 0x0084: 'SELECT.SPECIAL',
  19470. 0x0085: 'APPLY.NAMES',
  19471. 0x0086: 'REPLACE.FONT',
  19472. 0x0087: 'FREEZE.PANES',
  19473. 0x0088: 'SHOW.INFO',
  19474. 0x0089: 'SPLIT',
  19475. 0x008A: 'ON.WINDOW',
  19476. 0x008B: 'ON.DATA',
  19477. 0x008C: 'DISABLE.INPUT',
  19478. 0x008E: 'OUTLINE',
  19479. 0x008F: 'LIST.NAMES',
  19480. 0x0090: 'FILE.CLOSE',
  19481. 0x0091: 'SAVE.WORKBOOK',
  19482. 0x0092: 'DATA.FORM',
  19483. 0x0093: 'COPY.CHART',
  19484. 0x0094: 'ON.TIME',
  19485. 0x0095: 'WAIT',
  19486. 0x0096: 'FORMAT.FONT',
  19487. 0x0097: 'FILL.UP',
  19488. 0x0098: 'FILL.LEFT',
  19489. 0x0099: 'DELETE.OVERLAY',
  19490. 0x009B: 'SHORT.MENUS',
  19491. 0x009F: 'SET.UPDATE.STATUS',
  19492. 0x00A1: 'COLOR.PALETTE',
  19493. 0x00A2: 'DELETE.STYLE',
  19494. 0x00A3: 'WINDOW.RESTORE',
  19495. 0x00A4: 'WINDOW.MAXIMIZE',
  19496. 0x00A6: 'CHANGE.LINK',
  19497. 0x00A7: 'CALCULATE.DOCUMENT',
  19498. 0x00A8: 'ON.KEY',
  19499. 0x00A9: 'APP.RESTORE',
  19500. 0x00AA: 'APP.MOVE',
  19501. 0x00AB: 'APP.SIZE',
  19502. 0x00AC: 'APP.MINIMIZE',
  19503. 0x00AD: 'APP.MAXIMIZE',
  19504. 0x00AE: 'BRING.TO.FRONT',
  19505. 0x00AF: 'SEND.TO.BACK',
  19506. 0x00B9: 'MAIN.CHART.TYPE',
  19507. 0x00BA: 'OVERLAY.CHART.TYPE',
  19508. 0x00BB: 'SELECT.END',
  19509. 0x00BC: 'OPEN.MAIL',
  19510. 0x00BD: 'SEND.MAIL',
  19511. 0x00BE: 'STANDARD.FONT',
  19512. 0x00BF: 'CONSOLIDATE',
  19513. 0x00C0: 'SORT.SPECIAL',
  19514. 0x00C1: 'GALLERY.3D.AREA',
  19515. 0x00C2: 'GALLERY.3D.COLUMN',
  19516. 0x00C3: 'GALLERY.3D.LINE',
  19517. 0x00C4: 'GALLERY.3D.PIE',
  19518. 0x00C5: 'VIEW.3D',
  19519. 0x00C6: 'GOAL.SEEK',
  19520. 0x00C7: 'WORKGROUP',
  19521. 0x00C8: 'FILL.GROUP',
  19522. 0x00C9: 'UPDATE.LINK',
  19523. 0x00CA: 'PROMOTE',
  19524. 0x00CB: 'DEMOTE',
  19525. 0x00CC: 'SHOW.DETAIL',
  19526. 0x00CE: 'UNGROUP',
  19527. 0x00CF: 'OBJECT.PROPERTIES',
  19528. 0x00D0: 'SAVE.NEW.OBJECT',
  19529. 0x00D1: 'SHARE',
  19530. 0x00D2: 'SHARE.NAME',
  19531. 0x00D3: 'DUPLICATE',
  19532. 0x00D4: 'APPLY.STYLE',
  19533. 0x00D5: 'ASSIGN.TO.OBJECT',
  19534. 0x00D6: 'OBJECT.PROTECTION',
  19535. 0x00D7: 'HIDE.OBJECT',
  19536. 0x00D8: 'SET.EXTRACT',
  19537. 0x00D9: 'CREATE.PUBLISHER',
  19538. 0x00DA: 'SUBSCRIBE.TO',
  19539. 0x00DB: 'ATTRIBUTES',
  19540. 0x00DC: 'SHOW.TOOLBAR',
  19541. 0x00DE: 'PRINT.PREVIEW',
  19542. 0x00DF: 'EDIT.COLOR',
  19543. 0x00E0: 'SHOW.LEVELS',
  19544. 0x00E1: 'FORMAT.MAIN',
  19545. 0x00E2: 'FORMAT.OVERLAY',
  19546. 0x00E3: 'ON.RECALC',
  19547. 0x00E4: 'EDIT.SERIES',
  19548. 0x00E5: 'DEFINE.STYLE',
  19549. 0x00F0: 'LINE.PRINT',
  19550. 0x00F3: 'ENTER.DATA',
  19551. 0x00F9: 'GALLERY.RADAR',
  19552. 0x00FA: 'MERGE.STYLES',
  19553. 0x00FB: 'EDITION.OPTIONS',
  19554. 0x00FC: 'PASTE.PICTURE',
  19555. 0x00FD: 'PASTE.PICTURE.LINK',
  19556. 0x00FE: 'SPELLING',
  19557. 0x0100: 'ZOOM',
  19558. 0x0103: 'INSERT.OBJECT',
  19559. 0x0104: 'WINDOW.MINIMIZE',
  19560. 0x0109: 'SOUND.NOTE',
  19561. 0x010A: 'SOUND.PLAY',
  19562. 0x010B: 'FORMAT.SHAPE',
  19563. 0x010C: 'EXTEND.POLYGON',
  19564. 0x010D: 'FORMAT.AUTO',
  19565. 0x0110: 'GALLERY.3D.BAR',
  19566. 0x0111: 'GALLERY.3D.SURFACE',
  19567. 0x0112: 'FILL.AUTO',
  19568. 0x0114: 'CUSTOMIZE.TOOLBAR',
  19569. 0x0115: 'ADD.TOOL',
  19570. 0x0116: 'EDIT.OBJECT',
  19571. 0x0117: 'ON.DOUBLECLICK',
  19572. 0x0118: 'ON.ENTRY',
  19573. 0x0119: 'WORKBOOK.ADD',
  19574. 0x011A: 'WORKBOOK.MOVE',
  19575. 0x011B: 'WORKBOOK.COPY',
  19576. 0x011C: 'WORKBOOK.OPTIONS',
  19577. 0x011D: 'SAVE.WORKSPACE',
  19578. 0x0120: 'CHART.WIZARD',
  19579. 0x0121: 'DELETE.TOOL',
  19580. 0x0122: 'MOVE.TOOL',
  19581. 0x0123: 'WORKBOOK.SELECT',
  19582. 0x0124: 'WORKBOOK.ACTIVATE',
  19583. 0x0125: 'ASSIGN.TO.TOOL',
  19584. 0x0127: 'COPY.TOOL',
  19585. 0x0128: 'RESET.TOOL',
  19586. 0x0129: 'CONSTRAIN.NUMERIC',
  19587. 0x012A: 'PASTE.TOOL',
  19588. 0x012E: 'WORKBOOK.NEW',
  19589. 0x0131: 'SCENARIO.CELLS',
  19590. 0x0132: 'SCENARIO.DELETE',
  19591. 0x0133: 'SCENARIO.ADD',
  19592. 0x0134: 'SCENARIO.EDIT',
  19593. 0x0135: 'SCENARIO.SHOW',
  19594. 0x0136: 'SCENARIO.SHOW.NEXT',
  19595. 0x0137: 'SCENARIO.SUMMARY',
  19596. 0x0138: 'PIVOT.TABLE.WIZARD',
  19597. 0x0139: 'PIVOT.FIELD.PROPERTIES',
  19598. 0x013A: 'PIVOT.FIELD',
  19599. 0x013B: 'PIVOT.ITEM',
  19600. 0x013C: 'PIVOT.ADD.FIELDS',
  19601. 0x013E: 'OPTIONS.CALCULATION',
  19602. 0x013F: 'OPTIONS.EDIT',
  19603. 0x0140: 'OPTIONS.VIEW',
  19604. 0x0141: 'ADDIN.MANAGER',
  19605. 0x0142: 'MENU.EDITOR',
  19606. 0x0143: 'ATTACH.TOOLBARS',
  19607. 0x0144: 'VBAActivate',
  19608. 0x0145: 'OPTIONS.CHART',
  19609. 0x0148: 'VBA.INSERT.FILE',
  19610. 0x014A: 'VBA.PROCEDURE.DEFINITION',
  19611. 0x0150: 'ROUTING.SLIP',
  19612. 0x0152: 'ROUTE.DOCUMENT',
  19613. 0x0153: 'MAIL.LOGON',
  19614. 0x0156: 'INSERT.PICTURE',
  19615. 0x0157: 'EDIT.TOOL',
  19616. 0x0158: 'GALLERY.DOUGHNUT',
  19617. 0x015E: 'CHART.TREND',
  19618. 0x0160: 'PIVOT.ITEM.PROPERTIES',
  19619. 0x0162: 'WORKBOOK.INSERT',
  19620. 0x0163: 'OPTIONS.TRANSITION',
  19621. 0x0164: 'OPTIONS.GENERAL',
  19622. 0x0172: 'FILTER.ADVANCED',
  19623. 0x0175: 'MAIL.ADD.MAILER',
  19624. 0x0176: 'MAIL.DELETE.MAILER',
  19625. 0x0177: 'MAIL.REPLY',
  19626. 0x0178: 'MAIL.REPLY.ALL',
  19627. 0x0179: 'MAIL.FORWARD',
  19628. 0x017A: 'MAIL.NEXT.LETTER',
  19629. 0x017B: 'DATA.LABEL',
  19630. 0x017C: 'INSERT.TITLE',
  19631. 0x017D: 'FONT.PROPERTIES',
  19632. 0x017E: 'MACRO.OPTIONS',
  19633. 0x017F: 'WORKBOOK.HIDE',
  19634. 0x0180: 'WORKBOOK.UNHIDE',
  19635. 0x0181: 'WORKBOOK.DELETE',
  19636. 0x0182: 'WORKBOOK.NAME',
  19637. 0x0184: 'GALLERY.CUSTOM',
  19638. 0x0186: 'ADD.CHART.AUTOFORMAT',
  19639. 0x0187: 'DELETE.CHART.AUTOFORMAT',
  19640. 0x0188: 'CHART.ADD.DATA',
  19641. 0x0189: 'AUTO.OUTLINE',
  19642. 0x018A: 'TAB.ORDER',
  19643. 0x018B: 'SHOW.DIALOG',
  19644. 0x018C: 'SELECT.ALL',
  19645. 0x018D: 'UNGROUP.SHEETS',
  19646. 0x018E: 'SUBTOTAL.CREATE',
  19647. 0x018F: 'SUBTOTAL.REMOVE',
  19648. 0x0190: 'RENAME.OBJECT',
  19649. 0x019C: 'WORKBOOK.SCROLL',
  19650. 0x019D: 'WORKBOOK.NEXT',
  19651. 0x019E: 'WORKBOOK.PREV',
  19652. 0x019F: 'WORKBOOK.TAB.SPLIT',
  19653. 0x01A0: 'FULL.SCREEN',
  19654. 0x01A1: 'WORKBOOK.PROTECT',
  19655. 0x01A4: 'SCROLLBAR.PROPERTIES',
  19656. 0x01A5: 'PIVOT.SHOW.PAGES',
  19657. 0x01A6: 'TEXT.TO.COLUMNS',
  19658. 0x01A7: 'FORMAT.CHARTTYPE',
  19659. 0x01A8: 'LINK.FORMAT',
  19660. 0x01A9: 'TRACER.DISPLAY',
  19661. 0x01AE: 'TRACER.NAVIGATE',
  19662. 0x01AF: 'TRACER.CLEAR',
  19663. 0x01B0: 'TRACER.ERROR',
  19664. 0x01B1: 'PIVOT.FIELD.GROUP',
  19665. 0x01B2: 'PIVOT.FIELD.UNGROUP',
  19666. 0x01B3: 'CHECKBOX.PROPERTIES',
  19667. 0x01B4: 'LABEL.PROPERTIES',
  19668. 0x01B5: 'LISTBOX.PROPERTIES',
  19669. 0x01B6: 'EDITBOX.PROPERTIES',
  19670. 0x01B7: 'PIVOT.REFRESH',
  19671. 0x01B8: 'LINK.COMBO',
  19672. 0x01B9: 'OPEN.TEXT',
  19673. 0x01BA: 'HIDE.DIALOG',
  19674. 0x01BB: 'SET.DIALOG.FOCUS',
  19675. 0x01BC: 'ENABLE.OBJECT',
  19676. 0x01BD: 'PUSHBUTTON.PROPERTIES',
  19677. 0x01BE: 'SET.DIALOG.DEFAULT',
  19678. 0x01BF: 'FILTER',
  19679. 0x01C0: 'FILTER.SHOW.ALL',
  19680. 0x01C1: 'CLEAR.OUTLINE',
  19681. 0x01C2: 'FUNCTION.WIZARD',
  19682. 0x01C3: 'ADD.LIST.ITEM',
  19683. 0x01C4: 'SET.LIST.ITEM',
  19684. 0x01C5: 'REMOVE.LIST.ITEM',
  19685. 0x01C6: 'SELECT.LIST.ITEM',
  19686. 0x01C7: 'SET.CONTROL.VALUE',
  19687. 0x01C8: 'SAVE.COPY.AS',
  19688. 0x01CA: 'OPTIONS.LISTS.ADD',
  19689. 0x01CB: 'OPTIONS.LISTS.DELETE',
  19690. 0x01CC: 'SERIES.AXES',
  19691. 0x01CD: 'SERIES.X',
  19692. 0x01CE: 'SERIES.Y',
  19693. 0x01CF: 'ERRORBAR.X',
  19694. 0x01D0: 'ERRORBAR.Y',
  19695. 0x01D1: 'FORMAT.CHART',
  19696. 0x01D2: 'SERIES.ORDER',
  19697. 0x01D3: 'MAIL.LOGOFF',
  19698. 0x01D4: 'CLEAR.ROUTING.SLIP',
  19699. 0x01D5: 'APP.ACTIVATE.MICROSOFT',
  19700. 0x01D6: 'MAIL.EDIT.MAILER',
  19701. 0x01D7: 'ON.SHEET',
  19702. 0x01D8: 'STANDARD.WIDTH',
  19703. 0x01D9: 'SCENARIO.MERGE',
  19704. 0x01DA: 'SUMMARY.INFO',
  19705. 0x01DB: 'FIND.FILE',
  19706. 0x01DC: 'ACTIVE.CELL.FONT',
  19707. 0x01DD: 'ENABLE.TIPWIZARD',
  19708. 0x01DE: 'VBA.MAKE.ADDIN',
  19709. 0x01E0: 'INSERTDATATABLE',
  19710. 0x01E1: 'WORKGROUP.OPTIONS',
  19711. 0x01E2: 'MAIL.SEND.MAILER',
  19712. 0x01E5: 'AUTOCORRECT',
  19713. 0x01E9: 'POST.DOCUMENT',
  19714. 0x01EB: 'PICKLIST',
  19715. 0x01ED: 'VIEW.SHOW',
  19716. 0x01EE: 'VIEW.DEFINE',
  19717. 0x01EF: 'VIEW.DELETE',
  19718. 0x01FD: 'SHEET.BACKGROUND',
  19719. 0x01FE: 'INSERT.MAP.OBJECT',
  19720. 0x01FF: 'OPTIONS.MENONO',
  19721. 0x0205: 'MSOCHECKS',
  19722. 0x0206: 'NORMAL',
  19723. 0x0207: 'LAYOUT',
  19724. 0x0208: 'RM.PRINT.AREA',
  19725. 0x0209: 'CLEAR.PRINT.AREA',
  19726. 0x020A: 'ADD.PRINT.AREA',
  19727. 0x020B: 'MOVE.BRK',
  19728. 0x0221: 'HIDECURR.NOTE',
  19729. 0x0222: 'HIDEALL.NOTES',
  19730. 0x0223: 'DELETE.NOTE',
  19731. 0x0224: 'TRAVERSE.NOTES',
  19732. 0x0225: 'ACTIVATE.NOTES',
  19733. 0x026C: 'PROTECT.REVISIONS',
  19734. 0x026D: 'UNPROTECT.REVISIONS',
  19735. 0x0287: 'OPTIONS.ME',
  19736. 0x028D: 'WEB.PUBLISH',
  19737. 0x029B: 'NEWWEBQUERY',
  19738. 0x02A1: 'PIVOT.TABLE.CHART',
  19739. 0x02F1: 'OPTIONS.SAVE',
  19740. 0x02F3: 'OPTIONS.SPELL',
  19741. 0x0328: 'HIDEALL.INKANNOTS'
  19742. };
  19743. /* [MS-XLS] 2.5.198.17 */
  19744. /* [MS-XLSB] 2.5.97.10 */
  19745. var Ftab = {
  19746. 0x0000: 'COUNT',
  19747. 0x0001: 'IF',
  19748. 0x0002: 'ISNA',
  19749. 0x0003: 'ISERROR',
  19750. 0x0004: 'SUM',
  19751. 0x0005: 'AVERAGE',
  19752. 0x0006: 'MIN',
  19753. 0x0007: 'MAX',
  19754. 0x0008: 'ROW',
  19755. 0x0009: 'COLUMN',
  19756. 0x000A: 'NA',
  19757. 0x000B: 'NPV',
  19758. 0x000C: 'STDEV',
  19759. 0x000D: 'DOLLAR',
  19760. 0x000E: 'FIXED',
  19761. 0x000F: 'SIN',
  19762. 0x0010: 'COS',
  19763. 0x0011: 'TAN',
  19764. 0x0012: 'ATAN',
  19765. 0x0013: 'PI',
  19766. 0x0014: 'SQRT',
  19767. 0x0015: 'EXP',
  19768. 0x0016: 'LN',
  19769. 0x0017: 'LOG10',
  19770. 0x0018: 'ABS',
  19771. 0x0019: 'INT',
  19772. 0x001A: 'SIGN',
  19773. 0x001B: 'ROUND',
  19774. 0x001C: 'LOOKUP',
  19775. 0x001D: 'INDEX',
  19776. 0x001E: 'REPT',
  19777. 0x001F: 'MID',
  19778. 0x0020: 'LEN',
  19779. 0x0021: 'VALUE',
  19780. 0x0022: 'TRUE',
  19781. 0x0023: 'FALSE',
  19782. 0x0024: 'AND',
  19783. 0x0025: 'OR',
  19784. 0x0026: 'NOT',
  19785. 0x0027: 'MOD',
  19786. 0x0028: 'DCOUNT',
  19787. 0x0029: 'DSUM',
  19788. 0x002A: 'DAVERAGE',
  19789. 0x002B: 'DMIN',
  19790. 0x002C: 'DMAX',
  19791. 0x002D: 'DSTDEV',
  19792. 0x002E: 'VAR',
  19793. 0x002F: 'DVAR',
  19794. 0x0030: 'TEXT',
  19795. 0x0031: 'LINEST',
  19796. 0x0032: 'TREND',
  19797. 0x0033: 'LOGEST',
  19798. 0x0034: 'GROWTH',
  19799. 0x0035: 'GOTO',
  19800. 0x0036: 'HALT',
  19801. 0x0037: 'RETURN',
  19802. 0x0038: 'PV',
  19803. 0x0039: 'FV',
  19804. 0x003A: 'NPER',
  19805. 0x003B: 'PMT',
  19806. 0x003C: 'RATE',
  19807. 0x003D: 'MIRR',
  19808. 0x003E: 'IRR',
  19809. 0x003F: 'RAND',
  19810. 0x0040: 'MATCH',
  19811. 0x0041: 'DATE',
  19812. 0x0042: 'TIME',
  19813. 0x0043: 'DAY',
  19814. 0x0044: 'MONTH',
  19815. 0x0045: 'YEAR',
  19816. 0x0046: 'WEEKDAY',
  19817. 0x0047: 'HOUR',
  19818. 0x0048: 'MINUTE',
  19819. 0x0049: 'SECOND',
  19820. 0x004A: 'NOW',
  19821. 0x004B: 'AREAS',
  19822. 0x004C: 'ROWS',
  19823. 0x004D: 'COLUMNS',
  19824. 0x004E: 'OFFSET',
  19825. 0x004F: 'ABSREF',
  19826. 0x0050: 'RELREF',
  19827. 0x0051: 'ARGUMENT',
  19828. 0x0052: 'SEARCH',
  19829. 0x0053: 'TRANSPOSE',
  19830. 0x0054: 'ERROR',
  19831. 0x0055: 'STEP',
  19832. 0x0056: 'TYPE',
  19833. 0x0057: 'ECHO',
  19834. 0x0058: 'SET.NAME',
  19835. 0x0059: 'CALLER',
  19836. 0x005A: 'DEREF',
  19837. 0x005B: 'WINDOWS',
  19838. 0x005C: 'SERIES',
  19839. 0x005D: 'DOCUMENTS',
  19840. 0x005E: 'ACTIVE.CELL',
  19841. 0x005F: 'SELECTION',
  19842. 0x0060: 'RESULT',
  19843. 0x0061: 'ATAN2',
  19844. 0x0062: 'ASIN',
  19845. 0x0063: 'ACOS',
  19846. 0x0064: 'CHOOSE',
  19847. 0x0065: 'HLOOKUP',
  19848. 0x0066: 'VLOOKUP',
  19849. 0x0067: 'LINKS',
  19850. 0x0068: 'INPUT',
  19851. 0x0069: 'ISREF',
  19852. 0x006A: 'GET.FORMULA',
  19853. 0x006B: 'GET.NAME',
  19854. 0x006C: 'SET.VALUE',
  19855. 0x006D: 'LOG',
  19856. 0x006E: 'EXEC',
  19857. 0x006F: 'CHAR',
  19858. 0x0070: 'LOWER',
  19859. 0x0071: 'UPPER',
  19860. 0x0072: 'PROPER',
  19861. 0x0073: 'LEFT',
  19862. 0x0074: 'RIGHT',
  19863. 0x0075: 'EXACT',
  19864. 0x0076: 'TRIM',
  19865. 0x0077: 'REPLACE',
  19866. 0x0078: 'SUBSTITUTE',
  19867. 0x0079: 'CODE',
  19868. 0x007A: 'NAMES',
  19869. 0x007B: 'DIRECTORY',
  19870. 0x007C: 'FIND',
  19871. 0x007D: 'CELL',
  19872. 0x007E: 'ISERR',
  19873. 0x007F: 'ISTEXT',
  19874. 0x0080: 'ISNUMBER',
  19875. 0x0081: 'ISBLANK',
  19876. 0x0082: 'T',
  19877. 0x0083: 'N',
  19878. 0x0084: 'FOPEN',
  19879. 0x0085: 'FCLOSE',
  19880. 0x0086: 'FSIZE',
  19881. 0x0087: 'FREADLN',
  19882. 0x0088: 'FREAD',
  19883. 0x0089: 'FWRITELN',
  19884. 0x008A: 'FWRITE',
  19885. 0x008B: 'FPOS',
  19886. 0x008C: 'DATEVALUE',
  19887. 0x008D: 'TIMEVALUE',
  19888. 0x008E: 'SLN',
  19889. 0x008F: 'SYD',
  19890. 0x0090: 'DDB',
  19891. 0x0091: 'GET.DEF',
  19892. 0x0092: 'REFTEXT',
  19893. 0x0093: 'TEXTREF',
  19894. 0x0094: 'INDIRECT',
  19895. 0x0095: 'REGISTER',
  19896. 0x0096: 'CALL',
  19897. 0x0097: 'ADD.BAR',
  19898. 0x0098: 'ADD.MENU',
  19899. 0x0099: 'ADD.COMMAND',
  19900. 0x009A: 'ENABLE.COMMAND',
  19901. 0x009B: 'CHECK.COMMAND',
  19902. 0x009C: 'RENAME.COMMAND',
  19903. 0x009D: 'SHOW.BAR',
  19904. 0x009E: 'DELETE.MENU',
  19905. 0x009F: 'DELETE.COMMAND',
  19906. 0x00A0: 'GET.CHART.ITEM',
  19907. 0x00A1: 'DIALOG.BOX',
  19908. 0x00A2: 'CLEAN',
  19909. 0x00A3: 'MDETERM',
  19910. 0x00A4: 'MINVERSE',
  19911. 0x00A5: 'MMULT',
  19912. 0x00A6: 'FILES',
  19913. 0x00A7: 'IPMT',
  19914. 0x00A8: 'PPMT',
  19915. 0x00A9: 'COUNTA',
  19916. 0x00AA: 'CANCEL.KEY',
  19917. 0x00AB: 'FOR',
  19918. 0x00AC: 'WHILE',
  19919. 0x00AD: 'BREAK',
  19920. 0x00AE: 'NEXT',
  19921. 0x00AF: 'INITIATE',
  19922. 0x00B0: 'REQUEST',
  19923. 0x00B1: 'POKE',
  19924. 0x00B2: 'EXECUTE',
  19925. 0x00B3: 'TERMINATE',
  19926. 0x00B4: 'RESTART',
  19927. 0x00B5: 'HELP',
  19928. 0x00B6: 'GET.BAR',
  19929. 0x00B7: 'PRODUCT',
  19930. 0x00B8: 'FACT',
  19931. 0x00B9: 'GET.CELL',
  19932. 0x00BA: 'GET.WORKSPACE',
  19933. 0x00BB: 'GET.WINDOW',
  19934. 0x00BC: 'GET.DOCUMENT',
  19935. 0x00BD: 'DPRODUCT',
  19936. 0x00BE: 'ISNONTEXT',
  19937. 0x00BF: 'GET.NOTE',
  19938. 0x00C0: 'NOTE',
  19939. 0x00C1: 'STDEVP',
  19940. 0x00C2: 'VARP',
  19941. 0x00C3: 'DSTDEVP',
  19942. 0x00C4: 'DVARP',
  19943. 0x00C5: 'TRUNC',
  19944. 0x00C6: 'ISLOGICAL',
  19945. 0x00C7: 'DCOUNTA',
  19946. 0x00C8: 'DELETE.BAR',
  19947. 0x00C9: 'UNREGISTER',
  19948. 0x00CC: 'USDOLLAR',
  19949. 0x00CD: 'FINDB',
  19950. 0x00CE: 'SEARCHB',
  19951. 0x00CF: 'REPLACEB',
  19952. 0x00D0: 'LEFTB',
  19953. 0x00D1: 'RIGHTB',
  19954. 0x00D2: 'MIDB',
  19955. 0x00D3: 'LENB',
  19956. 0x00D4: 'ROUNDUP',
  19957. 0x00D5: 'ROUNDDOWN',
  19958. 0x00D6: 'ASC',
  19959. 0x00D7: 'DBCS',
  19960. 0x00D8: 'RANK',
  19961. 0x00DB: 'ADDRESS',
  19962. 0x00DC: 'DAYS360',
  19963. 0x00DD: 'TODAY',
  19964. 0x00DE: 'VDB',
  19965. 0x00DF: 'ELSE',
  19966. 0x00E0: 'ELSE.IF',
  19967. 0x00E1: 'END.IF',
  19968. 0x00E2: 'FOR.CELL',
  19969. 0x00E3: 'MEDIAN',
  19970. 0x00E4: 'SUMPRODUCT',
  19971. 0x00E5: 'SINH',
  19972. 0x00E6: 'COSH',
  19973. 0x00E7: 'TANH',
  19974. 0x00E8: 'ASINH',
  19975. 0x00E9: 'ACOSH',
  19976. 0x00EA: 'ATANH',
  19977. 0x00EB: 'DGET',
  19978. 0x00EC: 'CREATE.OBJECT',
  19979. 0x00ED: 'VOLATILE',
  19980. 0x00EE: 'LAST.ERROR',
  19981. 0x00EF: 'CUSTOM.UNDO',
  19982. 0x00F0: 'CUSTOM.REPEAT',
  19983. 0x00F1: 'FORMULA.CONVERT',
  19984. 0x00F2: 'GET.LINK.INFO',
  19985. 0x00F3: 'TEXT.BOX',
  19986. 0x00F4: 'INFO',
  19987. 0x00F5: 'GROUP',
  19988. 0x00F6: 'GET.OBJECT',
  19989. 0x00F7: 'DB',
  19990. 0x00F8: 'PAUSE',
  19991. 0x00FB: 'RESUME',
  19992. 0x00FC: 'FREQUENCY',
  19993. 0x00FD: 'ADD.TOOLBAR',
  19994. 0x00FE: 'DELETE.TOOLBAR',
  19995. 0x00FF: 'User',
  19996. 0x0100: 'RESET.TOOLBAR',
  19997. 0x0101: 'EVALUATE',
  19998. 0x0102: 'GET.TOOLBAR',
  19999. 0x0103: 'GET.TOOL',
  20000. 0x0104: 'SPELLING.CHECK',
  20001. 0x0105: 'ERROR.TYPE',
  20002. 0x0106: 'APP.TITLE',
  20003. 0x0107: 'WINDOW.TITLE',
  20004. 0x0108: 'SAVE.TOOLBAR',
  20005. 0x0109: 'ENABLE.TOOL',
  20006. 0x010A: 'PRESS.TOOL',
  20007. 0x010B: 'REGISTER.ID',
  20008. 0x010C: 'GET.WORKBOOK',
  20009. 0x010D: 'AVEDEV',
  20010. 0x010E: 'BETADIST',
  20011. 0x010F: 'GAMMALN',
  20012. 0x0110: 'BETAINV',
  20013. 0x0111: 'BINOMDIST',
  20014. 0x0112: 'CHIDIST',
  20015. 0x0113: 'CHIINV',
  20016. 0x0114: 'COMBIN',
  20017. 0x0115: 'CONFIDENCE',
  20018. 0x0116: 'CRITBINOM',
  20019. 0x0117: 'EVEN',
  20020. 0x0118: 'EXPONDIST',
  20021. 0x0119: 'FDIST',
  20022. 0x011A: 'FINV',
  20023. 0x011B: 'FISHER',
  20024. 0x011C: 'FISHERINV',
  20025. 0x011D: 'FLOOR',
  20026. 0x011E: 'GAMMADIST',
  20027. 0x011F: 'GAMMAINV',
  20028. 0x0120: 'CEILING',
  20029. 0x0121: 'HYPGEOMDIST',
  20030. 0x0122: 'LOGNORMDIST',
  20031. 0x0123: 'LOGINV',
  20032. 0x0124: 'NEGBINOMDIST',
  20033. 0x0125: 'NORMDIST',
  20034. 0x0126: 'NORMSDIST',
  20035. 0x0127: 'NORMINV',
  20036. 0x0128: 'NORMSINV',
  20037. 0x0129: 'STANDARDIZE',
  20038. 0x012A: 'ODD',
  20039. 0x012B: 'PERMUT',
  20040. 0x012C: 'POISSON',
  20041. 0x012D: 'TDIST',
  20042. 0x012E: 'WEIBULL',
  20043. 0x012F: 'SUMXMY2',
  20044. 0x0130: 'SUMX2MY2',
  20045. 0x0131: 'SUMX2PY2',
  20046. 0x0132: 'CHITEST',
  20047. 0x0133: 'CORREL',
  20048. 0x0134: 'COVAR',
  20049. 0x0135: 'FORECAST',
  20050. 0x0136: 'FTEST',
  20051. 0x0137: 'INTERCEPT',
  20052. 0x0138: 'PEARSON',
  20053. 0x0139: 'RSQ',
  20054. 0x013A: 'STEYX',
  20055. 0x013B: 'SLOPE',
  20056. 0x013C: 'TTEST',
  20057. 0x013D: 'PROB',
  20058. 0x013E: 'DEVSQ',
  20059. 0x013F: 'GEOMEAN',
  20060. 0x0140: 'HARMEAN',
  20061. 0x0141: 'SUMSQ',
  20062. 0x0142: 'KURT',
  20063. 0x0143: 'SKEW',
  20064. 0x0144: 'ZTEST',
  20065. 0x0145: 'LARGE',
  20066. 0x0146: 'SMALL',
  20067. 0x0147: 'QUARTILE',
  20068. 0x0148: 'PERCENTILE',
  20069. 0x0149: 'PERCENTRANK',
  20070. 0x014A: 'MODE',
  20071. 0x014B: 'TRIMMEAN',
  20072. 0x014C: 'TINV',
  20073. 0x014E: 'MOVIE.COMMAND',
  20074. 0x014F: 'GET.MOVIE',
  20075. 0x0150: 'CONCATENATE',
  20076. 0x0151: 'POWER',
  20077. 0x0152: 'PIVOT.ADD.DATA',
  20078. 0x0153: 'GET.PIVOT.TABLE',
  20079. 0x0154: 'GET.PIVOT.FIELD',
  20080. 0x0155: 'GET.PIVOT.ITEM',
  20081. 0x0156: 'RADIANS',
  20082. 0x0157: 'DEGREES',
  20083. 0x0158: 'SUBTOTAL',
  20084. 0x0159: 'SUMIF',
  20085. 0x015A: 'COUNTIF',
  20086. 0x015B: 'COUNTBLANK',
  20087. 0x015C: 'SCENARIO.GET',
  20088. 0x015D: 'OPTIONS.LISTS.GET',
  20089. 0x015E: 'ISPMT',
  20090. 0x015F: 'DATEDIF',
  20091. 0x0160: 'DATESTRING',
  20092. 0x0161: 'NUMBERSTRING',
  20093. 0x0162: 'ROMAN',
  20094. 0x0163: 'OPEN.DIALOG',
  20095. 0x0164: 'SAVE.DIALOG',
  20096. 0x0165: 'VIEW.GET',
  20097. 0x0166: 'GETPIVOTDATA',
  20098. 0x0167: 'HYPERLINK',
  20099. 0x0168: 'PHONETIC',
  20100. 0x0169: 'AVERAGEA',
  20101. 0x016A: 'MAXA',
  20102. 0x016B: 'MINA',
  20103. 0x016C: 'STDEVPA',
  20104. 0x016D: 'VARPA',
  20105. 0x016E: 'STDEVA',
  20106. 0x016F: 'VARA',
  20107. 0x0170: 'BAHTTEXT',
  20108. 0x0171: 'THAIDAYOFWEEK',
  20109. 0x0172: 'THAIDIGIT',
  20110. 0x0173: 'THAIMONTHOFYEAR',
  20111. 0x0174: 'THAINUMSOUND',
  20112. 0x0175: 'THAINUMSTRING',
  20113. 0x0176: 'THAISTRINGLENGTH',
  20114. 0x0177: 'ISTHAIDIGIT',
  20115. 0x0178: 'ROUNDBAHTDOWN',
  20116. 0x0179: 'ROUNDBAHTUP',
  20117. 0x017A: 'THAIYEAR',
  20118. 0x017B: 'RTD',
  20119. 0x017C: 'CUBEVALUE',
  20120. 0x017D: 'CUBEMEMBER',
  20121. 0x017E: 'CUBEMEMBERPROPERTY',
  20122. 0x017F: 'CUBERANKEDMEMBER',
  20123. 0x0180: 'HEX2BIN',
  20124. 0x0181: 'HEX2DEC',
  20125. 0x0182: 'HEX2OCT',
  20126. 0x0183: 'DEC2BIN',
  20127. 0x0184: 'DEC2HEX',
  20128. 0x0185: 'DEC2OCT',
  20129. 0x0186: 'OCT2BIN',
  20130. 0x0187: 'OCT2HEX',
  20131. 0x0188: 'OCT2DEC',
  20132. 0x0189: 'BIN2DEC',
  20133. 0x018A: 'BIN2OCT',
  20134. 0x018B: 'BIN2HEX',
  20135. 0x018C: 'IMSUB',
  20136. 0x018D: 'IMDIV',
  20137. 0x018E: 'IMPOWER',
  20138. 0x018F: 'IMABS',
  20139. 0x0190: 'IMSQRT',
  20140. 0x0191: 'IMLN',
  20141. 0x0192: 'IMLOG2',
  20142. 0x0193: 'IMLOG10',
  20143. 0x0194: 'IMSIN',
  20144. 0x0195: 'IMCOS',
  20145. 0x0196: 'IMEXP',
  20146. 0x0197: 'IMARGUMENT',
  20147. 0x0198: 'IMCONJUGATE',
  20148. 0x0199: 'IMAGINARY',
  20149. 0x019A: 'IMREAL',
  20150. 0x019B: 'COMPLEX',
  20151. 0x019C: 'IMSUM',
  20152. 0x019D: 'IMPRODUCT',
  20153. 0x019E: 'SERIESSUM',
  20154. 0x019F: 'FACTDOUBLE',
  20155. 0x01A0: 'SQRTPI',
  20156. 0x01A1: 'QUOTIENT',
  20157. 0x01A2: 'DELTA',
  20158. 0x01A3: 'GESTEP',
  20159. 0x01A4: 'ISEVEN',
  20160. 0x01A5: 'ISODD',
  20161. 0x01A6: 'MROUND',
  20162. 0x01A7: 'ERF',
  20163. 0x01A8: 'ERFC',
  20164. 0x01A9: 'BESSELJ',
  20165. 0x01AA: 'BESSELK',
  20166. 0x01AB: 'BESSELY',
  20167. 0x01AC: 'BESSELI',
  20168. 0x01AD: 'XIRR',
  20169. 0x01AE: 'XNPV',
  20170. 0x01AF: 'PRICEMAT',
  20171. 0x01B0: 'YIELDMAT',
  20172. 0x01B1: 'INTRATE',
  20173. 0x01B2: 'RECEIVED',
  20174. 0x01B3: 'DISC',
  20175. 0x01B4: 'PRICEDISC',
  20176. 0x01B5: 'YIELDDISC',
  20177. 0x01B6: 'TBILLEQ',
  20178. 0x01B7: 'TBILLPRICE',
  20179. 0x01B8: 'TBILLYIELD',
  20180. 0x01B9: 'PRICE',
  20181. 0x01BA: 'YIELD',
  20182. 0x01BB: 'DOLLARDE',
  20183. 0x01BC: 'DOLLARFR',
  20184. 0x01BD: 'NOMINAL',
  20185. 0x01BE: 'EFFECT',
  20186. 0x01BF: 'CUMPRINC',
  20187. 0x01C0: 'CUMIPMT',
  20188. 0x01C1: 'EDATE',
  20189. 0x01C2: 'EOMONTH',
  20190. 0x01C3: 'YEARFRAC',
  20191. 0x01C4: 'COUPDAYBS',
  20192. 0x01C5: 'COUPDAYS',
  20193. 0x01C6: 'COUPDAYSNC',
  20194. 0x01C7: 'COUPNCD',
  20195. 0x01C8: 'COUPNUM',
  20196. 0x01C9: 'COUPPCD',
  20197. 0x01CA: 'DURATION',
  20198. 0x01CB: 'MDURATION',
  20199. 0x01CC: 'ODDLPRICE',
  20200. 0x01CD: 'ODDLYIELD',
  20201. 0x01CE: 'ODDFPRICE',
  20202. 0x01CF: 'ODDFYIELD',
  20203. 0x01D0: 'RANDBETWEEN',
  20204. 0x01D1: 'WEEKNUM',
  20205. 0x01D2: 'AMORDEGRC',
  20206. 0x01D3: 'AMORLINC',
  20207. 0x01D4: 'CONVERT',
  20208. 0x02D4: 'SHEETJS',
  20209. 0x01D5: 'ACCRINT',
  20210. 0x01D6: 'ACCRINTM',
  20211. 0x01D7: 'WORKDAY',
  20212. 0x01D8: 'NETWORKDAYS',
  20213. 0x01D9: 'GCD',
  20214. 0x01DA: 'MULTINOMIAL',
  20215. 0x01DB: 'LCM',
  20216. 0x01DC: 'FVSCHEDULE',
  20217. 0x01DD: 'CUBEKPIMEMBER',
  20218. 0x01DE: 'CUBESET',
  20219. 0x01DF: 'CUBESETCOUNT',
  20220. 0x01E0: 'IFERROR',
  20221. 0x01E1: 'COUNTIFS',
  20222. 0x01E2: 'SUMIFS',
  20223. 0x01E3: 'AVERAGEIF',
  20224. 0x01E4: 'AVERAGEIFS'
  20225. };
  20226. var FtabArgc = {
  20227. 0x0002: 1, /* ISNA */
  20228. 0x0003: 1, /* ISERROR */
  20229. 0x000A: 0, /* NA */
  20230. 0x000F: 1, /* SIN */
  20231. 0x0010: 1, /* COS */
  20232. 0x0011: 1, /* TAN */
  20233. 0x0012: 1, /* ATAN */
  20234. 0x0013: 0, /* PI */
  20235. 0x0014: 1, /* SQRT */
  20236. 0x0015: 1, /* EXP */
  20237. 0x0016: 1, /* LN */
  20238. 0x0017: 1, /* LOG10 */
  20239. 0x0018: 1, /* ABS */
  20240. 0x0019: 1, /* INT */
  20241. 0x001A: 1, /* SIGN */
  20242. 0x001B: 2, /* ROUND */
  20243. 0x001E: 2, /* REPT */
  20244. 0x001F: 3, /* MID */
  20245. 0x0020: 1, /* LEN */
  20246. 0x0021: 1, /* VALUE */
  20247. 0x0022: 0, /* TRUE */
  20248. 0x0023: 0, /* FALSE */
  20249. 0x0026: 1, /* NOT */
  20250. 0x0027: 2, /* MOD */
  20251. 0x0028: 3, /* DCOUNT */
  20252. 0x0029: 3, /* DSUM */
  20253. 0x002A: 3, /* DAVERAGE */
  20254. 0x002B: 3, /* DMIN */
  20255. 0x002C: 3, /* DMAX */
  20256. 0x002D: 3, /* DSTDEV */
  20257. 0x002F: 3, /* DVAR */
  20258. 0x0030: 2, /* TEXT */
  20259. 0x0035: 1, /* GOTO */
  20260. 0x003D: 3, /* MIRR */
  20261. 0x003F: 0, /* RAND */
  20262. 0x0041: 3, /* DATE */
  20263. 0x0042: 3, /* TIME */
  20264. 0x0043: 1, /* DAY */
  20265. 0x0044: 1, /* MONTH */
  20266. 0x0045: 1, /* YEAR */
  20267. 0x0046: 1, /* WEEKDAY */
  20268. 0x0047: 1, /* HOUR */
  20269. 0x0048: 1, /* MINUTE */
  20270. 0x0049: 1, /* SECOND */
  20271. 0x004A: 0, /* NOW */
  20272. 0x004B: 1, /* AREAS */
  20273. 0x004C: 1, /* ROWS */
  20274. 0x004D: 1, /* COLUMNS */
  20275. 0x004F: 2, /* ABSREF */
  20276. 0x0050: 2, /* RELREF */
  20277. 0x0053: 1, /* TRANSPOSE */
  20278. 0x0055: 0, /* STEP */
  20279. 0x0056: 1, /* TYPE */
  20280. 0x0059: 0, /* CALLER */
  20281. 0x005A: 1, /* DEREF */
  20282. 0x005E: 0, /* ACTIVE.CELL */
  20283. 0x005F: 0, /* SELECTION */
  20284. 0x0061: 2, /* ATAN2 */
  20285. 0x0062: 1, /* ASIN */
  20286. 0x0063: 1, /* ACOS */
  20287. 0x0065: 3, /* HLOOKUP */
  20288. 0x0066: 3, /* VLOOKUP */
  20289. 0x0069: 1, /* ISREF */
  20290. 0x006A: 1, /* GET.FORMULA */
  20291. 0x006C: 2, /* SET.VALUE */
  20292. 0x006F: 1, /* CHAR */
  20293. 0x0070: 1, /* LOWER */
  20294. 0x0071: 1, /* UPPER */
  20295. 0x0072: 1, /* PROPER */
  20296. 0x0075: 2, /* EXACT */
  20297. 0x0076: 1, /* TRIM */
  20298. 0x0077: 4, /* REPLACE */
  20299. 0x0079: 1, /* CODE */
  20300. 0x007E: 1, /* ISERR */
  20301. 0x007F: 1, /* ISTEXT */
  20302. 0x0080: 1, /* ISNUMBER */
  20303. 0x0081: 1, /* ISBLANK */
  20304. 0x0082: 1, /* T */
  20305. 0x0083: 1, /* N */
  20306. 0x0085: 1, /* FCLOSE */
  20307. 0x0086: 1, /* FSIZE */
  20308. 0x0087: 1, /* FREADLN */
  20309. 0x0088: 2, /* FREAD */
  20310. 0x0089: 2, /* FWRITELN */
  20311. 0x008A: 2, /* FWRITE */
  20312. 0x008C: 1, /* DATEVALUE */
  20313. 0x008D: 1, /* TIMEVALUE */
  20314. 0x008E: 3, /* SLN */
  20315. 0x008F: 4, /* SYD */
  20316. 0x0090: 4, /* DDB */
  20317. 0x00A1: 1, /* DIALOG.BOX */
  20318. 0x00A2: 1, /* CLEAN */
  20319. 0x00A3: 1, /* MDETERM */
  20320. 0x00A4: 1, /* MINVERSE */
  20321. 0x00A5: 2, /* MMULT */
  20322. 0x00AC: 1, /* WHILE */
  20323. 0x00AF: 2, /* INITIATE */
  20324. 0x00B0: 2, /* REQUEST */
  20325. 0x00B1: 3, /* POKE */
  20326. 0x00B2: 2, /* EXECUTE */
  20327. 0x00B3: 1, /* TERMINATE */
  20328. 0x00B8: 1, /* FACT */
  20329. 0x00BA: 1, /* GET.WORKSPACE */
  20330. 0x00BD: 3, /* DPRODUCT */
  20331. 0x00BE: 1, /* ISNONTEXT */
  20332. 0x00C3: 3, /* DSTDEVP */
  20333. 0x00C4: 3, /* DVARP */
  20334. 0x00C5: 1, /* TRUNC */
  20335. 0x00C6: 1, /* ISLOGICAL */
  20336. 0x00C7: 3, /* DCOUNTA */
  20337. 0x00C9: 1, /* UNREGISTER */
  20338. 0x00CF: 4, /* REPLACEB */
  20339. 0x00D2: 3, /* MIDB */
  20340. 0x00D3: 1, /* LENB */
  20341. 0x00D4: 2, /* ROUNDUP */
  20342. 0x00D5: 2, /* ROUNDDOWN */
  20343. 0x00D6: 1, /* ASC */
  20344. 0x00D7: 1, /* DBCS */
  20345. 0x00E1: 0, /* END.IF */
  20346. 0x00E5: 1, /* SINH */
  20347. 0x00E6: 1, /* COSH */
  20348. 0x00E7: 1, /* TANH */
  20349. 0x00E8: 1, /* ASINH */
  20350. 0x00E9: 1, /* ACOSH */
  20351. 0x00EA: 1, /* ATANH */
  20352. 0x00EB: 3, /* DGET */
  20353. 0x00F4: 1, /* INFO */
  20354. 0x00F7: 4, /* DB */
  20355. 0x00FC: 2, /* FREQUENCY */
  20356. 0x0101: 1, /* EVALUATE */
  20357. 0x0105: 1, /* ERROR.TYPE */
  20358. 0x010F: 1, /* GAMMALN */
  20359. 0x0111: 4, /* BINOMDIST */
  20360. 0x0112: 2, /* CHIDIST */
  20361. 0x0113: 2, /* CHIINV */
  20362. 0x0114: 2, /* COMBIN */
  20363. 0x0115: 3, /* CONFIDENCE */
  20364. 0x0116: 3, /* CRITBINOM */
  20365. 0x0117: 1, /* EVEN */
  20366. 0x0118: 3, /* EXPONDIST */
  20367. 0x0119: 3, /* FDIST */
  20368. 0x011A: 3, /* FINV */
  20369. 0x011B: 1, /* FISHER */
  20370. 0x011C: 1, /* FISHERINV */
  20371. 0x011D: 2, /* FLOOR */
  20372. 0x011E: 4, /* GAMMADIST */
  20373. 0x011F: 3, /* GAMMAINV */
  20374. 0x0120: 2, /* CEILING */
  20375. 0x0121: 4, /* HYPGEOMDIST */
  20376. 0x0122: 3, /* LOGNORMDIST */
  20377. 0x0123: 3, /* LOGINV */
  20378. 0x0124: 3, /* NEGBINOMDIST */
  20379. 0x0125: 4, /* NORMDIST */
  20380. 0x0126: 1, /* NORMSDIST */
  20381. 0x0127: 3, /* NORMINV */
  20382. 0x0128: 1, /* NORMSINV */
  20383. 0x0129: 3, /* STANDARDIZE */
  20384. 0x012A: 1, /* ODD */
  20385. 0x012B: 2, /* PERMUT */
  20386. 0x012C: 3, /* POISSON */
  20387. 0x012D: 3, /* TDIST */
  20388. 0x012E: 4, /* WEIBULL */
  20389. 0x012F: 2, /* SUMXMY2 */
  20390. 0x0130: 2, /* SUMX2MY2 */
  20391. 0x0131: 2, /* SUMX2PY2 */
  20392. 0x0132: 2, /* CHITEST */
  20393. 0x0133: 2, /* CORREL */
  20394. 0x0134: 2, /* COVAR */
  20395. 0x0135: 3, /* FORECAST */
  20396. 0x0136: 2, /* FTEST */
  20397. 0x0137: 2, /* INTERCEPT */
  20398. 0x0138: 2, /* PEARSON */
  20399. 0x0139: 2, /* RSQ */
  20400. 0x013A: 2, /* STEYX */
  20401. 0x013B: 2, /* SLOPE */
  20402. 0x013C: 4, /* TTEST */
  20403. 0x0145: 2, /* LARGE */
  20404. 0x0146: 2, /* SMALL */
  20405. 0x0147: 2, /* QUARTILE */
  20406. 0x0148: 2, /* PERCENTILE */
  20407. 0x014B: 2, /* TRIMMEAN */
  20408. 0x014C: 2, /* TINV */
  20409. 0x0151: 2, /* POWER */
  20410. 0x0156: 1, /* RADIANS */
  20411. 0x0157: 1, /* DEGREES */
  20412. 0x015A: 2, /* COUNTIF */
  20413. 0x015B: 1, /* COUNTBLANK */
  20414. 0x015E: 4, /* ISPMT */
  20415. 0x015F: 3, /* DATEDIF */
  20416. 0x0160: 1, /* DATESTRING */
  20417. 0x0161: 2, /* NUMBERSTRING */
  20418. 0x0168: 1, /* PHONETIC */
  20419. 0x0170: 1, /* BAHTTEXT */
  20420. 0x0171: 1, /* THAIDAYOFWEEK */
  20421. 0x0172: 1, /* THAIDIGIT */
  20422. 0x0173: 1, /* THAIMONTHOFYEAR */
  20423. 0x0174: 1, /* THAINUMSOUND */
  20424. 0x0175: 1, /* THAINUMSTRING */
  20425. 0x0176: 1, /* THAISTRINGLENGTH */
  20426. 0x0177: 1, /* ISTHAIDIGIT */
  20427. 0x0178: 1, /* ROUNDBAHTDOWN */
  20428. 0x0179: 1, /* ROUNDBAHTUP */
  20429. 0x017A: 1, /* THAIYEAR */
  20430. 0x017E: 3, /* CUBEMEMBERPROPERTY */
  20431. 0x0181: 1, /* HEX2DEC */
  20432. 0x0188: 1, /* OCT2DEC */
  20433. 0x0189: 1, /* BIN2DEC */
  20434. 0x018C: 2, /* IMSUB */
  20435. 0x018D: 2, /* IMDIV */
  20436. 0x018E: 2, /* IMPOWER */
  20437. 0x018F: 1, /* IMABS */
  20438. 0x0190: 1, /* IMSQRT */
  20439. 0x0191: 1, /* IMLN */
  20440. 0x0192: 1, /* IMLOG2 */
  20441. 0x0193: 1, /* IMLOG10 */
  20442. 0x0194: 1, /* IMSIN */
  20443. 0x0195: 1, /* IMCOS */
  20444. 0x0196: 1, /* IMEXP */
  20445. 0x0197: 1, /* IMARGUMENT */
  20446. 0x0198: 1, /* IMCONJUGATE */
  20447. 0x0199: 1, /* IMAGINARY */
  20448. 0x019A: 1, /* IMREAL */
  20449. 0x019E: 4, /* SERIESSUM */
  20450. 0x019F: 1, /* FACTDOUBLE */
  20451. 0x01A0: 1, /* SQRTPI */
  20452. 0x01A1: 2, /* QUOTIENT */
  20453. 0x01A4: 1, /* ISEVEN */
  20454. 0x01A5: 1, /* ISODD */
  20455. 0x01A6: 2, /* MROUND */
  20456. 0x01A8: 1, /* ERFC */
  20457. 0x01A9: 2, /* BESSELJ */
  20458. 0x01AA: 2, /* BESSELK */
  20459. 0x01AB: 2, /* BESSELY */
  20460. 0x01AC: 2, /* BESSELI */
  20461. 0x01AE: 3, /* XNPV */
  20462. 0x01B6: 3, /* TBILLEQ */
  20463. 0x01B7: 3, /* TBILLPRICE */
  20464. 0x01B8: 3, /* TBILLYIELD */
  20465. 0x01BB: 2, /* DOLLARDE */
  20466. 0x01BC: 2, /* DOLLARFR */
  20467. 0x01BD: 2, /* NOMINAL */
  20468. 0x01BE: 2, /* EFFECT */
  20469. 0x01BF: 6, /* CUMPRINC */
  20470. 0x01C0: 6, /* CUMIPMT */
  20471. 0x01C1: 2, /* EDATE */
  20472. 0x01C2: 2, /* EOMONTH */
  20473. 0x01D0: 2, /* RANDBETWEEN */
  20474. 0x01D4: 3, /* CONVERT */
  20475. 0x01DC: 2, /* FVSCHEDULE */
  20476. 0x01DF: 1, /* CUBESETCOUNT */
  20477. 0x01E0: 2, /* IFERROR */
  20478. 0xFFFF: 0
  20479. };
  20480. /* [MS-XLSX] 2.2.3 Functions */
  20481. /* [MS-XLSB] 2.5.97.10 Ftab */
  20482. var XLSXFutureFunctions = {
  20483. "_xlfn.ACOT": "ACOT",
  20484. "_xlfn.ACOTH": "ACOTH",
  20485. "_xlfn.AGGREGATE": "AGGREGATE",
  20486. "_xlfn.ARABIC": "ARABIC",
  20487. "_xlfn.AVERAGEIF": "AVERAGEIF",
  20488. "_xlfn.AVERAGEIFS": "AVERAGEIFS",
  20489. "_xlfn.BASE": "BASE",
  20490. "_xlfn.BETA.DIST": "BETA.DIST",
  20491. "_xlfn.BETA.INV": "BETA.INV",
  20492. "_xlfn.BINOM.DIST": "BINOM.DIST",
  20493. "_xlfn.BINOM.DIST.RANGE": "BINOM.DIST.RANGE",
  20494. "_xlfn.BINOM.INV": "BINOM.INV",
  20495. "_xlfn.BITAND": "BITAND",
  20496. "_xlfn.BITLSHIFT": "BITLSHIFT",
  20497. "_xlfn.BITOR": "BITOR",
  20498. "_xlfn.BITRSHIFT": "BITRSHIFT",
  20499. "_xlfn.BITXOR": "BITXOR",
  20500. "_xlfn.CEILING.MATH": "CEILING.MATH",
  20501. "_xlfn.CEILING.PRECISE": "CEILING.PRECISE",
  20502. "_xlfn.CHISQ.DIST": "CHISQ.DIST",
  20503. "_xlfn.CHISQ.DIST.RT": "CHISQ.DIST.RT",
  20504. "_xlfn.CHISQ.INV": "CHISQ.INV",
  20505. "_xlfn.CHISQ.INV.RT": "CHISQ.INV.RT",
  20506. "_xlfn.CHISQ.TEST": "CHISQ.TEST",
  20507. "_xlfn.COMBINA": "COMBINA",
  20508. "_xlfn.CONCAT": "CONCAT",
  20509. "_xlfn.CONFIDENCE.NORM": "CONFIDENCE.NORM",
  20510. "_xlfn.CONFIDENCE.T": "CONFIDENCE.T",
  20511. "_xlfn.COT": "COT",
  20512. "_xlfn.COTH": "COTH",
  20513. "_xlfn.COUNTIFS": "COUNTIFS",
  20514. "_xlfn.COVARIANCE.P": "COVARIANCE.P",
  20515. "_xlfn.COVARIANCE.S": "COVARIANCE.S",
  20516. "_xlfn.CSC": "CSC",
  20517. "_xlfn.CSCH": "CSCH",
  20518. "_xlfn.DAYS": "DAYS",
  20519. "_xlfn.DECIMAL": "DECIMAL",
  20520. "_xlfn.ECMA.CEILING": "ECMA.CEILING",
  20521. "_xlfn.ERF.PRECISE": "ERF.PRECISE",
  20522. "_xlfn.ERFC.PRECISE": "ERFC.PRECISE",
  20523. "_xlfn.EXPON.DIST": "EXPON.DIST",
  20524. "_xlfn.F.DIST": "F.DIST",
  20525. "_xlfn.F.DIST.RT": "F.DIST.RT",
  20526. "_xlfn.F.INV": "F.INV",
  20527. "_xlfn.F.INV.RT": "F.INV.RT",
  20528. "_xlfn.F.TEST": "F.TEST",
  20529. "_xlfn.FILTERXML": "FILTERXML",
  20530. "_xlfn.FLOOR.MATH": "FLOOR.MATH",
  20531. "_xlfn.FLOOR.PRECISE": "FLOOR.PRECISE",
  20532. "_xlfn.FORECAST.ETS": "FORECAST.ETS",
  20533. "_xlfn.FORECAST.ETS.CONFINT": "FORECAST.ETS.CONFINT",
  20534. "_xlfn.FORECAST.ETS.SEASONALITY": "FORECAST.ETS.SEASONALITY",
  20535. "_xlfn.FORECAST.ETS.STAT": "FORECAST.ETS.STAT",
  20536. "_xlfn.FORECAST.LINEAR": "FORECAST.LINEAR",
  20537. "_xlfn.FORMULATEXT": "FORMULATEXT",
  20538. "_xlfn.GAMMA": "GAMMA",
  20539. "_xlfn.GAMMA.DIST": "GAMMA.DIST",
  20540. "_xlfn.GAMMA.INV": "GAMMA.INV",
  20541. "_xlfn.GAMMALN.PRECISE": "GAMMALN.PRECISE",
  20542. "_xlfn.GAUSS": "GAUSS",
  20543. "_xlfn.HYPGEOM.DIST": "HYPGEOM.DIST",
  20544. "_xlfn.IFERROR": "IFERROR",
  20545. "_xlfn.IFNA": "IFNA",
  20546. "_xlfn.IFS": "IFS",
  20547. "_xlfn.IMCOSH": "IMCOSH",
  20548. "_xlfn.IMCOT": "IMCOT",
  20549. "_xlfn.IMCSC": "IMCSC",
  20550. "_xlfn.IMCSCH": "IMCSCH",
  20551. "_xlfn.IMSEC": "IMSEC",
  20552. "_xlfn.IMSECH": "IMSECH",
  20553. "_xlfn.IMSINH": "IMSINH",
  20554. "_xlfn.IMTAN": "IMTAN",
  20555. "_xlfn.ISFORMULA": "ISFORMULA",
  20556. "_xlfn.ISO.CEILING": "ISO.CEILING",
  20557. "_xlfn.ISOWEEKNUM": "ISOWEEKNUM",
  20558. "_xlfn.LOGNORM.DIST": "LOGNORM.DIST",
  20559. "_xlfn.LOGNORM.INV": "LOGNORM.INV",
  20560. "_xlfn.MAXIFS": "MAXIFS",
  20561. "_xlfn.MINIFS": "MINIFS",
  20562. "_xlfn.MODE.MULT": "MODE.MULT",
  20563. "_xlfn.MODE.SNGL": "MODE.SNGL",
  20564. "_xlfn.MUNIT": "MUNIT",
  20565. "_xlfn.NEGBINOM.DIST": "NEGBINOM.DIST",
  20566. "_xlfn.NETWORKDAYS.INTL": "NETWORKDAYS.INTL",
  20567. "_xlfn.NIGBINOM": "NIGBINOM",
  20568. "_xlfn.NORM.DIST": "NORM.DIST",
  20569. "_xlfn.NORM.INV": "NORM.INV",
  20570. "_xlfn.NORM.S.DIST": "NORM.S.DIST",
  20571. "_xlfn.NORM.S.INV": "NORM.S.INV",
  20572. "_xlfn.NUMBERVALUE": "NUMBERVALUE",
  20573. "_xlfn.PDURATION": "PDURATION",
  20574. "_xlfn.PERCENTILE.EXC": "PERCENTILE.EXC",
  20575. "_xlfn.PERCENTILE.INC": "PERCENTILE.INC",
  20576. "_xlfn.PERCENTRANK.EXC": "PERCENTRANK.EXC",
  20577. "_xlfn.PERCENTRANK.INC": "PERCENTRANK.INC",
  20578. "_xlfn.PERMUTATIONA": "PERMUTATIONA",
  20579. "_xlfn.PHI": "PHI",
  20580. "_xlfn.POISSON.DIST": "POISSON.DIST",
  20581. "_xlfn.QUARTILE.EXC": "QUARTILE.EXC",
  20582. "_xlfn.QUARTILE.INC": "QUARTILE.INC",
  20583. "_xlfn.QUERYSTRING": "QUERYSTRING",
  20584. "_xlfn.RANK.AVG": "RANK.AVG",
  20585. "_xlfn.RANK.EQ": "RANK.EQ",
  20586. "_xlfn.RRI": "RRI",
  20587. "_xlfn.SEC": "SEC",
  20588. "_xlfn.SECH": "SECH",
  20589. "_xlfn.SHEET": "SHEET",
  20590. "_xlfn.SHEETS": "SHEETS",
  20591. "_xlfn.SKEW.P": "SKEW.P",
  20592. "_xlfn.STDEV.P": "STDEV.P",
  20593. "_xlfn.STDEV.S": "STDEV.S",
  20594. "_xlfn.SUMIFS": "SUMIFS",
  20595. "_xlfn.SWITCH": "SWITCH",
  20596. "_xlfn.T.DIST": "T.DIST",
  20597. "_xlfn.T.DIST.2T": "T.DIST.2T",
  20598. "_xlfn.T.DIST.RT": "T.DIST.RT",
  20599. "_xlfn.T.INV": "T.INV",
  20600. "_xlfn.T.INV.2T": "T.INV.2T",
  20601. "_xlfn.T.TEST": "T.TEST",
  20602. "_xlfn.TEXTJOIN": "TEXTJOIN",
  20603. "_xlfn.UNICHAR": "UNICHAR",
  20604. "_xlfn.UNICODE": "UNICODE",
  20605. "_xlfn.VAR.P": "VAR.P",
  20606. "_xlfn.VAR.S": "VAR.S",
  20607. "_xlfn.WEBSERVICE": "WEBSERVICE",
  20608. "_xlfn.WEIBULL.DIST": "WEIBULL.DIST",
  20609. "_xlfn.WORKDAY.INTL": "WORKDAY.INTL",
  20610. "_xlfn.XOR": "XOR",
  20611. "_xlfn.Z.TEST": "Z.TEST"
  20612. };
  20613. /* Part 3 TODO: actually parse formulae */
  20614. function ods_to_csf_formula(f) {
  20615. if(f.slice(0,3) == "of:") f = f.slice(3);
  20616. /* 5.2 Basic Expressions */
  20617. if(f.charCodeAt(0) == 61) {
  20618. f = f.slice(1);
  20619. if(f.charCodeAt(0) == 61) f = f.slice(1);
  20620. }
  20621. f = f.replace(/COM\.MICROSOFT\./g, "");
  20622. /* Part 3 Section 5.8 References */
  20623. f = f.replace(/\[((?:\.[A-Z]+[0-9]+)(?::\.[A-Z]+[0-9]+)?)\]/g, function($$, $1) { return $1.replace(/\./g,""); });
  20624. /* TODO: something other than this */
  20625. f = f.replace(/\[.(#[A-Z]*[?!])\]/g, "$1");
  20626. return f.replace(/[;~]/g,",").replace(/\|/g,";");
  20627. }
  20628. function csf_to_ods_formula(f) {
  20629. var o = "of:=" + f.replace(crefregex, "$1[.$2$3$4$5]").replace(/\]:\[/g,":");
  20630. /* TODO: something other than this */
  20631. return o.replace(/;/g, "|").replace(/,/g,";");
  20632. }
  20633. function ods_to_csf_3D(r) {
  20634. var a = r.split(":");
  20635. var s = a[0].split(".")[0];
  20636. return [s, a[0].split(".")[1] + (a.length > 1 ? (":" + (a[1].split(".")[1] || a[1].split(".")[0])) : "")];
  20637. }
  20638. function csf_to_ods_3D(r) {
  20639. return r.replace(/\./,"!");
  20640. }
  20641. var strs = {}; // shared strings
  20642. var _ssfopts = {}; // spreadsheet formatting options
  20643. RELS.WS = [
  20644. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
  20645. "http://purl.oclc.org/ooxml/officeDocument/relationships/worksheet"
  20646. ];
  20647. /*global Map */
  20648. var browser_has_Map = typeof Map !== 'undefined';
  20649. function get_sst_id(sst, str, rev) {
  20650. var i = 0, len = sst.length;
  20651. if(rev) {
  20652. if(browser_has_Map ? rev.has(str) : rev.hasOwnProperty(str)) {
  20653. var revarr = browser_has_Map ? rev.get(str) : rev[str];
  20654. for(; i < revarr.length; ++i) {
  20655. if(sst[revarr[i]].t === str) { sst.Count ++; return revarr[i]; }
  20656. }
  20657. }
  20658. } else for(; i < len; ++i) {
  20659. if(sst[i].t === str) { sst.Count ++; return i; }
  20660. }
  20661. sst[len] = ({t:str}); sst.Count ++; sst.Unique ++;
  20662. if(rev) {
  20663. if(browser_has_Map) {
  20664. if(!rev.has(str)) rev.set(str, []);
  20665. rev.get(str).push(len);
  20666. } else {
  20667. if(!rev.hasOwnProperty(str)) rev[str] = [];
  20668. rev[str].push(len);
  20669. }
  20670. }
  20671. return len;
  20672. }
  20673. function col_obj_w(C, col) {
  20674. var p = ({min:C+1,max:C+1});
  20675. /* wch (chars), wpx (pixels) */
  20676. var wch = -1;
  20677. if(col.MDW) MDW = col.MDW;
  20678. if(col.width != null) p.customWidth = 1;
  20679. else if(col.wpx != null) wch = px2char(col.wpx);
  20680. else if(col.wch != null) wch = col.wch;
  20681. if(wch > -1) { p.width = char2width(wch); p.customWidth = 1; }
  20682. else if(col.width != null) p.width = col.width;
  20683. if(col.hidden) p.hidden = true;
  20684. return p;
  20685. }
  20686. function default_margins(margins, mode) {
  20687. if(!margins) return;
  20688. var defs = [0.7, 0.7, 0.75, 0.75, 0.3, 0.3];
  20689. if(mode == 'xlml') defs = [1, 1, 1, 1, 0.5, 0.5];
  20690. if(margins.left == null) margins.left = defs[0];
  20691. if(margins.right == null) margins.right = defs[1];
  20692. if(margins.top == null) margins.top = defs[2];
  20693. if(margins.bottom == null) margins.bottom = defs[3];
  20694. if(margins.header == null) margins.header = defs[4];
  20695. if(margins.footer == null) margins.footer = defs[5];
  20696. }
  20697. function get_cell_style(styles, cell, opts) {
  20698. if (typeof style_builder != 'undefined') {
  20699. if (/^\d+$/.exec(cell.s)) { return cell.s} // if its already an integer index, let it be
  20700. if (cell.s && (cell.s == +cell.s)) { return cell.s} // if its already an integer index, let it be
  20701. var s = cell.s || {};
  20702. if (cell.z) s.numFmt = cell.z;
  20703. return style_builder.addStyle(s);
  20704. }
  20705. else {
  20706. var z = opts.revssf[cell.z != null ? cell.z : "General"];
  20707. var i = 0x3c, len = styles.length;
  20708. if (z == null && opts.ssf) {
  20709. for (; i < 0x188; ++i) if (opts.ssf[i] == null) {
  20710. SSF.load(cell.z, i);
  20711. // $FlowIgnore
  20712. opts.ssf[i] = cell.z;
  20713. opts.revssf[cell.z] = z = i;
  20714. break;
  20715. }
  20716. }
  20717. for (i = 0; i != len; ++i) if (styles[i].numFmtId === z) return i;
  20718. styles[len] = {
  20719. numFmtId: z,
  20720. fontId: 0,
  20721. fillId: 0,
  20722. borderId: 0,
  20723. xfId: 0,
  20724. applyNumberFormat: 1
  20725. };
  20726. return len;
  20727. }
  20728. }
  20729. function safe_format(p, fmtid, fillid, opts, themes, styles) {
  20730. if(p.t === 'z') return;
  20731. if(p.t === 'd' && typeof p.v === 'string') p.v = parseDate(p.v);
  20732. try {
  20733. if(opts.cellNF) p.z = SSF._table[fmtid];
  20734. } catch(e) { if(opts.WTF) throw e; }
  20735. if(!opts || opts.cellText !== false) try {
  20736. if(SSF._table[fmtid] == null) SSF.load(SSFImplicit[fmtid] || "General", fmtid);
  20737. if(p.t === 'e') p.w = p.w || BErr[p.v];
  20738. else if(fmtid === 0) {
  20739. if(p.t === 'n') {
  20740. if((p.v|0) === p.v) p.w = SSF._general_int(p.v);
  20741. else p.w = SSF._general_num(p.v);
  20742. }
  20743. else if(p.t === 'd') {
  20744. var dd = datenum(p.v);
  20745. if((dd|0) === dd) p.w = SSF._general_int(dd);
  20746. else p.w = SSF._general_num(dd);
  20747. }
  20748. else if(p.v === undefined) return "";
  20749. else p.w = SSF._general(p.v,_ssfopts);
  20750. }
  20751. else if(p.t === 'd') p.w = SSF.format(fmtid,datenum(p.v),_ssfopts);
  20752. else p.w = SSF.format(fmtid,p.v,_ssfopts);
  20753. } catch(e) { if(opts.WTF) throw e; }
  20754. if(!opts.cellStyles) return;
  20755. if(fillid != null) try {
  20756. p.s = styles.Fills[fillid];
  20757. if (p.s.fgColor && p.s.fgColor.theme && !p.s.fgColor.rgb) {
  20758. p.s.fgColor.rgb = rgb_tint(themes.themeElements.clrScheme[p.s.fgColor.theme].rgb, p.s.fgColor.tint || 0);
  20759. if(opts.WTF) p.s.fgColor.raw_rgb = themes.themeElements.clrScheme[p.s.fgColor.theme].rgb;
  20760. }
  20761. if (p.s.bgColor && p.s.bgColor.theme) {
  20762. p.s.bgColor.rgb = rgb_tint(themes.themeElements.clrScheme[p.s.bgColor.theme].rgb, p.s.bgColor.tint || 0);
  20763. if(opts.WTF) p.s.bgColor.raw_rgb = themes.themeElements.clrScheme[p.s.bgColor.theme].rgb;
  20764. }
  20765. } catch(e) { if(opts.WTF && styles.Fills) throw e; }
  20766. }
  20767. function check_ws(ws, sname, i) {
  20768. if(ws && ws['!ref']) {
  20769. var range = safe_decode_range(ws['!ref']);
  20770. if(range.e.c < range.s.c || range.e.r < range.s.r) throw new Error("Bad range (" + i + "): " + ws['!ref']);
  20771. }
  20772. }
  20773. function parse_ws_xml_dim(ws, s) {
  20774. var d = safe_decode_range(s);
  20775. if(d.s.r<=d.e.r && d.s.c<=d.e.c && d.s.r>=0 && d.s.c>=0) ws["!ref"] = encode_range(d);
  20776. }
  20777. var mergecregex = /<(?:\w:)?mergeCell ref="[A-Z0-9:]+"\s*[\/]?>/g;
  20778. var sheetdataregex = /<(?:\w+:)?sheetData>([\s\S]*)<\/(?:\w+:)?sheetData>/;
  20779. var hlinkregex = /<(?:\w:)?hyperlink [^>]*>/mg;
  20780. var dimregex = /"(\w*:\w*)"/;
  20781. var colregex = /<(?:\w:)?col\b[^>]*[\/]?>/g;
  20782. var afregex = /<(?:\w:)?autoFilter[^>]*([\/]|>([\s\S]*)<\/(?:\w:)?autoFilter)>/g;
  20783. var marginregex= /<(?:\w:)?pageMargins[^>]*\/>/g;
  20784. var sheetprregex = /<(?:\w:)?sheetPr\b(?:[^>a-z][^>]*)?\/>/;
  20785. var svsregex = /<(?:\w:)?sheetViews[^>]*(?:[\/]|>([\s\S]*)<\/(?:\w:)?sheetViews)>/;
  20786. /* 18.3 Worksheets */
  20787. function parse_ws_xml(data, opts, idx, rels, wb, themes, styles) {
  20788. if(!data) return data;
  20789. if(DENSE != null && opts.dense == null) opts.dense = DENSE;
  20790. /* 18.3.1.99 worksheet CT_Worksheet */
  20791. var s = opts.dense ? ([]) : ({});
  20792. var refguess = ({s: {r:2000000, c:2000000}, e: {r:0, c:0} });
  20793. var data1 = "", data2 = "";
  20794. var mtch = data.match(sheetdataregex);
  20795. if(mtch) {
  20796. data1 = data.slice(0, mtch.index);
  20797. data2 = data.slice(mtch.index + mtch[0].length);
  20798. } else data1 = data2 = data;
  20799. /* 18.3.1.82 sheetPr CT_SheetPr */
  20800. var sheetPr = data1.match(sheetprregex);
  20801. if(sheetPr) parse_ws_xml_sheetpr(sheetPr[0], s, wb, idx);
  20802. /* 18.3.1.35 dimension CT_SheetDimension */
  20803. // $FlowIgnore
  20804. var ridx = (data1.match(/<(?:\w*:)?dimension/)||{index:-1}).index;
  20805. if(ridx > 0) {
  20806. var ref = data1.slice(ridx,ridx+50).match(dimregex);
  20807. if(ref) parse_ws_xml_dim(s, ref[1]);
  20808. }
  20809. /* 18.3.1.88 sheetViews CT_SheetViews */
  20810. var svs = data1.match(svsregex);
  20811. if(svs && svs[1]) parse_ws_xml_sheetviews(svs[1], wb);
  20812. /* 18.3.1.17 cols CT_Cols */
  20813. var columns = [];
  20814. if(opts.cellStyles) {
  20815. /* 18.3.1.13 col CT_Col */
  20816. var cols = data1.match(colregex);
  20817. if(cols) parse_ws_xml_cols(columns, cols);
  20818. }
  20819. /* 18.3.1.80 sheetData CT_SheetData ? */
  20820. if(mtch) parse_ws_xml_data(mtch[1], s, opts, refguess, themes, styles);
  20821. /* 18.3.1.2 autoFilter CT_AutoFilter */
  20822. var afilter = data2.match(afregex);
  20823. if(afilter) s['!autofilter'] = parse_ws_xml_autofilter(afilter[0]);
  20824. /* 18.3.1.55 mergeCells CT_MergeCells */
  20825. var merges = [];
  20826. var _merge = data2.match(mergecregex);
  20827. if(_merge) for(ridx = 0; ridx != _merge.length; ++ridx)
  20828. merges[ridx] = safe_decode_range(_merge[ridx].slice(_merge[ridx].indexOf("\"")+1));
  20829. /* 18.3.1.48 hyperlinks CT_Hyperlinks */
  20830. var hlink = data2.match(hlinkregex);
  20831. if(hlink) parse_ws_xml_hlinks(s, hlink, rels);
  20832. /* 18.3.1.62 pageMargins CT_PageMargins */
  20833. var margins = data2.match(marginregex);
  20834. if(margins) s['!margins'] = parse_ws_xml_margins(parsexmltag(margins[0]));
  20835. if(!s["!ref"] && refguess.e.c >= refguess.s.c && refguess.e.r >= refguess.s.r) s["!ref"] = encode_range(refguess);
  20836. if(opts.sheetRows > 0 && s["!ref"]) {
  20837. var tmpref = safe_decode_range(s["!ref"]);
  20838. if(opts.sheetRows <= +tmpref.e.r) {
  20839. tmpref.e.r = opts.sheetRows - 1;
  20840. if(tmpref.e.r > refguess.e.r) tmpref.e.r = refguess.e.r;
  20841. if(tmpref.e.r < tmpref.s.r) tmpref.s.r = tmpref.e.r;
  20842. if(tmpref.e.c > refguess.e.c) tmpref.e.c = refguess.e.c;
  20843. if(tmpref.e.c < tmpref.s.c) tmpref.s.c = tmpref.e.c;
  20844. s["!fullref"] = s["!ref"];
  20845. s["!ref"] = encode_range(tmpref);
  20846. }
  20847. }
  20848. if(columns.length > 0) s["!cols"] = columns;
  20849. if(merges.length > 0) s["!merges"] = merges;
  20850. return s;
  20851. }
  20852. function write_ws_xml_merges(merges) {
  20853. if(merges.length === 0) return "";
  20854. var o = '<mergeCells count="' + merges.length + '">';
  20855. for(var i = 0; i != merges.length; ++i) o += '<mergeCell ref="' + encode_range(merges[i]) + '"/>';
  20856. return o + '</mergeCells>';
  20857. }
  20858. /* 18.3.1.82-3 sheetPr CT_ChartsheetPr / CT_SheetPr */
  20859. function parse_ws_xml_sheetpr(sheetPr, s, wb, idx) {
  20860. var data = parsexmltag(sheetPr);
  20861. if(!wb.Sheets[idx]) wb.Sheets[idx] = {};
  20862. if(data.codeName) wb.Sheets[idx].CodeName = data.codeName;
  20863. }
  20864. /* 18.3.1.85 sheetProtection CT_SheetProtection */
  20865. function write_ws_xml_protection(sp) {
  20866. // algorithmName, hashValue, saltValue, spinCountpassword
  20867. var o = ({sheet:1});
  20868. var deffalse = ["objects", "scenarios", "selectLockedCells", "selectUnlockedCells"];
  20869. var deftrue = [
  20870. "formatColumns", "formatRows", "formatCells",
  20871. "insertColumns", "insertRows", "insertHyperlinks",
  20872. "deleteColumns", "deleteRows",
  20873. "sort", "autoFilter", "pivotTables"
  20874. ];
  20875. deffalse.forEach(function(n) { if(sp[n] != null && sp[n]) o[n] = "1"; });
  20876. deftrue.forEach(function(n) { if(sp[n] != null && !sp[n]) o[n] = "0"; });
  20877. /* TODO: algorithm */
  20878. if(sp.password) o.password = crypto_CreatePasswordVerifier_Method1(sp.password).toString(16).toUpperCase();
  20879. return writextag('sheetProtection', null, o);
  20880. }
  20881. function parse_ws_xml_hlinks(s, data, rels) {
  20882. var dense = Array.isArray(s);
  20883. for(var i = 0; i != data.length; ++i) {
  20884. var val = parsexmltag(utf8read(data[i]), true);
  20885. if(!val.ref) return;
  20886. var rel = ((rels || {})['!id']||[])[val.id];
  20887. if(rel) {
  20888. val.Target = rel.Target;
  20889. if(val.location) val.Target += "#"+val.location;
  20890. } else {
  20891. val.Target = "#" + val.location;
  20892. rel = {Target: val.Target, TargetMode: 'Internal'};
  20893. }
  20894. val.Rel = rel;
  20895. if(val.tooltip) { val.Tooltip = val.tooltip; delete val.tooltip; }
  20896. var rng = safe_decode_range(val.ref);
  20897. for(var R=rng.s.r;R<=rng.e.r;++R) for(var C=rng.s.c;C<=rng.e.c;++C) {
  20898. var addr = encode_cell({c:C,r:R});
  20899. if(dense) {
  20900. if(!s[R]) s[R] = [];
  20901. if(!s[R][C]) s[R][C] = {t:"z",v:undefined};
  20902. s[R][C].l = val;
  20903. } else {
  20904. if(!s[addr]) s[addr] = {t:"z",v:undefined};
  20905. s[addr].l = val;
  20906. }
  20907. }
  20908. }
  20909. }
  20910. function parse_ws_xml_margins(margin) {
  20911. var o = {};
  20912. ["left", "right", "top", "bottom", "header", "footer"].forEach(function(k) {
  20913. if(margin[k]) o[k] = parseFloat(margin[k]);
  20914. });
  20915. return o;
  20916. }
  20917. function write_ws_xml_margins(margin) {
  20918. default_margins(margin);
  20919. return writextag('pageMargins', null, margin);
  20920. }
  20921. function parse_ws_xml_cols(columns, cols) {
  20922. var seencol = false;
  20923. for(var coli = 0; coli != cols.length; ++coli) {
  20924. var coll = parsexmltag(cols[coli], true);
  20925. if(coll.hidden) coll.hidden = parsexmlbool(coll.hidden);
  20926. var colm=parseInt(coll.min, 10)-1, colM=parseInt(coll.max,10)-1;
  20927. delete coll.min; delete coll.max; coll.width = +coll.width;
  20928. if(!seencol && coll.width) { seencol = true; find_mdw_colw(coll.width); }
  20929. process_col(coll);
  20930. while(colm <= colM) columns[colm++] = dup(coll);
  20931. }
  20932. }
  20933. function write_ws_xml_cols(ws, cols) {
  20934. var o = ["<cols>"], col;
  20935. for(var i = 0; i != cols.length; ++i) {
  20936. if(!(col = cols[i])) continue;
  20937. o[o.length] = (writextag('col', null, col_obj_w(i, col)));
  20938. }
  20939. o[o.length] = "</cols>";
  20940. return o.join("");
  20941. }
  20942. function parse_ws_xml_autofilter(data) {
  20943. var o = { ref: (data.match(/ref="([^"]*)"/)||[])[1]};
  20944. return o;
  20945. }
  20946. function write_ws_xml_autofilter(data, ws, wb, idx) {
  20947. var ref = typeof data.ref == "string" ? data.ref : encode_range(data.ref);
  20948. if(!wb.Workbook) wb.Workbook = {};
  20949. if(!wb.Workbook.Names) wb.Workbook.Names = [];
  20950. var names = wb.Workbook.Names;
  20951. var range = decode_range(ref);
  20952. if(range.s.r == range.e.r) { range.e.r = decode_range(ws["!ref"]).e.r; ref = encode_range(range); }
  20953. for(var i = 0; i < names.length; ++i) {
  20954. var name = names[i];
  20955. if(name.Name != '_xlnm._FilterDatabase') continue;
  20956. if(name.Sheet != idx) continue;
  20957. name.Ref = "'" + wb.SheetNames[idx] + "'!" + ref; break;
  20958. }
  20959. if(i == names.length) names.push({ Name: '_xlnm._FilterDatabase', Sheet: idx, Ref: "'" + wb.SheetNames[idx] + "'!" + ref });
  20960. return writextag("autoFilter", null, {ref:ref});
  20961. }
  20962. /* 18.3.1.88 sheetViews CT_SheetViews */
  20963. /* 18.3.1.87 sheetView CT_SheetView */
  20964. var sviewregex = /<(?:\w:)?sheetView(?:[^>a-z][^>]*)?\/>/;
  20965. function parse_ws_xml_sheetviews(data, wb) {
  20966. (data.match(sviewregex)||[]).forEach(function(r) {
  20967. var tag = parsexmltag(r);
  20968. if(parsexmlbool(tag.rightToLeft)) {
  20969. if(!wb.Views) wb.Views = [{}];
  20970. if(!wb.Views[0]) wb.Views[0] = {};
  20971. wb.Views[0].RTL = true;
  20972. }
  20973. });
  20974. }
  20975. function write_ws_xml_sheetviews(ws, opts, idx, wb) {
  20976. var sview = {workbookViewId:"0"};
  20977. // $FlowIgnore
  20978. if( (((wb||{}).Workbook||{}).Views||[])[0] ) sview.rightToLeft = wb.Workbook.Views[0].RTL ? "1" : "0";
  20979. return writextag("sheetViews", writextag("sheetView", null, sview), {});
  20980. }
  20981. function write_ws_xml_cell(cell, ref, ws, opts) {
  20982. if(cell.v === undefined && cell.f === undefined || cell.t === 'z') return "";
  20983. var vv = "";
  20984. var oldt = cell.t, oldv = cell.v;
  20985. switch(cell.t) {
  20986. case 'b': vv = cell.v ? "1" : "0"; break;
  20987. case 'n': vv = ''+cell.v; break;
  20988. case 'e': vv = BErr[cell.v]; break;
  20989. case 'd':
  20990. if(opts.cellDates) vv = parseDate(cell.v, -1).toISOString();
  20991. else {
  20992. cell = dup(cell);
  20993. cell.t = 'n';
  20994. vv = ''+(cell.v = datenum(parseDate(cell.v)));
  20995. }
  20996. if(typeof cell.z === 'undefined') cell.z = SSF._table[14];
  20997. break;
  20998. default: vv = cell.v; break;
  20999. }
  21000. var v = writetag('v', escapexml(vv)), o = ({r:ref});
  21001. /* TODO: cell style */
  21002. var os = get_cell_style(opts.cellXfs, cell, opts);
  21003. if(os !== 0) o.s = os;
  21004. switch(cell.t) {
  21005. case 'n': break;
  21006. case 'd': o.t = "d"; break;
  21007. case 'b': o.t = "b"; break;
  21008. case 'e': o.t = "e"; break;
  21009. default: if(cell.v == null) { delete cell.t; break; }
  21010. if(opts.bookSST) {
  21011. v = writetag('v', ''+get_sst_id(opts.Strings, cell.v, opts.revStrings));
  21012. o.t = "s"; break;
  21013. }
  21014. o.t = "str"; break;
  21015. }
  21016. if(cell.t != oldt) { cell.t = oldt; cell.v = oldv; }
  21017. if(cell.f) {
  21018. var ff = cell.F && cell.F.slice(0, ref.length) == ref ? {t:"array", ref:cell.F} : null;
  21019. v = writextag('f', escapexml(cell.f), ff) + (cell.v != null ? v : "");
  21020. }
  21021. if(cell.l) ws['!links'].push([ref, cell.l]);
  21022. if(cell.c) ws['!comments'].push([ref, cell.c]);
  21023. return writextag('c', v, o);
  21024. }
  21025. var parse_ws_xml_data = (function() {
  21026. var cellregex = /<(?:\w+:)?c[ >]/, rowregex = /<\/(?:\w+:)?row>/;
  21027. var rregex = /r=["']([^"']*)["']/, isregex = /<(?:\w+:)?is>([\S\s]*?)<\/(?:\w+:)?is>/;
  21028. var refregex = /ref=["']([^"']*)["']/;
  21029. var match_v = matchtag("v"), match_f = matchtag("f");
  21030. return function parse_ws_xml_data(sdata, s, opts, guess, themes, styles) {
  21031. var ri = 0, x = "", cells = [], cref = [], idx=0, i=0, cc=0, d="", p;
  21032. var tag, tagr = 0, tagc = 0;
  21033. var sstr, ftag;
  21034. var fmtid = 0, fillid = 0;
  21035. var do_format = Array.isArray(styles.CellXf), cf;
  21036. var arrayf = [];
  21037. var sharedf = [];
  21038. var dense = Array.isArray(s);
  21039. var rows = [], rowobj = {}, rowrite = false;
  21040. for(var marr = sdata.split(rowregex), mt = 0, marrlen = marr.length; mt != marrlen; ++mt) {
  21041. x = marr[mt].trim();
  21042. var xlen = x.length;
  21043. if(xlen === 0) continue;
  21044. /* 18.3.1.73 row CT_Row */
  21045. for(ri = 0; ri < xlen; ++ri) if(x.charCodeAt(ri) === 62) break; ++ri;
  21046. tag = parsexmltag(x.slice(0,ri), true);
  21047. tagr = tag.r != null ? parseInt(tag.r, 10) : tagr+1; tagc = -1;
  21048. if(opts.sheetRows && opts.sheetRows < tagr) continue;
  21049. if(guess.s.r > tagr - 1) guess.s.r = tagr - 1;
  21050. if(guess.e.r < tagr - 1) guess.e.r = tagr - 1;
  21051. if(opts && opts.cellStyles) {
  21052. rowobj = {}; rowrite = false;
  21053. if(tag.ht) { rowrite = true; rowobj.hpt = parseFloat(tag.ht); rowobj.hpx = pt2px(rowobj.hpt); }
  21054. if(tag.hidden == "1") { rowrite = true; rowobj.hidden = true; }
  21055. if(tag.outlineLevel != null) { rowrite = true; rowobj.level = +tag.outlineLevel; }
  21056. if(rowrite) rows[tagr-1] = rowobj;
  21057. }
  21058. /* 18.3.1.4 c CT_Cell */
  21059. cells = x.slice(ri).split(cellregex);
  21060. for(ri = 0; ri != cells.length; ++ri) {
  21061. x = cells[ri].trim();
  21062. if(x.length === 0) continue;
  21063. cref = x.match(rregex); idx = ri; i=0; cc=0;
  21064. x = "<c " + (x.slice(0,1)=="<"?">":"") + x;
  21065. if(cref != null && cref.length === 2) {
  21066. idx = 0; d=cref[1];
  21067. for(i=0; i != d.length; ++i) {
  21068. if((cc=d.charCodeAt(i)-64) < 1 || cc > 26) break;
  21069. idx = 26*idx + cc;
  21070. }
  21071. --idx;
  21072. tagc = idx;
  21073. } else ++tagc;
  21074. for(i = 0; i != x.length; ++i) if(x.charCodeAt(i) === 62) break; ++i;
  21075. tag = parsexmltag(x.slice(0,i), true);
  21076. if(!tag.r) tag.r = encode_cell({r:tagr-1, c:tagc});
  21077. d = x.slice(i);
  21078. p = ({t:""});
  21079. if((cref=d.match(match_v))!= null && cref[1] !== '') p.v=unescapexml(cref[1]);
  21080. if(opts.cellFormula) {
  21081. if((cref=d.match(match_f))!= null && cref[1] !== '') {
  21082. /* TODO: match against XLSXFutureFunctions */
  21083. p.f=_xlfn(unescapexml(utf8read(cref[1])));
  21084. if(cref[0].indexOf('t="array"') > -1) {
  21085. p.F = (d.match(refregex)||[])[1];
  21086. if(p.F.indexOf(":") > -1) arrayf.push([safe_decode_range(p.F), p.F]);
  21087. } else if(cref[0].indexOf('t="shared"') > -1) {
  21088. // TODO: parse formula
  21089. ftag = parsexmltag(cref[0]);
  21090. sharedf[parseInt(ftag.si, 10)] = [ftag, _xlfn(unescapexml(utf8read(cref[1]))), tag.r];
  21091. }
  21092. } else if((cref=d.match(/<f[^>]*\/>/))) {
  21093. ftag = parsexmltag(cref[0]);
  21094. if(sharedf[ftag.si]) p.f = shift_formula_xlsx(sharedf[ftag.si][1], sharedf[ftag.si][2]/*[0].ref*/, tag.r);
  21095. }
  21096. /* TODO: factor out contains logic */
  21097. var _tag = decode_cell(tag.r);
  21098. for(i = 0; i < arrayf.length; ++i)
  21099. if(_tag.r >= arrayf[i][0].s.r && _tag.r <= arrayf[i][0].e.r)
  21100. if(_tag.c >= arrayf[i][0].s.c && _tag.c <= arrayf[i][0].e.c)
  21101. p.F = arrayf[i][1];
  21102. }
  21103. if(tag.t == null && p.v === undefined) {
  21104. if(p.f || p.F) {
  21105. p.v = 0; p.t = "n";
  21106. } else if(!opts.sheetStubs) continue;
  21107. else p.t = "z";
  21108. }
  21109. else p.t = tag.t || "n";
  21110. if(guess.s.c > tagc) guess.s.c = tagc;
  21111. if(guess.e.c < tagc) guess.e.c = tagc;
  21112. /* 18.18.11 t ST_CellType */
  21113. switch(p.t) {
  21114. case 'n':
  21115. if(p.v == "" || p.v == null) {
  21116. if(!opts.sheetStubs) continue;
  21117. p.t = 'z';
  21118. } else p.v = parseFloat(p.v);
  21119. break;
  21120. case 's':
  21121. if(typeof p.v == 'undefined') {
  21122. if(!opts.sheetStubs) continue;
  21123. p.t = 'z';
  21124. } else {
  21125. sstr = strs[parseInt(p.v, 10)];
  21126. p.v = sstr.t;
  21127. p.r = sstr.r;
  21128. if(opts.cellHTML) p.h = sstr.h;
  21129. }
  21130. break;
  21131. case 'str':
  21132. p.t = "s";
  21133. p.v = (p.v!=null) ? utf8read(p.v) : '';
  21134. if(opts.cellHTML) p.h = escapehtml(p.v);
  21135. break;
  21136. case 'inlineStr':
  21137. cref = d.match(isregex);
  21138. p.t = 's';
  21139. if(cref != null && (sstr = parse_si(cref[1]))) p.v = sstr.t; else p.v = "";
  21140. break;
  21141. case 'b': p.v = parsexmlbool(p.v); break;
  21142. case 'd':
  21143. if(opts.cellDates) p.v = parseDate(p.v, 1);
  21144. else { p.v = datenum(parseDate(p.v, 1)); p.t = 'n'; }
  21145. break;
  21146. /* error string in .w, number in .v */
  21147. case 'e':
  21148. if(!opts || opts.cellText !== false) p.w = p.v;
  21149. p.v = RBErr[p.v]; break;
  21150. }
  21151. /* formatting */
  21152. fmtid = fillid = 0;
  21153. if(do_format && tag.s !== undefined) {
  21154. cf = styles.CellXf[tag.s];
  21155. if(cf != null) {
  21156. if(cf.numFmtId != null) fmtid = cf.numFmtId;
  21157. if(opts.cellStyles) {
  21158. if(cf.fillId != null) fillid = cf.fillId;
  21159. }
  21160. }
  21161. }
  21162. safe_format(p, fmtid, fillid, opts, themes, styles);
  21163. if(opts.cellDates && do_format && p.t == 'n' && SSF.is_date(SSF._table[fmtid])) { p.t = 'd'; p.v = numdate(p.v); }
  21164. if(dense) {
  21165. var _r = decode_cell(tag.r);
  21166. if(!s[_r.r]) s[_r.r] = [];
  21167. s[_r.r][_r.c] = p;
  21168. } else s[tag.r] = p;
  21169. }
  21170. }
  21171. if(rows.length > 0) s['!rows'] = rows;
  21172. }; })();
  21173. function write_ws_xml_data(ws, opts, idx, wb) {
  21174. var o = [], r = [], range = safe_decode_range(ws['!ref']), cell="", ref, rr = "", cols = [], R=0, C=0, rows = ws['!rows'];
  21175. var dense = Array.isArray(ws);
  21176. var params = ({r:rr}), row, height = -1;
  21177. for(C = range.s.c; C <= range.e.c; ++C) cols[C] = encode_col(C);
  21178. for(R = range.s.r; R <= range.e.r; ++R) {
  21179. r = [];
  21180. rr = encode_row(R);
  21181. for(C = range.s.c; C <= range.e.c; ++C) {
  21182. ref = cols[C] + rr;
  21183. var _cell = dense ? (ws[R]||[])[C]: ws[ref];
  21184. if(_cell === undefined) continue;
  21185. if((cell = write_ws_xml_cell(_cell, ref, ws, opts, idx, wb)) != null) r.push(cell);
  21186. }
  21187. if(r.length > 0 || (rows && rows[R])) {
  21188. params = ({r:rr});
  21189. if(rows && rows[R]) {
  21190. row = rows[R];
  21191. if(row.hidden) params.hidden = 1;
  21192. height = -1;
  21193. if(row.hpx) height = px2pt(row.hpx);
  21194. else if(row.hpt) height = row.hpt;
  21195. if(height > -1) { params.ht = height; params.customHeight = 1; }
  21196. if(row.level) { params.outlineLevel = row.level; }
  21197. }
  21198. o[o.length] = (writextag('row', r.join(""), params));
  21199. }
  21200. }
  21201. if(rows) for(; R < rows.length; ++R) {
  21202. if(rows && rows[R]) {
  21203. params = ({r:R+1});
  21204. row = rows[R];
  21205. if(row.hidden) params.hidden = 1;
  21206. height = -1;
  21207. if (row.hpx) height = px2pt(row.hpx);
  21208. else if (row.hpt) height = row.hpt;
  21209. if (height > -1) { params.ht = height; params.customHeight = 1; }
  21210. if (row.level) { params.outlineLevel = row.level; }
  21211. o[o.length] = (writextag('row', "", params));
  21212. }
  21213. }
  21214. return o.join("");
  21215. }
  21216. var WS_XML_ROOT = writextag('worksheet', null, {
  21217. 'xmlns': XMLNS.main[0],
  21218. 'xmlns:r': XMLNS.r
  21219. });
  21220. function write_ws_xml(idx, opts, wb, rels) {
  21221. var o = [XML_HEADER, WS_XML_ROOT];
  21222. var s = wb.SheetNames[idx], sidx = 0, rdata = "";
  21223. var ws = wb.Sheets[s];
  21224. if(ws == null) ws = {};
  21225. var ref = ws['!ref'] || 'A1';
  21226. var range = safe_decode_range(ref);
  21227. if(range.e.c > 0x3FFF || range.e.r > 0xFFFFF) {
  21228. if(opts.WTF) throw new Error("Range " + ref + " exceeds format limit A1:XFD1048576");
  21229. range.e.c = Math.min(range.e.c, 0x3FFF);
  21230. range.e.r = Math.min(range.e.c, 0xFFFFF);
  21231. ref = encode_range(range);
  21232. }
  21233. if(!rels) rels = {};
  21234. ws['!comments'] = [];
  21235. ws['!drawing'] = [];
  21236. if(opts.bookType !== 'xlsx' && wb.vbaraw) {
  21237. var cname = wb.SheetNames[idx];
  21238. try { if(wb.Workbook) cname = wb.Workbook.Sheets[idx].CodeName || cname; } catch(e) {}
  21239. o[o.length] = (writextag('sheetPr', null, {'codeName': escapexml(cname)}));
  21240. }
  21241. o[o.length] = (writextag('dimension', null, {'ref': ref}));
  21242. o[o.length] = write_ws_xml_sheetviews(ws, opts, idx, wb);
  21243. /* TODO: store in WB, process styles */
  21244. if(opts.sheetFormat) o[o.length] = (writextag('sheetFormatPr', null, {
  21245. defaultRowHeight:opts.sheetFormat.defaultRowHeight||'16',
  21246. baseColWidth:opts.sheetFormat.baseColWidth||'10',
  21247. outlineLevelRow:opts.sheetFormat.outlineLevelRow||'7'
  21248. }));
  21249. if(ws['!cols'] != null && ws['!cols'].length > 0) o[o.length] = (write_ws_xml_cols(ws, ws['!cols']));
  21250. o[sidx = o.length] = '<sheetData/>';
  21251. ws['!links'] = [];
  21252. if(ws['!ref'] != null) {
  21253. rdata = write_ws_xml_data(ws, opts, idx, wb, rels);
  21254. if(rdata.length > 0) o[o.length] = (rdata);
  21255. }
  21256. if(o.length>sidx+1) { o[o.length] = ('</sheetData>'); o[sidx]=o[sidx].replace("/>",">"); }
  21257. /* sheetCalcPr */
  21258. if(ws['!protect'] != null) o[o.length] = write_ws_xml_protection(ws['!protect']);
  21259. /* protectedRanges */
  21260. /* scenarios */
  21261. if(ws['!autofilter'] != null) o[o.length] = write_ws_xml_autofilter(ws['!autofilter'], ws, wb, idx);
  21262. /* sortState */
  21263. /* dataConsolidate */
  21264. /* customSheetViews */
  21265. if(ws['!merges'] != null && ws['!merges'].length > 0) o[o.length] = (write_ws_xml_merges(ws['!merges']));
  21266. /* phoneticPr */
  21267. /* conditionalFormatting */
  21268. /* dataValidations */
  21269. var relc = -1, rel, rId = -1;
  21270. if(ws['!links'].length > 0) {
  21271. o[o.length] = "<hyperlinks>";
  21272. ws['!links'].forEach(function(l) {
  21273. if(!l[1].Target) return;
  21274. rel = ({"ref":l[0]});
  21275. if(l[1].Target.charAt(0) != "#") {
  21276. rId = add_rels(rels, -1, escapexml(l[1].Target).replace(/#.*$/, ""), RELS.HLINK);
  21277. rel["r:id"] = "rId"+rId;
  21278. }
  21279. if((relc = l[1].Target.indexOf("#")) > -1) rel.location = escapexml(l[1].Target.slice(relc+1));
  21280. if(l[1].Tooltip) rel.tooltip = escapexml(l[1].Tooltip);
  21281. o[o.length] = writextag("hyperlink",null,rel);
  21282. });
  21283. o[o.length] = "</hyperlinks>";
  21284. }
  21285. delete ws['!links'];
  21286. /* printOptions */
  21287. if (ws['!margins'] != null) o[o.length] = write_ws_xml_margins(ws['!margins']);
  21288. /* pageSetup */
  21289. //var hfidx = o.length;
  21290. o[o.length] = "";
  21291. /* rowBreaks */
  21292. /* colBreaks */
  21293. /* customProperties */
  21294. /* cellWatches */
  21295. if(!opts || opts.ignoreEC || (opts.ignoreEC == (void 0))) o[o.length] = writetag("ignoredErrors", writextag("ignoredError", null, {numberStoredAsText:1, sqref:ref}));
  21296. /* smartTags */
  21297. if(ws['!drawing'].length > 0) {
  21298. rId = add_rels(rels, -1, "../drawings/drawing" + (idx+1) + ".xml", RELS.DRAW);
  21299. o[o.length] = writextag("drawing", null, {"r:id":"rId" + rId});
  21300. }
  21301. else delete ws['!drawing'];
  21302. if(ws['!comments'].length > 0) {
  21303. rId = add_rels(rels, -1, "../drawings/vmlDrawing" + (idx+1) + ".vml", RELS.VML);
  21304. o[o.length] = writextag("legacyDrawing", null, {"r:id":"rId" + rId});
  21305. ws['!legacy'] = rId;
  21306. }
  21307. /* drawingHF */
  21308. /* picture */
  21309. /* oleObjects */
  21310. /* controls */
  21311. /* webPublishItems */
  21312. /* tableParts */
  21313. /* extList */
  21314. if(o.length>2) { o[o.length] = ('</worksheet>'); o[1]=o[1].replace("/>",">"); }
  21315. return o.join("");
  21316. }
  21317. /* [MS-XLSB] 2.4.726 BrtRowHdr */
  21318. function parse_BrtRowHdr(data, length) {
  21319. var z = ({});
  21320. var tgt = data.l + length;
  21321. z.r = data.read_shift(4);
  21322. data.l += 4; // TODO: ixfe
  21323. var miyRw = data.read_shift(2);
  21324. data.l += 1; // TODO: top/bot padding
  21325. var flags = data.read_shift(1);
  21326. data.l = tgt;
  21327. if(flags & 0x07) z.level = flags & 0x07;
  21328. if(flags & 0x10) z.hidden = true;
  21329. if(flags & 0x20) z.hpt = miyRw / 20;
  21330. return z;
  21331. }
  21332. function write_BrtRowHdr(R, range, ws) {
  21333. var o = new_buf(17+8*16);
  21334. var row = (ws['!rows']||[])[R]||{};
  21335. o.write_shift(4, R);
  21336. o.write_shift(4, 0); /* TODO: ixfe */
  21337. var miyRw = 0x0140;
  21338. if(row.hpx) miyRw = px2pt(row.hpx) * 20;
  21339. else if(row.hpt) miyRw = row.hpt * 20;
  21340. o.write_shift(2, miyRw);
  21341. o.write_shift(1, 0); /* top/bot padding */
  21342. var flags = 0x0;
  21343. if(row.level) flags |= row.level;
  21344. if(row.hidden) flags |= 0x10;
  21345. if(row.hpx || row.hpt) flags |= 0x20;
  21346. o.write_shift(1, flags);
  21347. o.write_shift(1, 0); /* phonetic guide */
  21348. /* [MS-XLSB] 2.5.8 BrtColSpan explains the mechanism */
  21349. var ncolspan = 0, lcs = o.l;
  21350. o.l += 4;
  21351. var caddr = {r:R, c:0};
  21352. for(var i = 0; i < 16; ++i) {
  21353. if((range.s.c > ((i+1) << 10)) || (range.e.c < (i << 10))) continue;
  21354. var first = -1, last = -1;
  21355. for(var j = (i<<10); j < ((i+1)<<10); ++j) {
  21356. caddr.c = j;
  21357. var cell = Array.isArray(ws) ? (ws[caddr.r]||[])[caddr.c] : ws[encode_cell(caddr)];
  21358. if(cell) { if(first < 0) first = j; last = j; }
  21359. }
  21360. if(first < 0) continue;
  21361. ++ncolspan;
  21362. o.write_shift(4, first);
  21363. o.write_shift(4, last);
  21364. }
  21365. var l = o.l;
  21366. o.l = lcs;
  21367. o.write_shift(4, ncolspan);
  21368. o.l = l;
  21369. return o.length > o.l ? o.slice(0, o.l) : o;
  21370. }
  21371. function write_row_header(ba, ws, range, R) {
  21372. var o = write_BrtRowHdr(R, range, ws);
  21373. if((o.length > 17) || (ws['!rows']||[])[R]) write_record(ba, 'BrtRowHdr', o);
  21374. }
  21375. /* [MS-XLSB] 2.4.820 BrtWsDim */
  21376. var parse_BrtWsDim = parse_UncheckedRfX;
  21377. var write_BrtWsDim = write_UncheckedRfX;
  21378. /* [MS-XLSB] 2.4.821 BrtWsFmtInfo */
  21379. function parse_BrtWsFmtInfo() {
  21380. }
  21381. //function write_BrtWsFmtInfo(ws, o) { }
  21382. /* [MS-XLSB] 2.4.823 BrtWsProp */
  21383. function parse_BrtWsProp(data, length) {
  21384. var z = {};
  21385. /* TODO: pull flags */
  21386. data.l += 19;
  21387. z.name = parse_XLSBCodeName(data, length - 19);
  21388. return z;
  21389. }
  21390. function write_BrtWsProp(str, o) {
  21391. if(o == null) o = new_buf(84+4*str.length);
  21392. for(var i = 0; i < 3; ++i) o.write_shift(1,0);
  21393. write_BrtColor({auto:1}, o);
  21394. o.write_shift(-4,-1);
  21395. o.write_shift(-4,-1);
  21396. write_XLSBCodeName(str, o);
  21397. return o.slice(0, o.l);
  21398. }
  21399. /* [MS-XLSB] 2.4.306 BrtCellBlank */
  21400. function parse_BrtCellBlank(data) {
  21401. var cell = parse_XLSBCell(data);
  21402. return [cell];
  21403. }
  21404. function write_BrtCellBlank(cell, ncell, o) {
  21405. if(o == null) o = new_buf(8);
  21406. return write_XLSBCell(ncell, o);
  21407. }
  21408. /* [MS-XLSB] 2.4.307 BrtCellBool */
  21409. function parse_BrtCellBool(data) {
  21410. var cell = parse_XLSBCell(data);
  21411. var fBool = data.read_shift(1);
  21412. return [cell, fBool, 'b'];
  21413. }
  21414. function write_BrtCellBool(cell, ncell, o) {
  21415. if(o == null) o = new_buf(9);
  21416. write_XLSBCell(ncell, o);
  21417. o.write_shift(1, cell.v ? 1 : 0);
  21418. return o;
  21419. }
  21420. /* [MS-XLSB] 2.4.308 BrtCellError */
  21421. function parse_BrtCellError(data) {
  21422. var cell = parse_XLSBCell(data);
  21423. var bError = data.read_shift(1);
  21424. return [cell, bError, 'e'];
  21425. }
  21426. /* [MS-XLSB] 2.4.311 BrtCellIsst */
  21427. function parse_BrtCellIsst(data) {
  21428. var cell = parse_XLSBCell(data);
  21429. var isst = data.read_shift(4);
  21430. return [cell, isst, 's'];
  21431. }
  21432. function write_BrtCellIsst(cell, ncell, o) {
  21433. if(o == null) o = new_buf(12);
  21434. write_XLSBCell(ncell, o);
  21435. o.write_shift(4, ncell.v);
  21436. return o;
  21437. }
  21438. /* [MS-XLSB] 2.4.313 BrtCellReal */
  21439. function parse_BrtCellReal(data) {
  21440. var cell = parse_XLSBCell(data);
  21441. var value = parse_Xnum(data);
  21442. return [cell, value, 'n'];
  21443. }
  21444. function write_BrtCellReal(cell, ncell, o) {
  21445. if(o == null) o = new_buf(16);
  21446. write_XLSBCell(ncell, o);
  21447. write_Xnum(cell.v, o);
  21448. return o;
  21449. }
  21450. /* [MS-XLSB] 2.4.314 BrtCellRk */
  21451. function parse_BrtCellRk(data) {
  21452. var cell = parse_XLSBCell(data);
  21453. var value = parse_RkNumber(data);
  21454. return [cell, value, 'n'];
  21455. }
  21456. function write_BrtCellRk(cell, ncell, o) {
  21457. if(o == null) o = new_buf(12);
  21458. write_XLSBCell(ncell, o);
  21459. write_RkNumber(cell.v, o);
  21460. return o;
  21461. }
  21462. /* [MS-XLSB] 2.4.317 BrtCellSt */
  21463. function parse_BrtCellSt(data) {
  21464. var cell = parse_XLSBCell(data);
  21465. var value = parse_XLWideString(data);
  21466. return [cell, value, 'str'];
  21467. }
  21468. function write_BrtCellSt(cell, ncell, o) {
  21469. if(o == null) o = new_buf(12 + 4 * cell.v.length);
  21470. write_XLSBCell(ncell, o);
  21471. write_XLWideString(cell.v, o);
  21472. return o.length > o.l ? o.slice(0, o.l) : o;
  21473. }
  21474. /* [MS-XLSB] 2.4.653 BrtFmlaBool */
  21475. function parse_BrtFmlaBool(data, length, opts) {
  21476. var end = data.l + length;
  21477. var cell = parse_XLSBCell(data);
  21478. cell.r = opts['!row'];
  21479. var value = data.read_shift(1);
  21480. var o = [cell, value, 'b'];
  21481. if(opts.cellFormula) {
  21482. data.l += 2;
  21483. var formula = parse_XLSBCellParsedFormula(data, end - data.l, opts);
  21484. o[3] = stringify_formula(formula, null/*range*/, cell, opts.supbooks, opts);/* TODO */
  21485. }
  21486. else data.l = end;
  21487. return o;
  21488. }
  21489. /* [MS-XLSB] 2.4.654 BrtFmlaError */
  21490. function parse_BrtFmlaError(data, length, opts) {
  21491. var end = data.l + length;
  21492. var cell = parse_XLSBCell(data);
  21493. cell.r = opts['!row'];
  21494. var value = data.read_shift(1);
  21495. var o = [cell, value, 'e'];
  21496. if(opts.cellFormula) {
  21497. data.l += 2;
  21498. var formula = parse_XLSBCellParsedFormula(data, end - data.l, opts);
  21499. o[3] = stringify_formula(formula, null/*range*/, cell, opts.supbooks, opts);/* TODO */
  21500. }
  21501. else data.l = end;
  21502. return o;
  21503. }
  21504. /* [MS-XLSB] 2.4.655 BrtFmlaNum */
  21505. function parse_BrtFmlaNum(data, length, opts) {
  21506. var end = data.l + length;
  21507. var cell = parse_XLSBCell(data);
  21508. cell.r = opts['!row'];
  21509. var value = parse_Xnum(data);
  21510. var o = [cell, value, 'n'];
  21511. if(opts.cellFormula) {
  21512. data.l += 2;
  21513. var formula = parse_XLSBCellParsedFormula(data, end - data.l, opts);
  21514. o[3] = stringify_formula(formula, null/*range*/, cell, opts.supbooks, opts);/* TODO */
  21515. }
  21516. else data.l = end;
  21517. return o;
  21518. }
  21519. /* [MS-XLSB] 2.4.656 BrtFmlaString */
  21520. function parse_BrtFmlaString(data, length, opts) {
  21521. var end = data.l + length;
  21522. var cell = parse_XLSBCell(data);
  21523. cell.r = opts['!row'];
  21524. var value = parse_XLWideString(data);
  21525. var o = [cell, value, 'str'];
  21526. if(opts.cellFormula) {
  21527. data.l += 2;
  21528. var formula = parse_XLSBCellParsedFormula(data, end - data.l, opts);
  21529. o[3] = stringify_formula(formula, null/*range*/, cell, opts.supbooks, opts);/* TODO */
  21530. }
  21531. else data.l = end;
  21532. return o;
  21533. }
  21534. /* [MS-XLSB] 2.4.682 BrtMergeCell */
  21535. var parse_BrtMergeCell = parse_UncheckedRfX;
  21536. var write_BrtMergeCell = write_UncheckedRfX;
  21537. /* [MS-XLSB] 2.4.107 BrtBeginMergeCells */
  21538. function write_BrtBeginMergeCells(cnt, o) {
  21539. if(o == null) o = new_buf(4);
  21540. o.write_shift(4, cnt);
  21541. return o;
  21542. }
  21543. /* [MS-XLSB] 2.4.662 BrtHLink */
  21544. function parse_BrtHLink(data, length) {
  21545. var end = data.l + length;
  21546. var rfx = parse_UncheckedRfX(data, 16);
  21547. var relId = parse_XLNullableWideString(data);
  21548. var loc = parse_XLWideString(data);
  21549. var tooltip = parse_XLWideString(data);
  21550. var display = parse_XLWideString(data);
  21551. data.l = end;
  21552. var o = ({rfx:rfx, relId:relId, loc:loc, display:display});
  21553. if(tooltip) o.Tooltip = tooltip;
  21554. return o;
  21555. }
  21556. function write_BrtHLink(l, rId) {
  21557. var o = new_buf(50+4*(l[1].Target.length + (l[1].Tooltip || "").length));
  21558. write_UncheckedRfX({s:decode_cell(l[0]), e:decode_cell(l[0])}, o);
  21559. write_RelID("rId" + rId, o);
  21560. var locidx = l[1].Target.indexOf("#");
  21561. var loc = locidx == -1 ? "" : l[1].Target.slice(locidx+1);
  21562. write_XLWideString(loc || "", o);
  21563. write_XLWideString(l[1].Tooltip || "", o);
  21564. write_XLWideString("", o);
  21565. return o.slice(0, o.l);
  21566. }
  21567. /* [MS-XLSB] 2.4.6 BrtArrFmla */
  21568. function parse_BrtArrFmla(data, length, opts) {
  21569. var end = data.l + length;
  21570. var rfx = parse_RfX(data, 16);
  21571. var fAlwaysCalc = data.read_shift(1);
  21572. var o = [rfx]; o[2] = fAlwaysCalc;
  21573. if(opts.cellFormula) {
  21574. var formula = parse_XLSBArrayParsedFormula(data, end - data.l, opts);
  21575. o[1] = formula;
  21576. } else data.l = end;
  21577. return o;
  21578. }
  21579. /* [MS-XLSB] 2.4.750 BrtShrFmla */
  21580. function parse_BrtShrFmla(data, length, opts) {
  21581. var end = data.l + length;
  21582. var rfx = parse_UncheckedRfX(data, 16);
  21583. var o = [rfx];
  21584. if(opts.cellFormula) {
  21585. var formula = parse_XLSBSharedParsedFormula(data, end - data.l, opts);
  21586. o[1] = formula;
  21587. data.l = end;
  21588. } else data.l = end;
  21589. return o;
  21590. }
  21591. /* [MS-XLSB] 2.4.323 BrtColInfo */
  21592. /* TODO: once XLS ColInfo is set, combine the functions */
  21593. function write_BrtColInfo(C, col, o) {
  21594. if(o == null) o = new_buf(18);
  21595. var p = col_obj_w(C, col);
  21596. o.write_shift(-4, C);
  21597. o.write_shift(-4, C);
  21598. o.write_shift(4, (p.width || 10) * 256);
  21599. o.write_shift(4, 0/*ixfe*/); // style
  21600. var flags = 0;
  21601. if(col.hidden) flags |= 0x01;
  21602. if(typeof p.width == 'number') flags |= 0x02;
  21603. o.write_shift(1, flags); // bit flag
  21604. o.write_shift(1, 0); // bit flag
  21605. return o;
  21606. }
  21607. /* [MS-XLSB] 2.4.678 BrtMargins */
  21608. var BrtMarginKeys = ["left","right","top","bottom","header","footer"];
  21609. function parse_BrtMargins(data) {
  21610. var margins = ({});
  21611. BrtMarginKeys.forEach(function(k) { margins[k] = parse_Xnum(data, 8); });
  21612. return margins;
  21613. }
  21614. function write_BrtMargins(margins, o) {
  21615. if(o == null) o = new_buf(6*8);
  21616. default_margins(margins);
  21617. BrtMarginKeys.forEach(function(k) { write_Xnum((margins)[k], o); });
  21618. return o;
  21619. }
  21620. /* [MS-XLSB] 2.4.299 BrtBeginWsView */
  21621. function parse_BrtBeginWsView(data) {
  21622. var f = data.read_shift(2);
  21623. data.l += 28;
  21624. return { RTL: f & 0x20 };
  21625. }
  21626. function write_BrtBeginWsView(ws, Workbook, o) {
  21627. if(o == null) o = new_buf(30);
  21628. var f = 0x39c;
  21629. if((((Workbook||{}).Views||[])[0]||{}).RTL) f |= 0x20;
  21630. o.write_shift(2, f); // bit flag
  21631. o.write_shift(4, 0);
  21632. o.write_shift(4, 0); // view first row
  21633. o.write_shift(4, 0); // view first col
  21634. o.write_shift(1, 0); // gridline color ICV
  21635. o.write_shift(1, 0);
  21636. o.write_shift(2, 0);
  21637. o.write_shift(2, 100); // zoom scale
  21638. o.write_shift(2, 0);
  21639. o.write_shift(2, 0);
  21640. o.write_shift(2, 0);
  21641. o.write_shift(4, 0); // workbook view id
  21642. return o;
  21643. }
  21644. /* [MS-XLSB] 2.4.309 BrtCellIgnoreEC */
  21645. function write_BrtCellIgnoreEC(ref) {
  21646. var o = new_buf(24);
  21647. o.write_shift(4, 4);
  21648. o.write_shift(4, 1);
  21649. write_UncheckedRfX(ref, o);
  21650. return o;
  21651. }
  21652. /* [MS-XLSB] 2.4.748 BrtSheetProtection */
  21653. function write_BrtSheetProtection(sp, o) {
  21654. if(o == null) o = new_buf(16*4+2);
  21655. o.write_shift(2, sp.password ? crypto_CreatePasswordVerifier_Method1(sp.password) : 0);
  21656. o.write_shift(4, 1); // this record should not be written if no protection
  21657. [
  21658. ["objects", false], // fObjects
  21659. ["scenarios", false], // fScenarios
  21660. ["formatCells", true], // fFormatCells
  21661. ["formatColumns", true], // fFormatColumns
  21662. ["formatRows", true], // fFormatRows
  21663. ["insertColumns", true], // fInsertColumns
  21664. ["insertRows", true], // fInsertRows
  21665. ["insertHyperlinks", true], // fInsertHyperlinks
  21666. ["deleteColumns", true], // fDeleteColumns
  21667. ["deleteRows", true], // fDeleteRows
  21668. ["selectLockedCells", false], // fSelLockedCells
  21669. ["sort", true], // fSort
  21670. ["autoFilter", true], // fAutoFilter
  21671. ["pivotTables", true], // fPivotTables
  21672. ["selectUnlockedCells", false] // fSelUnlockedCells
  21673. ].forEach(function(n) {
  21674. if(n[1]) o.write_shift(4, sp[n[0]] != null && !sp[n[0]] ? 1 : 0);
  21675. else o.write_shift(4, sp[n[0]] != null && sp[n[0]] ? 0 : 1);
  21676. });
  21677. return o;
  21678. }
  21679. /* [MS-XLSB] 2.1.7.61 Worksheet */
  21680. function parse_ws_bin(data, _opts, idx, rels, wb, themes, styles) {
  21681. if(!data) return data;
  21682. var opts = _opts || {};
  21683. if(!rels) rels = {'!id':{}};
  21684. if(DENSE != null && opts.dense == null) opts.dense = DENSE;
  21685. var s = (opts.dense ? [] : {});
  21686. var ref;
  21687. var refguess = {s: {r:2000000, c:2000000}, e: {r:0, c:0} };
  21688. var pass = false, end = false;
  21689. var row, p, cf, R, C, addr, sstr, rr, cell;
  21690. var merges = [];
  21691. opts.biff = 12;
  21692. opts['!row'] = 0;
  21693. var ai = 0, af = false;
  21694. var arrayf = [];
  21695. var sharedf = {};
  21696. var supbooks = opts.supbooks || wb.supbooks || ([[]]);
  21697. supbooks.sharedf = sharedf;
  21698. supbooks.arrayf = arrayf;
  21699. supbooks.SheetNames = wb.SheetNames || wb.Sheets.map(function(x) { return x.name; });
  21700. if(!opts.supbooks) {
  21701. opts.supbooks = supbooks;
  21702. if(wb.Names) for(var i = 0; i < wb.Names.length; ++i) supbooks[0][i+1] = wb.Names[i];
  21703. }
  21704. var colinfo = [], rowinfo = [];
  21705. var seencol = false;
  21706. recordhopper(data, function ws_parse(val, R_n, RT) {
  21707. if(end) return;
  21708. switch(RT) {
  21709. case 0x0094: /* 'BrtWsDim' */
  21710. ref = val; break;
  21711. case 0x0000: /* 'BrtRowHdr' */
  21712. row = val;
  21713. if(opts.sheetRows && opts.sheetRows <= row.r) end=true;
  21714. rr = encode_row(R = row.r);
  21715. opts['!row'] = row.r;
  21716. if(val.hidden || val.hpt || val.level != null) {
  21717. if(val.hpt) val.hpx = pt2px(val.hpt);
  21718. rowinfo[val.r] = val;
  21719. }
  21720. break;
  21721. case 0x0002: /* 'BrtCellRk' */
  21722. case 0x0003: /* 'BrtCellError' */
  21723. case 0x0004: /* 'BrtCellBool' */
  21724. case 0x0005: /* 'BrtCellReal' */
  21725. case 0x0006: /* 'BrtCellSt' */
  21726. case 0x0007: /* 'BrtCellIsst' */
  21727. case 0x0008: /* 'BrtFmlaString' */
  21728. case 0x0009: /* 'BrtFmlaNum' */
  21729. case 0x000A: /* 'BrtFmlaBool' */
  21730. case 0x000B: /* 'BrtFmlaError' */
  21731. p = ({t:val[2]});
  21732. switch(val[2]) {
  21733. case 'n': p.v = val[1]; break;
  21734. case 's': sstr = strs[val[1]]; p.v = sstr.t; p.r = sstr.r; break;
  21735. case 'b': p.v = val[1] ? true : false; break;
  21736. case 'e': p.v = val[1]; if(opts.cellText !== false) p.w = BErr[p.v]; break;
  21737. case 'str': p.t = 's'; p.v = val[1]; break;
  21738. }
  21739. if((cf = styles.CellXf[val[0].iStyleRef])) safe_format(p,cf.numFmtId,null,opts, themes, styles);
  21740. C = val[0].c;
  21741. if(opts.dense) { if(!s[R]) s[R] = []; s[R][C] = p; }
  21742. else s[encode_col(C) + rr] = p;
  21743. if(opts.cellFormula) {
  21744. af = false;
  21745. for(ai = 0; ai < arrayf.length; ++ai) {
  21746. var aii = arrayf[ai];
  21747. if(row.r >= aii[0].s.r && row.r <= aii[0].e.r)
  21748. if(C >= aii[0].s.c && C <= aii[0].e.c) {
  21749. p.F = encode_range(aii[0]); af = true;
  21750. }
  21751. }
  21752. if(!af && val.length > 3) p.f = val[3];
  21753. }
  21754. if(refguess.s.r > row.r) refguess.s.r = row.r;
  21755. if(refguess.s.c > C) refguess.s.c = C;
  21756. if(refguess.e.r < row.r) refguess.e.r = row.r;
  21757. if(refguess.e.c < C) refguess.e.c = C;
  21758. if(opts.cellDates && cf && p.t == 'n' && SSF.is_date(SSF._table[cf.numFmtId])) {
  21759. var _d = SSF.parse_date_code(p.v); if(_d) { p.t = 'd'; p.v = new Date(_d.y, _d.m-1,_d.d,_d.H,_d.M,_d.S,_d.u); }
  21760. }
  21761. break;
  21762. case 0x0001: /* 'BrtCellBlank' */
  21763. if(!opts.sheetStubs || pass) break;
  21764. p = ({t:'z',v:undefined});
  21765. C = val[0].c;
  21766. if(opts.dense) { if(!s[R]) s[R] = []; s[R][C] = p; }
  21767. else s[encode_col(C) + rr] = p;
  21768. if(refguess.s.r > row.r) refguess.s.r = row.r;
  21769. if(refguess.s.c > C) refguess.s.c = C;
  21770. if(refguess.e.r < row.r) refguess.e.r = row.r;
  21771. if(refguess.e.c < C) refguess.e.c = C;
  21772. break;
  21773. case 0x00B0: /* 'BrtMergeCell' */
  21774. merges.push(val); break;
  21775. case 0x01EE: /* 'BrtHLink' */
  21776. var rel = rels['!id'][val.relId];
  21777. if(rel) {
  21778. val.Target = rel.Target;
  21779. if(val.loc) val.Target += "#"+val.loc;
  21780. val.Rel = rel;
  21781. } else if(val.relId == '') {
  21782. val.Target = "#" + val.loc;
  21783. }
  21784. for(R=val.rfx.s.r;R<=val.rfx.e.r;++R) for(C=val.rfx.s.c;C<=val.rfx.e.c;++C) {
  21785. if(opts.dense) {
  21786. if(!s[R]) s[R] = [];
  21787. if(!s[R][C]) s[R][C] = {t:'z',v:undefined};
  21788. s[R][C].l = val;
  21789. } else {
  21790. addr = encode_cell({c:C,r:R});
  21791. if(!s[addr]) s[addr] = {t:'z',v:undefined};
  21792. s[addr].l = val;
  21793. }
  21794. }
  21795. break;
  21796. case 0x01AA: /* 'BrtArrFmla' */
  21797. if(!opts.cellFormula) break;
  21798. arrayf.push(val);
  21799. cell = ((opts.dense ? s[R][C] : s[encode_col(C) + rr]));
  21800. cell.f = stringify_formula(val[1], refguess, {r:row.r, c:C}, supbooks, opts);
  21801. cell.F = encode_range(val[0]);
  21802. break;
  21803. case 0x01AB: /* 'BrtShrFmla' */
  21804. if(!opts.cellFormula) break;
  21805. sharedf[encode_cell(val[0].s)] = val[1];
  21806. cell = (opts.dense ? s[R][C] : s[encode_col(C) + rr]);
  21807. cell.f = stringify_formula(val[1], refguess, {r:row.r, c:C}, supbooks, opts);
  21808. break;
  21809. /* identical to 'ColInfo' in XLS */
  21810. case 0x003C: /* 'BrtColInfo' */
  21811. if(!opts.cellStyles) break;
  21812. while(val.e >= val.s) {
  21813. colinfo[val.e--] = { width: val.w/256, hidden: !!(val.flags & 0x01) };
  21814. if(!seencol) { seencol = true; find_mdw_colw(val.w/256); }
  21815. process_col(colinfo[val.e+1]);
  21816. }
  21817. break;
  21818. case 0x00A1: /* 'BrtBeginAFilter' */
  21819. s['!autofilter'] = { ref:encode_range(val) };
  21820. break;
  21821. case 0x01DC: /* 'BrtMargins' */
  21822. s['!margins'] = val;
  21823. break;
  21824. case 0x0093: /* 'BrtWsProp' */
  21825. if(!wb.Sheets[idx]) wb.Sheets[idx] = {};
  21826. if(val.name) wb.Sheets[idx].CodeName = val.name;
  21827. break;
  21828. case 0x0089: /* 'BrtBeginWsView' */
  21829. if(!wb.Views) wb.Views = [{}];
  21830. if(!wb.Views[0]) wb.Views[0] = {};
  21831. if(val.RTL) wb.Views[0].RTL = true;
  21832. break;
  21833. case 0x01E5: /* 'BrtWsFmtInfo' */
  21834. break;
  21835. case 0x00AF: /* 'BrtAFilterDateGroupItem' */
  21836. case 0x0284: /* 'BrtActiveX' */
  21837. case 0x0271: /* 'BrtBigName' */
  21838. case 0x0232: /* 'BrtBkHim' */
  21839. case 0x018C: /* 'BrtBrk' */
  21840. case 0x0458: /* 'BrtCFIcon' */
  21841. case 0x047A: /* 'BrtCFRuleExt' */
  21842. case 0x01D7: /* 'BrtCFVO' */
  21843. case 0x041A: /* 'BrtCFVO14' */
  21844. case 0x0289: /* 'BrtCellIgnoreEC' */
  21845. case 0x0451: /* 'BrtCellIgnoreEC14' */
  21846. case 0x0031: /* 'BrtCellMeta' */
  21847. case 0x024D: /* 'BrtCellSmartTagProperty' */
  21848. case 0x025F: /* 'BrtCellWatch' */
  21849. case 0x0234: /* 'BrtColor' */
  21850. case 0x041F: /* 'BrtColor14' */
  21851. case 0x00A8: /* 'BrtColorFilter' */
  21852. case 0x00AE: /* 'BrtCustomFilter' */
  21853. case 0x049C: /* 'BrtCustomFilter14' */
  21854. case 0x01F3: /* 'BrtDRef' */
  21855. case 0x0040: /* 'BrtDVal' */
  21856. case 0x041D: /* 'BrtDVal14' */
  21857. case 0x0226: /* 'BrtDrawing' */
  21858. case 0x00AB: /* 'BrtDynamicFilter' */
  21859. case 0x00A7: /* 'BrtFilter' */
  21860. case 0x0499: /* 'BrtFilter14' */
  21861. case 0x00A9: /* 'BrtIconFilter' */
  21862. case 0x049D: /* 'BrtIconFilter14' */
  21863. case 0x0227: /* 'BrtLegacyDrawing' */
  21864. case 0x0228: /* 'BrtLegacyDrawingHF' */
  21865. case 0x0295: /* 'BrtListPart' */
  21866. case 0x027F: /* 'BrtOleObject' */
  21867. case 0x01DE: /* 'BrtPageSetup' */
  21868. case 0x0097: /* 'BrtPane' */
  21869. case 0x0219: /* 'BrtPhoneticInfo' */
  21870. case 0x01DD: /* 'BrtPrintOptions' */
  21871. case 0x0218: /* 'BrtRangeProtection' */
  21872. case 0x044F: /* 'BrtRangeProtection14' */
  21873. case 0x02A8: /* 'BrtRangeProtectionIso' */
  21874. case 0x0450: /* 'BrtRangeProtectionIso14' */
  21875. case 0x0400: /* 'BrtRwDescent' */
  21876. case 0x0098: /* 'BrtSel' */
  21877. case 0x0297: /* 'BrtSheetCalcProp' */
  21878. case 0x0217: /* 'BrtSheetProtection' */
  21879. case 0x02A6: /* 'BrtSheetProtectionIso' */
  21880. case 0x01F8: /* 'BrtSlc' */
  21881. case 0x0413: /* 'BrtSparkline' */
  21882. case 0x01AC: /* 'BrtTable' */
  21883. case 0x00AA: /* 'BrtTop10Filter' */
  21884. case 0x0C00: /* 'BrtUid' */
  21885. case 0x0032: /* 'BrtValueMeta' */
  21886. case 0x0816: /* 'BrtWebExtension' */
  21887. case 0x0415: /* 'BrtWsFmtInfoEx14' */
  21888. break;
  21889. case 0x0023: /* 'BrtFRTBegin' */
  21890. pass = true; break;
  21891. case 0x0024: /* 'BrtFRTEnd' */
  21892. pass = false; break;
  21893. case 0x0025: /* 'BrtACBegin' */ break;
  21894. case 0x0026: /* 'BrtACEnd' */ break;
  21895. default:
  21896. if((R_n||"").indexOf("Begin") > 0){/* empty */}
  21897. else if((R_n||"").indexOf("End") > 0){/* empty */}
  21898. else if(!pass || opts.WTF) throw new Error("Unexpected record " + RT + " " + R_n);
  21899. }
  21900. }, opts);
  21901. delete opts.supbooks;
  21902. delete opts['!row'];
  21903. if(!s["!ref"] && (refguess.s.r < 2000000 || ref && (ref.e.r > 0 || ref.e.c > 0 || ref.s.r > 0 || ref.s.c > 0))) s["!ref"] = encode_range(ref || refguess);
  21904. if(opts.sheetRows && s["!ref"]) {
  21905. var tmpref = safe_decode_range(s["!ref"]);
  21906. if(opts.sheetRows <= +tmpref.e.r) {
  21907. tmpref.e.r = opts.sheetRows - 1;
  21908. if(tmpref.e.r > refguess.e.r) tmpref.e.r = refguess.e.r;
  21909. if(tmpref.e.r < tmpref.s.r) tmpref.s.r = tmpref.e.r;
  21910. if(tmpref.e.c > refguess.e.c) tmpref.e.c = refguess.e.c;
  21911. if(tmpref.e.c < tmpref.s.c) tmpref.s.c = tmpref.e.c;
  21912. s["!fullref"] = s["!ref"];
  21913. s["!ref"] = encode_range(tmpref);
  21914. }
  21915. }
  21916. if(merges.length > 0) s["!merges"] = merges;
  21917. if(colinfo.length > 0) s["!cols"] = colinfo;
  21918. if(rowinfo.length > 0) s["!rows"] = rowinfo;
  21919. return s;
  21920. }
  21921. /* TODO: something useful -- this is a stub */
  21922. function write_ws_bin_cell(ba, cell, R, C, opts, ws) {
  21923. if(cell.v === undefined) return "";
  21924. var vv = "";
  21925. switch(cell.t) {
  21926. case 'b': vv = cell.v ? "1" : "0"; break;
  21927. case 'd': // no BrtCellDate :(
  21928. cell = dup(cell);
  21929. cell.z = cell.z || SSF._table[14];
  21930. cell.v = datenum(parseDate(cell.v)); cell.t = 'n';
  21931. break;
  21932. /* falls through */
  21933. case 'n': case 'e': vv = ''+cell.v; break;
  21934. default: vv = cell.v; break;
  21935. }
  21936. var o = ({r:R, c:C});
  21937. /* TODO: cell style */
  21938. o.s = get_cell_style(opts.cellXfs, cell, opts);
  21939. if(cell.l) ws['!links'].push([encode_cell(o), cell.l]);
  21940. if(cell.c) ws['!comments'].push([encode_cell(o), cell.c]);
  21941. switch(cell.t) {
  21942. case 's': case 'str':
  21943. if(opts.bookSST) {
  21944. vv = get_sst_id(opts.Strings, (cell.v), opts.revStrings);
  21945. o.t = "s"; o.v = vv;
  21946. write_record(ba, "BrtCellIsst", write_BrtCellIsst(cell, o));
  21947. } else {
  21948. o.t = "str";
  21949. write_record(ba, "BrtCellSt", write_BrtCellSt(cell, o));
  21950. }
  21951. return;
  21952. case 'n':
  21953. /* TODO: determine threshold for Real vs RK */
  21954. if(cell.v == (cell.v | 0) && cell.v > -1000 && cell.v < 1000) write_record(ba, "BrtCellRk", write_BrtCellRk(cell, o));
  21955. else write_record(ba, "BrtCellReal", write_BrtCellReal(cell, o));
  21956. return;
  21957. case 'b':
  21958. o.t = "b";
  21959. write_record(ba, "BrtCellBool", write_BrtCellBool(cell, o));
  21960. return;
  21961. case 'e': /* TODO: error */ o.t = "e"; break;
  21962. }
  21963. write_record(ba, "BrtCellBlank", write_BrtCellBlank(cell, o));
  21964. }
  21965. function write_CELLTABLE(ba, ws, idx, opts) {
  21966. var range = safe_decode_range(ws['!ref'] || "A1"), ref, rr = "", cols = [];
  21967. write_record(ba, 'BrtBeginSheetData');
  21968. var dense = Array.isArray(ws);
  21969. var cap = range.e.r;
  21970. if(ws['!rows']) cap = Math.max(range.e.r, ws['!rows'].length - 1);
  21971. for(var R = range.s.r; R <= cap; ++R) {
  21972. rr = encode_row(R);
  21973. /* [ACCELLTABLE] */
  21974. /* BrtRowHdr */
  21975. write_row_header(ba, ws, range, R);
  21976. if(R <= range.e.r) for(var C = range.s.c; C <= range.e.c; ++C) {
  21977. /* *16384CELL */
  21978. if(R === range.s.r) cols[C] = encode_col(C);
  21979. ref = cols[C] + rr;
  21980. var cell = dense ? (ws[R]||[])[C] : ws[ref];
  21981. if(!cell) continue;
  21982. /* write cell */
  21983. write_ws_bin_cell(ba, cell, R, C, opts, ws);
  21984. }
  21985. }
  21986. write_record(ba, 'BrtEndSheetData');
  21987. }
  21988. function write_MERGECELLS(ba, ws) {
  21989. if(!ws || !ws['!merges']) return;
  21990. write_record(ba, 'BrtBeginMergeCells', write_BrtBeginMergeCells(ws['!merges'].length));
  21991. ws['!merges'].forEach(function(m) { write_record(ba, 'BrtMergeCell', write_BrtMergeCell(m)); });
  21992. write_record(ba, 'BrtEndMergeCells');
  21993. }
  21994. function write_COLINFOS(ba, ws) {
  21995. if(!ws || !ws['!cols']) return;
  21996. write_record(ba, 'BrtBeginColInfos');
  21997. ws['!cols'].forEach(function(m, i) { if(m) write_record(ba, 'BrtColInfo', write_BrtColInfo(i, m)); });
  21998. write_record(ba, 'BrtEndColInfos');
  21999. }
  22000. function write_IGNOREECS(ba, ws) {
  22001. if(!ws || !ws['!ref']) return;
  22002. write_record(ba, 'BrtBeginCellIgnoreECs');
  22003. write_record(ba, 'BrtCellIgnoreEC', write_BrtCellIgnoreEC(safe_decode_range(ws['!ref'])));
  22004. write_record(ba, 'BrtEndCellIgnoreECs');
  22005. }
  22006. function write_HLINKS(ba, ws, rels) {
  22007. /* *BrtHLink */
  22008. ws['!links'].forEach(function(l) {
  22009. if(!l[1].Target) return;
  22010. var rId = add_rels(rels, -1, l[1].Target.replace(/#.*$/, ""), RELS.HLINK);
  22011. write_record(ba, "BrtHLink", write_BrtHLink(l, rId));
  22012. });
  22013. delete ws['!links'];
  22014. }
  22015. function write_LEGACYDRAWING(ba, ws, idx, rels) {
  22016. /* [BrtLegacyDrawing] */
  22017. if(ws['!comments'].length > 0) {
  22018. var rId = add_rels(rels, -1, "../drawings/vmlDrawing" + (idx+1) + ".vml", RELS.VML);
  22019. write_record(ba, "BrtLegacyDrawing", write_RelID("rId" + rId));
  22020. ws['!legacy'] = rId;
  22021. }
  22022. }
  22023. function write_AUTOFILTER(ba, ws) {
  22024. if(!ws['!autofilter']) return;
  22025. write_record(ba, "BrtBeginAFilter", write_UncheckedRfX(safe_decode_range(ws['!autofilter'].ref)));
  22026. /* *FILTERCOLUMN */
  22027. /* [SORTSTATE] */
  22028. /* BrtEndAFilter */
  22029. write_record(ba, "BrtEndAFilter");
  22030. }
  22031. function write_WSVIEWS2(ba, ws, Workbook) {
  22032. write_record(ba, "BrtBeginWsViews");
  22033. { /* 1*WSVIEW2 */
  22034. /* [ACUID] */
  22035. write_record(ba, "BrtBeginWsView", write_BrtBeginWsView(ws, Workbook));
  22036. /* [BrtPane] */
  22037. /* *4BrtSel */
  22038. /* *4SXSELECT */
  22039. /* *FRT */
  22040. write_record(ba, "BrtEndWsView");
  22041. }
  22042. /* *FRT */
  22043. write_record(ba, "BrtEndWsViews");
  22044. }
  22045. function write_WSFMTINFO() {
  22046. /* [ACWSFMTINFO] */
  22047. //write_record(ba, "BrtWsFmtInfo", write_BrtWsFmtInfo(ws));
  22048. }
  22049. function write_SHEETPROTECT(ba, ws) {
  22050. if(!ws['!protect']) return;
  22051. /* [BrtSheetProtectionIso] */
  22052. write_record(ba, "BrtSheetProtection", write_BrtSheetProtection(ws['!protect']));
  22053. }
  22054. function write_ws_bin(idx, opts, wb, rels) {
  22055. var ba = buf_array();
  22056. var s = wb.SheetNames[idx], ws = wb.Sheets[s] || {};
  22057. var c = s; try { if(wb && wb.Workbook) c = wb.Workbook.Sheets[idx].CodeName || c; } catch(e) {}
  22058. var r = safe_decode_range(ws['!ref'] || "A1");
  22059. if(r.e.c > 0x3FFF || r.e.r > 0xFFFFF) {
  22060. if(opts.WTF) throw new Error("Range " + (ws['!ref'] || "A1") + " exceeds format limit A1:XFD1048576");
  22061. r.e.c = Math.min(r.e.c, 0x3FFF);
  22062. r.e.r = Math.min(r.e.c, 0xFFFFF);
  22063. }
  22064. ws['!links'] = [];
  22065. /* passed back to write_zip and removed there */
  22066. ws['!comments'] = [];
  22067. write_record(ba, "BrtBeginSheet");
  22068. if(wb.vbaraw) write_record(ba, "BrtWsProp", write_BrtWsProp(c));
  22069. write_record(ba, "BrtWsDim", write_BrtWsDim(r));
  22070. write_WSVIEWS2(ba, ws, wb.Workbook);
  22071. write_WSFMTINFO(ba, ws);
  22072. write_COLINFOS(ba, ws, idx, opts, wb);
  22073. write_CELLTABLE(ba, ws, idx, opts, wb);
  22074. /* [BrtSheetCalcProp] */
  22075. write_SHEETPROTECT(ba, ws);
  22076. /* *([BrtRangeProtectionIso] BrtRangeProtection) */
  22077. /* [SCENMAN] */
  22078. write_AUTOFILTER(ba, ws);
  22079. /* [SORTSTATE] */
  22080. /* [DCON] */
  22081. /* [USERSHVIEWS] */
  22082. write_MERGECELLS(ba, ws);
  22083. /* [BrtPhoneticInfo] */
  22084. /* *CONDITIONALFORMATTING */
  22085. /* [DVALS] */
  22086. write_HLINKS(ba, ws, rels);
  22087. /* [BrtPrintOptions] */
  22088. if(ws['!margins']) write_record(ba, "BrtMargins", write_BrtMargins(ws['!margins']));
  22089. /* [BrtPageSetup] */
  22090. /* [HEADERFOOTER] */
  22091. /* [RWBRK] */
  22092. /* [COLBRK] */
  22093. /* *BrtBigName */
  22094. /* [CELLWATCHES] */
  22095. if(!opts || opts.ignoreEC || (opts.ignoreEC == (void 0))) write_IGNOREECS(ba, ws);
  22096. /* [SMARTTAGS] */
  22097. /* [BrtDrawing] */
  22098. write_LEGACYDRAWING(ba, ws, idx, rels);
  22099. /* [BrtLegacyDrawingHF] */
  22100. /* [BrtBkHim] */
  22101. /* [OLEOBJECTS] */
  22102. /* [ACTIVEXCONTROLS] */
  22103. /* [WEBPUBITEMS] */
  22104. /* [LISTPARTS] */
  22105. /* FRTWORKSHEET */
  22106. write_record(ba, "BrtEndSheet");
  22107. return ba.end();
  22108. }
  22109. function parse_numCache(data) {
  22110. var col = [];
  22111. /* 21.2.2.150 pt CT_NumVal */
  22112. (data.match(/<c:pt idx="(\d*)">(.*?)<\/c:pt>/mg)||[]).forEach(function(pt) {
  22113. var q = pt.match(/<c:pt idx="(\d*?)"><c:v>(.*)<\/c:v><\/c:pt>/);
  22114. if(!q) return;
  22115. col[+q[1]] = +q[2];
  22116. });
  22117. /* 21.2.2.71 formatCode CT_Xstring */
  22118. var nf = unescapexml((data.match(/<c:formatCode>([\s\S]*?)<\/c:formatCode>/) || ["","General"])[1]);
  22119. return [col, nf];
  22120. }
  22121. /* 21.2 DrawingML - Charts */
  22122. function parse_chart(data, name, opts, rels, wb, csheet) {
  22123. var cs = ((csheet || {"!type":"chart"}));
  22124. if(!data) return csheet;
  22125. /* 21.2.2.27 chart CT_Chart */
  22126. var C = 0, R = 0, col = "A";
  22127. var refguess = {s: {r:2000000, c:2000000}, e: {r:0, c:0} };
  22128. /* 21.2.2.120 numCache CT_NumData */
  22129. (data.match(/<c:numCache>[\s\S]*?<\/c:numCache>/gm)||[]).forEach(function(nc) {
  22130. var cache = parse_numCache(nc);
  22131. refguess.s.r = refguess.s.c = 0;
  22132. refguess.e.c = C;
  22133. col = encode_col(C);
  22134. cache[0].forEach(function(n,i) {
  22135. cs[col + encode_row(i)] = {t:'n', v:n, z:cache[1] };
  22136. R = i;
  22137. });
  22138. if(refguess.e.r < R) refguess.e.r = R;
  22139. ++C;
  22140. });
  22141. if(C > 0) cs["!ref"] = encode_range(refguess);
  22142. return cs;
  22143. }
  22144. RELS.CS = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet";
  22145. var CS_XML_ROOT = writextag('chartsheet', null, {
  22146. 'xmlns': XMLNS.main[0],
  22147. 'xmlns:r': XMLNS.r
  22148. });
  22149. /* 18.3 Worksheets also covers Chartsheets */
  22150. function parse_cs_xml(data, opts, idx, rels, wb) {
  22151. if(!data) return data;
  22152. /* 18.3.1.12 chartsheet CT_ChartSheet */
  22153. if(!rels) rels = {'!id':{}};
  22154. var s = {'!type':"chart", '!chart':null, '!rel':""};
  22155. var m;
  22156. /* 18.3.1.83 sheetPr CT_ChartsheetPr */
  22157. var sheetPr = data.match(sheetprregex);
  22158. if(sheetPr) parse_ws_xml_sheetpr(sheetPr[0], s, wb, idx);
  22159. /* 18.3.1.36 drawing CT_Drawing */
  22160. if((m = data.match(/drawing r:id="(.*?)"/))) s['!rel'] = m[1];
  22161. if(rels['!id'][s['!rel']]) s['!chart'] = rels['!id'][s['!rel']];
  22162. return s;
  22163. }
  22164. function write_cs_xml(idx, opts, wb, rels) {
  22165. var o = [XML_HEADER, CS_XML_ROOT];
  22166. o[o.length] = writextag("drawing", null, {"r:id": "rId1"});
  22167. add_rels(rels, -1, "../drawings/drawing" + (idx+1) + ".xml", RELS.DRAW);
  22168. if(o.length>2) { o[o.length] = ('</chartsheet>'); o[1]=o[1].replace("/>",">"); }
  22169. return o.join("");
  22170. }
  22171. /* [MS-XLSB] 2.4.331 BrtCsProp */
  22172. function parse_BrtCsProp(data, length) {
  22173. data.l += 10;
  22174. var name = parse_XLWideString(data, length - 10);
  22175. return { name: name };
  22176. }
  22177. /* [MS-XLSB] 2.1.7.7 Chart Sheet */
  22178. function parse_cs_bin(data, opts, idx, rels, wb) {
  22179. if(!data) return data;
  22180. if(!rels) rels = {'!id':{}};
  22181. var s = {'!type':"chart", '!chart':null, '!rel':""};
  22182. var state = [];
  22183. var pass = false;
  22184. recordhopper(data, function cs_parse(val, R_n, RT) {
  22185. switch(RT) {
  22186. case 0x0226: /* 'BrtDrawing' */
  22187. s['!rel'] = val; break;
  22188. case 0x028B: /* 'BrtCsProp' */
  22189. if(!wb.Sheets[idx]) wb.Sheets[idx] = {};
  22190. if(val.name) wb.Sheets[idx].CodeName = val.name;
  22191. break;
  22192. case 0x0232: /* 'BrtBkHim' */
  22193. case 0x028C: /* 'BrtCsPageSetup' */
  22194. case 0x029D: /* 'BrtCsProtection' */
  22195. case 0x02A7: /* 'BrtCsProtectionIso' */
  22196. case 0x0227: /* 'BrtLegacyDrawing' */
  22197. case 0x0228: /* 'BrtLegacyDrawingHF' */
  22198. case 0x01DC: /* 'BrtMargins' */
  22199. case 0x0C00: /* 'BrtUid' */
  22200. break;
  22201. case 0x0023: /* 'BrtFRTBegin' */
  22202. pass = true; break;
  22203. case 0x0024: /* 'BrtFRTEnd' */
  22204. pass = false; break;
  22205. case 0x0025: /* 'BrtACBegin' */
  22206. state.push(R_n); break;
  22207. case 0x0026: /* 'BrtACEnd' */
  22208. state.pop(); break;
  22209. default:
  22210. if((R_n||"").indexOf("Begin") > 0) state.push(R_n);
  22211. else if((R_n||"").indexOf("End") > 0) state.pop();
  22212. else if(!pass || opts.WTF) throw new Error("Unexpected record " + RT + " " + R_n);
  22213. }
  22214. }, opts);
  22215. if(rels['!id'][s['!rel']]) s['!chart'] = rels['!id'][s['!rel']];
  22216. return s;
  22217. }
  22218. function write_cs_bin() {
  22219. var ba = buf_array();
  22220. write_record(ba, "BrtBeginSheet");
  22221. /* [BrtCsProp] */
  22222. /* CSVIEWS */
  22223. /* [[BrtCsProtectionIso] BrtCsProtection] */
  22224. /* [USERCSVIEWS] */
  22225. /* [BrtMargins] */
  22226. /* [BrtCsPageSetup] */
  22227. /* [HEADERFOOTER] */
  22228. /* BrtDrawing */
  22229. /* [BrtLegacyDrawing] */
  22230. /* [BrtLegacyDrawingHF] */
  22231. /* [BrtBkHim] */
  22232. /* [WEBPUBITEMS] */
  22233. /* FRTCHARTSHEET */
  22234. write_record(ba, "BrtEndSheet");
  22235. return ba.end();
  22236. }
  22237. /* 18.2.28 (CT_WorkbookProtection) Defaults */
  22238. var WBPropsDef = [
  22239. ['allowRefreshQuery', false, "bool"],
  22240. ['autoCompressPictures', true, "bool"],
  22241. ['backupFile', false, "bool"],
  22242. ['checkCompatibility', false, "bool"],
  22243. ['CodeName', ''],
  22244. ['date1904', false, "bool"],
  22245. ['defaultThemeVersion', 0, "int"],
  22246. ['filterPrivacy', false, "bool"],
  22247. ['hidePivotFieldList', false, "bool"],
  22248. ['promptedSolutions', false, "bool"],
  22249. ['publishItems', false, "bool"],
  22250. ['refreshAllConnections', false, "bool"],
  22251. ['saveExternalLinkValues', true, "bool"],
  22252. ['showBorderUnselectedTables', true, "bool"],
  22253. ['showInkAnnotation', true, "bool"],
  22254. ['showObjects', 'all'],
  22255. ['showPivotChartFilter', false, "bool"],
  22256. ['updateLinks', 'userSet']
  22257. ];
  22258. /* 18.2.30 (CT_BookView) Defaults */
  22259. var WBViewDef = [
  22260. ['activeTab', 0, "int"],
  22261. ['autoFilterDateGrouping', true, "bool"],
  22262. ['firstSheet', 0, "int"],
  22263. ['minimized', false, "bool"],
  22264. ['showHorizontalScroll', true, "bool"],
  22265. ['showSheetTabs', true, "bool"],
  22266. ['showVerticalScroll', true, "bool"],
  22267. ['tabRatio', 600, "int"],
  22268. ['visibility', 'visible']
  22269. //window{Height,Width}, {x,y}Window
  22270. ];
  22271. /* 18.2.19 (CT_Sheet) Defaults */
  22272. var SheetDef = [
  22273. //['state', 'visible']
  22274. ];
  22275. /* 18.2.2 (CT_CalcPr) Defaults */
  22276. var CalcPrDef = [
  22277. ['calcCompleted', 'true'],
  22278. ['calcMode', 'auto'],
  22279. ['calcOnSave', 'true'],
  22280. ['concurrentCalc', 'true'],
  22281. ['fullCalcOnLoad', 'false'],
  22282. ['fullPrecision', 'true'],
  22283. ['iterate', 'false'],
  22284. ['iterateCount', '100'],
  22285. ['iterateDelta', '0.001'],
  22286. ['refMode', 'A1']
  22287. ];
  22288. /* 18.2.3 (CT_CustomWorkbookView) Defaults */
  22289. /*var CustomWBViewDef = [
  22290. ['autoUpdate', 'false'],
  22291. ['changesSavedWin', 'false'],
  22292. ['includeHiddenRowCol', 'true'],
  22293. ['includePrintSettings', 'true'],
  22294. ['maximized', 'false'],
  22295. ['minimized', 'false'],
  22296. ['onlySync', 'false'],
  22297. ['personalView', 'false'],
  22298. ['showComments', 'commIndicator'],
  22299. ['showFormulaBar', 'true'],
  22300. ['showHorizontalScroll', 'true'],
  22301. ['showObjects', 'all'],
  22302. ['showSheetTabs', 'true'],
  22303. ['showStatusbar', 'true'],
  22304. ['showVerticalScroll', 'true'],
  22305. ['tabRatio', '600'],
  22306. ['xWindow', '0'],
  22307. ['yWindow', '0']
  22308. ];*/
  22309. function push_defaults_array(target, defaults) {
  22310. for(var j = 0; j != target.length; ++j) { var w = target[j];
  22311. for(var i=0; i != defaults.length; ++i) { var z = defaults[i];
  22312. if(w[z[0]] == null) w[z[0]] = z[1];
  22313. else switch(z[2]) {
  22314. case "bool": if(typeof w[z[0]] == "string") w[z[0]] = parsexmlbool(w[z[0]]); break;
  22315. case "int": if(typeof w[z[0]] == "string") w[z[0]] = parseInt(w[z[0]], 10); break;
  22316. }
  22317. }
  22318. }
  22319. }
  22320. function push_defaults(target, defaults) {
  22321. for(var i = 0; i != defaults.length; ++i) { var z = defaults[i];
  22322. if(target[z[0]] == null) target[z[0]] = z[1];
  22323. else switch(z[2]) {
  22324. case "bool": if(typeof target[z[0]] == "string") target[z[0]] = parsexmlbool(target[z[0]]); break;
  22325. case "int": if(typeof target[z[0]] == "string") target[z[0]] = parseInt(target[z[0]], 10); break;
  22326. }
  22327. }
  22328. }
  22329. function parse_wb_defaults(wb) {
  22330. push_defaults(wb.WBProps, WBPropsDef);
  22331. push_defaults(wb.CalcPr, CalcPrDef);
  22332. push_defaults_array(wb.WBView, WBViewDef);
  22333. push_defaults_array(wb.Sheets, SheetDef);
  22334. _ssfopts.date1904 = parsexmlbool(wb.WBProps.date1904);
  22335. }
  22336. function safe1904(wb) {
  22337. /* TODO: store date1904 somewhere else */
  22338. if(!wb.Workbook) return "false";
  22339. if(!wb.Workbook.WBProps) return "false";
  22340. return parsexmlbool(wb.Workbook.WBProps.date1904) ? "true" : "false";
  22341. }
  22342. var badchars = "][*?\/\\".split("");
  22343. function check_ws_name(n, safe) {
  22344. if(n.length > 31) { if(safe) return false; throw new Error("Sheet names cannot exceed 31 chars"); }
  22345. var _good = true;
  22346. badchars.forEach(function(c) {
  22347. if(n.indexOf(c) == -1) return;
  22348. if(!safe) throw new Error("Sheet name cannot contain : \\ / ? * [ ]");
  22349. _good = false;
  22350. });
  22351. return _good;
  22352. }
  22353. function check_wb_names(N, S, codes) {
  22354. N.forEach(function(n,i) {
  22355. check_ws_name(n);
  22356. for(var j = 0; j < i; ++j) if(n == N[j]) throw new Error("Duplicate Sheet Name: " + n);
  22357. if(codes) {
  22358. var cn = (S && S[i] && S[i].CodeName) || n;
  22359. if(cn.charCodeAt(0) == 95 && cn.length > 22) throw new Error("Bad Code Name: Worksheet" + cn);
  22360. }
  22361. });
  22362. }
  22363. function check_wb(wb) {
  22364. if(!wb || !wb.SheetNames || !wb.Sheets) throw new Error("Invalid Workbook");
  22365. if(!wb.SheetNames.length) throw new Error("Workbook is empty");
  22366. var Sheets = (wb.Workbook && wb.Workbook.Sheets) || [];
  22367. check_wb_names(wb.SheetNames, Sheets, !!wb.vbaraw);
  22368. for(var i = 0; i < wb.SheetNames.length; ++i) check_ws(wb.Sheets[wb.SheetNames[i]], wb.SheetNames[i], i);
  22369. /* TODO: validate workbook */
  22370. }
  22371. /* 18.2 Workbook */
  22372. var wbnsregex = /<\w+:workbook/;
  22373. function parse_wb_xml(data, opts) {
  22374. if(!data) throw new Error("Could not find file");
  22375. var wb = { AppVersion:{}, WBProps:{}, WBView:[], Sheets:[], CalcPr:{}, Names:[], xmlns: "" };
  22376. var pass = false, xmlns = "xmlns";
  22377. var dname = {}, dnstart = 0;
  22378. data.replace(tagregex, function xml_wb(x, idx) {
  22379. var y = parsexmltag(x);
  22380. switch(strip_ns(y[0])) {
  22381. case '<?xml': break;
  22382. /* 18.2.27 workbook CT_Workbook 1 */
  22383. case '<workbook':
  22384. if(x.match(wbnsregex)) xmlns = "xmlns" + x.match(/<(\w+):/)[1];
  22385. wb.xmlns = y[xmlns];
  22386. break;
  22387. case '</workbook>': break;
  22388. /* 18.2.13 fileVersion CT_FileVersion ? */
  22389. case '<fileVersion': delete y[0]; wb.AppVersion = y; break;
  22390. case '<fileVersion/>': case '</fileVersion>': break;
  22391. /* 18.2.12 fileSharing CT_FileSharing ? */
  22392. case '<fileSharing': case '<fileSharing/>': break;
  22393. /* 18.2.28 workbookPr CT_WorkbookPr ? */
  22394. case '<workbookPr':
  22395. case '<workbookPr/>':
  22396. WBPropsDef.forEach(function(w) {
  22397. if(y[w[0]] == null) return;
  22398. switch(w[2]) {
  22399. case "bool": wb.WBProps[w[0]] = parsexmlbool(y[w[0]]); break;
  22400. case "int": wb.WBProps[w[0]] = parseInt(y[w[0]], 10); break;
  22401. default: wb.WBProps[w[0]] = y[w[0]];
  22402. }
  22403. });
  22404. if(y.codeName) wb.WBProps.CodeName = y.codeName;
  22405. break;
  22406. case '</workbookPr>': break;
  22407. /* 18.2.29 workbookProtection CT_WorkbookProtection ? */
  22408. case '<workbookProtection': break;
  22409. case '<workbookProtection/>': break;
  22410. /* 18.2.1 bookViews CT_BookViews ? */
  22411. case '<bookViews': case '<bookViews>': case '</bookViews>': break;
  22412. /* 18.2.30 workbookView CT_BookView + */
  22413. case '<workbookView': case '<workbookView/>': delete y[0]; wb.WBView.push(y); break;
  22414. case '</workbookView>': break;
  22415. /* 18.2.20 sheets CT_Sheets 1 */
  22416. case '<sheets': case '<sheets>': case '</sheets>': break; // aggregate sheet
  22417. /* 18.2.19 sheet CT_Sheet + */
  22418. case '<sheet':
  22419. switch(y.state) {
  22420. case "hidden": y.Hidden = 1; break;
  22421. case "veryHidden": y.Hidden = 2; break;
  22422. default: y.Hidden = 0;
  22423. }
  22424. delete y.state;
  22425. y.name = unescapexml(utf8read(y.name));
  22426. delete y[0]; wb.Sheets.push(y); break;
  22427. case '</sheet>': break;
  22428. /* 18.2.15 functionGroups CT_FunctionGroups ? */
  22429. case '<functionGroups': case '<functionGroups/>': break;
  22430. /* 18.2.14 functionGroup CT_FunctionGroup + */
  22431. case '<functionGroup': break;
  22432. /* 18.2.9 externalReferences CT_ExternalReferences ? */
  22433. case '<externalReferences': case '</externalReferences>': case '<externalReferences>': break;
  22434. /* 18.2.8 externalReference CT_ExternalReference + */
  22435. case '<externalReference': break;
  22436. /* 18.2.6 definedNames CT_DefinedNames ? */
  22437. case '<definedNames/>': break;
  22438. case '<definedNames>': case '<definedNames': pass=true; break;
  22439. case '</definedNames>': pass=false; break;
  22440. /* 18.2.5 definedName CT_DefinedName + */
  22441. case '<definedName': {
  22442. dname = {};
  22443. dname.Name = utf8read(y.name);
  22444. if(y.comment) dname.Comment = y.comment;
  22445. if(y.localSheetId) dname.Sheet = +y.localSheetId;
  22446. if(parsexmlbool(y.hidden||"0")) dname.Hidden = true;
  22447. dnstart = idx + x.length;
  22448. } break;
  22449. case '</definedName>': {
  22450. dname.Ref = unescapexml(utf8read(data.slice(dnstart, idx)));
  22451. wb.Names.push(dname);
  22452. } break;
  22453. case '<definedName/>': break;
  22454. /* 18.2.2 calcPr CT_CalcPr ? */
  22455. case '<calcPr': delete y[0]; wb.CalcPr = y; break;
  22456. case '<calcPr/>': delete y[0]; wb.CalcPr = y; break;
  22457. case '</calcPr>': break;
  22458. /* 18.2.16 oleSize CT_OleSize ? (ref required) */
  22459. case '<oleSize': break;
  22460. /* 18.2.4 customWorkbookViews CT_CustomWorkbookViews ? */
  22461. case '<customWorkbookViews>': case '</customWorkbookViews>': case '<customWorkbookViews': break;
  22462. /* 18.2.3 customWorkbookView CT_CustomWorkbookView + */
  22463. case '<customWorkbookView': case '</customWorkbookView>': break;
  22464. /* 18.2.18 pivotCaches CT_PivotCaches ? */
  22465. case '<pivotCaches>': case '</pivotCaches>': case '<pivotCaches': break;
  22466. /* 18.2.17 pivotCache CT_PivotCache ? */
  22467. case '<pivotCache': break;
  22468. /* 18.2.21 smartTagPr CT_SmartTagPr ? */
  22469. case '<smartTagPr': case '<smartTagPr/>': break;
  22470. /* 18.2.23 smartTagTypes CT_SmartTagTypes ? */
  22471. case '<smartTagTypes': case '<smartTagTypes>': case '</smartTagTypes>': break;
  22472. /* 18.2.22 smartTagType CT_SmartTagType ? */
  22473. case '<smartTagType': break;
  22474. /* 18.2.24 webPublishing CT_WebPublishing ? */
  22475. case '<webPublishing': case '<webPublishing/>': break;
  22476. /* 18.2.11 fileRecoveryPr CT_FileRecoveryPr ? */
  22477. case '<fileRecoveryPr': case '<fileRecoveryPr/>': break;
  22478. /* 18.2.26 webPublishObjects CT_WebPublishObjects ? */
  22479. case '<webPublishObjects>': case '<webPublishObjects': case '</webPublishObjects>': break;
  22480. /* 18.2.25 webPublishObject CT_WebPublishObject ? */
  22481. case '<webPublishObject': break;
  22482. /* 18.2.10 extLst CT_ExtensionList ? */
  22483. case '<extLst': case '<extLst>': case '</extLst>': case '<extLst/>': break;
  22484. /* 18.2.7 ext CT_Extension + */
  22485. case '<ext': pass=true; break; //TODO: check with versions of excel
  22486. case '</ext>': pass=false; break;
  22487. /* Others */
  22488. case '<ArchID': break;
  22489. case '<AlternateContent':
  22490. case '<AlternateContent>': pass=true; break;
  22491. case '</AlternateContent>': pass=false; break;
  22492. /* TODO */
  22493. case '<revisionPtr': break;
  22494. default: if(!pass && opts.WTF) throw new Error('unrecognized ' + y[0] + ' in workbook');
  22495. }
  22496. return x;
  22497. });
  22498. if(XMLNS.main.indexOf(wb.xmlns) === -1) throw new Error("Unknown Namespace: " + wb.xmlns);
  22499. parse_wb_defaults(wb);
  22500. return wb;
  22501. }
  22502. var WB_XML_ROOT = writextag('workbook', null, {
  22503. 'xmlns': XMLNS.main[0],
  22504. //'xmlns:mx': XMLNS.mx,
  22505. //'xmlns:s': XMLNS.main[0],
  22506. 'xmlns:r': XMLNS.r
  22507. });
  22508. function write_wb_xml(wb) {
  22509. var o = [XML_HEADER];
  22510. o[o.length] = WB_XML_ROOT;
  22511. var write_names = (wb.Workbook && (wb.Workbook.Names||[]).length > 0);
  22512. /* fileVersion */
  22513. /* fileSharing */
  22514. var workbookPr = ({codeName:"ThisWorkbook"});
  22515. if(wb.Workbook && wb.Workbook.WBProps) {
  22516. WBPropsDef.forEach(function(x) {
  22517. if((wb.Workbook.WBProps[x[0]]) == null) return;
  22518. if((wb.Workbook.WBProps[x[0]]) == x[1]) return;
  22519. workbookPr[x[0]] = (wb.Workbook.WBProps[x[0]]);
  22520. });
  22521. if(wb.Workbook.WBProps.CodeName) { workbookPr.codeName = wb.Workbook.WBProps.CodeName; delete workbookPr.CodeName; }
  22522. }
  22523. o[o.length] = (writextag('workbookPr', null, workbookPr));
  22524. /* workbookProtection */
  22525. var sheets = wb.Workbook && wb.Workbook.Sheets || [];
  22526. var i = 0;
  22527. /* bookViews */
  22528. o[o.length] = "<sheets>";
  22529. for(i = 0; i != wb.SheetNames.length; ++i) {
  22530. var sht = ({name:escapexml(wb.SheetNames[i].slice(0,31))});
  22531. sht.sheetId = ""+(i+1);
  22532. sht["r:id"] = "rId"+(i+1);
  22533. if(sheets[i]) switch(sheets[i].Hidden) {
  22534. case 1: sht.state = "hidden"; break;
  22535. case 2: sht.state = "veryHidden"; break;
  22536. }
  22537. o[o.length] = (writextag('sheet',null,sht));
  22538. }
  22539. o[o.length] = "</sheets>";
  22540. /* functionGroups */
  22541. /* externalReferences */
  22542. if(write_names) {
  22543. o[o.length] = "<definedNames>";
  22544. if(wb.Workbook && wb.Workbook.Names) wb.Workbook.Names.forEach(function(n) {
  22545. var d = {name:n.Name};
  22546. if(n.Comment) d.comment = n.Comment;
  22547. if(n.Sheet != null) d.localSheetId = ""+n.Sheet;
  22548. if(n.Hidden) d.hidden = "1";
  22549. if(!n.Ref) return;
  22550. o[o.length] = writextag('definedName', String(n.Ref).replace(/</g, "&lt;").replace(/>/g, "&gt;"), d);
  22551. });
  22552. o[o.length] = "</definedNames>";
  22553. }
  22554. /* calcPr */
  22555. /* oleSize */
  22556. /* customWorkbookViews */
  22557. /* pivotCaches */
  22558. /* smartTagPr */
  22559. /* smartTagTypes */
  22560. /* webPublishing */
  22561. /* fileRecoveryPr */
  22562. /* webPublishObjects */
  22563. /* extLst */
  22564. if(o.length>2){ o[o.length] = '</workbook>'; o[1]=o[1].replace("/>",">"); }
  22565. return o.join("");
  22566. }
  22567. /* [MS-XLSB] 2.4.304 BrtBundleSh */
  22568. function parse_BrtBundleSh(data, length) {
  22569. var z = {};
  22570. z.Hidden = data.read_shift(4); //hsState ST_SheetState
  22571. z.iTabID = data.read_shift(4);
  22572. z.strRelID = parse_RelID(data,length-8);
  22573. z.name = parse_XLWideString(data);
  22574. return z;
  22575. }
  22576. function write_BrtBundleSh(data, o) {
  22577. if(!o) o = new_buf(127);
  22578. o.write_shift(4, data.Hidden);
  22579. o.write_shift(4, data.iTabID);
  22580. write_RelID(data.strRelID, o);
  22581. write_XLWideString(data.name.slice(0,31), o);
  22582. return o.length > o.l ? o.slice(0, o.l) : o;
  22583. }
  22584. /* [MS-XLSB] 2.4.815 BrtWbProp */
  22585. function parse_BrtWbProp(data, length) {
  22586. var o = ({});
  22587. var flags = data.read_shift(4);
  22588. o.defaultThemeVersion = data.read_shift(4);
  22589. var strName = (length > 8) ? parse_XLWideString(data) : "";
  22590. if(strName.length > 0) o.CodeName = strName;
  22591. o.autoCompressPictures = !!(flags & 0x10000);
  22592. o.backupFile = !!(flags & 0x40);
  22593. o.checkCompatibility = !!(flags & 0x1000);
  22594. o.date1904 = !!(flags & 0x01);
  22595. o.filterPrivacy = !!(flags & 0x08);
  22596. o.hidePivotFieldList = !!(flags & 0x400);
  22597. o.promptedSolutions = !!(flags & 0x10);
  22598. o.publishItems = !!(flags & 0x800);
  22599. o.refreshAllConnections = !!(flags & 0x40000);
  22600. o.saveExternalLinkValues = !!(flags & 0x80);
  22601. o.showBorderUnselectedTables = !!(flags & 0x04);
  22602. o.showInkAnnotation = !!(flags & 0x20);
  22603. o.showObjects = ["all", "placeholders", "none"][(flags >> 13) & 0x03];
  22604. o.showPivotChartFilter = !!(flags & 0x8000);
  22605. o.updateLinks = ["userSet", "never", "always"][(flags >> 8) & 0x03];
  22606. return o;
  22607. }
  22608. function write_BrtWbProp(data, o) {
  22609. if(!o) o = new_buf(72);
  22610. var flags = 0;
  22611. if(data) {
  22612. /* TODO: mirror parse_BrtWbProp fields */
  22613. if(data.filterPrivacy) flags |= 0x08;
  22614. }
  22615. o.write_shift(4, flags);
  22616. o.write_shift(4, 0);
  22617. write_XLSBCodeName(data && data.CodeName || "ThisWorkbook", o);
  22618. return o.slice(0, o.l);
  22619. }
  22620. function parse_BrtFRTArchID$(data, length) {
  22621. var o = {};
  22622. data.read_shift(4);
  22623. o.ArchID = data.read_shift(4);
  22624. data.l += length - 8;
  22625. return o;
  22626. }
  22627. /* [MS-XLSB] 2.4.687 BrtName */
  22628. function parse_BrtName(data, length, opts) {
  22629. var end = data.l + length;
  22630. data.l += 4; //var flags = data.read_shift(4);
  22631. data.l += 1; //var chKey = data.read_shift(1);
  22632. var itab = data.read_shift(4);
  22633. var name = parse_XLNameWideString(data);
  22634. var formula = parse_XLSBNameParsedFormula(data, 0, opts);
  22635. var comment = parse_XLNullableWideString(data);
  22636. //if(0 /* fProc */) {
  22637. // unusedstring1: XLNullableWideString
  22638. // description: XLNullableWideString
  22639. // helpTopic: XLNullableWideString
  22640. // unusedstring2: XLNullableWideString
  22641. //}
  22642. data.l = end;
  22643. var out = ({Name:name, Ptg:formula});
  22644. if(itab < 0xFFFFFFF) out.Sheet = itab;
  22645. if(comment) out.Comment = comment;
  22646. return out;
  22647. }
  22648. /* [MS-XLSB] 2.1.7.61 Workbook */
  22649. function parse_wb_bin(data, opts) {
  22650. var wb = { AppVersion:{}, WBProps:{}, WBView:[], Sheets:[], CalcPr:{}, xmlns: "" };
  22651. var state = [];
  22652. var pass = false;
  22653. if(!opts) opts = {};
  22654. opts.biff = 12;
  22655. var Names = [];
  22656. var supbooks = ([[]]);
  22657. supbooks.SheetNames = [];
  22658. supbooks.XTI = [];
  22659. recordhopper(data, function hopper_wb(val, R_n, RT) {
  22660. switch(RT) {
  22661. case 0x009C: /* 'BrtBundleSh' */
  22662. supbooks.SheetNames.push(val.name);
  22663. wb.Sheets.push(val); break;
  22664. case 0x0099: /* 'BrtWbProp' */
  22665. wb.WBProps = val; break;
  22666. case 0x0027: /* 'BrtName' */
  22667. if(val.Sheet != null) opts.SID = val.Sheet;
  22668. val.Ref = stringify_formula(val.Ptg, null, null, supbooks, opts);
  22669. delete opts.SID;
  22670. delete val.Ptg;
  22671. Names.push(val);
  22672. break;
  22673. case 0x040C: /* 'BrtNameExt' */ break;
  22674. case 0x0165: /* 'BrtSupSelf' */
  22675. case 0x0166: /* 'BrtSupSame' */
  22676. case 0x0163: /* 'BrtSupBookSrc' */
  22677. case 0x029B: /* 'BrtSupAddin' */
  22678. if(!supbooks[0].length) supbooks[0] = [RT, val];
  22679. else supbooks.push([RT, val]);
  22680. supbooks[supbooks.length - 1].XTI = [];
  22681. break;
  22682. case 0x016A: /* 'BrtExternSheet' */
  22683. if(supbooks.length === 0) { supbooks[0] = []; supbooks[0].XTI = []; }
  22684. supbooks[supbooks.length - 1].XTI = supbooks[supbooks.length - 1].XTI.concat(val);
  22685. supbooks.XTI = supbooks.XTI.concat(val);
  22686. break;
  22687. case 0x0169: /* 'BrtPlaceholderName' */
  22688. break;
  22689. /* case 'BrtModelTimeGroupingCalcCol' */
  22690. case 0x0C00: /* 'BrtUid' */
  22691. case 0x0C01: /* 'BrtRevisionPtr' */
  22692. case 0x0817: /* 'BrtAbsPath15' */
  22693. case 0x0216: /* 'BrtBookProtection' */
  22694. case 0x02A5: /* 'BrtBookProtectionIso' */
  22695. case 0x009E: /* 'BrtBookView' */
  22696. case 0x009D: /* 'BrtCalcProp' */
  22697. case 0x0262: /* 'BrtCrashRecErr' */
  22698. case 0x0802: /* 'BrtDecoupledPivotCacheID' */
  22699. case 0x009B: /* 'BrtFileRecover' */
  22700. case 0x0224: /* 'BrtFileSharing' */
  22701. case 0x02A4: /* 'BrtFileSharingIso' */
  22702. case 0x0080: /* 'BrtFileVersion' */
  22703. case 0x0299: /* 'BrtFnGroup' */
  22704. case 0x0850: /* 'BrtModelRelationship' */
  22705. case 0x084D: /* 'BrtModelTable' */
  22706. case 0x0225: /* 'BrtOleSize' */
  22707. case 0x0805: /* 'BrtPivotTableRef' */
  22708. case 0x0254: /* 'BrtSmartTagType' */
  22709. case 0x081C: /* 'BrtTableSlicerCacheID' */
  22710. case 0x081B: /* 'BrtTableSlicerCacheIDs' */
  22711. case 0x0822: /* 'BrtTimelineCachePivotCacheID' */
  22712. case 0x018D: /* 'BrtUserBookView' */
  22713. case 0x009A: /* 'BrtWbFactoid' */
  22714. case 0x045D: /* 'BrtWbProp14' */
  22715. case 0x0229: /* 'BrtWebOpt' */
  22716. case 0x082B: /* 'BrtWorkBookPr15' */
  22717. break;
  22718. case 0x0023: /* 'BrtFRTBegin' */
  22719. state.push(R_n); pass = true; break;
  22720. case 0x0024: /* 'BrtFRTEnd' */
  22721. state.pop(); pass = false; break;
  22722. case 0x0025: /* 'BrtACBegin' */
  22723. state.push(R_n); pass = true; break;
  22724. case 0x0026: /* 'BrtACEnd' */
  22725. state.pop(); pass = false; break;
  22726. case 0x0010: /* 'BrtFRTArchID$' */ break;
  22727. default:
  22728. if((R_n||"").indexOf("Begin") > 0){/* empty */}
  22729. else if((R_n||"").indexOf("End") > 0){/* empty */}
  22730. else if(!pass || (opts.WTF && state[state.length-1] != "BrtACBegin" && state[state.length-1] != "BrtFRTBegin")) throw new Error("Unexpected record " + RT + " " + R_n);
  22731. }
  22732. }, opts);
  22733. parse_wb_defaults(wb);
  22734. // $FlowIgnore
  22735. wb.Names = Names;
  22736. (wb).supbooks = supbooks;
  22737. return wb;
  22738. }
  22739. function write_BUNDLESHS(ba, wb) {
  22740. write_record(ba, "BrtBeginBundleShs");
  22741. for(var idx = 0; idx != wb.SheetNames.length; ++idx) {
  22742. var viz = wb.Workbook && wb.Workbook.Sheets && wb.Workbook.Sheets[idx] && wb.Workbook.Sheets[idx].Hidden || 0;
  22743. var d = { Hidden: viz, iTabID: idx+1, strRelID: 'rId' + (idx+1), name: wb.SheetNames[idx] };
  22744. write_record(ba, "BrtBundleSh", write_BrtBundleSh(d));
  22745. }
  22746. write_record(ba, "BrtEndBundleShs");
  22747. }
  22748. /* [MS-XLSB] 2.4.649 BrtFileVersion */
  22749. function write_BrtFileVersion(data, o) {
  22750. if(!o) o = new_buf(127);
  22751. for(var i = 0; i != 4; ++i) o.write_shift(4, 0);
  22752. write_XLWideString("SheetJS", o);
  22753. write_XLWideString(XLSX.version, o);
  22754. write_XLWideString(XLSX.version, o);
  22755. write_XLWideString("7262", o);
  22756. o.length = o.l;
  22757. return o.length > o.l ? o.slice(0, o.l) : o;
  22758. }
  22759. /* [MS-XLSB] 2.4.301 BrtBookView */
  22760. function write_BrtBookView(idx, o) {
  22761. if(!o) o = new_buf(29);
  22762. o.write_shift(-4, 0);
  22763. o.write_shift(-4, 460);
  22764. o.write_shift(4, 28800);
  22765. o.write_shift(4, 17600);
  22766. o.write_shift(4, 500);
  22767. o.write_shift(4, idx);
  22768. o.write_shift(4, idx);
  22769. var flags = 0x78;
  22770. o.write_shift(1, flags);
  22771. return o.length > o.l ? o.slice(0, o.l) : o;
  22772. }
  22773. function write_BOOKVIEWS(ba, wb) {
  22774. /* required if hidden tab appears before visible tab */
  22775. if(!wb.Workbook || !wb.Workbook.Sheets) return;
  22776. var sheets = wb.Workbook.Sheets;
  22777. var i = 0, vistab = -1, hidden = -1;
  22778. for(; i < sheets.length; ++i) {
  22779. if(!sheets[i] || !sheets[i].Hidden && vistab == -1) vistab = i;
  22780. else if(sheets[i].Hidden == 1 && hidden == -1) hidden = i;
  22781. }
  22782. if(hidden > vistab) return;
  22783. write_record(ba, "BrtBeginBookViews");
  22784. write_record(ba, "BrtBookView", write_BrtBookView(vistab));
  22785. /* 1*(BrtBookView *FRT) */
  22786. write_record(ba, "BrtEndBookViews");
  22787. }
  22788. /* [MS-XLSB] 2.4.305 BrtCalcProp */
  22789. /*function write_BrtCalcProp(data, o) {
  22790. if(!o) o = new_buf(26);
  22791. o.write_shift(4,0); // force recalc
  22792. o.write_shift(4,1);
  22793. o.write_shift(4,0);
  22794. write_Xnum(0, o);
  22795. o.write_shift(-4, 1023);
  22796. o.write_shift(1, 0x33);
  22797. o.write_shift(1, 0x00);
  22798. return o;
  22799. }*/
  22800. /* [MS-XLSB] 2.4.646 BrtFileRecover */
  22801. /*function write_BrtFileRecover(data, o) {
  22802. if(!o) o = new_buf(1);
  22803. o.write_shift(1,0);
  22804. return o;
  22805. }*/
  22806. /* [MS-XLSB] 2.1.7.61 Workbook */
  22807. function write_wb_bin(wb, opts) {
  22808. var ba = buf_array();
  22809. write_record(ba, "BrtBeginBook");
  22810. write_record(ba, "BrtFileVersion", write_BrtFileVersion());
  22811. /* [[BrtFileSharingIso] BrtFileSharing] */
  22812. write_record(ba, "BrtWbProp", write_BrtWbProp(wb.Workbook && wb.Workbook.WBProps || null));
  22813. /* [ACABSPATH] */
  22814. /* [[BrtBookProtectionIso] BrtBookProtection] */
  22815. write_BOOKVIEWS(ba, wb, opts);
  22816. write_BUNDLESHS(ba, wb, opts);
  22817. /* [FNGROUP] */
  22818. /* [EXTERNALS] */
  22819. /* *BrtName */
  22820. /* write_record(ba, "BrtCalcProp", write_BrtCalcProp()); */
  22821. /* [BrtOleSize] */
  22822. /* *(BrtUserBookView *FRT) */
  22823. /* [PIVOTCACHEIDS] */
  22824. /* [BrtWbFactoid] */
  22825. /* [SMARTTAGTYPES] */
  22826. /* [BrtWebOpt] */
  22827. /* write_record(ba, "BrtFileRecover", write_BrtFileRecover()); */
  22828. /* [WEBPUBITEMS] */
  22829. /* [CRERRS] */
  22830. /* FRTWORKBOOK */
  22831. write_record(ba, "BrtEndBook");
  22832. return ba.end();
  22833. }
  22834. function parse_wb(data, name, opts) {
  22835. if(name.slice(-4)===".bin") return parse_wb_bin((data), opts);
  22836. return parse_wb_xml((data), opts);
  22837. }
  22838. function parse_ws(data, name, idx, opts, rels, wb, themes, styles) {
  22839. if(name.slice(-4)===".bin") return parse_ws_bin((data), opts, idx, rels, wb, themes, styles);
  22840. return parse_ws_xml((data), opts, idx, rels, wb, themes, styles);
  22841. }
  22842. function parse_cs(data, name, idx, opts, rels, wb, themes, styles) {
  22843. if(name.slice(-4)===".bin") return parse_cs_bin((data), opts, idx, rels, wb, themes, styles);
  22844. return parse_cs_xml((data), opts, idx, rels, wb, themes, styles);
  22845. }
  22846. function parse_ms(data, name, idx, opts, rels, wb, themes, styles) {
  22847. if(name.slice(-4)===".bin") return parse_ms_bin((data), opts, idx, rels, wb, themes, styles);
  22848. return parse_ms_xml((data), opts, idx, rels, wb, themes, styles);
  22849. }
  22850. function parse_ds(data, name, idx, opts, rels, wb, themes, styles) {
  22851. if(name.slice(-4)===".bin") return parse_ds_bin((data), opts, idx, rels, wb, themes, styles);
  22852. return parse_ds_xml((data), opts, idx, rels, wb, themes, styles);
  22853. }
  22854. function parse_sty(data, name, themes, opts) {
  22855. if(name.slice(-4)===".bin") return parse_sty_bin((data), themes, opts);
  22856. return parse_sty_xml((data), themes, opts);
  22857. }
  22858. function parse_theme(data, name, opts) {
  22859. return parse_theme_xml(data, opts);
  22860. }
  22861. function parse_sst(data, name, opts) {
  22862. if(name.slice(-4)===".bin") return parse_sst_bin((data), opts);
  22863. return parse_sst_xml((data), opts);
  22864. }
  22865. function parse_cmnt(data, name, opts) {
  22866. if(name.slice(-4)===".bin") return parse_comments_bin((data), opts);
  22867. return parse_comments_xml((data), opts);
  22868. }
  22869. function parse_cc(data, name, opts) {
  22870. if(name.slice(-4)===".bin") return parse_cc_bin((data), name, opts);
  22871. return parse_cc_xml((data), name, opts);
  22872. }
  22873. function parse_xlink(data, name, opts) {
  22874. if(name.slice(-4)===".bin") return parse_xlink_bin((data), name, opts);
  22875. return parse_xlink_xml((data), name, opts);
  22876. }
  22877. function write_wb(wb, name, opts) {
  22878. return (name.slice(-4)===".bin" ? write_wb_bin : write_wb_xml)(wb, opts);
  22879. }
  22880. function write_ws(data, name, opts, wb, rels) {
  22881. return (name.slice(-4)===".bin" ? write_ws_bin : write_ws_xml)(data, opts, wb, rels);
  22882. }
  22883. // eslint-disable-next-line no-unused-vars
  22884. function write_cs(data, name, opts, wb, rels) {
  22885. return (name.slice(-4)===".bin" ? write_cs_bin : write_cs_xml)(data, opts, wb, rels);
  22886. }
  22887. function write_sty(data, name, opts) {
  22888. return (name.slice(-4)===".bin" ? write_sty_bin : write_sty_xml)(data, opts);
  22889. }
  22890. function write_sst(data, name, opts) {
  22891. return (name.slice(-4)===".bin" ? write_sst_bin : write_sst_xml)(data, opts);
  22892. }
  22893. function write_cmnt(data, name, opts) {
  22894. return (name.slice(-4)===".bin" ? write_comments_bin : write_comments_xml)(data, opts);
  22895. }
  22896. /*
  22897. function write_cc(data, name:string, opts) {
  22898. return (name.slice(-4)===".bin" ? write_cc_bin : write_cc_xml)(data, opts);
  22899. }
  22900. */
  22901. var attregexg2=/([\w:]+)=((?:")([^"]*)(?:")|(?:')([^']*)(?:'))/g;
  22902. var attregex2=/([\w:]+)=((?:")(?:[^"]*)(?:")|(?:')(?:[^']*)(?:'))/;
  22903. var _chr = function(c) { return String.fromCharCode(c); };
  22904. function xlml_parsexmltag(tag, skip_root) {
  22905. var words = tag.split(/\s+/);
  22906. var z = ([]); if(!skip_root) z[0] = words[0];
  22907. if(words.length === 1) return z;
  22908. var m = tag.match(attregexg2), y, j, w, i;
  22909. if(m) for(i = 0; i != m.length; ++i) {
  22910. y = m[i].match(attregex2);
  22911. if((j=y[1].indexOf(":")) === -1) z[y[1]] = y[2].slice(1,y[2].length-1);
  22912. else {
  22913. if(y[1].slice(0,6) === "xmlns:") w = "xmlns"+y[1].slice(6);
  22914. else w = y[1].slice(j+1);
  22915. z[w] = y[2].slice(1,y[2].length-1);
  22916. }
  22917. }
  22918. return z;
  22919. }
  22920. function xlml_parsexmltagobj(tag) {
  22921. var words = tag.split(/\s+/);
  22922. var z = {};
  22923. if(words.length === 1) return z;
  22924. var m = tag.match(attregexg2), y, j, w, i;
  22925. if(m) for(i = 0; i != m.length; ++i) {
  22926. y = m[i].match(attregex2);
  22927. if((j=y[1].indexOf(":")) === -1) z[y[1]] = y[2].slice(1,y[2].length-1);
  22928. else {
  22929. if(y[1].slice(0,6) === "xmlns:") w = "xmlns"+y[1].slice(6);
  22930. else w = y[1].slice(j+1);
  22931. z[w] = y[2].slice(1,y[2].length-1);
  22932. }
  22933. }
  22934. return z;
  22935. }
  22936. // ----
  22937. function xlml_format(format, value) {
  22938. var fmt = XLMLFormatMap[format] || unescapexml(format);
  22939. if(fmt === "General") return SSF._general(value);
  22940. return SSF.format(fmt, value);
  22941. }
  22942. function xlml_set_custprop(Custprops, key, cp, val) {
  22943. var oval = val;
  22944. switch((cp[0].match(/dt:dt="([\w.]+)"/)||["",""])[1]) {
  22945. case "boolean": oval = parsexmlbool(val); break;
  22946. case "i2": case "int": oval = parseInt(val, 10); break;
  22947. case "r4": case "float": oval = parseFloat(val); break;
  22948. case "date": case "dateTime.tz": oval = parseDate(val); break;
  22949. case "i8": case "string": case "fixed": case "uuid": case "bin.base64": break;
  22950. default: throw new Error("bad custprop:" + cp[0]);
  22951. }
  22952. Custprops[unescapexml(key)] = oval;
  22953. }
  22954. function safe_format_xlml(cell, nf, o) {
  22955. if(cell.t === 'z') return;
  22956. if(!o || o.cellText !== false) try {
  22957. if(cell.t === 'e') { cell.w = cell.w || BErr[cell.v]; }
  22958. else if(nf === "General") {
  22959. if(cell.t === 'n') {
  22960. if((cell.v|0) === cell.v) cell.w = SSF._general_int(cell.v);
  22961. else cell.w = SSF._general_num(cell.v);
  22962. }
  22963. else cell.w = SSF._general(cell.v);
  22964. }
  22965. else cell.w = xlml_format(nf||"General", cell.v);
  22966. } catch(e) { if(o.WTF) throw e; }
  22967. try {
  22968. var z = XLMLFormatMap[nf]||nf||"General";
  22969. if(o.cellNF) cell.z = z;
  22970. if(o.cellDates && cell.t == 'n' && SSF.is_date(z)) {
  22971. var _d = SSF.parse_date_code(cell.v); if(_d) { cell.t = 'd'; cell.v = new Date(_d.y, _d.m-1,_d.d,_d.H,_d.M,_d.S,_d.u); }
  22972. }
  22973. } catch(e) { if(o.WTF) throw e; }
  22974. }
  22975. function process_style_xlml(styles, stag, opts) {
  22976. if(opts.cellStyles) {
  22977. if(stag.Interior) {
  22978. var I = stag.Interior;
  22979. if(I.Pattern) I.patternType = XLMLPatternTypeMap[I.Pattern] || I.Pattern;
  22980. }
  22981. }
  22982. styles[stag.ID] = stag;
  22983. }
  22984. /* TODO: there must exist some form of OSP-blessed spec */
  22985. function parse_xlml_data(xml, ss, data, cell, base, styles, csty, row, arrayf, o) {
  22986. var nf = "General", sid = cell.StyleID, S = {}; o = o || {};
  22987. var interiors = [];
  22988. var i = 0;
  22989. if(sid === undefined && row) sid = row.StyleID;
  22990. if(sid === undefined && csty) sid = csty.StyleID;
  22991. while(styles[sid] !== undefined) {
  22992. if(styles[sid].nf) nf = styles[sid].nf;
  22993. if(styles[sid].Interior) interiors.push(styles[sid].Interior);
  22994. if(!styles[sid].Parent) break;
  22995. sid = styles[sid].Parent;
  22996. }
  22997. switch(data.Type) {
  22998. case 'Boolean':
  22999. cell.t = 'b';
  23000. cell.v = parsexmlbool(xml);
  23001. break;
  23002. case 'String':
  23003. cell.t = 's'; cell.r = xlml_fixstr(unescapexml(xml));
  23004. cell.v = xml.indexOf("<") > -1 ? unescapexml(ss) : cell.r;
  23005. break;
  23006. case 'DateTime':
  23007. if(xml.slice(-1) != "Z") xml += "Z";
  23008. cell.v = (parseDate(xml) - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
  23009. if(cell.v !== cell.v) cell.v = unescapexml(xml);
  23010. else if(cell.v<60) cell.v = cell.v -1;
  23011. if(!nf || nf == "General") nf = "yyyy-mm-dd";
  23012. /* falls through */
  23013. case 'Number':
  23014. if(cell.v === undefined) cell.v=+xml;
  23015. if(!cell.t) cell.t = 'n';
  23016. break;
  23017. case 'Error': cell.t = 'e'; cell.v = RBErr[xml]; if(o.cellText !== false) cell.w = xml; break;
  23018. default: cell.t = 's'; cell.v = xlml_fixstr(ss||xml); break;
  23019. }
  23020. safe_format_xlml(cell, nf, o);
  23021. if(o.cellFormula !== false) {
  23022. if(cell.Formula) {
  23023. var fstr = unescapexml(cell.Formula);
  23024. /* strictly speaking, the leading = is required but some writers omit */
  23025. if(fstr.charCodeAt(0) == 61 /* = */) fstr = fstr.slice(1);
  23026. cell.f = rc_to_a1(fstr, base);
  23027. delete cell.Formula;
  23028. if(cell.ArrayRange == "RC") cell.F = rc_to_a1("RC:RC", base);
  23029. else if(cell.ArrayRange) {
  23030. cell.F = rc_to_a1(cell.ArrayRange, base);
  23031. arrayf.push([safe_decode_range(cell.F), cell.F]);
  23032. }
  23033. } else {
  23034. for(i = 0; i < arrayf.length; ++i)
  23035. if(base.r >= arrayf[i][0].s.r && base.r <= arrayf[i][0].e.r)
  23036. if(base.c >= arrayf[i][0].s.c && base.c <= arrayf[i][0].e.c)
  23037. cell.F = arrayf[i][1];
  23038. }
  23039. }
  23040. if(o.cellStyles) {
  23041. interiors.forEach(function(x) {
  23042. if(!S.patternType && x.patternType) S.patternType = x.patternType;
  23043. });
  23044. cell.s = S;
  23045. }
  23046. if(cell.StyleID !== undefined) cell.ixfe = cell.StyleID;
  23047. }
  23048. function xlml_clean_comment(comment) {
  23049. comment.t = comment.v || "";
  23050. comment.t = comment.t.replace(/\r\n/g,"\n").replace(/\r/g,"\n");
  23051. comment.v = comment.w = comment.ixfe = undefined;
  23052. }
  23053. function xlml_normalize(d) {
  23054. if(has_buf && Buffer.isBuffer(d)) return d.toString('utf8');
  23055. if(typeof d === 'string') return d;
  23056. /* duktape */
  23057. if(typeof Uint8Array !== 'undefined' && d instanceof Uint8Array) return utf8read(a2s(ab2a(d)));
  23058. throw new Error("Bad input format: expected Buffer or string");
  23059. }
  23060. /* TODO: Everything */
  23061. /* UOS uses CJK in tags */
  23062. var xlmlregex = /<(\/?)([^\s?>!\/:]*:|)([^\s?>:\/]+)[^>]*>/mg;
  23063. //var xlmlregex = /<(\/?)([a-z0-9]*:|)(\w+)[^>]*>/mg;
  23064. function parse_xlml_xml(d, _opts) {
  23065. var opts = _opts || {};
  23066. make_ssf(SSF);
  23067. var str = debom(xlml_normalize(d));
  23068. if(opts.type == 'binary' || opts.type == 'array' || opts.type == 'base64') {
  23069. if(typeof cptable !== 'undefined') str = cptable.utils.decode(65001, char_codes(str));
  23070. else str = utf8read(str);
  23071. }
  23072. var opening = str.slice(0, 1024).toLowerCase(), ishtml = false;
  23073. if(opening.indexOf("<?xml") == -1) ["html", "table", "head", "meta", "script", "style", "div"].forEach(function(tag) { if(opening.indexOf("<" + tag) >= 0) ishtml = true; });
  23074. if(ishtml) return HTML_.to_workbook(str, opts);
  23075. var Rn;
  23076. var state = [], tmp;
  23077. if(DENSE != null && opts.dense == null) opts.dense = DENSE;
  23078. var sheets = {}, sheetnames = [], cursheet = (opts.dense ? [] : {}), sheetname = "";
  23079. var table = {}, cell = ({}), row = {};// eslint-disable-line no-unused-vars
  23080. var dtag = xlml_parsexmltag('<Data ss:Type="String">'), didx = 0;
  23081. var c = 0, r = 0;
  23082. var refguess = {s: {r:2000000, c:2000000}, e: {r:0, c:0} };
  23083. var styles = {}, stag = {};
  23084. var ss = "", fidx = 0;
  23085. var merges = [];
  23086. var Props = {}, Custprops = {}, pidx = 0, cp = [];
  23087. var comments = [], comment = ({});
  23088. var cstys = [], csty, seencol = false;
  23089. var arrayf = [];
  23090. var rowinfo = [], rowobj = {}, cc = 0, rr = 0;
  23091. var Workbook = ({ Sheets:[], WBProps:{date1904:false} }), wsprops = {};
  23092. xlmlregex.lastIndex = 0;
  23093. str = str.replace(/<!--([\s\S]*?)-->/mg,"");
  23094. while((Rn = xlmlregex.exec(str))) switch(Rn[3]) {
  23095. case 'Data':
  23096. if(state[state.length-1][1]) break;
  23097. if(Rn[1]==='/') parse_xlml_data(str.slice(didx, Rn.index), ss, dtag, state[state.length-1][0]=="Comment"?comment:cell, {c:c,r:r}, styles, cstys[c], row, arrayf, opts);
  23098. else { ss = ""; dtag = xlml_parsexmltag(Rn[0]); didx = Rn.index + Rn[0].length; }
  23099. break;
  23100. case 'Cell':
  23101. if(Rn[1]==='/'){
  23102. if(comments.length > 0) cell.c = comments;
  23103. if((!opts.sheetRows || opts.sheetRows > r) && cell.v !== undefined) {
  23104. if(opts.dense) {
  23105. if(!cursheet[r]) cursheet[r] = [];
  23106. cursheet[r][c] = cell;
  23107. } else cursheet[encode_col(c) + encode_row(r)] = cell;
  23108. }
  23109. if(cell.HRef) {
  23110. cell.l = ({Target:cell.HRef});
  23111. if(cell.HRefScreenTip) cell.l.Tooltip = cell.HRefScreenTip;
  23112. delete cell.HRef; delete cell.HRefScreenTip;
  23113. }
  23114. if(cell.MergeAcross || cell.MergeDown) {
  23115. cc = c + (parseInt(cell.MergeAcross,10)|0);
  23116. rr = r + (parseInt(cell.MergeDown,10)|0);
  23117. merges.push({s:{c:c,r:r},e:{c:cc,r:rr}});
  23118. }
  23119. if(!opts.sheetStubs) { if(cell.MergeAcross) c = cc + 1; else ++c; }
  23120. else if(cell.MergeAcross || cell.MergeDown) {
  23121. for(var cma = c; cma <= cc; ++cma) {
  23122. for(var cmd = r; cmd <= rr; ++cmd) {
  23123. if(cma > c || cmd > r) {
  23124. if(opts.dense) {
  23125. if(!cursheet[cmd]) cursheet[cmd] = [];
  23126. cursheet[cmd][cma] = {t:'z'};
  23127. } else cursheet[encode_col(cma) + encode_row(cmd)] = {t:'z'};
  23128. }
  23129. }
  23130. }
  23131. c = cc + 1;
  23132. }
  23133. else ++c;
  23134. } else {
  23135. cell = xlml_parsexmltagobj(Rn[0]);
  23136. if(cell.Index) c = +cell.Index - 1;
  23137. if(c < refguess.s.c) refguess.s.c = c;
  23138. if(c > refguess.e.c) refguess.e.c = c;
  23139. if(Rn[0].slice(-2) === "/>") ++c;
  23140. comments = [];
  23141. }
  23142. break;
  23143. case 'Row':
  23144. if(Rn[1]==='/' || Rn[0].slice(-2) === "/>") {
  23145. if(r < refguess.s.r) refguess.s.r = r;
  23146. if(r > refguess.e.r) refguess.e.r = r;
  23147. if(Rn[0].slice(-2) === "/>") {
  23148. row = xlml_parsexmltag(Rn[0]);
  23149. if(row.Index) r = +row.Index - 1;
  23150. }
  23151. c = 0; ++r;
  23152. } else {
  23153. row = xlml_parsexmltag(Rn[0]);
  23154. if(row.Index) r = +row.Index - 1;
  23155. rowobj = {};
  23156. if(row.AutoFitHeight == "0" || row.Height) {
  23157. rowobj.hpx = parseInt(row.Height, 10); rowobj.hpt = px2pt(rowobj.hpx);
  23158. rowinfo[r] = rowobj;
  23159. }
  23160. if(row.Hidden == "1") { rowobj.hidden = true; rowinfo[r] = rowobj; }
  23161. }
  23162. break;
  23163. case 'Worksheet': /* TODO: read range from FullRows/FullColumns */
  23164. if(Rn[1]==='/'){
  23165. if((tmp=state.pop())[0]!==Rn[3]) throw new Error("Bad state: "+tmp.join("|"));
  23166. sheetnames.push(sheetname);
  23167. if(refguess.s.r <= refguess.e.r && refguess.s.c <= refguess.e.c) {
  23168. cursheet["!ref"] = encode_range(refguess);
  23169. if(opts.sheetRows && opts.sheetRows <= refguess.e.r) {
  23170. cursheet["!fullref"] = cursheet["!ref"];
  23171. refguess.e.r = opts.sheetRows - 1;
  23172. cursheet["!ref"] = encode_range(refguess);
  23173. }
  23174. }
  23175. if(merges.length) cursheet["!merges"] = merges;
  23176. if(cstys.length > 0) cursheet["!cols"] = cstys;
  23177. if(rowinfo.length > 0) cursheet["!rows"] = rowinfo;
  23178. sheets[sheetname] = cursheet;
  23179. } else {
  23180. refguess = {s: {r:2000000, c:2000000}, e: {r:0, c:0} };
  23181. r = c = 0;
  23182. state.push([Rn[3], false]);
  23183. tmp = xlml_parsexmltag(Rn[0]);
  23184. sheetname = unescapexml(tmp.Name);
  23185. cursheet = (opts.dense ? [] : {});
  23186. merges = [];
  23187. arrayf = [];
  23188. rowinfo = [];
  23189. wsprops = {name:sheetname, Hidden:0};
  23190. Workbook.Sheets.push(wsprops);
  23191. }
  23192. break;
  23193. case 'Table':
  23194. if(Rn[1]==='/'){if((tmp=state.pop())[0]!==Rn[3]) throw new Error("Bad state: "+tmp.join("|"));}
  23195. else if(Rn[0].slice(-2) == "/>") break;
  23196. else {
  23197. table = xlml_parsexmltag(Rn[0]);
  23198. state.push([Rn[3], false]);
  23199. cstys = []; seencol = false;
  23200. }
  23201. break;
  23202. case 'Style':
  23203. if(Rn[1]==='/') process_style_xlml(styles, stag, opts);
  23204. else stag = xlml_parsexmltag(Rn[0]);
  23205. break;
  23206. case 'NumberFormat':
  23207. stag.nf = unescapexml(xlml_parsexmltag(Rn[0]).Format || "General");
  23208. if(XLMLFormatMap[stag.nf]) stag.nf = XLMLFormatMap[stag.nf];
  23209. for(var ssfidx = 0; ssfidx != 0x188; ++ssfidx) if(SSF._table[ssfidx] == stag.nf) break;
  23210. if(ssfidx == 0x188) for(ssfidx = 0x39; ssfidx != 0x188; ++ssfidx) if(SSF._table[ssfidx] == null) { SSF.load(stag.nf, ssfidx); break; }
  23211. break;
  23212. case 'Column':
  23213. if(state[state.length-1][0] !== 'Table') break;
  23214. csty = xlml_parsexmltag(Rn[0]);
  23215. if(csty.Hidden) { csty.hidden = true; delete csty.Hidden; }
  23216. if(csty.Width) csty.wpx = parseInt(csty.Width, 10);
  23217. if(!seencol && csty.wpx > 10) {
  23218. seencol = true; MDW = DEF_MDW; //find_mdw_wpx(csty.wpx);
  23219. for(var _col = 0; _col < cstys.length; ++_col) if(cstys[_col]) process_col(cstys[_col]);
  23220. }
  23221. if(seencol) process_col(csty);
  23222. cstys[(csty.Index-1||cstys.length)] = csty;
  23223. for(var i = 0; i < +csty.Span; ++i) cstys[cstys.length] = dup(csty);
  23224. break;
  23225. case 'NamedRange':
  23226. if(!Workbook.Names) Workbook.Names = [];
  23227. var _NamedRange = parsexmltag(Rn[0]);
  23228. var _DefinedName = ({
  23229. Name: _NamedRange.Name,
  23230. Ref: rc_to_a1(_NamedRange.RefersTo.slice(1), {r:0, c:0})
  23231. });
  23232. if(Workbook.Sheets.length>0) _DefinedName.Sheet=Workbook.Sheets.length-1;
  23233. Workbook.Names.push(_DefinedName);
  23234. break;
  23235. case 'NamedCell': break;
  23236. case 'B': break;
  23237. case 'I': break;
  23238. case 'U': break;
  23239. case 'S': break;
  23240. case 'Sub': break;
  23241. case 'Sup': break;
  23242. case 'Span': break;
  23243. case 'Border': break;
  23244. case 'Alignment': break;
  23245. case 'Borders': break;
  23246. case 'Font':
  23247. if(Rn[0].slice(-2) === "/>") break;
  23248. else if(Rn[1]==="/") ss += str.slice(fidx, Rn.index);
  23249. else fidx = Rn.index + Rn[0].length;
  23250. break;
  23251. case 'Interior':
  23252. if(!opts.cellStyles) break;
  23253. stag.Interior = xlml_parsexmltag(Rn[0]);
  23254. break;
  23255. case 'Protection': break;
  23256. case 'Author':
  23257. case 'Title':
  23258. case 'Description':
  23259. case 'Created':
  23260. case 'Keywords':
  23261. case 'Subject':
  23262. case 'Category':
  23263. case 'Company':
  23264. case 'LastAuthor':
  23265. case 'LastSaved':
  23266. case 'LastPrinted':
  23267. case 'Version':
  23268. case 'Revision':
  23269. case 'TotalTime':
  23270. case 'HyperlinkBase':
  23271. case 'Manager':
  23272. case 'ContentStatus':
  23273. case 'Identifier':
  23274. case 'Language':
  23275. case 'AppName':
  23276. if(Rn[0].slice(-2) === "/>") break;
  23277. else if(Rn[1]==="/") xlml_set_prop(Props, Rn[3], str.slice(pidx, Rn.index));
  23278. else pidx = Rn.index + Rn[0].length;
  23279. break;
  23280. case 'Paragraphs': break;
  23281. case 'Styles':
  23282. case 'Workbook':
  23283. if(Rn[1]==='/'){if((tmp=state.pop())[0]!==Rn[3]) throw new Error("Bad state: "+tmp.join("|"));}
  23284. else state.push([Rn[3], false]);
  23285. break;
  23286. case 'Comment':
  23287. if(Rn[1]==='/'){
  23288. if((tmp=state.pop())[0]!==Rn[3]) throw new Error("Bad state: "+tmp.join("|"));
  23289. xlml_clean_comment(comment);
  23290. comments.push(comment);
  23291. } else {
  23292. state.push([Rn[3], false]);
  23293. tmp = xlml_parsexmltag(Rn[0]);
  23294. comment = ({a:tmp.Author});
  23295. }
  23296. break;
  23297. case 'AutoFilter':
  23298. if(Rn[1]==='/'){if((tmp=state.pop())[0]!==Rn[3]) throw new Error("Bad state: "+tmp.join("|"));}
  23299. else if(Rn[0].charAt(Rn[0].length-2) !== '/') {
  23300. var AutoFilter = xlml_parsexmltag(Rn[0]);
  23301. cursheet['!autofilter'] = { ref:rc_to_a1(AutoFilter.Range).replace(/\$/g,"") };
  23302. state.push([Rn[3], true]);
  23303. }
  23304. break;
  23305. case 'Name': break;
  23306. case 'ComponentOptions':
  23307. case 'DocumentProperties':
  23308. case 'CustomDocumentProperties':
  23309. case 'OfficeDocumentSettings':
  23310. case 'PivotTable':
  23311. case 'PivotCache':
  23312. case 'Names':
  23313. case 'MapInfo':
  23314. case 'PageBreaks':
  23315. case 'QueryTable':
  23316. case 'DataValidation':
  23317. case 'Sorting':
  23318. case 'Schema':
  23319. case 'data':
  23320. case 'ConditionalFormatting':
  23321. case 'SmartTagType':
  23322. case 'SmartTags':
  23323. case 'ExcelWorkbook':
  23324. case 'WorkbookOptions':
  23325. case 'WorksheetOptions':
  23326. if(Rn[1]==='/'){if((tmp=state.pop())[0]!==Rn[3]) throw new Error("Bad state: "+tmp.join("|"));}
  23327. else if(Rn[0].charAt(Rn[0].length-2) !== '/') state.push([Rn[3], true]);
  23328. break;
  23329. default:
  23330. /* FODS file root is <office:document> */
  23331. if(state.length == 0 && Rn[3] == "document") return parse_fods(str, opts);
  23332. /* UOS file root is <uof:UOF> */
  23333. if(state.length == 0 && Rn[3] == "UOF") return parse_fods(str, opts);
  23334. var seen = true;
  23335. switch(state[state.length-1][0]) {
  23336. /* OfficeDocumentSettings */
  23337. case 'OfficeDocumentSettings': switch(Rn[3]) {
  23338. case 'AllowPNG': break;
  23339. case 'RemovePersonalInformation': break;
  23340. case 'DownloadComponents': break;
  23341. case 'LocationOfComponents': break;
  23342. case 'Colors': break;
  23343. case 'Color': break;
  23344. case 'Index': break;
  23345. case 'RGB': break;
  23346. case 'PixelsPerInch': break; // TODO: set PPI
  23347. case 'TargetScreenSize': break;
  23348. case 'ReadOnlyRecommended': break;
  23349. default: seen = false;
  23350. } break;
  23351. /* ComponentOptions */
  23352. case 'ComponentOptions': switch(Rn[3]) {
  23353. case 'Toolbar': break;
  23354. case 'HideOfficeLogo': break;
  23355. case 'SpreadsheetAutoFit': break;
  23356. case 'Label': break;
  23357. case 'Caption': break;
  23358. case 'MaxHeight': break;
  23359. case 'MaxWidth': break;
  23360. case 'NextSheetNumber': break;
  23361. default: seen = false;
  23362. } break;
  23363. /* ExcelWorkbook */
  23364. case 'ExcelWorkbook': switch(Rn[3]) {
  23365. case 'Date1904':
  23366. Workbook.WBProps.date1904 = true;
  23367. break;
  23368. case 'WindowHeight': break;
  23369. case 'WindowWidth': break;
  23370. case 'WindowTopX': break;
  23371. case 'WindowTopY': break;
  23372. case 'TabRatio': break;
  23373. case 'ProtectStructure': break;
  23374. case 'ProtectWindows': break;
  23375. case 'ActiveSheet': break;
  23376. case 'DisplayInkNotes': break;
  23377. case 'FirstVisibleSheet': break;
  23378. case 'SupBook': break;
  23379. case 'SheetName': break;
  23380. case 'SheetIndex': break;
  23381. case 'SheetIndexFirst': break;
  23382. case 'SheetIndexLast': break;
  23383. case 'Dll': break;
  23384. case 'AcceptLabelsInFormulas': break;
  23385. case 'DoNotSaveLinkValues': break;
  23386. case 'Iteration': break;
  23387. case 'MaxIterations': break;
  23388. case 'MaxChange': break;
  23389. case 'Path': break;
  23390. case 'Xct': break;
  23391. case 'Count': break;
  23392. case 'SelectedSheets': break;
  23393. case 'Calculation': break;
  23394. case 'Uncalced': break;
  23395. case 'StartupPrompt': break;
  23396. case 'Crn': break;
  23397. case 'ExternName': break;
  23398. case 'Formula': break;
  23399. case 'ColFirst': break;
  23400. case 'ColLast': break;
  23401. case 'WantAdvise': break;
  23402. case 'Boolean': break;
  23403. case 'Error': break;
  23404. case 'Text': break;
  23405. case 'OLE': break;
  23406. case 'NoAutoRecover': break;
  23407. case 'PublishObjects': break;
  23408. case 'DoNotCalculateBeforeSave': break;
  23409. case 'Number': break;
  23410. case 'RefModeR1C1': break;
  23411. case 'EmbedSaveSmartTags': break;
  23412. default: seen = false;
  23413. } break;
  23414. /* WorkbookOptions */
  23415. case 'WorkbookOptions': switch(Rn[3]) {
  23416. case 'OWCVersion': break;
  23417. case 'Height': break;
  23418. case 'Width': break;
  23419. default: seen = false;
  23420. } break;
  23421. /* WorksheetOptions */
  23422. case 'WorksheetOptions': switch(Rn[3]) {
  23423. case 'Visible':
  23424. if(Rn[0].slice(-2) === "/>"){/* empty */}
  23425. else if(Rn[1]==="/") switch(str.slice(pidx, Rn.index)) {
  23426. case "SheetHidden": wsprops.Hidden = 1; break;
  23427. case "SheetVeryHidden": wsprops.Hidden = 2; break;
  23428. }
  23429. else pidx = Rn.index + Rn[0].length;
  23430. break;
  23431. case 'Header':
  23432. if(!cursheet['!margins']) default_margins(cursheet['!margins']={}, 'xlml');
  23433. cursheet['!margins'].header = parsexmltag(Rn[0]).Margin;
  23434. break;
  23435. case 'Footer':
  23436. if(!cursheet['!margins']) default_margins(cursheet['!margins']={}, 'xlml');
  23437. cursheet['!margins'].footer = parsexmltag(Rn[0]).Margin;
  23438. break;
  23439. case 'PageMargins':
  23440. var pagemargins = parsexmltag(Rn[0]);
  23441. if(!cursheet['!margins']) default_margins(cursheet['!margins']={},'xlml');
  23442. if(pagemargins.Top) cursheet['!margins'].top = pagemargins.Top;
  23443. if(pagemargins.Left) cursheet['!margins'].left = pagemargins.Left;
  23444. if(pagemargins.Right) cursheet['!margins'].right = pagemargins.Right;
  23445. if(pagemargins.Bottom) cursheet['!margins'].bottom = pagemargins.Bottom;
  23446. break;
  23447. case 'DisplayRightToLeft':
  23448. if(!Workbook.Views) Workbook.Views = [];
  23449. if(!Workbook.Views[0]) Workbook.Views[0] = {};
  23450. Workbook.Views[0].RTL = true;
  23451. break;
  23452. case 'Unsynced': break;
  23453. case 'Print': break;
  23454. case 'Panes': break;
  23455. case 'Scale': break;
  23456. case 'Pane': break;
  23457. case 'Number': break;
  23458. case 'Layout': break;
  23459. case 'PageSetup': break;
  23460. case 'Selected': break;
  23461. case 'ProtectObjects': break;
  23462. case 'EnableSelection': break;
  23463. case 'ProtectScenarios': break;
  23464. case 'ValidPrinterInfo': break;
  23465. case 'HorizontalResolution': break;
  23466. case 'VerticalResolution': break;
  23467. case 'NumberofCopies': break;
  23468. case 'ActiveRow': break;
  23469. case 'ActiveCol': break;
  23470. case 'ActivePane': break;
  23471. case 'TopRowVisible': break;
  23472. case 'TopRowBottomPane': break;
  23473. case 'LeftColumnVisible': break;
  23474. case 'LeftColumnRightPane': break;
  23475. case 'FitToPage': break;
  23476. case 'RangeSelection': break;
  23477. case 'PaperSizeIndex': break;
  23478. case 'PageLayoutZoom': break;
  23479. case 'PageBreakZoom': break;
  23480. case 'FilterOn': break;
  23481. case 'DoNotDisplayGridlines': break;
  23482. case 'SplitHorizontal': break;
  23483. case 'SplitVertical': break;
  23484. case 'FreezePanes': break;
  23485. case 'FrozenNoSplit': break;
  23486. case 'FitWidth': break;
  23487. case 'FitHeight': break;
  23488. case 'CommentsLayout': break;
  23489. case 'Zoom': break;
  23490. case 'LeftToRight': break;
  23491. case 'Gridlines': break;
  23492. case 'AllowSort': break;
  23493. case 'AllowFilter': break;
  23494. case 'AllowInsertRows': break;
  23495. case 'AllowDeleteRows': break;
  23496. case 'AllowInsertCols': break;
  23497. case 'AllowDeleteCols': break;
  23498. case 'AllowInsertHyperlinks': break;
  23499. case 'AllowFormatCells': break;
  23500. case 'AllowSizeCols': break;
  23501. case 'AllowSizeRows': break;
  23502. case 'NoSummaryRowsBelowDetail': break;
  23503. case 'TabColorIndex': break;
  23504. case 'DoNotDisplayHeadings': break;
  23505. case 'ShowPageLayoutZoom': break;
  23506. case 'NoSummaryColumnsRightDetail': break;
  23507. case 'BlackAndWhite': break;
  23508. case 'DoNotDisplayZeros': break;
  23509. case 'DisplayPageBreak': break;
  23510. case 'RowColHeadings': break;
  23511. case 'DoNotDisplayOutline': break;
  23512. case 'NoOrientation': break;
  23513. case 'AllowUsePivotTables': break;
  23514. case 'ZeroHeight': break;
  23515. case 'ViewableRange': break;
  23516. case 'Selection': break;
  23517. case 'ProtectContents': break;
  23518. default: seen = false;
  23519. } break;
  23520. /* PivotTable */
  23521. case 'PivotTable': case 'PivotCache': switch(Rn[3]) {
  23522. case 'ImmediateItemsOnDrop': break;
  23523. case 'ShowPageMultipleItemLabel': break;
  23524. case 'CompactRowIndent': break;
  23525. case 'Location': break;
  23526. case 'PivotField': break;
  23527. case 'Orientation': break;
  23528. case 'LayoutForm': break;
  23529. case 'LayoutSubtotalLocation': break;
  23530. case 'LayoutCompactRow': break;
  23531. case 'Position': break;
  23532. case 'PivotItem': break;
  23533. case 'DataType': break;
  23534. case 'DataField': break;
  23535. case 'SourceName': break;
  23536. case 'ParentField': break;
  23537. case 'PTLineItems': break;
  23538. case 'PTLineItem': break;
  23539. case 'CountOfSameItems': break;
  23540. case 'Item': break;
  23541. case 'ItemType': break;
  23542. case 'PTSource': break;
  23543. case 'CacheIndex': break;
  23544. case 'ConsolidationReference': break;
  23545. case 'FileName': break;
  23546. case 'Reference': break;
  23547. case 'NoColumnGrand': break;
  23548. case 'NoRowGrand': break;
  23549. case 'BlankLineAfterItems': break;
  23550. case 'Hidden': break;
  23551. case 'Subtotal': break;
  23552. case 'BaseField': break;
  23553. case 'MapChildItems': break;
  23554. case 'Function': break;
  23555. case 'RefreshOnFileOpen': break;
  23556. case 'PrintSetTitles': break;
  23557. case 'MergeLabels': break;
  23558. case 'DefaultVersion': break;
  23559. case 'RefreshName': break;
  23560. case 'RefreshDate': break;
  23561. case 'RefreshDateCopy': break;
  23562. case 'VersionLastRefresh': break;
  23563. case 'VersionLastUpdate': break;
  23564. case 'VersionUpdateableMin': break;
  23565. case 'VersionRefreshableMin': break;
  23566. case 'Calculation': break;
  23567. default: seen = false;
  23568. } break;
  23569. /* PageBreaks */
  23570. case 'PageBreaks': switch(Rn[3]) {
  23571. case 'ColBreaks': break;
  23572. case 'ColBreak': break;
  23573. case 'RowBreaks': break;
  23574. case 'RowBreak': break;
  23575. case 'ColStart': break;
  23576. case 'ColEnd': break;
  23577. case 'RowEnd': break;
  23578. default: seen = false;
  23579. } break;
  23580. /* AutoFilter */
  23581. case 'AutoFilter': switch(Rn[3]) {
  23582. case 'AutoFilterColumn': break;
  23583. case 'AutoFilterCondition': break;
  23584. case 'AutoFilterAnd': break;
  23585. case 'AutoFilterOr': break;
  23586. default: seen = false;
  23587. } break;
  23588. /* QueryTable */
  23589. case 'QueryTable': switch(Rn[3]) {
  23590. case 'Id': break;
  23591. case 'AutoFormatFont': break;
  23592. case 'AutoFormatPattern': break;
  23593. case 'QuerySource': break;
  23594. case 'QueryType': break;
  23595. case 'EnableRedirections': break;
  23596. case 'RefreshedInXl9': break;
  23597. case 'URLString': break;
  23598. case 'HTMLTables': break;
  23599. case 'Connection': break;
  23600. case 'CommandText': break;
  23601. case 'RefreshInfo': break;
  23602. case 'NoTitles': break;
  23603. case 'NextId': break;
  23604. case 'ColumnInfo': break;
  23605. case 'OverwriteCells': break;
  23606. case 'DoNotPromptForFile': break;
  23607. case 'TextWizardSettings': break;
  23608. case 'Source': break;
  23609. case 'Number': break;
  23610. case 'Decimal': break;
  23611. case 'ThousandSeparator': break;
  23612. case 'TrailingMinusNumbers': break;
  23613. case 'FormatSettings': break;
  23614. case 'FieldType': break;
  23615. case 'Delimiters': break;
  23616. case 'Tab': break;
  23617. case 'Comma': break;
  23618. case 'AutoFormatName': break;
  23619. case 'VersionLastEdit': break;
  23620. case 'VersionLastRefresh': break;
  23621. default: seen = false;
  23622. } break;
  23623. case 'Sorting':
  23624. case 'ConditionalFormatting':
  23625. case 'DataValidation':
  23626. switch(Rn[3]) {
  23627. case 'Range': break;
  23628. case 'Type': break;
  23629. case 'Min': break;
  23630. case 'Max': break;
  23631. case 'Sort': break;
  23632. case 'Descending': break;
  23633. case 'Order': break;
  23634. case 'CaseSensitive': break;
  23635. case 'Value': break;
  23636. case 'ErrorStyle': break;
  23637. case 'ErrorMessage': break;
  23638. case 'ErrorTitle': break;
  23639. case 'CellRangeList': break;
  23640. case 'InputMessage': break;
  23641. case 'InputTitle': break;
  23642. case 'ComboHide': break;
  23643. case 'InputHide': break;
  23644. case 'Condition': break;
  23645. case 'Qualifier': break;
  23646. case 'UseBlank': break;
  23647. case 'Value1': break;
  23648. case 'Value2': break;
  23649. case 'Format': break;
  23650. default: seen = false;
  23651. } break;
  23652. /* MapInfo (schema) */
  23653. case 'MapInfo': case 'Schema': case 'data': switch(Rn[3]) {
  23654. case 'Map': break;
  23655. case 'Entry': break;
  23656. case 'Range': break;
  23657. case 'XPath': break;
  23658. case 'Field': break;
  23659. case 'XSDType': break;
  23660. case 'FilterOn': break;
  23661. case 'Aggregate': break;
  23662. case 'ElementType': break;
  23663. case 'AttributeType': break;
  23664. /* These are from xsd (XML Schema Definition) */
  23665. case 'schema':
  23666. case 'element':
  23667. case 'complexType':
  23668. case 'datatype':
  23669. case 'all':
  23670. case 'attribute':
  23671. case 'extends': break;
  23672. case 'row': break;
  23673. default: seen = false;
  23674. } break;
  23675. /* SmartTags (can be anything) */
  23676. case 'SmartTags': break;
  23677. default: seen = false; break;
  23678. }
  23679. if(seen) break;
  23680. /* CustomDocumentProperties */
  23681. if(!state[state.length-1][1]) throw 'Unrecognized tag: ' + Rn[3] + "|" + state.join("|");
  23682. if(state[state.length-1][0]==='CustomDocumentProperties') {
  23683. if(Rn[0].slice(-2) === "/>") break;
  23684. else if(Rn[1]==="/") xlml_set_custprop(Custprops, Rn[3], cp, str.slice(pidx, Rn.index));
  23685. else { cp = Rn; pidx = Rn.index + Rn[0].length; }
  23686. break;
  23687. }
  23688. if(opts.WTF) throw 'Unrecognized tag: ' + Rn[3] + "|" + state.join("|");
  23689. }
  23690. var out = ({});
  23691. if(!opts.bookSheets && !opts.bookProps) out.Sheets = sheets;
  23692. out.SheetNames = sheetnames;
  23693. out.Workbook = Workbook;
  23694. out.SSF = SSF.get_table();
  23695. out.Props = Props;
  23696. out.Custprops = Custprops;
  23697. return out;
  23698. }
  23699. function parse_xlml(data, opts) {
  23700. fix_read_opts(opts=opts||{});
  23701. switch(opts.type||"base64") {
  23702. case "base64": return parse_xlml_xml(Base64.decode(data), opts);
  23703. case "binary": case "buffer": case "file": return parse_xlml_xml(data, opts);
  23704. case "array": return parse_xlml_xml(a2s(data), opts);
  23705. }
  23706. }
  23707. /* TODO */
  23708. function write_props_xlml(wb, opts) {
  23709. var o = [];
  23710. /* DocumentProperties */
  23711. if(wb.Props) o.push(xlml_write_docprops(wb.Props, opts));
  23712. /* CustomDocumentProperties */
  23713. if(wb.Custprops) o.push(xlml_write_custprops(wb.Props, wb.Custprops, opts));
  23714. return o.join("");
  23715. }
  23716. /* TODO */
  23717. function write_wb_xlml() {
  23718. /* OfficeDocumentSettings */
  23719. /* ExcelWorkbook */
  23720. return "";
  23721. }
  23722. /* TODO */
  23723. function write_sty_xlml(wb, opts) {
  23724. /* Styles */
  23725. var styles = ['<Style ss:ID="Default" ss:Name="Normal"><NumberFormat/></Style>'];
  23726. opts.cellXfs.forEach(function(xf, id) {
  23727. var payload = [];
  23728. payload.push(writextag('NumberFormat', null, {"ss:Format": escapexml(SSF._table[xf.numFmtId])}));
  23729. styles.push(writextag('Style', payload.join(""), {"ss:ID": "s" + (21+id)}));
  23730. });
  23731. return writextag("Styles", styles.join(""));
  23732. }
  23733. function write_name_xlml(n) { return writextag("NamedRange", null, {"ss:Name": n.Name, "ss:RefersTo":"=" + a1_to_rc(n.Ref, {r:0,c:0})}); }
  23734. function write_names_xlml(wb) {
  23735. if(!((wb||{}).Workbook||{}).Names) return "";
  23736. var names = wb.Workbook.Names;
  23737. var out = [];
  23738. for(var i = 0; i < names.length; ++i) {
  23739. var n = names[i];
  23740. if(n.Sheet != null) continue;
  23741. if(n.Name.match(/^_xlfn\./)) continue;
  23742. out.push(write_name_xlml(n));
  23743. }
  23744. return writextag("Names", out.join(""));
  23745. }
  23746. function write_ws_xlml_names(ws, opts, idx, wb) {
  23747. if(!ws) return "";
  23748. if(!((wb||{}).Workbook||{}).Names) return "";
  23749. var names = wb.Workbook.Names;
  23750. var out = [];
  23751. for(var i = 0; i < names.length; ++i) {
  23752. var n = names[i];
  23753. if(n.Sheet != idx) continue;
  23754. /*switch(n.Name) {
  23755. case "_": continue;
  23756. }*/
  23757. if(n.Name.match(/^_xlfn\./)) continue;
  23758. out.push(write_name_xlml(n));
  23759. }
  23760. return out.join("");
  23761. }
  23762. /* WorksheetOptions */
  23763. function write_ws_xlml_wsopts(ws, opts, idx, wb) {
  23764. if(!ws) return "";
  23765. var o = [];
  23766. /* NOTE: spec technically allows any order, but stick with implied order */
  23767. /* FitToPage */
  23768. /* DoNotDisplayColHeaders */
  23769. /* DoNotDisplayRowHeaders */
  23770. /* ViewableRange */
  23771. /* Selection */
  23772. /* GridlineColor */
  23773. /* Name */
  23774. /* ExcelWorksheetType */
  23775. /* IntlMacro */
  23776. /* Unsynced */
  23777. /* Selected */
  23778. /* CodeName */
  23779. if(ws['!margins']) {
  23780. o.push("<PageSetup>");
  23781. if(ws['!margins'].header) o.push(writextag("Header", null, {'x:Margin':ws['!margins'].header}));
  23782. if(ws['!margins'].footer) o.push(writextag("Footer", null, {'x:Margin':ws['!margins'].footer}));
  23783. o.push(writextag("PageMargins", null, {
  23784. 'x:Bottom': ws['!margins'].bottom || "0.75",
  23785. 'x:Left': ws['!margins'].left || "0.7",
  23786. 'x:Right': ws['!margins'].right || "0.7",
  23787. 'x:Top': ws['!margins'].top || "0.75"
  23788. }));
  23789. o.push("</PageSetup>");
  23790. }
  23791. /* PageSetup */
  23792. /* DisplayPageBreak */
  23793. /* TransitionExpressionEvaluation */
  23794. /* TransitionFormulaEntry */
  23795. /* Print */
  23796. /* Zoom */
  23797. /* PageLayoutZoom */
  23798. /* PageBreakZoom */
  23799. /* ShowPageBreakZoom */
  23800. /* DefaultRowHeight */
  23801. /* DefaultColumnWidth */
  23802. /* StandardWidth */
  23803. if(wb && wb.Workbook && wb.Workbook.Sheets && wb.Workbook.Sheets[idx]) {
  23804. /* Visible */
  23805. if(wb.Workbook.Sheets[idx].Hidden) o.push(writextag("Visible", (wb.Workbook.Sheets[idx].Hidden == 1 ? "SheetHidden" : "SheetVeryHidden"), {}));
  23806. else {
  23807. /* Selected */
  23808. for(var i = 0; i < idx; ++i) if(wb.Workbook.Sheets[i] && !wb.Workbook.Sheets[i].Hidden) break;
  23809. if(i == idx) o.push("<Selected/>");
  23810. }
  23811. }
  23812. /* LeftColumnVisible */
  23813. if(((((wb||{}).Workbook||{}).Views||[])[0]||{}).RTL) o.push("<DisplayRightToLeft/>");
  23814. /* GridlineColorIndex */
  23815. /* DisplayFormulas */
  23816. /* DoNotDisplayGridlines */
  23817. /* DoNotDisplayHeadings */
  23818. /* DoNotDisplayOutline */
  23819. /* ApplyAutomaticOutlineStyles */
  23820. /* NoSummaryRowsBelowDetail */
  23821. /* NoSummaryColumnsRightDetail */
  23822. /* DoNotDisplayZeros */
  23823. /* ActiveRow */
  23824. /* ActiveColumn */
  23825. /* FilterOn */
  23826. /* RangeSelection */
  23827. /* TopRowVisible */
  23828. /* TopRowBottomPane */
  23829. /* LeftColumnRightPane */
  23830. /* ActivePane */
  23831. /* SplitHorizontal */
  23832. /* SplitVertical */
  23833. /* FreezePanes */
  23834. /* FrozenNoSplit */
  23835. /* TabColorIndex */
  23836. /* Panes */
  23837. /* NOTE: Password not supported in XLML Format */
  23838. if(ws['!protect']) {
  23839. o.push(writetag("ProtectContents", "True"));
  23840. if(ws['!protect'].objects) o.push(writetag("ProtectObjects", "True"));
  23841. if(ws['!protect'].scenarios) o.push(writetag("ProtectScenarios", "True"));
  23842. if(ws['!protect'].selectLockedCells != null && !ws['!protect'].selectLockedCells) o.push(writetag("EnableSelection", "NoSelection"));
  23843. else if(ws['!protect'].selectUnlockedCells != null && !ws['!protect'].selectUnlockedCells) o.push(writetag("EnableSelection", "UnlockedCells"));
  23844. [
  23845. [ "formatCells", "AllowFormatCells" ],
  23846. [ "formatColumns", "AllowSizeCols" ],
  23847. [ "formatRows", "AllowSizeRows" ],
  23848. [ "insertColumns", "AllowInsertCols" ],
  23849. [ "insertRows", "AllowInsertRows" ],
  23850. [ "insertHyperlinks", "AllowInsertHyperlinks" ],
  23851. [ "deleteColumns", "AllowDeleteCols" ],
  23852. [ "deleteRows", "AllowDeleteRows" ],
  23853. [ "sort", "AllowSort" ],
  23854. [ "autoFilter", "AllowFilter" ],
  23855. [ "pivotTables", "AllowUsePivotTables" ]
  23856. ].forEach(function(x) { if(ws['!protect'][x[0]]) o.push("<"+x[1]+"/>"); });
  23857. }
  23858. if(o.length == 0) return "";
  23859. return writextag("WorksheetOptions", o.join(""), {xmlns:XLMLNS.x});
  23860. }
  23861. function write_ws_xlml_comment(comments) {
  23862. return comments.map(function(c) {
  23863. // TODO: formatted text
  23864. var t = xlml_unfixstr(c.t||"");
  23865. var d =writextag("ss:Data", t, {"xmlns":"http://www.w3.org/TR/REC-html40"});
  23866. return writextag("Comment", d, {"ss:Author":c.a});
  23867. }).join("");
  23868. }
  23869. function write_ws_xlml_cell(cell, ref, ws, opts, idx, wb, addr){
  23870. if(!cell || (cell.v == undefined && cell.f == undefined)) return "";
  23871. var attr = {};
  23872. if(cell.f) attr["ss:Formula"] = "=" + escapexml(a1_to_rc(cell.f, addr));
  23873. if(cell.F && cell.F.slice(0, ref.length) == ref) {
  23874. var end = decode_cell(cell.F.slice(ref.length + 1));
  23875. attr["ss:ArrayRange"] = "RC:R" + (end.r == addr.r ? "" : "[" + (end.r - addr.r) + "]") + "C" + (end.c == addr.c ? "" : "[" + (end.c - addr.c) + "]");
  23876. }
  23877. if(cell.l && cell.l.Target) {
  23878. attr["ss:HRef"] = escapexml(cell.l.Target);
  23879. if(cell.l.Tooltip) attr["x:HRefScreenTip"] = escapexml(cell.l.Tooltip);
  23880. }
  23881. if(ws['!merges']) {
  23882. var marr = ws['!merges'];
  23883. for(var mi = 0; mi != marr.length; ++mi) {
  23884. if(marr[mi].s.c != addr.c || marr[mi].s.r != addr.r) continue;
  23885. if(marr[mi].e.c > marr[mi].s.c) attr['ss:MergeAcross'] = marr[mi].e.c - marr[mi].s.c;
  23886. if(marr[mi].e.r > marr[mi].s.r) attr['ss:MergeDown'] = marr[mi].e.r - marr[mi].s.r;
  23887. }
  23888. }
  23889. var t = "", p = "";
  23890. switch(cell.t) {
  23891. case 'z': return "";
  23892. case 'n': t = 'Number'; p = String(cell.v); break;
  23893. case 'b': t = 'Boolean'; p = (cell.v ? "1" : "0"); break;
  23894. case 'e': t = 'Error'; p = BErr[cell.v]; break;
  23895. case 'd': t = 'DateTime'; p = new Date(cell.v).toISOString(); if(cell.z == null) cell.z = cell.z || SSF._table[14]; break;
  23896. case 's': t = 'String'; p = escapexlml(cell.v||""); break;
  23897. }
  23898. /* TODO: cell style */
  23899. var os = get_cell_style(opts.cellXfs, cell, opts);
  23900. attr["ss:StyleID"] = "s" + (21+os);
  23901. attr["ss:Index"] = addr.c + 1;
  23902. var _v = (cell.v != null ? p : "");
  23903. var m = '<Data ss:Type="' + t + '">' + _v + '</Data>';
  23904. if((cell.c||[]).length > 0) m += write_ws_xlml_comment(cell.c);
  23905. return writextag("Cell", m, attr);
  23906. }
  23907. function write_ws_xlml_row(R, row) {
  23908. var o = '<Row ss:Index="' + (R+1) + '"';
  23909. if(row) {
  23910. if(row.hpt && !row.hpx) row.hpx = pt2px(row.hpt);
  23911. if(row.hpx) o += ' ss:AutoFitHeight="0" ss:Height="' + row.hpx + '"';
  23912. if(row.hidden) o += ' ss:Hidden="1"';
  23913. }
  23914. return o + '>';
  23915. }
  23916. /* TODO */
  23917. function write_ws_xlml_table(ws, opts, idx, wb) {
  23918. if(!ws['!ref']) return "";
  23919. var range = safe_decode_range(ws['!ref']);
  23920. var marr = ws['!merges'] || [], mi = 0;
  23921. var o = [];
  23922. if(ws['!cols']) ws['!cols'].forEach(function(n, i) {
  23923. process_col(n);
  23924. var w = !!n.width;
  23925. var p = col_obj_w(i, n);
  23926. var k = {"ss:Index":i+1};
  23927. if(w) k['ss:Width'] = width2px(p.width);
  23928. if(n.hidden) k['ss:Hidden']="1";
  23929. o.push(writextag("Column",null,k));
  23930. });
  23931. var dense = Array.isArray(ws);
  23932. for(var R = range.s.r; R <= range.e.r; ++R) {
  23933. var row = [write_ws_xlml_row(R, (ws['!rows']||[])[R])];
  23934. for(var C = range.s.c; C <= range.e.c; ++C) {
  23935. var skip = false;
  23936. for(mi = 0; mi != marr.length; ++mi) {
  23937. if(marr[mi].s.c > C) continue;
  23938. if(marr[mi].s.r > R) continue;
  23939. if(marr[mi].e.c < C) continue;
  23940. if(marr[mi].e.r < R) continue;
  23941. if(marr[mi].s.c != C || marr[mi].s.r != R) skip = true;
  23942. break;
  23943. }
  23944. if(skip) continue;
  23945. var addr = {r:R,c:C};
  23946. var ref = encode_cell(addr), cell = dense ? (ws[R]||[])[C] : ws[ref];
  23947. row.push(write_ws_xlml_cell(cell, ref, ws, opts, idx, wb, addr));
  23948. }
  23949. row.push("</Row>");
  23950. if(row.length > 2) o.push(row.join(""));
  23951. }
  23952. return o.join("");
  23953. }
  23954. function write_ws_xlml(idx, opts, wb) {
  23955. var o = [];
  23956. var s = wb.SheetNames[idx];
  23957. var ws = wb.Sheets[s];
  23958. var t = ws ? write_ws_xlml_names(ws, opts, idx, wb) : "";
  23959. if(t.length > 0) o.push("<Names>" + t + "</Names>");
  23960. /* Table */
  23961. t = ws ? write_ws_xlml_table(ws, opts, idx, wb) : "";
  23962. if(t.length > 0) o.push("<Table>" + t + "</Table>");
  23963. /* WorksheetOptions */
  23964. o.push(write_ws_xlml_wsopts(ws, opts, idx, wb));
  23965. return o.join("");
  23966. }
  23967. function write_xlml(wb, opts) {
  23968. if(!opts) opts = {};
  23969. if(!wb.SSF) wb.SSF = SSF.get_table();
  23970. if(wb.SSF) {
  23971. make_ssf(SSF); SSF.load_table(wb.SSF);
  23972. // $FlowIgnore
  23973. opts.revssf = evert_num(wb.SSF); opts.revssf[wb.SSF[65535]] = 0;
  23974. opts.ssf = wb.SSF;
  23975. opts.cellXfs = [];
  23976. get_cell_style(opts.cellXfs, {}, {revssf:{"General":0}});
  23977. }
  23978. var d = [];
  23979. d.push(write_props_xlml(wb, opts));
  23980. d.push(write_wb_xlml(wb, opts));
  23981. d.push("");
  23982. d.push("");
  23983. for(var i = 0; i < wb.SheetNames.length; ++i)
  23984. d.push(writextag("Worksheet", write_ws_xlml(i, opts, wb), {"ss:Name":escapexml(wb.SheetNames[i])}));
  23985. d[2] = write_sty_xlml(wb, opts);
  23986. d[3] = write_names_xlml(wb, opts);
  23987. return XML_HEADER + writextag("Workbook", d.join(""), {
  23988. 'xmlns': XLMLNS.ss,
  23989. 'xmlns:o': XLMLNS.o,
  23990. 'xmlns:x': XLMLNS.x,
  23991. 'xmlns:ss': XLMLNS.ss,
  23992. 'xmlns:dt': XLMLNS.dt,
  23993. 'xmlns:html': XLMLNS.html
  23994. });
  23995. }
  23996. /* [MS-OLEDS] 2.3.8 CompObjStream */
  23997. function parse_compobj(obj) {
  23998. var v = {};
  23999. var o = obj.content;
  24000. /* [MS-OLEDS] 2.3.7 CompObjHeader -- All fields MUST be ignored */
  24001. o.l = 28;
  24002. v.AnsiUserType = o.read_shift(0, "lpstr-ansi");
  24003. v.AnsiClipboardFormat = parse_ClipboardFormatOrAnsiString(o);
  24004. if(o.length - o.l <= 4) return v;
  24005. var m = o.read_shift(4);
  24006. if(m == 0 || m > 40) return v;
  24007. o.l-=4; v.Reserved1 = o.read_shift(0, "lpstr-ansi");
  24008. if(o.length - o.l <= 4) return v;
  24009. m = o.read_shift(4);
  24010. if(m !== 0x71b239f4) return v;
  24011. v.UnicodeClipboardFormat = parse_ClipboardFormatOrUnicodeString(o);
  24012. m = o.read_shift(4);
  24013. if(m == 0 || m > 40) return v;
  24014. o.l-=4; v.Reserved2 = o.read_shift(0, "lpwstr");
  24015. }
  24016. /*
  24017. Continue logic for:
  24018. - 2.4.58 Continue
  24019. - 2.4.59 ContinueBigName
  24020. - 2.4.60 ContinueFrt
  24021. - 2.4.61 ContinueFrt11
  24022. - 2.4.62 ContinueFrt12
  24023. */
  24024. function slurp(R, blob, length, opts) {
  24025. var l = length;
  24026. var bufs = [];
  24027. var d = blob.slice(blob.l,blob.l+l);
  24028. if(opts && opts.enc && opts.enc.insitu) switch(R.n) {
  24029. case 'BOF': case 'FilePass': case 'FileLock': case 'InterfaceHdr': case 'RRDInfo': case 'RRDHead': case 'UsrExcl': break;
  24030. default:
  24031. if(d.length === 0) break;
  24032. opts.enc.insitu(d);
  24033. }
  24034. bufs.push(d);
  24035. blob.l += l;
  24036. var next = (XLSRecordEnum[__readUInt16LE(blob,blob.l)]);
  24037. var start = 0;
  24038. while(next != null && next.n.slice(0,8) === 'Continue') {
  24039. l = __readUInt16LE(blob,blob.l+2);
  24040. start = blob.l + 4;
  24041. if(next.n == 'ContinueFrt') start += 4;
  24042. else if(next.n.slice(0,11) == 'ContinueFrt') start += 12;
  24043. bufs.push(blob.slice(start,blob.l+4+l));
  24044. blob.l += 4+l;
  24045. next = (XLSRecordEnum[__readUInt16LE(blob, blob.l)]);
  24046. }
  24047. var b = (bconcat(bufs));
  24048. prep_blob(b, 0);
  24049. var ll = 0; b.lens = [];
  24050. for(var j = 0; j < bufs.length; ++j) { b.lens.push(ll); ll += bufs[j].length; }
  24051. return R.f(b, b.length, opts);
  24052. }
  24053. function safe_format_xf(p, opts, date1904) {
  24054. if(p.t === 'z') return;
  24055. if(!p.XF) return;
  24056. var fmtid = 0;
  24057. try {
  24058. fmtid = p.z || p.XF.numFmtId || 0;
  24059. if(opts.cellNF) p.z = SSF._table[fmtid];
  24060. } catch(e) { if(opts.WTF) throw e; }
  24061. if(!opts || opts.cellText !== false) try {
  24062. if(p.t === 'e') { p.w = p.w || BErr[p.v]; }
  24063. else if(fmtid === 0 || fmtid == "General") {
  24064. if(p.t === 'n') {
  24065. if((p.v|0) === p.v) p.w = SSF._general_int(p.v);
  24066. else p.w = SSF._general_num(p.v);
  24067. }
  24068. else p.w = SSF._general(p.v);
  24069. }
  24070. else p.w = SSF.format(fmtid,p.v, {date1904:!!date1904});
  24071. } catch(e) { if(opts.WTF) throw e; }
  24072. if(opts.cellDates && fmtid && p.t == 'n' && SSF.is_date(SSF._table[fmtid] || String(fmtid))) {
  24073. var _d = SSF.parse_date_code(p.v); if(_d) { p.t = 'd'; p.v = new Date(_d.y, _d.m-1,_d.d,_d.H,_d.M,_d.S,_d.u); }
  24074. }
  24075. }
  24076. function make_cell(val, ixfe, t) {
  24077. return ({v:val, ixfe:ixfe, t:t});
  24078. }
  24079. // 2.3.2
  24080. function parse_workbook(blob, options) {
  24081. var wb = ({opts:{}});
  24082. var Sheets = {};
  24083. if(DENSE != null && options.dense == null) options.dense = DENSE;
  24084. var out = ((options.dense ? [] : {}));
  24085. var Directory = {};
  24086. var range = ({});
  24087. var last_formula = null;
  24088. var sst = ([]);
  24089. var cur_sheet = "";
  24090. var Preamble = {};
  24091. var lastcell, last_cell = "", cc, cmnt, rngC, rngR;
  24092. var sharedf = {};
  24093. var arrayf = [];
  24094. var temp_val;
  24095. var country;
  24096. var cell_valid = true;
  24097. var XFs = []; /* XF records */
  24098. var palette = [];
  24099. var Workbook = ({ Sheets:[], WBProps:{date1904:false}, Views:[{}] }), wsprops = {};
  24100. var get_rgb = function getrgb(icv) {
  24101. if(icv < 8) return XLSIcv[icv];
  24102. if(icv < 64) return palette[icv-8] || XLSIcv[icv];
  24103. return XLSIcv[icv];
  24104. };
  24105. var process_cell_style = function pcs(cell, line, options) {
  24106. var xfd = line.XF.data;
  24107. if(!xfd || !xfd.patternType || !options || !options.cellStyles) return;
  24108. line.s = ({});
  24109. line.s.patternType = xfd.patternType;
  24110. var t;
  24111. if((t = rgb2Hex(get_rgb(xfd.icvFore)))) { line.s.fgColor = {rgb:t}; }
  24112. if((t = rgb2Hex(get_rgb(xfd.icvBack)))) { line.s.bgColor = {rgb:t}; }
  24113. };
  24114. var addcell = function addcell(cell, line, options) {
  24115. if(file_depth > 1) return;
  24116. if(options.sheetRows && cell.r >= options.sheetRows) cell_valid = false;
  24117. if(!cell_valid) return;
  24118. if(options.cellStyles && line.XF && line.XF.data) process_cell_style(cell, line, options);
  24119. delete line.ixfe; delete line.XF;
  24120. lastcell = cell;
  24121. last_cell = encode_cell(cell);
  24122. if(!range || !range.s || !range.e) range = {s:{r:0,c:0},e:{r:0,c:0}};
  24123. if(cell.r < range.s.r) range.s.r = cell.r;
  24124. if(cell.c < range.s.c) range.s.c = cell.c;
  24125. if(cell.r + 1 > range.e.r) range.e.r = cell.r + 1;
  24126. if(cell.c + 1 > range.e.c) range.e.c = cell.c + 1;
  24127. if(options.cellFormula && line.f) {
  24128. for(var afi = 0; afi < arrayf.length; ++afi) {
  24129. if(arrayf[afi][0].s.c > cell.c || arrayf[afi][0].s.r > cell.r) continue;
  24130. if(arrayf[afi][0].e.c < cell.c || arrayf[afi][0].e.r < cell.r) continue;
  24131. line.F = encode_range(arrayf[afi][0]);
  24132. if(arrayf[afi][0].s.c != cell.c || arrayf[afi][0].s.r != cell.r) delete line.f;
  24133. if(line.f) line.f = "" + stringify_formula(arrayf[afi][1], range, cell, supbooks, opts);
  24134. break;
  24135. }
  24136. }
  24137. {
  24138. if(options.dense) {
  24139. if(!out[cell.r]) out[cell.r] = [];
  24140. out[cell.r][cell.c] = line;
  24141. } else out[last_cell] = line;
  24142. }
  24143. };
  24144. var opts = ({
  24145. enc: false, // encrypted
  24146. sbcch: 0, // cch in the preceding SupBook
  24147. snames: [], // sheetnames
  24148. sharedf: sharedf, // shared formulae by address
  24149. arrayf: arrayf, // array formulae array
  24150. rrtabid: [], // RRTabId
  24151. lastuser: "", // Last User from WriteAccess
  24152. biff: 8, // BIFF version
  24153. codepage: 0, // CP from CodePage record
  24154. winlocked: 0, // fLockWn from WinProtect
  24155. cellStyles: !!options && !!options.cellStyles,
  24156. WTF: !!options && !!options.wtf
  24157. });
  24158. if(options.password) opts.password = options.password;
  24159. var themes;
  24160. var merges = [];
  24161. var objects = [];
  24162. var colinfo = [], rowinfo = [];
  24163. // eslint-disable-next-line no-unused-vars
  24164. var defwidth = 0, defheight = 0; // twips / MDW respectively
  24165. var seencol = false;
  24166. var supbooks = ([]); // 1-indexed, will hold extern names
  24167. supbooks.SheetNames = opts.snames;
  24168. supbooks.sharedf = opts.sharedf;
  24169. supbooks.arrayf = opts.arrayf;
  24170. supbooks.names = [];
  24171. supbooks.XTI = [];
  24172. var last_Rn = '';
  24173. var file_depth = 0; /* TODO: make a real stack */
  24174. var BIFF2Fmt = 0, BIFF2FmtTable = [];
  24175. var FilterDatabases = []; /* TODO: sort out supbooks and process elsewhere */
  24176. var last_lbl;
  24177. /* explicit override for some broken writers */
  24178. opts.codepage = 1200;
  24179. set_cp(1200);
  24180. var seen_codepage = false;
  24181. while(blob.l < blob.length - 1) {
  24182. var s = blob.l;
  24183. var RecordType = blob.read_shift(2);
  24184. if(RecordType === 0 && last_Rn === 'EOF') break;
  24185. var length = (blob.l === blob.length ? 0 : blob.read_shift(2));
  24186. var R = XLSRecordEnum[RecordType];
  24187. //console.log(RecordType.toString(16), RecordType, R, blob.l, length, blob.length);
  24188. //if(!R) console.log(blob.slice(blob.l, blob.l + length));
  24189. if(R && R.f) {
  24190. if(options.bookSheets) {
  24191. if(last_Rn === 'BoundSheet8' && R.n !== 'BoundSheet8') break;
  24192. }
  24193. last_Rn = R.n;
  24194. if(R.r === 2 || R.r == 12) {
  24195. var rt = blob.read_shift(2); length -= 2;
  24196. if(!opts.enc && rt !== RecordType && (((rt&0xFF)<<8)|(rt>>8)) !== RecordType) throw new Error("rt mismatch: " + rt + "!=" + RecordType);
  24197. if(R.r == 12){ blob.l += 10; length -= 10; } // skip FRT
  24198. }
  24199. //console.error(R,blob.l,length,blob.length);
  24200. var val;
  24201. if(R.n === 'EOF') val = R.f(blob, length, opts);
  24202. else val = slurp(R, blob, length, opts);
  24203. var Rn = R.n;
  24204. if(file_depth == 0 && Rn != 'BOF') continue;
  24205. /* nested switch statements to workaround V8 128 limit */
  24206. switch(Rn) {
  24207. /* Workbook Options */
  24208. case 'Date1904':
  24209. wb.opts.Date1904 = Workbook.WBProps.date1904 = val; break;
  24210. case 'WriteProtect': wb.opts.WriteProtect = true; break;
  24211. case 'FilePass':
  24212. if(!opts.enc) blob.l = 0;
  24213. opts.enc = val;
  24214. if(!options.password) throw new Error("File is password-protected");
  24215. if(val.valid == null) throw new Error("Encryption scheme unsupported");
  24216. if(!val.valid) throw new Error("Password is incorrect");
  24217. break;
  24218. case 'WriteAccess': opts.lastuser = val; break;
  24219. case 'FileSharing': break; //TODO
  24220. case 'CodePage':
  24221. /* overrides based on test cases */
  24222. switch(val) {
  24223. case 0x5212: val = 1200; break;
  24224. case 0x8000: val = 10000; break;
  24225. case 0x8001: val = 1252; break;
  24226. }
  24227. set_cp(opts.codepage = val);
  24228. seen_codepage = true;
  24229. break;
  24230. case 'RRTabId': opts.rrtabid = val; break;
  24231. case 'WinProtect': opts.winlocked = val; break;
  24232. case 'Template': break; // TODO
  24233. case 'BookBool': break; // TODO
  24234. case 'UsesELFs': break;
  24235. case 'MTRSettings': break;
  24236. case 'RefreshAll':
  24237. case 'CalcCount':
  24238. case 'CalcDelta':
  24239. case 'CalcIter':
  24240. case 'CalcMode':
  24241. case 'CalcPrecision':
  24242. case 'CalcSaveRecalc':
  24243. wb.opts[Rn] = val; break;
  24244. case 'CalcRefMode': opts.CalcRefMode = val; break; // TODO: implement R1C1
  24245. case 'Uncalced': break;
  24246. case 'ForceFullCalculation': wb.opts.FullCalc = val; break;
  24247. case 'WsBool':
  24248. if(val.fDialog) out["!type"] = "dialog";
  24249. break; // TODO
  24250. case 'XF':
  24251. XFs.push(val); break;
  24252. case 'ExtSST': break; // TODO
  24253. case 'BookExt': break; // TODO
  24254. case 'RichTextStream': break;
  24255. case 'BkHim': break;
  24256. case 'SupBook':
  24257. supbooks.push([val]);
  24258. supbooks[supbooks.length-1].XTI = [];
  24259. break;
  24260. case 'ExternName':
  24261. supbooks[supbooks.length-1].push(val);
  24262. break;
  24263. case 'Index': break; // TODO
  24264. case 'Lbl':
  24265. last_lbl = ({
  24266. Name: val.Name,
  24267. Ref: stringify_formula(val.rgce,range,null,supbooks,opts)
  24268. });
  24269. if(val.itab > 0) last_lbl.Sheet = val.itab - 1;
  24270. supbooks.names.push(last_lbl);
  24271. if(!supbooks[0]) { supbooks[0] = []; supbooks[0].XTI = []; }
  24272. supbooks[supbooks.length-1].push(val);
  24273. if(val.Name == "_xlnm._FilterDatabase" && val.itab > 0)
  24274. if(val.rgce && val.rgce[0] && val.rgce[0][0] && val.rgce[0][0][0] == 'PtgArea3d')
  24275. FilterDatabases[val.itab - 1] = { ref: encode_range(val.rgce[0][0][1][2]) };
  24276. break;
  24277. case 'ExternCount': opts.ExternCount = val; break;
  24278. case 'ExternSheet':
  24279. if(supbooks.length == 0) { supbooks[0] = []; supbooks[0].XTI = []; }
  24280. supbooks[supbooks.length - 1].XTI = supbooks[supbooks.length - 1].XTI.concat(val); supbooks.XTI = supbooks.XTI.concat(val); break;
  24281. case 'NameCmt':
  24282. /* TODO: search for correct name */
  24283. if(opts.biff < 8) break;
  24284. if(last_lbl != null) last_lbl.Comment = val[1];
  24285. break;
  24286. case 'Protect': out["!protect"] = val; break; /* for sheet or book */
  24287. case 'Password': if(val !== 0 && opts.WTF) console.error("Password verifier: " + val); break;
  24288. case 'Prot4Rev': case 'Prot4RevPass': break; /*TODO: Revision Control*/
  24289. case 'BoundSheet8': {
  24290. Directory[val.pos] = val;
  24291. opts.snames.push(val.name);
  24292. } break;
  24293. case 'EOF': {
  24294. if(--file_depth) break;
  24295. if(range.e) {
  24296. if(range.e.r > 0 && range.e.c > 0) {
  24297. range.e.r--; range.e.c--;
  24298. out["!ref"] = encode_range(range);
  24299. if(options.sheetRows && options.sheetRows <= range.e.r) {
  24300. var tmpri = range.e.r;
  24301. range.e.r = options.sheetRows - 1;
  24302. out["!fullref"] = out["!ref"];
  24303. out["!ref"] = encode_range(range);
  24304. range.e.r = tmpri;
  24305. }
  24306. range.e.r++; range.e.c++;
  24307. }
  24308. if(merges.length > 0) out["!merges"] = merges;
  24309. if(objects.length > 0) out["!objects"] = objects;
  24310. if(colinfo.length > 0) out["!cols"] = colinfo;
  24311. if(rowinfo.length > 0) out["!rows"] = rowinfo;
  24312. Workbook.Sheets.push(wsprops);
  24313. }
  24314. if(cur_sheet === "") Preamble = out; else Sheets[cur_sheet] = out;
  24315. out = ((options.dense ? [] : {}));
  24316. } break;
  24317. case 'BOF': {
  24318. if(opts.biff === 8) opts.biff = {
  24319. 0x0009:2,
  24320. 0x0209:3,
  24321. 0x0409:4
  24322. }[RecordType] || {
  24323. 0x0200:2,
  24324. 0x0300:3,
  24325. 0x0400:4,
  24326. 0x0500:5,
  24327. 0x0600:8,
  24328. 0x0002:2,
  24329. 0x0007:2
  24330. }[val.BIFFVer] || 8;
  24331. if(opts.biff == 8 && val.BIFFVer == 0 && val.dt == 16) opts.biff = 2;
  24332. if(file_depth++) break;
  24333. cell_valid = true;
  24334. out = ((options.dense ? [] : {}));
  24335. if(opts.biff < 8 && !seen_codepage) { seen_codepage = true; set_cp(opts.codepage = options.codepage || 1252); }
  24336. if(opts.biff < 5) {
  24337. if(cur_sheet === "") cur_sheet = "Sheet1";
  24338. range = {s:{r:0,c:0},e:{r:0,c:0}};
  24339. /* fake BoundSheet8 */
  24340. var fakebs8 = {pos: blob.l - length, name:cur_sheet};
  24341. Directory[fakebs8.pos] = fakebs8;
  24342. opts.snames.push(cur_sheet);
  24343. }
  24344. else cur_sheet = (Directory[s] || {name:""}).name;
  24345. if(val.dt == 0x20) out["!type"] = "chart";
  24346. if(val.dt == 0x40) out["!type"] = "macro";
  24347. merges = [];
  24348. objects = [];
  24349. opts.arrayf = arrayf = [];
  24350. colinfo = []; rowinfo = [];
  24351. defwidth = defheight = 0;
  24352. seencol = false;
  24353. wsprops = {Hidden:(Directory[s]||{hs:0}).hs, name:cur_sheet };
  24354. } break;
  24355. case 'Number': case 'BIFF2NUM': case 'BIFF2INT': {
  24356. if(out["!type"] == "chart") if(options.dense ? (out[val.r]||[])[val.c]: out[encode_cell({c:val.c, r:val.r})]) ++val.c;
  24357. temp_val = ({ixfe: val.ixfe, XF: XFs[val.ixfe]||{}, v:val.val, t:'n'});
  24358. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  24359. safe_format_xf(temp_val, options, wb.opts.Date1904);
  24360. addcell({c:val.c, r:val.r}, temp_val, options);
  24361. } break;
  24362. case 'BoolErr': {
  24363. temp_val = ({ixfe: val.ixfe, XF: XFs[val.ixfe], v:val.val, t:val.t});
  24364. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  24365. safe_format_xf(temp_val, options, wb.opts.Date1904);
  24366. addcell({c:val.c, r:val.r}, temp_val, options);
  24367. } break;
  24368. case 'RK': {
  24369. temp_val = ({ixfe: val.ixfe, XF: XFs[val.ixfe], v:val.rknum, t:'n'});
  24370. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  24371. safe_format_xf(temp_val, options, wb.opts.Date1904);
  24372. addcell({c:val.c, r:val.r}, temp_val, options);
  24373. } break;
  24374. case 'MulRk': {
  24375. for(var j = val.c; j <= val.C; ++j) {
  24376. var ixfe = val.rkrec[j-val.c][0];
  24377. temp_val= ({ixfe:ixfe, XF:XFs[ixfe], v:val.rkrec[j-val.c][1], t:'n'});
  24378. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  24379. safe_format_xf(temp_val, options, wb.opts.Date1904);
  24380. addcell({c:j, r:val.r}, temp_val, options);
  24381. }
  24382. } break;
  24383. case 'Formula': {
  24384. if(val.val == 'String') { last_formula = val; break; }
  24385. temp_val = make_cell(val.val, val.cell.ixfe, val.tt);
  24386. temp_val.XF = XFs[temp_val.ixfe];
  24387. if(options.cellFormula) {
  24388. var _f = val.formula;
  24389. if(_f && _f[0] && _f[0][0] && _f[0][0][0] == 'PtgExp') {
  24390. var _fr = _f[0][0][1][0], _fc = _f[0][0][1][1];
  24391. var _fe = encode_cell({r:_fr, c:_fc});
  24392. if(sharedf[_fe]) temp_val.f = ""+stringify_formula(val.formula,range,val.cell,supbooks, opts);
  24393. else temp_val.F = ((options.dense ? (out[_fr]||[])[_fc]: out[_fe]) || {}).F;
  24394. } else temp_val.f = ""+stringify_formula(val.formula,range,val.cell,supbooks, opts);
  24395. }
  24396. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  24397. safe_format_xf(temp_val, options, wb.opts.Date1904);
  24398. addcell(val.cell, temp_val, options);
  24399. last_formula = val;
  24400. } break;
  24401. case 'String': {
  24402. if(last_formula) { /* technically always true */
  24403. last_formula.val = val;
  24404. temp_val = make_cell(val, last_formula.cell.ixfe, 's');
  24405. temp_val.XF = XFs[temp_val.ixfe];
  24406. if(options.cellFormula) {
  24407. temp_val.f = ""+stringify_formula(last_formula.formula, range, last_formula.cell, supbooks, opts);
  24408. }
  24409. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  24410. safe_format_xf(temp_val, options, wb.opts.Date1904);
  24411. addcell(last_formula.cell, temp_val, options);
  24412. last_formula = null;
  24413. } else throw new Error("String record expects Formula");
  24414. } break;
  24415. case 'Array': {
  24416. arrayf.push(val);
  24417. var _arraystart = encode_cell(val[0].s);
  24418. cc = options.dense ? (out[val[0].s.r]||[])[val[0].s.c] : out[_arraystart];
  24419. if(options.cellFormula && cc) {
  24420. if(!last_formula) break; /* technically unreachable */
  24421. if(!_arraystart || !cc) break;
  24422. cc.f = ""+stringify_formula(val[1], range, val[0], supbooks, opts);
  24423. cc.F = encode_range(val[0]);
  24424. }
  24425. } break;
  24426. case 'ShrFmla': {
  24427. if(!cell_valid) break;
  24428. if(!options.cellFormula) break;
  24429. if(last_cell) {
  24430. /* TODO: capture range */
  24431. if(!last_formula) break; /* technically unreachable */
  24432. sharedf[encode_cell(last_formula.cell)]= val[0];
  24433. cc = options.dense ? (out[last_formula.cell.r]||[])[last_formula.cell.c] : out[encode_cell(last_formula.cell)];
  24434. (cc||{}).f = ""+stringify_formula(val[0], range, lastcell, supbooks, opts);
  24435. }
  24436. } break;
  24437. case 'LabelSst':
  24438. temp_val=make_cell(sst[val.isst].t, val.ixfe, 's');
  24439. temp_val.XF = XFs[temp_val.ixfe];
  24440. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  24441. safe_format_xf(temp_val, options, wb.opts.Date1904);
  24442. addcell({c:val.c, r:val.r}, temp_val, options);
  24443. break;
  24444. case 'Blank': if(options.sheetStubs) {
  24445. temp_val = ({ixfe: val.ixfe, XF: XFs[val.ixfe], t:'z'});
  24446. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  24447. safe_format_xf(temp_val, options, wb.opts.Date1904);
  24448. addcell({c:val.c, r:val.r}, temp_val, options);
  24449. } break;
  24450. case 'MulBlank': if(options.sheetStubs) {
  24451. for(var _j = val.c; _j <= val.C; ++_j) {
  24452. var _ixfe = val.ixfe[_j-val.c];
  24453. temp_val= ({ixfe:_ixfe, XF:XFs[_ixfe], t:'z'});
  24454. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  24455. safe_format_xf(temp_val, options, wb.opts.Date1904);
  24456. addcell({c:_j, r:val.r}, temp_val, options);
  24457. }
  24458. } break;
  24459. case 'RString':
  24460. case 'Label': case 'BIFF2STR':
  24461. temp_val=make_cell(val.val, val.ixfe, 's');
  24462. temp_val.XF = XFs[temp_val.ixfe];
  24463. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  24464. safe_format_xf(temp_val, options, wb.opts.Date1904);
  24465. addcell({c:val.c, r:val.r}, temp_val, options);
  24466. break;
  24467. case 'Dimensions': {
  24468. if(file_depth === 1) range = val; /* TODO: stack */
  24469. } break;
  24470. case 'SST': {
  24471. sst = val;
  24472. } break;
  24473. case 'Format': { /* val = [id, fmt] */
  24474. if(opts.biff == 4) {
  24475. BIFF2FmtTable[BIFF2Fmt++] = val[1];
  24476. for(var b4idx = 0; b4idx < BIFF2Fmt + 163; ++b4idx) if(SSF._table[b4idx] == val[1]) break;
  24477. if(b4idx >= 163) SSF.load(val[1], BIFF2Fmt + 163);
  24478. }
  24479. else SSF.load(val[1], val[0]);
  24480. } break;
  24481. case 'BIFF2FORMAT': {
  24482. BIFF2FmtTable[BIFF2Fmt++] = val;
  24483. for(var b2idx = 0; b2idx < BIFF2Fmt + 163; ++b2idx) if(SSF._table[b2idx] == val) break;
  24484. if(b2idx >= 163) SSF.load(val, BIFF2Fmt + 163);
  24485. } break;
  24486. case 'MergeCells': merges = merges.concat(val); break;
  24487. case 'Obj': objects[val.cmo[0]] = opts.lastobj = val; break;
  24488. case 'TxO': opts.lastobj.TxO = val; break;
  24489. case 'ImData': opts.lastobj.ImData = val; break;
  24490. case 'HLink': {
  24491. for(rngR = val[0].s.r; rngR <= val[0].e.r; ++rngR)
  24492. for(rngC = val[0].s.c; rngC <= val[0].e.c; ++rngC) {
  24493. cc = options.dense ? (out[rngR]||[])[rngC] : out[encode_cell({c:rngC,r:rngR})];
  24494. if(cc) cc.l = val[1];
  24495. }
  24496. } break;
  24497. case 'HLinkTooltip': {
  24498. for(rngR = val[0].s.r; rngR <= val[0].e.r; ++rngR)
  24499. for(rngC = val[0].s.c; rngC <= val[0].e.c; ++rngC) {
  24500. cc = options.dense ? (out[rngR]||[])[rngC] : out[encode_cell({c:rngC,r:rngR})];
  24501. if(cc && cc.l) cc.l.Tooltip = val[1];
  24502. }
  24503. } break;
  24504. /* Comments */
  24505. case 'Note': {
  24506. if(opts.biff <= 5 && opts.biff >= 2) break; /* TODO: BIFF5 */
  24507. cc = options.dense ? (out[val[0].r]||[])[val[0].c] : out[encode_cell(val[0])];
  24508. var noteobj = objects[val[2]];
  24509. if(!cc) break;
  24510. if(!cc.c) cc.c = [];
  24511. cmnt = {a:val[1],t:noteobj.TxO.t};
  24512. cc.c.push(cmnt);
  24513. } break;
  24514. default: switch(R.n) { /* nested */
  24515. case 'ClrtClient': break;
  24516. case 'XFExt': update_xfext(XFs[val.ixfe], val.ext); break;
  24517. case 'DefColWidth': defwidth = val; break;
  24518. case 'DefaultRowHeight': defheight = val[1]; break; // TODO: flags
  24519. case 'ColInfo': {
  24520. if(!opts.cellStyles) break;
  24521. while(val.e >= val.s) {
  24522. colinfo[val.e--] = { width: val.w/256 };
  24523. if(!seencol) { seencol = true; find_mdw_colw(val.w/256); }
  24524. process_col(colinfo[val.e+1]);
  24525. }
  24526. } break;
  24527. case 'Row': {
  24528. var rowobj = {};
  24529. if(val.level != null) { rowinfo[val.r] = rowobj; rowobj.level = val.level; }
  24530. if(val.hidden) { rowinfo[val.r] = rowobj; rowobj.hidden = true; }
  24531. if(val.hpt) {
  24532. rowinfo[val.r] = rowobj;
  24533. rowobj.hpt = val.hpt; rowobj.hpx = pt2px(val.hpt);
  24534. }
  24535. } break;
  24536. case 'LeftMargin':
  24537. case 'RightMargin':
  24538. case 'TopMargin':
  24539. case 'BottomMargin':
  24540. if(!out['!margins']) default_margins(out['!margins'] = {});
  24541. out['!margins'][Rn.slice(0,-6).toLowerCase()] = val;
  24542. break;
  24543. case 'Setup': // TODO
  24544. if(!out['!margins']) default_margins(out['!margins'] = {});
  24545. out['!margins'].header = val.header;
  24546. out['!margins'].footer = val.footer;
  24547. break;
  24548. case 'Window2': // TODO
  24549. // $FlowIgnore
  24550. if(val.RTL) Workbook.Views[0].RTL = true;
  24551. break;
  24552. case 'Header': break; // TODO
  24553. case 'Footer': break; // TODO
  24554. case 'HCenter': break; // TODO
  24555. case 'VCenter': break; // TODO
  24556. case 'Pls': break; // TODO
  24557. case 'GCW': break;
  24558. case 'LHRecord': break;
  24559. case 'DBCell': break; // TODO
  24560. case 'EntExU2': break; // TODO
  24561. case 'SxView': break; // TODO
  24562. case 'Sxvd': break; // TODO
  24563. case 'SXVI': break; // TODO
  24564. case 'SXVDEx': break; // TODO
  24565. case 'SxIvd': break; // TODO
  24566. case 'SXString': break; // TODO
  24567. case 'Sync': break;
  24568. case 'Addin': break;
  24569. case 'SXDI': break; // TODO
  24570. case 'SXLI': break; // TODO
  24571. case 'SXEx': break; // TODO
  24572. case 'QsiSXTag': break; // TODO
  24573. case 'Selection': break;
  24574. case 'Feat': break;
  24575. case 'FeatHdr': case 'FeatHdr11': break;
  24576. case 'Feature11': case 'Feature12': case 'List12': break;
  24577. case 'Country': country = val; break;
  24578. case 'RecalcId': break;
  24579. case 'DxGCol': break; // TODO: htmlify
  24580. case 'Fbi': case 'Fbi2': case 'GelFrame': break;
  24581. case 'Font': break; // TODO
  24582. case 'XFCRC': break; // TODO
  24583. case 'Style': break; // TODO
  24584. case 'StyleExt': break; // TODO
  24585. case 'Palette': palette = val; break;
  24586. case 'Theme': themes = val; break;
  24587. /* Protection */
  24588. case 'ScenarioProtect': break;
  24589. case 'ObjProtect': break;
  24590. /* Conditional Formatting */
  24591. case 'CondFmt12': break;
  24592. /* Table */
  24593. case 'Table': break; // TODO
  24594. case 'TableStyles': break; // TODO
  24595. case 'TableStyle': break; // TODO
  24596. case 'TableStyleElement': break; // TODO
  24597. /* PivotTable */
  24598. case 'SXStreamID': break; // TODO
  24599. case 'SXVS': break; // TODO
  24600. case 'DConRef': break; // TODO
  24601. case 'SXAddl': break; // TODO
  24602. case 'DConBin': break; // TODO
  24603. case 'DConName': break; // TODO
  24604. case 'SXPI': break; // TODO
  24605. case 'SxFormat': break; // TODO
  24606. case 'SxSelect': break; // TODO
  24607. case 'SxRule': break; // TODO
  24608. case 'SxFilt': break; // TODO
  24609. case 'SxItm': break; // TODO
  24610. case 'SxDXF': break; // TODO
  24611. /* Scenario Manager */
  24612. case 'ScenMan': break;
  24613. /* Data Consolidation */
  24614. case 'DCon': break;
  24615. /* Watched Cell */
  24616. case 'CellWatch': break;
  24617. /* Print Settings */
  24618. case 'PrintRowCol': break;
  24619. case 'PrintGrid': break;
  24620. case 'PrintSize': break;
  24621. case 'XCT': break;
  24622. case 'CRN': break;
  24623. case 'Scl': {
  24624. //console.log("Zoom Level:", val[0]/val[1],val);
  24625. } break;
  24626. case 'SheetExt': {
  24627. /* empty */
  24628. } break;
  24629. case 'SheetExtOptional': {
  24630. /* empty */
  24631. } break;
  24632. /* VBA */
  24633. case 'ObNoMacros': {
  24634. /* empty */
  24635. } break;
  24636. case 'ObProj': {
  24637. /* empty */
  24638. } break;
  24639. case 'CodeName': {
  24640. if(!cur_sheet) Workbook.WBProps.CodeName = val || "ThisWorkbook";
  24641. else wsprops.CodeName = val || wsprops.name;
  24642. } break;
  24643. case 'GUIDTypeLib': {
  24644. /* empty */
  24645. } break;
  24646. case 'WOpt': break; // TODO: WTF?
  24647. case 'PhoneticInfo': break;
  24648. case 'OleObjectSize': break;
  24649. /* Differential Formatting */
  24650. case 'DXF': case 'DXFN': case 'DXFN12': case 'DXFN12List': case 'DXFN12NoCB': break;
  24651. /* Data Validation */
  24652. case 'Dv': case 'DVal': break;
  24653. /* Data Series */
  24654. case 'BRAI': case 'Series': case 'SeriesText': break;
  24655. /* Data Connection */
  24656. case 'DConn': break;
  24657. case 'DbOrParamQry': break;
  24658. case 'DBQueryExt': break;
  24659. case 'OleDbConn': break;
  24660. case 'ExtString': break;
  24661. /* Formatting */
  24662. case 'IFmtRecord': break;
  24663. case 'CondFmt': case 'CF': case 'CF12': case 'CFEx': break;
  24664. /* Explicitly Ignored */
  24665. case 'Excel9File': break;
  24666. case 'Units': break;
  24667. case 'InterfaceHdr': case 'Mms': case 'InterfaceEnd': case 'DSF': break;
  24668. case 'BuiltInFnGroupCount': /* 2.4.30 0x0E or 0x10 but excel 2011 generates 0x11? */ break;
  24669. /* View Stuff */
  24670. case 'Window1': case 'HideObj': case 'GridSet': case 'Guts':
  24671. case 'UserBView': case 'UserSViewBegin': case 'UserSViewEnd':
  24672. case 'Pane': break;
  24673. default: switch(R.n) { /* nested */
  24674. /* Chart */
  24675. case 'Dat':
  24676. case 'Begin': case 'End':
  24677. case 'StartBlock': case 'EndBlock':
  24678. case 'Frame': case 'Area':
  24679. case 'Axis': case 'AxisLine': case 'Tick': break;
  24680. case 'AxesUsed':
  24681. case 'CrtLayout12': case 'CrtLayout12A': case 'CrtLink': case 'CrtLine': case 'CrtMlFrt': case 'CrtMlFrtContinue': break;
  24682. case 'LineFormat': case 'AreaFormat':
  24683. case 'Chart': case 'Chart3d': case 'Chart3DBarShape': case 'ChartFormat': case 'ChartFrtInfo': break;
  24684. case 'PlotArea': case 'PlotGrowth': break;
  24685. case 'SeriesList': case 'SerParent': case 'SerAuxTrend': break;
  24686. case 'DataFormat': case 'SerToCrt': case 'FontX': break;
  24687. case 'CatSerRange': case 'AxcExt': case 'SerFmt': break;
  24688. case 'ShtProps': break;
  24689. case 'DefaultText': case 'Text': case 'CatLab': break;
  24690. case 'DataLabExtContents': break;
  24691. case 'Legend': case 'LegendException': break;
  24692. case 'Pie': case 'Scatter': break;
  24693. case 'PieFormat': case 'MarkerFormat': break;
  24694. case 'StartObject': case 'EndObject': break;
  24695. case 'AlRuns': case 'ObjectLink': break;
  24696. case 'SIIndex': break;
  24697. case 'AttachedLabel': case 'YMult': break;
  24698. /* Chart Group */
  24699. case 'Line': case 'Bar': break;
  24700. case 'Surf': break;
  24701. /* Axis Group */
  24702. case 'AxisParent': break;
  24703. case 'Pos': break;
  24704. case 'ValueRange': break;
  24705. /* Pivot Chart */
  24706. case 'SXViewEx9': break; // TODO
  24707. case 'SXViewLink': break;
  24708. case 'PivotChartBits': break;
  24709. case 'SBaseRef': break;
  24710. case 'TextPropsStream': break;
  24711. /* Chart Misc */
  24712. case 'LnExt': break;
  24713. case 'MkrExt': break;
  24714. case 'CrtCoopt': break;
  24715. /* Query Table */
  24716. case 'Qsi': case 'Qsif': case 'Qsir': case 'QsiSXTag': break;
  24717. case 'TxtQry': break;
  24718. /* Filter */
  24719. case 'FilterMode': break;
  24720. case 'AutoFilter': case 'AutoFilterInfo': break;
  24721. case 'AutoFilter12': break;
  24722. case 'DropDownObjIds': break;
  24723. case 'Sort': break;
  24724. case 'SortData': break;
  24725. /* Drawing */
  24726. case 'ShapePropsStream': break;
  24727. case 'MsoDrawing': case 'MsoDrawingGroup': case 'MsoDrawingSelection': break;
  24728. /* Pub Stuff */
  24729. case 'WebPub': case 'AutoWebPub': break;
  24730. /* Print Stuff */
  24731. case 'HeaderFooter': case 'HFPicture': case 'PLV':
  24732. case 'HorizontalPageBreaks': case 'VerticalPageBreaks': break;
  24733. /* Behavioral */
  24734. case 'Backup': case 'CompressPictures': case 'Compat12': break;
  24735. /* Should not Happen */
  24736. case 'Continue': case 'ContinueFrt12': break;
  24737. /* Future Records */
  24738. case 'FrtFontList': case 'FrtWrapper': break;
  24739. default: switch(R.n) { /* nested */
  24740. /* BIFF5 records */
  24741. case 'TabIdConf': case 'Radar': case 'RadarArea': case 'DropBar': case 'Intl': case 'CoordList': case 'SerAuxErrBar': break;
  24742. /* BIFF2-4 records */
  24743. case 'BIFF2FONTCLR': case 'BIFF2FMTCNT': case 'BIFF2FONTXTRA': break;
  24744. case 'BIFF2XF': case 'BIFF3XF': case 'BIFF4XF': break;
  24745. case 'BIFF4FMTCNT': case 'BIFF2ROW': case 'BIFF2WINDOW2': break;
  24746. /* Miscellaneous */
  24747. case 'SCENARIO': case 'DConBin': case 'PicF': case 'DataLabExt':
  24748. case 'Lel': case 'BopPop': case 'BopPopCustom': case 'RealTimeData':
  24749. case 'Name': break;
  24750. case 'LHNGraph': case 'FnGroupName': case 'AddMenu': case 'LPr': break;
  24751. case 'ListObj': case 'ListField': break;
  24752. case 'RRSort': break;
  24753. case 'BigName': break;
  24754. case 'ToolbarHdr': case 'ToolbarEnd': break;
  24755. case 'DDEObjName': break;
  24756. case 'FRTArchId$': break;
  24757. default: if(options.WTF) throw 'Unrecognized Record ' + R.n;
  24758. }}}}
  24759. } else blob.l += length;
  24760. }
  24761. wb.SheetNames=keys(Directory).sort(function(a,b) { return Number(a) - Number(b); }).map(function(x){return Directory[x].name;});
  24762. if(!options.bookSheets) wb.Sheets=Sheets;
  24763. if(wb.Sheets) FilterDatabases.forEach(function(r,i) { wb.Sheets[wb.SheetNames[i]]['!autofilter'] = r; });
  24764. wb.Preamble=Preamble;
  24765. wb.Strings = sst;
  24766. wb.SSF = SSF.get_table();
  24767. if(opts.enc) wb.Encryption = opts.enc;
  24768. if(themes) wb.Themes = themes;
  24769. wb.Metadata = {};
  24770. if(country !== undefined) wb.Metadata.Country = country;
  24771. if(supbooks.names.length > 0) Workbook.Names = supbooks.names;
  24772. wb.Workbook = Workbook;
  24773. return wb;
  24774. }
  24775. /* TODO: split props*/
  24776. var PSCLSID = {
  24777. SI: "e0859ff2f94f6810ab9108002b27b3d9",
  24778. DSI: "02d5cdd59c2e1b10939708002b2cf9ae",
  24779. UDI: "05d5cdd59c2e1b10939708002b2cf9ae"
  24780. };
  24781. function parse_xls_props(cfb, props, o) {
  24782. /* [MS-OSHARED] 2.3.3.2.2 Document Summary Information Property Set */
  24783. var DSI = CFB.find(cfb, '!DocumentSummaryInformation');
  24784. if(DSI && DSI.size > 0) try {
  24785. var DocSummary = parse_PropertySetStream(DSI, DocSummaryPIDDSI, PSCLSID.DSI);
  24786. for(var d in DocSummary) props[d] = DocSummary[d];
  24787. } catch(e) {if(o.WTF) throw e;/* empty */}
  24788. /* [MS-OSHARED] 2.3.3.2.1 Summary Information Property Set*/
  24789. var SI = CFB.find(cfb, '!SummaryInformation');
  24790. if(SI && SI.size > 0) try {
  24791. var Summary = parse_PropertySetStream(SI, SummaryPIDSI, PSCLSID.SI);
  24792. for(var s in Summary) if(props[s] == null) props[s] = Summary[s];
  24793. } catch(e) {if(o.WTF) throw e;/* empty */}
  24794. if(props.HeadingPairs && props.TitlesOfParts) {
  24795. load_props_pairs(props.HeadingPairs, props.TitlesOfParts, props, o);
  24796. delete props.HeadingPairs; delete props.TitlesOfParts;
  24797. }
  24798. }
  24799. function write_xls_props(wb, cfb) {
  24800. var DSEntries = [], SEntries = [], CEntries = [];
  24801. var i = 0, Keys;
  24802. if(wb.Props) {
  24803. Keys = keys(wb.Props);
  24804. // $FlowIgnore
  24805. for(i = 0; i < Keys.length; ++i) (DocSummaryRE.hasOwnProperty(Keys[i]) ? DSEntries : SummaryRE.hasOwnProperty(Keys[i]) ? SEntries : CEntries).push([Keys[i], wb.Props[Keys[i]]]);
  24806. }
  24807. if(wb.Custprops) {
  24808. Keys = keys(wb.Custprops);
  24809. // $FlowIgnore
  24810. for(i = 0; i < Keys.length; ++i) if(!(wb.Props||{}).hasOwnProperty(Keys[i])) (DocSummaryRE.hasOwnProperty(Keys[i]) ? DSEntries : SummaryRE.hasOwnProperty(Keys[i]) ? SEntries : CEntries).push([Keys[i], wb.Custprops[Keys[i]]]);
  24811. }
  24812. var CEntries2 = [];
  24813. for(i = 0; i < CEntries.length; ++i) {
  24814. if(XLSPSSkip.indexOf(CEntries[i][0]) > -1) continue;
  24815. if(CEntries[i][1] == null) continue;
  24816. CEntries2.push(CEntries[i]);
  24817. }
  24818. if(SEntries.length) CFB.utils.cfb_add(cfb, "/\u0005SummaryInformation", write_PropertySetStream(SEntries, PSCLSID.SI, SummaryRE, SummaryPIDSI));
  24819. if(DSEntries.length || CEntries2.length) CFB.utils.cfb_add(cfb, "/\u0005DocumentSummaryInformation", write_PropertySetStream(DSEntries, PSCLSID.DSI, DocSummaryRE, DocSummaryPIDDSI, CEntries2.length ? CEntries2 : null, PSCLSID.UDI));
  24820. }
  24821. function parse_xlscfb(cfb, options) {
  24822. if(!options) options = {};
  24823. fix_read_opts(options);
  24824. reset_cp();
  24825. if(options.codepage) set_ansi(options.codepage);
  24826. var CompObj, WB;
  24827. if(cfb.FullPaths) {
  24828. if(CFB.find(cfb, '/encryption')) throw new Error("File is password-protected");
  24829. CompObj = CFB.find(cfb, '!CompObj');
  24830. WB = CFB.find(cfb, '/Workbook') || CFB.find(cfb, '/Book');
  24831. } else {
  24832. switch(options.type) {
  24833. case 'base64': cfb = s2a(Base64.decode(cfb)); break;
  24834. case 'binary': cfb = s2a(cfb); break;
  24835. case 'buffer': break;
  24836. case 'array': if(!Array.isArray(cfb)) cfb = Array.prototype.slice.call(cfb); break;
  24837. }
  24838. prep_blob(cfb, 0);
  24839. WB = ({content: cfb});
  24840. }
  24841. var WorkbookP;
  24842. var _data;
  24843. if(CompObj) parse_compobj(CompObj);
  24844. if(options.bookProps && !options.bookSheets) WorkbookP = ({});
  24845. else {
  24846. var T = has_buf ? 'buffer' : 'array';
  24847. if(WB && WB.content) WorkbookP = parse_workbook(WB.content, options);
  24848. /* Quattro Pro 7-8 */
  24849. else if((_data=CFB.find(cfb, 'PerfectOffice_MAIN')) && _data.content) WorkbookP = WK_.to_workbook(_data.content, (options.type = T, options));
  24850. /* Quattro Pro 9 */
  24851. else if((_data=CFB.find(cfb, 'NativeContent_MAIN')) && _data.content) WorkbookP = WK_.to_workbook(_data.content, (options.type = T, options));
  24852. else throw new Error("Cannot find Workbook stream");
  24853. if(options.bookVBA && cfb.FullPaths && CFB.find(cfb, '/_VBA_PROJECT_CUR/VBA/dir')) WorkbookP.vbaraw = make_vba_xls(cfb);
  24854. }
  24855. var props = {};
  24856. if(cfb.FullPaths) parse_xls_props(cfb, props, options);
  24857. WorkbookP.Props = WorkbookP.Custprops = props; /* TODO: split up properties */
  24858. if(options.bookFiles) WorkbookP.cfb = cfb;
  24859. /*WorkbookP.CompObjP = CompObjP; // TODO: storage? */
  24860. return WorkbookP;
  24861. }
  24862. function write_xlscfb(wb, opts) {
  24863. var o = opts || {};
  24864. var cfb = CFB.utils.cfb_new({root:"R"});
  24865. var wbpath = "/Workbook";
  24866. switch(o.bookType || "xls") {
  24867. case "xls": o.bookType = "biff8";
  24868. /* falls through */
  24869. case "xla": if(!o.bookType) o.bookType = "xla";
  24870. /* falls through */
  24871. case "biff8": wbpath = "/Workbook"; o.biff = 8; break;
  24872. case "biff5": wbpath = "/Book"; o.biff = 5; break;
  24873. default: throw new Error("invalid type " + o.bookType + " for XLS CFB");
  24874. }
  24875. CFB.utils.cfb_add(cfb, wbpath, write_biff_buf(wb, o));
  24876. if(o.biff == 8 && (wb.Props || wb.Custprops)) write_xls_props(wb, cfb);
  24877. // TODO: SI, DSI, CO
  24878. if(o.biff == 8 && wb.vbaraw) fill_vba_xls(cfb, CFB.read(wb.vbaraw, {type: typeof wb.vbaraw == "string" ? "binary" : "buffer"}));
  24879. return cfb;
  24880. }
  24881. /* [MS-XLSB] 2.3 Record Enumeration */
  24882. var XLSBRecordEnum = {
  24883. 0x0000: { n:"BrtRowHdr", f:parse_BrtRowHdr },
  24884. 0x0001: { n:"BrtCellBlank", f:parse_BrtCellBlank },
  24885. 0x0002: { n:"BrtCellRk", f:parse_BrtCellRk },
  24886. 0x0003: { n:"BrtCellError", f:parse_BrtCellError },
  24887. 0x0004: { n:"BrtCellBool", f:parse_BrtCellBool },
  24888. 0x0005: { n:"BrtCellReal", f:parse_BrtCellReal },
  24889. 0x0006: { n:"BrtCellSt", f:parse_BrtCellSt },
  24890. 0x0007: { n:"BrtCellIsst", f:parse_BrtCellIsst },
  24891. 0x0008: { n:"BrtFmlaString", f:parse_BrtFmlaString },
  24892. 0x0009: { n:"BrtFmlaNum", f:parse_BrtFmlaNum },
  24893. 0x000A: { n:"BrtFmlaBool", f:parse_BrtFmlaBool },
  24894. 0x000B: { n:"BrtFmlaError", f:parse_BrtFmlaError },
  24895. 0x0010: { n:"BrtFRTArchID$", f:parse_BrtFRTArchID$ },
  24896. 0x0013: { n:"BrtSSTItem", f:parse_RichStr },
  24897. 0x0014: { n:"BrtPCDIMissing" },
  24898. 0x0015: { n:"BrtPCDINumber" },
  24899. 0x0016: { n:"BrtPCDIBoolean" },
  24900. 0x0017: { n:"BrtPCDIError" },
  24901. 0x0018: { n:"BrtPCDIString" },
  24902. 0x0019: { n:"BrtPCDIDatetime" },
  24903. 0x001A: { n:"BrtPCDIIndex" },
  24904. 0x001B: { n:"BrtPCDIAMissing" },
  24905. 0x001C: { n:"BrtPCDIANumber" },
  24906. 0x001D: { n:"BrtPCDIABoolean" },
  24907. 0x001E: { n:"BrtPCDIAError" },
  24908. 0x001F: { n:"BrtPCDIAString" },
  24909. 0x0020: { n:"BrtPCDIADatetime" },
  24910. 0x0021: { n:"BrtPCRRecord" },
  24911. 0x0022: { n:"BrtPCRRecordDt" },
  24912. 0x0023: { n:"BrtFRTBegin" },
  24913. 0x0024: { n:"BrtFRTEnd" },
  24914. 0x0025: { n:"BrtACBegin" },
  24915. 0x0026: { n:"BrtACEnd" },
  24916. 0x0027: { n:"BrtName", f:parse_BrtName },
  24917. 0x0028: { n:"BrtIndexRowBlock" },
  24918. 0x002A: { n:"BrtIndexBlock" },
  24919. 0x002B: { n:"BrtFont", f:parse_BrtFont },
  24920. 0x002C: { n:"BrtFmt", f:parse_BrtFmt },
  24921. 0x002D: { n:"BrtFill", f:parse_BrtFill },
  24922. 0x002E: { n:"BrtBorder", f:parse_BrtBorder },
  24923. 0x002F: { n:"BrtXF", f:parse_BrtXF },
  24924. 0x0030: { n:"BrtStyle" },
  24925. 0x0031: { n:"BrtCellMeta" },
  24926. 0x0032: { n:"BrtValueMeta" },
  24927. 0x0033: { n:"BrtMdb" },
  24928. 0x0034: { n:"BrtBeginFmd" },
  24929. 0x0035: { n:"BrtEndFmd" },
  24930. 0x0036: { n:"BrtBeginMdx" },
  24931. 0x0037: { n:"BrtEndMdx" },
  24932. 0x0038: { n:"BrtBeginMdxTuple" },
  24933. 0x0039: { n:"BrtEndMdxTuple" },
  24934. 0x003A: { n:"BrtMdxMbrIstr" },
  24935. 0x003B: { n:"BrtStr" },
  24936. 0x003C: { n:"BrtColInfo", f:parse_ColInfo },
  24937. 0x003E: { n:"BrtCellRString" },
  24938. 0x003F: { n:"BrtCalcChainItem$", f:parse_BrtCalcChainItem$ },
  24939. 0x0040: { n:"BrtDVal" },
  24940. 0x0041: { n:"BrtSxvcellNum" },
  24941. 0x0042: { n:"BrtSxvcellStr" },
  24942. 0x0043: { n:"BrtSxvcellBool" },
  24943. 0x0044: { n:"BrtSxvcellErr" },
  24944. 0x0045: { n:"BrtSxvcellDate" },
  24945. 0x0046: { n:"BrtSxvcellNil" },
  24946. 0x0080: { n:"BrtFileVersion" },
  24947. 0x0081: { n:"BrtBeginSheet" },
  24948. 0x0082: { n:"BrtEndSheet" },
  24949. 0x0083: { n:"BrtBeginBook", f:parsenoop, p:0 },
  24950. 0x0084: { n:"BrtEndBook" },
  24951. 0x0085: { n:"BrtBeginWsViews" },
  24952. 0x0086: { n:"BrtEndWsViews" },
  24953. 0x0087: { n:"BrtBeginBookViews" },
  24954. 0x0088: { n:"BrtEndBookViews" },
  24955. 0x0089: { n:"BrtBeginWsView", f:parse_BrtBeginWsView },
  24956. 0x008A: { n:"BrtEndWsView" },
  24957. 0x008B: { n:"BrtBeginCsViews" },
  24958. 0x008C: { n:"BrtEndCsViews" },
  24959. 0x008D: { n:"BrtBeginCsView" },
  24960. 0x008E: { n:"BrtEndCsView" },
  24961. 0x008F: { n:"BrtBeginBundleShs" },
  24962. 0x0090: { n:"BrtEndBundleShs" },
  24963. 0x0091: { n:"BrtBeginSheetData" },
  24964. 0x0092: { n:"BrtEndSheetData" },
  24965. 0x0093: { n:"BrtWsProp", f:parse_BrtWsProp },
  24966. 0x0094: { n:"BrtWsDim", f:parse_BrtWsDim, p:16 },
  24967. 0x0097: { n:"BrtPane" },
  24968. 0x0098: { n:"BrtSel" },
  24969. 0x0099: { n:"BrtWbProp", f:parse_BrtWbProp },
  24970. 0x009A: { n:"BrtWbFactoid" },
  24971. 0x009B: { n:"BrtFileRecover" },
  24972. 0x009C: { n:"BrtBundleSh", f:parse_BrtBundleSh },
  24973. 0x009D: { n:"BrtCalcProp" },
  24974. 0x009E: { n:"BrtBookView" },
  24975. 0x009F: { n:"BrtBeginSst", f:parse_BrtBeginSst },
  24976. 0x00A0: { n:"BrtEndSst" },
  24977. 0x00A1: { n:"BrtBeginAFilter", f:parse_UncheckedRfX },
  24978. 0x00A2: { n:"BrtEndAFilter" },
  24979. 0x00A3: { n:"BrtBeginFilterColumn" },
  24980. 0x00A4: { n:"BrtEndFilterColumn" },
  24981. 0x00A5: { n:"BrtBeginFilters" },
  24982. 0x00A6: { n:"BrtEndFilters" },
  24983. 0x00A7: { n:"BrtFilter" },
  24984. 0x00A8: { n:"BrtColorFilter" },
  24985. 0x00A9: { n:"BrtIconFilter" },
  24986. 0x00AA: { n:"BrtTop10Filter" },
  24987. 0x00AB: { n:"BrtDynamicFilter" },
  24988. 0x00AC: { n:"BrtBeginCustomFilters" },
  24989. 0x00AD: { n:"BrtEndCustomFilters" },
  24990. 0x00AE: { n:"BrtCustomFilter" },
  24991. 0x00AF: { n:"BrtAFilterDateGroupItem" },
  24992. 0x00B0: { n:"BrtMergeCell", f:parse_BrtMergeCell },
  24993. 0x00B1: { n:"BrtBeginMergeCells" },
  24994. 0x00B2: { n:"BrtEndMergeCells" },
  24995. 0x00B3: { n:"BrtBeginPivotCacheDef" },
  24996. 0x00B4: { n:"BrtEndPivotCacheDef" },
  24997. 0x00B5: { n:"BrtBeginPCDFields" },
  24998. 0x00B6: { n:"BrtEndPCDFields" },
  24999. 0x00B7: { n:"BrtBeginPCDField" },
  25000. 0x00B8: { n:"BrtEndPCDField" },
  25001. 0x00B9: { n:"BrtBeginPCDSource" },
  25002. 0x00BA: { n:"BrtEndPCDSource" },
  25003. 0x00BB: { n:"BrtBeginPCDSRange" },
  25004. 0x00BC: { n:"BrtEndPCDSRange" },
  25005. 0x00BD: { n:"BrtBeginPCDFAtbl" },
  25006. 0x00BE: { n:"BrtEndPCDFAtbl" },
  25007. 0x00BF: { n:"BrtBeginPCDIRun" },
  25008. 0x00C0: { n:"BrtEndPCDIRun" },
  25009. 0x00C1: { n:"BrtBeginPivotCacheRecords" },
  25010. 0x00C2: { n:"BrtEndPivotCacheRecords" },
  25011. 0x00C3: { n:"BrtBeginPCDHierarchies" },
  25012. 0x00C4: { n:"BrtEndPCDHierarchies" },
  25013. 0x00C5: { n:"BrtBeginPCDHierarchy" },
  25014. 0x00C6: { n:"BrtEndPCDHierarchy" },
  25015. 0x00C7: { n:"BrtBeginPCDHFieldsUsage" },
  25016. 0x00C8: { n:"BrtEndPCDHFieldsUsage" },
  25017. 0x00C9: { n:"BrtBeginExtConnection" },
  25018. 0x00CA: { n:"BrtEndExtConnection" },
  25019. 0x00CB: { n:"BrtBeginECDbProps" },
  25020. 0x00CC: { n:"BrtEndECDbProps" },
  25021. 0x00CD: { n:"BrtBeginECOlapProps" },
  25022. 0x00CE: { n:"BrtEndECOlapProps" },
  25023. 0x00CF: { n:"BrtBeginPCDSConsol" },
  25024. 0x00D0: { n:"BrtEndPCDSConsol" },
  25025. 0x00D1: { n:"BrtBeginPCDSCPages" },
  25026. 0x00D2: { n:"BrtEndPCDSCPages" },
  25027. 0x00D3: { n:"BrtBeginPCDSCPage" },
  25028. 0x00D4: { n:"BrtEndPCDSCPage" },
  25029. 0x00D5: { n:"BrtBeginPCDSCPItem" },
  25030. 0x00D6: { n:"BrtEndPCDSCPItem" },
  25031. 0x00D7: { n:"BrtBeginPCDSCSets" },
  25032. 0x00D8: { n:"BrtEndPCDSCSets" },
  25033. 0x00D9: { n:"BrtBeginPCDSCSet" },
  25034. 0x00DA: { n:"BrtEndPCDSCSet" },
  25035. 0x00DB: { n:"BrtBeginPCDFGroup" },
  25036. 0x00DC: { n:"BrtEndPCDFGroup" },
  25037. 0x00DD: { n:"BrtBeginPCDFGItems" },
  25038. 0x00DE: { n:"BrtEndPCDFGItems" },
  25039. 0x00DF: { n:"BrtBeginPCDFGRange" },
  25040. 0x00E0: { n:"BrtEndPCDFGRange" },
  25041. 0x00E1: { n:"BrtBeginPCDFGDiscrete" },
  25042. 0x00E2: { n:"BrtEndPCDFGDiscrete" },
  25043. 0x00E3: { n:"BrtBeginPCDSDTupleCache" },
  25044. 0x00E4: { n:"BrtEndPCDSDTupleCache" },
  25045. 0x00E5: { n:"BrtBeginPCDSDTCEntries" },
  25046. 0x00E6: { n:"BrtEndPCDSDTCEntries" },
  25047. 0x00E7: { n:"BrtBeginPCDSDTCEMembers" },
  25048. 0x00E8: { n:"BrtEndPCDSDTCEMembers" },
  25049. 0x00E9: { n:"BrtBeginPCDSDTCEMember" },
  25050. 0x00EA: { n:"BrtEndPCDSDTCEMember" },
  25051. 0x00EB: { n:"BrtBeginPCDSDTCQueries" },
  25052. 0x00EC: { n:"BrtEndPCDSDTCQueries" },
  25053. 0x00ED: { n:"BrtBeginPCDSDTCQuery" },
  25054. 0x00EE: { n:"BrtEndPCDSDTCQuery" },
  25055. 0x00EF: { n:"BrtBeginPCDSDTCSets" },
  25056. 0x00F0: { n:"BrtEndPCDSDTCSets" },
  25057. 0x00F1: { n:"BrtBeginPCDSDTCSet" },
  25058. 0x00F2: { n:"BrtEndPCDSDTCSet" },
  25059. 0x00F3: { n:"BrtBeginPCDCalcItems" },
  25060. 0x00F4: { n:"BrtEndPCDCalcItems" },
  25061. 0x00F5: { n:"BrtBeginPCDCalcItem" },
  25062. 0x00F6: { n:"BrtEndPCDCalcItem" },
  25063. 0x00F7: { n:"BrtBeginPRule" },
  25064. 0x00F8: { n:"BrtEndPRule" },
  25065. 0x00F9: { n:"BrtBeginPRFilters" },
  25066. 0x00FA: { n:"BrtEndPRFilters" },
  25067. 0x00FB: { n:"BrtBeginPRFilter" },
  25068. 0x00FC: { n:"BrtEndPRFilter" },
  25069. 0x00FD: { n:"BrtBeginPNames" },
  25070. 0x00FE: { n:"BrtEndPNames" },
  25071. 0x00FF: { n:"BrtBeginPName" },
  25072. 0x0100: { n:"BrtEndPName" },
  25073. 0x0101: { n:"BrtBeginPNPairs" },
  25074. 0x0102: { n:"BrtEndPNPairs" },
  25075. 0x0103: { n:"BrtBeginPNPair" },
  25076. 0x0104: { n:"BrtEndPNPair" },
  25077. 0x0105: { n:"BrtBeginECWebProps" },
  25078. 0x0106: { n:"BrtEndECWebProps" },
  25079. 0x0107: { n:"BrtBeginEcWpTables" },
  25080. 0x0108: { n:"BrtEndECWPTables" },
  25081. 0x0109: { n:"BrtBeginECParams" },
  25082. 0x010A: { n:"BrtEndECParams" },
  25083. 0x010B: { n:"BrtBeginECParam" },
  25084. 0x010C: { n:"BrtEndECParam" },
  25085. 0x010D: { n:"BrtBeginPCDKPIs" },
  25086. 0x010E: { n:"BrtEndPCDKPIs" },
  25087. 0x010F: { n:"BrtBeginPCDKPI" },
  25088. 0x0110: { n:"BrtEndPCDKPI" },
  25089. 0x0111: { n:"BrtBeginDims" },
  25090. 0x0112: { n:"BrtEndDims" },
  25091. 0x0113: { n:"BrtBeginDim" },
  25092. 0x0114: { n:"BrtEndDim" },
  25093. 0x0115: { n:"BrtIndexPartEnd" },
  25094. 0x0116: { n:"BrtBeginStyleSheet" },
  25095. 0x0117: { n:"BrtEndStyleSheet" },
  25096. 0x0118: { n:"BrtBeginSXView" },
  25097. 0x0119: { n:"BrtEndSXVI" },
  25098. 0x011A: { n:"BrtBeginSXVI" },
  25099. 0x011B: { n:"BrtBeginSXVIs" },
  25100. 0x011C: { n:"BrtEndSXVIs" },
  25101. 0x011D: { n:"BrtBeginSXVD" },
  25102. 0x011E: { n:"BrtEndSXVD" },
  25103. 0x011F: { n:"BrtBeginSXVDs" },
  25104. 0x0120: { n:"BrtEndSXVDs" },
  25105. 0x0121: { n:"BrtBeginSXPI" },
  25106. 0x0122: { n:"BrtEndSXPI" },
  25107. 0x0123: { n:"BrtBeginSXPIs" },
  25108. 0x0124: { n:"BrtEndSXPIs" },
  25109. 0x0125: { n:"BrtBeginSXDI" },
  25110. 0x0126: { n:"BrtEndSXDI" },
  25111. 0x0127: { n:"BrtBeginSXDIs" },
  25112. 0x0128: { n:"BrtEndSXDIs" },
  25113. 0x0129: { n:"BrtBeginSXLI" },
  25114. 0x012A: { n:"BrtEndSXLI" },
  25115. 0x012B: { n:"BrtBeginSXLIRws" },
  25116. 0x012C: { n:"BrtEndSXLIRws" },
  25117. 0x012D: { n:"BrtBeginSXLICols" },
  25118. 0x012E: { n:"BrtEndSXLICols" },
  25119. 0x012F: { n:"BrtBeginSXFormat" },
  25120. 0x0130: { n:"BrtEndSXFormat" },
  25121. 0x0131: { n:"BrtBeginSXFormats" },
  25122. 0x0132: { n:"BrtEndSxFormats" },
  25123. 0x0133: { n:"BrtBeginSxSelect" },
  25124. 0x0134: { n:"BrtEndSxSelect" },
  25125. 0x0135: { n:"BrtBeginISXVDRws" },
  25126. 0x0136: { n:"BrtEndISXVDRws" },
  25127. 0x0137: { n:"BrtBeginISXVDCols" },
  25128. 0x0138: { n:"BrtEndISXVDCols" },
  25129. 0x0139: { n:"BrtEndSXLocation" },
  25130. 0x013A: { n:"BrtBeginSXLocation" },
  25131. 0x013B: { n:"BrtEndSXView" },
  25132. 0x013C: { n:"BrtBeginSXTHs" },
  25133. 0x013D: { n:"BrtEndSXTHs" },
  25134. 0x013E: { n:"BrtBeginSXTH" },
  25135. 0x013F: { n:"BrtEndSXTH" },
  25136. 0x0140: { n:"BrtBeginISXTHRws" },
  25137. 0x0141: { n:"BrtEndISXTHRws" },
  25138. 0x0142: { n:"BrtBeginISXTHCols" },
  25139. 0x0143: { n:"BrtEndISXTHCols" },
  25140. 0x0144: { n:"BrtBeginSXTDMPS" },
  25141. 0x0145: { n:"BrtEndSXTDMPs" },
  25142. 0x0146: { n:"BrtBeginSXTDMP" },
  25143. 0x0147: { n:"BrtEndSXTDMP" },
  25144. 0x0148: { n:"BrtBeginSXTHItems" },
  25145. 0x0149: { n:"BrtEndSXTHItems" },
  25146. 0x014A: { n:"BrtBeginSXTHItem" },
  25147. 0x014B: { n:"BrtEndSXTHItem" },
  25148. 0x014C: { n:"BrtBeginMetadata" },
  25149. 0x014D: { n:"BrtEndMetadata" },
  25150. 0x014E: { n:"BrtBeginEsmdtinfo" },
  25151. 0x014F: { n:"BrtMdtinfo" },
  25152. 0x0150: { n:"BrtEndEsmdtinfo" },
  25153. 0x0151: { n:"BrtBeginEsmdb" },
  25154. 0x0152: { n:"BrtEndEsmdb" },
  25155. 0x0153: { n:"BrtBeginEsfmd" },
  25156. 0x0154: { n:"BrtEndEsfmd" },
  25157. 0x0155: { n:"BrtBeginSingleCells" },
  25158. 0x0156: { n:"BrtEndSingleCells" },
  25159. 0x0157: { n:"BrtBeginList" },
  25160. 0x0158: { n:"BrtEndList" },
  25161. 0x0159: { n:"BrtBeginListCols" },
  25162. 0x015A: { n:"BrtEndListCols" },
  25163. 0x015B: { n:"BrtBeginListCol" },
  25164. 0x015C: { n:"BrtEndListCol" },
  25165. 0x015D: { n:"BrtBeginListXmlCPr" },
  25166. 0x015E: { n:"BrtEndListXmlCPr" },
  25167. 0x015F: { n:"BrtListCCFmla" },
  25168. 0x0160: { n:"BrtListTrFmla" },
  25169. 0x0161: { n:"BrtBeginExternals" },
  25170. 0x0162: { n:"BrtEndExternals" },
  25171. 0x0163: { n:"BrtSupBookSrc", f:parse_RelID},
  25172. 0x0165: { n:"BrtSupSelf" },
  25173. 0x0166: { n:"BrtSupSame" },
  25174. 0x0167: { n:"BrtSupTabs" },
  25175. 0x0168: { n:"BrtBeginSupBook" },
  25176. 0x0169: { n:"BrtPlaceholderName" },
  25177. 0x016A: { n:"BrtExternSheet", f:parse_ExternSheet },
  25178. 0x016B: { n:"BrtExternTableStart" },
  25179. 0x016C: { n:"BrtExternTableEnd" },
  25180. 0x016E: { n:"BrtExternRowHdr" },
  25181. 0x016F: { n:"BrtExternCellBlank" },
  25182. 0x0170: { n:"BrtExternCellReal" },
  25183. 0x0171: { n:"BrtExternCellBool" },
  25184. 0x0172: { n:"BrtExternCellError" },
  25185. 0x0173: { n:"BrtExternCellString" },
  25186. 0x0174: { n:"BrtBeginEsmdx" },
  25187. 0x0175: { n:"BrtEndEsmdx" },
  25188. 0x0176: { n:"BrtBeginMdxSet" },
  25189. 0x0177: { n:"BrtEndMdxSet" },
  25190. 0x0178: { n:"BrtBeginMdxMbrProp" },
  25191. 0x0179: { n:"BrtEndMdxMbrProp" },
  25192. 0x017A: { n:"BrtBeginMdxKPI" },
  25193. 0x017B: { n:"BrtEndMdxKPI" },
  25194. 0x017C: { n:"BrtBeginEsstr" },
  25195. 0x017D: { n:"BrtEndEsstr" },
  25196. 0x017E: { n:"BrtBeginPRFItem" },
  25197. 0x017F: { n:"BrtEndPRFItem" },
  25198. 0x0180: { n:"BrtBeginPivotCacheIDs" },
  25199. 0x0181: { n:"BrtEndPivotCacheIDs" },
  25200. 0x0182: { n:"BrtBeginPivotCacheID" },
  25201. 0x0183: { n:"BrtEndPivotCacheID" },
  25202. 0x0184: { n:"BrtBeginISXVIs" },
  25203. 0x0185: { n:"BrtEndISXVIs" },
  25204. 0x0186: { n:"BrtBeginColInfos" },
  25205. 0x0187: { n:"BrtEndColInfos" },
  25206. 0x0188: { n:"BrtBeginRwBrk" },
  25207. 0x0189: { n:"BrtEndRwBrk" },
  25208. 0x018A: { n:"BrtBeginColBrk" },
  25209. 0x018B: { n:"BrtEndColBrk" },
  25210. 0x018C: { n:"BrtBrk" },
  25211. 0x018D: { n:"BrtUserBookView" },
  25212. 0x018E: { n:"BrtInfo" },
  25213. 0x018F: { n:"BrtCUsr" },
  25214. 0x0190: { n:"BrtUsr" },
  25215. 0x0191: { n:"BrtBeginUsers" },
  25216. 0x0193: { n:"BrtEOF" },
  25217. 0x0194: { n:"BrtUCR" },
  25218. 0x0195: { n:"BrtRRInsDel" },
  25219. 0x0196: { n:"BrtRREndInsDel" },
  25220. 0x0197: { n:"BrtRRMove" },
  25221. 0x0198: { n:"BrtRREndMove" },
  25222. 0x0199: { n:"BrtRRChgCell" },
  25223. 0x019A: { n:"BrtRREndChgCell" },
  25224. 0x019B: { n:"BrtRRHeader" },
  25225. 0x019C: { n:"BrtRRUserView" },
  25226. 0x019D: { n:"BrtRRRenSheet" },
  25227. 0x019E: { n:"BrtRRInsertSh" },
  25228. 0x019F: { n:"BrtRRDefName" },
  25229. 0x01A0: { n:"BrtRRNote" },
  25230. 0x01A1: { n:"BrtRRConflict" },
  25231. 0x01A2: { n:"BrtRRTQSIF" },
  25232. 0x01A3: { n:"BrtRRFormat" },
  25233. 0x01A4: { n:"BrtRREndFormat" },
  25234. 0x01A5: { n:"BrtRRAutoFmt" },
  25235. 0x01A6: { n:"BrtBeginUserShViews" },
  25236. 0x01A7: { n:"BrtBeginUserShView" },
  25237. 0x01A8: { n:"BrtEndUserShView" },
  25238. 0x01A9: { n:"BrtEndUserShViews" },
  25239. 0x01AA: { n:"BrtArrFmla", f:parse_BrtArrFmla },
  25240. 0x01AB: { n:"BrtShrFmla", f:parse_BrtShrFmla },
  25241. 0x01AC: { n:"BrtTable" },
  25242. 0x01AD: { n:"BrtBeginExtConnections" },
  25243. 0x01AE: { n:"BrtEndExtConnections" },
  25244. 0x01AF: { n:"BrtBeginPCDCalcMems" },
  25245. 0x01B0: { n:"BrtEndPCDCalcMems" },
  25246. 0x01B1: { n:"BrtBeginPCDCalcMem" },
  25247. 0x01B2: { n:"BrtEndPCDCalcMem" },
  25248. 0x01B3: { n:"BrtBeginPCDHGLevels" },
  25249. 0x01B4: { n:"BrtEndPCDHGLevels" },
  25250. 0x01B5: { n:"BrtBeginPCDHGLevel" },
  25251. 0x01B6: { n:"BrtEndPCDHGLevel" },
  25252. 0x01B7: { n:"BrtBeginPCDHGLGroups" },
  25253. 0x01B8: { n:"BrtEndPCDHGLGroups" },
  25254. 0x01B9: { n:"BrtBeginPCDHGLGroup" },
  25255. 0x01BA: { n:"BrtEndPCDHGLGroup" },
  25256. 0x01BB: { n:"BrtBeginPCDHGLGMembers" },
  25257. 0x01BC: { n:"BrtEndPCDHGLGMembers" },
  25258. 0x01BD: { n:"BrtBeginPCDHGLGMember" },
  25259. 0x01BE: { n:"BrtEndPCDHGLGMember" },
  25260. 0x01BF: { n:"BrtBeginQSI" },
  25261. 0x01C0: { n:"BrtEndQSI" },
  25262. 0x01C1: { n:"BrtBeginQSIR" },
  25263. 0x01C2: { n:"BrtEndQSIR" },
  25264. 0x01C3: { n:"BrtBeginDeletedNames" },
  25265. 0x01C4: { n:"BrtEndDeletedNames" },
  25266. 0x01C5: { n:"BrtBeginDeletedName" },
  25267. 0x01C6: { n:"BrtEndDeletedName" },
  25268. 0x01C7: { n:"BrtBeginQSIFs" },
  25269. 0x01C8: { n:"BrtEndQSIFs" },
  25270. 0x01C9: { n:"BrtBeginQSIF" },
  25271. 0x01CA: { n:"BrtEndQSIF" },
  25272. 0x01CB: { n:"BrtBeginAutoSortScope" },
  25273. 0x01CC: { n:"BrtEndAutoSortScope" },
  25274. 0x01CD: { n:"BrtBeginConditionalFormatting" },
  25275. 0x01CE: { n:"BrtEndConditionalFormatting" },
  25276. 0x01CF: { n:"BrtBeginCFRule" },
  25277. 0x01D0: { n:"BrtEndCFRule" },
  25278. 0x01D1: { n:"BrtBeginIconSet" },
  25279. 0x01D2: { n:"BrtEndIconSet" },
  25280. 0x01D3: { n:"BrtBeginDatabar" },
  25281. 0x01D4: { n:"BrtEndDatabar" },
  25282. 0x01D5: { n:"BrtBeginColorScale" },
  25283. 0x01D6: { n:"BrtEndColorScale" },
  25284. 0x01D7: { n:"BrtCFVO" },
  25285. 0x01D8: { n:"BrtExternValueMeta" },
  25286. 0x01D9: { n:"BrtBeginColorPalette" },
  25287. 0x01DA: { n:"BrtEndColorPalette" },
  25288. 0x01DB: { n:"BrtIndexedColor" },
  25289. 0x01DC: { n:"BrtMargins", f:parse_BrtMargins },
  25290. 0x01DD: { n:"BrtPrintOptions" },
  25291. 0x01DE: { n:"BrtPageSetup" },
  25292. 0x01DF: { n:"BrtBeginHeaderFooter" },
  25293. 0x01E0: { n:"BrtEndHeaderFooter" },
  25294. 0x01E1: { n:"BrtBeginSXCrtFormat" },
  25295. 0x01E2: { n:"BrtEndSXCrtFormat" },
  25296. 0x01E3: { n:"BrtBeginSXCrtFormats" },
  25297. 0x01E4: { n:"BrtEndSXCrtFormats" },
  25298. 0x01E5: { n:"BrtWsFmtInfo", f:parse_BrtWsFmtInfo },
  25299. 0x01E6: { n:"BrtBeginMgs" },
  25300. 0x01E7: { n:"BrtEndMGs" },
  25301. 0x01E8: { n:"BrtBeginMGMaps" },
  25302. 0x01E9: { n:"BrtEndMGMaps" },
  25303. 0x01EA: { n:"BrtBeginMG" },
  25304. 0x01EB: { n:"BrtEndMG" },
  25305. 0x01EC: { n:"BrtBeginMap" },
  25306. 0x01ED: { n:"BrtEndMap" },
  25307. 0x01EE: { n:"BrtHLink", f:parse_BrtHLink },
  25308. 0x01EF: { n:"BrtBeginDCon" },
  25309. 0x01F0: { n:"BrtEndDCon" },
  25310. 0x01F1: { n:"BrtBeginDRefs" },
  25311. 0x01F2: { n:"BrtEndDRefs" },
  25312. 0x01F3: { n:"BrtDRef" },
  25313. 0x01F4: { n:"BrtBeginScenMan" },
  25314. 0x01F5: { n:"BrtEndScenMan" },
  25315. 0x01F6: { n:"BrtBeginSct" },
  25316. 0x01F7: { n:"BrtEndSct" },
  25317. 0x01F8: { n:"BrtSlc" },
  25318. 0x01F9: { n:"BrtBeginDXFs" },
  25319. 0x01FA: { n:"BrtEndDXFs" },
  25320. 0x01FB: { n:"BrtDXF" },
  25321. 0x01FC: { n:"BrtBeginTableStyles" },
  25322. 0x01FD: { n:"BrtEndTableStyles" },
  25323. 0x01FE: { n:"BrtBeginTableStyle" },
  25324. 0x01FF: { n:"BrtEndTableStyle" },
  25325. 0x0200: { n:"BrtTableStyleElement" },
  25326. 0x0201: { n:"BrtTableStyleClient" },
  25327. 0x0202: { n:"BrtBeginVolDeps" },
  25328. 0x0203: { n:"BrtEndVolDeps" },
  25329. 0x0204: { n:"BrtBeginVolType" },
  25330. 0x0205: { n:"BrtEndVolType" },
  25331. 0x0206: { n:"BrtBeginVolMain" },
  25332. 0x0207: { n:"BrtEndVolMain" },
  25333. 0x0208: { n:"BrtBeginVolTopic" },
  25334. 0x0209: { n:"BrtEndVolTopic" },
  25335. 0x020A: { n:"BrtVolSubtopic" },
  25336. 0x020B: { n:"BrtVolRef" },
  25337. 0x020C: { n:"BrtVolNum" },
  25338. 0x020D: { n:"BrtVolErr" },
  25339. 0x020E: { n:"BrtVolStr" },
  25340. 0x020F: { n:"BrtVolBool" },
  25341. 0x0210: { n:"BrtBeginCalcChain$" },
  25342. 0x0211: { n:"BrtEndCalcChain$" },
  25343. 0x0212: { n:"BrtBeginSortState" },
  25344. 0x0213: { n:"BrtEndSortState" },
  25345. 0x0214: { n:"BrtBeginSortCond" },
  25346. 0x0215: { n:"BrtEndSortCond" },
  25347. 0x0216: { n:"BrtBookProtection" },
  25348. 0x0217: { n:"BrtSheetProtection" },
  25349. 0x0218: { n:"BrtRangeProtection" },
  25350. 0x0219: { n:"BrtPhoneticInfo" },
  25351. 0x021A: { n:"BrtBeginECTxtWiz" },
  25352. 0x021B: { n:"BrtEndECTxtWiz" },
  25353. 0x021C: { n:"BrtBeginECTWFldInfoLst" },
  25354. 0x021D: { n:"BrtEndECTWFldInfoLst" },
  25355. 0x021E: { n:"BrtBeginECTwFldInfo" },
  25356. 0x0224: { n:"BrtFileSharing" },
  25357. 0x0225: { n:"BrtOleSize" },
  25358. 0x0226: { n:"BrtDrawing", f:parse_RelID },
  25359. 0x0227: { n:"BrtLegacyDrawing" },
  25360. 0x0228: { n:"BrtLegacyDrawingHF" },
  25361. 0x0229: { n:"BrtWebOpt" },
  25362. 0x022A: { n:"BrtBeginWebPubItems" },
  25363. 0x022B: { n:"BrtEndWebPubItems" },
  25364. 0x022C: { n:"BrtBeginWebPubItem" },
  25365. 0x022D: { n:"BrtEndWebPubItem" },
  25366. 0x022E: { n:"BrtBeginSXCondFmt" },
  25367. 0x022F: { n:"BrtEndSXCondFmt" },
  25368. 0x0230: { n:"BrtBeginSXCondFmts" },
  25369. 0x0231: { n:"BrtEndSXCondFmts" },
  25370. 0x0232: { n:"BrtBkHim" },
  25371. 0x0234: { n:"BrtColor" },
  25372. 0x0235: { n:"BrtBeginIndexedColors" },
  25373. 0x0236: { n:"BrtEndIndexedColors" },
  25374. 0x0239: { n:"BrtBeginMRUColors" },
  25375. 0x023A: { n:"BrtEndMRUColors" },
  25376. 0x023C: { n:"BrtMRUColor" },
  25377. 0x023D: { n:"BrtBeginDVals" },
  25378. 0x023E: { n:"BrtEndDVals" },
  25379. 0x0241: { n:"BrtSupNameStart" },
  25380. 0x0242: { n:"BrtSupNameValueStart" },
  25381. 0x0243: { n:"BrtSupNameValueEnd" },
  25382. 0x0244: { n:"BrtSupNameNum" },
  25383. 0x0245: { n:"BrtSupNameErr" },
  25384. 0x0246: { n:"BrtSupNameSt" },
  25385. 0x0247: { n:"BrtSupNameNil" },
  25386. 0x0248: { n:"BrtSupNameBool" },
  25387. 0x0249: { n:"BrtSupNameFmla" },
  25388. 0x024A: { n:"BrtSupNameBits" },
  25389. 0x024B: { n:"BrtSupNameEnd" },
  25390. 0x024C: { n:"BrtEndSupBook" },
  25391. 0x024D: { n:"BrtCellSmartTagProperty" },
  25392. 0x024E: { n:"BrtBeginCellSmartTag" },
  25393. 0x024F: { n:"BrtEndCellSmartTag" },
  25394. 0x0250: { n:"BrtBeginCellSmartTags" },
  25395. 0x0251: { n:"BrtEndCellSmartTags" },
  25396. 0x0252: { n:"BrtBeginSmartTags" },
  25397. 0x0253: { n:"BrtEndSmartTags" },
  25398. 0x0254: { n:"BrtSmartTagType" },
  25399. 0x0255: { n:"BrtBeginSmartTagTypes" },
  25400. 0x0256: { n:"BrtEndSmartTagTypes" },
  25401. 0x0257: { n:"BrtBeginSXFilters" },
  25402. 0x0258: { n:"BrtEndSXFilters" },
  25403. 0x0259: { n:"BrtBeginSXFILTER" },
  25404. 0x025A: { n:"BrtEndSXFilter" },
  25405. 0x025B: { n:"BrtBeginFills" },
  25406. 0x025C: { n:"BrtEndFills" },
  25407. 0x025D: { n:"BrtBeginCellWatches" },
  25408. 0x025E: { n:"BrtEndCellWatches" },
  25409. 0x025F: { n:"BrtCellWatch" },
  25410. 0x0260: { n:"BrtBeginCRErrs" },
  25411. 0x0261: { n:"BrtEndCRErrs" },
  25412. 0x0262: { n:"BrtCrashRecErr" },
  25413. 0x0263: { n:"BrtBeginFonts" },
  25414. 0x0264: { n:"BrtEndFonts" },
  25415. 0x0265: { n:"BrtBeginBorders" },
  25416. 0x0266: { n:"BrtEndBorders" },
  25417. 0x0267: { n:"BrtBeginFmts" },
  25418. 0x0268: { n:"BrtEndFmts" },
  25419. 0x0269: { n:"BrtBeginCellXFs" },
  25420. 0x026A: { n:"BrtEndCellXFs" },
  25421. 0x026B: { n:"BrtBeginStyles" },
  25422. 0x026C: { n:"BrtEndStyles" },
  25423. 0x0271: { n:"BrtBigName" },
  25424. 0x0272: { n:"BrtBeginCellStyleXFs" },
  25425. 0x0273: { n:"BrtEndCellStyleXFs" },
  25426. 0x0274: { n:"BrtBeginComments" },
  25427. 0x0275: { n:"BrtEndComments" },
  25428. 0x0276: { n:"BrtBeginCommentAuthors" },
  25429. 0x0277: { n:"BrtEndCommentAuthors" },
  25430. 0x0278: { n:"BrtCommentAuthor", f:parse_BrtCommentAuthor },
  25431. 0x0279: { n:"BrtBeginCommentList" },
  25432. 0x027A: { n:"BrtEndCommentList" },
  25433. 0x027B: { n:"BrtBeginComment", f:parse_BrtBeginComment},
  25434. 0x027C: { n:"BrtEndComment" },
  25435. 0x027D: { n:"BrtCommentText", f:parse_BrtCommentText },
  25436. 0x027E: { n:"BrtBeginOleObjects" },
  25437. 0x027F: { n:"BrtOleObject" },
  25438. 0x0280: { n:"BrtEndOleObjects" },
  25439. 0x0281: { n:"BrtBeginSxrules" },
  25440. 0x0282: { n:"BrtEndSxRules" },
  25441. 0x0283: { n:"BrtBeginActiveXControls" },
  25442. 0x0284: { n:"BrtActiveX" },
  25443. 0x0285: { n:"BrtEndActiveXControls" },
  25444. 0x0286: { n:"BrtBeginPCDSDTCEMembersSortBy" },
  25445. 0x0288: { n:"BrtBeginCellIgnoreECs" },
  25446. 0x0289: { n:"BrtCellIgnoreEC" },
  25447. 0x028A: { n:"BrtEndCellIgnoreECs" },
  25448. 0x028B: { n:"BrtCsProp", f:parse_BrtCsProp },
  25449. 0x028C: { n:"BrtCsPageSetup" },
  25450. 0x028D: { n:"BrtBeginUserCsViews" },
  25451. 0x028E: { n:"BrtEndUserCsViews" },
  25452. 0x028F: { n:"BrtBeginUserCsView" },
  25453. 0x0290: { n:"BrtEndUserCsView" },
  25454. 0x0291: { n:"BrtBeginPcdSFCIEntries" },
  25455. 0x0292: { n:"BrtEndPCDSFCIEntries" },
  25456. 0x0293: { n:"BrtPCDSFCIEntry" },
  25457. 0x0294: { n:"BrtBeginListParts" },
  25458. 0x0295: { n:"BrtListPart" },
  25459. 0x0296: { n:"BrtEndListParts" },
  25460. 0x0297: { n:"BrtSheetCalcProp" },
  25461. 0x0298: { n:"BrtBeginFnGroup" },
  25462. 0x0299: { n:"BrtFnGroup" },
  25463. 0x029A: { n:"BrtEndFnGroup" },
  25464. 0x029B: { n:"BrtSupAddin" },
  25465. 0x029C: { n:"BrtSXTDMPOrder" },
  25466. 0x029D: { n:"BrtCsProtection" },
  25467. 0x029F: { n:"BrtBeginWsSortMap" },
  25468. 0x02A0: { n:"BrtEndWsSortMap" },
  25469. 0x02A1: { n:"BrtBeginRRSort" },
  25470. 0x02A2: { n:"BrtEndRRSort" },
  25471. 0x02A3: { n:"BrtRRSortItem" },
  25472. 0x02A4: { n:"BrtFileSharingIso" },
  25473. 0x02A5: { n:"BrtBookProtectionIso" },
  25474. 0x02A6: { n:"BrtSheetProtectionIso" },
  25475. 0x02A7: { n:"BrtCsProtectionIso" },
  25476. 0x02A8: { n:"BrtRangeProtectionIso" },
  25477. 0x0400: { n:"BrtRwDescent" },
  25478. 0x0401: { n:"BrtKnownFonts" },
  25479. 0x0402: { n:"BrtBeginSXTupleSet" },
  25480. 0x0403: { n:"BrtEndSXTupleSet" },
  25481. 0x0404: { n:"BrtBeginSXTupleSetHeader" },
  25482. 0x0405: { n:"BrtEndSXTupleSetHeader" },
  25483. 0x0406: { n:"BrtSXTupleSetHeaderItem" },
  25484. 0x0407: { n:"BrtBeginSXTupleSetData" },
  25485. 0x0408: { n:"BrtEndSXTupleSetData" },
  25486. 0x0409: { n:"BrtBeginSXTupleSetRow" },
  25487. 0x040A: { n:"BrtEndSXTupleSetRow" },
  25488. 0x040B: { n:"BrtSXTupleSetRowItem" },
  25489. 0x040C: { n:"BrtNameExt" },
  25490. 0x040D: { n:"BrtPCDH14" },
  25491. 0x040E: { n:"BrtBeginPCDCalcMem14" },
  25492. 0x040F: { n:"BrtEndPCDCalcMem14" },
  25493. 0x0410: { n:"BrtSXTH14" },
  25494. 0x0411: { n:"BrtBeginSparklineGroup" },
  25495. 0x0412: { n:"BrtEndSparklineGroup" },
  25496. 0x0413: { n:"BrtSparkline" },
  25497. 0x0414: { n:"BrtSXDI14" },
  25498. 0x0415: { n:"BrtWsFmtInfoEx14" },
  25499. 0x0416: { n:"BrtBeginConditionalFormatting14" },
  25500. 0x0417: { n:"BrtEndConditionalFormatting14" },
  25501. 0x0418: { n:"BrtBeginCFRule14" },
  25502. 0x0419: { n:"BrtEndCFRule14" },
  25503. 0x041A: { n:"BrtCFVO14" },
  25504. 0x041B: { n:"BrtBeginDatabar14" },
  25505. 0x041C: { n:"BrtBeginIconSet14" },
  25506. 0x041D: { n:"BrtDVal14" },
  25507. 0x041E: { n:"BrtBeginDVals14" },
  25508. 0x041F: { n:"BrtColor14" },
  25509. 0x0420: { n:"BrtBeginSparklines" },
  25510. 0x0421: { n:"BrtEndSparklines" },
  25511. 0x0422: { n:"BrtBeginSparklineGroups" },
  25512. 0x0423: { n:"BrtEndSparklineGroups" },
  25513. 0x0425: { n:"BrtSXVD14" },
  25514. 0x0426: { n:"BrtBeginSXView14" },
  25515. 0x0427: { n:"BrtEndSXView14" },
  25516. 0x0428: { n:"BrtBeginSXView16" },
  25517. 0x0429: { n:"BrtEndSXView16" },
  25518. 0x042A: { n:"BrtBeginPCD14" },
  25519. 0x042B: { n:"BrtEndPCD14" },
  25520. 0x042C: { n:"BrtBeginExtConn14" },
  25521. 0x042D: { n:"BrtEndExtConn14" },
  25522. 0x042E: { n:"BrtBeginSlicerCacheIDs" },
  25523. 0x042F: { n:"BrtEndSlicerCacheIDs" },
  25524. 0x0430: { n:"BrtBeginSlicerCacheID" },
  25525. 0x0431: { n:"BrtEndSlicerCacheID" },
  25526. 0x0433: { n:"BrtBeginSlicerCache" },
  25527. 0x0434: { n:"BrtEndSlicerCache" },
  25528. 0x0435: { n:"BrtBeginSlicerCacheDef" },
  25529. 0x0436: { n:"BrtEndSlicerCacheDef" },
  25530. 0x0437: { n:"BrtBeginSlicersEx" },
  25531. 0x0438: { n:"BrtEndSlicersEx" },
  25532. 0x0439: { n:"BrtBeginSlicerEx" },
  25533. 0x043A: { n:"BrtEndSlicerEx" },
  25534. 0x043B: { n:"BrtBeginSlicer" },
  25535. 0x043C: { n:"BrtEndSlicer" },
  25536. 0x043D: { n:"BrtSlicerCachePivotTables" },
  25537. 0x043E: { n:"BrtBeginSlicerCacheOlapImpl" },
  25538. 0x043F: { n:"BrtEndSlicerCacheOlapImpl" },
  25539. 0x0440: { n:"BrtBeginSlicerCacheLevelsData" },
  25540. 0x0441: { n:"BrtEndSlicerCacheLevelsData" },
  25541. 0x0442: { n:"BrtBeginSlicerCacheLevelData" },
  25542. 0x0443: { n:"BrtEndSlicerCacheLevelData" },
  25543. 0x0444: { n:"BrtBeginSlicerCacheSiRanges" },
  25544. 0x0445: { n:"BrtEndSlicerCacheSiRanges" },
  25545. 0x0446: { n:"BrtBeginSlicerCacheSiRange" },
  25546. 0x0447: { n:"BrtEndSlicerCacheSiRange" },
  25547. 0x0448: { n:"BrtSlicerCacheOlapItem" },
  25548. 0x0449: { n:"BrtBeginSlicerCacheSelections" },
  25549. 0x044A: { n:"BrtSlicerCacheSelection" },
  25550. 0x044B: { n:"BrtEndSlicerCacheSelections" },
  25551. 0x044C: { n:"BrtBeginSlicerCacheNative" },
  25552. 0x044D: { n:"BrtEndSlicerCacheNative" },
  25553. 0x044E: { n:"BrtSlicerCacheNativeItem" },
  25554. 0x044F: { n:"BrtRangeProtection14" },
  25555. 0x0450: { n:"BrtRangeProtectionIso14" },
  25556. 0x0451: { n:"BrtCellIgnoreEC14" },
  25557. 0x0457: { n:"BrtList14" },
  25558. 0x0458: { n:"BrtCFIcon" },
  25559. 0x0459: { n:"BrtBeginSlicerCachesPivotCacheIDs" },
  25560. 0x045A: { n:"BrtEndSlicerCachesPivotCacheIDs" },
  25561. 0x045B: { n:"BrtBeginSlicers" },
  25562. 0x045C: { n:"BrtEndSlicers" },
  25563. 0x045D: { n:"BrtWbProp14" },
  25564. 0x045E: { n:"BrtBeginSXEdit" },
  25565. 0x045F: { n:"BrtEndSXEdit" },
  25566. 0x0460: { n:"BrtBeginSXEdits" },
  25567. 0x0461: { n:"BrtEndSXEdits" },
  25568. 0x0462: { n:"BrtBeginSXChange" },
  25569. 0x0463: { n:"BrtEndSXChange" },
  25570. 0x0464: { n:"BrtBeginSXChanges" },
  25571. 0x0465: { n:"BrtEndSXChanges" },
  25572. 0x0466: { n:"BrtSXTupleItems" },
  25573. 0x0468: { n:"BrtBeginSlicerStyle" },
  25574. 0x0469: { n:"BrtEndSlicerStyle" },
  25575. 0x046A: { n:"BrtSlicerStyleElement" },
  25576. 0x046B: { n:"BrtBeginStyleSheetExt14" },
  25577. 0x046C: { n:"BrtEndStyleSheetExt14" },
  25578. 0x046D: { n:"BrtBeginSlicerCachesPivotCacheID" },
  25579. 0x046E: { n:"BrtEndSlicerCachesPivotCacheID" },
  25580. 0x046F: { n:"BrtBeginConditionalFormattings" },
  25581. 0x0470: { n:"BrtEndConditionalFormattings" },
  25582. 0x0471: { n:"BrtBeginPCDCalcMemExt" },
  25583. 0x0472: { n:"BrtEndPCDCalcMemExt" },
  25584. 0x0473: { n:"BrtBeginPCDCalcMemsExt" },
  25585. 0x0474: { n:"BrtEndPCDCalcMemsExt" },
  25586. 0x0475: { n:"BrtPCDField14" },
  25587. 0x0476: { n:"BrtBeginSlicerStyles" },
  25588. 0x0477: { n:"BrtEndSlicerStyles" },
  25589. 0x0478: { n:"BrtBeginSlicerStyleElements" },
  25590. 0x0479: { n:"BrtEndSlicerStyleElements" },
  25591. 0x047A: { n:"BrtCFRuleExt" },
  25592. 0x047B: { n:"BrtBeginSXCondFmt14" },
  25593. 0x047C: { n:"BrtEndSXCondFmt14" },
  25594. 0x047D: { n:"BrtBeginSXCondFmts14" },
  25595. 0x047E: { n:"BrtEndSXCondFmts14" },
  25596. 0x0480: { n:"BrtBeginSortCond14" },
  25597. 0x0481: { n:"BrtEndSortCond14" },
  25598. 0x0482: { n:"BrtEndDVals14" },
  25599. 0x0483: { n:"BrtEndIconSet14" },
  25600. 0x0484: { n:"BrtEndDatabar14" },
  25601. 0x0485: { n:"BrtBeginColorScale14" },
  25602. 0x0486: { n:"BrtEndColorScale14" },
  25603. 0x0487: { n:"BrtBeginSxrules14" },
  25604. 0x0488: { n:"BrtEndSxrules14" },
  25605. 0x0489: { n:"BrtBeginPRule14" },
  25606. 0x048A: { n:"BrtEndPRule14" },
  25607. 0x048B: { n:"BrtBeginPRFilters14" },
  25608. 0x048C: { n:"BrtEndPRFilters14" },
  25609. 0x048D: { n:"BrtBeginPRFilter14" },
  25610. 0x048E: { n:"BrtEndPRFilter14" },
  25611. 0x048F: { n:"BrtBeginPRFItem14" },
  25612. 0x0490: { n:"BrtEndPRFItem14" },
  25613. 0x0491: { n:"BrtBeginCellIgnoreECs14" },
  25614. 0x0492: { n:"BrtEndCellIgnoreECs14" },
  25615. 0x0493: { n:"BrtDxf14" },
  25616. 0x0494: { n:"BrtBeginDxF14s" },
  25617. 0x0495: { n:"BrtEndDxf14s" },
  25618. 0x0499: { n:"BrtFilter14" },
  25619. 0x049A: { n:"BrtBeginCustomFilters14" },
  25620. 0x049C: { n:"BrtCustomFilter14" },
  25621. 0x049D: { n:"BrtIconFilter14" },
  25622. 0x049E: { n:"BrtPivotCacheConnectionName" },
  25623. 0x0800: { n:"BrtBeginDecoupledPivotCacheIDs" },
  25624. 0x0801: { n:"BrtEndDecoupledPivotCacheIDs" },
  25625. 0x0802: { n:"BrtDecoupledPivotCacheID" },
  25626. 0x0803: { n:"BrtBeginPivotTableRefs" },
  25627. 0x0804: { n:"BrtEndPivotTableRefs" },
  25628. 0x0805: { n:"BrtPivotTableRef" },
  25629. 0x0806: { n:"BrtSlicerCacheBookPivotTables" },
  25630. 0x0807: { n:"BrtBeginSxvcells" },
  25631. 0x0808: { n:"BrtEndSxvcells" },
  25632. 0x0809: { n:"BrtBeginSxRow" },
  25633. 0x080A: { n:"BrtEndSxRow" },
  25634. 0x080C: { n:"BrtPcdCalcMem15" },
  25635. 0x0813: { n:"BrtQsi15" },
  25636. 0x0814: { n:"BrtBeginWebExtensions" },
  25637. 0x0815: { n:"BrtEndWebExtensions" },
  25638. 0x0816: { n:"BrtWebExtension" },
  25639. 0x0817: { n:"BrtAbsPath15" },
  25640. 0x0818: { n:"BrtBeginPivotTableUISettings" },
  25641. 0x0819: { n:"BrtEndPivotTableUISettings" },
  25642. 0x081B: { n:"BrtTableSlicerCacheIDs" },
  25643. 0x081C: { n:"BrtTableSlicerCacheID" },
  25644. 0x081D: { n:"BrtBeginTableSlicerCache" },
  25645. 0x081E: { n:"BrtEndTableSlicerCache" },
  25646. 0x081F: { n:"BrtSxFilter15" },
  25647. 0x0820: { n:"BrtBeginTimelineCachePivotCacheIDs" },
  25648. 0x0821: { n:"BrtEndTimelineCachePivotCacheIDs" },
  25649. 0x0822: { n:"BrtTimelineCachePivotCacheID" },
  25650. 0x0823: { n:"BrtBeginTimelineCacheIDs" },
  25651. 0x0824: { n:"BrtEndTimelineCacheIDs" },
  25652. 0x0825: { n:"BrtBeginTimelineCacheID" },
  25653. 0x0826: { n:"BrtEndTimelineCacheID" },
  25654. 0x0827: { n:"BrtBeginTimelinesEx" },
  25655. 0x0828: { n:"BrtEndTimelinesEx" },
  25656. 0x0829: { n:"BrtBeginTimelineEx" },
  25657. 0x082A: { n:"BrtEndTimelineEx" },
  25658. 0x082B: { n:"BrtWorkBookPr15" },
  25659. 0x082C: { n:"BrtPCDH15" },
  25660. 0x082D: { n:"BrtBeginTimelineStyle" },
  25661. 0x082E: { n:"BrtEndTimelineStyle" },
  25662. 0x082F: { n:"BrtTimelineStyleElement" },
  25663. 0x0830: { n:"BrtBeginTimelineStylesheetExt15" },
  25664. 0x0831: { n:"BrtEndTimelineStylesheetExt15" },
  25665. 0x0832: { n:"BrtBeginTimelineStyles" },
  25666. 0x0833: { n:"BrtEndTimelineStyles" },
  25667. 0x0834: { n:"BrtBeginTimelineStyleElements" },
  25668. 0x0835: { n:"BrtEndTimelineStyleElements" },
  25669. 0x0836: { n:"BrtDxf15" },
  25670. 0x0837: { n:"BrtBeginDxfs15" },
  25671. 0x0838: { n:"brtEndDxfs15" },
  25672. 0x0839: { n:"BrtSlicerCacheHideItemsWithNoData" },
  25673. 0x083A: { n:"BrtBeginItemUniqueNames" },
  25674. 0x083B: { n:"BrtEndItemUniqueNames" },
  25675. 0x083C: { n:"BrtItemUniqueName" },
  25676. 0x083D: { n:"BrtBeginExtConn15" },
  25677. 0x083E: { n:"BrtEndExtConn15" },
  25678. 0x083F: { n:"BrtBeginOledbPr15" },
  25679. 0x0840: { n:"BrtEndOledbPr15" },
  25680. 0x0841: { n:"BrtBeginDataFeedPr15" },
  25681. 0x0842: { n:"BrtEndDataFeedPr15" },
  25682. 0x0843: { n:"BrtTextPr15" },
  25683. 0x0844: { n:"BrtRangePr15" },
  25684. 0x0845: { n:"BrtDbCommand15" },
  25685. 0x0846: { n:"BrtBeginDbTables15" },
  25686. 0x0847: { n:"BrtEndDbTables15" },
  25687. 0x0848: { n:"BrtDbTable15" },
  25688. 0x0849: { n:"BrtBeginDataModel" },
  25689. 0x084A: { n:"BrtEndDataModel" },
  25690. 0x084B: { n:"BrtBeginModelTables" },
  25691. 0x084C: { n:"BrtEndModelTables" },
  25692. 0x084D: { n:"BrtModelTable" },
  25693. 0x084E: { n:"BrtBeginModelRelationships" },
  25694. 0x084F: { n:"BrtEndModelRelationships" },
  25695. 0x0850: { n:"BrtModelRelationship" },
  25696. 0x0851: { n:"BrtBeginECTxtWiz15" },
  25697. 0x0852: { n:"BrtEndECTxtWiz15" },
  25698. 0x0853: { n:"BrtBeginECTWFldInfoLst15" },
  25699. 0x0854: { n:"BrtEndECTWFldInfoLst15" },
  25700. 0x0855: { n:"BrtBeginECTWFldInfo15" },
  25701. 0x0856: { n:"BrtFieldListActiveItem" },
  25702. 0x0857: { n:"BrtPivotCacheIdVersion" },
  25703. 0x0858: { n:"BrtSXDI15" },
  25704. 0x0859: { n:"BrtBeginModelTimeGroupings" },
  25705. 0x085A: { n:"BrtEndModelTimeGroupings" },
  25706. 0x085B: { n:"BrtBeginModelTimeGrouping" },
  25707. 0x085C: { n:"BrtEndModelTimeGrouping" },
  25708. 0x085D: { n:"BrtModelTimeGroupingCalcCol" },
  25709. 0x0C00: { n:"BrtUid" },
  25710. 0x0C01: { n:"BrtRevisionPtr" },
  25711. 0x13e7: { n:"BrtBeginCalcFeatures" },
  25712. 0x13e8: { n:"BrtEndCalcFeatures" },
  25713. 0x13e9: { n:"BrtCalcFeature" },
  25714. 0xFFFF: { n:"" }
  25715. };
  25716. var XLSBRE = evert_key(XLSBRecordEnum, 'n');
  25717. /* [MS-XLS] 2.3 Record Enumeration */
  25718. var XLSRecordEnum = {
  25719. 0x0003: { n:"BIFF2NUM", f:parse_BIFF2NUM },
  25720. 0x0004: { n:"BIFF2STR", f:parse_BIFF2STR },
  25721. 0x0006: { n:"Formula", f:parse_Formula },
  25722. 0x0009: { n:'BOF', f:parse_BOF },
  25723. 0x000a: { n:'EOF', f:parsenoop2 },
  25724. 0x000c: { n:"CalcCount", f:parseuint16 },
  25725. 0x000d: { n:"CalcMode", f:parseuint16 },
  25726. 0x000e: { n:"CalcPrecision", f:parsebool },
  25727. 0x000f: { n:"CalcRefMode", f:parsebool },
  25728. 0x0010: { n:"CalcDelta", f:parse_Xnum },
  25729. 0x0011: { n:"CalcIter", f:parsebool },
  25730. 0x0012: { n:"Protect", f:parsebool },
  25731. 0x0013: { n:"Password", f:parseuint16 },
  25732. 0x0014: { n:"Header", f:parse_XLHeaderFooter },
  25733. 0x0015: { n:"Footer", f:parse_XLHeaderFooter },
  25734. 0x0017: { n:"ExternSheet", f:parse_ExternSheet },
  25735. 0x0018: { n:"Lbl", f:parse_Lbl },
  25736. 0x0019: { n:"WinProtect", f:parsebool },
  25737. 0x001a: { n:"VerticalPageBreaks" },
  25738. 0x001b: { n:"HorizontalPageBreaks" },
  25739. 0x001c: { n:"Note", f:parse_Note },
  25740. 0x001d: { n:"Selection" },
  25741. 0x0022: { n:"Date1904", f:parsebool },
  25742. 0x0023: { n:"ExternName", f:parse_ExternName },
  25743. 0x0026: { n:"LeftMargin", f:parse_Xnum },
  25744. 0x0027: { n:"RightMargin", f:parse_Xnum },
  25745. 0x0028: { n:"TopMargin", f:parse_Xnum },
  25746. 0x0029: { n:"BottomMargin", f:parse_Xnum },
  25747. 0x002a: { n:"PrintRowCol", f:parsebool },
  25748. 0x002b: { n:"PrintGrid", f:parsebool },
  25749. 0x002f: { n:"FilePass", f:parse_FilePass },
  25750. 0x0031: { n:"Font", f:parse_Font },
  25751. 0x0033: { n:"PrintSize", f:parseuint16 },
  25752. 0x003c: { n:"Continue" },
  25753. 0x003d: { n:"Window1", f:parse_Window1 },
  25754. 0x0040: { n:"Backup", f:parsebool },
  25755. 0x0041: { n:"Pane" },
  25756. 0x0042: { n:'CodePage', f:parseuint16 },
  25757. 0x004d: { n:"Pls" },
  25758. 0x0050: { n:"DCon" },
  25759. 0x0051: { n:"DConRef" },
  25760. 0x0052: { n:"DConName" },
  25761. 0x0055: { n:"DefColWidth", f:parseuint16 },
  25762. 0x0059: { n:"XCT" },
  25763. 0x005a: { n:"CRN" },
  25764. 0x005b: { n:"FileSharing" },
  25765. 0x005c: { n:'WriteAccess', f:parse_WriteAccess },
  25766. 0x005d: { n:"Obj", f:parse_Obj },
  25767. 0x005e: { n:"Uncalced" },
  25768. 0x005f: { n:"CalcSaveRecalc", f:parsebool },
  25769. 0x0060: { n:"Template" },
  25770. 0x0061: { n:"Intl" },
  25771. 0x0063: { n:"ObjProtect", f:parsebool },
  25772. 0x007d: { n:"ColInfo", f:parse_ColInfo },
  25773. 0x0080: { n:"Guts", f:parse_Guts },
  25774. 0x0081: { n:"WsBool", f:parse_WsBool },
  25775. 0x0082: { n:"GridSet", f:parseuint16 },
  25776. 0x0083: { n:"HCenter", f:parsebool },
  25777. 0x0084: { n:"VCenter", f:parsebool },
  25778. 0x0085: { n:'BoundSheet8', f:parse_BoundSheet8 },
  25779. 0x0086: { n:"WriteProtect" },
  25780. 0x008c: { n:"Country", f:parse_Country },
  25781. 0x008d: { n:"HideObj", f:parseuint16 },
  25782. 0x0090: { n:"Sort" },
  25783. 0x0092: { n:"Palette", f:parse_Palette },
  25784. 0x0097: { n:"Sync" },
  25785. 0x0098: { n:"LPr" },
  25786. 0x0099: { n:"DxGCol" },
  25787. 0x009a: { n:"FnGroupName" },
  25788. 0x009b: { n:"FilterMode" },
  25789. 0x009c: { n:"BuiltInFnGroupCount", f:parseuint16 },
  25790. 0x009d: { n:"AutoFilterInfo" },
  25791. 0x009e: { n:"AutoFilter" },
  25792. 0x00a0: { n:"Scl", f:parse_Scl },
  25793. 0x00a1: { n:"Setup", f:parse_Setup },
  25794. 0x00ae: { n:"ScenMan" },
  25795. 0x00af: { n:"SCENARIO" },
  25796. 0x00b0: { n:"SxView" },
  25797. 0x00b1: { n:"Sxvd" },
  25798. 0x00b2: { n:"SXVI" },
  25799. 0x00b4: { n:"SxIvd" },
  25800. 0x00b5: { n:"SXLI" },
  25801. 0x00b6: { n:"SXPI" },
  25802. 0x00b8: { n:"DocRoute" },
  25803. 0x00b9: { n:"RecipName" },
  25804. 0x00bd: { n:"MulRk", f:parse_MulRk },
  25805. 0x00be: { n:"MulBlank", f:parse_MulBlank },
  25806. 0x00c1: { n:'Mms', f:parsenoop2 },
  25807. 0x00c5: { n:"SXDI" },
  25808. 0x00c6: { n:"SXDB" },
  25809. 0x00c7: { n:"SXFDB" },
  25810. 0x00c8: { n:"SXDBB" },
  25811. 0x00c9: { n:"SXNum" },
  25812. 0x00ca: { n:"SxBool", f:parsebool },
  25813. 0x00cb: { n:"SxErr" },
  25814. 0x00cc: { n:"SXInt" },
  25815. 0x00cd: { n:"SXString" },
  25816. 0x00ce: { n:"SXDtr" },
  25817. 0x00cf: { n:"SxNil" },
  25818. 0x00d0: { n:"SXTbl" },
  25819. 0x00d1: { n:"SXTBRGIITM" },
  25820. 0x00d2: { n:"SxTbpg" },
  25821. 0x00d3: { n:"ObProj" },
  25822. 0x00d5: { n:"SXStreamID" },
  25823. 0x00d7: { n:"DBCell" },
  25824. 0x00d8: { n:"SXRng" },
  25825. 0x00d9: { n:"SxIsxoper" },
  25826. 0x00da: { n:"BookBool", f:parseuint16 },
  25827. 0x00dc: { n:"DbOrParamQry" },
  25828. 0x00dd: { n:"ScenarioProtect", f:parsebool },
  25829. 0x00de: { n:"OleObjectSize" },
  25830. 0x00e0: { n:"XF", f:parse_XF },
  25831. 0x00e1: { n:'InterfaceHdr', f:parse_InterfaceHdr },
  25832. 0x00e2: { n:'InterfaceEnd', f:parsenoop2 },
  25833. 0x00e3: { n:"SXVS" },
  25834. 0x00e5: { n:"MergeCells", f:parse_MergeCells },
  25835. 0x00e9: { n:"BkHim" },
  25836. 0x00eb: { n:"MsoDrawingGroup" },
  25837. 0x00ec: { n:"MsoDrawing" },
  25838. 0x00ed: { n:"MsoDrawingSelection" },
  25839. 0x00ef: { n:"PhoneticInfo" },
  25840. 0x00f0: { n:"SxRule" },
  25841. 0x00f1: { n:"SXEx" },
  25842. 0x00f2: { n:"SxFilt" },
  25843. 0x00f4: { n:"SxDXF" },
  25844. 0x00f5: { n:"SxItm" },
  25845. 0x00f6: { n:"SxName" },
  25846. 0x00f7: { n:"SxSelect" },
  25847. 0x00f8: { n:"SXPair" },
  25848. 0x00f9: { n:"SxFmla" },
  25849. 0x00fb: { n:"SxFormat" },
  25850. 0x00fc: { n:"SST", f:parse_SST },
  25851. 0x00fd: { n:"LabelSst", f:parse_LabelSst },
  25852. 0x00ff: { n:"ExtSST", f:parse_ExtSST },
  25853. 0x0100: { n:"SXVDEx" },
  25854. 0x0103: { n:"SXFormula" },
  25855. 0x0122: { n:"SXDBEx" },
  25856. 0x0137: { n:"RRDInsDel" },
  25857. 0x0138: { n:"RRDHead" },
  25858. 0x013b: { n:"RRDChgCell" },
  25859. 0x013d: { n:"RRTabId", f:parseuint16a },
  25860. 0x013e: { n:"RRDRenSheet" },
  25861. 0x013f: { n:"RRSort" },
  25862. 0x0140: { n:"RRDMove" },
  25863. 0x014a: { n:"RRFormat" },
  25864. 0x014b: { n:"RRAutoFmt" },
  25865. 0x014d: { n:"RRInsertSh" },
  25866. 0x014e: { n:"RRDMoveBegin" },
  25867. 0x014f: { n:"RRDMoveEnd" },
  25868. 0x0150: { n:"RRDInsDelBegin" },
  25869. 0x0151: { n:"RRDInsDelEnd" },
  25870. 0x0152: { n:"RRDConflict" },
  25871. 0x0153: { n:"RRDDefName" },
  25872. 0x0154: { n:"RRDRstEtxp" },
  25873. 0x015f: { n:"LRng" },
  25874. 0x0160: { n:"UsesELFs", f:parsebool },
  25875. 0x0161: { n:"DSF", f:parsenoop2 },
  25876. 0x0191: { n:"CUsr" },
  25877. 0x0192: { n:"CbUsr" },
  25878. 0x0193: { n:"UsrInfo" },
  25879. 0x0194: { n:"UsrExcl" },
  25880. 0x0195: { n:"FileLock" },
  25881. 0x0196: { n:"RRDInfo" },
  25882. 0x0197: { n:"BCUsrs" },
  25883. 0x0198: { n:"UsrChk" },
  25884. 0x01a9: { n:"UserBView" },
  25885. 0x01aa: { n:"UserSViewBegin" },
  25886. 0x01ab: { n:"UserSViewEnd" },
  25887. 0x01ac: { n:"RRDUserView" },
  25888. 0x01ad: { n:"Qsi" },
  25889. 0x01ae: { n:"SupBook", f:parse_SupBook },
  25890. 0x01af: { n:"Prot4Rev", f:parsebool },
  25891. 0x01b0: { n:"CondFmt" },
  25892. 0x01b1: { n:"CF" },
  25893. 0x01b2: { n:"DVal" },
  25894. 0x01b5: { n:"DConBin" },
  25895. 0x01b6: { n:"TxO", f:parse_TxO },
  25896. 0x01b7: { n:"RefreshAll", f:parsebool },
  25897. 0x01b8: { n:"HLink", f:parse_HLink },
  25898. 0x01b9: { n:"Lel" },
  25899. 0x01ba: { n:"CodeName", f:parse_XLUnicodeString },
  25900. 0x01bb: { n:"SXFDBType" },
  25901. 0x01bc: { n:"Prot4RevPass", f:parseuint16 },
  25902. 0x01bd: { n:"ObNoMacros" },
  25903. 0x01be: { n:"Dv" },
  25904. 0x01c0: { n:"Excel9File", f:parsenoop2 },
  25905. 0x01c1: { n:"RecalcId", f:parse_RecalcId, r:2},
  25906. 0x01c2: { n:"EntExU2", f:parsenoop2 },
  25907. 0x0200: { n:"Dimensions", f:parse_Dimensions },
  25908. 0x0201: { n:"Blank", f:parse_Blank },
  25909. 0x0203: { n:"Number", f:parse_Number },
  25910. 0x0204: { n:"Label", f:parse_Label },
  25911. 0x0205: { n:"BoolErr", f:parse_BoolErr },
  25912. 0x0206: { n:"Formula", f:parse_Formula },
  25913. 0x0207: { n:"String", f:parse_String },
  25914. 0x0208: { n:'Row', f:parse_Row },
  25915. 0x020b: { n:"Index" },
  25916. 0x0221: { n:"Array", f:parse_Array },
  25917. 0x0225: { n:"DefaultRowHeight", f:parse_DefaultRowHeight },
  25918. 0x0236: { n:"Table" },
  25919. 0x023e: { n:"Window2", f:parse_Window2 },
  25920. 0x027e: { n:"RK", f:parse_RK },
  25921. 0x0293: { n:"Style" },
  25922. 0x0406: { n:"Formula", f:parse_Formula },
  25923. 0x0418: { n:"BigName" },
  25924. 0x041e: { n:"Format", f:parse_Format },
  25925. 0x043c: { n:"ContinueBigName" },
  25926. 0x04bc: { n:"ShrFmla", f:parse_ShrFmla },
  25927. 0x0800: { n:"HLinkTooltip", f:parse_HLinkTooltip },
  25928. 0x0801: { n:"WebPub" },
  25929. 0x0802: { n:"QsiSXTag" },
  25930. 0x0803: { n:"DBQueryExt" },
  25931. 0x0804: { n:"ExtString" },
  25932. 0x0805: { n:"TxtQry" },
  25933. 0x0806: { n:"Qsir" },
  25934. 0x0807: { n:"Qsif" },
  25935. 0x0808: { n:"RRDTQSIF" },
  25936. 0x0809: { n:'BOF', f:parse_BOF },
  25937. 0x080a: { n:"OleDbConn" },
  25938. 0x080b: { n:"WOpt" },
  25939. 0x080c: { n:"SXViewEx" },
  25940. 0x080d: { n:"SXTH" },
  25941. 0x080e: { n:"SXPIEx" },
  25942. 0x080f: { n:"SXVDTEx" },
  25943. 0x0810: { n:"SXViewEx9" },
  25944. 0x0812: { n:"ContinueFrt" },
  25945. 0x0813: { n:"RealTimeData" },
  25946. 0x0850: { n:"ChartFrtInfo" },
  25947. 0x0851: { n:"FrtWrapper" },
  25948. 0x0852: { n:"StartBlock" },
  25949. 0x0853: { n:"EndBlock" },
  25950. 0x0854: { n:"StartObject" },
  25951. 0x0855: { n:"EndObject" },
  25952. 0x0856: { n:"CatLab" },
  25953. 0x0857: { n:"YMult" },
  25954. 0x0858: { n:"SXViewLink" },
  25955. 0x0859: { n:"PivotChartBits" },
  25956. 0x085a: { n:"FrtFontList" },
  25957. 0x0862: { n:"SheetExt" },
  25958. 0x0863: { n:"BookExt", r:12},
  25959. 0x0864: { n:"SXAddl" },
  25960. 0x0865: { n:"CrErr" },
  25961. 0x0866: { n:"HFPicture" },
  25962. 0x0867: { n:'FeatHdr', f:parsenoop2 },
  25963. 0x0868: { n:"Feat" },
  25964. 0x086a: { n:"DataLabExt" },
  25965. 0x086b: { n:"DataLabExtContents" },
  25966. 0x086c: { n:"CellWatch" },
  25967. 0x0871: { n:"FeatHdr11" },
  25968. 0x0872: { n:"Feature11" },
  25969. 0x0874: { n:"DropDownObjIds" },
  25970. 0x0875: { n:"ContinueFrt11" },
  25971. 0x0876: { n:"DConn" },
  25972. 0x0877: { n:"List12" },
  25973. 0x0878: { n:"Feature12" },
  25974. 0x0879: { n:"CondFmt12" },
  25975. 0x087a: { n:"CF12" },
  25976. 0x087b: { n:"CFEx" },
  25977. 0x087c: { n:"XFCRC", f:parse_XFCRC, r:12 },
  25978. 0x087d: { n:"XFExt", f:parse_XFExt, r:12 },
  25979. 0x087e: { n:"AutoFilter12" },
  25980. 0x087f: { n:"ContinueFrt12" },
  25981. 0x0884: { n:"MDTInfo" },
  25982. 0x0885: { n:"MDXStr" },
  25983. 0x0886: { n:"MDXTuple" },
  25984. 0x0887: { n:"MDXSet" },
  25985. 0x0888: { n:"MDXProp" },
  25986. 0x0889: { n:"MDXKPI" },
  25987. 0x088a: { n:"MDB" },
  25988. 0x088b: { n:"PLV" },
  25989. 0x088c: { n:"Compat12", f:parsebool, r:12 },
  25990. 0x088d: { n:"DXF" },
  25991. 0x088e: { n:"TableStyles", r:12 },
  25992. 0x088f: { n:"TableStyle" },
  25993. 0x0890: { n:"TableStyleElement" },
  25994. 0x0892: { n:"StyleExt" },
  25995. 0x0893: { n:"NamePublish" },
  25996. 0x0894: { n:"NameCmt", f:parse_NameCmt, r:12 },
  25997. 0x0895: { n:"SortData" },
  25998. 0x0896: { n:"Theme", f:parse_Theme, r:12 },
  25999. 0x0897: { n:"GUIDTypeLib" },
  26000. 0x0898: { n:"FnGrp12" },
  26001. 0x0899: { n:"NameFnGrp12" },
  26002. 0x089a: { n:"MTRSettings", f:parse_MTRSettings, r:12 },
  26003. 0x089b: { n:"CompressPictures", f:parsenoop2 },
  26004. 0x089c: { n:"HeaderFooter" },
  26005. 0x089d: { n:"CrtLayout12" },
  26006. 0x089e: { n:"CrtMlFrt" },
  26007. 0x089f: { n:"CrtMlFrtContinue" },
  26008. 0x08a3: { n:"ForceFullCalculation", f:parse_ForceFullCalculation },
  26009. 0x08a4: { n:"ShapePropsStream" },
  26010. 0x08a5: { n:"TextPropsStream" },
  26011. 0x08a6: { n:"RichTextStream" },
  26012. 0x08a7: { n:"CrtLayout12A" },
  26013. 0x1001: { n:"Units" },
  26014. 0x1002: { n:"Chart" },
  26015. 0x1003: { n:"Series" },
  26016. 0x1006: { n:"DataFormat" },
  26017. 0x1007: { n:"LineFormat" },
  26018. 0x1009: { n:"MarkerFormat" },
  26019. 0x100a: { n:"AreaFormat" },
  26020. 0x100b: { n:"PieFormat" },
  26021. 0x100c: { n:"AttachedLabel" },
  26022. 0x100d: { n:"SeriesText" },
  26023. 0x1014: { n:"ChartFormat" },
  26024. 0x1015: { n:"Legend" },
  26025. 0x1016: { n:"SeriesList" },
  26026. 0x1017: { n:"Bar" },
  26027. 0x1018: { n:"Line" },
  26028. 0x1019: { n:"Pie" },
  26029. 0x101a: { n:"Area" },
  26030. 0x101b: { n:"Scatter" },
  26031. 0x101c: { n:"CrtLine" },
  26032. 0x101d: { n:"Axis" },
  26033. 0x101e: { n:"Tick" },
  26034. 0x101f: { n:"ValueRange" },
  26035. 0x1020: { n:"CatSerRange" },
  26036. 0x1021: { n:"AxisLine" },
  26037. 0x1022: { n:"CrtLink" },
  26038. 0x1024: { n:"DefaultText" },
  26039. 0x1025: { n:"Text" },
  26040. 0x1026: { n:"FontX", f:parseuint16 },
  26041. 0x1027: { n:"ObjectLink" },
  26042. 0x1032: { n:"Frame" },
  26043. 0x1033: { n:"Begin" },
  26044. 0x1034: { n:"End" },
  26045. 0x1035: { n:"PlotArea" },
  26046. 0x103a: { n:"Chart3d" },
  26047. 0x103c: { n:"PicF" },
  26048. 0x103d: { n:"DropBar" },
  26049. 0x103e: { n:"Radar" },
  26050. 0x103f: { n:"Surf" },
  26051. 0x1040: { n:"RadarArea" },
  26052. 0x1041: { n:"AxisParent" },
  26053. 0x1043: { n:"LegendException" },
  26054. 0x1044: { n:"ShtProps", f:parse_ShtProps },
  26055. 0x1045: { n:"SerToCrt" },
  26056. 0x1046: { n:"AxesUsed" },
  26057. 0x1048: { n:"SBaseRef" },
  26058. 0x104a: { n:"SerParent" },
  26059. 0x104b: { n:"SerAuxTrend" },
  26060. 0x104e: { n:"IFmtRecord" },
  26061. 0x104f: { n:"Pos" },
  26062. 0x1050: { n:"AlRuns" },
  26063. 0x1051: { n:"BRAI" },
  26064. 0x105b: { n:"SerAuxErrBar" },
  26065. 0x105c: { n:"ClrtClient", f:parse_ClrtClient },
  26066. 0x105d: { n:"SerFmt" },
  26067. 0x105f: { n:"Chart3DBarShape" },
  26068. 0x1060: { n:"Fbi" },
  26069. 0x1061: { n:"BopPop" },
  26070. 0x1062: { n:"AxcExt" },
  26071. 0x1063: { n:"Dat" },
  26072. 0x1064: { n:"PlotGrowth" },
  26073. 0x1065: { n:"SIIndex" },
  26074. 0x1066: { n:"GelFrame" },
  26075. 0x1067: { n:"BopPopCustom" },
  26076. 0x1068: { n:"Fbi2" },
  26077. 0x0000: { n:"Dimensions", f:parse_Dimensions },
  26078. 0x0002: { n:"BIFF2INT", f:parse_BIFF2INT },
  26079. 0x0005: { n:"BoolErr", f:parse_BoolErr },
  26080. 0x0007: { n:"String", f:parse_BIFF2STRING },
  26081. 0x0008: { n:"BIFF2ROW" },
  26082. 0x000b: { n:"Index" },
  26083. 0x0016: { n:"ExternCount", f:parseuint16 },
  26084. 0x001e: { n:"BIFF2FORMAT", f:parse_BIFF2Format },
  26085. 0x001f: { n:"BIFF2FMTCNT" }, /* 16-bit cnt of BIFF2FORMAT records */
  26086. 0x0020: { n:"BIFF2COLINFO" },
  26087. 0x0021: { n:"Array", f:parse_Array },
  26088. 0x0025: { n:"DefaultRowHeight", f:parse_DefaultRowHeight },
  26089. 0x0032: { n:"BIFF2FONTXTRA", f:parse_BIFF2FONTXTRA },
  26090. 0x0034: { n:"DDEObjName" },
  26091. 0x003e: { n:"BIFF2WINDOW2" },
  26092. 0x0043: { n:"BIFF2XF" },
  26093. 0x0045: { n:"BIFF2FONTCLR" },
  26094. 0x0056: { n:"BIFF4FMTCNT" }, /* 16-bit cnt, similar to BIFF2 */
  26095. 0x007e: { n:"RK" }, /* Not necessarily same as 0x027e */
  26096. 0x007f: { n:"ImData", f:parse_ImData },
  26097. 0x0087: { n:"Addin" },
  26098. 0x0088: { n:"Edg" },
  26099. 0x0089: { n:"Pub" },
  26100. 0x0091: { n:"Sub" },
  26101. 0x0094: { n:"LHRecord" },
  26102. 0x0095: { n:"LHNGraph" },
  26103. 0x0096: { n:"Sound" },
  26104. 0x00a9: { n:"CoordList" },
  26105. 0x00ab: { n:"GCW" },
  26106. 0x00bc: { n:"ShrFmla" }, /* Not necessarily same as 0x04bc */
  26107. 0x00bf: { n:"ToolbarHdr" },
  26108. 0x00c0: { n:"ToolbarEnd" },
  26109. 0x00c2: { n:"AddMenu" },
  26110. 0x00c3: { n:"DelMenu" },
  26111. 0x00d6: { n:"RString", f:parse_RString },
  26112. 0x00df: { n:"UDDesc" },
  26113. 0x00ea: { n:"TabIdConf" },
  26114. 0x0162: { n:"XL5Modify" },
  26115. 0x01a5: { n:"FileSharing2" },
  26116. 0x0209: { n:'BOF', f:parse_BOF },
  26117. 0x0218: { n:"Lbl", f:parse_Lbl },
  26118. 0x0223: { n:"ExternName", f:parse_ExternName },
  26119. 0x0231: { n:"Font" },
  26120. 0x0243: { n:"BIFF3XF" },
  26121. 0x0409: { n:'BOF', f:parse_BOF },
  26122. 0x0443: { n:"BIFF4XF" },
  26123. 0x086d: { n:"FeatInfo" },
  26124. 0x0873: { n:"FeatInfo11" },
  26125. 0x0881: { n:"SXAddl12" },
  26126. 0x08c0: { n:"AutoWebPub" },
  26127. 0x08c1: { n:"ListObj" },
  26128. 0x08c2: { n:"ListField" },
  26129. 0x08c3: { n:"ListDV" },
  26130. 0x08c4: { n:"ListCondFmt" },
  26131. 0x08c5: { n:"ListCF" },
  26132. 0x08c6: { n:"FMQry" },
  26133. 0x08c7: { n:"FMSQry" },
  26134. 0x08c8: { n:"PLV" },
  26135. 0x08c9: { n:"LnExt" },
  26136. 0x08ca: { n:"MkrExt" },
  26137. 0x08cb: { n:"CrtCoopt" },
  26138. 0x08d6: { n:"FRTArchId$", r:12 },
  26139. 0x7262: {}
  26140. };
  26141. var XLSRE = evert_key(XLSRecordEnum, 'n');
  26142. function write_biff_rec(ba, type, payload, length) {
  26143. var t = +type || +XLSRE[type];
  26144. if(isNaN(t)) return;
  26145. var len = length || (payload||[]).length || 0;
  26146. var o = ba.next(4);
  26147. o.write_shift(2, t);
  26148. o.write_shift(2, len);
  26149. if(len > 0 && is_buf(payload)) ba.push(payload);
  26150. }
  26151. function write_BIFF2Cell(out, r, c) {
  26152. if(!out) out = new_buf(7);
  26153. out.write_shift(2, r);
  26154. out.write_shift(2, c);
  26155. out.write_shift(2, 0);
  26156. out.write_shift(1, 0);
  26157. return out;
  26158. }
  26159. function write_BIFF2BERR(r, c, val, t) {
  26160. var out = new_buf(9);
  26161. write_BIFF2Cell(out, r, c);
  26162. if(t == 'e') { out.write_shift(1, val); out.write_shift(1, 1); }
  26163. else { out.write_shift(1, val?1:0); out.write_shift(1, 0); }
  26164. return out;
  26165. }
  26166. /* TODO: codepage, large strings */
  26167. function write_BIFF2LABEL(r, c, val) {
  26168. var out = new_buf(8 + 2*val.length);
  26169. write_BIFF2Cell(out, r, c);
  26170. out.write_shift(1, val.length);
  26171. out.write_shift(val.length, val, 'sbcs');
  26172. return out.l < out.length ? out.slice(0, out.l) : out;
  26173. }
  26174. function write_ws_biff2_cell(ba, cell, R, C) {
  26175. if(cell.v != null) switch(cell.t) {
  26176. case 'd': case 'n':
  26177. var v = cell.t == 'd' ? datenum(parseDate(cell.v)) : cell.v;
  26178. if((v == (v|0)) && (v >= 0) && (v < 65536))
  26179. write_biff_rec(ba, 0x0002, write_BIFF2INT(R, C, v));
  26180. else
  26181. write_biff_rec(ba, 0x0003, write_BIFF2NUM(R,C, v));
  26182. return;
  26183. case 'b': case 'e': write_biff_rec(ba, 0x0005, write_BIFF2BERR(R, C, cell.v, cell.t)); return;
  26184. /* TODO: codepage, sst */
  26185. case 's': case 'str':
  26186. write_biff_rec(ba, 0x0004, write_BIFF2LABEL(R, C, cell.v));
  26187. return;
  26188. }
  26189. write_biff_rec(ba, 0x0001, write_BIFF2Cell(null, R, C));
  26190. }
  26191. function write_ws_biff2(ba, ws, idx, opts) {
  26192. var dense = Array.isArray(ws);
  26193. var range = safe_decode_range(ws['!ref'] || "A1"), ref, rr = "", cols = [];
  26194. if(range.e.c > 0xFF || range.e.r > 0x3FFF) {
  26195. if(opts.WTF) throw new Error("Range " + (ws['!ref'] || "A1") + " exceeds format limit A1:IV16384");
  26196. range.e.c = Math.min(range.e.c, 0xFF);
  26197. range.e.r = Math.min(range.e.c, 0x3FFF);
  26198. ref = encode_range(range);
  26199. }
  26200. for(var R = range.s.r; R <= range.e.r; ++R) {
  26201. rr = encode_row(R);
  26202. for(var C = range.s.c; C <= range.e.c; ++C) {
  26203. if(R === range.s.r) cols[C] = encode_col(C);
  26204. ref = cols[C] + rr;
  26205. var cell = dense ? (ws[R]||[])[C] : ws[ref];
  26206. if(!cell) continue;
  26207. /* write cell */
  26208. write_ws_biff2_cell(ba, cell, R, C, opts);
  26209. }
  26210. }
  26211. }
  26212. /* Based on test files */
  26213. function write_biff2_buf(wb, opts) {
  26214. var o = opts || {};
  26215. if(DENSE != null && o.dense == null) o.dense = DENSE;
  26216. var ba = buf_array();
  26217. var idx = 0;
  26218. for(var i=0;i<wb.SheetNames.length;++i) if(wb.SheetNames[i] == o.sheet) idx=i;
  26219. if(idx == 0 && !!o.sheet && wb.SheetNames[0] != o.sheet) throw new Error("Sheet not found: " + o.sheet);
  26220. write_biff_rec(ba, 0x0009, write_BOF(wb, 0x10, o));
  26221. /* ... */
  26222. write_ws_biff2(ba, wb.Sheets[wb.SheetNames[idx]], idx, o, wb);
  26223. /* ... */
  26224. write_biff_rec(ba, 0x000A);
  26225. return ba.end();
  26226. }
  26227. function write_FONTS_biff8(ba, data, opts) {
  26228. write_biff_rec(ba, "Font", write_Font({
  26229. sz:12,
  26230. color: {theme:1},
  26231. name: "Arial",
  26232. family: 2,
  26233. scheme: "minor"
  26234. }, opts));
  26235. }
  26236. function write_FMTS_biff8(ba, NF, opts) {
  26237. if(!NF) return;
  26238. [[5,8],[23,26],[41,44],[/*63*/50,/*66],[164,*/392]].forEach(function(r) {
  26239. for(var i = r[0]; i <= r[1]; ++i) if(NF[i] != null) write_biff_rec(ba, "Format", write_Format(i, NF[i], opts));
  26240. });
  26241. }
  26242. function write_FEAT(ba, ws) {
  26243. /* [MS-XLS] 2.4.112 */
  26244. var o = new_buf(19);
  26245. o.write_shift(4, 0x867); o.write_shift(4, 0); o.write_shift(4, 0);
  26246. o.write_shift(2, 3); o.write_shift(1, 1); o.write_shift(4, 0);
  26247. write_biff_rec(ba, "FeatHdr", o);
  26248. /* [MS-XLS] 2.4.111 */
  26249. o = new_buf(39);
  26250. o.write_shift(4, 0x868); o.write_shift(4, 0); o.write_shift(4, 0);
  26251. o.write_shift(2, 3); o.write_shift(1, 0); o.write_shift(4, 0);
  26252. o.write_shift(2, 1); o.write_shift(4, 4); o.write_shift(2, 0);
  26253. write_Ref8U(safe_decode_range(ws['!ref']||"A1"), o);
  26254. o.write_shift(4, 4);
  26255. write_biff_rec(ba, "Feat", o);
  26256. }
  26257. function write_CELLXFS_biff8(ba, opts) {
  26258. for(var i = 0; i < 16; ++i) write_biff_rec(ba, "XF", write_XF({numFmtId:0, style:true}, 0, opts));
  26259. opts.cellXfs.forEach(function(c) {
  26260. write_biff_rec(ba, "XF", write_XF(c, 0, opts));
  26261. });
  26262. }
  26263. function write_ws_biff8_hlinks(ba, ws) {
  26264. for(var R=0; R<ws['!links'].length; ++R) {
  26265. var HL = ws['!links'][R];
  26266. write_biff_rec(ba, "HLink", write_HLink(HL));
  26267. if(HL[1].Tooltip) write_biff_rec(ba, "HLinkTooltip", write_HLinkTooltip(HL));
  26268. }
  26269. delete ws['!links'];
  26270. }
  26271. function write_ws_biff8_cell(ba, cell, R, C, opts) {
  26272. var os = 16 + get_cell_style(opts.cellXfs, cell, opts);
  26273. if(cell.v != null) switch(cell.t) {
  26274. case 'd': case 'n':
  26275. var v = cell.t == 'd' ? datenum(parseDate(cell.v)) : cell.v;
  26276. /* TODO: emit RK as appropriate */
  26277. write_biff_rec(ba, "Number", write_Number(R, C, v, os, opts));
  26278. return;
  26279. case 'b': case 'e': write_biff_rec(ba, 0x0205, write_BoolErr(R, C, cell.v, os, opts, cell.t)); return;
  26280. /* TODO: codepage, sst */
  26281. case 's': case 'str':
  26282. write_biff_rec(ba, "Label", write_Label(R, C, cell.v, os, opts));
  26283. return;
  26284. }
  26285. write_biff_rec(ba, "Blank", write_XLSCell(R, C, os));
  26286. }
  26287. /* [MS-XLS] 2.1.7.20.5 */
  26288. function write_ws_biff8(idx, opts, wb) {
  26289. var ba = buf_array();
  26290. var s = wb.SheetNames[idx], ws = wb.Sheets[s] || {};
  26291. var _WB = ((wb||{}).Workbook||{});
  26292. var _sheet = ((_WB.Sheets||[])[idx]||{});
  26293. var dense = Array.isArray(ws);
  26294. var b8 = opts.biff == 8;
  26295. var ref, rr = "", cols = [];
  26296. var range = safe_decode_range(ws['!ref'] || "A1");
  26297. var MAX_ROWS = b8 ? 65536 : 16384;
  26298. if(range.e.c > 0xFF || range.e.r >= MAX_ROWS) {
  26299. if(opts.WTF) throw new Error("Range " + (ws['!ref'] || "A1") + " exceeds format limit A1:IV16384");
  26300. range.e.c = Math.min(range.e.c, 0xFF);
  26301. range.e.r = Math.min(range.e.c, MAX_ROWS-1);
  26302. }
  26303. write_biff_rec(ba, 0x0809, write_BOF(wb, 0x10, opts));
  26304. /* ... */
  26305. write_biff_rec(ba, "CalcMode", writeuint16(1));
  26306. write_biff_rec(ba, "CalcCount", writeuint16(100));
  26307. write_biff_rec(ba, "CalcRefMode", writebool(true));
  26308. write_biff_rec(ba, "CalcIter", writebool(false));
  26309. write_biff_rec(ba, "CalcDelta", write_Xnum(0.001));
  26310. write_biff_rec(ba, "CalcSaveRecalc", writebool(true));
  26311. write_biff_rec(ba, "PrintRowCol", writebool(false));
  26312. write_biff_rec(ba, "PrintGrid", writebool(false));
  26313. write_biff_rec(ba, "GridSet", writeuint16(1));
  26314. write_biff_rec(ba, "Guts", write_Guts([0,0]));
  26315. /* ... */
  26316. write_biff_rec(ba, "HCenter", writebool(false));
  26317. write_biff_rec(ba, "VCenter", writebool(false));
  26318. /* ... */
  26319. write_biff_rec(ba, 0x200, write_Dimensions(range, opts));
  26320. /* ... */
  26321. if(b8) ws['!links'] = [];
  26322. for(var R = range.s.r; R <= range.e.r; ++R) {
  26323. rr = encode_row(R);
  26324. for(var C = range.s.c; C <= range.e.c; ++C) {
  26325. if(R === range.s.r) cols[C] = encode_col(C);
  26326. ref = cols[C] + rr;
  26327. var cell = dense ? (ws[R]||[])[C] : ws[ref];
  26328. if(!cell) continue;
  26329. /* write cell */
  26330. write_ws_biff8_cell(ba, cell, R, C, opts);
  26331. if(b8 && cell.l) ws['!links'].push([ref, cell.l]);
  26332. }
  26333. }
  26334. var cname = _sheet.CodeName || _sheet.name || s;
  26335. /* ... */
  26336. if(b8 && _WB.Views) write_biff_rec(ba, "Window2", write_Window2(_WB.Views[0]));
  26337. /* ... */
  26338. if(b8 && (ws['!merges']||[]).length) write_biff_rec(ba, "MergeCells", write_MergeCells(ws['!merges']));
  26339. /* ... */
  26340. if(b8) write_ws_biff8_hlinks(ba, ws);
  26341. /* ... */
  26342. write_biff_rec(ba, "CodeName", write_XLUnicodeString(cname, opts));
  26343. /* ... */
  26344. if(b8) write_FEAT(ba, ws);
  26345. /* ... */
  26346. write_biff_rec(ba, "EOF");
  26347. return ba.end();
  26348. }
  26349. /* [MS-XLS] 2.1.7.20.3 */
  26350. function write_biff8_global(wb, bufs, opts) {
  26351. var A = buf_array();
  26352. var _WB = ((wb||{}).Workbook||{});
  26353. var _sheets = (_WB.Sheets||[]);
  26354. var _wb = _WB.WBProps||{};
  26355. var b8 = opts.biff == 8, b5 = opts.biff == 5;
  26356. write_biff_rec(A, 0x0809, write_BOF(wb, 0x05, opts));
  26357. if(opts.bookType == "xla") write_biff_rec(A, "Addin");
  26358. write_biff_rec(A, "InterfaceHdr", b8 ? writeuint16(0x04b0) : null);
  26359. write_biff_rec(A, "Mms", writezeroes(2));
  26360. if(b5) write_biff_rec(A, "ToolbarHdr");
  26361. if(b5) write_biff_rec(A, "ToolbarEnd");
  26362. write_biff_rec(A, "InterfaceEnd");
  26363. write_biff_rec(A, "WriteAccess", write_WriteAccess("SheetJS", opts));
  26364. write_biff_rec(A, "CodePage", writeuint16(b8 ? 0x04b0 : 0x04E4));
  26365. if(b8) write_biff_rec(A, "DSF", writeuint16(0));
  26366. if(b8) write_biff_rec(A, "Excel9File");
  26367. write_biff_rec(A, "RRTabId", write_RRTabId(wb.SheetNames.length));
  26368. if(b8 && wb.vbaraw) {
  26369. write_biff_rec(A, "ObProj");
  26370. var cname = _wb.CodeName || "ThisWorkbook";
  26371. write_biff_rec(A, "CodeName", write_XLUnicodeString(cname, opts));
  26372. }
  26373. write_biff_rec(A, "BuiltInFnGroupCount", writeuint16(0x11));
  26374. write_biff_rec(A, "WinProtect", writebool(false));
  26375. write_biff_rec(A, "Protect", writebool(false));
  26376. write_biff_rec(A, "Password", writeuint16(0));
  26377. if(b8) write_biff_rec(A, "Prot4Rev", writebool(false));
  26378. if(b8) write_biff_rec(A, "Prot4RevPass", writeuint16(0));
  26379. write_biff_rec(A, "Window1", write_Window1(opts));
  26380. write_biff_rec(A, "Backup", writebool(false));
  26381. write_biff_rec(A, "HideObj", writeuint16(0));
  26382. write_biff_rec(A, "Date1904", writebool(safe1904(wb)=="true"));
  26383. write_biff_rec(A, "CalcPrecision", writebool(true));
  26384. if(b8) write_biff_rec(A, "RefreshAll", writebool(false));
  26385. write_biff_rec(A, "BookBool", writeuint16(0));
  26386. /* ... */
  26387. write_FONTS_biff8(A, wb, opts);
  26388. write_FMTS_biff8(A, wb.SSF, opts);
  26389. write_CELLXFS_biff8(A, opts);
  26390. /* ... */
  26391. if(b8) write_biff_rec(A, "UsesELFs", writebool(false));
  26392. var a = A.end();
  26393. var C = buf_array();
  26394. if(b8) write_biff_rec(C, "Country", write_Country());
  26395. /* BIFF8: [SST *Continue] ExtSST */
  26396. write_biff_rec(C, "EOF");
  26397. var c = C.end();
  26398. var B = buf_array();
  26399. var blen = 0, j = 0;
  26400. for(j = 0; j < wb.SheetNames.length; ++j) blen += (b8 ? 12 : 11) + (b8 ? 2 : 1) * wb.SheetNames[j].length;
  26401. var start = a.length + blen + c.length;
  26402. for(j = 0; j < wb.SheetNames.length; ++j) {
  26403. var _sheet = _sheets[j] || ({});
  26404. write_biff_rec(B, "BoundSheet8", write_BoundSheet8({pos:start, hs:_sheet.Hidden||0, dt:0, name:wb.SheetNames[j]}, opts));
  26405. start += bufs[j].length;
  26406. }
  26407. /* 1*BoundSheet8 */
  26408. var b = B.end();
  26409. if(blen != b.length) throw new Error("BS8 " + blen + " != " + b.length);
  26410. var out = [];
  26411. if(a.length) out.push(a);
  26412. if(b.length) out.push(b);
  26413. if(c.length) out.push(c);
  26414. return __toBuffer([out]);
  26415. }
  26416. /* [MS-XLS] 2.1.7.20 Workbook Stream */
  26417. function write_biff8_buf(wb, opts) {
  26418. var o = opts || {};
  26419. var bufs = [];
  26420. if(wb && !wb.SSF) {
  26421. wb.SSF = SSF.get_table();
  26422. }
  26423. if(wb && wb.SSF) {
  26424. make_ssf(SSF); SSF.load_table(wb.SSF);
  26425. // $FlowIgnore
  26426. o.revssf = evert_num(wb.SSF); o.revssf[wb.SSF[65535]] = 0;
  26427. o.ssf = wb.SSF;
  26428. }
  26429. o.cellXfs = [];
  26430. o.Strings = []; o.Strings.Count = 0; o.Strings.Unique = 0;
  26431. get_cell_style(o.cellXfs, {}, {revssf:{"General":0}});
  26432. for(var i = 0; i < wb.SheetNames.length; ++i) bufs[bufs.length] = write_ws_biff8(i, o, wb);
  26433. bufs.unshift(write_biff8_global(wb, bufs, o));
  26434. return __toBuffer([bufs]);
  26435. }
  26436. function write_biff_buf(wb, opts) {
  26437. var o = opts || {};
  26438. switch(o.biff || 2) {
  26439. case 8: case 5: return write_biff8_buf(wb, opts);
  26440. case 4: case 3: case 2: return write_biff2_buf(wb, opts);
  26441. }
  26442. throw new Error("invalid type " + o.bookType + " for BIFF");
  26443. }
  26444. /* note: browser DOM element cannot see mso- style attrs, must parse */
  26445. var HTML_ = (function() {
  26446. function html_to_sheet(str, _opts) {
  26447. var opts = _opts || {};
  26448. if(DENSE != null && opts.dense == null) opts.dense = DENSE;
  26449. var ws = opts.dense ? ([]) : ({});
  26450. var mtch = str.match(/<table/i);
  26451. if(!mtch) throw new Error("Invalid HTML: could not find <table>");
  26452. var mtch2 = str.match(/<\/table/i);
  26453. var i = mtch.index, j = mtch2 && mtch2.index || str.length;
  26454. var rows = split_regex(str.slice(i, j), /(:?<tr[^>]*>)/i, "<tr>");
  26455. var R = -1, C = 0, RS = 0, CS = 0;
  26456. var range = {s:{r:10000000, c:10000000},e:{r:0,c:0}};
  26457. var merges = [];
  26458. for(i = 0; i < rows.length; ++i) {
  26459. var row = rows[i].trim();
  26460. var hd = row.slice(0,3).toLowerCase();
  26461. if(hd == "<tr") { ++R; if(opts.sheetRows && opts.sheetRows <= R) { --R; break; } C = 0; continue; }
  26462. if(hd != "<td" && hd != "<th") continue;
  26463. var cells = row.split(/<\/t[dh]>/i);
  26464. for(j = 0; j < cells.length; ++j) {
  26465. var cell = cells[j].trim();
  26466. if(!cell.match(/<t[dh]/i)) continue;
  26467. var m = cell, cc = 0;
  26468. /* TODO: parse styles etc */
  26469. while(m.charAt(0) == "<" && (cc = m.indexOf(">")) > -1) m = m.slice(cc+1);
  26470. var tag = parsexmltag(cell.slice(0, cell.indexOf(">")));
  26471. CS = tag.colspan ? +tag.colspan : 1;
  26472. if((RS = +tag.rowspan)>1 || CS>1) merges.push({s:{r:R,c:C},e:{r:R + (RS||1) - 1, c:C + CS - 1}});
  26473. var _t = tag.t || "";
  26474. /* TODO: generate stub cells */
  26475. if(!m.length) { C += CS; continue; }
  26476. m = htmldecode(m);
  26477. if(range.s.r > R) range.s.r = R; if(range.e.r < R) range.e.r = R;
  26478. if(range.s.c > C) range.s.c = C; if(range.e.c < C) range.e.c = C;
  26479. if(!m.length) continue;
  26480. var o = {t:'s', v:m};
  26481. if(opts.raw || !m.trim().length || _t == 's'){}
  26482. else if(m === 'TRUE') o = {t:'b', v:true};
  26483. else if(m === 'FALSE') o = {t:'b', v:false};
  26484. else if(!isNaN(fuzzynum(m))) o = {t:'n', v:fuzzynum(m)};
  26485. else if(!isNaN(fuzzydate(m).getDate())) {
  26486. o = ({t:'d', v:parseDate(m)});
  26487. if(!opts.cellDates) o = ({t:'n', v:datenum(o.v)});
  26488. o.z = opts.dateNF || SSF._table[14];
  26489. }
  26490. if(opts.dense) { if(!ws[R]) ws[R] = []; ws[R][C] = o; }
  26491. else ws[encode_cell({r:R, c:C})] = o;
  26492. C += CS;
  26493. }
  26494. }
  26495. ws['!ref'] = encode_range(range);
  26496. return ws;
  26497. }
  26498. function html_to_book(str, opts) {
  26499. return sheet_to_workbook(html_to_sheet(str, opts), opts);
  26500. }
  26501. function make_html_row(ws, r, R, o) {
  26502. var M = (ws['!merges'] ||[]);
  26503. var oo = [];
  26504. for(var C = r.s.c; C <= r.e.c; ++C) {
  26505. var RS = 0, CS = 0;
  26506. for(var j = 0; j < M.length; ++j) {
  26507. if(M[j].s.r > R || M[j].s.c > C) continue;
  26508. if(M[j].e.r < R || M[j].e.c < C) continue;
  26509. if(M[j].s.r < R || M[j].s.c < C) { RS = -1; break; }
  26510. RS = M[j].e.r - M[j].s.r + 1; CS = M[j].e.c - M[j].s.c + 1; break;
  26511. }
  26512. if(RS < 0) continue;
  26513. var coord = encode_cell({r:R,c:C});
  26514. var cell = o.dense ? (ws[R]||[])[C] : ws[coord];
  26515. var sp = {};
  26516. if(RS > 1) sp.rowspan = RS;
  26517. if(CS > 1) sp.colspan = CS;
  26518. /* TODO: html entities */
  26519. var w = (cell && cell.v != null) && (cell.h || escapehtml(cell.w || (format_cell(cell), cell.w) || "")) || "";
  26520. sp.t = cell && cell.t || 'z';
  26521. if(o.editable) w = '<span contenteditable="true">' + w + '</span>';
  26522. sp.id = "sjs-" + coord;
  26523. oo.push(writextag('td', w, sp));
  26524. }
  26525. var preamble = "<tr>";
  26526. return preamble + oo.join("") + "</tr>";
  26527. }
  26528. function make_html_preamble(ws, R, o) {
  26529. var out = [];
  26530. return out.join("") + '<table' + (o && o.id ? ' id="' + o.id + '"' : "") + '>';
  26531. }
  26532. var _BEGIN = '<html><head><meta charset="utf-8"/><title>SheetJS Table Export</title></head><body>';
  26533. var _END = '</body></html>';
  26534. function sheet_to_html(ws, opts/*, wb:?Workbook*/) {
  26535. var o = opts || {};
  26536. var header = o.header != null ? o.header : _BEGIN;
  26537. var footer = o.footer != null ? o.footer : _END;
  26538. var out = [header];
  26539. var r = decode_range(ws['!ref']);
  26540. o.dense = Array.isArray(ws);
  26541. out.push(make_html_preamble(ws, r, o));
  26542. for(var R = r.s.r; R <= r.e.r; ++R) out.push(make_html_row(ws, r, R, o));
  26543. out.push("</table>" + footer);
  26544. return out.join("");
  26545. }
  26546. return {
  26547. to_workbook: html_to_book,
  26548. to_sheet: html_to_sheet,
  26549. _row: make_html_row,
  26550. BEGIN: _BEGIN,
  26551. END: _END,
  26552. _preamble: make_html_preamble,
  26553. from_sheet: sheet_to_html
  26554. };
  26555. })();
  26556. function parse_dom_table(table, _opts) {
  26557. var opts = _opts || {};
  26558. if(DENSE != null) opts.dense = DENSE;
  26559. var ws = opts.dense ? ([]) : ({});
  26560. var rows = table.getElementsByTagName('tr');
  26561. var sheetRows = opts.sheetRows || 10000000;
  26562. var range = {s:{r:0,c:0},e:{r:0,c:0}};
  26563. var merges = [], midx = 0;
  26564. var rowinfo = [];
  26565. var _R = 0, R = 0, _C, C, RS, CS;
  26566. for(; _R < rows.length && R < sheetRows; ++_R) {
  26567. var row = rows[_R];
  26568. if (is_dom_element_hidden(row)) {
  26569. if (opts.display) continue;
  26570. rowinfo[R] = {hidden: true};
  26571. }
  26572. var elts = (row.children);
  26573. for(_C = C = 0; _C < elts.length; ++_C) {
  26574. var elt = elts[_C];
  26575. if (opts.display && is_dom_element_hidden(elt)) continue;
  26576. var v = htmldecode(elt.innerHTML);
  26577. for(midx = 0; midx < merges.length; ++midx) {
  26578. var m = merges[midx];
  26579. if(m.s.c == C && m.s.r <= R && R <= m.e.r) { C = m.e.c+1; midx = -1; }
  26580. }
  26581. /* TODO: figure out how to extract nonstandard mso- style */
  26582. CS = +elt.getAttribute("colspan") || 1;
  26583. if((RS = +elt.getAttribute("rowspan"))>0 || CS>1) merges.push({s:{r:R,c:C},e:{r:R + (RS||1) - 1, c:C + CS - 1}});
  26584. var o = {t:'s', v:v};
  26585. var _t = elt.getAttribute("t") || "";
  26586. if(v != null) {
  26587. if(v.length == 0) o.t = _t || 'z';
  26588. else if(opts.raw || v.trim().length == 0 || _t == "s"){}
  26589. else if(v === 'TRUE') o = {t:'b', v:true};
  26590. else if(v === 'FALSE') o = {t:'b', v:false};
  26591. else if(!isNaN(fuzzynum(v))) o = {t:'n', v:fuzzynum(v)};
  26592. else if(!isNaN(fuzzydate(v).getDate())) {
  26593. o = ({t:'d', v:parseDate(v)});
  26594. if(!opts.cellDates) o = ({t:'n', v:datenum(o.v)});
  26595. o.z = opts.dateNF || SSF._table[14];
  26596. }
  26597. }
  26598. if(opts.dense) { if(!ws[R]) ws[R] = []; ws[R][C] = o; }
  26599. else ws[encode_cell({c:C, r:R})] = o;
  26600. if(range.e.c < C) range.e.c = C;
  26601. C += CS;
  26602. }
  26603. ++R;
  26604. }
  26605. if(merges.length) ws['!merges'] = merges;
  26606. if(rowinfo.length) ws['!rows'] = rowinfo;
  26607. range.e.r = R - 1;
  26608. ws['!ref'] = encode_range(range);
  26609. if(R >= sheetRows) ws['!fullref'] = encode_range((range.e.r = rows.length-_R+R-1,range)); // We can count the real number of rows to parse but we don't to improve the performance
  26610. return ws;
  26611. }
  26612. function table_to_book(table, opts) {
  26613. return sheet_to_workbook(parse_dom_table(table, opts), opts);
  26614. }
  26615. function is_dom_element_hidden(element) {
  26616. var display = '';
  26617. var get_computed_style = get_get_computed_style_function(element);
  26618. if(get_computed_style) display = get_computed_style(element).getPropertyValue('display');
  26619. if(!display) display = element.style.display; // Fallback for cases when getComputedStyle is not available (e.g. an old browser or some Node.js environments) or doesn't work (e.g. if the element is not inserted to a document)
  26620. return display === 'none';
  26621. }
  26622. /* global getComputedStyle */
  26623. function get_get_computed_style_function(element) {
  26624. // The proper getComputedStyle implementation is the one defined in the element window
  26625. if(element.ownerDocument.defaultView && typeof element.ownerDocument.defaultView.getComputedStyle === 'function') return element.ownerDocument.defaultView.getComputedStyle;
  26626. // If it is not available, try to get one from the global namespace
  26627. if(typeof getComputedStyle === 'function') return getComputedStyle;
  26628. return null;
  26629. }
  26630. /* OpenDocument */
  26631. var parse_content_xml = (function() {
  26632. var parse_text_p = function(text) {
  26633. /* 6.1.2 White Space Characters */
  26634. var fixed = text
  26635. .replace(/[\t\r\n]/g, " ").trim().replace(/ +/g, " ")
  26636. .replace(/<text:s\/>/g," ")
  26637. .replace(/<text:s text:c="(\d+)"\/>/g, function($$,$1) { return Array(parseInt($1,10)+1).join(" "); })
  26638. .replace(/<text:tab[^>]*\/>/g,"\t")
  26639. .replace(/<text:line-break\/>/g,"\n");
  26640. var v = unescapexml(fixed.replace(/<[^>]*>/g,""));
  26641. return [v];
  26642. };
  26643. var number_formats = {
  26644. /* ods name: [short ssf fmt, long ssf fmt] */
  26645. day: ["d", "dd"],
  26646. month: ["m", "mm"],
  26647. year: ["y", "yy"],
  26648. hours: ["h", "hh"],
  26649. minutes: ["m", "mm"],
  26650. seconds: ["s", "ss"],
  26651. "am-pm": ["A/P", "AM/PM"],
  26652. "day-of-week": ["ddd", "dddd"],
  26653. era: ["e", "ee"],
  26654. /* there is no native representation of LO "Q" format */
  26655. quarter: ["\\Qm", "m\\\"th quarter\""]
  26656. };
  26657. return function pcx(d, _opts) {
  26658. var opts = _opts || {};
  26659. if(DENSE != null && opts.dense == null) opts.dense = DENSE;
  26660. var str = xlml_normalize(d);
  26661. var state = [], tmp;
  26662. var tag;
  26663. var NFtag = {name:""}, NF = "", pidx = 0;
  26664. var sheetag;
  26665. var rowtag;
  26666. var Sheets = {}, SheetNames = [];
  26667. var ws = opts.dense ? ([]) : ({});
  26668. var Rn, q;
  26669. var ctag = ({value:""});
  26670. var textp = "", textpidx = 0, textptag;
  26671. var textR = [];
  26672. var R = -1, C = -1, range = {s: {r:1000000,c:10000000}, e: {r:0, c:0}};
  26673. var row_ol = 0;
  26674. var number_format_map = {};
  26675. var merges = [], mrange = {}, mR = 0, mC = 0;
  26676. var rowinfo = [], rowpeat = 1, colpeat = 1;
  26677. var arrayf = [];
  26678. var WB = {Names:[]};
  26679. var atag = ({});
  26680. var _Ref = ["", ""];
  26681. var comments = [], comment = ({});
  26682. var creator = "", creatoridx = 0;
  26683. var isstub = false, intable = false;
  26684. var i = 0;
  26685. xlmlregex.lastIndex = 0;
  26686. str = str.replace(/<!--([\s\S]*?)-->/mg,"").replace(/<!DOCTYPE[^\[]*\[[^\]]*\]>/gm,"");
  26687. while((Rn = xlmlregex.exec(str))) switch((Rn[3]=Rn[3].replace(/_.*$/,""))) {
  26688. case 'table': case '工作表': // 9.1.2 <table:table>
  26689. if(Rn[1]==='/') {
  26690. if(range.e.c >= range.s.c && range.e.r >= range.s.r) ws['!ref'] = encode_range(range);
  26691. if(opts.sheetRows > 0 && opts.sheetRows <= range.e.r) {
  26692. ws['!fullref'] = ws['!ref'];
  26693. range.e.r = opts.sheetRows - 1;
  26694. ws['!ref'] = encode_range(range);
  26695. }
  26696. if(merges.length) ws['!merges'] = merges;
  26697. if(rowinfo.length) ws["!rows"] = rowinfo;
  26698. sheetag.name = sheetag['名称'] || sheetag.name;
  26699. if(typeof JSON !== 'undefined') JSON.stringify(sheetag);
  26700. SheetNames.push(sheetag.name);
  26701. Sheets[sheetag.name] = ws;
  26702. intable = false;
  26703. }
  26704. else if(Rn[0].charAt(Rn[0].length-2) !== '/') {
  26705. sheetag = parsexmltag(Rn[0], false);
  26706. R = C = -1;
  26707. range.s.r = range.s.c = 10000000; range.e.r = range.e.c = 0;
  26708. ws = opts.dense ? ([]) : ({}); merges = [];
  26709. rowinfo = [];
  26710. intable = true;
  26711. }
  26712. break;
  26713. case 'table-row-group': // 9.1.9 <table:table-row-group>
  26714. if(Rn[1] === "/") --row_ol; else ++row_ol;
  26715. break;
  26716. case 'table-row': case '行': // 9.1.3 <table:table-row>
  26717. if(Rn[1] === '/') { R+=rowpeat; rowpeat = 1; break; }
  26718. rowtag = parsexmltag(Rn[0], false);
  26719. if(rowtag['行号']) R = rowtag['行号'] - 1; else if(R == -1) R = 0;
  26720. rowpeat = +rowtag['number-rows-repeated'] || 1;
  26721. /* TODO: remove magic */
  26722. if(rowpeat < 10) for(i = 0; i < rowpeat; ++i) if(row_ol > 0) rowinfo[R + i] = {level: row_ol};
  26723. C = -1; break;
  26724. case 'covered-table-cell': // 9.1.5 <table:covered-table-cell>
  26725. if(Rn[1] !== '/') ++C;
  26726. if(opts.sheetStubs) {
  26727. if(opts.dense) { if(!ws[R]) ws[R] = []; ws[R][C] = {t:'z'}; }
  26728. else ws[encode_cell({r:R,c:C})] = {t:'z'};
  26729. }
  26730. textp = ""; textR = [];
  26731. break; /* stub */
  26732. case 'table-cell': case '数据':
  26733. if(Rn[0].charAt(Rn[0].length-2) === '/') {
  26734. ++C;
  26735. ctag = parsexmltag(Rn[0], false);
  26736. colpeat = parseInt(ctag['number-columns-repeated']||"1", 10);
  26737. q = ({t:'z', v:null});
  26738. if(ctag.formula && opts.cellFormula != false) q.f = ods_to_csf_formula(unescapexml(ctag.formula));
  26739. if((ctag['数据类型'] || ctag['value-type']) == "string") {
  26740. q.t = "s"; q.v = unescapexml(ctag['string-value'] || "");
  26741. if(opts.dense) {
  26742. if(!ws[R]) ws[R] = [];
  26743. ws[R][C] = q;
  26744. } else {
  26745. ws[encode_cell({r:R,c:C})] = q;
  26746. }
  26747. }
  26748. C+= colpeat-1;
  26749. } else if(Rn[1]!=='/') {
  26750. ++C;
  26751. colpeat = 1;
  26752. var rptR = rowpeat ? R + rowpeat - 1 : R;
  26753. if(C > range.e.c) range.e.c = C;
  26754. if(C < range.s.c) range.s.c = C;
  26755. if(R < range.s.r) range.s.r = R;
  26756. if(rptR > range.e.r) range.e.r = rptR;
  26757. ctag = parsexmltag(Rn[0], false);
  26758. comments = []; comment = ({});
  26759. q = ({t:ctag['数据类型'] || ctag['value-type'], v:null});
  26760. if(opts.cellFormula) {
  26761. if(ctag.formula) ctag.formula = unescapexml(ctag.formula);
  26762. if(ctag['number-matrix-columns-spanned'] && ctag['number-matrix-rows-spanned']) {
  26763. mR = parseInt(ctag['number-matrix-rows-spanned'],10) || 0;
  26764. mC = parseInt(ctag['number-matrix-columns-spanned'],10) || 0;
  26765. mrange = {s: {r:R,c:C}, e:{r:R + mR-1,c:C + mC-1}};
  26766. q.F = encode_range(mrange);
  26767. arrayf.push([mrange, q.F]);
  26768. }
  26769. if(ctag.formula) q.f = ods_to_csf_formula(ctag.formula);
  26770. else for(i = 0; i < arrayf.length; ++i)
  26771. if(R >= arrayf[i][0].s.r && R <= arrayf[i][0].e.r)
  26772. if(C >= arrayf[i][0].s.c && C <= arrayf[i][0].e.c)
  26773. q.F = arrayf[i][1];
  26774. }
  26775. if(ctag['number-columns-spanned'] || ctag['number-rows-spanned']) {
  26776. mR = parseInt(ctag['number-rows-spanned'],10) || 0;
  26777. mC = parseInt(ctag['number-columns-spanned'],10) || 0;
  26778. mrange = {s: {r:R,c:C}, e:{r:R + mR-1,c:C + mC-1}};
  26779. merges.push(mrange);
  26780. }
  26781. /* 19.675.2 table:number-columns-repeated */
  26782. if(ctag['number-columns-repeated']) colpeat = parseInt(ctag['number-columns-repeated'], 10);
  26783. /* 19.385 office:value-type */
  26784. switch(q.t) {
  26785. case 'boolean': q.t = 'b'; q.v = parsexmlbool(ctag['boolean-value']); break;
  26786. case 'float': q.t = 'n'; q.v = parseFloat(ctag.value); break;
  26787. case 'percentage': q.t = 'n'; q.v = parseFloat(ctag.value); break;
  26788. case 'currency': q.t = 'n'; q.v = parseFloat(ctag.value); break;
  26789. case 'date': q.t = 'd'; q.v = parseDate(ctag['date-value']);
  26790. if(!opts.cellDates) { q.t = 'n'; q.v = datenum(q.v); }
  26791. q.z = 'm/d/yy'; break;
  26792. case 'time': q.t = 'n'; q.v = parse_isodur(ctag['time-value'])/86400; break;
  26793. case 'number': q.t = 'n'; q.v = parseFloat(ctag['数据数值']); break;
  26794. default:
  26795. if(q.t === 'string' || q.t === 'text' || !q.t) {
  26796. q.t = 's';
  26797. if(ctag['string-value'] != null) { textp = unescapexml(ctag['string-value']); textR = []; }
  26798. } else throw new Error('Unsupported value type ' + q.t);
  26799. }
  26800. } else {
  26801. isstub = false;
  26802. if(q.t === 's') {
  26803. q.v = textp || '';
  26804. if(textR.length) q.R = textR;
  26805. isstub = textpidx == 0;
  26806. }
  26807. if(atag.Target) q.l = atag;
  26808. if(comments.length > 0) { q.c = comments; comments = []; }
  26809. if(textp && opts.cellText !== false) q.w = textp;
  26810. if(!isstub || opts.sheetStubs) {
  26811. if(!(opts.sheetRows && opts.sheetRows <= R)) {
  26812. for(var rpt = 0; rpt < rowpeat; ++rpt) {
  26813. colpeat = parseInt(ctag['number-columns-repeated']||"1", 10);
  26814. if(opts.dense) {
  26815. if(!ws[R + rpt]) ws[R + rpt] = [];
  26816. ws[R + rpt][C] = rpt == 0 ? q : dup(q);
  26817. while(--colpeat > 0) ws[R + rpt][C + colpeat] = dup(q);
  26818. } else {
  26819. ws[encode_cell({r:R + rpt,c:C})] = q;
  26820. while(--colpeat > 0) ws[encode_cell({r:R + rpt,c:C + colpeat})] = dup(q);
  26821. }
  26822. if(range.e.c <= C) range.e.c = C;
  26823. }
  26824. }
  26825. }
  26826. colpeat = parseInt(ctag['number-columns-repeated']||"1", 10);
  26827. C += colpeat-1; colpeat = 0;
  26828. q = {};
  26829. textp = ""; textR = [];
  26830. }
  26831. atag = ({});
  26832. break; // 9.1.4 <table:table-cell>
  26833. /* pure state */
  26834. case 'document': // TODO: <office:document> is the root for FODS
  26835. case 'document-content': case '电子表格文档': // 3.1.3.2 <office:document-content>
  26836. case 'spreadsheet': case '主体': // 3.7 <office:spreadsheet>
  26837. case 'scripts': // 3.12 <office:scripts>
  26838. case 'styles': // TODO <office:styles>
  26839. case 'font-face-decls': // 3.14 <office:font-face-decls>
  26840. if(Rn[1]==='/'){if((tmp=state.pop())[0]!==Rn[3]) throw "Bad state: "+tmp;}
  26841. else if(Rn[0].charAt(Rn[0].length-2) !== '/') state.push([Rn[3], true]);
  26842. break;
  26843. case 'annotation': // 14.1 <office:annotation>
  26844. if(Rn[1]==='/'){
  26845. if((tmp=state.pop())[0]!==Rn[3]) throw "Bad state: "+tmp;
  26846. comment.t = textp;
  26847. if(textR.length) comment.R = textR;
  26848. comment.a = creator;
  26849. comments.push(comment);
  26850. }
  26851. else if(Rn[0].charAt(Rn[0].length-2) !== '/') {state.push([Rn[3], false]);}
  26852. creator = ""; creatoridx = 0;
  26853. textp = ""; textpidx = 0; textR = [];
  26854. break;
  26855. case 'creator': // 4.3.2.7 <dc:creator>
  26856. if(Rn[1]==='/') { creator = str.slice(creatoridx,Rn.index); }
  26857. else creatoridx = Rn.index + Rn[0].length;
  26858. break;
  26859. /* ignore state */
  26860. case 'meta': case '元数据': // TODO: <office:meta> <uof:元数据> FODS/UOF
  26861. case 'settings': // TODO: <office:settings>
  26862. case 'config-item-set': // TODO: <office:config-item-set>
  26863. case 'config-item-map-indexed': // TODO: <office:config-item-map-indexed>
  26864. case 'config-item-map-entry': // TODO: <office:config-item-map-entry>
  26865. case 'config-item-map-named': // TODO: <office:config-item-map-entry>
  26866. case 'shapes': // 9.2.8 <table:shapes>
  26867. case 'frame': // 10.4.2 <draw:frame>
  26868. case 'text-box': // 10.4.3 <draw:text-box>
  26869. case 'image': // 10.4.4 <draw:image>
  26870. case 'data-pilot-tables': // 9.6.2 <table:data-pilot-tables>
  26871. case 'list-style': // 16.30 <text:list-style>
  26872. case 'form': // 13.13 <form:form>
  26873. case 'dde-links': // 9.8 <table:dde-links>
  26874. case 'event-listeners': // TODO
  26875. case 'chart': // TODO
  26876. if(Rn[1]==='/'){if((tmp=state.pop())[0]!==Rn[3]) throw "Bad state: "+tmp;}
  26877. else if(Rn[0].charAt(Rn[0].length-2) !== '/') state.push([Rn[3], false]);
  26878. textp = ""; textpidx = 0; textR = [];
  26879. break;
  26880. case 'scientific-number': // TODO: <number:scientific-number>
  26881. break;
  26882. case 'currency-symbol': // TODO: <number:currency-symbol>
  26883. break;
  26884. case 'currency-style': // TODO: <number:currency-style>
  26885. break;
  26886. case 'number-style': // 16.27.2 <number:number-style>
  26887. case 'percentage-style': // 16.27.9 <number:percentage-style>
  26888. case 'date-style': // 16.27.10 <number:date-style>
  26889. case 'time-style': // 16.27.18 <number:time-style>
  26890. if(Rn[1]==='/'){
  26891. number_format_map[NFtag.name] = NF;
  26892. if((tmp=state.pop())[0]!==Rn[3]) throw "Bad state: "+tmp;
  26893. } else if(Rn[0].charAt(Rn[0].length-2) !== '/') {
  26894. NF = "";
  26895. NFtag = parsexmltag(Rn[0], false);
  26896. state.push([Rn[3], true]);
  26897. } break;
  26898. case 'script': break; // 3.13 <office:script>
  26899. case 'libraries': break; // TODO: <ooo:libraries>
  26900. case 'automatic-styles': break; // 3.15.3 <office:automatic-styles>
  26901. case 'master-styles': break; // TODO: <office:master-styles>
  26902. case 'default-style': // TODO: <style:default-style>
  26903. case 'page-layout': break; // TODO: <style:page-layout>
  26904. case 'style': // 16.2 <style:style>
  26905. break;
  26906. case 'map': break; // 16.3 <style:map>
  26907. case 'font-face': break; // 16.21 <style:font-face>
  26908. case 'paragraph-properties': break; // 17.6 <style:paragraph-properties>
  26909. case 'table-properties': break; // 17.15 <style:table-properties>
  26910. case 'table-column-properties': break; // 17.16 <style:table-column-properties>
  26911. case 'table-row-properties': break; // 17.17 <style:table-row-properties>
  26912. case 'table-cell-properties': break; // 17.18 <style:table-cell-properties>
  26913. case 'number': // 16.27.3 <number:number>
  26914. switch(state[state.length-1][0]) {
  26915. case 'time-style':
  26916. case 'date-style':
  26917. tag = parsexmltag(Rn[0], false);
  26918. NF += number_formats[Rn[3]][tag.style==='long'?1:0]; break;
  26919. } break;
  26920. case 'fraction': break; // TODO 16.27.6 <number:fraction>
  26921. case 'day': // 16.27.11 <number:day>
  26922. case 'month': // 16.27.12 <number:month>
  26923. case 'year': // 16.27.13 <number:year>
  26924. case 'era': // 16.27.14 <number:era>
  26925. case 'day-of-week': // 16.27.15 <number:day-of-week>
  26926. case 'week-of-year': // 16.27.16 <number:week-of-year>
  26927. case 'quarter': // 16.27.17 <number:quarter>
  26928. case 'hours': // 16.27.19 <number:hours>
  26929. case 'minutes': // 16.27.20 <number:minutes>
  26930. case 'seconds': // 16.27.21 <number:seconds>
  26931. case 'am-pm': // 16.27.22 <number:am-pm>
  26932. switch(state[state.length-1][0]) {
  26933. case 'time-style':
  26934. case 'date-style':
  26935. tag = parsexmltag(Rn[0], false);
  26936. NF += number_formats[Rn[3]][tag.style==='long'?1:0]; break;
  26937. } break;
  26938. case 'boolean-style': break; // 16.27.23 <number:boolean-style>
  26939. case 'boolean': break; // 16.27.24 <number:boolean>
  26940. case 'text-style': break; // 16.27.25 <number:text-style>
  26941. case 'text': // 16.27.26 <number:text>
  26942. if(Rn[0].slice(-2) === "/>") break;
  26943. else if(Rn[1]==="/") switch(state[state.length-1][0]) {
  26944. case 'number-style':
  26945. case 'date-style':
  26946. case 'time-style':
  26947. NF += str.slice(pidx, Rn.index);
  26948. break;
  26949. }
  26950. else pidx = Rn.index + Rn[0].length;
  26951. break;
  26952. case 'named-range': // 9.4.12 <table:named-range>
  26953. tag = parsexmltag(Rn[0], false);
  26954. _Ref = ods_to_csf_3D(tag['cell-range-address']);
  26955. var nrange = ({Name:tag.name, Ref:_Ref[0] + '!' + _Ref[1]});
  26956. if(intable) nrange.Sheet = SheetNames.length;
  26957. WB.Names.push(nrange);
  26958. break;
  26959. case 'text-content': break; // 16.27.27 <number:text-content>
  26960. case 'text-properties': break; // 16.27.27 <style:text-properties>
  26961. case 'embedded-text': break; // 16.27.4 <number:embedded-text>
  26962. case 'body': case '电子表格': break; // 3.3 16.9.6 19.726.3
  26963. case 'forms': break; // 12.25.2 13.2
  26964. case 'table-column': break; // 9.1.6 <table:table-column>
  26965. case 'table-header-rows': break; // 9.1.7 <table:table-header-rows>
  26966. case 'table-rows': break; // 9.1.12 <table:table-rows>
  26967. /* TODO: outline levels */
  26968. case 'table-column-group': break; // 9.1.10 <table:table-column-group>
  26969. case 'table-header-columns': break; // 9.1.11 <table:table-header-columns>
  26970. case 'table-columns': break; // 9.1.12 <table:table-columns>
  26971. case 'null-date': break; // 9.4.2 <table:null-date> TODO: date1904
  26972. case 'graphic-properties': break; // 17.21 <style:graphic-properties>
  26973. case 'calculation-settings': break; // 9.4.1 <table:calculation-settings>
  26974. case 'named-expressions': break; // 9.4.11 <table:named-expressions>
  26975. case 'label-range': break; // 9.4.9 <table:label-range>
  26976. case 'label-ranges': break; // 9.4.10 <table:label-ranges>
  26977. case 'named-expression': break; // 9.4.13 <table:named-expression>
  26978. case 'sort': break; // 9.4.19 <table:sort>
  26979. case 'sort-by': break; // 9.4.20 <table:sort-by>
  26980. case 'sort-groups': break; // 9.4.22 <table:sort-groups>
  26981. case 'tab': break; // 6.1.4 <text:tab>
  26982. case 'line-break': break; // 6.1.5 <text:line-break>
  26983. case 'span': break; // 6.1.7 <text:span>
  26984. case 'p': case '文本串': // 5.1.3 <text:p>
  26985. if(Rn[1]==='/' && (!ctag || !ctag['string-value'])) {
  26986. var ptp = parse_text_p(str.slice(textpidx,Rn.index), textptag);
  26987. textp = (textp.length > 0 ? textp + "\n" : "") + ptp[0];
  26988. } else { textptag = parsexmltag(Rn[0], false); textpidx = Rn.index + Rn[0].length; }
  26989. break; // <text:p>
  26990. case 's': break; // <text:s>
  26991. case 'database-range': // 9.4.15 <table:database-range>
  26992. if(Rn[1]==='/') break;
  26993. try {
  26994. _Ref = ods_to_csf_3D(parsexmltag(Rn[0])['target-range-address']);
  26995. Sheets[_Ref[0]]['!autofilter'] = { ref:_Ref[1] };
  26996. } catch(e) {/* empty */}
  26997. break;
  26998. case 'date': break; // <*:date>
  26999. case 'object': break; // 10.4.6.2 <draw:object>
  27000. case 'title': case '标题': break; // <*:title> OR <uof:标题>
  27001. case 'desc': break; // <*:desc>
  27002. case 'binary-data': break; // 10.4.5 TODO: b64 blob
  27003. /* 9.2 Advanced Tables */
  27004. case 'table-source': break; // 9.2.6
  27005. case 'scenario': break; // 9.2.6
  27006. case 'iteration': break; // 9.4.3 <table:iteration>
  27007. case 'content-validations': break; // 9.4.4 <table:
  27008. case 'content-validation': break; // 9.4.5 <table:
  27009. case 'help-message': break; // 9.4.6 <table:
  27010. case 'error-message': break; // 9.4.7 <table:
  27011. case 'database-ranges': break; // 9.4.14 <table:database-ranges>
  27012. case 'filter': break; // 9.5.2 <table:filter>
  27013. case 'filter-and': break; // 9.5.3 <table:filter-and>
  27014. case 'filter-or': break; // 9.5.4 <table:filter-or>
  27015. case 'filter-condition': break; // 9.5.5 <table:filter-condition>
  27016. case 'list-level-style-bullet': break; // 16.31 <text:
  27017. case 'list-level-style-number': break; // 16.32 <text:
  27018. case 'list-level-properties': break; // 17.19 <style:
  27019. /* 7.3 Document Fields */
  27020. case 'sender-firstname': // 7.3.6.2
  27021. case 'sender-lastname': // 7.3.6.3
  27022. case 'sender-initials': // 7.3.6.4
  27023. case 'sender-title': // 7.3.6.5
  27024. case 'sender-position': // 7.3.6.6
  27025. case 'sender-email': // 7.3.6.7
  27026. case 'sender-phone-private': // 7.3.6.8
  27027. case 'sender-fax': // 7.3.6.9
  27028. case 'sender-company': // 7.3.6.10
  27029. case 'sender-phone-work': // 7.3.6.11
  27030. case 'sender-street': // 7.3.6.12
  27031. case 'sender-city': // 7.3.6.13
  27032. case 'sender-postal-code': // 7.3.6.14
  27033. case 'sender-country': // 7.3.6.15
  27034. case 'sender-state-or-province': // 7.3.6.16
  27035. case 'author-name': // 7.3.7.1
  27036. case 'author-initials': // 7.3.7.2
  27037. case 'chapter': // 7.3.8
  27038. case 'file-name': // 7.3.9
  27039. case 'template-name': // 7.3.9
  27040. case 'sheet-name': // 7.3.9
  27041. break;
  27042. case 'event-listener':
  27043. break;
  27044. /* TODO: FODS Properties */
  27045. case 'initial-creator':
  27046. case 'creation-date':
  27047. case 'print-date':
  27048. case 'generator':
  27049. case 'document-statistic':
  27050. case 'user-defined':
  27051. case 'editing-duration':
  27052. case 'editing-cycles':
  27053. break;
  27054. /* TODO: FODS Config */
  27055. case 'config-item':
  27056. break;
  27057. /* TODO: style tokens */
  27058. case 'page-number': break; // TODO <text:page-number>
  27059. case 'page-count': break; // TODO <text:page-count>
  27060. case 'time': break; // TODO <text:time>
  27061. /* 9.3 Advanced Table Cells */
  27062. case 'cell-range-source': break; // 9.3.1 <table:
  27063. case 'detective': break; // 9.3.2 <table:
  27064. case 'operation': break; // 9.3.3 <table:
  27065. case 'highlighted-range': break; // 9.3.4 <table:
  27066. /* 9.6 Data Pilot Tables <table: */
  27067. case 'data-pilot-table': // 9.6.3
  27068. case 'source-cell-range': // 9.6.5
  27069. case 'source-service': // 9.6.6
  27070. case 'data-pilot-field': // 9.6.7
  27071. case 'data-pilot-level': // 9.6.8
  27072. case 'data-pilot-subtotals': // 9.6.9
  27073. case 'data-pilot-subtotal': // 9.6.10
  27074. case 'data-pilot-members': // 9.6.11
  27075. case 'data-pilot-member': // 9.6.12
  27076. case 'data-pilot-display-info': // 9.6.13
  27077. case 'data-pilot-sort-info': // 9.6.14
  27078. case 'data-pilot-layout-info': // 9.6.15
  27079. case 'data-pilot-field-reference': // 9.6.16
  27080. case 'data-pilot-groups': // 9.6.17
  27081. case 'data-pilot-group': // 9.6.18
  27082. case 'data-pilot-group-member': // 9.6.19
  27083. break;
  27084. /* 10.3 Drawing Shapes */
  27085. case 'rect': // 10.3.2
  27086. break;
  27087. /* 14.6 DDE Connections */
  27088. case 'dde-connection-decls': // 14.6.2 <text:
  27089. case 'dde-connection-decl': // 14.6.3 <text:
  27090. case 'dde-link': // 14.6.4 <table:
  27091. case 'dde-source': // 14.6.5 <office:
  27092. break;
  27093. case 'properties': break; // 13.7 <form:properties>
  27094. case 'property': break; // 13.8 <form:property>
  27095. case 'a': // 6.1.8 hyperlink
  27096. if(Rn[1]!== '/') {
  27097. atag = parsexmltag(Rn[0], false);
  27098. if(!atag.href) break;
  27099. atag.Target = atag.href; delete atag.href;
  27100. if(atag.Target.charAt(0) == "#" && atag.Target.indexOf(".") > -1) {
  27101. _Ref = ods_to_csf_3D(atag.Target.slice(1));
  27102. atag.Target = "#" + _Ref[0] + "!" + _Ref[1];
  27103. }
  27104. }
  27105. break;
  27106. /* non-standard */
  27107. case 'table-protection': break;
  27108. case 'data-pilot-grand-total': break; // <table:
  27109. case 'office-document-common-attrs': break; // bare
  27110. default: switch(Rn[2]) {
  27111. case 'dc:': // TODO: properties
  27112. case 'calcext:': // ignore undocumented extensions
  27113. case 'loext:': // ignore undocumented extensions
  27114. case 'ooo:': // ignore undocumented extensions
  27115. case 'chartooo:': // ignore undocumented extensions
  27116. case 'draw:': // TODO: drawing
  27117. case 'style:': // TODO: styles
  27118. case 'chart:': // TODO: charts
  27119. case 'form:': // TODO: forms
  27120. case 'uof:': // TODO: uof
  27121. case '表:': // TODO: uof
  27122. case '字:': // TODO: uof
  27123. break;
  27124. default: if(opts.WTF) throw new Error(Rn);
  27125. }
  27126. }
  27127. var out = ({
  27128. Sheets: Sheets,
  27129. SheetNames: SheetNames,
  27130. Workbook: WB
  27131. });
  27132. if(opts.bookSheets) delete out.Sheets;
  27133. return out;
  27134. };
  27135. })();
  27136. function parse_ods(zip, opts) {
  27137. opts = opts || ({});
  27138. var ods = !!safegetzipfile(zip, 'objectdata');
  27139. if(ods) parse_manifest(getzipdata(zip, 'META-INF/manifest.xml'), opts);
  27140. var content = getzipstr(zip, 'content.xml');
  27141. if(!content) throw new Error("Missing content.xml in " + (ods ? "ODS" : "UOF")+ " file");
  27142. var wb = parse_content_xml(ods ? content : utf8read(content), opts);
  27143. if(safegetzipfile(zip, 'meta.xml')) wb.Props = parse_core_props(getzipdata(zip, 'meta.xml'));
  27144. return wb;
  27145. }
  27146. function parse_fods(data, opts) {
  27147. return parse_content_xml(data, opts);
  27148. }
  27149. /* OpenDocument */
  27150. var write_styles_ods = (function() {
  27151. var payload = '<office:document-styles ' + wxt_helper({
  27152. 'xmlns:office': "urn:oasis:names:tc:opendocument:xmlns:office:1.0",
  27153. 'xmlns:table': "urn:oasis:names:tc:opendocument:xmlns:table:1.0",
  27154. 'xmlns:style': "urn:oasis:names:tc:opendocument:xmlns:style:1.0",
  27155. 'xmlns:text': "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
  27156. 'xmlns:draw': "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
  27157. 'xmlns:fo': "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0",
  27158. 'xmlns:xlink': "http://www.w3.org/1999/xlink",
  27159. 'xmlns:dc': "http://purl.org/dc/elements/1.1/",
  27160. 'xmlns:number': "urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0",
  27161. 'xmlns:svg': "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0",
  27162. 'xmlns:of': "urn:oasis:names:tc:opendocument:xmlns:of:1.2",
  27163. 'office:version': "1.2"
  27164. }) + '></office:document-styles>';
  27165. return function wso() {
  27166. return XML_HEADER + payload;
  27167. };
  27168. })();
  27169. var write_content_ods = (function() {
  27170. /* 6.1.2 White Space Characters */
  27171. var write_text_p = function(text) {
  27172. return escapexml(text)
  27173. .replace(/ +/g, function($$){return '<text:s text:c="'+$$.length+'"/>';})
  27174. .replace(/\t/g, "<text:tab/>")
  27175. .replace(/\n/g, "<text:line-break/>")
  27176. .replace(/^ /, "<text:s/>").replace(/ $/, "<text:s/>");
  27177. };
  27178. var null_cell_xml = ' <table:table-cell />\n';
  27179. var covered_cell_xml = ' <table:covered-table-cell/>\n';
  27180. var write_ws = function(ws, wb, i) {
  27181. /* Section 9 Tables */
  27182. var o = [];
  27183. o.push(' <table:table table:name="' + escapexml(wb.SheetNames[i]) + '">\n');
  27184. var R=0,C=0, range = decode_range(ws['!ref']);
  27185. var marr = ws['!merges'] || [], mi = 0;
  27186. var dense = Array.isArray(ws);
  27187. for(R = 0; R < range.s.r; ++R) o.push(' <table:table-row></table:table-row>\n');
  27188. for(; R <= range.e.r; ++R) {
  27189. o.push(' <table:table-row>\n');
  27190. for(C=0; C < range.s.c; ++C) o.push(null_cell_xml);
  27191. for(; C <= range.e.c; ++C) {
  27192. var skip = false, ct = {}, textp = "";
  27193. for(mi = 0; mi != marr.length; ++mi) {
  27194. if(marr[mi].s.c > C) continue;
  27195. if(marr[mi].s.r > R) continue;
  27196. if(marr[mi].e.c < C) continue;
  27197. if(marr[mi].e.r < R) continue;
  27198. if(marr[mi].s.c != C || marr[mi].s.r != R) skip = true;
  27199. ct['table:number-columns-spanned'] = (marr[mi].e.c - marr[mi].s.c + 1);
  27200. ct['table:number-rows-spanned'] = (marr[mi].e.r - marr[mi].s.r + 1);
  27201. break;
  27202. }
  27203. if(skip) { o.push(covered_cell_xml); continue; }
  27204. var ref = encode_cell({r:R, c:C}), cell = dense ? (ws[R]||[])[C]: ws[ref];
  27205. if(cell && cell.f) {
  27206. ct['table:formula'] = escapexml(csf_to_ods_formula(cell.f));
  27207. if(cell.F) {
  27208. if(cell.F.slice(0, ref.length) == ref) {
  27209. var _Fref = decode_range(cell.F);
  27210. ct['table:number-matrix-columns-spanned'] = (_Fref.e.c - _Fref.s.c + 1);
  27211. ct['table:number-matrix-rows-spanned'] = (_Fref.e.r - _Fref.s.r + 1);
  27212. }
  27213. }
  27214. }
  27215. if(!cell) { o.push(null_cell_xml); continue; }
  27216. switch(cell.t) {
  27217. case 'b':
  27218. textp = (cell.v ? 'TRUE' : 'FALSE');
  27219. ct['office:value-type'] = "boolean";
  27220. ct['office:boolean-value'] = (cell.v ? 'true' : 'false');
  27221. break;
  27222. case 'n':
  27223. textp = (cell.w||String(cell.v||0));
  27224. ct['office:value-type'] = "float";
  27225. ct['office:value'] = (cell.v||0);
  27226. break;
  27227. case 's': case 'str':
  27228. textp = cell.v;
  27229. ct['office:value-type'] = "string";
  27230. break;
  27231. case 'd':
  27232. textp = (cell.w||(parseDate(cell.v).toISOString()));
  27233. ct['office:value-type'] = "date";
  27234. ct['office:date-value'] = (parseDate(cell.v).toISOString());
  27235. ct['table:style-name'] = "ce1";
  27236. break;
  27237. //case 'e':
  27238. default: o.push(null_cell_xml); continue;
  27239. }
  27240. var text_p = write_text_p(textp);
  27241. if(cell.l && cell.l.Target) {
  27242. var _tgt = cell.l.Target; _tgt = _tgt.charAt(0) == "#" ? "#" + csf_to_ods_3D(_tgt.slice(1)) : _tgt;
  27243. text_p = writextag('text:a', text_p, {'xlink:href': _tgt});
  27244. }
  27245. o.push(' ' + writextag('table:table-cell', writextag('text:p', text_p, {}), ct) + '\n');
  27246. }
  27247. o.push(' </table:table-row>\n');
  27248. }
  27249. o.push(' </table:table>\n');
  27250. return o.join("");
  27251. };
  27252. var write_automatic_styles_ods = function(o) {
  27253. o.push(' <office:automatic-styles>\n');
  27254. o.push(' <number:date-style style:name="N37" number:automatic-order="true">\n');
  27255. o.push(' <number:month number:style="long"/>\n');
  27256. o.push(' <number:text>/</number:text>\n');
  27257. o.push(' <number:day number:style="long"/>\n');
  27258. o.push(' <number:text>/</number:text>\n');
  27259. o.push(' <number:year/>\n');
  27260. o.push(' </number:date-style>\n');
  27261. o.push(' <style:style style:name="ce1" style:family="table-cell" style:parent-style-name="Default" style:data-style-name="N37"/>\n');
  27262. o.push(' </office:automatic-styles>\n');
  27263. };
  27264. return function wcx(wb, opts) {
  27265. var o = [XML_HEADER];
  27266. /* 3.1.3.2 */
  27267. var attr = wxt_helper({
  27268. 'xmlns:office': "urn:oasis:names:tc:opendocument:xmlns:office:1.0",
  27269. 'xmlns:table': "urn:oasis:names:tc:opendocument:xmlns:table:1.0",
  27270. 'xmlns:style': "urn:oasis:names:tc:opendocument:xmlns:style:1.0",
  27271. 'xmlns:text': "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
  27272. 'xmlns:draw': "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
  27273. 'xmlns:fo': "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0",
  27274. 'xmlns:xlink': "http://www.w3.org/1999/xlink",
  27275. 'xmlns:dc': "http://purl.org/dc/elements/1.1/",
  27276. 'xmlns:meta': "urn:oasis:names:tc:opendocument:xmlns:meta:1.0",
  27277. 'xmlns:number': "urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0",
  27278. 'xmlns:presentation': "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0",
  27279. 'xmlns:svg': "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0",
  27280. 'xmlns:chart': "urn:oasis:names:tc:opendocument:xmlns:chart:1.0",
  27281. 'xmlns:dr3d': "urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0",
  27282. 'xmlns:math': "http://www.w3.org/1998/Math/MathML",
  27283. 'xmlns:form': "urn:oasis:names:tc:opendocument:xmlns:form:1.0",
  27284. 'xmlns:script': "urn:oasis:names:tc:opendocument:xmlns:script:1.0",
  27285. 'xmlns:ooo': "http://openoffice.org/2004/office",
  27286. 'xmlns:ooow': "http://openoffice.org/2004/writer",
  27287. 'xmlns:oooc': "http://openoffice.org/2004/calc",
  27288. 'xmlns:dom': "http://www.w3.org/2001/xml-events",
  27289. 'xmlns:xforms': "http://www.w3.org/2002/xforms",
  27290. 'xmlns:xsd': "http://www.w3.org/2001/XMLSchema",
  27291. 'xmlns:xsi': "http://www.w3.org/2001/XMLSchema-instance",
  27292. 'xmlns:sheet': "urn:oasis:names:tc:opendocument:sh33tjs:1.0",
  27293. 'xmlns:rpt': "http://openoffice.org/2005/report",
  27294. 'xmlns:of': "urn:oasis:names:tc:opendocument:xmlns:of:1.2",
  27295. 'xmlns:xhtml': "http://www.w3.org/1999/xhtml",
  27296. 'xmlns:grddl': "http://www.w3.org/2003/g/data-view#",
  27297. 'xmlns:tableooo': "http://openoffice.org/2009/table",
  27298. 'xmlns:drawooo': "http://openoffice.org/2010/draw",
  27299. 'xmlns:calcext': "urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0",
  27300. 'xmlns:loext': "urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0",
  27301. 'xmlns:field': "urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0",
  27302. 'xmlns:formx': "urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0",
  27303. 'xmlns:css3t': "http://www.w3.org/TR/css3-text/",
  27304. 'office:version': "1.2"
  27305. });
  27306. var fods = wxt_helper({
  27307. 'xmlns:config': "urn:oasis:names:tc:opendocument:xmlns:config:1.0",
  27308. 'office:mimetype': "application/vnd.oasis.opendocument.spreadsheet"
  27309. });
  27310. if(opts.bookType == "fods") o.push('<office:document' + attr + fods + '>\n');
  27311. else o.push('<office:document-content' + attr + '>\n');
  27312. write_automatic_styles_ods(o);
  27313. o.push(' <office:body>\n');
  27314. o.push(' <office:spreadsheet>\n');
  27315. for(var i = 0; i != wb.SheetNames.length; ++i) o.push(write_ws(wb.Sheets[wb.SheetNames[i]], wb, i, opts));
  27316. o.push(' </office:spreadsheet>\n');
  27317. o.push(' </office:body>\n');
  27318. if(opts.bookType == "fods") o.push('</office:document>');
  27319. else o.push('</office:document-content>');
  27320. return o.join("");
  27321. };
  27322. })();
  27323. function write_ods(wb, opts) {
  27324. if(opts.bookType == "fods") return write_content_ods(wb, opts);
  27325. var zip = new jszip();
  27326. var f = "";
  27327. var manifest = [];
  27328. var rdf = [];
  27329. /* Part 3 Section 3.3 MIME Media Type */
  27330. f = "mimetype";
  27331. zip.file(f, "application/vnd.oasis.opendocument.spreadsheet");
  27332. /* Part 1 Section 2.2 Documents */
  27333. f = "content.xml";
  27334. zip.file(f, write_content_ods(wb, opts));
  27335. manifest.push([f, "text/xml"]);
  27336. rdf.push([f, "ContentFile"]);
  27337. /* TODO: these are hard-coded styles to satiate excel */
  27338. f = "styles.xml";
  27339. zip.file(f, write_styles_ods(wb, opts));
  27340. manifest.push([f, "text/xml"]);
  27341. rdf.push([f, "StylesFile"]);
  27342. /* TODO: this is hard-coded to satiate excel */
  27343. f = "meta.xml";
  27344. zip.file(f, write_meta_ods());
  27345. manifest.push([f, "text/xml"]);
  27346. rdf.push([f, "MetadataFile"]);
  27347. /* Part 3 Section 6 Metadata Manifest File */
  27348. f = "manifest.rdf";
  27349. zip.file(f, write_rdf(rdf/*, opts*/));
  27350. manifest.push([f, "application/rdf+xml"]);
  27351. /* Part 3 Section 4 Manifest File */
  27352. f = "META-INF/manifest.xml";
  27353. zip.file(f, write_manifest(manifest/*, opts*/));
  27354. return zip;
  27355. }
  27356. function write_sheet_index(wb, sheet) {
  27357. if(!sheet) return 0;
  27358. var idx = wb.SheetNames.indexOf(sheet);
  27359. if(idx == -1) throw new Error("Sheet not found: " + sheet);
  27360. return idx;
  27361. }
  27362. function write_obj_str(factory) {
  27363. return function write_str(wb, o) {
  27364. var idx = write_sheet_index(wb, o.sheet);
  27365. return factory.from_sheet(wb.Sheets[wb.SheetNames[idx]], o, wb);
  27366. };
  27367. }
  27368. var write_htm_str = write_obj_str(HTML_);
  27369. var write_csv_str = write_obj_str({from_sheet:sheet_to_csv});
  27370. var write_slk_str = write_obj_str(SYLK);
  27371. var write_dif_str = write_obj_str(DIF);
  27372. var write_prn_str = write_obj_str(PRN);
  27373. var write_rtf_str = write_obj_str(RTF);
  27374. var write_txt_str = write_obj_str({from_sheet:sheet_to_txt});
  27375. var write_dbf_buf = write_obj_str(DBF);
  27376. var write_eth_str = write_obj_str(ETH);
  27377. function fix_opts_func(defaults) {
  27378. return function fix_opts(opts) {
  27379. for(var i = 0; i != defaults.length; ++i) {
  27380. var d = defaults[i];
  27381. if(opts[d[0]] === undefined) opts[d[0]] = d[1];
  27382. if(d[2] === 'n') opts[d[0]] = Number(opts[d[0]]);
  27383. }
  27384. };
  27385. }
  27386. var fix_read_opts = fix_opts_func([
  27387. ['cellNF', false], /* emit cell number format string as .z */
  27388. ['cellHTML', true], /* emit html string as .h */
  27389. ['cellFormula', true], /* emit formulae as .f */
  27390. ['cellStyles', false], /* emits style/theme as .s */
  27391. ['cellText', true], /* emit formatted text as .w */
  27392. ['cellDates', false], /* emit date cells with type `d` */
  27393. ['sheetStubs', false], /* emit empty cells */
  27394. ['sheetRows', 0, 'n'], /* read n rows (0 = read all rows) */
  27395. ['bookDeps', false], /* parse calculation chains */
  27396. ['bookSheets', false], /* only try to get sheet names (no Sheets) */
  27397. ['bookProps', false], /* only try to get properties (no Sheets) */
  27398. ['bookFiles', false], /* include raw file structure (keys, files, cfb) */
  27399. ['bookVBA', false], /* include vba raw data (vbaraw) */
  27400. ['password',''], /* password */
  27401. ['WTF', false] /* WTF mode (throws errors) */
  27402. ]);
  27403. var fix_write_opts = fix_opts_func([
  27404. ['cellDates', false], /* write date cells with type `d` */
  27405. ['bookSST', false], /* Generate Shared String Table */
  27406. ['bookType', 'xlsx'], /* Type of workbook (xlsx/m/b) */
  27407. ['compression', false], /* Use file compression */
  27408. ['WTF', false] /* WTF mode (throws errors) */
  27409. ]);
  27410. function get_sheet_type(n) {
  27411. if(RELS.WS.indexOf(n) > -1) return "sheet";
  27412. if(RELS.CS && n == RELS.CS) return "chart";
  27413. if(RELS.DS && n == RELS.DS) return "dialog";
  27414. if(RELS.MS && n == RELS.MS) return "macro";
  27415. return (n && n.length) ? n : "sheet";
  27416. }
  27417. function safe_parse_wbrels(wbrels, sheets) {
  27418. if(!wbrels) return 0;
  27419. try {
  27420. wbrels = sheets.map(function pwbr(w) { if(!w.id) w.id = w.strRelID; return [w.name, wbrels['!id'][w.id].Target, get_sheet_type(wbrels['!id'][w.id].Type)]; });
  27421. } catch(e) { return null; }
  27422. return !wbrels || wbrels.length === 0 ? null : wbrels;
  27423. }
  27424. function safe_parse_sheet(zip, path, relsPath, sheet, idx, sheetRels, sheets, stype, opts, wb, themes, styles) {
  27425. try {
  27426. sheetRels[sheet]=parse_rels(getzipstr(zip, relsPath, true), path);
  27427. var data = getzipdata(zip, path);
  27428. var _ws;
  27429. switch(stype) {
  27430. case 'sheet': _ws = parse_ws(data, path, idx, opts, sheetRels[sheet], wb, themes, styles); break;
  27431. case 'chart': _ws = parse_cs(data, path, idx, opts, sheetRels[sheet], wb, themes, styles);
  27432. if(!_ws || !_ws['!chart']) break;
  27433. var dfile = resolve_path(_ws['!chart'].Target, path);
  27434. var drelsp = get_rels_path(dfile);
  27435. var draw = parse_drawing(getzipstr(zip, dfile, true), parse_rels(getzipstr(zip, drelsp, true), dfile));
  27436. var chartp = resolve_path(draw, dfile);
  27437. var crelsp = get_rels_path(chartp);
  27438. _ws = parse_chart(getzipstr(zip, chartp, true), chartp, opts, parse_rels(getzipstr(zip, crelsp, true), chartp), wb, _ws);
  27439. break;
  27440. case 'macro': _ws = parse_ms(data, path, idx, opts, sheetRels[sheet], wb, themes, styles); break;
  27441. case 'dialog': _ws = parse_ds(data, path, idx, opts, sheetRels[sheet], wb, themes, styles); break;
  27442. }
  27443. sheets[sheet] = _ws;
  27444. } catch(e) { if(opts.WTF) throw e; }
  27445. }
  27446. function strip_front_slash(x) { return x.charAt(0) == '/' ? x.slice(1) : x; }
  27447. function parse_zip(zip, opts) {
  27448. make_ssf(SSF);
  27449. opts = opts || {};
  27450. fix_read_opts(opts);
  27451. /* OpenDocument Part 3 Section 2.2.1 OpenDocument Package */
  27452. if(safegetzipfile(zip, 'META-INF/manifest.xml')) return parse_ods(zip, opts);
  27453. /* UOC */
  27454. if(safegetzipfile(zip, 'objectdata.xml')) return parse_ods(zip, opts);
  27455. /* Numbers */
  27456. if(safegetzipfile(zip, 'Index/Document.iwa')) throw new Error('Unsupported NUMBERS file');
  27457. var entries = zipentries(zip);
  27458. var dir = parse_ct((getzipstr(zip, '[Content_Types].xml')));
  27459. var xlsb = false;
  27460. var sheets, binname;
  27461. if(dir.workbooks.length === 0) {
  27462. binname = "xl/workbook.xml";
  27463. if(getzipdata(zip,binname, true)) dir.workbooks.push(binname);
  27464. }
  27465. if(dir.workbooks.length === 0) {
  27466. binname = "xl/workbook.bin";
  27467. if(!getzipdata(zip,binname,true)) throw new Error("Could not find workbook");
  27468. dir.workbooks.push(binname);
  27469. xlsb = true;
  27470. }
  27471. if(dir.workbooks[0].slice(-3) == "bin") xlsb = true;
  27472. var themes = ({});
  27473. var styles = ({});
  27474. if(!opts.bookSheets && !opts.bookProps) {
  27475. strs = [];
  27476. if(dir.sst) try { strs=parse_sst(getzipdata(zip, strip_front_slash(dir.sst)), dir.sst, opts); } catch(e) { if(opts.WTF) throw e; }
  27477. if(opts.cellStyles && dir.themes.length) themes = parse_theme(getzipstr(zip, dir.themes[0].replace(/^\//,''), true)||"",dir.themes[0], opts);
  27478. if(dir.style) styles = parse_sty(getzipdata(zip, strip_front_slash(dir.style)), dir.style, themes, opts);
  27479. }
  27480. /*var externbooks = */dir.links.map(function(link) {
  27481. return parse_xlink(getzipdata(zip, strip_front_slash(link)), link, opts);
  27482. });
  27483. var wb = parse_wb(getzipdata(zip, strip_front_slash(dir.workbooks[0])), dir.workbooks[0], opts);
  27484. var props = {}, propdata = "";
  27485. if(dir.coreprops.length) {
  27486. propdata = getzipdata(zip, strip_front_slash(dir.coreprops[0]), true);
  27487. if(propdata) props = parse_core_props(propdata);
  27488. if(dir.extprops.length !== 0) {
  27489. propdata = getzipdata(zip, strip_front_slash(dir.extprops[0]), true);
  27490. if(propdata) parse_ext_props(propdata, props, opts);
  27491. }
  27492. }
  27493. var custprops = {};
  27494. if(!opts.bookSheets || opts.bookProps) {
  27495. if (dir.custprops.length !== 0) {
  27496. propdata = getzipstr(zip, strip_front_slash(dir.custprops[0]), true);
  27497. if(propdata) custprops = parse_cust_props(propdata, opts);
  27498. }
  27499. }
  27500. var out = ({});
  27501. if(opts.bookSheets || opts.bookProps) {
  27502. if(wb.Sheets) sheets = wb.Sheets.map(function pluck(x){ return x.name; });
  27503. else if(props.Worksheets && props.SheetNames.length > 0) sheets=props.SheetNames;
  27504. if(opts.bookProps) { out.Props = props; out.Custprops = custprops; }
  27505. if(opts.bookSheets && typeof sheets !== 'undefined') out.SheetNames = sheets;
  27506. if(opts.bookSheets ? out.SheetNames : opts.bookProps) return out;
  27507. }
  27508. sheets = {};
  27509. var deps = {};
  27510. if(opts.bookDeps && dir.calcchain) deps=parse_cc(getzipdata(zip, strip_front_slash(dir.calcchain)),dir.calcchain,opts);
  27511. var i=0;
  27512. var sheetRels = ({});
  27513. var path, relsPath;
  27514. {
  27515. var wbsheets = wb.Sheets;
  27516. props.Worksheets = wbsheets.length;
  27517. props.SheetNames = [];
  27518. for(var j = 0; j != wbsheets.length; ++j) {
  27519. props.SheetNames[j] = wbsheets[j].name;
  27520. }
  27521. }
  27522. var wbext = xlsb ? "bin" : "xml";
  27523. var wbrelsi = dir.workbooks[0].lastIndexOf("/");
  27524. var wbrelsfile = (dir.workbooks[0].slice(0, wbrelsi+1) + "_rels/" + dir.workbooks[0].slice(wbrelsi+1) + ".rels").replace(/^\//,"");
  27525. if(!safegetzipfile(zip, wbrelsfile)) wbrelsfile = 'xl/_rels/workbook.' + wbext + '.rels';
  27526. var wbrels = parse_rels(getzipstr(zip, wbrelsfile, true), wbrelsfile);
  27527. if(wbrels) wbrels = safe_parse_wbrels(wbrels, wb.Sheets);
  27528. /* Numbers iOS hack */
  27529. var nmode = (getzipdata(zip,"xl/worksheets/sheet.xml",true))?1:0;
  27530. for(i = 0; i != props.Worksheets; ++i) {
  27531. var stype = "sheet";
  27532. if(wbrels && wbrels[i]) {
  27533. path = 'xl/' + (wbrels[i][1]).replace(/[\/]?xl\//, "");
  27534. if(!safegetzipfile(zip, path)) path = wbrels[i][1];
  27535. if(!safegetzipfile(zip, path)) path = wbrelsfile.replace(/_rels\/.*$/,"") + wbrels[i][1];
  27536. stype = wbrels[i][2];
  27537. } else {
  27538. path = 'xl/worksheets/sheet'+(i+1-nmode)+"." + wbext;
  27539. path = path.replace(/sheet0\./,"sheet.");
  27540. }
  27541. relsPath = path.replace(/^(.*)(\/)([^\/]*)$/, "$1/_rels/$3.rels");
  27542. safe_parse_sheet(zip, path, relsPath, props.SheetNames[i], i, sheetRels, sheets, stype, opts, wb, themes, styles);
  27543. }
  27544. if(dir.comments) parse_comments(zip, dir.comments, sheets, sheetRels, opts);
  27545. out = ({
  27546. Directory: dir,
  27547. Workbook: wb,
  27548. Props: props,
  27549. Custprops: custprops,
  27550. Deps: deps,
  27551. Sheets: sheets,
  27552. SheetNames: props.SheetNames,
  27553. Strings: strs,
  27554. Styles: styles,
  27555. Themes: themes,
  27556. SSF: SSF.get_table()
  27557. });
  27558. if(opts.bookFiles) {
  27559. out.keys = entries;
  27560. out.files = zip.files;
  27561. }
  27562. if(opts.bookVBA) {
  27563. if(dir.vba.length > 0) out.vbaraw = getzipdata(zip,strip_front_slash(dir.vba[0]),true);
  27564. else if(dir.defaults && dir.defaults.bin === CT_VBA) out.vbaraw = getzipdata(zip, 'xl/vbaProject.bin',true);
  27565. }
  27566. return out;
  27567. }
  27568. /* [MS-OFFCRYPTO] 2.1.1 */
  27569. function parse_xlsxcfb(cfb, _opts) {
  27570. var opts = _opts || {};
  27571. var f = 'Workbook', data = CFB.find(cfb, f);
  27572. try {
  27573. f = '/!DataSpaces/Version';
  27574. data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
  27575. /*var version = */parse_DataSpaceVersionInfo(data.content);
  27576. /* 2.3.4.1 */
  27577. f = '/!DataSpaces/DataSpaceMap';
  27578. data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
  27579. var dsm = parse_DataSpaceMap(data.content);
  27580. if(dsm.length !== 1 || dsm[0].comps.length !== 1 || dsm[0].comps[0].t !== 0 || dsm[0].name !== "StrongEncryptionDataSpace" || dsm[0].comps[0].v !== "EncryptedPackage")
  27581. throw new Error("ECMA-376 Encrypted file bad " + f);
  27582. /* 2.3.4.2 */
  27583. f = '/!DataSpaces/DataSpaceInfo/StrongEncryptionDataSpace';
  27584. data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
  27585. var seds = parse_DataSpaceDefinition(data.content);
  27586. if(seds.length != 1 || seds[0] != "StrongEncryptionTransform")
  27587. throw new Error("ECMA-376 Encrypted file bad " + f);
  27588. /* 2.3.4.3 */
  27589. f = '/!DataSpaces/TransformInfo/StrongEncryptionTransform/!Primary';
  27590. data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
  27591. /*var hdr = */parse_Primary(data.content);
  27592. } catch(e) {}
  27593. f = '/EncryptionInfo';
  27594. data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
  27595. var einfo = parse_EncryptionInfo(data.content);
  27596. /* 2.3.4.4 */
  27597. f = '/EncryptedPackage';
  27598. data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
  27599. /*global decrypt_agile */
  27600. if(einfo[0] == 0x04 && typeof decrypt_agile !== 'undefined') return decrypt_agile(einfo[1], data.content, opts.password || "", opts);
  27601. /*global decrypt_std76 */
  27602. if(einfo[0] == 0x02 && typeof decrypt_std76 !== 'undefined') return decrypt_std76(einfo[1], data.content, opts.password || "", opts);
  27603. throw new Error("File is password-protected");
  27604. }
  27605. function write_zip(wb, opts) {
  27606. _shapeid = 1024;
  27607. if(opts.bookType == "ods") return write_ods(wb, opts);
  27608. if(wb && !wb.SSF) {
  27609. wb.SSF = SSF.get_table();
  27610. }
  27611. if(wb && wb.SSF) {
  27612. make_ssf(SSF); SSF.load_table(wb.SSF);
  27613. // $FlowIgnore
  27614. opts.revssf = evert_num(wb.SSF); opts.revssf[wb.SSF[65535]] = 0;
  27615. opts.ssf = wb.SSF;
  27616. }
  27617. opts.rels = {}; opts.wbrels = {};
  27618. opts.Strings = []; opts.Strings.Count = 0; opts.Strings.Unique = 0;
  27619. if(browser_has_Map) opts.revStrings = new Map();
  27620. else { opts.revStrings = {}; opts.revStrings.foo = []; delete opts.revStrings.foo; }
  27621. var wbext = opts.bookType == "xlsb" ? "bin" : "xml";
  27622. var vbafmt = VBAFMTS.indexOf(opts.bookType) > -1;
  27623. var ct = new_ct();
  27624. fix_write_opts(opts = opts || {});
  27625. var zip = new jszip();
  27626. var f = "", rId = 0;
  27627. opts.cellXfs = [];
  27628. get_cell_style(opts.cellXfs, {}, {revssf:{"General":0}});
  27629. if(!wb.Props) wb.Props = {};
  27630. f = "docProps/core.xml";
  27631. zip.file(f, write_core_props(wb.Props, opts));
  27632. ct.coreprops.push(f);
  27633. add_rels(opts.rels, 2, f, RELS.CORE_PROPS);
  27634. f = "docProps/app.xml";
  27635. if(wb.Props && wb.Props.SheetNames){/* empty */}
  27636. else if(!wb.Workbook || !wb.Workbook.Sheets) wb.Props.SheetNames = wb.SheetNames;
  27637. else {
  27638. var _sn = [];
  27639. for(var _i = 0; _i < wb.SheetNames.length; ++_i)
  27640. if((wb.Workbook.Sheets[_i]||{}).Hidden != 2) _sn.push(wb.SheetNames[_i]);
  27641. wb.Props.SheetNames = _sn;
  27642. }
  27643. wb.Props.Worksheets = wb.Props.SheetNames.length;
  27644. zip.file(f, write_ext_props(wb.Props, opts));
  27645. ct.extprops.push(f);
  27646. add_rels(opts.rels, 3, f, RELS.EXT_PROPS);
  27647. if(wb.Custprops !== wb.Props && keys(wb.Custprops||{}).length > 0) {
  27648. f = "docProps/custom.xml";
  27649. zip.file(f, write_cust_props(wb.Custprops, opts));
  27650. ct.custprops.push(f);
  27651. add_rels(opts.rels, 4, f, RELS.CUST_PROPS);
  27652. }
  27653. for(rId=1;rId <= wb.SheetNames.length; ++rId) {
  27654. var wsrels = {'!id':{}};
  27655. var ws = wb.Sheets[wb.SheetNames[rId-1]];
  27656. var _type = (ws || {})["!type"] || "sheet";
  27657. switch(_type) {
  27658. case "chart": /*
  27659. f = "xl/chartsheets/sheet" + rId + "." + wbext;
  27660. zip.file(f, write_cs(rId-1, f, opts, wb, wsrels));
  27661. ct.charts.push(f);
  27662. add_rels(wsrels, -1, "chartsheets/sheet" + rId + "." + wbext, RELS.CS);
  27663. break; */
  27664. /* falls through */
  27665. default:
  27666. f = "xl/worksheets/sheet" + rId + "." + wbext;
  27667. zip.file(f, write_ws(rId-1, f, opts, wb, wsrels));
  27668. ct.sheets.push(f);
  27669. add_rels(opts.wbrels, -1, "worksheets/sheet" + rId + "." + wbext, RELS.WS[0]);
  27670. }
  27671. if(ws) {
  27672. var comments = ws['!comments'];
  27673. var need_vml = false;
  27674. if(comments && comments.length > 0) {
  27675. var cf = "xl/comments" + rId + "." + wbext;
  27676. zip.file(cf, write_cmnt(comments, cf, opts));
  27677. ct.comments.push(cf);
  27678. add_rels(wsrels, -1, "../comments" + rId + "." + wbext, RELS.CMNT);
  27679. need_vml = true;
  27680. }
  27681. if(ws['!legacy']) {
  27682. if(need_vml) zip.file("xl/drawings/vmlDrawing" + (rId) + ".vml", write_comments_vml(rId, ws['!comments']));
  27683. }
  27684. delete ws['!comments'];
  27685. delete ws['!legacy'];
  27686. }
  27687. if(wsrels['!id'].rId1) zip.file(get_rels_path(f), write_rels(wsrels));
  27688. }
  27689. if(opts.Strings != null && opts.Strings.length > 0) {
  27690. f = "xl/sharedStrings." + wbext;
  27691. zip.file(f, write_sst(opts.Strings, f, opts));
  27692. ct.strs.push(f);
  27693. add_rels(opts.wbrels, -1, "sharedStrings." + wbext, RELS.SST);
  27694. }
  27695. f = "xl/workbook." + wbext;
  27696. zip.file(f, write_wb(wb, f, opts));
  27697. ct.workbooks.push(f);
  27698. add_rels(opts.rels, 1, f, RELS.WB);
  27699. /* TODO: something more intelligent with themes */
  27700. f = "xl/theme/theme1.xml";
  27701. zip.file(f, write_theme(wb.Themes, opts));
  27702. ct.themes.push(f);
  27703. add_rels(opts.wbrels, -1, "theme/theme1.xml", RELS.THEME);
  27704. /* TODO: something more intelligent with styles */
  27705. f = "xl/styles." + wbext;
  27706. zip.file(f, write_sty(wb, f, opts));
  27707. ct.styles.push(f);
  27708. add_rels(opts.wbrels, -1, "styles." + wbext, RELS.STY);
  27709. if(wb.vbaraw && vbafmt) {
  27710. f = "xl/vbaProject.bin";
  27711. zip.file(f, wb.vbaraw);
  27712. ct.vba.push(f);
  27713. add_rels(opts.wbrels, -1, "vbaProject.bin", RELS.VBA);
  27714. }
  27715. zip.file("[Content_Types].xml", write_ct(ct, opts));
  27716. zip.file('_rels/.rels', write_rels(opts.rels));
  27717. zip.file('xl/_rels/workbook.' + wbext + '.rels', write_rels(opts.wbrels));
  27718. delete opts.revssf; delete opts.ssf;
  27719. return zip;
  27720. }
  27721. function firstbyte(f,o) {
  27722. var x = "";
  27723. switch((o||{}).type || "base64") {
  27724. case 'buffer': return [f[0], f[1], f[2], f[3]];
  27725. case 'base64': x = Base64.decode(f.slice(0,24)); break;
  27726. case 'binary': x = f; break;
  27727. case 'array': return [f[0], f[1], f[2], f[3]];
  27728. default: throw new Error("Unrecognized type " + (o && o.type || "undefined"));
  27729. }
  27730. return [x.charCodeAt(0), x.charCodeAt(1), x.charCodeAt(2), x.charCodeAt(3)];
  27731. }
  27732. function read_cfb(cfb, opts) {
  27733. if(CFB.find(cfb, "EncryptedPackage")) return parse_xlsxcfb(cfb, opts);
  27734. return parse_xlscfb(cfb, opts);
  27735. }
  27736. function read_zip(data, opts) {
  27737. var zip, d = data;
  27738. var o = opts||{};
  27739. if(!o.type) o.type = (has_buf && Buffer.isBuffer(data)) ? "buffer" : "base64";
  27740. switch(o.type) {
  27741. case "base64": zip = new jszip(d, { base64:true }); break;
  27742. case "binary": case "array": zip = new jszip(d, { base64:false }); break;
  27743. case "buffer": zip = new jszip(d); break;
  27744. default: throw new Error("Unrecognized type " + o.type);
  27745. }
  27746. return parse_zip(zip, o);
  27747. }
  27748. function read_plaintext(data, o) {
  27749. var i = 0;
  27750. main: while(i < data.length) switch(data.charCodeAt(i)) {
  27751. case 0x0A: case 0x0D: case 0x20: ++i; break;
  27752. case 0x3C: return parse_xlml(data.slice(i),o);
  27753. default: break main;
  27754. }
  27755. return PRN.to_workbook(data, o);
  27756. }
  27757. function read_plaintext_raw(data, o) {
  27758. var str = "", bytes = firstbyte(data, o);
  27759. switch(o.type) {
  27760. case 'base64': str = Base64.decode(data); break;
  27761. case 'binary': str = data; break;
  27762. case 'buffer': str = data.toString('binary'); break;
  27763. case 'array': str = cc2str(data); break;
  27764. default: throw new Error("Unrecognized type " + o.type);
  27765. }
  27766. if(bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) str = utf8read(str);
  27767. return read_plaintext(str, o);
  27768. }
  27769. function read_utf16(data, o) {
  27770. var d = data;
  27771. if(o.type == 'base64') d = Base64.decode(d);
  27772. d = cptable.utils.decode(1200, d.slice(2), 'str');
  27773. o.type = "binary";
  27774. return read_plaintext(d, o);
  27775. }
  27776. function bstrify(data) {
  27777. return !data.match(/[^\x00-\x7F]/) ? data : utf8write(data);
  27778. }
  27779. function read_prn(data, d, o, str) {
  27780. if(str) { o.type = "string"; return PRN.to_workbook(data, o); }
  27781. return PRN.to_workbook(d, o);
  27782. }
  27783. function readSync(data, opts) {
  27784. reset_cp();
  27785. if(typeof ArrayBuffer !== 'undefined' && data instanceof ArrayBuffer) return readSync(new Uint8Array(data), opts);
  27786. var d = data, n = [0,0,0,0], str = false;
  27787. var o = opts||{};
  27788. _ssfopts = {};
  27789. if(o.dateNF) _ssfopts.dateNF = o.dateNF;
  27790. if(!o.type) o.type = (has_buf && Buffer.isBuffer(data)) ? "buffer" : "base64";
  27791. if(o.type == "file") { o.type = has_buf ? "buffer" : "binary"; d = read_binary(data); }
  27792. if(o.type == "string") { str = true; o.type = "binary"; o.codepage = 65001; d = bstrify(data); }
  27793. if(o.type == 'array' && typeof Uint8Array !== 'undefined' && data instanceof Uint8Array && typeof ArrayBuffer !== 'undefined') {
  27794. // $FlowIgnore
  27795. var ab=new ArrayBuffer(3), vu=new Uint8Array(ab); vu.foo="bar";
  27796. // $FlowIgnore
  27797. if(!vu.foo) {o=dup(o); o.type='array'; return readSync(ab2a(d), o);}
  27798. }
  27799. switch((n = firstbyte(d, o))[0]) {
  27800. case 0xD0: return read_cfb(CFB.read(d, o), o);
  27801. case 0x09: return parse_xlscfb(d, o);
  27802. case 0x3C: return parse_xlml(d, o);
  27803. case 0x49: if(n[1] === 0x44) return read_wb_ID(d, o); break;
  27804. case 0x54: if(n[1] === 0x41 && n[2] === 0x42 && n[3] === 0x4C) return DIF.to_workbook(d, o); break;
  27805. case 0x50: return (n[1] === 0x4B && n[2] < 0x09 && n[3] < 0x09) ? read_zip(d, o) : read_prn(data, d, o, str);
  27806. case 0xEF: return n[3] === 0x3C ? parse_xlml(d, o) : read_prn(data, d, o, str);
  27807. case 0xFF: if(n[1] === 0xFE) { return read_utf16(d, o); } break;
  27808. case 0x00: if(n[1] === 0x00 && n[2] >= 0x02 && n[3] === 0x00) return WK_.to_workbook(d, o); break;
  27809. case 0x03: case 0x83: case 0x8B: case 0x8C: return DBF.to_workbook(d, o);
  27810. case 0x7B: if(n[1] === 0x5C && n[2] === 0x72 && n[3] === 0x74) return RTF.to_workbook(d, o); break;
  27811. case 0x0A: case 0x0D: case 0x20: return read_plaintext_raw(d, o);
  27812. }
  27813. if(n[2] <= 12 && n[3] <= 31) return DBF.to_workbook(d, o);
  27814. return read_prn(data, d, o, str);
  27815. }
  27816. function readFileSync(filename, opts) {
  27817. var o = opts||{}; o.type = 'file';
  27818. return readSync(filename, o);
  27819. }
  27820. function write_cfb_ctr(cfb, o) {
  27821. switch(o.type) {
  27822. case "base64": case "binary": break;
  27823. case "buffer": case "array": o.type = ""; break;
  27824. case "file": return write_dl(o.file, CFB.write(cfb, {type:has_buf ? 'buffer' : ""}));
  27825. case "string": throw new Error("'string' output type invalid for '" + o.bookType + "' files");
  27826. default: throw new Error("Unrecognized type " + o.type);
  27827. }
  27828. return CFB.write(cfb, o);
  27829. }
  27830. /*global encrypt_agile */
  27831. function write_zip_type(wb, opts) {
  27832. var o = opts||{};
  27833. style_builder = new StyleBuilder(opts);
  27834. var z = write_zip(wb, o);
  27835. var oopts = {};
  27836. if(o.compression) oopts.compression = 'DEFLATE';
  27837. if(o.password) oopts.type = has_buf ? "nodebuffer" : "string";
  27838. else switch(o.type) {
  27839. case "base64": oopts.type = "base64"; break;
  27840. case "binary": oopts.type = "string"; break;
  27841. case "string": throw new Error("'string' output type invalid for '" + o.bookType + "' files");
  27842. case "buffer":
  27843. case "file": oopts.type = has_buf ? "nodebuffer" : "string"; break;
  27844. default: throw new Error("Unrecognized type " + o.type);
  27845. }
  27846. var out = z.generate(oopts);
  27847. if(o.password && typeof encrypt_agile !== 'undefined') return write_cfb_ctr(encrypt_agile(out, o.password), o);
  27848. if(o.type === "file") return write_dl(o.file, out);
  27849. return o.type == "string" ? utf8read(out) : out;
  27850. }
  27851. function write_cfb_type(wb, opts) {
  27852. var o = opts||{};
  27853. var cfb = write_xlscfb(wb, o);
  27854. return write_cfb_ctr(cfb, o);
  27855. }
  27856. function write_string_type(out, opts, bom) {
  27857. if(!bom) bom = "";
  27858. var o = bom + out;
  27859. switch(opts.type) {
  27860. case "base64": return Base64.encode(utf8write(o));
  27861. case "binary": return utf8write(o);
  27862. case "string": return out;
  27863. case "file": return write_dl(opts.file, o, 'utf8');
  27864. case "buffer": {
  27865. // $FlowIgnore
  27866. if(has_buf) return Buffer_from(o, 'utf8');
  27867. else return write_string_type(o, {type:'binary'}).split("").map(function(c) { return c.charCodeAt(0); });
  27868. }
  27869. }
  27870. throw new Error("Unrecognized type " + opts.type);
  27871. }
  27872. function write_stxt_type(out, opts) {
  27873. switch(opts.type) {
  27874. case "base64": return Base64.encode(out);
  27875. case "binary": return out;
  27876. case "string": return out; /* override in sheet_to_txt */
  27877. case "file": return write_dl(opts.file, out, 'binary');
  27878. case "buffer": {
  27879. // $FlowIgnore
  27880. if(has_buf) return Buffer_from(out, 'binary');
  27881. else return out.split("").map(function(c) { return c.charCodeAt(0); });
  27882. }
  27883. }
  27884. throw new Error("Unrecognized type " + opts.type);
  27885. }
  27886. /* TODO: test consistency */
  27887. function write_binary_type(out, opts) {
  27888. switch(opts.type) {
  27889. case "string":
  27890. case "base64":
  27891. case "binary":
  27892. var bstr = "";
  27893. // $FlowIgnore
  27894. for(var i = 0; i < out.length; ++i) bstr += String.fromCharCode(out[i]);
  27895. return opts.type == 'base64' ? Base64.encode(bstr) : opts.type == 'string' ? utf8read(bstr) : bstr;
  27896. case "file": return write_dl(opts.file, out);
  27897. case "buffer": return out;
  27898. default: throw new Error("Unrecognized type " + opts.type);
  27899. }
  27900. }
  27901. function writeSync(wb, opts) {
  27902. check_wb(wb);
  27903. var o = opts||{};
  27904. if(o.type == "array") { o.type = "binary"; var out = (writeSync(wb, o)); o.type = "array"; return s2ab(out); }
  27905. switch(o.bookType || 'xlsb') {
  27906. case 'xml':
  27907. case 'xlml': return write_string_type(write_xlml(wb, o), o);
  27908. case 'slk':
  27909. case 'sylk': return write_string_type(write_slk_str(wb, o), o);
  27910. case 'htm':
  27911. case 'html': return write_string_type(write_htm_str(wb, o), o);
  27912. case 'txt': return write_stxt_type(write_txt_str(wb, o), o);
  27913. case 'csv': return write_string_type(write_csv_str(wb, o), o, "\ufeff");
  27914. case 'dif': return write_string_type(write_dif_str(wb, o), o);
  27915. case 'dbf': return write_binary_type(write_dbf_buf(wb, o), o);
  27916. case 'prn': return write_string_type(write_prn_str(wb, o), o);
  27917. case 'rtf': return write_string_type(write_rtf_str(wb, o), o);
  27918. case 'eth': return write_string_type(write_eth_str(wb, o), o);
  27919. case 'fods': return write_string_type(write_ods(wb, o), o);
  27920. case 'biff2': if(!o.biff) o.biff = 2; /* falls through */
  27921. case 'biff3': if(!o.biff) o.biff = 3; /* falls through */
  27922. case 'biff4': if(!o.biff) o.biff = 4; return write_binary_type(write_biff_buf(wb, o), o);
  27923. case 'biff5': if(!o.biff) o.biff = 5; /* falls through */
  27924. case 'biff8':
  27925. case 'xla':
  27926. case 'xls': if(!o.biff) o.biff = 8; return write_cfb_type(wb, o);
  27927. case 'xlsx':
  27928. case 'xlsm':
  27929. case 'xlam':
  27930. case 'xlsb':
  27931. case 'ods': return write_zip_type(wb, o);
  27932. default: throw new Error ("Unrecognized bookType |" + o.bookType + "|");
  27933. }
  27934. }
  27935. function resolve_book_type(o) {
  27936. if(o.bookType) return;
  27937. var _BT = {
  27938. "xls": "biff8",
  27939. "htm": "html",
  27940. "slk": "sylk",
  27941. "socialcalc": "eth",
  27942. "Sh33tJS": "WTF"
  27943. };
  27944. var ext = o.file.slice(o.file.lastIndexOf(".")).toLowerCase();
  27945. if(ext.match(/^\.[a-z]+$/)) o.bookType = ext.slice(1);
  27946. o.bookType = _BT[o.bookType] || o.bookType;
  27947. }
  27948. function writeFileSync(wb, filename, opts) {
  27949. var o = opts||{}; o.type = 'file';
  27950. o.file = filename;
  27951. resolve_book_type(o);
  27952. return writeSync(wb, o);
  27953. }
  27954. function writeFileAsync(filename, wb, opts, cb) {
  27955. var o = opts||{}; o.type = 'file';
  27956. o.file = filename;
  27957. resolve_book_type(o);
  27958. o.type = 'buffer';
  27959. var _cb = cb; if(!(_cb instanceof Function)) _cb = (opts);
  27960. return _fs.writeFile(filename, writeSync(wb, o), _cb);
  27961. }
  27962. function make_json_row(sheet, r, R, cols, header, hdr, dense, o) {
  27963. var rr = encode_row(R);
  27964. var defval = o.defval, raw = o.raw || !o.hasOwnProperty("raw");
  27965. var isempty = true;
  27966. var row = (header === 1) ? [] : {};
  27967. if(header !== 1) {
  27968. if(Object.defineProperty) try { Object.defineProperty(row, '__rowNum__', {value:R, enumerable:false}); } catch(e) { row.__rowNum__ = R; }
  27969. else row.__rowNum__ = R;
  27970. }
  27971. if(!dense || sheet[R]) for (var C = r.s.c; C <= r.e.c; ++C) {
  27972. var val = dense ? sheet[R][C] : sheet[cols[C] + rr];
  27973. if(val === undefined || val.t === undefined) {
  27974. if(defval === undefined) continue;
  27975. if(hdr[C] != null) { row[hdr[C]] = defval; }
  27976. continue;
  27977. }
  27978. var v = val.v;
  27979. switch(val.t){
  27980. case 'z': if(v == null) break; continue;
  27981. case 'e': v = void 0; break;
  27982. case 's': case 'd': case 'b': case 'n': break;
  27983. default: throw new Error('unrecognized type ' + val.t);
  27984. }
  27985. if(hdr[C] != null) {
  27986. if(v == null) {
  27987. if(defval !== undefined) row[hdr[C]] = defval;
  27988. else if(raw && v === null) row[hdr[C]] = null;
  27989. else continue;
  27990. } else {
  27991. row[hdr[C]] = raw ? v : format_cell(val,v,o);
  27992. }
  27993. if(v != null) isempty = false;
  27994. }
  27995. }
  27996. return { row: row, isempty: isempty };
  27997. }
  27998. function sheet_to_json(sheet, opts) {
  27999. if(sheet == null || sheet["!ref"] == null) return [];
  28000. var val = {t:'n',v:0}, header = 0, offset = 1, hdr = [], v=0, vv="";
  28001. var r = {s:{r:0,c:0},e:{r:0,c:0}};
  28002. var o = opts || {};
  28003. var range = o.range != null ? o.range : sheet["!ref"];
  28004. if(o.header === 1) header = 1;
  28005. else if(o.header === "A") header = 2;
  28006. else if(Array.isArray(o.header)) header = 3;
  28007. switch(typeof range) {
  28008. case 'string': r = safe_decode_range(range); break;
  28009. case 'number': r = safe_decode_range(sheet["!ref"]); r.s.r = range; break;
  28010. default: r = range;
  28011. }
  28012. if(header > 0) offset = 0;
  28013. var rr = encode_row(r.s.r);
  28014. var cols = [];
  28015. var out = [];
  28016. var outi = 0, counter = 0;
  28017. var dense = Array.isArray(sheet);
  28018. var R = r.s.r, C = 0, CC = 0;
  28019. if(dense && !sheet[R]) sheet[R] = [];
  28020. for(C = r.s.c; C <= r.e.c; ++C) {
  28021. cols[C] = encode_col(C);
  28022. val = dense ? sheet[R][C] : sheet[cols[C] + rr];
  28023. switch(header) {
  28024. case 1: hdr[C] = C - r.s.c; break;
  28025. case 2: hdr[C] = cols[C]; break;
  28026. case 3: hdr[C] = o.header[C - r.s.c]; break;
  28027. default:
  28028. if(val == null) val = {w: "__EMPTY", t: "s"};
  28029. vv = v = format_cell(val, null, o);
  28030. counter = 0;
  28031. for(CC = 0; CC < hdr.length; ++CC) if(hdr[CC] == vv) vv = v + "_" + (++counter);
  28032. hdr[C] = vv;
  28033. }
  28034. }
  28035. for (R = r.s.r + offset; R <= r.e.r; ++R) {
  28036. var row = make_json_row(sheet, r, R, cols, header, hdr, dense, o);
  28037. if((row.isempty === false) || (header === 1 ? o.blankrows !== false : !!o.blankrows)) out[outi++] = row.row;
  28038. }
  28039. out.length = outi;
  28040. return out;
  28041. }
  28042. var qreg = /"/g;
  28043. function make_csv_row(sheet, r, R, cols, fs, rs, FS, o) {
  28044. var isempty = true;
  28045. var row = [], txt = "", rr = encode_row(R);
  28046. for(var C = r.s.c; C <= r.e.c; ++C) {
  28047. if (!cols[C]) continue;
  28048. var val = o.dense ? (sheet[R]||[])[C]: sheet[cols[C] + rr];
  28049. if(val == null) txt = "";
  28050. else if(val.v != null) {
  28051. isempty = false;
  28052. txt = ''+format_cell(val, null, o);
  28053. for(var i = 0, cc = 0; i !== txt.length; ++i) if((cc = txt.charCodeAt(i)) === fs || cc === rs || cc === 34) {txt = "\"" + txt.replace(qreg, '""') + "\""; break; }
  28054. if(txt == "ID") txt = '"ID"';
  28055. } else if(val.f != null && !val.F) {
  28056. isempty = false;
  28057. txt = '=' + val.f; if(txt.indexOf(",") >= 0) txt = '"' + txt.replace(qreg, '""') + '"';
  28058. } else txt = "";
  28059. /* NOTE: Excel CSV does not support array formulae */
  28060. row.push(txt);
  28061. }
  28062. if(o.blankrows === false && isempty) return null;
  28063. return row.join(FS);
  28064. }
  28065. function sheet_to_csv(sheet, opts) {
  28066. var out = [];
  28067. var o = opts == null ? {} : opts;
  28068. if(sheet == null || sheet["!ref"] == null) return "";
  28069. var r = safe_decode_range(sheet["!ref"]);
  28070. var FS = o.FS !== undefined ? o.FS : ",", fs = FS.charCodeAt(0);
  28071. var RS = o.RS !== undefined ? o.RS : "\n", rs = RS.charCodeAt(0);
  28072. var endregex = new RegExp((FS=="|" ? "\\|" : FS)+"+$");
  28073. var row = "", cols = [];
  28074. o.dense = Array.isArray(sheet);
  28075. var colinfo = o.skipHidden && sheet["!cols"] || [];
  28076. var rowinfo = o.skipHidden && sheet["!rows"] || [];
  28077. for(var C = r.s.c; C <= r.e.c; ++C) if (!((colinfo[C]||{}).hidden)) cols[C] = encode_col(C);
  28078. for(var R = r.s.r; R <= r.e.r; ++R) {
  28079. if ((rowinfo[R]||{}).hidden) continue;
  28080. row = make_csv_row(sheet, r, R, cols, fs, rs, FS, o);
  28081. if(row == null) { continue; }
  28082. if(o.strip) row = row.replace(endregex,"");
  28083. out.push(row + RS);
  28084. }
  28085. delete o.dense;
  28086. return out.join("");
  28087. }
  28088. function sheet_to_txt(sheet, opts) {
  28089. if(!opts) opts = {}; opts.FS = "\t"; opts.RS = "\n";
  28090. var s = sheet_to_csv(sheet, opts);
  28091. if(typeof cptable == 'undefined' || opts.type == 'string') return s;
  28092. var o = cptable.utils.encode(1200, s, 'str');
  28093. return String.fromCharCode(255) + String.fromCharCode(254) + o;
  28094. }
  28095. function sheet_to_formulae(sheet) {
  28096. var y = "", x, val="";
  28097. if(sheet == null || sheet["!ref"] == null) return [];
  28098. var r = safe_decode_range(sheet['!ref']), rr = "", cols = [], C;
  28099. var cmds = [];
  28100. var dense = Array.isArray(sheet);
  28101. for(C = r.s.c; C <= r.e.c; ++C) cols[C] = encode_col(C);
  28102. for(var R = r.s.r; R <= r.e.r; ++R) {
  28103. rr = encode_row(R);
  28104. for(C = r.s.c; C <= r.e.c; ++C) {
  28105. y = cols[C] + rr;
  28106. x = dense ? (sheet[R]||[])[C] : sheet[y];
  28107. val = "";
  28108. if(x === undefined) continue;
  28109. else if(x.F != null) {
  28110. y = x.F;
  28111. if(!x.f) continue;
  28112. val = x.f;
  28113. if(y.indexOf(":") == -1) y = y + ":" + y;
  28114. }
  28115. if(x.f != null) val = x.f;
  28116. else if(x.t == 'z') continue;
  28117. else if(x.t == 'n' && x.v != null) val = "" + x.v;
  28118. else if(x.t == 'b') val = x.v ? "TRUE" : "FALSE";
  28119. else if(x.w !== undefined) val = "'" + x.w;
  28120. else if(x.v === undefined) continue;
  28121. else if(x.t == 's') val = "'" + x.v;
  28122. else val = ""+x.v;
  28123. cmds[cmds.length] = y + "=" + val;
  28124. }
  28125. }
  28126. return cmds;
  28127. }
  28128. function sheet_add_json(_ws, js, opts) {
  28129. var o = opts || {};
  28130. var offset = +!o.skipHeader;
  28131. var ws = _ws || ({});
  28132. var _R = 0, _C = 0;
  28133. if(ws && o.origin != null) {
  28134. if(typeof o.origin == 'number') _R = o.origin;
  28135. else {
  28136. var _origin = typeof o.origin == "string" ? decode_cell(o.origin) : o.origin;
  28137. _R = _origin.r; _C = _origin.c;
  28138. }
  28139. }
  28140. var cell;
  28141. var range = ({s: {c:0, r:0}, e: {c:_C, r:_R + js.length - 1 + offset}});
  28142. if(ws['!ref']) {
  28143. var _range = safe_decode_range(ws['!ref']);
  28144. range.e.c = Math.max(range.e.c, _range.e.c);
  28145. range.e.r = Math.max(range.e.r, _range.e.r);
  28146. if(_R == -1) { _R = range.e.r + 1; range.e.r = _R + js.length - 1 + offset; }
  28147. }
  28148. var hdr = o.header || [], C = 0;
  28149. js.forEach(function (JS, R) {
  28150. keys(JS).forEach(function(k) {
  28151. if((C=hdr.indexOf(k)) == -1) hdr[C=hdr.length] = k;
  28152. var v = JS[k];
  28153. var t = 'z';
  28154. var z = "";
  28155. if(v && typeof v === 'object' && !(v instanceof Date)){
  28156. ws[encode_cell({c:_C + C,r:_R + R + offset})] = v;
  28157. } else {
  28158. if(typeof v == 'number') t = 'n';
  28159. else if(typeof v == 'boolean') t = 'b';
  28160. else if(typeof v == 'string') t = 's';
  28161. else if(v instanceof Date) {
  28162. t = 'd';
  28163. if(!o.cellDates) { t = 'n'; v = datenum(v); }
  28164. z = o.dateNF || SSF._table[14];
  28165. }
  28166. ws[encode_cell({c:_C + C,r:_R + R + offset})] = cell = ({t:t, v:v});
  28167. if(z) cell.z = z;
  28168. }
  28169. });
  28170. });
  28171. range.e.c = Math.max(range.e.c, _C + hdr.length - 1);
  28172. var __R = encode_row(_R);
  28173. if(offset) for(C = 0; C < hdr.length; ++C) ws[encode_col(C + _C) + __R] = {t:'s', v:hdr[C]};
  28174. ws['!ref'] = encode_range(range);
  28175. return ws;
  28176. }
  28177. function json_to_sheet(js, opts) { return sheet_add_json(null, js, opts); }
  28178. var utils = {
  28179. encode_col: encode_col,
  28180. encode_row: encode_row,
  28181. encode_cell: encode_cell,
  28182. encode_range: encode_range,
  28183. decode_col: decode_col,
  28184. decode_row: decode_row,
  28185. split_cell: split_cell,
  28186. decode_cell: decode_cell,
  28187. decode_range: decode_range,
  28188. format_cell: format_cell,
  28189. get_formulae: sheet_to_formulae,
  28190. make_csv: sheet_to_csv,
  28191. make_json: sheet_to_json,
  28192. make_formulae: sheet_to_formulae,
  28193. sheet_add_aoa: sheet_add_aoa,
  28194. sheet_add_json: sheet_add_json,
  28195. aoa_to_sheet: aoa_to_sheet,
  28196. json_to_sheet: json_to_sheet,
  28197. table_to_sheet: parse_dom_table,
  28198. table_to_book: table_to_book,
  28199. sheet_to_csv: sheet_to_csv,
  28200. sheet_to_txt: sheet_to_txt,
  28201. sheet_to_json: sheet_to_json,
  28202. sheet_to_html: HTML_.from_sheet,
  28203. sheet_to_dif: DIF.from_sheet,
  28204. sheet_to_slk: SYLK.from_sheet,
  28205. sheet_to_eth: ETH.from_sheet,
  28206. sheet_to_formulae: sheet_to_formulae,
  28207. sheet_to_row_object_array: sheet_to_json
  28208. };
  28209. (function(utils) {
  28210. utils.consts = utils.consts || {};
  28211. function add_consts(R/*Array<any>*/) { R.forEach(function(a){ utils.consts[a[0]] = a[1]; }); }
  28212. function get_default(x, y, z) { return x[y] != null ? x[y] : (x[y] = z); }
  28213. /* get cell, creating a stub if necessary */
  28214. function ws_get_cell_stub(ws, R, C) {
  28215. /* A1 cell address */
  28216. if(typeof R == "string") return ws[R] || (ws[R] = {t:'z'});
  28217. /* cell address object */
  28218. if(typeof R != "number") return ws_get_cell_stub(ws, encode_cell(R));
  28219. /* R and C are 0-based indices */
  28220. return ws_get_cell_stub(ws, encode_cell({r:R,c:C||0}));
  28221. }
  28222. /* find sheet index for given name / validate index */
  28223. function wb_sheet_idx(wb, sh) {
  28224. if(typeof sh == "number") {
  28225. if(sh >= 0 && wb.SheetNames.length > sh) return sh;
  28226. throw new Error("Cannot find sheet # " + sh);
  28227. } else if(typeof sh == "string") {
  28228. var idx = wb.SheetNames.indexOf(sh);
  28229. if(idx > -1) return idx;
  28230. throw new Error("Cannot find sheet name |" + sh + "|");
  28231. } else throw new Error("Cannot find sheet |" + sh + "|");
  28232. }
  28233. /* simple blank workbook object */
  28234. utils.book_new = function() {
  28235. return { SheetNames: [], Sheets: {} };
  28236. };
  28237. /* add a worksheet to the end of a given workbook */
  28238. utils.book_append_sheet = function(wb, ws, name) {
  28239. if(!name) for(var i = 1; i <= 0xFFFF; ++i) if(wb.SheetNames.indexOf(name = "Sheet" + i) == -1) break;
  28240. if(!name) throw new Error("Too many worksheets");
  28241. check_ws_name(name);
  28242. if(wb.SheetNames.indexOf(name) >= 0) throw new Error("Worksheet with name |" + name + "| already exists!");
  28243. wb.SheetNames.push(name);
  28244. wb.Sheets[name] = ws;
  28245. };
  28246. /* set sheet visibility (visible/hidden/very hidden) */
  28247. utils.book_set_sheet_visibility = function(wb, sh, vis) {
  28248. get_default(wb,"Workbook",{});
  28249. get_default(wb.Workbook,"Sheets",[]);
  28250. var idx = wb_sheet_idx(wb, sh);
  28251. // $FlowIgnore
  28252. get_default(wb.Workbook.Sheets,idx, {});
  28253. switch(vis) {
  28254. case 0: case 1: case 2: break;
  28255. default: throw new Error("Bad sheet visibility setting " + vis);
  28256. }
  28257. // $FlowIgnore
  28258. wb.Workbook.Sheets[idx].Hidden = vis;
  28259. };
  28260. add_consts([
  28261. ["SHEET_VISIBLE", 0],
  28262. ["SHEET_HIDDEN", 1],
  28263. ["SHEET_VERY_HIDDEN", 2]
  28264. ]);
  28265. /* set number format */
  28266. utils.cell_set_number_format = function(cell, fmt) {
  28267. cell.z = fmt;
  28268. return cell;
  28269. };
  28270. /* set cell hyperlink */
  28271. utils.cell_set_hyperlink = function(cell, target, tooltip) {
  28272. if(!target) {
  28273. delete cell.l;
  28274. } else {
  28275. cell.l = ({ Target: target });
  28276. if(tooltip) cell.l.Tooltip = tooltip;
  28277. }
  28278. return cell;
  28279. };
  28280. utils.cell_set_internal_link = function(cell, range, tooltip) { return utils.cell_set_hyperlink(cell, "#" + range, tooltip); };
  28281. /* add to cell comments */
  28282. utils.cell_add_comment = function(cell, text, author) {
  28283. if(!cell.c) cell.c = [];
  28284. cell.c.push({t:text, a:author||"SheetJS"});
  28285. };
  28286. /* set array formula and flush related cells */
  28287. utils.sheet_set_array_formula = function(ws, range, formula) {
  28288. var rng = typeof range != "string" ? range : safe_decode_range(range);
  28289. var rngstr = typeof range == "string" ? range : encode_range(range);
  28290. for(var R = rng.s.r; R <= rng.e.r; ++R) for(var C = rng.s.c; C <= rng.e.c; ++C) {
  28291. var cell = ws_get_cell_stub(ws, R, C);
  28292. cell.t = 'n';
  28293. cell.F = rngstr;
  28294. delete cell.v;
  28295. if(R == rng.s.r && C == rng.s.c) cell.f = formula;
  28296. }
  28297. return ws;
  28298. };
  28299. return utils;
  28300. })(utils);
  28301. if(has_buf && typeof require != 'undefined') (function() {
  28302. var Readable = {}.Readable;
  28303. var write_csv_stream = function(sheet, opts) {
  28304. var stream = Readable();
  28305. var o = opts == null ? {} : opts;
  28306. if(sheet == null || sheet["!ref"] == null) { stream.push(null); return stream; }
  28307. var r = safe_decode_range(sheet["!ref"]);
  28308. var FS = o.FS !== undefined ? o.FS : ",", fs = FS.charCodeAt(0);
  28309. var RS = o.RS !== undefined ? o.RS : "\n", rs = RS.charCodeAt(0);
  28310. var endregex = new RegExp((FS=="|" ? "\\|" : FS)+"+$");
  28311. var row = "", cols = [];
  28312. o.dense = Array.isArray(sheet);
  28313. var colinfo = o.skipHidden && sheet["!cols"] || [];
  28314. var rowinfo = o.skipHidden && sheet["!rows"] || [];
  28315. for(var C = r.s.c; C <= r.e.c; ++C) if (!((colinfo[C]||{}).hidden)) cols[C] = encode_col(C);
  28316. var R = r.s.r;
  28317. var BOM = false;
  28318. stream._read = function() {
  28319. if(!BOM) { BOM = true; return stream.push("\uFEFF"); }
  28320. if(R > r.e.r) return stream.push(null);
  28321. while(R <= r.e.r) {
  28322. ++R;
  28323. if ((rowinfo[R-1]||{}).hidden) continue;
  28324. row = make_csv_row(sheet, r, R-1, cols, fs, rs, FS, o);
  28325. if(row != null) {
  28326. if(o.strip) row = row.replace(endregex,"");
  28327. stream.push(row + RS);
  28328. break;
  28329. }
  28330. }
  28331. };
  28332. return stream;
  28333. };
  28334. var write_html_stream = function(ws, opts) {
  28335. var stream = Readable();
  28336. var o = opts || {};
  28337. var header = o.header != null ? o.header : HTML_.BEGIN;
  28338. var footer = o.footer != null ? o.footer : HTML_.END;
  28339. stream.push(header);
  28340. var r = decode_range(ws['!ref']);
  28341. o.dense = Array.isArray(ws);
  28342. stream.push(HTML_._preamble(ws, r, o));
  28343. var R = r.s.r;
  28344. var end = false;
  28345. stream._read = function() {
  28346. if(R > r.e.r) {
  28347. if(!end) { end = true; stream.push("</table>" + footer); }
  28348. return stream.push(null);
  28349. }
  28350. while(R <= r.e.r) {
  28351. stream.push(HTML_._row(ws, r, R, o));
  28352. ++R;
  28353. break;
  28354. }
  28355. };
  28356. return stream;
  28357. };
  28358. var write_json_stream = function(sheet, opts) {
  28359. var stream = Readable({objectMode:true});
  28360. if(sheet == null || sheet["!ref"] == null) { stream.push(null); return stream; }
  28361. var val = {t:'n',v:0}, header = 0, offset = 1, hdr = [], v=0, vv="";
  28362. var r = {s:{r:0,c:0},e:{r:0,c:0}};
  28363. var o = opts || {};
  28364. var range = o.range != null ? o.range : sheet["!ref"];
  28365. if(o.header === 1) header = 1;
  28366. else if(o.header === "A") header = 2;
  28367. else if(Array.isArray(o.header)) header = 3;
  28368. switch(typeof range) {
  28369. case 'string': r = safe_decode_range(range); break;
  28370. case 'number': r = safe_decode_range(sheet["!ref"]); r.s.r = range; break;
  28371. default: r = range;
  28372. }
  28373. if(header > 0) offset = 0;
  28374. var rr = encode_row(r.s.r);
  28375. var cols = [];
  28376. var counter = 0;
  28377. var dense = Array.isArray(sheet);
  28378. var R = r.s.r, C = 0, CC = 0;
  28379. if(dense && !sheet[R]) sheet[R] = [];
  28380. for(C = r.s.c; C <= r.e.c; ++C) {
  28381. cols[C] = encode_col(C);
  28382. val = dense ? sheet[R][C] : sheet[cols[C] + rr];
  28383. switch(header) {
  28384. case 1: hdr[C] = C - r.s.c; break;
  28385. case 2: hdr[C] = cols[C]; break;
  28386. case 3: hdr[C] = o.header[C - r.s.c]; break;
  28387. default:
  28388. if(val == null) val = {w: "__EMPTY", t: "s"};
  28389. vv = v = format_cell(val, null, o);
  28390. counter = 0;
  28391. for(CC = 0; CC < hdr.length; ++CC) if(hdr[CC] == vv) vv = v + "_" + (++counter);
  28392. hdr[C] = vv;
  28393. }
  28394. }
  28395. R = r.s.r + offset;
  28396. stream._read = function() {
  28397. if(R > r.e.r) return stream.push(null);
  28398. while(R <= r.e.r) {
  28399. //if ((rowinfo[R-1]||{}).hidden) continue;
  28400. var row = make_json_row(sheet, r, R, cols, header, hdr, dense, o);
  28401. ++R;
  28402. if((row.isempty === false) || (header === 1 ? o.blankrows !== false : !!o.blankrows)) {
  28403. stream.push(row.row);
  28404. break;
  28405. }
  28406. }
  28407. };
  28408. return stream;
  28409. };
  28410. XLSX.stream = {
  28411. to_json: write_json_stream,
  28412. to_html: write_html_stream,
  28413. to_csv: write_csv_stream
  28414. };
  28415. })();
  28416. /**
  28417. * 样式魔改区 -- start
  28418. */
  28419. /////////////////////////////////////////////////////////////////////////////////////////////////////
  28420. var XmlNode = (function () {
  28421. function XmlNode(tagName, attributes, children) {
  28422. if (!(this instanceof XmlNode)) {
  28423. return new XmlNode(tagName, attributes, children);
  28424. }
  28425. this.tagName = tagName;
  28426. this._attributes = attributes || {};
  28427. this._children = children || [];
  28428. this._prefix = '';
  28429. return this;
  28430. }
  28431. XmlNode.prototype.createElement = function () {
  28432. return new XmlNode(arguments)
  28433. }
  28434. XmlNode.prototype.children = function() {
  28435. return this._children;
  28436. }
  28437. XmlNode.prototype.append = function (node) {
  28438. this._children.push(node);
  28439. return this;
  28440. }
  28441. XmlNode.prototype.prefix = function (prefix) {
  28442. if (arguments.length==0) { return this._prefix;}
  28443. this._prefix = prefix;
  28444. return this;
  28445. }
  28446. XmlNode.prototype.attr = function (attr, value) {
  28447. if (value == undefined) {
  28448. delete this._attributes[attr];
  28449. return this;
  28450. }
  28451. if (arguments.length == 0) {
  28452. return this._attributes;
  28453. }
  28454. else if (typeof attr == 'string' && arguments.length == 1) {
  28455. return this._attributes.attr[attr];
  28456. }
  28457. if (typeof attr == 'object' && arguments.length == 1) {
  28458. for (var key in attr) {
  28459. this._attributes[key] = attr[key];
  28460. }
  28461. }
  28462. else if (arguments.length == 2 && typeof attr == 'string') {
  28463. this._attributes[attr] = value;
  28464. }
  28465. return this;
  28466. }
  28467. var APOS = "'"; QUOTE = '"'
  28468. var ESCAPED_QUOTE = { }
  28469. ESCAPED_QUOTE[QUOTE] = '&quot;'
  28470. ESCAPED_QUOTE[APOS] = '&apos;'
  28471. XmlNode.prototype.escapeAttributeValue = function(att_value) {
  28472. return '"' + att_value.replace(/\"/g,'&quot;') + '"';// TODO Extend with four other codes
  28473. }
  28474. XmlNode.prototype.toXml = function (node) {
  28475. if (!node) node = this;
  28476. var xml = node._prefix;
  28477. xml += '<' + node.tagName;
  28478. if (node._attributes) {
  28479. for (var key in node._attributes) {
  28480. xml += ' ' + key + '=' + this.escapeAttributeValue(''+node._attributes[key]) + ''
  28481. }
  28482. }
  28483. if (node._children && node._children.length > 0) {
  28484. xml += ">";
  28485. for (var i = 0; i < node._children.length; i++) {
  28486. xml += this.toXml(node._children[i]);
  28487. }
  28488. xml += '</' + node.tagName + '>';
  28489. }
  28490. else {
  28491. xml += '/>';
  28492. }
  28493. return xml;
  28494. }
  28495. return XmlNode;
  28496. })();
  28497. /////////////////////////////////////////////////////////////////////////////////////////////////////
  28498. var StyleBuilder = function (options) {
  28499. var customNumFmtId = 164;
  28500. var table_fmt = {
  28501. 0: 'General',
  28502. 1: '0',
  28503. 2: '0.00',
  28504. 3: '#,##0',
  28505. 4: '#,##0.00',
  28506. 9: '0%',
  28507. 10: '0.00%',
  28508. 11: '0.00E+00',
  28509. 12: '# ?/?',
  28510. 13: '# ??/??',
  28511. 14: 'm/d/yy',
  28512. 15: 'd-mmm-yy',
  28513. 16: 'd-mmm',
  28514. 17: 'mmm-yy',
  28515. 18: 'h:mm AM/PM',
  28516. 19: 'h:mm:ss AM/PM',
  28517. 20: 'h:mm',
  28518. 21: 'h:mm:ss',
  28519. 22: 'm/d/yy h:mm',
  28520. 37: '#,##0 ;(#,##0)',
  28521. 38: '#,##0 ;[Red](#,##0)',
  28522. 39: '#,##0.00;(#,##0.00)',
  28523. 40: '#,##0.00;[Red](#,##0.00)',
  28524. 45: 'mm:ss',
  28525. 46: '[h]:mm:ss',
  28526. 47: 'mmss.0',
  28527. 48: '##0.0E+0',
  28528. 49: '@',
  28529. 56: '"上午/下午 "hh"時"mm"分"ss"秒 "' };
  28530. var fmt_table = {};
  28531. for (var idx in table_fmt) {
  28532. fmt_table[table_fmt[idx]] = idx;
  28533. }
  28534. // cache style specs to avoid excessive duplication
  28535. _hashIndex = {};
  28536. _listIndex = [];
  28537. return {
  28538. initialize: function (options) {
  28539. this.$fonts = XmlNode('fonts').attr('count',0).attr("x14ac:knownFonts","1");
  28540. this.$fills = XmlNode('fills').attr('count',0);
  28541. this.$borders = XmlNode('borders').attr('count',0);
  28542. this.$numFmts = XmlNode('numFmts').attr('count',0);
  28543. this.$cellStyleXfs = XmlNode('cellStyleXfs');
  28544. this.$xf = XmlNode('xf')
  28545. .attr('numFmtId', 0)
  28546. .attr('fontId', 0)
  28547. .attr('fillId', 0)
  28548. .attr('borderId', 0);
  28549. this.$cellXfs = XmlNode('cellXfs').attr('count',0);
  28550. this.$cellStyles = XmlNode('cellStyles')
  28551. .append(XmlNode('cellStyle')
  28552. .attr('name', 'Normal')
  28553. .attr('xfId',0)
  28554. .attr('builtinId',0)
  28555. );
  28556. this.$dxfs = XmlNode('dxfs').attr('count', "0");
  28557. this.$tableStyles = XmlNode('tableStyles')
  28558. .attr('count','0')
  28559. .attr('defaultTableStyle','TableStyleMedium9')
  28560. .attr('defaultPivotStyle','PivotStyleMedium4')
  28561. this.$styles = XmlNode('styleSheet')
  28562. .attr('xmlns:mc','http://schemas.openxmlformats.org/markup-compatibility/2006')
  28563. .attr('xmlns:x14ac','http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac')
  28564. .attr('xmlns','http://schemas.openxmlformats.org/spreadsheetml/2006/main')
  28565. .attr('mc:Ignorable','x14ac')
  28566. .prefix('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>')
  28567. .append(this.$numFmts)
  28568. .append(this.$fonts)
  28569. .append(this.$fills)
  28570. .append(this.$borders)
  28571. .append(this.$cellStyleXfs.append(this.$xf))
  28572. .append(this.$cellXfs)
  28573. .append(this.$cellStyles)
  28574. .append(this.$dxfs)
  28575. .append(this.$tableStyles);
  28576. // need to specify styles at index 0 and 1.
  28577. // the second style MUST be gray125 for some reason
  28578. var defaultStyle = options.defaultCellStyle || {};
  28579. if (!defaultStyle.font) defaultStyle.font = {name: 'Calibri', sz: '12'};
  28580. if (!defaultStyle.font.name) defaultStyle.font.name = 'Calibri';
  28581. if (!defaultStyle.font.sz) defaultStyle.font.sz = 11;
  28582. if (!defaultStyle.fill) defaultStyle.fill = { patternType: "none", fgColor: {}};
  28583. if (!defaultStyle.border) defaultStyle.border = {};
  28584. if (!defaultStyle.numFmt) defaultStyle.numFmt = 0;
  28585. this.defaultStyle = defaultStyle;
  28586. var gray125Style = JSON.parse(JSON.stringify(defaultStyle));
  28587. gray125Style.fill = {patternType: "gray125", fgColor: { }}
  28588. this.addStyles([defaultStyle, gray125Style]);
  28589. return this;
  28590. },
  28591. // create a style entry and returns an integer index that can be used in the cell .s property
  28592. // these format of this object follows the emerging Common Spreadsheet Format
  28593. addStyle: function (attributes) {
  28594. var hashKey = JSON.stringify(attributes);
  28595. var index = _hashIndex[hashKey];
  28596. if (index == undefined) {
  28597. index = this._addXf(attributes); //_listIndex.push(attributes) -1;
  28598. _hashIndex[hashKey] = index;
  28599. }
  28600. else {
  28601. index = _hashIndex[hashKey];
  28602. }
  28603. return index;
  28604. },
  28605. // create style entries and returns array of integer indexes that can be used in cell .s property
  28606. addStyles: function (styles) {
  28607. var self = this;
  28608. return styles.map(function (style) {
  28609. return self.addStyle(style);
  28610. })
  28611. },
  28612. _duckTypeStyle: function(attributes) {
  28613. if (typeof attributes == 'object' && (attributes.patternFill || attributes.fgColor)) {
  28614. return {fill: attributes }; // this must be read via XLSX.parseFile(...)
  28615. }
  28616. else if (attributes.font || attributes.numFmt || attributes.border || attributes.fill) {
  28617. return attributes;
  28618. }
  28619. else {
  28620. return this._getStyleCSS(attributes)
  28621. }
  28622. },
  28623. _getStyleCSS: function(css) {
  28624. return css; //TODO
  28625. },
  28626. // Create an <xf> record for the style as well as corresponding <font>, <fill>, <border>, <numfmts>
  28627. // Right now this is simple and creates a <font>, <fill>, <border>, <numfmts> for every <xf>
  28628. // We could perhaps get fancier and avoid duplicating auxiliary entries as Excel presumably intended, but bother.
  28629. _addXf: function (attributes) {
  28630. var fontId = this._addFont(attributes.font);
  28631. var fillId = this._addFill(attributes.fill);
  28632. var borderId = this._addBorder(attributes.border);
  28633. var numFmtId = this._addNumFmt(attributes.numFmt);
  28634. var $xf = XmlNode('xf')
  28635. .attr("numFmtId", numFmtId)
  28636. .attr("fontId", fontId)
  28637. .attr("fillId", fillId)
  28638. .attr("borderId", borderId)
  28639. .attr("xfId", "0");
  28640. if (fontId > 0) {
  28641. $xf.attr('applyFont', "1");
  28642. }
  28643. if (fillId > 0) {
  28644. $xf.attr('applyFill', "1");
  28645. }
  28646. if (borderId > 0) {
  28647. $xf.attr('applyBorder', "1");
  28648. }
  28649. if (numFmtId > 0) {
  28650. $xf.attr('applyNumberFormat', "1");
  28651. }
  28652. if (attributes.alignment) {
  28653. var $alignment = XmlNode('alignment');
  28654. if (attributes.alignment.horizontal) { $alignment.attr('horizontal', attributes.alignment.horizontal);}
  28655. if (attributes.alignment.vertical) { $alignment.attr('vertical', attributes.alignment.vertical);}
  28656. if (attributes.alignment.indent) { $alignment.attr('indent', attributes.alignment.indent);}
  28657. if (attributes.alignment.readingOrder) { $alignment.attr('readingOrder', attributes.alignment.readingOrder);}
  28658. if (attributes.alignment.wrapText) { $alignment.attr('wrapText', attributes.alignment.wrapText);}
  28659. if (attributes.alignment.textRotation!=undefined) { $alignment.attr('textRotation', attributes.alignment.textRotation);}
  28660. $xf.append($alignment).attr('applyAlignment',1)
  28661. }
  28662. this.$cellXfs.append($xf);
  28663. var count = +this.$cellXfs.children().length;
  28664. this.$cellXfs.attr('count', count);
  28665. return count - 1;
  28666. },
  28667. _addFont: function (attributes) {
  28668. if (!attributes) { return 0; }
  28669. var $font = XmlNode('font')
  28670. .append(XmlNode('sz').attr('val', attributes.sz || this.defaultStyle.font.sz))
  28671. .append(XmlNode('name').attr('val', attributes.name || this.defaultStyle.font.name))
  28672. if (attributes.bold) $font.append(XmlNode('b'));
  28673. if (attributes.underline) $font.append(XmlNode('u'));
  28674. if (attributes.italic) $font.append(XmlNode('i'));
  28675. if (attributes.strike) $font.append(XmlNode('strike'));
  28676. if (attributes.outline) $font.append(XmlNode('outline'));
  28677. if (attributes.shadow) $font.append(XmlNode('shadow'));
  28678. if (attributes.vertAlign) {
  28679. $font.append(XmlNode('vertAlign').attr('val', attributes.vertAlign))
  28680. }
  28681. if (attributes.color) {
  28682. if (attributes.color.theme) {
  28683. $font.append(XmlNode('color').attr('theme', attributes.color.theme))
  28684. if (attributes.color.tint) { //tint only if theme
  28685. $font.append(XmlNode('tint').attr('theme', attributes.color.tint))
  28686. }
  28687. } else if (attributes.color.rgb) { // not both rgb and theme
  28688. $font.append(XmlNode('color').attr('rgb', attributes.color.rgb))
  28689. }
  28690. }
  28691. this.$fonts.append($font);
  28692. var count = this.$fonts.children().length;
  28693. this.$fonts.attr('count', count);
  28694. return count - 1;
  28695. },
  28696. _addNumFmt: function (numFmt) {
  28697. if (!numFmt) { return 0; }
  28698. if (typeof numFmt == 'string') {
  28699. var numFmtIdx = fmt_table[numFmt];
  28700. if (numFmtIdx >= 0) {
  28701. return numFmtIdx; // we found a match against built in formats
  28702. }
  28703. }
  28704. if (/^[0-9]+$/.exec(numFmt)) {
  28705. return numFmt; // we're matching an integer against some known code
  28706. }
  28707. numFmt = numFmt
  28708. .replace(/&/g, '&amp;')
  28709. .replace(/</g, '&lt;')
  28710. .replace(/>/g, '&gt;')
  28711. .replace(/"/g, '&quot;')
  28712. .replace(/'/g, '&apos;');
  28713. var $numFmt = XmlNode('numFmt')
  28714. .attr('numFmtId', (++customNumFmtId))
  28715. .attr('formatCode', numFmt);
  28716. this.$numFmts.append($numFmt);
  28717. var count = this.$numFmts.children().length;
  28718. this.$numFmts.attr('count', count);
  28719. return customNumFmtId ;
  28720. },
  28721. _addFill: function (attributes) {
  28722. if (!attributes) { return 0; }
  28723. var $patternFill = XmlNode('patternFill')
  28724. .attr('patternType', attributes.patternType || 'solid');
  28725. if (attributes.fgColor) {
  28726. var $fgColor = XmlNode('fgColor');
  28727. //Excel doesn't like it when we set both rgb and theme+tint, but xlsx.parseFile() sets both
  28728. //var $fgColor = createElement('<fgColor/>', null, null, {xmlMode: true}).attr(attributes.fgColor)
  28729. if (attributes.fgColor.rgb) {
  28730. if (attributes.fgColor.rgb.length == 6) {
  28731. attributes.fgColor.rgb = "FF" + attributes.fgColor.rgb /// add alpha to an RGB as Excel expects aRGB
  28732. }
  28733. $fgColor.attr('rgb', attributes.fgColor.rgb);
  28734. $patternFill.append($fgColor);
  28735. }
  28736. else if (attributes.fgColor.theme) {
  28737. $fgColor.attr('theme', attributes.fgColor.theme);
  28738. if (attributes.fgColor.tint) {
  28739. $fgColor.attr('tint', attributes.fgColor.tint);
  28740. }
  28741. $patternFill.append($fgColor);
  28742. }
  28743. if (!attributes.bgColor) {
  28744. attributes.bgColor = { "indexed": "64"}
  28745. }
  28746. }
  28747. if (attributes.bgColor) {
  28748. var $bgColor = XmlNode('bgColor').attr(attributes.bgColor);
  28749. $patternFill.append($bgColor);
  28750. }
  28751. var $fill = XmlNode('fill')
  28752. .append($patternFill);
  28753. this.$fills.append($fill);
  28754. var count = this.$fills.children().length;
  28755. this.$fills.attr('count', count);
  28756. return count - 1;
  28757. },
  28758. _getSubBorder: function(direction, spec) {
  28759. var $direction = XmlNode(direction);
  28760. if (spec){
  28761. if (spec.style) $direction.attr('style', spec.style);
  28762. if (spec.color) {
  28763. var $color = XmlNode('color');
  28764. if (spec.color.auto) {
  28765. $color.attr('auto', spec.color.auto);
  28766. }
  28767. else if (spec.color.rgb) {
  28768. $color.attr('rgb', spec.color.rgb);
  28769. }
  28770. else if (spec.color.theme || spec.color.tint) {
  28771. $color.attr('theme', spec.color.theme || "1");
  28772. $color.attr('tint', spec.color.tint || "0");
  28773. }
  28774. $direction.append($color)
  28775. }
  28776. }
  28777. return $direction;
  28778. },
  28779. _addBorder: function (attributes) {
  28780. if (!attributes) { return 0; }
  28781. var self = this;
  28782. var $border = XmlNode('border')
  28783. .attr("diagonalUp",attributes.diagonalUp)
  28784. .attr("diagonalDown",attributes.diagonalDown);
  28785. var directions = ["left","right","top","bottom","diagonal"];
  28786. directions.forEach(function(direction) {
  28787. $border.append(self._getSubBorder(direction, attributes[direction]))
  28788. });
  28789. this.$borders.append($border);
  28790. var count = this.$borders.children().length;
  28791. this.$borders.attr('count', count);
  28792. return count -1;
  28793. },
  28794. toXml: function () {
  28795. return this.$styles.toXml();
  28796. }
  28797. }.initialize(options||{});
  28798. }
  28799. /**
  28800. * 样式魔改区 -- end
  28801. */
  28802. XLSX.parse_xlscfb = parse_xlscfb;
  28803. XLSX.parse_ods = parse_ods;
  28804. XLSX.parse_fods = parse_fods;
  28805. XLSX.write_ods = write_ods;
  28806. XLSX.parse_zip = parse_zip;
  28807. XLSX.read = readSync; //xlsread
  28808. XLSX.readFile = readFileSync; //readFile
  28809. XLSX.readFileSync = readFileSync;
  28810. XLSX.write = writeSync;
  28811. XLSX.writeFile = writeFileSync;
  28812. XLSX.writeFileSync = writeFileSync;
  28813. XLSX.writeFileAsync = writeFileAsync;
  28814. XLSX.utils = utils;
  28815. XLSX.SSF = SSF;
  28816. XLSX.CFB = CFB;
  28817. }
  28818. /*global define */
  28819. if(typeof exports !== 'undefined') make_xlsx_lib(exports);
  28820. else if(typeof module !== 'undefined' && module.exports) make_xlsx_lib(module.exports);
  28821. else if(typeof define === 'function' && define.amd) define('xlsx', function() { if(!XLSX.version) make_xlsx_lib(XLSX); return XLSX; });
  28822. else make_xlsx_lib(XLSX);
  28823. /*exported XLS, ODS */
  28824. var XLS = XLSX, ODS = XLSX;/*---------split--------*//*
  28825. Copyright (c) 2010, Linden Research, Inc.
  28826. Copyright (c) 2014, Joshua Bell
  28827. Permission is hereby granted, free of charge, to any person obtaining a copy
  28828. of this software and associated documentation files (the "Software"), to deal
  28829. in the Software without restriction, including without limitation the rights
  28830. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  28831. copies of the Software, and to permit persons to whom the Software is
  28832. furnished to do so, subject to the following conditions:
  28833. The above copyright notice and this permission notice shall be included in
  28834. all copies or substantial portions of the Software.
  28835. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28836. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28837. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  28838. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28839. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28840. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28841. THE SOFTWARE.
  28842. $/LicenseInfo$
  28843. */
  28844. // Original can be found at:
  28845. // https://bitbucket.org/lindenlab/llsd
  28846. // Modifications by Joshua Bell inexorabletash@gmail.com
  28847. // https://github.com/inexorabletash/polyfill
  28848. // ES3/ES5 implementation of the Krhonos Typed Array Specification
  28849. // Ref: http://www.khronos.org/registry/typedarray/specs/latest/
  28850. // Date: 2011-02-01
  28851. //
  28852. // Variations:
  28853. // * Allows typed_array.get/set() as alias for subscripts (typed_array[])
  28854. // * Gradually migrating structure from Khronos spec to ES2015 spec
  28855. //
  28856. // Caveats:
  28857. // * Beyond 10000 or so entries, polyfilled array accessors (ta[0],
  28858. // etc) become memory-prohibitive, so array creation will fail. Set
  28859. // self.TYPED_ARRAY_POLYFILL_NO_ARRAY_ACCESSORS=true to disable
  28860. // creation of accessors. Your code will need to use the
  28861. // non-standard get()/set() instead, and will need to add those to
  28862. // native arrays for interop.
  28863. (function(global) {
  28864. 'use strict';
  28865. var undefined = (void 0); // Paranoia
  28866. // Beyond this value, index getters/setters (i.e. array[0], array[1]) are so slow to
  28867. // create, and consume so much memory, that the browser appears frozen.
  28868. var MAX_ARRAY_LENGTH = 1e5;
  28869. // Approximations of internal ECMAScript conversion functions
  28870. function Type(v) {
  28871. switch(typeof v) {
  28872. case 'undefined': return 'undefined';
  28873. case 'boolean': return 'boolean';
  28874. case 'number': return 'number';
  28875. case 'string': return 'string';
  28876. default: return v === null ? 'null' : 'object';
  28877. }
  28878. }
  28879. // Class returns internal [[Class]] property, used to avoid cross-frame instanceof issues:
  28880. function Class(v) { return Object.prototype.toString.call(v).replace(/^\[object *|\]$/g, ''); }
  28881. function IsCallable(o) { return typeof o === 'function'; }
  28882. function ToObject(v) {
  28883. if (v === null || v === undefined) throw TypeError();
  28884. return Object(v);
  28885. }
  28886. function ToInt32(v) { return v >> 0; }
  28887. function ToUint32(v) { return v >>> 0; }
  28888. // Snapshot intrinsics
  28889. var LN2 = Math.LN2,
  28890. abs = Math.abs,
  28891. floor = Math.floor,
  28892. log = Math.log,
  28893. max = Math.max,
  28894. min = Math.min,
  28895. pow = Math.pow,
  28896. round = Math.round;
  28897. // emulate ES5 getter/setter API using legacy APIs
  28898. // http://blogs.msdn.com/b/ie/archive/2010/09/07/transitioning-existing-code-to-the-es5-getter-setter-apis.aspx
  28899. // (second clause tests for Object.defineProperty() in IE<9 that only supports extending DOM prototypes, but
  28900. // note that IE<9 does not support __defineGetter__ or __defineSetter__ so it just renders the method harmless)
  28901. (function() {
  28902. var orig = Object.defineProperty;
  28903. var dom_only = !(function(){try{return Object.defineProperty({},'x',{});}catch(_){return false;}}());
  28904. if (!orig || dom_only) {
  28905. Object.defineProperty = function (o, prop, desc) {
  28906. // In IE8 try built-in implementation for defining properties on DOM prototypes.
  28907. if (orig)
  28908. try { return orig(o, prop, desc); } catch (_) {}
  28909. if (o !== Object(o))
  28910. throw TypeError('Object.defineProperty called on non-object');
  28911. if (Object.prototype.__defineGetter__ && ('get' in desc))
  28912. Object.prototype.__defineGetter__.call(o, prop, desc.get);
  28913. if (Object.prototype.__defineSetter__ && ('set' in desc))
  28914. Object.prototype.__defineSetter__.call(o, prop, desc.set);
  28915. if ('value' in desc)
  28916. o[prop] = desc.value;
  28917. return o;
  28918. };
  28919. }
  28920. }());
  28921. // ES5: Make obj[index] an alias for obj._getter(index)/obj._setter(index, value)
  28922. // for index in 0 ... obj.length
  28923. function makeArrayAccessors(obj) {
  28924. if ('TYPED_ARRAY_POLYFILL_NO_ARRAY_ACCESSORS' in global)
  28925. return;
  28926. if (obj.length > MAX_ARRAY_LENGTH) throw RangeError('Array too large for polyfill');
  28927. function makeArrayAccessor(index) {
  28928. Object.defineProperty(obj, index, {
  28929. 'get': function() { return obj._getter(index); },
  28930. 'set': function(v) { obj._setter(index, v); },
  28931. enumerable: true,
  28932. configurable: false
  28933. });
  28934. }
  28935. var i;
  28936. for (i = 0; i < obj.length; i += 1) {
  28937. makeArrayAccessor(i);
  28938. }
  28939. }
  28940. // Internal conversion functions:
  28941. // pack<Type>() - take a number (interpreted as Type), output a byte array
  28942. // unpack<Type>() - take a byte array, output a Type-like number
  28943. function as_signed(value, bits) { var s = 32 - bits; return (value << s) >> s; }
  28944. function as_unsigned(value, bits) { var s = 32 - bits; return (value << s) >>> s; }
  28945. function packI8(n) { return [n & 0xff]; }
  28946. function unpackI8(bytes) { return as_signed(bytes[0], 8); }
  28947. function packU8(n) { return [n & 0xff]; }
  28948. function unpackU8(bytes) { return as_unsigned(bytes[0], 8); }
  28949. function packU8Clamped(n) { n = round(Number(n)); return [n < 0 ? 0 : n > 0xff ? 0xff : n & 0xff]; }
  28950. function packI16(n) { return [n & 0xff, (n >> 8) & 0xff]; }
  28951. function unpackI16(bytes) { return as_signed(bytes[1] << 8 | bytes[0], 16); }
  28952. function packU16(n) { return [n & 0xff, (n >> 8) & 0xff]; }
  28953. function unpackU16(bytes) { return as_unsigned(bytes[1] << 8 | bytes[0], 16); }
  28954. function packI32(n) { return [n & 0xff, (n >> 8) & 0xff, (n >> 16) & 0xff, (n >> 24) & 0xff]; }
  28955. function unpackI32(bytes) { return as_signed(bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0], 32); }
  28956. function packU32(n) { return [n & 0xff, (n >> 8) & 0xff, (n >> 16) & 0xff, (n >> 24) & 0xff]; }
  28957. function unpackU32(bytes) { return as_unsigned(bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0], 32); }
  28958. function packIEEE754(v, ebits, fbits) {
  28959. var bias = (1 << (ebits - 1)) - 1;
  28960. function roundToEven(n) {
  28961. var w = floor(n), f = n - w;
  28962. if (f < 0.5)
  28963. return w;
  28964. if (f > 0.5)
  28965. return w + 1;
  28966. return w % 2 ? w + 1 : w;
  28967. }
  28968. // Compute sign, exponent, fraction
  28969. var s, e, f;
  28970. if (v !== v) {
  28971. // NaN
  28972. // http://dev.w3.org/2006/webapi/WebIDL/#es-type-mapping
  28973. e = (1 << ebits) - 1; f = pow(2, fbits - 1); s = 0;
  28974. } else if (v === Infinity || v === -Infinity) {
  28975. e = (1 << ebits) - 1; f = 0; s = (v < 0) ? 1 : 0;
  28976. } else if (v === 0) {
  28977. e = 0; f = 0; s = (1 / v === -Infinity) ? 1 : 0;
  28978. } else {
  28979. s = v < 0;
  28980. v = abs(v);
  28981. if (v >= pow(2, 1 - bias)) {
  28982. // Normalized
  28983. e = min(floor(log(v) / LN2), 1023);
  28984. var significand = v / pow(2, e);
  28985. if (significand < 1) {
  28986. e -= 1;
  28987. significand *= 2;
  28988. }
  28989. if (significand >= 2) {
  28990. e += 1;
  28991. significand /= 2;
  28992. }
  28993. var d = pow(2, fbits);
  28994. f = roundToEven(significand * d) - d;
  28995. e += bias;
  28996. if (f / d >= 1) {
  28997. e += 1;
  28998. f = 0;
  28999. }
  29000. if (e > 2 * bias) {
  29001. // Overflow
  29002. e = (1 << ebits) - 1;
  29003. f = 0;
  29004. }
  29005. } else {
  29006. // Denormalized
  29007. e = 0;
  29008. f = roundToEven(v / pow(2, 1 - bias - fbits));
  29009. }
  29010. }
  29011. // Pack sign, exponent, fraction
  29012. var bits = [], i;
  29013. for (i = fbits; i; i -= 1) { bits.push(f % 2 ? 1 : 0); f = floor(f / 2); }
  29014. for (i = ebits; i; i -= 1) { bits.push(e % 2 ? 1 : 0); e = floor(e / 2); }
  29015. bits.push(s ? 1 : 0);
  29016. bits.reverse();
  29017. var str = bits.join('');
  29018. // Bits to bytes
  29019. var bytes = [];
  29020. while (str.length) {
  29021. bytes.unshift(parseInt(str.substring(0, 8), 2));
  29022. str = str.substring(8);
  29023. }
  29024. return bytes;
  29025. }
  29026. function unpackIEEE754(bytes, ebits, fbits) {
  29027. // Bytes to bits
  29028. var bits = [], i, j, b, str,
  29029. bias, s, e, f;
  29030. for (i = 0; i < bytes.length; ++i) {
  29031. b = bytes[i];
  29032. for (j = 8; j; j -= 1) {
  29033. bits.push(b % 2 ? 1 : 0); b = b >> 1;
  29034. }
  29035. }
  29036. bits.reverse();
  29037. str = bits.join('');
  29038. // Unpack sign, exponent, fraction
  29039. bias = (1 << (ebits - 1)) - 1;
  29040. s = parseInt(str.substring(0, 1), 2) ? -1 : 1;
  29041. e = parseInt(str.substring(1, 1 + ebits), 2);
  29042. f = parseInt(str.substring(1 + ebits), 2);
  29043. // Produce number
  29044. if (e === (1 << ebits) - 1) {
  29045. return f !== 0 ? NaN : s * Infinity;
  29046. } else if (e > 0) {
  29047. // Normalized
  29048. return s * pow(2, e - bias) * (1 + f / pow(2, fbits));
  29049. } else if (f !== 0) {
  29050. // Denormalized
  29051. return s * pow(2, -(bias - 1)) * (f / pow(2, fbits));
  29052. } else {
  29053. return s < 0 ? -0 : 0;
  29054. }
  29055. }
  29056. function unpackF64(b) { return unpackIEEE754(b, 11, 52); }
  29057. function packF64(v) { return packIEEE754(v, 11, 52); }
  29058. function unpackF32(b) { return unpackIEEE754(b, 8, 23); }
  29059. function packF32(v) { return packIEEE754(v, 8, 23); }
  29060. //
  29061. // 3 The ArrayBuffer Type
  29062. //
  29063. (function() {
  29064. function ArrayBuffer(length) {
  29065. length = ToInt32(length);
  29066. if (length < 0) throw RangeError('ArrayBuffer size is not a small enough positive integer.');
  29067. Object.defineProperty(this, 'byteLength', {value: length});
  29068. Object.defineProperty(this, '_bytes', {value: Array(length)});
  29069. for (var i = 0; i < length; i += 1)
  29070. this._bytes[i] = 0;
  29071. }
  29072. global.ArrayBuffer = global.ArrayBuffer || ArrayBuffer;
  29073. //
  29074. // 5 The Typed Array View Types
  29075. //
  29076. function $TypedArray$() {
  29077. // %TypedArray% ( length )
  29078. if (!arguments.length || typeof arguments[0] !== 'object') {
  29079. return (function(length) {
  29080. length = ToInt32(length);
  29081. if (length < 0) throw RangeError('length is not a small enough positive integer.');
  29082. Object.defineProperty(this, 'length', {value: length});
  29083. Object.defineProperty(this, 'byteLength', {value: length * this.BYTES_PER_ELEMENT});
  29084. Object.defineProperty(this, 'buffer', {value: new ArrayBuffer(this.byteLength)});
  29085. Object.defineProperty(this, 'byteOffset', {value: 0});
  29086. }).apply(this, arguments);
  29087. }
  29088. // %TypedArray% ( typedArray )
  29089. if (arguments.length >= 1 &&
  29090. Type(arguments[0]) === 'object' &&
  29091. arguments[0] instanceof $TypedArray$) {
  29092. return (function(typedArray){
  29093. if (this.constructor !== typedArray.constructor) throw TypeError();
  29094. var byteLength = typedArray.length * this.BYTES_PER_ELEMENT;
  29095. Object.defineProperty(this, 'buffer', {value: new ArrayBuffer(byteLength)});
  29096. Object.defineProperty(this, 'byteLength', {value: byteLength});
  29097. Object.defineProperty(this, 'byteOffset', {value: 0});
  29098. Object.defineProperty(this, 'length', {value: typedArray.length});
  29099. for (var i = 0; i < this.length; i += 1)
  29100. this._setter(i, typedArray._getter(i));
  29101. }).apply(this, arguments);
  29102. }
  29103. // %TypedArray% ( array )
  29104. if (arguments.length >= 1 &&
  29105. Type(arguments[0]) === 'object' &&
  29106. !(arguments[0] instanceof $TypedArray$) &&
  29107. !(arguments[0] instanceof ArrayBuffer || Class(arguments[0]) === 'ArrayBuffer')) {
  29108. return (function(array) {
  29109. var byteLength = array.length * this.BYTES_PER_ELEMENT;
  29110. Object.defineProperty(this, 'buffer', {value: new ArrayBuffer(byteLength)});
  29111. Object.defineProperty(this, 'byteLength', {value: byteLength});
  29112. Object.defineProperty(this, 'byteOffset', {value: 0});
  29113. Object.defineProperty(this, 'length', {value: array.length});
  29114. for (var i = 0; i < this.length; i += 1) {
  29115. var s = array[i];
  29116. this._setter(i, Number(s));
  29117. }
  29118. }).apply(this, arguments);
  29119. }
  29120. // %TypedArray% ( buffer, byteOffset=0, length=undefined )
  29121. if (arguments.length >= 1 &&
  29122. Type(arguments[0]) === 'object' &&
  29123. (arguments[0] instanceof ArrayBuffer || Class(arguments[0]) === 'ArrayBuffer')) {
  29124. return (function(buffer, byteOffset, length) {
  29125. byteOffset = ToUint32(byteOffset);
  29126. if (byteOffset > buffer.byteLength)
  29127. throw RangeError('byteOffset out of range');
  29128. // The given byteOffset must be a multiple of the element
  29129. // size of the specific type, otherwise an exception is raised.
  29130. if (byteOffset % this.BYTES_PER_ELEMENT)
  29131. throw RangeError('buffer length minus the byteOffset is not a multiple of the element size.');
  29132. if (length === undefined) {
  29133. var byteLength = buffer.byteLength - byteOffset;
  29134. if (byteLength % this.BYTES_PER_ELEMENT)
  29135. throw RangeError('length of buffer minus byteOffset not a multiple of the element size');
  29136. length = byteLength / this.BYTES_PER_ELEMENT;
  29137. } else {
  29138. length = ToUint32(length);
  29139. byteLength = length * this.BYTES_PER_ELEMENT;
  29140. }
  29141. if ((byteOffset + byteLength) > buffer.byteLength)
  29142. throw RangeError('byteOffset and length reference an area beyond the end of the buffer');
  29143. Object.defineProperty(this, 'buffer', {value: buffer});
  29144. Object.defineProperty(this, 'byteLength', {value: byteLength});
  29145. Object.defineProperty(this, 'byteOffset', {value: byteOffset});
  29146. Object.defineProperty(this, 'length', {value: length});
  29147. }).apply(this, arguments);
  29148. }
  29149. // %TypedArray% ( all other argument combinations )
  29150. throw TypeError();
  29151. }
  29152. // Properties of the %TypedArray Instrinsic Object
  29153. // %TypedArray%.from ( source , mapfn=undefined, thisArg=undefined )
  29154. Object.defineProperty($TypedArray$, 'from', {value: function(iterable) {
  29155. return new this(iterable);
  29156. }});
  29157. // %TypedArray%.of ( ...items )
  29158. Object.defineProperty($TypedArray$, 'of', {value: function(/*...items*/) {
  29159. return new this(arguments);
  29160. }});
  29161. // %TypedArray%.prototype
  29162. var $TypedArrayPrototype$ = {};
  29163. $TypedArray$.prototype = $TypedArrayPrototype$;
  29164. // WebIDL: getter type (unsigned long index);
  29165. Object.defineProperty($TypedArray$.prototype, '_getter', {value: function(index) {
  29166. if (arguments.length < 1) throw SyntaxError('Not enough arguments');
  29167. index = ToUint32(index);
  29168. if (index >= this.length)
  29169. return undefined;
  29170. var bytes = [], i, o;
  29171. for (i = 0, o = this.byteOffset + index * this.BYTES_PER_ELEMENT;
  29172. i < this.BYTES_PER_ELEMENT;
  29173. i += 1, o += 1) {
  29174. bytes.push(this.buffer._bytes[o]);
  29175. }
  29176. return this._unpack(bytes);
  29177. }});
  29178. // NONSTANDARD: convenience alias for getter: type get(unsigned long index);
  29179. Object.defineProperty($TypedArray$.prototype, 'get', {value: $TypedArray$.prototype._getter});
  29180. // WebIDL: setter void (unsigned long index, type value);
  29181. Object.defineProperty($TypedArray$.prototype, '_setter', {value: function(index, value) {
  29182. if (arguments.length < 2) throw SyntaxError('Not enough arguments');
  29183. index = ToUint32(index);
  29184. if (index >= this.length)
  29185. return;
  29186. var bytes = this._pack(value), i, o;
  29187. for (i = 0, o = this.byteOffset + index * this.BYTES_PER_ELEMENT;
  29188. i < this.BYTES_PER_ELEMENT;
  29189. i += 1, o += 1) {
  29190. this.buffer._bytes[o] = bytes[i];
  29191. }
  29192. }});
  29193. // get %TypedArray%.prototype.buffer
  29194. // get %TypedArray%.prototype.byteLength
  29195. // get %TypedArray%.prototype.byteOffset
  29196. // -- applied directly to the object in the constructor
  29197. // %TypedArray%.prototype.constructor
  29198. Object.defineProperty($TypedArray$.prototype, 'constructor', {value: $TypedArray$});
  29199. // %TypedArray%.prototype.copyWithin (target, start, end = this.length )
  29200. Object.defineProperty($TypedArray$.prototype, 'copyWithin', {value: function(target, start) {
  29201. var end = arguments[2];
  29202. var o = ToObject(this);
  29203. var lenVal = o.length;
  29204. var len = ToUint32(lenVal);
  29205. len = max(len, 0);
  29206. var relativeTarget = ToInt32(target);
  29207. var to;
  29208. if (relativeTarget < 0)
  29209. to = max(len + relativeTarget, 0);
  29210. else
  29211. to = min(relativeTarget, len);
  29212. var relativeStart = ToInt32(start);
  29213. var from;
  29214. if (relativeStart < 0)
  29215. from = max(len + relativeStart, 0);
  29216. else
  29217. from = min(relativeStart, len);
  29218. var relativeEnd;
  29219. if (end === undefined)
  29220. relativeEnd = len;
  29221. else
  29222. relativeEnd = ToInt32(end);
  29223. var final;
  29224. if (relativeEnd < 0)
  29225. final = max(len + relativeEnd, 0);
  29226. else
  29227. final = min(relativeEnd, len);
  29228. var count = min(final - from, len - to);
  29229. var direction;
  29230. if (from < to && to < from + count) {
  29231. direction = -1;
  29232. from = from + count - 1;
  29233. to = to + count - 1;
  29234. } else {
  29235. direction = 1;
  29236. }
  29237. while (count > 0) {
  29238. o._setter(to, o._getter(from));
  29239. from = from + direction;
  29240. to = to + direction;
  29241. count = count - 1;
  29242. }
  29243. return o;
  29244. }});
  29245. // %TypedArray%.prototype.entries ( )
  29246. // -- defined in es6.js to shim browsers w/ native TypedArrays
  29247. // %TypedArray%.prototype.every ( callbackfn, thisArg = undefined )
  29248. Object.defineProperty($TypedArray$.prototype, 'every', {value: function(callbackfn) {
  29249. if (this === undefined || this === null) throw TypeError();
  29250. var t = Object(this);
  29251. var len = ToUint32(t.length);
  29252. if (!IsCallable(callbackfn)) throw TypeError();
  29253. var thisArg = arguments[1];
  29254. for (var i = 0; i < len; i++) {
  29255. if (!callbackfn.call(thisArg, t._getter(i), i, t))
  29256. return false;
  29257. }
  29258. return true;
  29259. }});
  29260. // %TypedArray%.prototype.fill (value, start = 0, end = this.length )
  29261. Object.defineProperty($TypedArray$.prototype, 'fill', {value: function(value) {
  29262. var start = arguments[1],
  29263. end = arguments[2];
  29264. var o = ToObject(this);
  29265. var lenVal = o.length;
  29266. var len = ToUint32(lenVal);
  29267. len = max(len, 0);
  29268. var relativeStart = ToInt32(start);
  29269. var k;
  29270. if (relativeStart < 0)
  29271. k = max((len + relativeStart), 0);
  29272. else
  29273. k = min(relativeStart, len);
  29274. var relativeEnd;
  29275. if (end === undefined)
  29276. relativeEnd = len;
  29277. else
  29278. relativeEnd = ToInt32(end);
  29279. var final;
  29280. if (relativeEnd < 0)
  29281. final = max((len + relativeEnd), 0);
  29282. else
  29283. final = min(relativeEnd, len);
  29284. while (k < final) {
  29285. o._setter(k, value);
  29286. k += 1;
  29287. }
  29288. return o;
  29289. }});
  29290. // %TypedArray%.prototype.filter ( callbackfn, thisArg = undefined )
  29291. Object.defineProperty($TypedArray$.prototype, 'filter', {value: function(callbackfn) {
  29292. if (this === undefined || this === null) throw TypeError();
  29293. var t = Object(this);
  29294. var len = ToUint32(t.length);
  29295. if (!IsCallable(callbackfn)) throw TypeError();
  29296. var res = [];
  29297. var thisp = arguments[1];
  29298. for (var i = 0; i < len; i++) {
  29299. var val = t._getter(i); // in case fun mutates this
  29300. if (callbackfn.call(thisp, val, i, t))
  29301. res.push(val);
  29302. }
  29303. return new this.constructor(res);
  29304. }});
  29305. // %TypedArray%.prototype.find (predicate, thisArg = undefined)
  29306. Object.defineProperty($TypedArray$.prototype, 'find', {value: function(predicate) {
  29307. var o = ToObject(this);
  29308. var lenValue = o.length;
  29309. var len = ToUint32(lenValue);
  29310. if (!IsCallable(predicate)) throw TypeError();
  29311. var t = arguments.length > 1 ? arguments[1] : undefined;
  29312. var k = 0;
  29313. while (k < len) {
  29314. var kValue = o._getter(k);
  29315. var testResult = predicate.call(t, kValue, k, o);
  29316. if (Boolean(testResult))
  29317. return kValue;
  29318. ++k;
  29319. }
  29320. return undefined;
  29321. }});
  29322. // %TypedArray%.prototype.findIndex ( predicate, thisArg = undefined )
  29323. Object.defineProperty($TypedArray$.prototype, 'findIndex', {value: function(predicate) {
  29324. var o = ToObject(this);
  29325. var lenValue = o.length;
  29326. var len = ToUint32(lenValue);
  29327. if (!IsCallable(predicate)) throw TypeError();
  29328. var t = arguments.length > 1 ? arguments[1] : undefined;
  29329. var k = 0;
  29330. while (k < len) {
  29331. var kValue = o._getter(k);
  29332. var testResult = predicate.call(t, kValue, k, o);
  29333. if (Boolean(testResult))
  29334. return k;
  29335. ++k;
  29336. }
  29337. return -1;
  29338. }});
  29339. // %TypedArray%.prototype.forEach ( callbackfn, thisArg = undefined )
  29340. Object.defineProperty($TypedArray$.prototype, 'forEach', {value: function(callbackfn) {
  29341. if (this === undefined || this === null) throw TypeError();
  29342. var t = Object(this);
  29343. var len = ToUint32(t.length);
  29344. if (!IsCallable(callbackfn)) throw TypeError();
  29345. var thisp = arguments[1];
  29346. for (var i = 0; i < len; i++)
  29347. callbackfn.call(thisp, t._getter(i), i, t);
  29348. }});
  29349. // %TypedArray%.prototype.indexOf (searchElement, fromIndex = 0 )
  29350. Object.defineProperty($TypedArray$.prototype, 'indexOf', {value: function(searchElement) {
  29351. if (this === undefined || this === null) throw TypeError();
  29352. var t = Object(this);
  29353. var len = ToUint32(t.length);
  29354. if (len === 0) return -1;
  29355. var n = 0;
  29356. if (arguments.length > 0) {
  29357. n = Number(arguments[1]);
  29358. if (n !== n) {
  29359. n = 0;
  29360. } else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) {
  29361. n = (n > 0 || -1) * floor(abs(n));
  29362. }
  29363. }
  29364. if (n >= len) return -1;
  29365. var k = n >= 0 ? n : max(len - abs(n), 0);
  29366. for (; k < len; k++) {
  29367. if (t._getter(k) === searchElement) {
  29368. return k;
  29369. }
  29370. }
  29371. return -1;
  29372. }});
  29373. // %TypedArray%.prototype.join ( separator )
  29374. Object.defineProperty($TypedArray$.prototype, 'join', {value: function(separator) {
  29375. if (this === undefined || this === null) throw TypeError();
  29376. var t = Object(this);
  29377. var len = ToUint32(t.length);
  29378. var tmp = Array(len);
  29379. for (var i = 0; i < len; ++i)
  29380. tmp[i] = t._getter(i);
  29381. return tmp.join(separator === undefined ? ',' : separator); // Hack for IE7
  29382. }});
  29383. // %TypedArray%.prototype.keys ( )
  29384. // -- defined in es6.js to shim browsers w/ native TypedArrays
  29385. // %TypedArray%.prototype.lastIndexOf ( searchElement, fromIndex = this.length-1 )
  29386. Object.defineProperty($TypedArray$.prototype, 'lastIndexOf', {value: function(searchElement) {
  29387. if (this === undefined || this === null) throw TypeError();
  29388. var t = Object(this);
  29389. var len = ToUint32(t.length);
  29390. if (len === 0) return -1;
  29391. var n = len;
  29392. if (arguments.length > 1) {
  29393. n = Number(arguments[1]);
  29394. if (n !== n) {
  29395. n = 0;
  29396. } else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) {
  29397. n = (n > 0 || -1) * floor(abs(n));
  29398. }
  29399. }
  29400. var k = n >= 0 ? min(n, len - 1) : len - abs(n);
  29401. for (; k >= 0; k--) {
  29402. if (t._getter(k) === searchElement)
  29403. return k;
  29404. }
  29405. return -1;
  29406. }});
  29407. // get %TypedArray%.prototype.length
  29408. // -- applied directly to the object in the constructor
  29409. // %TypedArray%.prototype.map ( callbackfn, thisArg = undefined )
  29410. Object.defineProperty($TypedArray$.prototype, 'map', {value: function(callbackfn) {
  29411. if (this === undefined || this === null) throw TypeError();
  29412. var t = Object(this);
  29413. var len = ToUint32(t.length);
  29414. if (!IsCallable(callbackfn)) throw TypeError();
  29415. var res = []; res.length = len;
  29416. var thisp = arguments[1];
  29417. for (var i = 0; i < len; i++)
  29418. res[i] = callbackfn.call(thisp, t._getter(i), i, t);
  29419. return new this.constructor(res);
  29420. }});
  29421. // %TypedArray%.prototype.reduce ( callbackfn [, initialValue] )
  29422. Object.defineProperty($TypedArray$.prototype, 'reduce', {value: function(callbackfn) {
  29423. if (this === undefined || this === null) throw TypeError();
  29424. var t = Object(this);
  29425. var len = ToUint32(t.length);
  29426. if (!IsCallable(callbackfn)) throw TypeError();
  29427. // no value to return if no initial value and an empty array
  29428. if (len === 0 && arguments.length === 1) throw TypeError();
  29429. var k = 0;
  29430. var accumulator;
  29431. if (arguments.length >= 2) {
  29432. accumulator = arguments[1];
  29433. } else {
  29434. accumulator = t._getter(k++);
  29435. }
  29436. while (k < len) {
  29437. accumulator = callbackfn.call(undefined, accumulator, t._getter(k), k, t);
  29438. k++;
  29439. }
  29440. return accumulator;
  29441. }});
  29442. // %TypedArray%.prototype.reduceRight ( callbackfn [, initialValue] )
  29443. Object.defineProperty($TypedArray$.prototype, 'reduceRight', {value: function(callbackfn) {
  29444. if (this === undefined || this === null) throw TypeError();
  29445. var t = Object(this);
  29446. var len = ToUint32(t.length);
  29447. if (!IsCallable(callbackfn)) throw TypeError();
  29448. // no value to return if no initial value, empty array
  29449. if (len === 0 && arguments.length === 1) throw TypeError();
  29450. var k = len - 1;
  29451. var accumulator;
  29452. if (arguments.length >= 2) {
  29453. accumulator = arguments[1];
  29454. } else {
  29455. accumulator = t._getter(k--);
  29456. }
  29457. while (k >= 0) {
  29458. accumulator = callbackfn.call(undefined, accumulator, t._getter(k), k, t);
  29459. k--;
  29460. }
  29461. return accumulator;
  29462. }});
  29463. // %TypedArray%.prototype.reverse ( )
  29464. Object.defineProperty($TypedArray$.prototype, 'reverse', {value: function() {
  29465. if (this === undefined || this === null) throw TypeError();
  29466. var t = Object(this);
  29467. var len = ToUint32(t.length);
  29468. var half = floor(len / 2);
  29469. for (var i = 0, j = len - 1; i < half; ++i, --j) {
  29470. var tmp = t._getter(i);
  29471. t._setter(i, t._getter(j));
  29472. t._setter(j, tmp);
  29473. }
  29474. return t;
  29475. }});
  29476. // %TypedArray%.prototype.set(array, offset = 0 )
  29477. // %TypedArray%.prototype.set(typedArray, offset = 0 )
  29478. // WebIDL: void set(TypedArray array, optional unsigned long offset);
  29479. // WebIDL: void set(sequence<type> array, optional unsigned long offset);
  29480. Object.defineProperty($TypedArray$.prototype, 'set', {value: function(index, value) {
  29481. if (arguments.length < 1) throw SyntaxError('Not enough arguments');
  29482. var array, sequence, offset, len,
  29483. i, s, d,
  29484. byteOffset, byteLength, tmp;
  29485. if (typeof arguments[0] === 'object' && arguments[0].constructor === this.constructor) {
  29486. // void set(TypedArray array, optional unsigned long offset);
  29487. array = arguments[0];
  29488. offset = ToUint32(arguments[1]);
  29489. if (offset + array.length > this.length) {
  29490. throw RangeError('Offset plus length of array is out of range');
  29491. }
  29492. byteOffset = this.byteOffset + offset * this.BYTES_PER_ELEMENT;
  29493. byteLength = array.length * this.BYTES_PER_ELEMENT;
  29494. if (array.buffer === this.buffer) {
  29495. tmp = [];
  29496. for (i = 0, s = array.byteOffset; i < byteLength; i += 1, s += 1) {
  29497. tmp[i] = array.buffer._bytes[s];
  29498. }
  29499. for (i = 0, d = byteOffset; i < byteLength; i += 1, d += 1) {
  29500. this.buffer._bytes[d] = tmp[i];
  29501. }
  29502. } else {
  29503. for (i = 0, s = array.byteOffset, d = byteOffset;
  29504. i < byteLength; i += 1, s += 1, d += 1) {
  29505. this.buffer._bytes[d] = array.buffer._bytes[s];
  29506. }
  29507. }
  29508. } else if (typeof arguments[0] === 'object' && typeof arguments[0].length !== 'undefined') {
  29509. // void set(sequence<type> array, optional unsigned long offset);
  29510. sequence = arguments[0];
  29511. len = ToUint32(sequence.length);
  29512. offset = ToUint32(arguments[1]);
  29513. if (offset + len > this.length) {
  29514. throw RangeError('Offset plus length of array is out of range');
  29515. }
  29516. for (i = 0; i < len; i += 1) {
  29517. s = sequence[i];
  29518. this._setter(offset + i, Number(s));
  29519. }
  29520. } else {
  29521. throw TypeError('Unexpected argument type(s)');
  29522. }
  29523. }});
  29524. // %TypedArray%.prototype.slice ( start, end )
  29525. Object.defineProperty($TypedArray$.prototype, 'slice', {value: function(start, end) {
  29526. var o = ToObject(this);
  29527. var lenVal = o.length;
  29528. var len = ToUint32(lenVal);
  29529. var relativeStart = ToInt32(start);
  29530. var k = (relativeStart < 0) ? max(len + relativeStart, 0) : min(relativeStart, len);
  29531. var relativeEnd = (end === undefined) ? len : ToInt32(end);
  29532. var final = (relativeEnd < 0) ? max(len + relativeEnd, 0) : min(relativeEnd, len);
  29533. var count = final - k;
  29534. var c = o.constructor;
  29535. var a = new c(count);
  29536. var n = 0;
  29537. while (k < final) {
  29538. var kValue = o._getter(k);
  29539. a._setter(n, kValue);
  29540. ++k;
  29541. ++n;
  29542. }
  29543. return a;
  29544. }});
  29545. // %TypedArray%.prototype.some ( callbackfn, thisArg = undefined )
  29546. Object.defineProperty($TypedArray$.prototype, 'some', {value: function(callbackfn) {
  29547. if (this === undefined || this === null) throw TypeError();
  29548. var t = Object(this);
  29549. var len = ToUint32(t.length);
  29550. if (!IsCallable(callbackfn)) throw TypeError();
  29551. var thisp = arguments[1];
  29552. for (var i = 0; i < len; i++) {
  29553. if (callbackfn.call(thisp, t._getter(i), i, t)) {
  29554. return true;
  29555. }
  29556. }
  29557. return false;
  29558. }});
  29559. // %TypedArray%.prototype.sort ( comparefn )
  29560. Object.defineProperty($TypedArray$.prototype, 'sort', {value: function(comparefn) {
  29561. if (this === undefined || this === null) throw TypeError();
  29562. var t = Object(this);
  29563. var len = ToUint32(t.length);
  29564. var tmp = Array(len);
  29565. for (var i = 0; i < len; ++i)
  29566. tmp[i] = t._getter(i);
  29567. function sortCompare(x, y) {
  29568. if (x !== x && y !== y) return +0;
  29569. if (x !== x) return 1;
  29570. if (y !== y) return -1;
  29571. if (comparefn !== undefined) {
  29572. return comparefn(x, y);
  29573. }
  29574. if (x < y) return -1;
  29575. if (x > y) return 1;
  29576. return +0;
  29577. }
  29578. tmp.sort(sortCompare);
  29579. for (i = 0; i < len; ++i)
  29580. t._setter(i, tmp[i]);
  29581. return t;
  29582. }});
  29583. // %TypedArray%.prototype.subarray(begin = 0, end = this.length )
  29584. // WebIDL: TypedArray subarray(long begin, optional long end);
  29585. Object.defineProperty($TypedArray$.prototype, 'subarray', {value: function(start, end) {
  29586. function clamp(v, min, max) { return v < min ? min : v > max ? max : v; }
  29587. start = ToInt32(start);
  29588. end = ToInt32(end);
  29589. if (arguments.length < 1) { start = 0; }
  29590. if (arguments.length < 2) { end = this.length; }
  29591. if (start < 0) { start = this.length + start; }
  29592. if (end < 0) { end = this.length + end; }
  29593. start = clamp(start, 0, this.length);
  29594. end = clamp(end, 0, this.length);
  29595. var len = end - start;
  29596. if (len < 0) {
  29597. len = 0;
  29598. }
  29599. return new this.constructor(
  29600. this.buffer, this.byteOffset + start * this.BYTES_PER_ELEMENT, len);
  29601. }});
  29602. // %TypedArray%.prototype.toLocaleString ( )
  29603. // %TypedArray%.prototype.toString ( )
  29604. // %TypedArray%.prototype.values ( )
  29605. // %TypedArray%.prototype [ @@iterator ] ( )
  29606. // get %TypedArray%.prototype [ @@toStringTag ]
  29607. // -- defined in es6.js to shim browsers w/ native TypedArrays
  29608. function makeTypedArray(elementSize, pack, unpack) {
  29609. // Each TypedArray type requires a distinct constructor instance with
  29610. // identical logic, which this produces.
  29611. var TypedArray = function() {
  29612. Object.defineProperty(this, 'constructor', {value: TypedArray});
  29613. $TypedArray$.apply(this, arguments);
  29614. makeArrayAccessors(this);
  29615. };
  29616. if ('__proto__' in TypedArray) {
  29617. TypedArray.__proto__ = $TypedArray$;
  29618. } else {
  29619. TypedArray.from = $TypedArray$.from;
  29620. TypedArray.of = $TypedArray$.of;
  29621. }
  29622. TypedArray.BYTES_PER_ELEMENT = elementSize;
  29623. var TypedArrayPrototype = function() {};
  29624. TypedArrayPrototype.prototype = $TypedArrayPrototype$;
  29625. TypedArray.prototype = new TypedArrayPrototype();
  29626. Object.defineProperty(TypedArray.prototype, 'BYTES_PER_ELEMENT', {value: elementSize});
  29627. Object.defineProperty(TypedArray.prototype, '_pack', {value: pack});
  29628. Object.defineProperty(TypedArray.prototype, '_unpack', {value: unpack});
  29629. return TypedArray;
  29630. }
  29631. var Int8Array = makeTypedArray(1, packI8, unpackI8);
  29632. var Uint8Array = makeTypedArray(1, packU8, unpackU8);
  29633. var Uint8ClampedArray = makeTypedArray(1, packU8Clamped, unpackU8);
  29634. var Int16Array = makeTypedArray(2, packI16, unpackI16);
  29635. var Uint16Array = makeTypedArray(2, packU16, unpackU16);
  29636. var Int32Array = makeTypedArray(4, packI32, unpackI32);
  29637. var Uint32Array = makeTypedArray(4, packU32, unpackU32);
  29638. var Float32Array = makeTypedArray(4, packF32, unpackF32);
  29639. var Float64Array = makeTypedArray(8, packF64, unpackF64);
  29640. global.Int8Array = global.Int8Array || Int8Array;
  29641. global.Uint8Array = global.Uint8Array || Uint8Array;
  29642. global.Uint8ClampedArray = global.Uint8ClampedArray || Uint8ClampedArray;
  29643. global.Int16Array = global.Int16Array || Int16Array;
  29644. global.Uint16Array = global.Uint16Array || Uint16Array;
  29645. global.Int32Array = global.Int32Array || Int32Array;
  29646. global.Uint32Array = global.Uint32Array || Uint32Array;
  29647. global.Float32Array = global.Float32Array || Float32Array;
  29648. global.Float64Array = global.Float64Array || Float64Array;
  29649. }());
  29650. //
  29651. // 6 The DataView View Type
  29652. //
  29653. (function() {
  29654. function r(array, index) {
  29655. return IsCallable(array.get) ? array.get(index) : array[index];
  29656. }
  29657. var IS_BIG_ENDIAN = (function() {
  29658. var u16array = new Uint16Array([0x1234]),
  29659. u8array = new Uint8Array(u16array.buffer);
  29660. return r(u8array, 0) === 0x12;
  29661. }());
  29662. // DataView(buffer, byteOffset=0, byteLength=undefined)
  29663. // WebIDL: Constructor(ArrayBuffer buffer,
  29664. // optional unsigned long byteOffset,
  29665. // optional unsigned long byteLength)
  29666. function DataView(buffer, byteOffset, byteLength) {
  29667. if (!(buffer instanceof ArrayBuffer || Class(buffer) === 'ArrayBuffer')) throw TypeError();
  29668. byteOffset = ToUint32(byteOffset);
  29669. if (byteOffset > buffer.byteLength)
  29670. throw RangeError('byteOffset out of range');
  29671. if (byteLength === undefined)
  29672. byteLength = buffer.byteLength - byteOffset;
  29673. else
  29674. byteLength = ToUint32(byteLength);
  29675. if ((byteOffset + byteLength) > buffer.byteLength)
  29676. throw RangeError('byteOffset and length reference an area beyond the end of the buffer');
  29677. Object.defineProperty(this, 'buffer', {value: buffer});
  29678. Object.defineProperty(this, 'byteLength', {value: byteLength});
  29679. Object.defineProperty(this, 'byteOffset', {value: byteOffset});
  29680. };
  29681. // get DataView.prototype.buffer
  29682. // get DataView.prototype.byteLength
  29683. // get DataView.prototype.byteOffset
  29684. // -- applied directly to instances by the constructor
  29685. function makeGetter(arrayType) {
  29686. return function GetViewValue(byteOffset, littleEndian) {
  29687. byteOffset = ToUint32(byteOffset);
  29688. if (byteOffset + arrayType.BYTES_PER_ELEMENT > this.byteLength)
  29689. throw RangeError('Array index out of range');
  29690. byteOffset += this.byteOffset;
  29691. var uint8Array = new Uint8Array(this.buffer, byteOffset, arrayType.BYTES_PER_ELEMENT),
  29692. bytes = [];
  29693. for (var i = 0; i < arrayType.BYTES_PER_ELEMENT; i += 1)
  29694. bytes.push(r(uint8Array, i));
  29695. if (Boolean(littleEndian) === Boolean(IS_BIG_ENDIAN))
  29696. bytes.reverse();
  29697. return r(new arrayType(new Uint8Array(bytes).buffer), 0);
  29698. };
  29699. }
  29700. Object.defineProperty(DataView.prototype, 'getUint8', {value: makeGetter(Uint8Array)});
  29701. Object.defineProperty(DataView.prototype, 'getInt8', {value: makeGetter(Int8Array)});
  29702. Object.defineProperty(DataView.prototype, 'getUint16', {value: makeGetter(Uint16Array)});
  29703. Object.defineProperty(DataView.prototype, 'getInt16', {value: makeGetter(Int16Array)});
  29704. Object.defineProperty(DataView.prototype, 'getUint32', {value: makeGetter(Uint32Array)});
  29705. Object.defineProperty(DataView.prototype, 'getInt32', {value: makeGetter(Int32Array)});
  29706. Object.defineProperty(DataView.prototype, 'getFloat32', {value: makeGetter(Float32Array)});
  29707. Object.defineProperty(DataView.prototype, 'getFloat64', {value: makeGetter(Float64Array)});
  29708. function makeSetter(arrayType) {
  29709. return function SetViewValue(byteOffset, value, littleEndian) {
  29710. byteOffset = ToUint32(byteOffset);
  29711. if (byteOffset + arrayType.BYTES_PER_ELEMENT > this.byteLength)
  29712. throw RangeError('Array index out of range');
  29713. // Get bytes
  29714. var typeArray = new arrayType([value]),
  29715. byteArray = new Uint8Array(typeArray.buffer),
  29716. bytes = [], i, byteView;
  29717. for (i = 0; i < arrayType.BYTES_PER_ELEMENT; i += 1)
  29718. bytes.push(r(byteArray, i));
  29719. // Flip if necessary
  29720. if (Boolean(littleEndian) === Boolean(IS_BIG_ENDIAN))
  29721. bytes.reverse();
  29722. // Write them
  29723. byteView = new Uint8Array(this.buffer, byteOffset, arrayType.BYTES_PER_ELEMENT);
  29724. byteView.set(bytes);
  29725. };
  29726. }
  29727. Object.defineProperty(DataView.prototype, 'setUint8', {value: makeSetter(Uint8Array)});
  29728. Object.defineProperty(DataView.prototype, 'setInt8', {value: makeSetter(Int8Array)});
  29729. Object.defineProperty(DataView.prototype, 'setUint16', {value: makeSetter(Uint16Array)});
  29730. Object.defineProperty(DataView.prototype, 'setInt16', {value: makeSetter(Int16Array)});
  29731. Object.defineProperty(DataView.prototype, 'setUint32', {value: makeSetter(Uint32Array)});
  29732. Object.defineProperty(DataView.prototype, 'setInt32', {value: makeSetter(Int32Array)});
  29733. Object.defineProperty(DataView.prototype, 'setFloat32', {value: makeSetter(Float32Array)});
  29734. Object.defineProperty(DataView.prototype, 'setFloat64', {value: makeSetter(Float64Array)});
  29735. global.DataView = global.DataView || DataView;
  29736. }());
  29737. }(self));