@cherry-markdown/cherry-markdown-dev 0.8.58-dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (319) hide show
  1. package/package.json +149 -0
  2. package/src/Cherry.config.js +625 -0
  3. package/src/Cherry.js +1104 -0
  4. package/src/CherryStatic.js +70 -0
  5. package/src/Editor.js +748 -0
  6. package/src/Engine.js +381 -0
  7. package/src/Event.js +140 -0
  8. package/src/Factory.js +180 -0
  9. package/src/Logger.js +31 -0
  10. package/src/Previewer.js +1183 -0
  11. package/src/Sanitizer.js +4 -0
  12. package/src/Sanitizer.node.js +7 -0
  13. package/src/UrlCache.js +98 -0
  14. package/src/addons/advance/cherry-table-echarts-plugin.js +170 -0
  15. package/src/addons/cherry-code-block-mermaid-plugin.js +158 -0
  16. package/src/addons/cherry-code-block-plantuml-plugin.js +106 -0
  17. package/src/core/HookCenter.js +297 -0
  18. package/src/core/HooksConfig.js +100 -0
  19. package/src/core/ParagraphBase.js +332 -0
  20. package/src/core/SentenceBase.js +65 -0
  21. package/src/core/SyntaxBase.js +194 -0
  22. package/src/core/hooks/AutoLink.js +232 -0
  23. package/src/core/hooks/BackgroundColor.js +46 -0
  24. package/src/core/hooks/Blockquote.js +70 -0
  25. package/src/core/hooks/Br.js +85 -0
  26. package/src/core/hooks/CodeBlock.js +446 -0
  27. package/src/core/hooks/Color.js +46 -0
  28. package/src/core/hooks/CommentReference.js +96 -0
  29. package/src/core/hooks/Detail.js +108 -0
  30. package/src/core/hooks/Emoji.config.js +1825 -0
  31. package/src/core/hooks/Emoji.js +119 -0
  32. package/src/core/hooks/Emphasis.js +113 -0
  33. package/src/core/hooks/Footnote.js +125 -0
  34. package/src/core/hooks/FrontMatter.js +51 -0
  35. package/src/core/hooks/Header.js +234 -0
  36. package/src/core/hooks/HighLight.js +37 -0
  37. package/src/core/hooks/Hr.js +52 -0
  38. package/src/core/hooks/HtmlBlock.js +184 -0
  39. package/src/core/hooks/Image.js +174 -0
  40. package/src/core/hooks/InlineCode.js +48 -0
  41. package/src/core/hooks/InlineMath.js +107 -0
  42. package/src/core/hooks/Link.js +160 -0
  43. package/src/core/hooks/List.js +264 -0
  44. package/src/core/hooks/MathBlock.js +103 -0
  45. package/src/core/hooks/Panel.js +145 -0
  46. package/src/core/hooks/Paragraph.js +84 -0
  47. package/src/core/hooks/Ruby.js +34 -0
  48. package/src/core/hooks/Size.js +51 -0
  49. package/src/core/hooks/Strikethrough.js +54 -0
  50. package/src/core/hooks/Sub.js +47 -0
  51. package/src/core/hooks/SuggestList.js +333 -0
  52. package/src/core/hooks/Suggester.js +707 -0
  53. package/src/core/hooks/Sup.js +47 -0
  54. package/src/core/hooks/Table.js +275 -0
  55. package/src/core/hooks/Toc.js +292 -0
  56. package/src/core/hooks/Transfer.js +47 -0
  57. package/src/core/hooks/Underline.js +37 -0
  58. package/src/index.core.js +29 -0
  59. package/src/index.engine.core.js +68 -0
  60. package/src/index.engine.js +28 -0
  61. package/src/index.js +32 -0
  62. package/src/libs/mermaidAPI.8.4.8.js +1 -0
  63. package/src/libs/mermaidAPI.8.5.2.js +42 -0
  64. package/src/libs/rawdeflate.js +1663 -0
  65. package/src/locales/en_US.js +139 -0
  66. package/src/locales/index.js +25 -0
  67. package/src/locales/ru_RU.js +139 -0
  68. package/src/locales/zh_CN.js +142 -0
  69. package/src/sass/base.scss +26 -0
  70. package/src/sass/bubble_formula.scss +166 -0
  71. package/src/sass/ch-icon.scss +118 -0
  72. package/src/sass/cherry.scss +1116 -0
  73. package/src/sass/components/bubble.scss +173 -0
  74. package/src/sass/components/shortcut_key_config.scss +108 -0
  75. package/src/sass/formula_utils_bubble.scss +82 -0
  76. package/src/sass/icon_template.scss +24 -0
  77. package/src/sass/icons/uEA03-list.svg +19 -0
  78. package/src/sass/icons/uEA04-check.svg +14 -0
  79. package/src/sass/icons/uEA09-square.svg +10 -0
  80. package/src/sass/icons/uEA0A-bold.svg +20 -0
  81. package/src/sass/icons/uEA0B-code.svg +18 -0
  82. package/src/sass/icons/uEA0C-color.svg +13 -0
  83. package/src/sass/icons/uEA0D-header.svg +8 -0
  84. package/src/sass/icons/uEA0E-image.svg +15 -0
  85. package/src/sass/icons/uEA0F-italic.svg +8 -0
  86. package/src/sass/icons/uEA10-link.svg +16 -0
  87. package/src/sass/icons/uEA11-ol.svg +21 -0
  88. package/src/sass/icons/uEA12-size.svg +11 -0
  89. package/src/sass/icons/uEA13-strike.svg +16 -0
  90. package/src/sass/icons/uEA14-table.svg +12 -0
  91. package/src/sass/icons/uEA15-ul.svg +17 -0
  92. package/src/sass/icons/uEA16-underline.svg +13 -0
  93. package/src/sass/icons/uEA17-word.svg +16 -0
  94. package/src/sass/icons/uEA18-blockquote.svg +11 -0
  95. package/src/sass/icons/uEA19-font.svg +10 -0
  96. package/src/sass/icons/uEA1F-insertClass.svg +39 -0
  97. package/src/sass/icons/uEA20-insertFlow.svg +8 -0
  98. package/src/sass/icons/uEA21-insertFormula.svg +23 -0
  99. package/src/sass/icons/uEA22-insertGantt.svg +13 -0
  100. package/src/sass/icons/uEA23-insertGraph.svg +13 -0
  101. package/src/sass/icons/uEA24-insertPie.svg +19 -0
  102. package/src/sass/icons/uEA25-insertSeq.svg +20 -0
  103. package/src/sass/icons/uEA26-insertState.svg +35 -0
  104. package/src/sass/icons/uEA27-line.svg +11 -0
  105. package/src/sass/icons/uEA28-preview.svg +18 -0
  106. package/src/sass/icons/uEA29-previewClose.svg +24 -0
  107. package/src/sass/icons/uEA2A-toc.svg +24 -0
  108. package/src/sass/icons/uEA2D-sub.svg +15 -0
  109. package/src/sass/icons/uEA2E-sup.svg +15 -0
  110. package/src/sass/icons/uEA2F-h1.svg +16 -0
  111. package/src/sass/icons/uEA30-h2.svg +20 -0
  112. package/src/sass/icons/uEA31-h3.svg +23 -0
  113. package/src/sass/icons/uEA32-h4.svg +16 -0
  114. package/src/sass/icons/uEA33-h5.svg +20 -0
  115. package/src/sass/icons/uEA34-h6.svg +17 -0
  116. package/src/sass/icons/uEA35-video.svg +20 -0
  117. package/src/sass/icons/uEA36-insert.svg +25 -0
  118. package/src/sass/icons/uEA37-little_table.svg +30 -0
  119. package/src/sass/icons/uEA38-pdf.svg +27 -0
  120. package/src/sass/icons/uEA39-checklist.svg +22 -0
  121. package/src/sass/icons/uEA40-close.svg +12 -0
  122. package/src/sass/icons/uEA41-fullscreen.svg +81 -0
  123. package/src/sass/icons/uEA42-minscreen.svg +77 -0
  124. package/src/sass/icons/uEA43-insertChart.svg +23 -0
  125. package/src/sass/icons/uEA44-question.svg +25 -0
  126. package/src/sass/icons/uEA45-settings.svg +32 -0
  127. package/src/sass/icons/uEA46-ok.svg +7 -0
  128. package/src/sass/icons/uEA47-br.svg +22 -0
  129. package/src/sass/icons/uEA48-normal.svg +15 -0
  130. package/src/sass/icons/uEA49-undo.svg +19 -0
  131. package/src/sass/icons/uEA50-redo.svg +21 -0
  132. package/src/sass/icons/uEA51-copy.svg +6 -0
  133. package/src/sass/icons/uEA52-phone.svg +5 -0
  134. package/src/sass/icons/uEA53-cherry-table-delete.svg +17 -0
  135. package/src/sass/icons/uEA54-cherry-table-insert-bottom.svg +16 -0
  136. package/src/sass/icons/uEA55-cherry-table-insert-left.svg +15 -0
  137. package/src/sass/icons/uEA56-cherry-table-insert-right.svg +16 -0
  138. package/src/sass/icons/uEA57-cherry-table-insert-top.svg +16 -0
  139. package/src/sass/icons/uEA58-sort-s.svg +13 -0
  140. package/src/sass/icons/uEA59-pinyin.svg +1 -0
  141. package/src/sass/icons/uEA5A-create.svg +24 -0
  142. package/src/sass/icons/uEA5B-download.svg +34 -0
  143. package/src/sass/icons/uEA5C-edit.svg +3 -0
  144. package/src/sass/icons/uEA5D-export.svg +53 -0
  145. package/src/sass/icons/uEA5E-folder-open.svg +3 -0
  146. package/src/sass/icons/uEA5F-folder.svg +3 -0
  147. package/src/sass/icons/uEA60-help.svg +5 -0
  148. package/src/sass/icons/uEA61-pen-fill.svg +13 -0
  149. package/src/sass/icons/uEA62-pen.svg +3 -0
  150. package/src/sass/icons/uEA64-tips.svg +5 -0
  151. package/src/sass/icons/uEA65-warn.svg +5 -0
  152. package/src/sass/icons/uEA66-mistake.svg +4 -0
  153. package/src/sass/icons/uEA67-success.svg +4 -0
  154. package/src/sass/icons/uEA68-danger.svg +4 -0
  155. package/src/sass/icons/uEA69-info.svg +5 -0
  156. package/src/sass/icons/uEA6A-primary.svg +5 -0
  157. package/src/sass/icons/uEA6B-warning.svg +5 -0
  158. package/src/sass/icons/uEA6C-justify.svg +19 -0
  159. package/src/sass/icons/uEA6D-justifyCenter.svg +19 -0
  160. package/src/sass/icons/uEA6E-justifyLeft.svg +19 -0
  161. package/src/sass/icons/uEA6F-justifyRight.svg +19 -0
  162. package/src/sass/icons/uEA70-chevronsLeft.svg +1 -0
  163. package/src/sass/icons/uEA71-chevronsRight.svg +1 -0
  164. package/src/sass/icons/uEA72-trendingUp.svg +1 -0
  165. package/src/sass/icons/uEA74-codeBlock.svg +1 -0
  166. package/src/sass/icons/uEA75-expand.svg +3 -0
  167. package/src/sass/icons/uEA76-unExpand.svg +3 -0
  168. package/src/sass/icons/uEA77-swap-vert.svg +1 -0
  169. package/src/sass/icons/uEA78-swap.svg +1 -0
  170. package/src/sass/icons/uEA79-keyboard.svg +1 -0
  171. package/src/sass/icons/uEA7A-command.svg +1 -0
  172. package/src/sass/icons/uEA7B-search.svg +1 -0
  173. package/src/sass/index.scss +3 -0
  174. package/src/sass/markdown.scss +668 -0
  175. package/src/sass/markdown_pure.scss +9 -0
  176. package/src/sass/prettyprint/prettyprint.scss +118 -0
  177. package/src/sass/previewer.scss +179 -0
  178. package/src/sass/print.scss +13 -0
  179. package/src/sass/prism/coy.scss +220 -0
  180. package/src/sass/prism/dark.scss +132 -0
  181. package/src/sass/prism/default.scss +143 -0
  182. package/src/sass/prism/funky.scss +133 -0
  183. package/src/sass/prism/okaidia.scss +126 -0
  184. package/src/sass/prism/one-dark.scss +440 -0
  185. package/src/sass/prism/one-light.scss +428 -0
  186. package/src/sass/prism/solarized-light.scss +153 -0
  187. package/src/sass/prism/tomorrow-night.scss +125 -0
  188. package/src/sass/prism/twilight.scss +202 -0
  189. package/src/sass/prism/vs-dark.scss +275 -0
  190. package/src/sass/prism/vs-light.scss +168 -0
  191. package/src/sass/themes/blue.scss +411 -0
  192. package/src/sass/themes/dark.scss +517 -0
  193. package/src/sass/themes/default.scss +255 -0
  194. package/src/sass/themes/green.scss +395 -0
  195. package/src/sass/themes/light.scss +368 -0
  196. package/src/sass/themes/red.scss +397 -0
  197. package/src/sass/themes/violet.scss +410 -0
  198. package/src/sass/variable.scss +84 -0
  199. package/src/toolbars/Bubble.js +234 -0
  200. package/src/toolbars/BubbleFormula.js +298 -0
  201. package/src/toolbars/BubbleTable.js +147 -0
  202. package/src/toolbars/FloatMenu.js +131 -0
  203. package/src/toolbars/HiddenToolbar.js +36 -0
  204. package/src/toolbars/HookCenter.js +234 -0
  205. package/src/toolbars/MenuBase.js +569 -0
  206. package/src/toolbars/PreviewerBubble.js +608 -0
  207. package/src/toolbars/ShortcutKeyConfigPanel.js +345 -0
  208. package/src/toolbars/Sidebar.js +36 -0
  209. package/src/toolbars/Toc.js +242 -0
  210. package/src/toolbars/Toolbar.js +449 -0
  211. package/src/toolbars/ToolbarRight.js +37 -0
  212. package/src/toolbars/hooks/Audio.js +79 -0
  213. package/src/toolbars/hooks/BarTable.js +41 -0
  214. package/src/toolbars/hooks/Bold.js +73 -0
  215. package/src/toolbars/hooks/Br.js +34 -0
  216. package/src/toolbars/hooks/ChangeLocale.js +62 -0
  217. package/src/toolbars/hooks/ChatGpt.js +182 -0
  218. package/src/toolbars/hooks/CheckList.js +41 -0
  219. package/src/toolbars/hooks/Code.js +49 -0
  220. package/src/toolbars/hooks/CodeTheme.js +66 -0
  221. package/src/toolbars/hooks/Color.js +298 -0
  222. package/src/toolbars/hooks/Copy.js +141 -0
  223. package/src/toolbars/hooks/Detail.js +69 -0
  224. package/src/toolbars/hooks/DrawIo.js +57 -0
  225. package/src/toolbars/hooks/Export.js +49 -0
  226. package/src/toolbars/hooks/File.js +79 -0
  227. package/src/toolbars/hooks/Formula.js +69 -0
  228. package/src/toolbars/hooks/FullScreen.js +50 -0
  229. package/src/toolbars/hooks/Graph.js +263 -0
  230. package/src/toolbars/hooks/H1.js +71 -0
  231. package/src/toolbars/hooks/H2.js +71 -0
  232. package/src/toolbars/hooks/H3.js +71 -0
  233. package/src/toolbars/hooks/Header.js +118 -0
  234. package/src/toolbars/hooks/Hr.js +35 -0
  235. package/src/toolbars/hooks/Image.js +91 -0
  236. package/src/toolbars/hooks/InlineCode.js +53 -0
  237. package/src/toolbars/hooks/Insert.js +193 -0
  238. package/src/toolbars/hooks/Italic.js +72 -0
  239. package/src/toolbars/hooks/Justify.js +49 -0
  240. package/src/toolbars/hooks/LineTable.js +41 -0
  241. package/src/toolbars/hooks/Link.js +49 -0
  242. package/src/toolbars/hooks/List.js +55 -0
  243. package/src/toolbars/hooks/MobilePreview.js +44 -0
  244. package/src/toolbars/hooks/Ol.js +41 -0
  245. package/src/toolbars/hooks/Panel.js +140 -0
  246. package/src/toolbars/hooks/Pdf.js +78 -0
  247. package/src/toolbars/hooks/Publish.js +123 -0
  248. package/src/toolbars/hooks/QuickTable.js +43 -0
  249. package/src/toolbars/hooks/Quote.js +45 -0
  250. package/src/toolbars/hooks/Redo.js +33 -0
  251. package/src/toolbars/hooks/Ruby.js +59 -0
  252. package/src/toolbars/hooks/Search.js +53 -0
  253. package/src/toolbars/hooks/Settings.js +220 -0
  254. package/src/toolbars/hooks/ShortcutKey.js +62 -0
  255. package/src/toolbars/hooks/Size.js +118 -0
  256. package/src/toolbars/hooks/Split.js +37 -0
  257. package/src/toolbars/hooks/Strikethrough.js +71 -0
  258. package/src/toolbars/hooks/Sub.js +58 -0
  259. package/src/toolbars/hooks/Sup.js +58 -0
  260. package/src/toolbars/hooks/SwitchModel.js +56 -0
  261. package/src/toolbars/hooks/Table.js +56 -0
  262. package/src/toolbars/hooks/Theme.js +62 -0
  263. package/src/toolbars/hooks/Toc.js +35 -0
  264. package/src/toolbars/hooks/TogglePreview.js +91 -0
  265. package/src/toolbars/hooks/Ul.js +41 -0
  266. package/src/toolbars/hooks/Underline.js +68 -0
  267. package/src/toolbars/hooks/Undo.js +30 -0
  268. package/src/toolbars/hooks/Video.js +79 -0
  269. package/src/toolbars/hooks/Word.js +78 -0
  270. package/src/toolbars/hooks/WordCount.js +106 -0
  271. package/src/utils/autoindent.js +58 -0
  272. package/src/utils/cm-search-replace.js +794 -0
  273. package/src/utils/code-preview-language-setting.js +180 -0
  274. package/src/utils/codeBlockContentHandler.js +400 -0
  275. package/src/utils/config.js +174 -0
  276. package/src/utils/copy.js +55 -0
  277. package/src/utils/dialog.js +214 -0
  278. package/src/utils/dom.js +163 -0
  279. package/src/utils/downloadUtil.js +23 -0
  280. package/src/utils/env.js +22 -0
  281. package/src/utils/error.js +61 -0
  282. package/src/utils/event.js +38 -0
  283. package/src/utils/export.js +166 -0
  284. package/src/utils/file.js +164 -0
  285. package/src/utils/formulaUtilsHandler.js +232 -0
  286. package/src/utils/htmlparser.js +976 -0
  287. package/src/utils/image.js +99 -0
  288. package/src/utils/imgSizeHandler.js +279 -0
  289. package/src/utils/lazyLoadImg.js +327 -0
  290. package/src/utils/lineFeed.js +49 -0
  291. package/src/utils/listContentHandler.js +227 -0
  292. package/src/utils/lookbehind-replace.js +81 -0
  293. package/src/utils/mathjax.js +89 -0
  294. package/src/utils/myersDiff.js +211 -0
  295. package/src/utils/pasteHelper.js +253 -0
  296. package/src/utils/platformTransform.js +71 -0
  297. package/src/utils/recount-pos.js +59 -0
  298. package/src/utils/regexp.js +295 -0
  299. package/src/utils/sanitize.js +477 -0
  300. package/src/utils/selection.js +50 -0
  301. package/src/utils/shortcutKey.js +291 -0
  302. package/src/utils/svgUtils.js +96 -0
  303. package/src/utils/tableContentHandler.js +876 -0
  304. package/test/core/CommonMark.spec.ts +62 -0
  305. package/test/core/hooks/AutoLink.spec.ts +28 -0
  306. package/test/core/hooks/List.spec.ts +79 -0
  307. package/test/core/hooks/__snapshots__/List.spec.ts.snap +11 -0
  308. package/test/example.md +778 -0
  309. package/test/node.js +10 -0
  310. package/test/suites/commonmark.spec.json +5218 -0
  311. package/test/tsconfig.test.json +6 -0
  312. package/test/utils/regexp.spec.ts +28 -0
  313. package/types/cherry.d.ts +675 -0
  314. package/types/codemirror.d.ts +22 -0
  315. package/types/editor.d.ts +72 -0
  316. package/types/global.d.ts +16 -0
  317. package/types/menus.d.ts +24 -0
  318. package/types/previewer.d.ts +53 -0
  319. package/types/syntax.d.ts +52 -0
@@ -0,0 +1,1663 @@
1
+ // @ts-nocheck
2
+ /*
3
+ * $Id: rawdeflate.js,v 0.3 2009/03/01 19:05:05 dankogai Exp dankogai $
4
+ *
5
+ * Original:
6
+ * http://www.onicos.com/staff/iz/amuse/javascript/expert/deflate.txt
7
+ */
8
+
9
+ // if run as a web worker, respond to messages by deflating them
10
+ const deflate = (function () {
11
+ /* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
12
+ * Version: 1.0.1
13
+ * LastModified: Dec 25 1999
14
+ */
15
+
16
+ /* Interface:
17
+ * data = deflate(src);
18
+ */
19
+
20
+ /* constant parameters */
21
+ const zip_WSIZE = 32768; // Sliding Window size
22
+ const zip_STORED_BLOCK = 0;
23
+ const zip_STATIC_TREES = 1;
24
+ const zip_DYN_TREES = 2;
25
+
26
+ /* for deflate */
27
+ const zip_DEFAULT_LEVEL = 6;
28
+ const zip_FULL_SEARCH = true;
29
+ const zip_INBUFSIZ = 32768; // Input buffer size
30
+ const zip_INBUF_EXTRA = 64; // Extra buffer
31
+ const zip_OUTBUFSIZ = 1024 * 8;
32
+ const zip_window_size = 2 * zip_WSIZE;
33
+ const zip_MIN_MATCH = 3;
34
+ const zip_MAX_MATCH = 258;
35
+ const zip_BITS = 16;
36
+ // for SMALL_MEM
37
+ const zip_LIT_BUFSIZE = 0x2000;
38
+ const zip_HASH_BITS = 13;
39
+ // for MEDIUM_MEM
40
+ // var zip_LIT_BUFSIZE = 0x4000;
41
+ // var zip_HASH_BITS = 14;
42
+ // for BIG_MEM
43
+ // var zip_LIT_BUFSIZE = 0x8000;
44
+ // var zip_HASH_BITS = 15;
45
+ // if(zip_LIT_BUFSIZE > zip_INBUFSIZ)
46
+ // alert("error: zip_INBUFSIZ is too small");
47
+ // if((zip_WSIZE<<1) > (1<<zip_BITS))
48
+ // alert("error: zip_WSIZE is too large");
49
+ // if(zip_HASH_BITS > zip_BITS-1)
50
+ // alert("error: zip_HASH_BITS is too large");
51
+ // if(zip_HASH_BITS < 8 || zip_MAX_MATCH != 258)
52
+ // alert("error: Code too clever");
53
+ const zip_DIST_BUFSIZE = zip_LIT_BUFSIZE;
54
+ const zip_HASH_SIZE = 1 << zip_HASH_BITS;
55
+ const zip_HASH_MASK = zip_HASH_SIZE - 1;
56
+ const zip_WMASK = zip_WSIZE - 1;
57
+ const zip_NIL = 0; // Tail of hash chains
58
+ const zip_TOO_FAR = 4096;
59
+ const zip_MIN_LOOKAHEAD = zip_MAX_MATCH + zip_MIN_MATCH + 1;
60
+ const zip_MAX_DIST = zip_WSIZE - zip_MIN_LOOKAHEAD;
61
+ const zip_SMALLEST = 1;
62
+ const zip_MAX_BITS = 15;
63
+ const zip_MAX_BL_BITS = 7;
64
+ const zip_LENGTH_CODES = 29;
65
+ const zip_LITERALS = 256;
66
+ const zip_END_BLOCK = 256;
67
+ const zip_L_CODES = zip_LITERALS + 1 + zip_LENGTH_CODES;
68
+ const zip_D_CODES = 30;
69
+ const zip_BL_CODES = 19;
70
+ const zip_REP_3_6 = 16;
71
+ const zip_REPZ_3_10 = 17;
72
+ const zip_REPZ_11_138 = 18;
73
+ const zip_HEAP_SIZE = 2 * zip_L_CODES + 1;
74
+ const zip_H_SHIFT = parseInt((zip_HASH_BITS + zip_MIN_MATCH - 1) / zip_MIN_MATCH);
75
+
76
+ /* variables */
77
+ let zip_free_queue;
78
+ let zip_qhead;
79
+ let zip_qtail;
80
+ let zip_initflag;
81
+ let zip_outbuf = null;
82
+ let zip_outcnt;
83
+ let zip_outoff;
84
+ let zip_complete;
85
+ let zip_window;
86
+ let zip_d_buf;
87
+ let zip_l_buf;
88
+ let zip_prev;
89
+ let zip_bi_buf;
90
+ let zip_bi_valid;
91
+ let zip_block_start;
92
+ let zip_ins_h;
93
+ let zip_hash_head;
94
+ let zip_prev_match;
95
+ let zip_match_available;
96
+ let zip_match_length;
97
+ let zip_prev_length;
98
+ let zip_strstart;
99
+ let zip_match_start;
100
+ let zip_eofile;
101
+ let zip_lookahead;
102
+ let zip_max_chain_length;
103
+ let zip_max_lazy_match;
104
+ let zip_compr_level;
105
+ let zip_good_match;
106
+ let zip_nice_match;
107
+ let zip_dyn_ltree;
108
+ let zip_dyn_dtree;
109
+ let zip_static_ltree;
110
+ let zip_static_dtree;
111
+ let zip_bl_tree;
112
+ let zip_l_desc;
113
+ let zip_d_desc;
114
+ let zip_bl_desc;
115
+ let zip_bl_count;
116
+ let zip_heap;
117
+ let zip_heap_len;
118
+ let zip_heap_max;
119
+ let zip_depth;
120
+ let zip_length_code;
121
+ let zip_dist_code;
122
+ let zip_base_length;
123
+ let zip_base_dist;
124
+ let zip_flag_buf;
125
+ let zip_last_lit;
126
+ let zip_last_dist;
127
+ let zip_last_flags;
128
+ let zip_flags;
129
+ let zip_flag_bit;
130
+ let zip_opt_len;
131
+ let zip_static_len;
132
+ let zip_deflate_data;
133
+ let zip_deflate_pos;
134
+
135
+ /* objects (deflate) */
136
+
137
+ function zip_DeflateCT() {
138
+ this.fc = 0; // frequency count or bit string
139
+ this.dl = 0; // father node in Huffman tree or length of bit string
140
+ }
141
+
142
+ function zip_DeflateTreeDesc() {
143
+ this.dyn_tree = null; // the dynamic tree
144
+ this.static_tree = null; // corresponding static tree or NULL
145
+ this.extra_bits = null; // extra bits for each code or NULL
146
+ this.extra_base = 0; // base index for extra_bits
147
+ this.elems = 0; // max number of elements in the tree
148
+ this.max_length = 0; // max bit length for the codes
149
+ this.max_code = 0; // largest code with non zero frequency
150
+ }
151
+
152
+ /* Values for max_lazy_match, good_match and max_chain_length, depending on
153
+ * the desired pack level (0..9). The values given below have been tuned to
154
+ * exclude worst case performance for pathological files. Better values may be
155
+ * found for specific files.
156
+ */
157
+ function zip_DeflateConfiguration(a, b, c, d) {
158
+ this.good_length = a; // reduce lazy search above this match length
159
+ this.max_lazy = b; // do not perform lazy search above this match length
160
+ this.nice_length = c; // quit search above this match length
161
+ this.max_chain = d;
162
+ }
163
+
164
+ function zip_DeflateBuffer() {
165
+ this.next = null;
166
+ this.len = 0;
167
+ this.ptr = new Array(zip_OUTBUFSIZ);
168
+ this.off = 0;
169
+ }
170
+
171
+ /* constant tables */
172
+ const zip_extra_lbits = [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];
173
+ const zip_extra_dbits = [
174
+ 0,
175
+ 0,
176
+ 0,
177
+ 0,
178
+ 1,
179
+ 1,
180
+ 2,
181
+ 2,
182
+ 3,
183
+ 3,
184
+ 4,
185
+ 4,
186
+ 5,
187
+ 5,
188
+ 6,
189
+ 6,
190
+ 7,
191
+ 7,
192
+ 8,
193
+ 8,
194
+ 9,
195
+ 9,
196
+ 10,
197
+ 10,
198
+ 11,
199
+ 11,
200
+ 12,
201
+ 12,
202
+ 13,
203
+ 13,
204
+ ];
205
+ const zip_extra_blbits = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7];
206
+ const zip_bl_order = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
207
+ const zip_configuration_table = [
208
+ new zip_DeflateConfiguration(0, 0, 0, 0),
209
+ new zip_DeflateConfiguration(4, 4, 8, 4),
210
+ new zip_DeflateConfiguration(4, 5, 16, 8),
211
+ new zip_DeflateConfiguration(4, 6, 32, 32),
212
+ new zip_DeflateConfiguration(4, 4, 16, 16),
213
+ new zip_DeflateConfiguration(8, 16, 32, 32),
214
+ new zip_DeflateConfiguration(8, 16, 128, 128),
215
+ new zip_DeflateConfiguration(8, 32, 128, 256),
216
+ new zip_DeflateConfiguration(32, 128, 258, 1024),
217
+ new zip_DeflateConfiguration(32, 258, 258, 4096),
218
+ ];
219
+
220
+ /* routines (deflate) */
221
+
222
+ function zip_deflate_start(level) {
223
+ let i;
224
+
225
+ if (!level) level = zip_DEFAULT_LEVEL;
226
+ else if (level < 1) level = 1;
227
+ else if (level > 9) level = 9;
228
+
229
+ zip_compr_level = level;
230
+ zip_initflag = false;
231
+ zip_eofile = false;
232
+ if (zip_outbuf != null) return;
233
+
234
+ zip_free_queue = zip_qhead = zip_qtail = null;
235
+ zip_outbuf = new Array(zip_OUTBUFSIZ);
236
+ zip_window = new Array(zip_window_size);
237
+ zip_d_buf = new Array(zip_DIST_BUFSIZE);
238
+ zip_l_buf = new Array(zip_INBUFSIZ + zip_INBUF_EXTRA);
239
+ zip_prev = new Array(1 << zip_BITS);
240
+ zip_dyn_ltree = new Array(zip_HEAP_SIZE);
241
+ for (i = 0; i < zip_HEAP_SIZE; i++) zip_dyn_ltree[i] = new zip_DeflateCT();
242
+ zip_dyn_dtree = new Array(2 * zip_D_CODES + 1);
243
+ for (i = 0; i < 2 * zip_D_CODES + 1; i++) zip_dyn_dtree[i] = new zip_DeflateCT();
244
+ zip_static_ltree = new Array(zip_L_CODES + 2);
245
+ for (i = 0; i < zip_L_CODES + 2; i++) zip_static_ltree[i] = new zip_DeflateCT();
246
+ zip_static_dtree = new Array(zip_D_CODES);
247
+ for (i = 0; i < zip_D_CODES; i++) zip_static_dtree[i] = new zip_DeflateCT();
248
+ zip_bl_tree = new Array(2 * zip_BL_CODES + 1);
249
+ for (i = 0; i < 2 * zip_BL_CODES + 1; i++) zip_bl_tree[i] = new zip_DeflateCT();
250
+ zip_l_desc = new zip_DeflateTreeDesc();
251
+ zip_d_desc = new zip_DeflateTreeDesc();
252
+ zip_bl_desc = new zip_DeflateTreeDesc();
253
+ zip_bl_count = new Array(zip_MAX_BITS + 1);
254
+ zip_heap = new Array(2 * zip_L_CODES + 1);
255
+ zip_depth = new Array(2 * zip_L_CODES + 1);
256
+ zip_length_code = new Array(zip_MAX_MATCH - zip_MIN_MATCH + 1);
257
+ zip_dist_code = new Array(512);
258
+ zip_base_length = new Array(zip_LENGTH_CODES);
259
+ zip_base_dist = new Array(zip_D_CODES);
260
+ zip_flag_buf = new Array(parseInt(zip_LIT_BUFSIZE / 8));
261
+ }
262
+
263
+ function zip_deflate_end() {
264
+ zip_free_queue = zip_qhead = zip_qtail = null;
265
+ zip_outbuf = null;
266
+ zip_window = null;
267
+ zip_d_buf = null;
268
+ zip_l_buf = null;
269
+ zip_prev = null;
270
+ zip_dyn_ltree = null;
271
+ zip_dyn_dtree = null;
272
+ zip_static_ltree = null;
273
+ zip_static_dtree = null;
274
+ zip_bl_tree = null;
275
+ zip_l_desc = null;
276
+ zip_d_desc = null;
277
+ zip_bl_desc = null;
278
+ zip_bl_count = null;
279
+ zip_heap = null;
280
+ zip_depth = null;
281
+ zip_length_code = null;
282
+ zip_dist_code = null;
283
+ zip_base_length = null;
284
+ zip_base_dist = null;
285
+ zip_flag_buf = null;
286
+ }
287
+
288
+ function zip_reuse_queue(p) {
289
+ p.next = zip_free_queue;
290
+ zip_free_queue = p;
291
+ }
292
+
293
+ function zip_new_queue() {
294
+ let p;
295
+
296
+ if (zip_free_queue != null) {
297
+ p = zip_free_queue;
298
+ zip_free_queue = zip_free_queue.next;
299
+ } else p = new zip_DeflateBuffer();
300
+ p.next = null;
301
+ p.len = p.off = 0;
302
+
303
+ return p;
304
+ }
305
+
306
+ function zip_head1(i) {
307
+ return zip_prev[zip_WSIZE + i];
308
+ }
309
+
310
+ function zip_head2(i, val) {
311
+ return (zip_prev[zip_WSIZE + i] = val);
312
+ }
313
+
314
+ /* put_byte is used for the compressed output, put_ubyte for the
315
+ * uncompressed output. However unlzw() uses window for its
316
+ * suffix table instead of its output buffer, so it does not use put_ubyte
317
+ * (to be cleaned up).
318
+ */
319
+ function zip_put_byte(c) {
320
+ zip_outbuf[zip_outoff + zip_outcnt++] = c;
321
+ if (zip_outoff + zip_outcnt == zip_OUTBUFSIZ) zip_qoutbuf();
322
+ }
323
+
324
+ /* Output a 16 bit value, lsb first */
325
+ function zip_put_short(w) {
326
+ w &= 0xffff;
327
+ if (zip_outoff + zip_outcnt < zip_OUTBUFSIZ - 2) {
328
+ zip_outbuf[zip_outoff + zip_outcnt++] = w & 0xff;
329
+ zip_outbuf[zip_outoff + zip_outcnt++] = w >>> 8;
330
+ } else {
331
+ zip_put_byte(w & 0xff);
332
+ zip_put_byte(w >>> 8);
333
+ }
334
+ }
335
+
336
+ /* ==========================================================================
337
+ * Insert string s in the dictionary and set match_head to the previous head
338
+ * of the hash chain (the most recent string with same hash key). Return
339
+ * the previous length of the hash chain.
340
+ * IN assertion: all calls to to INSERT_STRING are made with consecutive
341
+ * input characters and the first MIN_MATCH bytes of s are valid
342
+ * (except for the last MIN_MATCH-1 bytes of the input file).
343
+ */
344
+ function zip_INSERT_STRING() {
345
+ zip_ins_h = ((zip_ins_h << zip_H_SHIFT) ^ (zip_window[zip_strstart + zip_MIN_MATCH - 1] & 0xff)) & zip_HASH_MASK;
346
+ zip_hash_head = zip_head1(zip_ins_h);
347
+ zip_prev[zip_strstart & zip_WMASK] = zip_hash_head;
348
+ zip_head2(zip_ins_h, zip_strstart);
349
+ }
350
+
351
+ /* Send a code of the given tree. c and tree must not have side effects */
352
+ function zip_SEND_CODE(c, tree) {
353
+ zip_send_bits(tree[c].fc, tree[c].dl);
354
+ }
355
+
356
+ /* Mapping from a distance to a distance code. dist is the distance - 1 and
357
+ * must not have side effects. dist_code[256] and dist_code[257] are never
358
+ * used.
359
+ */
360
+ function zip_D_CODE(dist) {
361
+ return (dist < 256 ? zip_dist_code[dist] : zip_dist_code[256 + (dist >> 7)]) & 0xff;
362
+ }
363
+
364
+ /* ==========================================================================
365
+ * Compares to subtrees, using the tree depth as tie breaker when
366
+ * the subtrees have equal frequency. This minimizes the worst case length.
367
+ */
368
+ function zip_SMALLER(tree, n, m) {
369
+ return tree[n].fc < tree[m].fc || (tree[n].fc == tree[m].fc && zip_depth[n] <= zip_depth[m]);
370
+ }
371
+
372
+ /* ==========================================================================
373
+ * read string data
374
+ */
375
+ function zip_read_buff(buff, offset, n) {
376
+ let i;
377
+ for (i = 0; i < n && zip_deflate_pos < zip_deflate_data.length; i++)
378
+ buff[offset + i] = zip_deflate_data.charCodeAt(zip_deflate_pos++) & 0xff;
379
+ return i;
380
+ }
381
+
382
+ /* ==========================================================================
383
+ * Initialize the "longest match" routines for a new file
384
+ */
385
+ function zip_lm_init() {
386
+ let j;
387
+
388
+ /* Initialize the hash table. */
389
+ for (j = 0; j < zip_HASH_SIZE; j++)
390
+ // zip_head2(j, zip_NIL);
391
+ zip_prev[zip_WSIZE + j] = 0;
392
+ /* prev will be initialized on the fly */
393
+
394
+ /* Set the default configuration parameters:
395
+ */
396
+ zip_max_lazy_match = zip_configuration_table[zip_compr_level].max_lazy;
397
+ zip_good_match = zip_configuration_table[zip_compr_level].good_length;
398
+ if (!zip_FULL_SEARCH) zip_nice_match = zip_configuration_table[zip_compr_level].nice_length;
399
+ zip_max_chain_length = zip_configuration_table[zip_compr_level].max_chain;
400
+
401
+ zip_strstart = 0;
402
+ zip_block_start = 0;
403
+
404
+ zip_lookahead = zip_read_buff(zip_window, 0, 2 * zip_WSIZE);
405
+ if (zip_lookahead <= 0) {
406
+ zip_eofile = true;
407
+ zip_lookahead = 0;
408
+ return;
409
+ }
410
+ zip_eofile = false;
411
+ /* Make sure that we always have enough lookahead. This is important
412
+ * if input comes from a device such as a tty.
413
+ */
414
+ while (zip_lookahead < zip_MIN_LOOKAHEAD && !zip_eofile) zip_fill_window();
415
+
416
+ /* If lookahead < MIN_MATCH, ins_h is garbage, but this is
417
+ * not important since only literal bytes will be emitted.
418
+ */
419
+ zip_ins_h = 0;
420
+ for (j = 0; j < zip_MIN_MATCH - 1; j++) {
421
+ // UPDATE_HASH(ins_h, window[j]);
422
+ zip_ins_h = ((zip_ins_h << zip_H_SHIFT) ^ (zip_window[j] & 0xff)) & zip_HASH_MASK;
423
+ }
424
+ }
425
+
426
+ /* ==========================================================================
427
+ * Set match_start to the longest match starting at the given string and
428
+ * return its length. Matches shorter or equal to prev_length are discarded,
429
+ * in which case the result is equal to prev_length and match_start is
430
+ * garbage.
431
+ * IN assertions: cur_match is the head of the hash chain for the current
432
+ * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
433
+ */
434
+ function zip_longest_match(cur_match) {
435
+ let chain_length = zip_max_chain_length; // max hash chain length
436
+ let scanp = zip_strstart; // current string
437
+ let matchp; // matched string
438
+ let len; // length of current match
439
+ let best_len = zip_prev_length; // best match length so far
440
+
441
+ /* Stop when cur_match becomes <= limit. To simplify the code,
442
+ * we prevent matches with the string of window index 0.
443
+ */
444
+ const limit = zip_strstart > zip_MAX_DIST ? zip_strstart - zip_MAX_DIST : zip_NIL;
445
+
446
+ const strendp = zip_strstart + zip_MAX_MATCH;
447
+ let scan_end1 = zip_window[scanp + best_len - 1];
448
+ let scan_end = zip_window[scanp + best_len];
449
+
450
+ /* Do not waste too much time if we already have a good match: */
451
+ if (zip_prev_length >= zip_good_match) chain_length >>= 2;
452
+
453
+ // Assert(encoder->strstart <= window_size-MIN_LOOKAHEAD, "insufficient lookahead");
454
+
455
+ do {
456
+ // Assert(cur_match < encoder->strstart, "no future");
457
+ matchp = cur_match;
458
+
459
+ /* Skip to next match if the match length cannot increase
460
+ * or if the match length is less than 2:
461
+ */
462
+ if (
463
+ zip_window[matchp + best_len] != scan_end ||
464
+ zip_window[matchp + best_len - 1] != scan_end1 ||
465
+ zip_window[matchp] != zip_window[scanp] ||
466
+ zip_window[++matchp] != zip_window[scanp + 1]
467
+ ) {
468
+ continue;
469
+ }
470
+
471
+ /* The check at best_len-1 can be removed because it will be made
472
+ * again later. (This heuristic is not always a win.)
473
+ * It is not necessary to compare scan[2] and match[2] since they
474
+ * are always equal when the other bytes match, given that
475
+ * the hash keys are equal and that HASH_BITS >= 8.
476
+ */
477
+ scanp += 2;
478
+ matchp++;
479
+
480
+ /* We check for insufficient lookahead only every 8th comparison;
481
+ * the 256th check will be made at strstart+258.
482
+ */
483
+ do {} while (
484
+ zip_window[++scanp] == zip_window[++matchp] &&
485
+ zip_window[++scanp] == zip_window[++matchp] &&
486
+ zip_window[++scanp] == zip_window[++matchp] &&
487
+ zip_window[++scanp] == zip_window[++matchp] &&
488
+ zip_window[++scanp] == zip_window[++matchp] &&
489
+ zip_window[++scanp] == zip_window[++matchp] &&
490
+ zip_window[++scanp] == zip_window[++matchp] &&
491
+ zip_window[++scanp] == zip_window[++matchp] &&
492
+ scanp < strendp
493
+ );
494
+
495
+ len = zip_MAX_MATCH - (strendp - scanp);
496
+ scanp = strendp - zip_MAX_MATCH;
497
+
498
+ if (len > best_len) {
499
+ zip_match_start = cur_match;
500
+ best_len = len;
501
+ if (zip_FULL_SEARCH) {
502
+ if (len >= zip_MAX_MATCH) break;
503
+ } else {
504
+ if (len >= zip_nice_match) break;
505
+ }
506
+
507
+ scan_end1 = zip_window[scanp + best_len - 1];
508
+ scan_end = zip_window[scanp + best_len];
509
+ }
510
+ } while ((cur_match = zip_prev[cur_match & zip_WMASK]) > limit && --chain_length != 0);
511
+
512
+ return best_len;
513
+ }
514
+
515
+ /* ==========================================================================
516
+ * Fill the window when the lookahead becomes insufficient.
517
+ * Updates strstart and lookahead, and sets eofile if end of input file.
518
+ * IN assertion: lookahead < MIN_LOOKAHEAD && strstart + lookahead > 0
519
+ * OUT assertions: at least one byte has been read, or eofile is set;
520
+ * file reads are performed for at least two bytes (required for the
521
+ * translate_eol option).
522
+ */
523
+ function zip_fill_window() {
524
+ let n;
525
+ let m;
526
+
527
+ // Amount of free space at the end of the window.
528
+ let more = zip_window_size - zip_lookahead - zip_strstart;
529
+
530
+ /* If the window is almost full and there is insufficient lookahead,
531
+ * move the upper half to the lower one to make room in the upper half.
532
+ */
533
+ if (more == -1) {
534
+ /* Very unlikely, but possible on 16 bit machine if strstart == 0
535
+ * and lookahead == 1 (input done one byte at time)
536
+ */
537
+ more--;
538
+ } else if (zip_strstart >= zip_WSIZE + zip_MAX_DIST) {
539
+ /* By the IN assertion, the window is not empty so we can't confuse
540
+ * more == 0 with more == 64K on a 16 bit machine.
541
+ */
542
+ // Assert(window_size == (ulg)2*WSIZE, "no sliding with BIG_MEM");
543
+
544
+ // System.arraycopy(window, WSIZE, window, 0, WSIZE);
545
+ for (n = 0; n < zip_WSIZE; n++) zip_window[n] = zip_window[n + zip_WSIZE];
546
+
547
+ zip_match_start -= zip_WSIZE;
548
+ zip_strstart -= zip_WSIZE; /* we now have strstart >= MAX_DIST: */
549
+ zip_block_start -= zip_WSIZE;
550
+
551
+ for (n = 0; n < zip_HASH_SIZE; n++) {
552
+ m = zip_head1(n);
553
+ zip_head2(n, m >= zip_WSIZE ? m - zip_WSIZE : zip_NIL);
554
+ }
555
+ for (n = 0; n < zip_WSIZE; n++) {
556
+ /* If n is not on any hash chain, prev[n] is garbage but
557
+ * its value will never be used.
558
+ */
559
+ m = zip_prev[n];
560
+ zip_prev[n] = m >= zip_WSIZE ? m - zip_WSIZE : zip_NIL;
561
+ }
562
+ more += zip_WSIZE;
563
+ }
564
+ // At this point, more >= 2
565
+ if (!zip_eofile) {
566
+ n = zip_read_buff(zip_window, zip_strstart + zip_lookahead, more);
567
+ if (n <= 0) zip_eofile = true;
568
+ else zip_lookahead += n;
569
+ }
570
+ }
571
+
572
+ /* ==========================================================================
573
+ * Processes a new input file and return its compressed length. This
574
+ * function does not perform lazy evaluationof matches and inserts
575
+ * new strings in the dictionary only for unmatched strings or for short
576
+ * matches. It is used only for the fast compression options.
577
+ */
578
+ function zip_deflate_fast() {
579
+ while (zip_lookahead != 0 && zip_qhead == null) {
580
+ var flush; // set if current block must be flushed
581
+
582
+ /* Insert the string window[strstart .. strstart+2] in the
583
+ * dictionary, and set hash_head to the head of the hash chain:
584
+ */
585
+ zip_INSERT_STRING();
586
+
587
+ /* Find the longest match, discarding those <= prev_length.
588
+ * At this point we have always match_length < MIN_MATCH
589
+ */
590
+ if (zip_hash_head != zip_NIL && zip_strstart - zip_hash_head <= zip_MAX_DIST) {
591
+ /* To simplify the code, we prevent matches with the string
592
+ * of window index 0 (in particular we have to avoid a match
593
+ * of the string with itself at the start of the input file).
594
+ */
595
+ zip_match_length = zip_longest_match(zip_hash_head);
596
+ /* longest_match() sets match_start */
597
+ if (zip_match_length > zip_lookahead) zip_match_length = zip_lookahead;
598
+ }
599
+ if (zip_match_length >= zip_MIN_MATCH) {
600
+ // check_match(strstart, match_start, match_length);
601
+
602
+ flush = zip_ct_tally(zip_strstart - zip_match_start, zip_match_length - zip_MIN_MATCH);
603
+ zip_lookahead -= zip_match_length;
604
+
605
+ /* Insert new strings in the hash table only if the match length
606
+ * is not too large. This saves time but degrades compression.
607
+ */
608
+ if (zip_match_length <= zip_max_lazy_match) {
609
+ zip_match_length--; // string at strstart already in hash table
610
+ do {
611
+ zip_strstart++;
612
+ zip_INSERT_STRING();
613
+ /* strstart never exceeds WSIZE-MAX_MATCH, so there are
614
+ * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
615
+ * these bytes are garbage, but it does not matter since
616
+ * the next lookahead bytes will be emitted as literals.
617
+ */
618
+ } while (--zip_match_length != 0);
619
+ zip_strstart++;
620
+ } else {
621
+ zip_strstart += zip_match_length;
622
+ zip_match_length = 0;
623
+ zip_ins_h = zip_window[zip_strstart] & 0xff;
624
+ // UPDATE_HASH(ins_h, window[strstart + 1]);
625
+ zip_ins_h = ((zip_ins_h << zip_H_SHIFT) ^ (zip_window[zip_strstart + 1] & 0xff)) & zip_HASH_MASK;
626
+
627
+ // #if MIN_MATCH != 3
628
+ // Call UPDATE_HASH() MIN_MATCH-3 more times
629
+ // #endif
630
+ }
631
+ } else {
632
+ /* No match, output a literal byte */
633
+ flush = zip_ct_tally(0, zip_window[zip_strstart] & 0xff);
634
+ zip_lookahead--;
635
+ zip_strstart++;
636
+ }
637
+ if (flush) {
638
+ zip_flush_block(0);
639
+ zip_block_start = zip_strstart;
640
+ }
641
+
642
+ /* Make sure that we always have enough lookahead, except
643
+ * at the end of the input file. We need MAX_MATCH bytes
644
+ * for the next match, plus MIN_MATCH bytes to insert the
645
+ * string following the next match.
646
+ */
647
+ while (zip_lookahead < zip_MIN_LOOKAHEAD && !zip_eofile) zip_fill_window();
648
+ }
649
+ }
650
+
651
+ function zip_deflate_better() {
652
+ /* Process the input block. */
653
+ while (zip_lookahead != 0 && zip_qhead == null) {
654
+ /* Insert the string window[strstart .. strstart+2] in the
655
+ * dictionary, and set hash_head to the head of the hash chain:
656
+ */
657
+ zip_INSERT_STRING();
658
+
659
+ /* Find the longest match, discarding those <= prev_length.
660
+ */
661
+ zip_prev_length = zip_match_length;
662
+ zip_prev_match = zip_match_start;
663
+ zip_match_length = zip_MIN_MATCH - 1;
664
+
665
+ if (
666
+ zip_hash_head != zip_NIL &&
667
+ zip_prev_length < zip_max_lazy_match &&
668
+ zip_strstart - zip_hash_head <= zip_MAX_DIST
669
+ ) {
670
+ /* To simplify the code, we prevent matches with the string
671
+ * of window index 0 (in particular we have to avoid a match
672
+ * of the string with itself at the start of the input file).
673
+ */
674
+ zip_match_length = zip_longest_match(zip_hash_head);
675
+ /* longest_match() sets match_start */
676
+ if (zip_match_length > zip_lookahead) zip_match_length = zip_lookahead;
677
+
678
+ /* Ignore a length 3 match if it is too distant: */
679
+ if (zip_match_length == zip_MIN_MATCH && zip_strstart - zip_match_start > zip_TOO_FAR) {
680
+ /* If prev_match is also MIN_MATCH, match_start is garbage
681
+ * but we will ignore the current match anyway.
682
+ */
683
+ zip_match_length--;
684
+ }
685
+ }
686
+ /* If there was a match at the previous step and the current
687
+ * match is not better, output the previous match:
688
+ */
689
+ if (zip_prev_length >= zip_MIN_MATCH && zip_match_length <= zip_prev_length) {
690
+ var flush; // set if current block must be flushed
691
+
692
+ // check_match(strstart - 1, prev_match, prev_length);
693
+ flush = zip_ct_tally(zip_strstart - 1 - zip_prev_match, zip_prev_length - zip_MIN_MATCH);
694
+
695
+ /* Insert in hash table all strings up to the end of the match.
696
+ * strstart-1 and strstart are already inserted.
697
+ */
698
+ zip_lookahead -= zip_prev_length - 1;
699
+ zip_prev_length -= 2;
700
+ do {
701
+ zip_strstart++;
702
+ zip_INSERT_STRING();
703
+ /* strstart never exceeds WSIZE-MAX_MATCH, so there are
704
+ * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
705
+ * these bytes are garbage, but it does not matter since the
706
+ * next lookahead bytes will always be emitted as literals.
707
+ */
708
+ } while (--zip_prev_length != 0);
709
+ zip_match_available = 0;
710
+ zip_match_length = zip_MIN_MATCH - 1;
711
+ zip_strstart++;
712
+ if (flush) {
713
+ zip_flush_block(0);
714
+ zip_block_start = zip_strstart;
715
+ }
716
+ } else if (zip_match_available != 0) {
717
+ /* If there was no match at the previous position, output a
718
+ * single literal. If there was a match but the current match
719
+ * is longer, truncate the previous match to a single literal.
720
+ */
721
+ if (zip_ct_tally(0, zip_window[zip_strstart - 1] & 0xff)) {
722
+ zip_flush_block(0);
723
+ zip_block_start = zip_strstart;
724
+ }
725
+ zip_strstart++;
726
+ zip_lookahead--;
727
+ } else {
728
+ /* There is no previous match to compare with, wait for
729
+ * the next step to decide.
730
+ */
731
+ zip_match_available = 1;
732
+ zip_strstart++;
733
+ zip_lookahead--;
734
+ }
735
+
736
+ /* Make sure that we always have enough lookahead, except
737
+ * at the end of the input file. We need MAX_MATCH bytes
738
+ * for the next match, plus MIN_MATCH bytes to insert the
739
+ * string following the next match.
740
+ */
741
+ while (zip_lookahead < zip_MIN_LOOKAHEAD && !zip_eofile) zip_fill_window();
742
+ }
743
+ }
744
+
745
+ function zip_init_deflate() {
746
+ if (zip_eofile) return;
747
+ zip_bi_buf = 0;
748
+ zip_bi_valid = 0;
749
+ zip_ct_init();
750
+ zip_lm_init();
751
+
752
+ zip_qhead = null;
753
+ zip_outcnt = 0;
754
+ zip_outoff = 0;
755
+
756
+ if (zip_compr_level <= 3) {
757
+ zip_prev_length = zip_MIN_MATCH - 1;
758
+ zip_match_length = 0;
759
+ } else {
760
+ zip_match_length = zip_MIN_MATCH - 1;
761
+ zip_match_available = 0;
762
+ }
763
+
764
+ zip_complete = false;
765
+ }
766
+
767
+ /* ==========================================================================
768
+ * Same as above, but achieves better compression. We use a lazy
769
+ * evaluation for matches: a match is finally adopted only if there is
770
+ * no better match at the next window position.
771
+ */
772
+ function zip_deflate_internal(buff, off, buff_size) {
773
+ let n;
774
+
775
+ if (!zip_initflag) {
776
+ zip_init_deflate();
777
+ zip_initflag = true;
778
+ if (zip_lookahead == 0) {
779
+ // empty
780
+ zip_complete = true;
781
+ return 0;
782
+ }
783
+ }
784
+
785
+ if ((n = zip_qcopy(buff, off, buff_size)) == buff_size) return buff_size;
786
+
787
+ if (zip_complete) return n;
788
+
789
+ if (zip_compr_level <= 3)
790
+ // optimized for speed
791
+ zip_deflate_fast();
792
+ else zip_deflate_better();
793
+ if (zip_lookahead == 0) {
794
+ if (zip_match_available != 0) zip_ct_tally(0, zip_window[zip_strstart - 1] & 0xff);
795
+ zip_flush_block(1);
796
+ zip_complete = true;
797
+ }
798
+ return n + zip_qcopy(buff, n + off, buff_size - n);
799
+ }
800
+
801
+ function zip_qcopy(buff, off, buff_size) {
802
+ let n;
803
+ let i;
804
+ let j;
805
+
806
+ n = 0;
807
+ while (zip_qhead != null && n < buff_size) {
808
+ i = buff_size - n;
809
+ if (i > zip_qhead.len) i = zip_qhead.len;
810
+ // System.arraycopy(qhead.ptr, qhead.off, buff, off + n, i);
811
+ for (j = 0; j < i; j++) buff[off + n + j] = zip_qhead.ptr[zip_qhead.off + j];
812
+
813
+ zip_qhead.off += i;
814
+ zip_qhead.len -= i;
815
+ n += i;
816
+ if (zip_qhead.len == 0) {
817
+ var p;
818
+ p = zip_qhead;
819
+ zip_qhead = zip_qhead.next;
820
+ zip_reuse_queue(p);
821
+ }
822
+ }
823
+
824
+ if (n == buff_size) return n;
825
+
826
+ if (zip_outoff < zip_outcnt) {
827
+ i = buff_size - n;
828
+ if (i > zip_outcnt - zip_outoff) i = zip_outcnt - zip_outoff;
829
+ // System.arraycopy(outbuf, outoff, buff, off + n, i);
830
+ for (j = 0; j < i; j++) buff[off + n + j] = zip_outbuf[zip_outoff + j];
831
+ zip_outoff += i;
832
+ n += i;
833
+ if (zip_outcnt == zip_outoff) zip_outcnt = zip_outoff = 0;
834
+ }
835
+ return n;
836
+ }
837
+
838
+ /* ==========================================================================
839
+ * Allocate the match buffer, initialize the various tables and save the
840
+ * location of the internal file attribute (ascii/binary) and method
841
+ * (DEFLATE/STORE).
842
+ */
843
+ function zip_ct_init() {
844
+ let n; // iterates over tree elements
845
+ let bits; // bit counter
846
+ let length; // length value
847
+ let code; // code value
848
+ let dist; // distance index
849
+
850
+ if (zip_static_dtree[0].dl != 0) return; // ct_init already called
851
+
852
+ zip_l_desc.dyn_tree = zip_dyn_ltree;
853
+ zip_l_desc.static_tree = zip_static_ltree;
854
+ zip_l_desc.extra_bits = zip_extra_lbits;
855
+ zip_l_desc.extra_base = zip_LITERALS + 1;
856
+ zip_l_desc.elems = zip_L_CODES;
857
+ zip_l_desc.max_length = zip_MAX_BITS;
858
+ zip_l_desc.max_code = 0;
859
+
860
+ zip_d_desc.dyn_tree = zip_dyn_dtree;
861
+ zip_d_desc.static_tree = zip_static_dtree;
862
+ zip_d_desc.extra_bits = zip_extra_dbits;
863
+ zip_d_desc.extra_base = 0;
864
+ zip_d_desc.elems = zip_D_CODES;
865
+ zip_d_desc.max_length = zip_MAX_BITS;
866
+ zip_d_desc.max_code = 0;
867
+
868
+ zip_bl_desc.dyn_tree = zip_bl_tree;
869
+ zip_bl_desc.static_tree = null;
870
+ zip_bl_desc.extra_bits = zip_extra_blbits;
871
+ zip_bl_desc.extra_base = 0;
872
+ zip_bl_desc.elems = zip_BL_CODES;
873
+ zip_bl_desc.max_length = zip_MAX_BL_BITS;
874
+ zip_bl_desc.max_code = 0;
875
+
876
+ // Initialize the mapping length (0..255) -> length code (0..28)
877
+ length = 0;
878
+ for (code = 0; code < zip_LENGTH_CODES - 1; code++) {
879
+ zip_base_length[code] = length;
880
+ for (n = 0; n < 1 << zip_extra_lbits[code]; n++) zip_length_code[length++] = code;
881
+ }
882
+ // Assert (length == 256, "ct_init: length != 256");
883
+
884
+ /* Note that the length 255 (match length 258) can be represented
885
+ * in two different ways: code 284 + 5 bits or code 285, so we
886
+ * overwrite length_code[255] to use the best encoding:
887
+ */
888
+ zip_length_code[length - 1] = code;
889
+
890
+ /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
891
+ dist = 0;
892
+ for (code = 0; code < 16; code++) {
893
+ zip_base_dist[code] = dist;
894
+ for (n = 0; n < 1 << zip_extra_dbits[code]; n++) {
895
+ zip_dist_code[dist++] = code;
896
+ }
897
+ }
898
+ // Assert (dist == 256, "ct_init: dist != 256");
899
+ dist >>= 7; // from now on, all distances are divided by 128
900
+ for (; code < zip_D_CODES; code++) {
901
+ zip_base_dist[code] = dist << 7;
902
+ for (n = 0; n < 1 << (zip_extra_dbits[code] - 7); n++) zip_dist_code[256 + dist++] = code;
903
+ }
904
+ // Assert (dist == 256, "ct_init: 256+dist != 512");
905
+
906
+ // Construct the codes of the static literal tree
907
+ for (bits = 0; bits <= zip_MAX_BITS; bits++) zip_bl_count[bits] = 0;
908
+ n = 0;
909
+ while (n <= 143) {
910
+ zip_static_ltree[n++].dl = 8;
911
+ zip_bl_count[8]++;
912
+ }
913
+ while (n <= 255) {
914
+ zip_static_ltree[n++].dl = 9;
915
+ zip_bl_count[9]++;
916
+ }
917
+ while (n <= 279) {
918
+ zip_static_ltree[n++].dl = 7;
919
+ zip_bl_count[7]++;
920
+ }
921
+ while (n <= 287) {
922
+ zip_static_ltree[n++].dl = 8;
923
+ zip_bl_count[8]++;
924
+ }
925
+ /* Codes 286 and 287 do not exist, but we must include them in the
926
+ * tree construction to get a canonical Huffman tree (longest code
927
+ * all ones)
928
+ */
929
+ zip_gen_codes(zip_static_ltree, zip_L_CODES + 1);
930
+
931
+ /* The static distance tree is trivial: */
932
+ for (n = 0; n < zip_D_CODES; n++) {
933
+ zip_static_dtree[n].dl = 5;
934
+ zip_static_dtree[n].fc = zip_bi_reverse(n, 5);
935
+ }
936
+
937
+ // Initialize the first block of the first file:
938
+ zip_init_block();
939
+ }
940
+
941
+ /* ==========================================================================
942
+ * Initialize a new block.
943
+ */
944
+ function zip_init_block() {
945
+ let n; // iterates over tree elements
946
+
947
+ // Initialize the trees.
948
+ for (n = 0; n < zip_L_CODES; n++) zip_dyn_ltree[n].fc = 0;
949
+ for (n = 0; n < zip_D_CODES; n++) zip_dyn_dtree[n].fc = 0;
950
+ for (n = 0; n < zip_BL_CODES; n++) zip_bl_tree[n].fc = 0;
951
+
952
+ zip_dyn_ltree[zip_END_BLOCK].fc = 1;
953
+ zip_opt_len = zip_static_len = 0;
954
+ zip_last_lit = zip_last_dist = zip_last_flags = 0;
955
+ zip_flags = 0;
956
+ zip_flag_bit = 1;
957
+ }
958
+
959
+ /* ==========================================================================
960
+ * Restore the heap property by moving down the tree starting at node k,
961
+ * exchanging a node with the smallest of its two sons if necessary, stopping
962
+ * when the heap property is re-established (each father smaller than its
963
+ * two sons).
964
+ */
965
+ function zip_pqdownheap(
966
+ tree, // the tree to restore
967
+ k,
968
+ ) {
969
+ // node to move down
970
+ const v = zip_heap[k];
971
+ let j = k << 1; // left son of k
972
+
973
+ while (j <= zip_heap_len) {
974
+ // Set j to the smallest of the two sons:
975
+ if (j < zip_heap_len && zip_SMALLER(tree, zip_heap[j + 1], zip_heap[j])) j++;
976
+
977
+ // Exit if v is smaller than both sons
978
+ if (zip_SMALLER(tree, v, zip_heap[j])) break;
979
+
980
+ // Exchange v with the smallest son
981
+ zip_heap[k] = zip_heap[j];
982
+ k = j;
983
+
984
+ // And continue down the tree, setting j to the left son of k
985
+ j <<= 1;
986
+ }
987
+ zip_heap[k] = v;
988
+ }
989
+
990
+ /* ==========================================================================
991
+ * Compute the optimal bit lengths for a tree and update the total bit length
992
+ * for the current block.
993
+ * IN assertion: the fields freq and dad are set, heap[heap_max] and
994
+ * above are the tree nodes sorted by increasing frequency.
995
+ * OUT assertions: the field len is set to the optimal bit length, the
996
+ * array bl_count contains the frequencies for each bit length.
997
+ * The length opt_len is updated; static_len is also updated if stree is
998
+ * not null.
999
+ */
1000
+ function zip_gen_bitlen(desc) {
1001
+ // the tree descriptor
1002
+ const tree = desc.dyn_tree;
1003
+ const extra = desc.extra_bits;
1004
+ const base = desc.extra_base;
1005
+ const { max_code } = desc;
1006
+ const { max_length } = desc;
1007
+ const stree = desc.static_tree;
1008
+ let h; // heap index
1009
+ let n;
1010
+ let m; // iterate over the tree elements
1011
+ let bits; // bit length
1012
+ let xbits; // extra bits
1013
+ let f; // frequency
1014
+ let overflow = 0; // number of elements with bit length too large
1015
+
1016
+ for (bits = 0; bits <= zip_MAX_BITS; bits++) zip_bl_count[bits] = 0;
1017
+
1018
+ /* In a first pass, compute the optimal bit lengths (which may
1019
+ * overflow in the case of the bit length tree).
1020
+ */
1021
+ tree[zip_heap[zip_heap_max]].dl = 0; // root of the heap
1022
+
1023
+ for (h = zip_heap_max + 1; h < zip_HEAP_SIZE; h++) {
1024
+ n = zip_heap[h];
1025
+ bits = tree[tree[n].dl].dl + 1;
1026
+ if (bits > max_length) {
1027
+ bits = max_length;
1028
+ overflow++;
1029
+ }
1030
+ tree[n].dl = bits;
1031
+ // We overwrite tree[n].dl which is no longer needed
1032
+
1033
+ if (n > max_code) continue; // not a leaf node
1034
+
1035
+ zip_bl_count[bits]++;
1036
+ xbits = 0;
1037
+ if (n >= base) xbits = extra[n - base];
1038
+ f = tree[n].fc;
1039
+ zip_opt_len += f * (bits + xbits);
1040
+ if (stree != null) zip_static_len += f * (stree[n].dl + xbits);
1041
+ }
1042
+ if (overflow == 0) return;
1043
+
1044
+ // This happens for example on obj2 and pic of the Calgary corpus
1045
+
1046
+ // Find the first bit length which could increase:
1047
+ do {
1048
+ bits = max_length - 1;
1049
+ while (zip_bl_count[bits] == 0) bits--;
1050
+ zip_bl_count[bits]--; // move one leaf down the tree
1051
+ zip_bl_count[bits + 1] += 2; // move one overflow item as its brother
1052
+ zip_bl_count[max_length]--;
1053
+ /* The brother of the overflow item also moves one step up,
1054
+ * but this does not affect bl_count[max_length]
1055
+ */
1056
+ overflow -= 2;
1057
+ } while (overflow > 0);
1058
+
1059
+ /* Now recompute all bit lengths, scanning in increasing frequency.
1060
+ * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
1061
+ * lengths instead of fixing only the wrong ones. This idea is taken
1062
+ * from 'ar' written by Haruhiko Okumura.)
1063
+ */
1064
+ for (bits = max_length; bits != 0; bits--) {
1065
+ n = zip_bl_count[bits];
1066
+ while (n != 0) {
1067
+ m = zip_heap[--h];
1068
+ if (m > max_code) continue;
1069
+ if (tree[m].dl != bits) {
1070
+ zip_opt_len += (bits - tree[m].dl) * tree[m].fc;
1071
+ tree[m].fc = bits;
1072
+ }
1073
+ n--;
1074
+ }
1075
+ }
1076
+ }
1077
+
1078
+ /* ==========================================================================
1079
+ * Generate the codes for a given tree and bit counts (which need not be
1080
+ * optimal).
1081
+ * IN assertion: the array bl_count contains the bit length statistics for
1082
+ * the given tree and the field len is set for all tree elements.
1083
+ * OUT assertion: the field code is set for all tree elements of non
1084
+ * zero code length.
1085
+ */
1086
+ function zip_gen_codes(
1087
+ tree, // the tree to decorate
1088
+ max_code,
1089
+ ) {
1090
+ // largest code with non zero frequency
1091
+ const next_code = new Array(zip_MAX_BITS + 1); // next code value for each bit length
1092
+ let code = 0; // running code value
1093
+ let bits; // bit index
1094
+ let n; // code index
1095
+
1096
+ /* The distribution counts are first used to generate the code values
1097
+ * without bit reversal.
1098
+ */
1099
+ for (bits = 1; bits <= zip_MAX_BITS; bits++) {
1100
+ code = (code + zip_bl_count[bits - 1]) << 1;
1101
+ next_code[bits] = code;
1102
+ }
1103
+
1104
+ /* Check that the bit counts in bl_count are consistent. The last code
1105
+ * must be all ones.
1106
+ */
1107
+ // Assert (code + encoder->bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
1108
+ // "inconsistent bit counts");
1109
+ // Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
1110
+
1111
+ for (n = 0; n <= max_code; n++) {
1112
+ const len = tree[n].dl;
1113
+ if (len == 0) continue;
1114
+ // Now reverse the bits
1115
+ tree[n].fc = zip_bi_reverse(next_code[len]++, len);
1116
+
1117
+ // Tracec(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
1118
+ // n, (isgraph(n) ? n : ' '), len, tree[n].fc, next_code[len]-1));
1119
+ }
1120
+ }
1121
+
1122
+ /* ==========================================================================
1123
+ * Construct one Huffman tree and assigns the code bit strings and lengths.
1124
+ * Update the total bit length for the current block.
1125
+ * IN assertion: the field freq is set for all tree elements.
1126
+ * OUT assertions: the fields len and code are set to the optimal bit length
1127
+ * and corresponding code. The length opt_len is updated; static_len is
1128
+ * also updated if stree is not null. The field max_code is set.
1129
+ */
1130
+ function zip_build_tree(desc) {
1131
+ // the tree descriptor
1132
+ const tree = desc.dyn_tree;
1133
+ const stree = desc.static_tree;
1134
+ const { elems } = desc;
1135
+ let n;
1136
+ let m; // iterate over heap elements
1137
+ let max_code = -1; // largest code with non zero frequency
1138
+ let node = elems; // next internal node of the tree
1139
+
1140
+ /* Construct the initial heap, with least frequent element in
1141
+ * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
1142
+ * heap[0] is not used.
1143
+ */
1144
+ zip_heap_len = 0;
1145
+ zip_heap_max = zip_HEAP_SIZE;
1146
+
1147
+ for (n = 0; n < elems; n++) {
1148
+ if (tree[n].fc != 0) {
1149
+ zip_heap[++zip_heap_len] = max_code = n;
1150
+ zip_depth[n] = 0;
1151
+ } else tree[n].dl = 0;
1152
+ }
1153
+
1154
+ /* The pkzip format requires that at least one distance code exists,
1155
+ * and that at least one bit should be sent even if there is only one
1156
+ * possible code. So to avoid special checks later on we force at least
1157
+ * two codes of non zero frequency.
1158
+ */
1159
+ while (zip_heap_len < 2) {
1160
+ const xnew = (zip_heap[++zip_heap_len] = max_code < 2 ? ++max_code : 0);
1161
+ tree[xnew].fc = 1;
1162
+ zip_depth[xnew] = 0;
1163
+ zip_opt_len--;
1164
+ if (stree != null) zip_static_len -= stree[xnew].dl;
1165
+ // new is 0 or 1 so it does not have extra bits
1166
+ }
1167
+ desc.max_code = max_code;
1168
+
1169
+ /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
1170
+ * establish sub-heaps of increasing lengths:
1171
+ */
1172
+ for (n = zip_heap_len >> 1; n >= 1; n--) zip_pqdownheap(tree, n);
1173
+
1174
+ /* Construct the Huffman tree by repeatedly combining the least two
1175
+ * frequent nodes.
1176
+ */
1177
+ do {
1178
+ n = zip_heap[zip_SMALLEST];
1179
+ zip_heap[zip_SMALLEST] = zip_heap[zip_heap_len--];
1180
+ zip_pqdownheap(tree, zip_SMALLEST);
1181
+
1182
+ m = zip_heap[zip_SMALLEST]; // m = node of next least frequency
1183
+
1184
+ // keep the nodes sorted by frequency
1185
+ zip_heap[--zip_heap_max] = n;
1186
+ zip_heap[--zip_heap_max] = m;
1187
+
1188
+ // Create a new node father of n and m
1189
+ tree[node].fc = tree[n].fc + tree[m].fc;
1190
+ // depth[node] = (char)(MAX(depth[n], depth[m]) + 1);
1191
+ if (zip_depth[n] > zip_depth[m] + 1) zip_depth[node] = zip_depth[n];
1192
+ else zip_depth[node] = zip_depth[m] + 1;
1193
+ tree[n].dl = tree[m].dl = node;
1194
+
1195
+ // and insert the new node in the heap
1196
+ zip_heap[zip_SMALLEST] = node++;
1197
+ zip_pqdownheap(tree, zip_SMALLEST);
1198
+ } while (zip_heap_len >= 2);
1199
+
1200
+ zip_heap[--zip_heap_max] = zip_heap[zip_SMALLEST];
1201
+
1202
+ /* At this point, the fields freq and dad are set. We can now
1203
+ * generate the bit lengths.
1204
+ */
1205
+ zip_gen_bitlen(desc);
1206
+
1207
+ // The field len is now set, we can generate the bit codes
1208
+ zip_gen_codes(tree, max_code);
1209
+ }
1210
+
1211
+ /* ==========================================================================
1212
+ * Scan a literal or distance tree to determine the frequencies of the codes
1213
+ * in the bit length tree. Updates opt_len to take into account the repeat
1214
+ * counts. (The contribution of the bit length codes will be added later
1215
+ * during the construction of bl_tree.)
1216
+ */
1217
+ function zip_scan_tree(
1218
+ tree, // the tree to be scanned
1219
+ max_code,
1220
+ ) {
1221
+ // and its largest code of non zero frequency
1222
+ let n; // iterates over all tree elements
1223
+ let prevlen = -1; // last emitted length
1224
+ let curlen; // length of current code
1225
+ let nextlen = tree[0].dl; // length of next code
1226
+ let count = 0; // repeat count of the current code
1227
+ let max_count = 7; // max repeat count
1228
+ let min_count = 4; // min repeat count
1229
+
1230
+ if (nextlen == 0) {
1231
+ max_count = 138;
1232
+ min_count = 3;
1233
+ }
1234
+ tree[max_code + 1].dl = 0xffff; // guard
1235
+
1236
+ for (n = 0; n <= max_code; n++) {
1237
+ curlen = nextlen;
1238
+ nextlen = tree[n + 1].dl;
1239
+ if (++count < max_count && curlen == nextlen) continue;
1240
+ else if (count < min_count) zip_bl_tree[curlen].fc += count;
1241
+ else if (curlen != 0) {
1242
+ if (curlen != prevlen) zip_bl_tree[curlen].fc++;
1243
+ zip_bl_tree[zip_REP_3_6].fc++;
1244
+ } else if (count <= 10) zip_bl_tree[zip_REPZ_3_10].fc++;
1245
+ else zip_bl_tree[zip_REPZ_11_138].fc++;
1246
+ count = 0;
1247
+ prevlen = curlen;
1248
+ if (nextlen == 0) {
1249
+ max_count = 138;
1250
+ min_count = 3;
1251
+ } else if (curlen == nextlen) {
1252
+ max_count = 6;
1253
+ min_count = 3;
1254
+ } else {
1255
+ max_count = 7;
1256
+ min_count = 4;
1257
+ }
1258
+ }
1259
+ }
1260
+
1261
+ /* ==========================================================================
1262
+ * Send a literal or distance tree in compressed form, using the codes in
1263
+ * bl_tree.
1264
+ */
1265
+ function zip_send_tree(
1266
+ tree, // the tree to be scanned
1267
+ max_code,
1268
+ ) {
1269
+ // and its largest code of non zero frequency
1270
+ let n; // iterates over all tree elements
1271
+ let prevlen = -1; // last emitted length
1272
+ let curlen; // length of current code
1273
+ let nextlen = tree[0].dl; // length of next code
1274
+ let count = 0; // repeat count of the current code
1275
+ let max_count = 7; // max repeat count
1276
+ let min_count = 4; /* guard already set */ // min repeat count
1277
+
1278
+ /* tree[max_code+1].dl = -1; */ if (nextlen == 0) {
1279
+ max_count = 138;
1280
+ min_count = 3;
1281
+ }
1282
+
1283
+ for (n = 0; n <= max_code; n++) {
1284
+ curlen = nextlen;
1285
+ nextlen = tree[n + 1].dl;
1286
+ if (++count < max_count && curlen == nextlen) {
1287
+ continue;
1288
+ } else if (count < min_count) {
1289
+ do {
1290
+ zip_SEND_CODE(curlen, zip_bl_tree);
1291
+ } while (--count != 0);
1292
+ } else if (curlen != 0) {
1293
+ if (curlen != prevlen) {
1294
+ zip_SEND_CODE(curlen, zip_bl_tree);
1295
+ count--;
1296
+ }
1297
+ // Assert(count >= 3 && count <= 6, " 3_6?");
1298
+ zip_SEND_CODE(zip_REP_3_6, zip_bl_tree);
1299
+ zip_send_bits(count - 3, 2);
1300
+ } else if (count <= 10) {
1301
+ zip_SEND_CODE(zip_REPZ_3_10, zip_bl_tree);
1302
+ zip_send_bits(count - 3, 3);
1303
+ } else {
1304
+ zip_SEND_CODE(zip_REPZ_11_138, zip_bl_tree);
1305
+ zip_send_bits(count - 11, 7);
1306
+ }
1307
+ count = 0;
1308
+ prevlen = curlen;
1309
+ if (nextlen == 0) {
1310
+ max_count = 138;
1311
+ min_count = 3;
1312
+ } else if (curlen == nextlen) {
1313
+ max_count = 6;
1314
+ min_count = 3;
1315
+ } else {
1316
+ max_count = 7;
1317
+ min_count = 4;
1318
+ }
1319
+ }
1320
+ }
1321
+
1322
+ /* ==========================================================================
1323
+ * Construct the Huffman tree for the bit lengths and return the index in
1324
+ * bl_order of the last bit length code to send.
1325
+ */
1326
+ function zip_build_bl_tree() {
1327
+ let max_blindex; // index of last bit length code of non zero freq
1328
+
1329
+ // Determine the bit length frequencies for literal and distance trees
1330
+ zip_scan_tree(zip_dyn_ltree, zip_l_desc.max_code);
1331
+ zip_scan_tree(zip_dyn_dtree, zip_d_desc.max_code);
1332
+
1333
+ // Build the bit length tree:
1334
+ zip_build_tree(zip_bl_desc);
1335
+ /* opt_len now includes the length of the tree representations, except
1336
+ * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
1337
+ */
1338
+
1339
+ /* Determine the number of bit length codes to send. The pkzip format
1340
+ * requires that at least 4 bit length codes be sent. (appnote.txt says
1341
+ * 3 but the actual value used is 4.)
1342
+ */
1343
+ for (max_blindex = zip_BL_CODES - 1; max_blindex >= 3; max_blindex--) {
1344
+ if (zip_bl_tree[zip_bl_order[max_blindex]].dl != 0) break;
1345
+ }
1346
+ /* Update opt_len to include the bit length tree and counts */
1347
+ zip_opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
1348
+ // Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
1349
+ // encoder->opt_len, encoder->static_len));
1350
+
1351
+ return max_blindex;
1352
+ }
1353
+
1354
+ /* ==========================================================================
1355
+ * Send the header for a block using dynamic Huffman trees: the counts, the
1356
+ * lengths of the bit length codes, the literal tree and the distance tree.
1357
+ * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
1358
+ */
1359
+ function zip_send_all_trees(lcodes, dcodes, blcodes) {
1360
+ // number of codes for each tree
1361
+ let rank; // index in bl_order
1362
+
1363
+ // Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
1364
+ // Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
1365
+ // "too many codes");
1366
+ // Tracev((stderr, "\nbl counts: "));
1367
+ zip_send_bits(lcodes - 257, 5); // not +255 as stated in appnote.txt
1368
+ zip_send_bits(dcodes - 1, 5);
1369
+ zip_send_bits(blcodes - 4, 4); // not -3 as stated in appnote.txt
1370
+ for (rank = 0; rank < blcodes; rank++) {
1371
+ // Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
1372
+ zip_send_bits(zip_bl_tree[zip_bl_order[rank]].dl, 3);
1373
+ }
1374
+
1375
+ // send the literal tree
1376
+ zip_send_tree(zip_dyn_ltree, lcodes - 1);
1377
+
1378
+ // send the distance tree
1379
+ zip_send_tree(zip_dyn_dtree, dcodes - 1);
1380
+ }
1381
+
1382
+ /* ==========================================================================
1383
+ * Determine the best encoding for the current block: dynamic trees, static
1384
+ * trees or store, and output the encoded block to the zip file.
1385
+ */
1386
+ function zip_flush_block(eof) {
1387
+ // true if this is the last block for a file
1388
+ let opt_lenb;
1389
+ let static_lenb; // opt_len and static_len in bytes
1390
+ let max_blindex; // index of last bit length code of non zero freq
1391
+ let stored_len; // length of input block
1392
+
1393
+ stored_len = zip_strstart - zip_block_start;
1394
+ zip_flag_buf[zip_last_flags] = zip_flags; // Save the flags for the last 8 items
1395
+
1396
+ // Construct the literal and distance trees
1397
+ zip_build_tree(zip_l_desc);
1398
+ // Tracev((stderr, "\nlit data: dyn %ld, stat %ld",
1399
+ // encoder->opt_len, encoder->static_len));
1400
+
1401
+ zip_build_tree(zip_d_desc);
1402
+ // Tracev((stderr, "\ndist data: dyn %ld, stat %ld",
1403
+ // encoder->opt_len, encoder->static_len));
1404
+ /* At this point, opt_len and static_len are the total bit lengths of
1405
+ * the compressed block data, excluding the tree representations.
1406
+ */
1407
+
1408
+ /* Build the bit length tree for the above two trees, and get the index
1409
+ * in bl_order of the last bit length code to send.
1410
+ */
1411
+ max_blindex = zip_build_bl_tree();
1412
+
1413
+ // Determine the best encoding. Compute first the block length in bytes
1414
+ opt_lenb = (zip_opt_len + 3 + 7) >> 3;
1415
+ static_lenb = (zip_static_len + 3 + 7) >> 3;
1416
+
1417
+ // Trace((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u dist %u ",
1418
+ // opt_lenb, encoder->opt_len,
1419
+ // static_lenb, encoder->static_len, stored_len,
1420
+ // encoder->last_lit, encoder->last_dist));
1421
+
1422
+ if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
1423
+ if (
1424
+ stored_len + 4 <= opt_lenb && // 4: two words for the lengths
1425
+ zip_block_start >= 0
1426
+ ) {
1427
+ let i;
1428
+
1429
+ /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
1430
+ * Otherwise we can't have processed more than WSIZE input bytes since
1431
+ * the last block flush, because compression would have been
1432
+ * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
1433
+ * transform a block into a stored block.
1434
+ */
1435
+ zip_send_bits((zip_STORED_BLOCK << 1) + eof, 3); /* send block type */
1436
+ zip_bi_windup(); /* align on byte boundary */
1437
+ zip_put_short(stored_len);
1438
+ zip_put_short(~stored_len);
1439
+
1440
+ // copy block
1441
+ /*
1442
+ p = &window[block_start];
1443
+ for(i = 0; i < stored_len; i++)
1444
+ put_byte(p[i]);
1445
+ */
1446
+ for (i = 0; i < stored_len; i++) zip_put_byte(zip_window[zip_block_start + i]);
1447
+ } else if (static_lenb == opt_lenb) {
1448
+ zip_send_bits((zip_STATIC_TREES << 1) + eof, 3);
1449
+ zip_compress_block(zip_static_ltree, zip_static_dtree);
1450
+ } else {
1451
+ zip_send_bits((zip_DYN_TREES << 1) + eof, 3);
1452
+ zip_send_all_trees(zip_l_desc.max_code + 1, zip_d_desc.max_code + 1, max_blindex + 1);
1453
+ zip_compress_block(zip_dyn_ltree, zip_dyn_dtree);
1454
+ }
1455
+
1456
+ zip_init_block();
1457
+
1458
+ if (eof != 0) zip_bi_windup();
1459
+ }
1460
+
1461
+ /* ==========================================================================
1462
+ * Save the match info and tally the frequency counts. Return true if
1463
+ * the current block must be flushed.
1464
+ */
1465
+ function zip_ct_tally(
1466
+ dist, // distance of matched string
1467
+ lc,
1468
+ ) {
1469
+ // match length-MIN_MATCH or unmatched char (if dist==0)
1470
+ zip_l_buf[zip_last_lit++] = lc;
1471
+ if (dist == 0) {
1472
+ // lc is the unmatched char
1473
+ zip_dyn_ltree[lc].fc++;
1474
+ } else {
1475
+ // Here, lc is the match length - MIN_MATCH
1476
+ dist--; // dist = match distance - 1
1477
+ // Assert((ush)dist < (ush)MAX_DIST &&
1478
+ // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
1479
+ // (ush)D_CODE(dist) < (ush)D_CODES, "ct_tally: bad match");
1480
+
1481
+ zip_dyn_ltree[zip_length_code[lc] + zip_LITERALS + 1].fc++;
1482
+ zip_dyn_dtree[zip_D_CODE(dist)].fc++;
1483
+
1484
+ zip_d_buf[zip_last_dist++] = dist;
1485
+ zip_flags |= zip_flag_bit;
1486
+ }
1487
+ zip_flag_bit <<= 1;
1488
+
1489
+ // Output the flags if they fill a byte
1490
+ if ((zip_last_lit & 7) == 0) {
1491
+ zip_flag_buf[zip_last_flags++] = zip_flags;
1492
+ zip_flags = 0;
1493
+ zip_flag_bit = 1;
1494
+ }
1495
+ // Try to guess if it is profitable to stop the current block here
1496
+ if (zip_compr_level > 2 && (zip_last_lit & 0xfff) == 0) {
1497
+ // Compute an upper bound for the compressed length
1498
+ let out_length = zip_last_lit * 8;
1499
+ const in_length = zip_strstart - zip_block_start;
1500
+ let dcode;
1501
+
1502
+ for (dcode = 0; dcode < zip_D_CODES; dcode++) {
1503
+ out_length += zip_dyn_dtree[dcode].fc * (5 + zip_extra_dbits[dcode]);
1504
+ }
1505
+ out_length >>= 3;
1506
+ // Trace((stderr,"\nlast_lit %u, last_dist %u, in %ld, out ~%ld(%ld%%) ",
1507
+ // encoder->last_lit, encoder->last_dist, in_length, out_length,
1508
+ // 100L - out_length*100L/in_length));
1509
+ if (zip_last_dist < parseInt(zip_last_lit / 2) && out_length < parseInt(in_length / 2)) return true;
1510
+ }
1511
+ return zip_last_lit == zip_LIT_BUFSIZE - 1 || zip_last_dist == zip_DIST_BUFSIZE;
1512
+ /* We avoid equality with LIT_BUFSIZE because of wraparound at 64K
1513
+ * on 16 bit machines and because stored blocks are restricted to
1514
+ * 64K-1 bytes.
1515
+ */
1516
+ }
1517
+
1518
+ /* ==========================================================================
1519
+ * Send the block data compressed using the given Huffman trees
1520
+ */
1521
+ function zip_compress_block(
1522
+ ltree, // literal tree
1523
+ dtree,
1524
+ ) {
1525
+ // distance tree
1526
+ let dist; // distance of matched string
1527
+ let lc; // match length or unmatched char (if dist == 0)
1528
+ let lx = 0; // running index in l_buf
1529
+ let dx = 0; // running index in d_buf
1530
+ let fx = 0; // running index in flag_buf
1531
+ let flag = 0; // current flags
1532
+ let code; // the code to send
1533
+ let extra; // number of extra bits to send
1534
+
1535
+ if (zip_last_lit != 0)
1536
+ do {
1537
+ if ((lx & 7) == 0) flag = zip_flag_buf[fx++];
1538
+ lc = zip_l_buf[lx++] & 0xff;
1539
+ if ((flag & 1) == 0) {
1540
+ zip_SEND_CODE(lc, ltree); /* send a literal byte */
1541
+ // Tracecv(isgraph(lc), (stderr," '%c' ", lc));
1542
+ } else {
1543
+ // Here, lc is the match length - MIN_MATCH
1544
+ code = zip_length_code[lc];
1545
+ zip_SEND_CODE(code + zip_LITERALS + 1, ltree); // send the length code
1546
+ extra = zip_extra_lbits[code];
1547
+ if (extra != 0) {
1548
+ lc -= zip_base_length[code];
1549
+ zip_send_bits(lc, extra); // send the extra length bits
1550
+ }
1551
+ dist = zip_d_buf[dx++];
1552
+ // Here, dist is the match distance - 1
1553
+ code = zip_D_CODE(dist);
1554
+ // Assert (code < D_CODES, "bad d_code");
1555
+
1556
+ zip_SEND_CODE(code, dtree); // send the distance code
1557
+ extra = zip_extra_dbits[code];
1558
+ if (extra != 0) {
1559
+ dist -= zip_base_dist[code];
1560
+ zip_send_bits(dist, extra); // send the extra distance bits
1561
+ }
1562
+ } // literal or match pair ?
1563
+ flag >>= 1;
1564
+ } while (lx < zip_last_lit);
1565
+
1566
+ zip_SEND_CODE(zip_END_BLOCK, ltree);
1567
+ }
1568
+
1569
+ /* ==========================================================================
1570
+ * Send a value on a given number of bits.
1571
+ * IN assertion: length <= 16 and value fits in length bits.
1572
+ */
1573
+ const zip_Buf_size = 16; // bit size of bi_buf
1574
+ function zip_send_bits(
1575
+ value, // value to send
1576
+ length,
1577
+ ) {
1578
+ // number of bits
1579
+ /* If not enough room in bi_buf, use (valid) bits from bi_buf and
1580
+ * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
1581
+ * unused bits in value.
1582
+ */
1583
+ if (zip_bi_valid > zip_Buf_size - length) {
1584
+ zip_bi_buf |= value << zip_bi_valid;
1585
+ zip_put_short(zip_bi_buf);
1586
+ zip_bi_buf = value >> (zip_Buf_size - zip_bi_valid);
1587
+ zip_bi_valid += length - zip_Buf_size;
1588
+ } else {
1589
+ zip_bi_buf |= value << zip_bi_valid;
1590
+ zip_bi_valid += length;
1591
+ }
1592
+ }
1593
+
1594
+ /* ==========================================================================
1595
+ * Reverse the first len bits of a code, using straightforward code (a faster
1596
+ * method would use a table)
1597
+ * IN assertion: 1 <= len <= 15
1598
+ */
1599
+ function zip_bi_reverse(
1600
+ code, // the value to invert
1601
+ len,
1602
+ ) {
1603
+ // its bit length
1604
+ let res = 0;
1605
+ do {
1606
+ res |= code & 1;
1607
+ code >>= 1;
1608
+ res <<= 1;
1609
+ } while (--len > 0);
1610
+ return res >> 1;
1611
+ }
1612
+
1613
+ /* ==========================================================================
1614
+ * Write out any remaining bits in an incomplete byte.
1615
+ */
1616
+ function zip_bi_windup() {
1617
+ if (zip_bi_valid > 8) {
1618
+ zip_put_short(zip_bi_buf);
1619
+ } else if (zip_bi_valid > 0) {
1620
+ zip_put_byte(zip_bi_buf);
1621
+ }
1622
+ zip_bi_buf = 0;
1623
+ zip_bi_valid = 0;
1624
+ }
1625
+
1626
+ function zip_qoutbuf() {
1627
+ if (zip_outcnt != 0) {
1628
+ let q;
1629
+ let i;
1630
+ q = zip_new_queue();
1631
+ if (zip_qhead == null) zip_qhead = zip_qtail = q;
1632
+ else zip_qtail = zip_qtail.next = q;
1633
+ q.len = zip_outcnt - zip_outoff;
1634
+ // System.arraycopy(zip_outbuf, zip_outoff, q.ptr, 0, q.len);
1635
+ for (i = 0; i < q.len; i++) q.ptr[i] = zip_outbuf[zip_outoff + i];
1636
+ zip_outcnt = zip_outoff = 0;
1637
+ }
1638
+ }
1639
+
1640
+ return function deflate(str, level) {
1641
+ let i;
1642
+ let j;
1643
+
1644
+ zip_deflate_data = str;
1645
+ zip_deflate_pos = 0;
1646
+ if (typeof level === 'undefined') level = zip_DEFAULT_LEVEL;
1647
+ zip_deflate_start(level);
1648
+
1649
+ const buff = new Array(1024);
1650
+ const aout = [];
1651
+ while ((i = zip_deflate_internal(buff, 0, buff.length)) > 0) {
1652
+ const cbuf = new Array(i);
1653
+ for (j = 0; j < i; j++) {
1654
+ cbuf[j] = String.fromCharCode(buff[j]);
1655
+ }
1656
+ aout[aout.length] = cbuf.join('');
1657
+ }
1658
+ zip_deflate_data = null; // G.C.
1659
+ return aout.join('');
1660
+ };
1661
+ })();
1662
+
1663
+ export default deflate;