inflate.js 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164
  1. /*
  2. Copyright (c) 2013 Gildas Lormeau. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. 1. Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. 2. Redistributions in binary form must reproduce the above copyright
  8. notice, this list of conditions and the following disclaimer in
  9. the documentation and/or other materials provided with the distribution.
  10. 3. The names of the authors may not be used to endorse or promote products
  11. derived from this software without specific prior written permission.
  12. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  13. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  14. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  15. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  16. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  17. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  18. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  20. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  21. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. /*
  24. * This program is based on JZlib 1.0.2 ymnk, JCraft,Inc.
  25. * JZlib is based on zlib-1.1.3, so all credit should go authors
  26. * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
  27. * and contributors of zlib.
  28. */
  29. (function(obj) {
  30. // Global
  31. var MAX_BITS = 15;
  32. var Z_OK = 0;
  33. var Z_STREAM_END = 1;
  34. var Z_NEED_DICT = 2;
  35. var Z_STREAM_ERROR = -2;
  36. var Z_DATA_ERROR = -3;
  37. var Z_MEM_ERROR = -4;
  38. var Z_BUF_ERROR = -5;
  39. var inflate_mask = [ 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff, 0x000003ff,
  40. 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff ];
  41. var MANY = 1440;
  42. // JZlib version : "1.0.2"
  43. var Z_NO_FLUSH = 0;
  44. var Z_FINISH = 4;
  45. // InfTree
  46. var fixed_bl = 9;
  47. var fixed_bd = 5;
  48. var fixed_tl = [ 96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9, 192, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 160, 0, 8, 0,
  49. 0, 8, 128, 0, 8, 64, 0, 9, 224, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 144, 83, 7, 59, 0, 8, 120, 0, 8, 56, 0, 9, 208, 81, 7, 17, 0, 8, 104, 0, 8, 40,
  50. 0, 9, 176, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 240, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8, 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 200, 81, 7, 13,
  51. 0, 8, 100, 0, 8, 36, 0, 9, 168, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 232, 80, 7, 8, 0, 8, 92, 0, 8, 28, 0, 9, 152, 84, 7, 83, 0, 8, 124, 0, 8, 60,
  52. 0, 9, 216, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 184, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9, 248, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7,
  53. 35, 0, 8, 114, 0, 8, 50, 0, 9, 196, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 164, 0, 8, 2, 0, 8, 130, 0, 8, 66, 0, 9, 228, 80, 7, 7, 0, 8, 90, 0, 8,
  54. 26, 0, 9, 148, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 212, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9, 180, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 244, 80,
  55. 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 204, 81, 7, 15, 0, 8, 102, 0, 8, 38, 0, 9, 172, 0, 8, 6, 0, 8, 134, 0,
  56. 8, 70, 0, 9, 236, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 156, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9, 220, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 188, 0,
  57. 8, 14, 0, 8, 142, 0, 8, 78, 0, 9, 252, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0, 8, 113, 0, 8, 49, 0, 9, 194, 80, 7, 10, 0, 8, 97,
  58. 0, 8, 33, 0, 9, 162, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 226, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9, 146, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 210,
  59. 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 178, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 242, 80, 7, 4, 0, 8, 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117,
  60. 0, 8, 53, 0, 9, 202, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 170, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9, 234, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 154,
  61. 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 218, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 186, 0, 8, 13, 0, 8, 141, 0, 8, 77, 0, 9, 250, 80, 7, 3, 0, 8, 83,
  62. 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 198, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9, 166, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 230,
  63. 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 150, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 214, 82, 7, 19, 0, 8, 107, 0, 8, 43, 0, 9, 182, 0, 8, 11, 0, 8, 139,
  64. 0, 8, 75, 0, 9, 246, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9, 206, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 174,
  65. 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 238, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 158, 84, 7, 99, 0, 8, 127, 0, 8, 63, 0, 9, 222, 82, 7, 27, 0, 8, 111,
  66. 0, 8, 47, 0, 9, 190, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 254, 96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9,
  67. 193, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 161, 0, 8, 0, 0, 8, 128, 0, 8, 64, 0, 9, 225, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 145, 83, 7, 59, 0, 8,
  68. 120, 0, 8, 56, 0, 9, 209, 81, 7, 17, 0, 8, 104, 0, 8, 40, 0, 9, 177, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 241, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8,
  69. 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 201, 81, 7, 13, 0, 8, 100, 0, 8, 36, 0, 9, 169, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 233, 80, 7, 8, 0, 8,
  70. 92, 0, 8, 28, 0, 9, 153, 84, 7, 83, 0, 8, 124, 0, 8, 60, 0, 9, 217, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 185, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9,
  71. 249, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7, 35, 0, 8, 114, 0, 8, 50, 0, 9, 197, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 165, 0, 8, 2, 0, 8,
  72. 130, 0, 8, 66, 0, 9, 229, 80, 7, 7, 0, 8, 90, 0, 8, 26, 0, 9, 149, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 213, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9,
  73. 181, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 245, 80, 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 205, 81, 7, 15, 0, 8,
  74. 102, 0, 8, 38, 0, 9, 173, 0, 8, 6, 0, 8, 134, 0, 8, 70, 0, 9, 237, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 157, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9,
  75. 221, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 189, 0, 8, 14, 0, 8, 142, 0, 8, 78, 0, 9, 253, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0,
  76. 8, 113, 0, 8, 49, 0, 9, 195, 80, 7, 10, 0, 8, 97, 0, 8, 33, 0, 9, 163, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 227, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9,
  77. 147, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 211, 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 179, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 243, 80, 7, 4, 0, 8,
  78. 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117, 0, 8, 53, 0, 9, 203, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 171, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9,
  79. 235, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 155, 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 219, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 187, 0, 8, 13, 0, 8,
  80. 141, 0, 8, 77, 0, 9, 251, 80, 7, 3, 0, 8, 83, 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 199, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9,
  81. 167, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 231, 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 151, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 215, 82, 7, 19, 0, 8,
  82. 107, 0, 8, 43, 0, 9, 183, 0, 8, 11, 0, 8, 139, 0, 8, 75, 0, 9, 247, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9,
  83. 207, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 175, 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 239, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 159, 84, 7, 99, 0, 8,
  84. 127, 0, 8, 63, 0, 9, 223, 82, 7, 27, 0, 8, 111, 0, 8, 47, 0, 9, 191, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 255 ];
  85. var fixed_td = [ 80, 5, 1, 87, 5, 257, 83, 5, 17, 91, 5, 4097, 81, 5, 5, 89, 5, 1025, 85, 5, 65, 93, 5, 16385, 80, 5, 3, 88, 5, 513, 84, 5, 33, 92, 5,
  86. 8193, 82, 5, 9, 90, 5, 2049, 86, 5, 129, 192, 5, 24577, 80, 5, 2, 87, 5, 385, 83, 5, 25, 91, 5, 6145, 81, 5, 7, 89, 5, 1537, 85, 5, 97, 93, 5,
  87. 24577, 80, 5, 4, 88, 5, 769, 84, 5, 49, 92, 5, 12289, 82, 5, 13, 90, 5, 3073, 86, 5, 193, 192, 5, 24577 ];
  88. // Tables for deflate from PKZIP's appnote.txt.
  89. var cplens = [ // Copy lengths for literal codes 257..285
  90. 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, 0, 0 ];
  91. // see note #13 above about 258
  92. var cplext = [ // Extra bits for literal codes 257..285
  93. 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, 112, 112 // 112==invalid
  94. ];
  95. var cpdist = [ // Copy offsets for distance codes 0..29
  96. 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 ];
  97. var cpdext = [ // Extra bits for distance codes
  98. 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 ];
  99. // If BMAX needs to be larger than 16, then h and x[] should be uLong.
  100. var BMAX = 15; // maximum bit length of any code
  101. function InfTree() {
  102. var that = this;
  103. var hn; // hufts used in space
  104. var v; // work area for huft_build
  105. var c; // bit length count table
  106. var r; // table entry for structure assignment
  107. var u; // table stack
  108. var x; // bit offsets, then code stack
  109. function huft_build(b, // code lengths in bits (all assumed <=
  110. // BMAX)
  111. bindex, n, // number of codes (assumed <= 288)
  112. s, // number of simple-valued codes (0..s-1)
  113. d, // list of base values for non-simple codes
  114. e, // list of extra bits for non-simple codes
  115. t, // result: starting table
  116. m, // maximum lookup bits, returns actual
  117. hp,// space for trees
  118. hn,// hufts used in space
  119. v // working area: values in order of bit length
  120. ) {
  121. // Given a list of code lengths and a maximum table size, make a set of
  122. // tables to decode that set of codes. Return Z_OK on success,
  123. // Z_BUF_ERROR
  124. // if the given code set is incomplete (the tables are still built in
  125. // this
  126. // case), Z_DATA_ERROR if the input is invalid (an over-subscribed set
  127. // of
  128. // lengths), or Z_MEM_ERROR if not enough memory.
  129. var a; // counter for codes of length k
  130. var f; // i repeats in table every f entries
  131. var g; // maximum code length
  132. var h; // table level
  133. var i; // counter, current code
  134. var j; // counter
  135. var k; // number of bits in current code
  136. var l; // bits per table (returned in m)
  137. var mask; // (1 << w) - 1, to avoid cc -O bug on HP
  138. var p; // pointer into c[], b[], or v[]
  139. var q; // points to current table
  140. var w; // bits before this table == (l * h)
  141. var xp; // pointer into x
  142. var y; // number of dummy codes added
  143. var z; // number of entries in current table
  144. // Generate counts for each bit length
  145. p = 0;
  146. i = n;
  147. do {
  148. c[b[bindex + p]]++;
  149. p++;
  150. i--; // assume all entries <= BMAX
  151. } while (i !== 0);
  152. if (c[0] == n) { // null input--all zero length codes
  153. t[0] = -1;
  154. m[0] = 0;
  155. return Z_OK;
  156. }
  157. // Find minimum and maximum length, bound *m by those
  158. l = m[0];
  159. for (j = 1; j <= BMAX; j++)
  160. if (c[j] !== 0)
  161. break;
  162. k = j; // minimum code length
  163. if (l < j) {
  164. l = j;
  165. }
  166. for (i = BMAX; i !== 0; i--) {
  167. if (c[i] !== 0)
  168. break;
  169. }
  170. g = i; // maximum code length
  171. if (l > i) {
  172. l = i;
  173. }
  174. m[0] = l;
  175. // Adjust last length count to fill out codes, if needed
  176. for (y = 1 << j; j < i; j++, y <<= 1) {
  177. if ((y -= c[j]) < 0) {
  178. return Z_DATA_ERROR;
  179. }
  180. }
  181. if ((y -= c[i]) < 0) {
  182. return Z_DATA_ERROR;
  183. }
  184. c[i] += y;
  185. // Generate starting offsets into the value table for each length
  186. x[1] = j = 0;
  187. p = 1;
  188. xp = 2;
  189. while (--i !== 0) { // note that i == g from above
  190. x[xp] = (j += c[p]);
  191. xp++;
  192. p++;
  193. }
  194. // Make a table of values in order of bit lengths
  195. i = 0;
  196. p = 0;
  197. do {
  198. if ((j = b[bindex + p]) !== 0) {
  199. v[x[j]++] = i;
  200. }
  201. p++;
  202. } while (++i < n);
  203. n = x[g]; // set n to length of v
  204. // Generate the Huffman codes and for each, make the table entries
  205. x[0] = i = 0; // first Huffman code is zero
  206. p = 0; // grab values in bit order
  207. h = -1; // no tables yet--level -1
  208. w = -l; // bits decoded == (l * h)
  209. u[0] = 0; // just to keep compilers happy
  210. q = 0; // ditto
  211. z = 0; // ditto
  212. // go through the bit lengths (k already is bits in shortest code)
  213. for (; k <= g; k++) {
  214. a = c[k];
  215. while (a-- !== 0) {
  216. // here i is the Huffman code of length k bits for value *p
  217. // make tables up to required level
  218. while (k > w + l) {
  219. h++;
  220. w += l; // previous table always l bits
  221. // compute minimum size table less than or equal to l bits
  222. z = g - w;
  223. z = (z > l) ? l : z; // table size upper limit
  224. if ((f = 1 << (j = k - w)) > a + 1) { // try a k-w bit table
  225. // too few codes for
  226. // k-w bit table
  227. f -= a + 1; // deduct codes from patterns left
  228. xp = k;
  229. if (j < z) {
  230. while (++j < z) { // try smaller tables up to z bits
  231. if ((f <<= 1) <= c[++xp])
  232. break; // enough codes to use up j bits
  233. f -= c[xp]; // else deduct codes from patterns
  234. }
  235. }
  236. }
  237. z = 1 << j; // table entries for j-bit table
  238. // allocate new table
  239. if (hn[0] + z > MANY) { // (note: doesn't matter for fixed)
  240. return Z_DATA_ERROR; // overflow of MANY
  241. }
  242. u[h] = q = /* hp+ */hn[0]; // DEBUG
  243. hn[0] += z;
  244. // connect to last table, if there is one
  245. if (h !== 0) {
  246. x[h] = i; // save pattern for backing up
  247. r[0] = /* (byte) */j; // bits in this table
  248. r[1] = /* (byte) */l; // bits to dump before this table
  249. j = i >>> (w - l);
  250. r[2] = /* (int) */(q - u[h - 1] - j); // offset to this table
  251. hp.set(r, (u[h - 1] + j) * 3);
  252. // to
  253. // last
  254. // table
  255. } else {
  256. t[0] = q; // first table is returned result
  257. }
  258. }
  259. // set up table entry in r
  260. r[1] = /* (byte) */(k - w);
  261. if (p >= n) {
  262. r[0] = 128 + 64; // out of values--invalid code
  263. } else if (v[p] < s) {
  264. r[0] = /* (byte) */(v[p] < 256 ? 0 : 32 + 64); // 256 is
  265. // end-of-block
  266. r[2] = v[p++]; // simple code is just the value
  267. } else {
  268. r[0] = /* (byte) */(e[v[p] - s] + 16 + 64); // non-simple--look
  269. // up in lists
  270. r[2] = d[v[p++] - s];
  271. }
  272. // fill code-like entries with r
  273. f = 1 << (k - w);
  274. for (j = i >>> w; j < z; j += f) {
  275. hp.set(r, (q + j) * 3);
  276. }
  277. // backwards increment the k-bit code i
  278. for (j = 1 << (k - 1); (i & j) !== 0; j >>>= 1) {
  279. i ^= j;
  280. }
  281. i ^= j;
  282. // backup over finished tables
  283. mask = (1 << w) - 1; // needed on HP, cc -O bug
  284. while ((i & mask) != x[h]) {
  285. h--; // don't need to update q
  286. w -= l;
  287. mask = (1 << w) - 1;
  288. }
  289. }
  290. }
  291. // Return Z_BUF_ERROR if we were given an incomplete table
  292. return y !== 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
  293. }
  294. function initWorkArea(vsize) {
  295. var i;
  296. if (!hn) {
  297. hn = []; // []; //new Array(1);
  298. v = []; // new Array(vsize);
  299. c = new Int32Array(BMAX + 1); // new Array(BMAX + 1);
  300. r = []; // new Array(3);
  301. u = new Int32Array(BMAX); // new Array(BMAX);
  302. x = new Int32Array(BMAX + 1); // new Array(BMAX + 1);
  303. }
  304. if (v.length < vsize) {
  305. v = []; // new Array(vsize);
  306. }
  307. for (i = 0; i < vsize; i++) {
  308. v[i] = 0;
  309. }
  310. for (i = 0; i < BMAX + 1; i++) {
  311. c[i] = 0;
  312. }
  313. for (i = 0; i < 3; i++) {
  314. r[i] = 0;
  315. }
  316. // for(int i=0; i<BMAX; i++){u[i]=0;}
  317. u.set(c.subarray(0, BMAX), 0);
  318. // for(int i=0; i<BMAX+1; i++){x[i]=0;}
  319. x.set(c.subarray(0, BMAX + 1), 0);
  320. }
  321. that.inflate_trees_bits = function(c, // 19 code lengths
  322. bb, // bits tree desired/actual depth
  323. tb, // bits tree result
  324. hp, // space for trees
  325. z // for messages
  326. ) {
  327. var result;
  328. initWorkArea(19);
  329. hn[0] = 0;
  330. result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);
  331. if (result == Z_DATA_ERROR) {
  332. z.msg = "oversubscribed dynamic bit lengths tree";
  333. } else if (result == Z_BUF_ERROR || bb[0] === 0) {
  334. z.msg = "incomplete dynamic bit lengths tree";
  335. result = Z_DATA_ERROR;
  336. }
  337. return result;
  338. };
  339. that.inflate_trees_dynamic = function(nl, // number of literal/length codes
  340. nd, // number of distance codes
  341. c, // that many (total) code lengths
  342. bl, // literal desired/actual bit depth
  343. bd, // distance desired/actual bit depth
  344. tl, // literal/length tree result
  345. td, // distance tree result
  346. hp, // space for trees
  347. z // for messages
  348. ) {
  349. var result;
  350. // build literal/length tree
  351. initWorkArea(288);
  352. hn[0] = 0;
  353. result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
  354. if (result != Z_OK || bl[0] === 0) {
  355. if (result == Z_DATA_ERROR) {
  356. z.msg = "oversubscribed literal/length tree";
  357. } else if (result != Z_MEM_ERROR) {
  358. z.msg = "incomplete literal/length tree";
  359. result = Z_DATA_ERROR;
  360. }
  361. return result;
  362. }
  363. // build distance tree
  364. initWorkArea(288);
  365. result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);
  366. if (result != Z_OK || (bd[0] === 0 && nl > 257)) {
  367. if (result == Z_DATA_ERROR) {
  368. z.msg = "oversubscribed distance tree";
  369. } else if (result == Z_BUF_ERROR) {
  370. z.msg = "incomplete distance tree";
  371. result = Z_DATA_ERROR;
  372. } else if (result != Z_MEM_ERROR) {
  373. z.msg = "empty distance tree with lengths";
  374. result = Z_DATA_ERROR;
  375. }
  376. return result;
  377. }
  378. return Z_OK;
  379. };
  380. }
  381. InfTree.inflate_trees_fixed = function(bl, // literal desired/actual bit depth
  382. bd, // distance desired/actual bit depth
  383. tl,// literal/length tree result
  384. td// distance tree result
  385. ) {
  386. bl[0] = fixed_bl;
  387. bd[0] = fixed_bd;
  388. tl[0] = fixed_tl;
  389. td[0] = fixed_td;
  390. return Z_OK;
  391. };
  392. // InfCodes
  393. // waiting for "i:"=input,
  394. // "o:"=output,
  395. // "x:"=nothing
  396. var START = 0; // x: set up for LEN
  397. var LEN = 1; // i: get length/literal/eob next
  398. var LENEXT = 2; // i: getting length extra (have base)
  399. var DIST = 3; // i: get distance next
  400. var DISTEXT = 4;// i: getting distance extra
  401. var COPY = 5; // o: copying bytes in window, waiting
  402. // for space
  403. var LIT = 6; // o: got literal, waiting for output
  404. // space
  405. var WASH = 7; // o: got eob, possibly still output
  406. // waiting
  407. var END = 8; // x: got eob and all data flushed
  408. var BADCODE = 9;// x: got error
  409. function InfCodes() {
  410. var that = this;
  411. var mode; // current inflate_codes mode
  412. // mode dependent information
  413. var len = 0;
  414. var tree; // pointer into tree
  415. var tree_index = 0;
  416. var need = 0; // bits needed
  417. var lit = 0;
  418. // if EXT or COPY, where and how much
  419. var get = 0; // bits to get for extra
  420. var dist = 0; // distance back to copy from
  421. var lbits = 0; // ltree bits decoded per branch
  422. var dbits = 0; // dtree bits decoder per branch
  423. var ltree; // literal/length/eob tree
  424. var ltree_index = 0; // literal/length/eob tree
  425. var dtree; // distance tree
  426. var dtree_index = 0; // distance tree
  427. // Called with number of bytes left to write in window at least 258
  428. // (the maximum string length) and number of input bytes available
  429. // at least ten. The ten bytes are six bytes for the longest length/
  430. // distance pair plus four bytes for overloading the bit buffer.
  431. function inflate_fast(bl, bd, tl, tl_index, td, td_index, s, z) {
  432. var t; // temporary pointer
  433. var tp; // temporary pointer
  434. var tp_index; // temporary pointer
  435. var e; // extra bits or operation
  436. var b; // bit buffer
  437. var k; // bits in bit buffer
  438. var p; // input data pointer
  439. var n; // bytes available there
  440. var q; // output window write pointer
  441. var m; // bytes to end of window or read pointer
  442. var ml; // mask for literal/length tree
  443. var md; // mask for distance tree
  444. var c; // bytes to copy
  445. var d; // distance back to copy from
  446. var r; // copy source pointer
  447. var tp_index_t_3; // (tp_index+t)*3
  448. // load input, output, bit values
  449. p = z.next_in_index;
  450. n = z.avail_in;
  451. b = s.bitb;
  452. k = s.bitk;
  453. q = s.write;
  454. m = q < s.read ? s.read - q - 1 : s.end - q;
  455. // initialize masks
  456. ml = inflate_mask[bl];
  457. md = inflate_mask[bd];
  458. // do until not enough input or output space for fast loop
  459. do { // assume called with m >= 258 && n >= 10
  460. // get literal/length code
  461. while (k < (20)) { // max bits for literal/length code
  462. n--;
  463. b |= (z.read_byte(p++) & 0xff) << k;
  464. k += 8;
  465. }
  466. t = b & ml;
  467. tp = tl;
  468. tp_index = tl_index;
  469. tp_index_t_3 = (tp_index + t) * 3;
  470. if ((e = tp[tp_index_t_3]) === 0) {
  471. b >>= (tp[tp_index_t_3 + 1]);
  472. k -= (tp[tp_index_t_3 + 1]);
  473. s.window[q++] = /* (byte) */tp[tp_index_t_3 + 2];
  474. m--;
  475. continue;
  476. }
  477. do {
  478. b >>= (tp[tp_index_t_3 + 1]);
  479. k -= (tp[tp_index_t_3 + 1]);
  480. if ((e & 16) !== 0) {
  481. e &= 15;
  482. c = tp[tp_index_t_3 + 2] + (/* (int) */b & inflate_mask[e]);
  483. b >>= e;
  484. k -= e;
  485. // decode distance base of block to copy
  486. while (k < (15)) { // max bits for distance code
  487. n--;
  488. b |= (z.read_byte(p++) & 0xff) << k;
  489. k += 8;
  490. }
  491. t = b & md;
  492. tp = td;
  493. tp_index = td_index;
  494. tp_index_t_3 = (tp_index + t) * 3;
  495. e = tp[tp_index_t_3];
  496. do {
  497. b >>= (tp[tp_index_t_3 + 1]);
  498. k -= (tp[tp_index_t_3 + 1]);
  499. if ((e & 16) !== 0) {
  500. // get extra bits to add to distance base
  501. e &= 15;
  502. while (k < (e)) { // get extra bits (up to 13)
  503. n--;
  504. b |= (z.read_byte(p++) & 0xff) << k;
  505. k += 8;
  506. }
  507. d = tp[tp_index_t_3 + 2] + (b & inflate_mask[e]);
  508. b >>= (e);
  509. k -= (e);
  510. // do the copy
  511. m -= c;
  512. if (q >= d) { // offset before dest
  513. // just copy
  514. r = q - d;
  515. if (q - r > 0 && 2 > (q - r)) {
  516. s.window[q++] = s.window[r++]; // minimum
  517. // count is
  518. // three,
  519. s.window[q++] = s.window[r++]; // so unroll
  520. // loop a
  521. // little
  522. c -= 2;
  523. } else {
  524. s.window.set(s.window.subarray(r, r + 2), q);
  525. q += 2;
  526. r += 2;
  527. c -= 2;
  528. }
  529. } else { // else offset after destination
  530. r = q - d;
  531. do {
  532. r += s.end; // force pointer in window
  533. } while (r < 0); // covers invalid distances
  534. e = s.end - r;
  535. if (c > e) { // if source crosses,
  536. c -= e; // wrapped copy
  537. if (q - r > 0 && e > (q - r)) {
  538. do {
  539. s.window[q++] = s.window[r++];
  540. } while (--e !== 0);
  541. } else {
  542. s.window.set(s.window.subarray(r, r + e), q);
  543. q += e;
  544. r += e;
  545. e = 0;
  546. }
  547. r = 0; // copy rest from start of window
  548. }
  549. }
  550. // copy all or what's left
  551. if (q - r > 0 && c > (q - r)) {
  552. do {
  553. s.window[q++] = s.window[r++];
  554. } while (--c !== 0);
  555. } else {
  556. s.window.set(s.window.subarray(r, r + c), q);
  557. q += c;
  558. r += c;
  559. c = 0;
  560. }
  561. break;
  562. } else if ((e & 64) === 0) {
  563. t += tp[tp_index_t_3 + 2];
  564. t += (b & inflate_mask[e]);
  565. tp_index_t_3 = (tp_index + t) * 3;
  566. e = tp[tp_index_t_3];
  567. } else {
  568. z.msg = "invalid distance code";
  569. c = z.avail_in - n;
  570. c = (k >> 3) < c ? k >> 3 : c;
  571. n += c;
  572. p -= c;
  573. k -= c << 3;
  574. s.bitb = b;
  575. s.bitk = k;
  576. z.avail_in = n;
  577. z.total_in += p - z.next_in_index;
  578. z.next_in_index = p;
  579. s.write = q;
  580. return Z_DATA_ERROR;
  581. }
  582. } while (true);
  583. break;
  584. }
  585. if ((e & 64) === 0) {
  586. t += tp[tp_index_t_3 + 2];
  587. t += (b & inflate_mask[e]);
  588. tp_index_t_3 = (tp_index + t) * 3;
  589. if ((e = tp[tp_index_t_3]) === 0) {
  590. b >>= (tp[tp_index_t_3 + 1]);
  591. k -= (tp[tp_index_t_3 + 1]);
  592. s.window[q++] = /* (byte) */tp[tp_index_t_3 + 2];
  593. m--;
  594. break;
  595. }
  596. } else if ((e & 32) !== 0) {
  597. c = z.avail_in - n;
  598. c = (k >> 3) < c ? k >> 3 : c;
  599. n += c;
  600. p -= c;
  601. k -= c << 3;
  602. s.bitb = b;
  603. s.bitk = k;
  604. z.avail_in = n;
  605. z.total_in += p - z.next_in_index;
  606. z.next_in_index = p;
  607. s.write = q;
  608. return Z_STREAM_END;
  609. } else {
  610. z.msg = "invalid literal/length code";
  611. c = z.avail_in - n;
  612. c = (k >> 3) < c ? k >> 3 : c;
  613. n += c;
  614. p -= c;
  615. k -= c << 3;
  616. s.bitb = b;
  617. s.bitk = k;
  618. z.avail_in = n;
  619. z.total_in += p - z.next_in_index;
  620. z.next_in_index = p;
  621. s.write = q;
  622. return Z_DATA_ERROR;
  623. }
  624. } while (true);
  625. } while (m >= 258 && n >= 10);
  626. // not enough input or output--restore pointers and return
  627. c = z.avail_in - n;
  628. c = (k >> 3) < c ? k >> 3 : c;
  629. n += c;
  630. p -= c;
  631. k -= c << 3;
  632. s.bitb = b;
  633. s.bitk = k;
  634. z.avail_in = n;
  635. z.total_in += p - z.next_in_index;
  636. z.next_in_index = p;
  637. s.write = q;
  638. return Z_OK;
  639. }
  640. that.init = function(bl, bd, tl, tl_index, td, td_index) {
  641. mode = START;
  642. lbits = /* (byte) */bl;
  643. dbits = /* (byte) */bd;
  644. ltree = tl;
  645. ltree_index = tl_index;
  646. dtree = td;
  647. dtree_index = td_index;
  648. tree = null;
  649. };
  650. that.proc = function(s, z, r) {
  651. var j; // temporary storage
  652. var tindex; // temporary pointer
  653. var e; // extra bits or operation
  654. var b = 0; // bit buffer
  655. var k = 0; // bits in bit buffer
  656. var p = 0; // input data pointer
  657. var n; // bytes available there
  658. var q; // output window write pointer
  659. var m; // bytes to end of window or read pointer
  660. var f; // pointer to copy strings from
  661. // copy input/output information to locals (UPDATE macro restores)
  662. p = z.next_in_index;
  663. n = z.avail_in;
  664. b = s.bitb;
  665. k = s.bitk;
  666. q = s.write;
  667. m = q < s.read ? s.read - q - 1 : s.end - q;
  668. // process input and output based on current state
  669. while (true) {
  670. switch (mode) {
  671. // waiting for "i:"=input, "o:"=output, "x:"=nothing
  672. case START: // x: set up for LEN
  673. if (m >= 258 && n >= 10) {
  674. s.bitb = b;
  675. s.bitk = k;
  676. z.avail_in = n;
  677. z.total_in += p - z.next_in_index;
  678. z.next_in_index = p;
  679. s.write = q;
  680. r = inflate_fast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, s, z);
  681. p = z.next_in_index;
  682. n = z.avail_in;
  683. b = s.bitb;
  684. k = s.bitk;
  685. q = s.write;
  686. m = q < s.read ? s.read - q - 1 : s.end - q;
  687. if (r != Z_OK) {
  688. mode = r == Z_STREAM_END ? WASH : BADCODE;
  689. break;
  690. }
  691. }
  692. need = lbits;
  693. tree = ltree;
  694. tree_index = ltree_index;
  695. mode = LEN;
  696. case LEN: // i: get length/literal/eob next
  697. j = need;
  698. while (k < (j)) {
  699. if (n !== 0)
  700. r = Z_OK;
  701. else {
  702. s.bitb = b;
  703. s.bitk = k;
  704. z.avail_in = n;
  705. z.total_in += p - z.next_in_index;
  706. z.next_in_index = p;
  707. s.write = q;
  708. return s.inflate_flush(z, r);
  709. }
  710. n--;
  711. b |= (z.read_byte(p++) & 0xff) << k;
  712. k += 8;
  713. }
  714. tindex = (tree_index + (b & inflate_mask[j])) * 3;
  715. b >>>= (tree[tindex + 1]);
  716. k -= (tree[tindex + 1]);
  717. e = tree[tindex];
  718. if (e === 0) { // literal
  719. lit = tree[tindex + 2];
  720. mode = LIT;
  721. break;
  722. }
  723. if ((e & 16) !== 0) { // length
  724. get = e & 15;
  725. len = tree[tindex + 2];
  726. mode = LENEXT;
  727. break;
  728. }
  729. if ((e & 64) === 0) { // next table
  730. need = e;
  731. tree_index = tindex / 3 + tree[tindex + 2];
  732. break;
  733. }
  734. if ((e & 32) !== 0) { // end of block
  735. mode = WASH;
  736. break;
  737. }
  738. mode = BADCODE; // invalid code
  739. z.msg = "invalid literal/length code";
  740. r = Z_DATA_ERROR;
  741. s.bitb = b;
  742. s.bitk = k;
  743. z.avail_in = n;
  744. z.total_in += p - z.next_in_index;
  745. z.next_in_index = p;
  746. s.write = q;
  747. return s.inflate_flush(z, r);
  748. case LENEXT: // i: getting length extra (have base)
  749. j = get;
  750. while (k < (j)) {
  751. if (n !== 0)
  752. r = Z_OK;
  753. else {
  754. s.bitb = b;
  755. s.bitk = k;
  756. z.avail_in = n;
  757. z.total_in += p - z.next_in_index;
  758. z.next_in_index = p;
  759. s.write = q;
  760. return s.inflate_flush(z, r);
  761. }
  762. n--;
  763. b |= (z.read_byte(p++) & 0xff) << k;
  764. k += 8;
  765. }
  766. len += (b & inflate_mask[j]);
  767. b >>= j;
  768. k -= j;
  769. need = dbits;
  770. tree = dtree;
  771. tree_index = dtree_index;
  772. mode = DIST;
  773. case DIST: // i: get distance next
  774. j = need;
  775. while (k < (j)) {
  776. if (n !== 0)
  777. r = Z_OK;
  778. else {
  779. s.bitb = b;
  780. s.bitk = k;
  781. z.avail_in = n;
  782. z.total_in += p - z.next_in_index;
  783. z.next_in_index = p;
  784. s.write = q;
  785. return s.inflate_flush(z, r);
  786. }
  787. n--;
  788. b |= (z.read_byte(p++) & 0xff) << k;
  789. k += 8;
  790. }
  791. tindex = (tree_index + (b & inflate_mask[j])) * 3;
  792. b >>= tree[tindex + 1];
  793. k -= tree[tindex + 1];
  794. e = (tree[tindex]);
  795. if ((e & 16) !== 0) { // distance
  796. get = e & 15;
  797. dist = tree[tindex + 2];
  798. mode = DISTEXT;
  799. break;
  800. }
  801. if ((e & 64) === 0) { // next table
  802. need = e;
  803. tree_index = tindex / 3 + tree[tindex + 2];
  804. break;
  805. }
  806. mode = BADCODE; // invalid code
  807. z.msg = "invalid distance code";
  808. r = Z_DATA_ERROR;
  809. s.bitb = b;
  810. s.bitk = k;
  811. z.avail_in = n;
  812. z.total_in += p - z.next_in_index;
  813. z.next_in_index = p;
  814. s.write = q;
  815. return s.inflate_flush(z, r);
  816. case DISTEXT: // i: getting distance extra
  817. j = get;
  818. while (k < (j)) {
  819. if (n !== 0)
  820. r = Z_OK;
  821. else {
  822. s.bitb = b;
  823. s.bitk = k;
  824. z.avail_in = n;
  825. z.total_in += p - z.next_in_index;
  826. z.next_in_index = p;
  827. s.write = q;
  828. return s.inflate_flush(z, r);
  829. }
  830. n--;
  831. b |= (z.read_byte(p++) & 0xff) << k;
  832. k += 8;
  833. }
  834. dist += (b & inflate_mask[j]);
  835. b >>= j;
  836. k -= j;
  837. mode = COPY;
  838. case COPY: // o: copying bytes in window, waiting for space
  839. f = q - dist;
  840. while (f < 0) { // modulo window size-"while" instead
  841. f += s.end; // of "if" handles invalid distances
  842. }
  843. while (len !== 0) {
  844. if (m === 0) {
  845. if (q == s.end && s.read !== 0) {
  846. q = 0;
  847. m = q < s.read ? s.read - q - 1 : s.end - q;
  848. }
  849. if (m === 0) {
  850. s.write = q;
  851. r = s.inflate_flush(z, r);
  852. q = s.write;
  853. m = q < s.read ? s.read - q - 1 : s.end - q;
  854. if (q == s.end && s.read !== 0) {
  855. q = 0;
  856. m = q < s.read ? s.read - q - 1 : s.end - q;
  857. }
  858. if (m === 0) {
  859. s.bitb = b;
  860. s.bitk = k;
  861. z.avail_in = n;
  862. z.total_in += p - z.next_in_index;
  863. z.next_in_index = p;
  864. s.write = q;
  865. return s.inflate_flush(z, r);
  866. }
  867. }
  868. }
  869. s.window[q++] = s.window[f++];
  870. m--;
  871. if (f == s.end)
  872. f = 0;
  873. len--;
  874. }
  875. mode = START;
  876. break;
  877. case LIT: // o: got literal, waiting for output space
  878. if (m === 0) {
  879. if (q == s.end && s.read !== 0) {
  880. q = 0;
  881. m = q < s.read ? s.read - q - 1 : s.end - q;
  882. }
  883. if (m === 0) {
  884. s.write = q;
  885. r = s.inflate_flush(z, r);
  886. q = s.write;
  887. m = q < s.read ? s.read - q - 1 : s.end - q;
  888. if (q == s.end && s.read !== 0) {
  889. q = 0;
  890. m = q < s.read ? s.read - q - 1 : s.end - q;
  891. }
  892. if (m === 0) {
  893. s.bitb = b;
  894. s.bitk = k;
  895. z.avail_in = n;
  896. z.total_in += p - z.next_in_index;
  897. z.next_in_index = p;
  898. s.write = q;
  899. return s.inflate_flush(z, r);
  900. }
  901. }
  902. }
  903. r = Z_OK;
  904. s.window[q++] = /* (byte) */lit;
  905. m--;
  906. mode = START;
  907. break;
  908. case WASH: // o: got eob, possibly more output
  909. if (k > 7) { // return unused byte, if any
  910. k -= 8;
  911. n++;
  912. p--; // can always return one
  913. }
  914. s.write = q;
  915. r = s.inflate_flush(z, r);
  916. q = s.write;
  917. m = q < s.read ? s.read - q - 1 : s.end - q;
  918. if (s.read != s.write) {
  919. s.bitb = b;
  920. s.bitk = k;
  921. z.avail_in = n;
  922. z.total_in += p - z.next_in_index;
  923. z.next_in_index = p;
  924. s.write = q;
  925. return s.inflate_flush(z, r);
  926. }
  927. mode = END;
  928. case END:
  929. r = Z_STREAM_END;
  930. s.bitb = b;
  931. s.bitk = k;
  932. z.avail_in = n;
  933. z.total_in += p - z.next_in_index;
  934. z.next_in_index = p;
  935. s.write = q;
  936. return s.inflate_flush(z, r);
  937. case BADCODE: // x: got error
  938. r = Z_DATA_ERROR;
  939. s.bitb = b;
  940. s.bitk = k;
  941. z.avail_in = n;
  942. z.total_in += p - z.next_in_index;
  943. z.next_in_index = p;
  944. s.write = q;
  945. return s.inflate_flush(z, r);
  946. default:
  947. r = Z_STREAM_ERROR;
  948. s.bitb = b;
  949. s.bitk = k;
  950. z.avail_in = n;
  951. z.total_in += p - z.next_in_index;
  952. z.next_in_index = p;
  953. s.write = q;
  954. return s.inflate_flush(z, r);
  955. }
  956. }
  957. };
  958. that.free = function() {
  959. // ZFREE(z, c);
  960. };
  961. }
  962. // InfBlocks
  963. // Table for deflate from PKZIP's appnote.txt.
  964. var border = [ // Order of the bit length code lengths
  965. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ];
  966. var TYPE = 0; // get type bits (3, including end bit)
  967. var LENS = 1; // get lengths for stored
  968. var STORED = 2;// processing stored block
  969. var TABLE = 3; // get table lengths
  970. var BTREE = 4; // get bit lengths tree for a dynamic
  971. // block
  972. var DTREE = 5; // get length, distance trees for a
  973. // dynamic block
  974. var CODES = 6; // processing fixed or dynamic block
  975. var DRY = 7; // output remaining window bytes
  976. var DONELOCKS = 8; // finished last block, done
  977. var BADBLOCKS = 9; // ot a data error--stuck here
  978. function InfBlocks(z, w) {
  979. var that = this;
  980. var mode = TYPE; // current inflate_block mode
  981. var left = 0; // if STORED, bytes left to copy
  982. var table = 0; // table lengths (14 bits)
  983. var index = 0; // index into blens (or border)
  984. var blens; // bit lengths of codes
  985. var bb = [ 0 ]; // bit length tree depth
  986. var tb = [ 0 ]; // bit length decoding tree
  987. var codes = new InfCodes(); // if CODES, current state
  988. var last = 0; // true if this block is the last block
  989. var hufts = new Int32Array(MANY * 3); // single malloc for tree space
  990. var check = 0; // check on output
  991. var inftree = new InfTree();
  992. that.bitk = 0; // bits in bit buffer
  993. that.bitb = 0; // bit buffer
  994. that.window = new Uint8Array(w); // sliding window
  995. that.end = w; // one byte after sliding window
  996. that.read = 0; // window read pointer
  997. that.write = 0; // window write pointer
  998. that.reset = function(z, c) {
  999. if (c)
  1000. c[0] = check;
  1001. // if (mode == BTREE || mode == DTREE) {
  1002. // }
  1003. if (mode == CODES) {
  1004. codes.free(z);
  1005. }
  1006. mode = TYPE;
  1007. that.bitk = 0;
  1008. that.bitb = 0;
  1009. that.read = that.write = 0;
  1010. };
  1011. that.reset(z, null);
  1012. // copy as much as possible from the sliding window to the output area
  1013. that.inflate_flush = function(z, r) {
  1014. var n;
  1015. var p;
  1016. var q;
  1017. // local copies of source and destination pointers
  1018. p = z.next_out_index;
  1019. q = that.read;
  1020. // compute number of bytes to copy as far as end of window
  1021. n = /* (int) */((q <= that.write ? that.write : that.end) - q);
  1022. if (n > z.avail_out)
  1023. n = z.avail_out;
  1024. if (n !== 0 && r == Z_BUF_ERROR)
  1025. r = Z_OK;
  1026. // update counters
  1027. z.avail_out -= n;
  1028. z.total_out += n;
  1029. // copy as far as end of window
  1030. z.next_out.set(that.window.subarray(q, q + n), p);
  1031. p += n;
  1032. q += n;
  1033. // see if more to copy at beginning of window
  1034. if (q == that.end) {
  1035. // wrap pointers
  1036. q = 0;
  1037. if (that.write == that.end)
  1038. that.write = 0;
  1039. // compute bytes to copy
  1040. n = that.write - q;
  1041. if (n > z.avail_out)
  1042. n = z.avail_out;
  1043. if (n !== 0 && r == Z_BUF_ERROR)
  1044. r = Z_OK;
  1045. // update counters
  1046. z.avail_out -= n;
  1047. z.total_out += n;
  1048. // copy
  1049. z.next_out.set(that.window.subarray(q, q + n), p);
  1050. p += n;
  1051. q += n;
  1052. }
  1053. // update pointers
  1054. z.next_out_index = p;
  1055. that.read = q;
  1056. // done
  1057. return r;
  1058. };
  1059. that.proc = function(z, r) {
  1060. var t; // temporary storage
  1061. var b; // bit buffer
  1062. var k; // bits in bit buffer
  1063. var p; // input data pointer
  1064. var n; // bytes available there
  1065. var q; // output window write pointer
  1066. var m; // bytes to end of window or read pointer
  1067. var i;
  1068. // copy input/output information to locals (UPDATE macro restores)
  1069. // {
  1070. p = z.next_in_index;
  1071. n = z.avail_in;
  1072. b = that.bitb;
  1073. k = that.bitk;
  1074. // }
  1075. // {
  1076. q = that.write;
  1077. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  1078. // }
  1079. // process input based on current state
  1080. // DEBUG dtree
  1081. while (true) {
  1082. switch (mode) {
  1083. case TYPE:
  1084. while (k < (3)) {
  1085. if (n !== 0) {
  1086. r = Z_OK;
  1087. } else {
  1088. that.bitb = b;
  1089. that.bitk = k;
  1090. z.avail_in = n;
  1091. z.total_in += p - z.next_in_index;
  1092. z.next_in_index = p;
  1093. that.write = q;
  1094. return that.inflate_flush(z, r);
  1095. }
  1096. n--;
  1097. b |= (z.read_byte(p++) & 0xff) << k;
  1098. k += 8;
  1099. }
  1100. t = /* (int) */(b & 7);
  1101. last = t & 1;
  1102. switch (t >>> 1) {
  1103. case 0: // stored
  1104. // {
  1105. b >>>= (3);
  1106. k -= (3);
  1107. // }
  1108. t = k & 7; // go to byte boundary
  1109. // {
  1110. b >>>= (t);
  1111. k -= (t);
  1112. // }
  1113. mode = LENS; // get length of stored block
  1114. break;
  1115. case 1: // fixed
  1116. // {
  1117. var bl = []; // new Array(1);
  1118. var bd = []; // new Array(1);
  1119. var tl = [ [] ]; // new Array(1);
  1120. var td = [ [] ]; // new Array(1);
  1121. InfTree.inflate_trees_fixed(bl, bd, tl, td);
  1122. codes.init(bl[0], bd[0], tl[0], 0, td[0], 0);
  1123. // }
  1124. // {
  1125. b >>>= (3);
  1126. k -= (3);
  1127. // }
  1128. mode = CODES;
  1129. break;
  1130. case 2: // dynamic
  1131. // {
  1132. b >>>= (3);
  1133. k -= (3);
  1134. // }
  1135. mode = TABLE;
  1136. break;
  1137. case 3: // illegal
  1138. // {
  1139. b >>>= (3);
  1140. k -= (3);
  1141. // }
  1142. mode = BADBLOCKS;
  1143. z.msg = "invalid block type";
  1144. r = Z_DATA_ERROR;
  1145. that.bitb = b;
  1146. that.bitk = k;
  1147. z.avail_in = n;
  1148. z.total_in += p - z.next_in_index;
  1149. z.next_in_index = p;
  1150. that.write = q;
  1151. return that.inflate_flush(z, r);
  1152. }
  1153. break;
  1154. case LENS:
  1155. while (k < (32)) {
  1156. if (n !== 0) {
  1157. r = Z_OK;
  1158. } else {
  1159. that.bitb = b;
  1160. that.bitk = k;
  1161. z.avail_in = n;
  1162. z.total_in += p - z.next_in_index;
  1163. z.next_in_index = p;
  1164. that.write = q;
  1165. return that.inflate_flush(z, r);
  1166. }
  1167. n--;
  1168. b |= (z.read_byte(p++) & 0xff) << k;
  1169. k += 8;
  1170. }
  1171. if ((((~b) >>> 16) & 0xffff) != (b & 0xffff)) {
  1172. mode = BADBLOCKS;
  1173. z.msg = "invalid stored block lengths";
  1174. r = Z_DATA_ERROR;
  1175. that.bitb = b;
  1176. that.bitk = k;
  1177. z.avail_in = n;
  1178. z.total_in += p - z.next_in_index;
  1179. z.next_in_index = p;
  1180. that.write = q;
  1181. return that.inflate_flush(z, r);
  1182. }
  1183. left = (b & 0xffff);
  1184. b = k = 0; // dump bits
  1185. mode = left !== 0 ? STORED : (last !== 0 ? DRY : TYPE);
  1186. break;
  1187. case STORED:
  1188. if (n === 0) {
  1189. that.bitb = b;
  1190. that.bitk = k;
  1191. z.avail_in = n;
  1192. z.total_in += p - z.next_in_index;
  1193. z.next_in_index = p;
  1194. that.write = q;
  1195. return that.inflate_flush(z, r);
  1196. }
  1197. if (m === 0) {
  1198. if (q == that.end && that.read !== 0) {
  1199. q = 0;
  1200. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  1201. }
  1202. if (m === 0) {
  1203. that.write = q;
  1204. r = that.inflate_flush(z, r);
  1205. q = that.write;
  1206. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  1207. if (q == that.end && that.read !== 0) {
  1208. q = 0;
  1209. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  1210. }
  1211. if (m === 0) {
  1212. that.bitb = b;
  1213. that.bitk = k;
  1214. z.avail_in = n;
  1215. z.total_in += p - z.next_in_index;
  1216. z.next_in_index = p;
  1217. that.write = q;
  1218. return that.inflate_flush(z, r);
  1219. }
  1220. }
  1221. }
  1222. r = Z_OK;
  1223. t = left;
  1224. if (t > n)
  1225. t = n;
  1226. if (t > m)
  1227. t = m;
  1228. that.window.set(z.read_buf(p, t), q);
  1229. p += t;
  1230. n -= t;
  1231. q += t;
  1232. m -= t;
  1233. if ((left -= t) !== 0)
  1234. break;
  1235. mode = last !== 0 ? DRY : TYPE;
  1236. break;
  1237. case TABLE:
  1238. while (k < (14)) {
  1239. if (n !== 0) {
  1240. r = Z_OK;
  1241. } else {
  1242. that.bitb = b;
  1243. that.bitk = k;
  1244. z.avail_in = n;
  1245. z.total_in += p - z.next_in_index;
  1246. z.next_in_index = p;
  1247. that.write = q;
  1248. return that.inflate_flush(z, r);
  1249. }
  1250. n--;
  1251. b |= (z.read_byte(p++) & 0xff) << k;
  1252. k += 8;
  1253. }
  1254. table = t = (b & 0x3fff);
  1255. if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29) {
  1256. mode = BADBLOCKS;
  1257. z.msg = "too many length or distance symbols";
  1258. r = Z_DATA_ERROR;
  1259. that.bitb = b;
  1260. that.bitk = k;
  1261. z.avail_in = n;
  1262. z.total_in += p - z.next_in_index;
  1263. z.next_in_index = p;
  1264. that.write = q;
  1265. return that.inflate_flush(z, r);
  1266. }
  1267. t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
  1268. if (!blens || blens.length < t) {
  1269. blens = []; // new Array(t);
  1270. } else {
  1271. for (i = 0; i < t; i++) {
  1272. blens[i] = 0;
  1273. }
  1274. }
  1275. // {
  1276. b >>>= (14);
  1277. k -= (14);
  1278. // }
  1279. index = 0;
  1280. mode = BTREE;
  1281. case BTREE:
  1282. while (index < 4 + (table >>> 10)) {
  1283. while (k < (3)) {
  1284. if (n !== 0) {
  1285. r = Z_OK;
  1286. } else {
  1287. that.bitb = b;
  1288. that.bitk = k;
  1289. z.avail_in = n;
  1290. z.total_in += p - z.next_in_index;
  1291. z.next_in_index = p;
  1292. that.write = q;
  1293. return that.inflate_flush(z, r);
  1294. }
  1295. n--;
  1296. b |= (z.read_byte(p++) & 0xff) << k;
  1297. k += 8;
  1298. }
  1299. blens[border[index++]] = b & 7;
  1300. // {
  1301. b >>>= (3);
  1302. k -= (3);
  1303. // }
  1304. }
  1305. while (index < 19) {
  1306. blens[border[index++]] = 0;
  1307. }
  1308. bb[0] = 7;
  1309. t = inftree.inflate_trees_bits(blens, bb, tb, hufts, z);
  1310. if (t != Z_OK) {
  1311. r = t;
  1312. if (r == Z_DATA_ERROR) {
  1313. blens = null;
  1314. mode = BADBLOCKS;
  1315. }
  1316. that.bitb = b;
  1317. that.bitk = k;
  1318. z.avail_in = n;
  1319. z.total_in += p - z.next_in_index;
  1320. z.next_in_index = p;
  1321. that.write = q;
  1322. return that.inflate_flush(z, r);
  1323. }
  1324. index = 0;
  1325. mode = DTREE;
  1326. case DTREE:
  1327. while (true) {
  1328. t = table;
  1329. if (!(index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))) {
  1330. break;
  1331. }
  1332. var j, c;
  1333. t = bb[0];
  1334. while (k < (t)) {
  1335. if (n !== 0) {
  1336. r = Z_OK;
  1337. } else {
  1338. that.bitb = b;
  1339. that.bitk = k;
  1340. z.avail_in = n;
  1341. z.total_in += p - z.next_in_index;
  1342. z.next_in_index = p;
  1343. that.write = q;
  1344. return that.inflate_flush(z, r);
  1345. }
  1346. n--;
  1347. b |= (z.read_byte(p++) & 0xff) << k;
  1348. k += 8;
  1349. }
  1350. // if (tb[0] == -1) {
  1351. // System.err.println("null...");
  1352. // }
  1353. t = hufts[(tb[0] + (b & inflate_mask[t])) * 3 + 1];
  1354. c = hufts[(tb[0] + (b & inflate_mask[t])) * 3 + 2];
  1355. if (c < 16) {
  1356. b >>>= (t);
  1357. k -= (t);
  1358. blens[index++] = c;
  1359. } else { // c == 16..18
  1360. i = c == 18 ? 7 : c - 14;
  1361. j = c == 18 ? 11 : 3;
  1362. while (k < (t + i)) {
  1363. if (n !== 0) {
  1364. r = Z_OK;
  1365. } else {
  1366. that.bitb = b;
  1367. that.bitk = k;
  1368. z.avail_in = n;
  1369. z.total_in += p - z.next_in_index;
  1370. z.next_in_index = p;
  1371. that.write = q;
  1372. return that.inflate_flush(z, r);
  1373. }
  1374. n--;
  1375. b |= (z.read_byte(p++) & 0xff) << k;
  1376. k += 8;
  1377. }
  1378. b >>>= (t);
  1379. k -= (t);
  1380. j += (b & inflate_mask[i]);
  1381. b >>>= (i);
  1382. k -= (i);
  1383. i = index;
  1384. t = table;
  1385. if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || (c == 16 && i < 1)) {
  1386. blens = null;
  1387. mode = BADBLOCKS;
  1388. z.msg = "invalid bit length repeat";
  1389. r = Z_DATA_ERROR;
  1390. that.bitb = b;
  1391. that.bitk = k;
  1392. z.avail_in = n;
  1393. z.total_in += p - z.next_in_index;
  1394. z.next_in_index = p;
  1395. that.write = q;
  1396. return that.inflate_flush(z, r);
  1397. }
  1398. c = c == 16 ? blens[i - 1] : 0;
  1399. do {
  1400. blens[i++] = c;
  1401. } while (--j !== 0);
  1402. index = i;
  1403. }
  1404. }
  1405. tb[0] = -1;
  1406. // {
  1407. var bl_ = []; // new Array(1);
  1408. var bd_ = []; // new Array(1);
  1409. var tl_ = []; // new Array(1);
  1410. var td_ = []; // new Array(1);
  1411. bl_[0] = 9; // must be <= 9 for lookahead assumptions
  1412. bd_[0] = 6; // must be <= 9 for lookahead assumptions
  1413. t = table;
  1414. t = inftree.inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f), blens, bl_, bd_, tl_, td_, hufts, z);
  1415. if (t != Z_OK) {
  1416. if (t == Z_DATA_ERROR) {
  1417. blens = null;
  1418. mode = BADBLOCKS;
  1419. }
  1420. r = t;
  1421. that.bitb = b;
  1422. that.bitk = k;
  1423. z.avail_in = n;
  1424. z.total_in += p - z.next_in_index;
  1425. z.next_in_index = p;
  1426. that.write = q;
  1427. return that.inflate_flush(z, r);
  1428. }
  1429. codes.init(bl_[0], bd_[0], hufts, tl_[0], hufts, td_[0]);
  1430. // }
  1431. mode = CODES;
  1432. case CODES:
  1433. that.bitb = b;
  1434. that.bitk = k;
  1435. z.avail_in = n;
  1436. z.total_in += p - z.next_in_index;
  1437. z.next_in_index = p;
  1438. that.write = q;
  1439. if ((r = codes.proc(that, z, r)) != Z_STREAM_END) {
  1440. return that.inflate_flush(z, r);
  1441. }
  1442. r = Z_OK;
  1443. codes.free(z);
  1444. p = z.next_in_index;
  1445. n = z.avail_in;
  1446. b = that.bitb;
  1447. k = that.bitk;
  1448. q = that.write;
  1449. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  1450. if (last === 0) {
  1451. mode = TYPE;
  1452. break;
  1453. }
  1454. mode = DRY;
  1455. case DRY:
  1456. that.write = q;
  1457. r = that.inflate_flush(z, r);
  1458. q = that.write;
  1459. m = /* (int) */(q < that.read ? that.read - q - 1 : that.end - q);
  1460. if (that.read != that.write) {
  1461. that.bitb = b;
  1462. that.bitk = k;
  1463. z.avail_in = n;
  1464. z.total_in += p - z.next_in_index;
  1465. z.next_in_index = p;
  1466. that.write = q;
  1467. return that.inflate_flush(z, r);
  1468. }
  1469. mode = DONELOCKS;
  1470. case DONELOCKS:
  1471. r = Z_STREAM_END;
  1472. that.bitb = b;
  1473. that.bitk = k;
  1474. z.avail_in = n;
  1475. z.total_in += p - z.next_in_index;
  1476. z.next_in_index = p;
  1477. that.write = q;
  1478. return that.inflate_flush(z, r);
  1479. case BADBLOCKS:
  1480. r = Z_DATA_ERROR;
  1481. that.bitb = b;
  1482. that.bitk = k;
  1483. z.avail_in = n;
  1484. z.total_in += p - z.next_in_index;
  1485. z.next_in_index = p;
  1486. that.write = q;
  1487. return that.inflate_flush(z, r);
  1488. default:
  1489. r = Z_STREAM_ERROR;
  1490. that.bitb = b;
  1491. that.bitk = k;
  1492. z.avail_in = n;
  1493. z.total_in += p - z.next_in_index;
  1494. z.next_in_index = p;
  1495. that.write = q;
  1496. return that.inflate_flush(z, r);
  1497. }
  1498. }
  1499. };
  1500. that.free = function(z) {
  1501. that.reset(z, null);
  1502. that.window = null;
  1503. hufts = null;
  1504. // ZFREE(z, s);
  1505. };
  1506. that.set_dictionary = function(d, start, n) {
  1507. that.window.set(d.subarray(start, start + n), 0);
  1508. that.read = that.write = n;
  1509. };
  1510. // Returns true if inflate is currently at the end of a block generated
  1511. // by Z_SYNC_FLUSH or Z_FULL_FLUSH.
  1512. that.sync_point = function() {
  1513. return mode == LENS ? 1 : 0;
  1514. };
  1515. }
  1516. // Inflate
  1517. // preset dictionary flag in zlib header
  1518. var PRESET_DICT = 0x20;
  1519. var Z_DEFLATED = 8;
  1520. var METHOD = 0; // waiting for method byte
  1521. var FLAG = 1; // waiting for flag byte
  1522. var DICT4 = 2; // four dictionary check bytes to go
  1523. var DICT3 = 3; // three dictionary check bytes to go
  1524. var DICT2 = 4; // two dictionary check bytes to go
  1525. var DICT1 = 5; // one dictionary check byte to go
  1526. var DICT0 = 6; // waiting for inflateSetDictionary
  1527. var BLOCKS = 7; // decompressing blocks
  1528. var DONE = 12; // finished check, done
  1529. var BAD = 13; // got an error--stay here
  1530. var mark = [ 0, 0, 0xff, 0xff ];
  1531. function Inflate() {
  1532. var that = this;
  1533. that.mode = 0; // current inflate mode
  1534. // mode dependent information
  1535. that.method = 0; // if FLAGS, method byte
  1536. // if CHECK, check values to compare
  1537. that.was = [ 0 ]; // new Array(1); // computed check value
  1538. that.need = 0; // stream check value
  1539. // if BAD, inflateSync's marker bytes count
  1540. that.marker = 0;
  1541. // mode independent information
  1542. that.wbits = 0; // log2(window size) (8..15, defaults to 15)
  1543. // this.blocks; // current inflate_blocks state
  1544. function inflateReset(z) {
  1545. if (!z || !z.istate)
  1546. return Z_STREAM_ERROR;
  1547. z.total_in = z.total_out = 0;
  1548. z.msg = null;
  1549. z.istate.mode = BLOCKS;
  1550. z.istate.blocks.reset(z, null);
  1551. return Z_OK;
  1552. }
  1553. that.inflateEnd = function(z) {
  1554. if (that.blocks)
  1555. that.blocks.free(z);
  1556. that.blocks = null;
  1557. // ZFREE(z, z->state);
  1558. return Z_OK;
  1559. };
  1560. that.inflateInit = function(z, w) {
  1561. z.msg = null;
  1562. that.blocks = null;
  1563. // set window size
  1564. if (w < 8 || w > 15) {
  1565. that.inflateEnd(z);
  1566. return Z_STREAM_ERROR;
  1567. }
  1568. that.wbits = w;
  1569. z.istate.blocks = new InfBlocks(z, 1 << w);
  1570. // reset state
  1571. inflateReset(z);
  1572. return Z_OK;
  1573. };
  1574. that.inflate = function(z, f) {
  1575. var r;
  1576. var b;
  1577. if (!z || !z.istate || !z.next_in)
  1578. return Z_STREAM_ERROR;
  1579. f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
  1580. r = Z_BUF_ERROR;
  1581. while (true) {
  1582. // System.out.println("mode: "+z.istate.mode);
  1583. switch (z.istate.mode) {
  1584. case METHOD:
  1585. if (z.avail_in === 0)
  1586. return r;
  1587. r = f;
  1588. z.avail_in--;
  1589. z.total_in++;
  1590. if (((z.istate.method = z.read_byte(z.next_in_index++)) & 0xf) != Z_DEFLATED) {
  1591. z.istate.mode = BAD;
  1592. z.msg = "unknown compression method";
  1593. z.istate.marker = 5; // can't try inflateSync
  1594. break;
  1595. }
  1596. if ((z.istate.method >> 4) + 8 > z.istate.wbits) {
  1597. z.istate.mode = BAD;
  1598. z.msg = "invalid window size";
  1599. z.istate.marker = 5; // can't try inflateSync
  1600. break;
  1601. }
  1602. z.istate.mode = FLAG;
  1603. case FLAG:
  1604. if (z.avail_in === 0)
  1605. return r;
  1606. r = f;
  1607. z.avail_in--;
  1608. z.total_in++;
  1609. b = (z.read_byte(z.next_in_index++)) & 0xff;
  1610. if ((((z.istate.method << 8) + b) % 31) !== 0) {
  1611. z.istate.mode = BAD;
  1612. z.msg = "incorrect header check";
  1613. z.istate.marker = 5; // can't try inflateSync
  1614. break;
  1615. }
  1616. if ((b & PRESET_DICT) === 0) {
  1617. z.istate.mode = BLOCKS;
  1618. break;
  1619. }
  1620. z.istate.mode = DICT4;
  1621. case DICT4:
  1622. if (z.avail_in === 0)
  1623. return r;
  1624. r = f;
  1625. z.avail_in--;
  1626. z.total_in++;
  1627. z.istate.need = ((z.read_byte(z.next_in_index++) & 0xff) << 24) & 0xff000000;
  1628. z.istate.mode = DICT3;
  1629. case DICT3:
  1630. if (z.avail_in === 0)
  1631. return r;
  1632. r = f;
  1633. z.avail_in--;
  1634. z.total_in++;
  1635. z.istate.need += ((z.read_byte(z.next_in_index++) & 0xff) << 16) & 0xff0000;
  1636. z.istate.mode = DICT2;
  1637. case DICT2:
  1638. if (z.avail_in === 0)
  1639. return r;
  1640. r = f;
  1641. z.avail_in--;
  1642. z.total_in++;
  1643. z.istate.need += ((z.read_byte(z.next_in_index++) & 0xff) << 8) & 0xff00;
  1644. z.istate.mode = DICT1;
  1645. case DICT1:
  1646. if (z.avail_in === 0)
  1647. return r;
  1648. r = f;
  1649. z.avail_in--;
  1650. z.total_in++;
  1651. z.istate.need += (z.read_byte(z.next_in_index++) & 0xff);
  1652. z.istate.mode = DICT0;
  1653. return Z_NEED_DICT;
  1654. case DICT0:
  1655. z.istate.mode = BAD;
  1656. z.msg = "need dictionary";
  1657. z.istate.marker = 0; // can try inflateSync
  1658. return Z_STREAM_ERROR;
  1659. case BLOCKS:
  1660. r = z.istate.blocks.proc(z, r);
  1661. if (r == Z_DATA_ERROR) {
  1662. z.istate.mode = BAD;
  1663. z.istate.marker = 0; // can try inflateSync
  1664. break;
  1665. }
  1666. if (r == Z_OK) {
  1667. r = f;
  1668. }
  1669. if (r != Z_STREAM_END) {
  1670. return r;
  1671. }
  1672. r = f;
  1673. z.istate.blocks.reset(z, z.istate.was);
  1674. z.istate.mode = DONE;
  1675. case DONE:
  1676. return Z_STREAM_END;
  1677. case BAD:
  1678. return Z_DATA_ERROR;
  1679. default:
  1680. return Z_STREAM_ERROR;
  1681. }
  1682. }
  1683. };
  1684. that.inflateSetDictionary = function(z, dictionary, dictLength) {
  1685. var index = 0;
  1686. var length = dictLength;
  1687. if (!z || !z.istate || z.istate.mode != DICT0)
  1688. return Z_STREAM_ERROR;
  1689. if (length >= (1 << z.istate.wbits)) {
  1690. length = (1 << z.istate.wbits) - 1;
  1691. index = dictLength - length;
  1692. }
  1693. z.istate.blocks.set_dictionary(dictionary, index, length);
  1694. z.istate.mode = BLOCKS;
  1695. return Z_OK;
  1696. };
  1697. that.inflateSync = function(z) {
  1698. var n; // number of bytes to look at
  1699. var p; // pointer to bytes
  1700. var m; // number of marker bytes found in a row
  1701. var r, w; // temporaries to save total_in and total_out
  1702. // set up
  1703. if (!z || !z.istate)
  1704. return Z_STREAM_ERROR;
  1705. if (z.istate.mode != BAD) {
  1706. z.istate.mode = BAD;
  1707. z.istate.marker = 0;
  1708. }
  1709. if ((n = z.avail_in) === 0)
  1710. return Z_BUF_ERROR;
  1711. p = z.next_in_index;
  1712. m = z.istate.marker;
  1713. // search
  1714. while (n !== 0 && m < 4) {
  1715. if (z.read_byte(p) == mark[m]) {
  1716. m++;
  1717. } else if (z.read_byte(p) !== 0) {
  1718. m = 0;
  1719. } else {
  1720. m = 4 - m;
  1721. }
  1722. p++;
  1723. n--;
  1724. }
  1725. // restore
  1726. z.total_in += p - z.next_in_index;
  1727. z.next_in_index = p;
  1728. z.avail_in = n;
  1729. z.istate.marker = m;
  1730. // return no joy or set up to restart on a new block
  1731. if (m != 4) {
  1732. return Z_DATA_ERROR;
  1733. }
  1734. r = z.total_in;
  1735. w = z.total_out;
  1736. inflateReset(z);
  1737. z.total_in = r;
  1738. z.total_out = w;
  1739. z.istate.mode = BLOCKS;
  1740. return Z_OK;
  1741. };
  1742. // Returns true if inflate is currently at the end of a block generated
  1743. // by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  1744. // implementation to provide an additional safety check. PPP uses
  1745. // Z_SYNC_FLUSH
  1746. // but removes the length bytes of the resulting empty stored block. When
  1747. // decompressing, PPP checks that at the end of input packet, inflate is
  1748. // waiting for these length bytes.
  1749. that.inflateSyncPoint = function(z) {
  1750. if (!z || !z.istate || !z.istate.blocks)
  1751. return Z_STREAM_ERROR;
  1752. return z.istate.blocks.sync_point();
  1753. };
  1754. }
  1755. // ZStream
  1756. function ZStream() {
  1757. }
  1758. ZStream.prototype = {
  1759. inflateInit : function(bits) {
  1760. var that = this;
  1761. that.istate = new Inflate();
  1762. if (!bits)
  1763. bits = MAX_BITS;
  1764. return that.istate.inflateInit(that, bits);
  1765. },
  1766. inflate : function(f) {
  1767. var that = this;
  1768. if (!that.istate)
  1769. return Z_STREAM_ERROR;
  1770. return that.istate.inflate(that, f);
  1771. },
  1772. inflateEnd : function() {
  1773. var that = this;
  1774. if (!that.istate)
  1775. return Z_STREAM_ERROR;
  1776. var ret = that.istate.inflateEnd(that);
  1777. that.istate = null;
  1778. return ret;
  1779. },
  1780. inflateSync : function() {
  1781. var that = this;
  1782. if (!that.istate)
  1783. return Z_STREAM_ERROR;
  1784. return that.istate.inflateSync(that);
  1785. },
  1786. inflateSetDictionary : function(dictionary, dictLength) {
  1787. var that = this;
  1788. if (!that.istate)
  1789. return Z_STREAM_ERROR;
  1790. return that.istate.inflateSetDictionary(that, dictionary, dictLength);
  1791. },
  1792. read_byte : function(start) {
  1793. var that = this;
  1794. return that.next_in.subarray(start, start + 1)[0];
  1795. },
  1796. read_buf : function(start, size) {
  1797. var that = this;
  1798. return that.next_in.subarray(start, start + size);
  1799. }
  1800. };
  1801. // Inflater
  1802. function Inflater() {
  1803. var that = this;
  1804. var z = new ZStream();
  1805. var bufsize = 512;
  1806. var flush = Z_NO_FLUSH;
  1807. var buf = new Uint8Array(bufsize);
  1808. var nomoreinput = false;
  1809. z.inflateInit();
  1810. z.next_out = buf;
  1811. that.append = function(data, onprogress) {
  1812. var err, buffers = [], lastIndex = 0, bufferIndex = 0, bufferSize = 0, array;
  1813. if (data.length === 0)
  1814. return;
  1815. z.next_in_index = 0;
  1816. z.next_in = data;
  1817. z.avail_in = data.length;
  1818. do {
  1819. z.next_out_index = 0;
  1820. z.avail_out = bufsize;
  1821. if ((z.avail_in === 0) && (!nomoreinput)) { // if buffer is empty and more input is available, refill it
  1822. z.next_in_index = 0;
  1823. nomoreinput = true;
  1824. }
  1825. err = z.inflate(flush);
  1826. if (nomoreinput && (err == Z_BUF_ERROR))
  1827. return -1;
  1828. if (err != Z_OK && err != Z_STREAM_END)
  1829. throw "inflating: " + z.msg;
  1830. if ((nomoreinput || err == Z_STREAM_END) && (z.avail_in == data.length))
  1831. return -1;
  1832. if (z.next_out_index)
  1833. if (z.next_out_index == bufsize)
  1834. buffers.push(new Uint8Array(buf));
  1835. else
  1836. buffers.push(new Uint8Array(buf.subarray(0, z.next_out_index)));
  1837. bufferSize += z.next_out_index;
  1838. if (onprogress && z.next_in_index > 0 && z.next_in_index != lastIndex) {
  1839. onprogress(z.next_in_index);
  1840. lastIndex = z.next_in_index;
  1841. }
  1842. } while (z.avail_in > 0 || z.avail_out === 0);
  1843. array = new Uint8Array(bufferSize);
  1844. buffers.forEach(function(chunk) {
  1845. array.set(chunk, bufferIndex);
  1846. bufferIndex += chunk.length;
  1847. });
  1848. return array;
  1849. };
  1850. that.flush = function() {
  1851. z.inflateEnd();
  1852. };
  1853. }
  1854. var inflater;
  1855. if (obj.zip)
  1856. obj.zip.Inflater = Inflater;
  1857. else {
  1858. inflater = new Inflater();
  1859. obj.addEventListener("message", function(event) {
  1860. var message = event.data;
  1861. if (message.append)
  1862. obj.postMessage({
  1863. onappend : true,
  1864. data : inflater.append(message.data, function(current) {
  1865. obj.postMessage({
  1866. progress : true,
  1867. current : current
  1868. });
  1869. })
  1870. });
  1871. if (message.flush) {
  1872. inflater.flush();
  1873. obj.postMessage({
  1874. onflush : true
  1875. });
  1876. }
  1877. }, false);
  1878. }
  1879. })(EasyDeflate);