@papyrus-sdk/engine-native 0.2.22-beta.9.epub.2 → 0.2.23

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 (276) hide show
  1. package/PapyrusNativeEngine.podspec +45 -0
  2. package/android/build.gradle +1 -0
  3. package/android/src/main/cpp/CMakeLists.txt +137 -0
  4. package/android/src/main/cpp/PapyrusComicArchiveJni.cpp +68 -0
  5. package/android/src/main/cpp/PapyrusComicArchiveTest.cpp +55 -0
  6. package/android/src/main/cpp/papyrus_outline_loader_test.cpp +240 -0
  7. package/android/src/main/cpp/papyrus_text_word_test.cpp +72 -0
  8. package/android/src/main/java/com/papyrus/engine/PapyrusComicArchiveNative.java +14 -0
  9. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentView.java +491 -0
  10. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentViewManager.java +32 -0
  11. package/android/src/main/java/com/papyrus/engine/PapyrusComicPageLayout.java +38 -0
  12. package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +305 -35
  13. package/android/src/main/java/com/papyrus/engine/PapyrusNativeTextModel.java +195 -0
  14. package/android/src/main/java/com/papyrus/engine/PapyrusPackage.java +3 -1
  15. package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerView.java +195 -5
  16. package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerViewManager.java +20 -1
  17. package/android/src/main/java/com/papyrus/engine/PapyrusSelectionMenuIcon.java +48 -0
  18. package/android/src/main/java/com/papyrus/engine/PapyrusTextAnnotationModel.java +22 -0
  19. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentView.java +661 -0
  20. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentViewManager.java +49 -0
  21. package/android/src/main/java/com/papyrus/engine/PapyrusTextStore.java +43 -0
  22. package/dist/index.d.mts +614 -4
  23. package/dist/index.d.ts +614 -4
  24. package/dist/index.js +484 -8
  25. package/dist/index.js.map +1 -1
  26. package/dist/index.mjs +477 -7
  27. package/dist/index.mjs.map +1 -1
  28. package/ios/PapyrusComicDocumentView.h +21 -0
  29. package/ios/PapyrusComicDocumentView.m +554 -0
  30. package/ios/PapyrusComicDocumentViewManager.m +23 -0
  31. package/ios/PapyrusEngineStore.h +3 -0
  32. package/ios/PapyrusEngineStore.m +32 -6
  33. package/ios/PapyrusNativeEngine.m +684 -5
  34. package/ios/PapyrusPageRotationRegistry.h +29 -0
  35. package/ios/PapyrusPageRotationRegistry.m +179 -0
  36. package/ios/PapyrusPageView.m +38 -11
  37. package/ios/PapyrusPdfDocumentView.h +55 -0
  38. package/ios/PapyrusPdfDocumentView.m +2370 -0
  39. package/ios/PapyrusPdfDocumentViewManager.m +61 -0
  40. package/ios/PapyrusPdfPageTheme.h +15 -0
  41. package/ios/PapyrusPdfPageTheme.m +218 -0
  42. package/ios/PapyrusSelectionMenu.h +27 -0
  43. package/ios/PapyrusTextAnnotationRange.h +2 -0
  44. package/ios/PapyrusTextAnnotationRange.m +29 -0
  45. package/ios/PapyrusTextDocumentView.h +32 -0
  46. package/ios/PapyrusTextDocumentView.m +518 -0
  47. package/ios/PapyrusTextDocumentViewManager.m +34 -0
  48. package/ios/PapyrusTextStore.h +13 -0
  49. package/ios/PapyrusTextStore.m +73 -0
  50. package/package.json +12 -10
  51. package/react-native.config.js +1 -1
  52. package/third_party_licenses/BLAKE2-NOTICE.txt +11 -0
  53. package/third_party_licenses/libarchive-COPYING +65 -0
  54. package/vendor/libarchive/PapyrusComicArchive.cpp +376 -0
  55. package/vendor/libarchive/PapyrusComicArchive.h +30 -0
  56. package/vendor/libarchive/README.papyrus.md +15 -0
  57. package/vendor/libarchive/android.h +192 -0
  58. package/vendor/libarchive/config/PapyrusArchiveConfig.h +71 -0
  59. package/vendor/libarchive/libarchive/CMakeLists.txt +293 -0
  60. package/vendor/libarchive/libarchive/android_lf.h +36 -0
  61. package/vendor/libarchive/libarchive/archive.h +1266 -0
  62. package/vendor/libarchive/libarchive/archive_acl.c +2025 -0
  63. package/vendor/libarchive/libarchive/archive_acl_private.h +85 -0
  64. package/vendor/libarchive/libarchive/archive_blake2.h +197 -0
  65. package/vendor/libarchive/libarchive/archive_blake2_impl.h +161 -0
  66. package/vendor/libarchive/libarchive/archive_blake2s_ref.c +369 -0
  67. package/vendor/libarchive/libarchive/archive_blake2sp_ref.c +361 -0
  68. package/vendor/libarchive/libarchive/archive_check_magic.c +183 -0
  69. package/vendor/libarchive/libarchive/archive_cmdline.c +211 -0
  70. package/vendor/libarchive/libarchive/archive_cmdline_private.h +45 -0
  71. package/vendor/libarchive/libarchive/archive_crc32.h +123 -0
  72. package/vendor/libarchive/libarchive/archive_cryptor.c +534 -0
  73. package/vendor/libarchive/libarchive/archive_cryptor_private.h +199 -0
  74. package/vendor/libarchive/libarchive/archive_digest.c +1584 -0
  75. package/vendor/libarchive/libarchive/archive_digest_private.h +431 -0
  76. package/vendor/libarchive/libarchive/archive_disk_acl_darwin.c +558 -0
  77. package/vendor/libarchive/libarchive/archive_disk_acl_freebsd.c +712 -0
  78. package/vendor/libarchive/libarchive/archive_disk_acl_linux.c +760 -0
  79. package/vendor/libarchive/libarchive/archive_disk_acl_sunos.c +824 -0
  80. package/vendor/libarchive/libarchive/archive_endian.h +234 -0
  81. package/vendor/libarchive/libarchive/archive_entry.3 +147 -0
  82. package/vendor/libarchive/libarchive/archive_entry.c +2297 -0
  83. package/vendor/libarchive/libarchive/archive_entry.h +763 -0
  84. package/vendor/libarchive/libarchive/archive_entry_acl.3 +458 -0
  85. package/vendor/libarchive/libarchive/archive_entry_copy_bhfi.c +55 -0
  86. package/vendor/libarchive/libarchive/archive_entry_copy_stat.c +82 -0
  87. package/vendor/libarchive/libarchive/archive_entry_link_resolver.c +472 -0
  88. package/vendor/libarchive/libarchive/archive_entry_linkify.3 +224 -0
  89. package/vendor/libarchive/libarchive/archive_entry_locale.h +90 -0
  90. package/vendor/libarchive/libarchive/archive_entry_misc.3 +63 -0
  91. package/vendor/libarchive/libarchive/archive_entry_paths.3 +153 -0
  92. package/vendor/libarchive/libarchive/archive_entry_perms.3 +210 -0
  93. package/vendor/libarchive/libarchive/archive_entry_private.h +205 -0
  94. package/vendor/libarchive/libarchive/archive_entry_sparse.c +156 -0
  95. package/vendor/libarchive/libarchive/archive_entry_stat.3 +274 -0
  96. package/vendor/libarchive/libarchive/archive_entry_stat.c +121 -0
  97. package/vendor/libarchive/libarchive/archive_entry_strmode.c +69 -0
  98. package/vendor/libarchive/libarchive/archive_entry_time.3 +127 -0
  99. package/vendor/libarchive/libarchive/archive_entry_xattr.c +155 -0
  100. package/vendor/libarchive/libarchive/archive_hmac.c +345 -0
  101. package/vendor/libarchive/libarchive/archive_hmac_private.h +119 -0
  102. package/vendor/libarchive/libarchive/archive_integer.h +267 -0
  103. package/vendor/libarchive/libarchive/archive_match.c +1869 -0
  104. package/vendor/libarchive/libarchive/archive_openssl_evp_private.h +54 -0
  105. package/vendor/libarchive/libarchive/archive_openssl_hmac_private.h +54 -0
  106. package/vendor/libarchive/libarchive/archive_options.c +219 -0
  107. package/vendor/libarchive/libarchive/archive_options_private.h +49 -0
  108. package/vendor/libarchive/libarchive/archive_pack_dev.c +330 -0
  109. package/vendor/libarchive/libarchive/archive_pack_dev.h +49 -0
  110. package/vendor/libarchive/libarchive/archive_parse_date.c +1173 -0
  111. package/vendor/libarchive/libarchive/archive_pathmatch.c +480 -0
  112. package/vendor/libarchive/libarchive/archive_pathmatch.h +50 -0
  113. package/vendor/libarchive/libarchive/archive_platform.h +213 -0
  114. package/vendor/libarchive/libarchive/archive_platform_acl.h +53 -0
  115. package/vendor/libarchive/libarchive/archive_platform_stat.h +45 -0
  116. package/vendor/libarchive/libarchive/archive_platform_xattr.h +45 -0
  117. package/vendor/libarchive/libarchive/archive_ppmd7.c +1173 -0
  118. package/vendor/libarchive/libarchive/archive_ppmd7_private.h +119 -0
  119. package/vendor/libarchive/libarchive/archive_ppmd8.c +1293 -0
  120. package/vendor/libarchive/libarchive/archive_ppmd8_private.h +123 -0
  121. package/vendor/libarchive/libarchive/archive_ppmd_private.h +159 -0
  122. package/vendor/libarchive/libarchive/archive_private.h +209 -0
  123. package/vendor/libarchive/libarchive/archive_random.c +274 -0
  124. package/vendor/libarchive/libarchive/archive_random_private.h +36 -0
  125. package/vendor/libarchive/libarchive/archive_rb.c +709 -0
  126. package/vendor/libarchive/libarchive/archive_rb.h +113 -0
  127. package/vendor/libarchive/libarchive/archive_read.3 +250 -0
  128. package/vendor/libarchive/libarchive/archive_read.c +1814 -0
  129. package/vendor/libarchive/libarchive/archive_read_add_passphrase.3 +72 -0
  130. package/vendor/libarchive/libarchive/archive_read_add_passphrase.c +189 -0
  131. package/vendor/libarchive/libarchive/archive_read_append_filter.c +209 -0
  132. package/vendor/libarchive/libarchive/archive_read_data.3 +128 -0
  133. package/vendor/libarchive/libarchive/archive_read_data_into_fd.c +207 -0
  134. package/vendor/libarchive/libarchive/archive_read_disk.3 +422 -0
  135. package/vendor/libarchive/libarchive/archive_read_disk_entry_from_file.c +1086 -0
  136. package/vendor/libarchive/libarchive/archive_read_disk_posix.c +2639 -0
  137. package/vendor/libarchive/libarchive/archive_read_disk_private.h +97 -0
  138. package/vendor/libarchive/libarchive/archive_read_disk_set_standard_lookup.c +315 -0
  139. package/vendor/libarchive/libarchive/archive_read_disk_windows.c +2603 -0
  140. package/vendor/libarchive/libarchive/archive_read_extract.3 +135 -0
  141. package/vendor/libarchive/libarchive/archive_read_extract.c +59 -0
  142. package/vendor/libarchive/libarchive/archive_read_extract2.c +154 -0
  143. package/vendor/libarchive/libarchive/archive_read_filter.3 +163 -0
  144. package/vendor/libarchive/libarchive/archive_read_format.3 +190 -0
  145. package/vendor/libarchive/libarchive/archive_read_free.3 +91 -0
  146. package/vendor/libarchive/libarchive/archive_read_header.3 +89 -0
  147. package/vendor/libarchive/libarchive/archive_read_new.3 +57 -0
  148. package/vendor/libarchive/libarchive/archive_read_open.3 +247 -0
  149. package/vendor/libarchive/libarchive/archive_read_open_fd.c +236 -0
  150. package/vendor/libarchive/libarchive/archive_read_open_file.c +257 -0
  151. package/vendor/libarchive/libarchive/archive_read_open_filename.c +652 -0
  152. package/vendor/libarchive/libarchive/archive_read_open_memory.c +188 -0
  153. package/vendor/libarchive/libarchive/archive_read_private.h +274 -0
  154. package/vendor/libarchive/libarchive/archive_read_set_format.c +116 -0
  155. package/vendor/libarchive/libarchive/archive_read_set_options.3 +290 -0
  156. package/vendor/libarchive/libarchive/archive_read_set_options.c +132 -0
  157. package/vendor/libarchive/libarchive/archive_read_support_filter_all.c +84 -0
  158. package/vendor/libarchive/libarchive/archive_read_support_filter_by_code.c +68 -0
  159. package/vendor/libarchive/libarchive/archive_read_support_filter_bzip2.c +360 -0
  160. package/vendor/libarchive/libarchive/archive_read_support_filter_compress.c +457 -0
  161. package/vendor/libarchive/libarchive/archive_read_support_filter_grzip.c +108 -0
  162. package/vendor/libarchive/libarchive/archive_read_support_filter_gzip.c +538 -0
  163. package/vendor/libarchive/libarchive/archive_read_support_filter_lrzip.c +119 -0
  164. package/vendor/libarchive/libarchive/archive_read_support_filter_lz4.c +829 -0
  165. package/vendor/libarchive/libarchive/archive_read_support_filter_lzop.c +493 -0
  166. package/vendor/libarchive/libarchive/archive_read_support_filter_none.c +51 -0
  167. package/vendor/libarchive/libarchive/archive_read_support_filter_program.c +497 -0
  168. package/vendor/libarchive/libarchive/archive_read_support_filter_rpm.c +249 -0
  169. package/vendor/libarchive/libarchive/archive_read_support_filter_uu.c +739 -0
  170. package/vendor/libarchive/libarchive/archive_read_support_filter_xz.c +792 -0
  171. package/vendor/libarchive/libarchive/archive_read_support_filter_zstd.c +356 -0
  172. package/vendor/libarchive/libarchive/archive_read_support_format_7zip.c +4650 -0
  173. package/vendor/libarchive/libarchive/archive_read_support_format_all.c +88 -0
  174. package/vendor/libarchive/libarchive/archive_read_support_format_ar.c +597 -0
  175. package/vendor/libarchive/libarchive/archive_read_support_format_by_code.c +76 -0
  176. package/vendor/libarchive/libarchive/archive_read_support_format_cab.c +3410 -0
  177. package/vendor/libarchive/libarchive/archive_read_support_format_cpio.c +1152 -0
  178. package/vendor/libarchive/libarchive/archive_read_support_format_empty.c +95 -0
  179. package/vendor/libarchive/libarchive/archive_read_support_format_iso9660.c +3516 -0
  180. package/vendor/libarchive/libarchive/archive_read_support_format_lha.c +2931 -0
  181. package/vendor/libarchive/libarchive/archive_read_support_format_mtree.c +2113 -0
  182. package/vendor/libarchive/libarchive/archive_read_support_format_rar.c +3932 -0
  183. package/vendor/libarchive/libarchive/archive_read_support_format_rar5.c +4501 -0
  184. package/vendor/libarchive/libarchive/archive_read_support_format_raw.c +194 -0
  185. package/vendor/libarchive/libarchive/archive_read_support_format_tar.c +3785 -0
  186. package/vendor/libarchive/libarchive/archive_read_support_format_warc.c +875 -0
  187. package/vendor/libarchive/libarchive/archive_read_support_format_xar.c +3770 -0
  188. package/vendor/libarchive/libarchive/archive_read_support_format_zip.c +4710 -0
  189. package/vendor/libarchive/libarchive/archive_string.c +4391 -0
  190. package/vendor/libarchive/libarchive/archive_string.h +244 -0
  191. package/vendor/libarchive/libarchive/archive_string_composition.h +2289 -0
  192. package/vendor/libarchive/libarchive/archive_string_sprintf.c +200 -0
  193. package/vendor/libarchive/libarchive/archive_time.c +168 -0
  194. package/vendor/libarchive/libarchive/archive_time_private.h +47 -0
  195. package/vendor/libarchive/libarchive/archive_util.3 +239 -0
  196. package/vendor/libarchive/libarchive/archive_util.c +668 -0
  197. package/vendor/libarchive/libarchive/archive_version_details.c +516 -0
  198. package/vendor/libarchive/libarchive/archive_virtual.c +162 -0
  199. package/vendor/libarchive/libarchive/archive_windows.c +1035 -0
  200. package/vendor/libarchive/libarchive/archive_windows.h +332 -0
  201. package/vendor/libarchive/libarchive/archive_write.3 +266 -0
  202. package/vendor/libarchive/libarchive/archive_write.c +896 -0
  203. package/vendor/libarchive/libarchive/archive_write_add_filter.c +71 -0
  204. package/vendor/libarchive/libarchive/archive_write_add_filter_b64encode.c +329 -0
  205. package/vendor/libarchive/libarchive/archive_write_add_filter_by_name.c +76 -0
  206. package/vendor/libarchive/libarchive/archive_write_add_filter_bzip2.c +411 -0
  207. package/vendor/libarchive/libarchive/archive_write_add_filter_compress.c +459 -0
  208. package/vendor/libarchive/libarchive/archive_write_add_filter_grzip.c +143 -0
  209. package/vendor/libarchive/libarchive/archive_write_add_filter_gzip.c +493 -0
  210. package/vendor/libarchive/libarchive/archive_write_add_filter_lrzip.c +214 -0
  211. package/vendor/libarchive/libarchive/archive_write_add_filter_lz4.c +710 -0
  212. package/vendor/libarchive/libarchive/archive_write_add_filter_lzop.c +500 -0
  213. package/vendor/libarchive/libarchive/archive_write_add_filter_none.c +42 -0
  214. package/vendor/libarchive/libarchive/archive_write_add_filter_program.c +398 -0
  215. package/vendor/libarchive/libarchive/archive_write_add_filter_uuencode.c +320 -0
  216. package/vendor/libarchive/libarchive/archive_write_add_filter_xz.c +547 -0
  217. package/vendor/libarchive/libarchive/archive_write_add_filter_zstd.c +608 -0
  218. package/vendor/libarchive/libarchive/archive_write_blocksize.3 +112 -0
  219. package/vendor/libarchive/libarchive/archive_write_data.3 +88 -0
  220. package/vendor/libarchive/libarchive/archive_write_disk.3 +360 -0
  221. package/vendor/libarchive/libarchive/archive_write_disk_posix.c +4789 -0
  222. package/vendor/libarchive/libarchive/archive_write_disk_private.h +43 -0
  223. package/vendor/libarchive/libarchive/archive_write_disk_set_standard_lookup.c +265 -0
  224. package/vendor/libarchive/libarchive/archive_write_disk_windows.c +2952 -0
  225. package/vendor/libarchive/libarchive/archive_write_filter.3 +140 -0
  226. package/vendor/libarchive/libarchive/archive_write_finish_entry.3 +77 -0
  227. package/vendor/libarchive/libarchive/archive_write_format.3 +187 -0
  228. package/vendor/libarchive/libarchive/archive_write_free.3 +94 -0
  229. package/vendor/libarchive/libarchive/archive_write_header.3 +71 -0
  230. package/vendor/libarchive/libarchive/archive_write_new.3 +56 -0
  231. package/vendor/libarchive/libarchive/archive_write_open.3 +270 -0
  232. package/vendor/libarchive/libarchive/archive_write_open_fd.c +141 -0
  233. package/vendor/libarchive/libarchive/archive_write_open_file.c +102 -0
  234. package/vendor/libarchive/libarchive/archive_write_open_filename.c +272 -0
  235. package/vendor/libarchive/libarchive/archive_write_open_memory.c +110 -0
  236. package/vendor/libarchive/libarchive/archive_write_private.h +166 -0
  237. package/vendor/libarchive/libarchive/archive_write_set_format.c +124 -0
  238. package/vendor/libarchive/libarchive/archive_write_set_format_7zip.c +2571 -0
  239. package/vendor/libarchive/libarchive/archive_write_set_format_ar.c +540 -0
  240. package/vendor/libarchive/libarchive/archive_write_set_format_by_name.c +93 -0
  241. package/vendor/libarchive/libarchive/archive_write_set_format_cpio.c +11 -0
  242. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_binary.c +618 -0
  243. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_newc.c +457 -0
  244. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_odc.c +508 -0
  245. package/vendor/libarchive/libarchive/archive_write_set_format_filter_by_ext.c +141 -0
  246. package/vendor/libarchive/libarchive/archive_write_set_format_gnutar.c +790 -0
  247. package/vendor/libarchive/libarchive/archive_write_set_format_iso9660.c +8216 -0
  248. package/vendor/libarchive/libarchive/archive_write_set_format_mtree.c +2307 -0
  249. package/vendor/libarchive/libarchive/archive_write_set_format_pax.c +2249 -0
  250. package/vendor/libarchive/libarchive/archive_write_set_format_private.h +40 -0
  251. package/vendor/libarchive/libarchive/archive_write_set_format_raw.c +123 -0
  252. package/vendor/libarchive/libarchive/archive_write_set_format_shar.c +650 -0
  253. package/vendor/libarchive/libarchive/archive_write_set_format_ustar.c +768 -0
  254. package/vendor/libarchive/libarchive/archive_write_set_format_v7tar.c +648 -0
  255. package/vendor/libarchive/libarchive/archive_write_set_format_warc.c +491 -0
  256. package/vendor/libarchive/libarchive/archive_write_set_format_xar.c +3799 -0
  257. package/vendor/libarchive/libarchive/archive_write_set_format_zip.c +2548 -0
  258. package/vendor/libarchive/libarchive/archive_write_set_options.3 +779 -0
  259. package/vendor/libarchive/libarchive/archive_write_set_options.c +129 -0
  260. package/vendor/libarchive/libarchive/archive_write_set_passphrase.3 +72 -0
  261. package/vendor/libarchive/libarchive/archive_write_set_passphrase.c +95 -0
  262. package/vendor/libarchive/libarchive/archive_xxhash.h +48 -0
  263. package/vendor/libarchive/libarchive/config_freebsd.h +275 -0
  264. package/vendor/libarchive/libarchive/cpio.5 +389 -0
  265. package/vendor/libarchive/libarchive/filter_fork.h +44 -0
  266. package/vendor/libarchive/libarchive/filter_fork_posix.c +269 -0
  267. package/vendor/libarchive/libarchive/filter_fork_windows.c +270 -0
  268. package/vendor/libarchive/libarchive/libarchive-formats.5 +478 -0
  269. package/vendor/libarchive/libarchive/libarchive.3 +297 -0
  270. package/vendor/libarchive/libarchive/libarchive_changes.3 +339 -0
  271. package/vendor/libarchive/libarchive/libarchive_internals.3 +363 -0
  272. package/vendor/libarchive/libarchive/module.modulemap +34 -0
  273. package/vendor/libarchive/libarchive/mtree.5 +323 -0
  274. package/vendor/libarchive/libarchive/tar.5 +947 -0
  275. package/vendor/libarchive/libarchive/xxhash.c +531 -0
  276. package/ios/PapyrusNativeEngine.podspec +0 -15
@@ -0,0 +1,1293 @@
1
+ /* Ppmd8.c -- PPMdI codec
2
+ 2016-05-21 : Igor Pavlov : Public domain
3
+ This code is based on PPMd var.I (2002): Dmitry Shkarin : Public domain */
4
+
5
+ #include "archive_platform.h"
6
+
7
+ #include <string.h>
8
+
9
+ #include "archive_ppmd8_private.h"
10
+
11
+ const Byte PPMD8_kExpEscape[16] = { 25, 14, 9, 7, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2 };
12
+ static const UInt16 kInitBinEsc[] = { 0x3CDD, 0x1F3F, 0x59BF, 0x48F3, 0x64A1, 0x5ABC, 0x6632, 0x6051};
13
+
14
+ #define MAX_FREQ 124
15
+ #define UNIT_SIZE 12
16
+
17
+ #define U2B(nu) ((UInt32)(nu) * UNIT_SIZE)
18
+ #define U2I(nu) (p->Units2Indx[(nu) - 1])
19
+ #define I2U(indx) (p->Indx2Units[indx])
20
+
21
+ #ifdef PPMD_32BIT
22
+ #define REF(ptr) (ptr)
23
+ #else
24
+ #define REF(ptr) ((UInt32)((Byte *)(ptr) - (p)->Base))
25
+ #endif
26
+
27
+ #define STATS_REF(ptr) ((CPpmd_State_Ref)REF(ptr))
28
+
29
+ #define CTX(ref) ((CPpmd8_Context *)Ppmd8_GetContext(p, ref))
30
+ #define STATS(ctx) Ppmd8_GetStats(p, ctx)
31
+ #define ONE_STATE(ctx) Ppmd8Context_OneState(ctx)
32
+ #define SUFFIX(ctx) CTX((ctx)->Suffix)
33
+
34
+ #define kTop (1 << 24)
35
+ #define kBot (1 << 15)
36
+
37
+ typedef CPpmd8_Context * CTX_PTR;
38
+
39
+ struct CPpmd8_Node_;
40
+
41
+ typedef
42
+ #ifdef PPMD_32BIT
43
+ struct CPpmd8_Node_ *
44
+ #else
45
+ UInt32
46
+ #endif
47
+ CPpmd8_Node_Ref;
48
+
49
+ typedef struct CPpmd8_Node_
50
+ {
51
+ UInt32 Stamp;
52
+ CPpmd8_Node_Ref Next;
53
+ UInt32 NU;
54
+ } CPpmd8_Node;
55
+
56
+ #ifdef PPMD_32BIT
57
+ #define NODE(ptr) (ptr)
58
+ #else
59
+ #define NODE(offs) ((CPpmd8_Node *)(p->Base + (offs)))
60
+ #endif
61
+
62
+ #define EMPTY_NODE 0xFFFFFFFF
63
+
64
+ static void Ppmd8_Construct(CPpmd8 *p)
65
+ {
66
+ unsigned i, k, m;
67
+
68
+ p->Base = 0;
69
+
70
+ for (i = 0, k = 0; i < PPMD_NUM_INDEXES; i++)
71
+ {
72
+ unsigned step = (i >= 12 ? 4 : (i >> 2) + 1);
73
+ do { p->Units2Indx[k++] = (Byte)i; } while (--step);
74
+ p->Indx2Units[i] = (Byte)k;
75
+ }
76
+
77
+ p->NS2BSIndx[0] = (0 << 1);
78
+ p->NS2BSIndx[1] = (1 << 1);
79
+ memset(p->NS2BSIndx + 2, (2 << 1), 9);
80
+ memset(p->NS2BSIndx + 11, (3 << 1), 256 - 11);
81
+
82
+ for (i = 0; i < 5; i++)
83
+ p->NS2Indx[i] = (Byte)i;
84
+ for (m = i, k = 1; i < 260; i++)
85
+ {
86
+ p->NS2Indx[i] = (Byte)m;
87
+ if (--k == 0)
88
+ k = (++m) - 4;
89
+ }
90
+ }
91
+
92
+ static void Ppmd8_Free(CPpmd8 *p)
93
+ {
94
+ free(p->Base);
95
+ p->Size = 0;
96
+ p->Base = 0;
97
+ }
98
+
99
+ static Bool Ppmd8_Alloc(CPpmd8 *p, UInt32 size)
100
+ {
101
+ if (p->Base == 0 || p->Size != size)
102
+ {
103
+ Ppmd8_Free(p);
104
+ p->AlignOffset =
105
+ #ifdef PPMD_32BIT
106
+ (4 - size) & 3;
107
+ #else
108
+ 4 - (size & 3);
109
+ #endif
110
+ if ((p->Base = malloc(p->AlignOffset + size)) == 0)
111
+ return False;
112
+ p->Size = size;
113
+ }
114
+ return True;
115
+ }
116
+
117
+ static void InsertNode(CPpmd8 *p, void *node, unsigned indx)
118
+ {
119
+ ((CPpmd8_Node *)node)->Stamp = EMPTY_NODE;
120
+ ((CPpmd8_Node *)node)->Next = (CPpmd8_Node_Ref)p->FreeList[indx];
121
+ ((CPpmd8_Node *)node)->NU = I2U(indx);
122
+ p->FreeList[indx] = REF(node);
123
+ p->Stamps[indx]++;
124
+ }
125
+
126
+ static void *RemoveNode(CPpmd8 *p, unsigned indx)
127
+ {
128
+ CPpmd8_Node *node = NODE((CPpmd8_Node_Ref)p->FreeList[indx]);
129
+ p->FreeList[indx] = node->Next;
130
+ p->Stamps[indx]--;
131
+ return node;
132
+ }
133
+
134
+ static void SplitBlock(CPpmd8 *p, void *ptr, unsigned oldIndx, unsigned newIndx)
135
+ {
136
+ unsigned i, nu = I2U(oldIndx) - I2U(newIndx);
137
+ ptr = (Byte *)ptr + U2B(I2U(newIndx));
138
+ if (I2U(i = U2I(nu)) != nu)
139
+ {
140
+ unsigned k = I2U(--i);
141
+ InsertNode(p, ((Byte *)ptr) + U2B(k), nu - k - 1);
142
+ }
143
+ InsertNode(p, ptr, i);
144
+ }
145
+
146
+ static void GlueFreeBlocks(CPpmd8 *p)
147
+ {
148
+ CPpmd8_Node_Ref head = 0;
149
+ CPpmd8_Node_Ref *prev = &head;
150
+ unsigned i;
151
+
152
+ p->GlueCount = 1 << 13;
153
+ memset(p->Stamps, 0, sizeof(p->Stamps));
154
+
155
+ /* Order-0 context is always at top UNIT, so we don't need guard NODE at the end.
156
+ All blocks up to p->LoUnit can be free, so we need guard NODE at LoUnit. */
157
+ if (p->LoUnit != p->HiUnit)
158
+ ((CPpmd8_Node *)p->LoUnit)->Stamp = 0;
159
+
160
+ /* Glue free blocks */
161
+ for (i = 0; i < PPMD_NUM_INDEXES; i++)
162
+ {
163
+ CPpmd8_Node_Ref next = (CPpmd8_Node_Ref)p->FreeList[i];
164
+ p->FreeList[i] = 0;
165
+ while (next != 0)
166
+ {
167
+ CPpmd8_Node *node = NODE(next);
168
+ if (node->NU != 0)
169
+ {
170
+ CPpmd8_Node *node2;
171
+ *prev = next;
172
+ prev = &(node->Next);
173
+ while ((node2 = node + node->NU)->Stamp == EMPTY_NODE)
174
+ {
175
+ node->NU += node2->NU;
176
+ node2->NU = 0;
177
+ }
178
+ }
179
+ next = node->Next;
180
+ }
181
+ }
182
+ *prev = 0;
183
+
184
+ /* Fill lists of free blocks */
185
+ while (head != 0)
186
+ {
187
+ CPpmd8_Node *node = NODE(head);
188
+ unsigned nu;
189
+ head = node->Next;
190
+ nu = node->NU;
191
+ if (nu == 0)
192
+ continue;
193
+ for (; nu > 128; nu -= 128, node += 128)
194
+ InsertNode(p, node, PPMD_NUM_INDEXES - 1);
195
+ if (I2U(i = U2I(nu)) != nu)
196
+ {
197
+ unsigned k = I2U(--i);
198
+ InsertNode(p, node + k, nu - k - 1);
199
+ }
200
+ InsertNode(p, node, i);
201
+ }
202
+ }
203
+
204
+ static void *AllocUnitsRare(CPpmd8 *p, unsigned indx)
205
+ {
206
+ unsigned i;
207
+ void *retVal;
208
+ if (p->GlueCount == 0)
209
+ {
210
+ GlueFreeBlocks(p);
211
+ if (p->FreeList[indx] != 0)
212
+ return RemoveNode(p, indx);
213
+ }
214
+ i = indx;
215
+ do
216
+ {
217
+ if (++i == PPMD_NUM_INDEXES)
218
+ {
219
+ UInt32 numBytes = U2B(I2U(indx));
220
+ p->GlueCount--;
221
+ return ((UInt32)(p->UnitsStart - p->Text) > numBytes) ? (p->UnitsStart -= numBytes) : (NULL);
222
+ }
223
+ }
224
+ while (p->FreeList[i] == 0);
225
+ retVal = RemoveNode(p, i);
226
+ SplitBlock(p, retVal, i, indx);
227
+ return retVal;
228
+ }
229
+
230
+ static void *AllocUnits(CPpmd8 *p, unsigned indx)
231
+ {
232
+ UInt32 numBytes;
233
+ if (p->FreeList[indx] != 0)
234
+ return RemoveNode(p, indx);
235
+ numBytes = U2B(I2U(indx));
236
+ if (numBytes <= (UInt32)(p->HiUnit - p->LoUnit))
237
+ {
238
+ void *retVal = p->LoUnit;
239
+ p->LoUnit += numBytes;
240
+ return retVal;
241
+ }
242
+ return AllocUnitsRare(p, indx);
243
+ }
244
+
245
+ #define MyMem12Cpy(dest, src, num) do { \
246
+ UInt32 *d = (UInt32 *)dest; \
247
+ const UInt32 *z = (const UInt32 *)src; \
248
+ UInt32 n = num; \
249
+ do { \
250
+ d[0] = z[0]; d[1] = z[1]; d[2] = z[2]; z += 3; d += 3; \
251
+ } while (--n); \
252
+ } while (0)
253
+
254
+ static void *ShrinkUnits(CPpmd8 *p, void *oldPtr, unsigned oldNU, unsigned newNU)
255
+ {
256
+ unsigned i0 = U2I(oldNU);
257
+ unsigned i1 = U2I(newNU);
258
+ if (i0 == i1)
259
+ return oldPtr;
260
+ if (p->FreeList[i1] != 0)
261
+ {
262
+ void *ptr = RemoveNode(p, i1);
263
+ MyMem12Cpy(ptr, oldPtr, newNU);
264
+ InsertNode(p, oldPtr, i0);
265
+ return ptr;
266
+ }
267
+ SplitBlock(p, oldPtr, i0, i1);
268
+ return oldPtr;
269
+ }
270
+
271
+ static void FreeUnits(CPpmd8 *p, void *ptr, unsigned nu)
272
+ {
273
+ InsertNode(p, ptr, U2I(nu));
274
+ }
275
+
276
+ static void SpecialFreeUnit(CPpmd8 *p, void *ptr)
277
+ {
278
+ if ((Byte *)ptr != p->UnitsStart)
279
+ InsertNode(p, ptr, 0);
280
+ else
281
+ {
282
+ #ifdef PPMD8_FREEZE_SUPPORT
283
+ *(UInt32 *)ptr = EMPTY_NODE; /* it's used for (Flags == 0xFF) check in RemoveBinContexts */
284
+ #endif
285
+ p->UnitsStart += UNIT_SIZE;
286
+ }
287
+ }
288
+
289
+ static void *MoveUnitsUp(CPpmd8 *p, void *oldPtr, unsigned nu)
290
+ {
291
+ unsigned indx = U2I(nu);
292
+ void *ptr;
293
+ if ((Byte *)oldPtr > p->UnitsStart + 16 * 1024 || REF(oldPtr) > p->FreeList[indx])
294
+ return oldPtr;
295
+ ptr = RemoveNode(p, indx);
296
+ MyMem12Cpy(ptr, oldPtr, nu);
297
+ if ((Byte*)oldPtr != p->UnitsStart)
298
+ InsertNode(p, oldPtr, indx);
299
+ else
300
+ p->UnitsStart += U2B(I2U(indx));
301
+ return ptr;
302
+ }
303
+
304
+ static void ExpandTextArea(CPpmd8 *p)
305
+ {
306
+ UInt32 count[PPMD_NUM_INDEXES];
307
+ unsigned i;
308
+ memset(count, 0, sizeof(count));
309
+ if (p->LoUnit != p->HiUnit)
310
+ ((CPpmd8_Node *)p->LoUnit)->Stamp = 0;
311
+
312
+ {
313
+ CPpmd8_Node *node = (CPpmd8_Node *)p->UnitsStart;
314
+ for (; node->Stamp == EMPTY_NODE; node += node->NU)
315
+ {
316
+ node->Stamp = 0;
317
+ count[U2I(node->NU)]++;
318
+ }
319
+ p->UnitsStart = (Byte *)node;
320
+ }
321
+
322
+ for (i = 0; i < PPMD_NUM_INDEXES; i++)
323
+ {
324
+ CPpmd8_Node_Ref *next = (CPpmd8_Node_Ref *)&p->FreeList[i];
325
+ while (count[i] != 0)
326
+ {
327
+ CPpmd8_Node *node = NODE(*next);
328
+ while (node->Stamp == 0)
329
+ {
330
+ *next = node->Next;
331
+ node = NODE(*next);
332
+ p->Stamps[i]--;
333
+ if (--count[i] == 0)
334
+ break;
335
+ }
336
+ next = &node->Next;
337
+ }
338
+ }
339
+ }
340
+
341
+ #define SUCCESSOR(p) ((CPpmd_Void_Ref)((p)->SuccessorLow | ((UInt32)(p)->SuccessorHigh << 16)))
342
+
343
+ static void SetSuccessor(CPpmd_State *p, CPpmd_Void_Ref v)
344
+ {
345
+ (p)->SuccessorLow = (UInt16)((UInt32)(v) & 0xFFFF);
346
+ (p)->SuccessorHigh = (UInt16)(((UInt32)(v) >> 16) & 0xFFFF);
347
+ }
348
+
349
+ #define RESET_TEXT(offs) do { \
350
+ p->Text = p->Base + p->AlignOffset + (offs); \
351
+ } while (0)
352
+
353
+ static void RestartModel(CPpmd8 *p)
354
+ {
355
+ unsigned i, k, m, r;
356
+
357
+ memset(p->FreeList, 0, sizeof(p->FreeList));
358
+ memset(p->Stamps, 0, sizeof(p->Stamps));
359
+ RESET_TEXT(0);
360
+ p->HiUnit = p->Text + p->Size;
361
+ p->LoUnit = p->UnitsStart = p->HiUnit - p->Size / 8 / UNIT_SIZE * 7 * UNIT_SIZE;
362
+ p->GlueCount = 0;
363
+
364
+ p->OrderFall = p->MaxOrder;
365
+ p->RunLength = p->InitRL = -(Int32)((p->MaxOrder < 12) ? p->MaxOrder : 12) - 1;
366
+ p->PrevSuccess = 0;
367
+
368
+ p->MinContext = p->MaxContext = (CTX_PTR)(p->HiUnit -= UNIT_SIZE); /* AllocContext(p); */
369
+ p->MinContext->Suffix = 0;
370
+ p->MinContext->NumStats = 255;
371
+ p->MinContext->Flags = 0;
372
+ p->MinContext->SummFreq = 256 + 1;
373
+ p->FoundState = (CPpmd_State *)p->LoUnit; /* AllocUnits(p, PPMD_NUM_INDEXES - 1); */
374
+ p->LoUnit += U2B(256 / 2);
375
+ p->MinContext->Stats = REF(p->FoundState);
376
+ for (i = 0; i < 256; i++)
377
+ {
378
+ CPpmd_State *s = &p->FoundState[i];
379
+ s->Symbol = (Byte)i;
380
+ s->Freq = 1;
381
+ SetSuccessor(s, 0);
382
+ }
383
+
384
+ for (i = m = 0; m < 25; m++)
385
+ {
386
+ while (p->NS2Indx[i] == m)
387
+ i++;
388
+ for (k = 0; k < 8; k++)
389
+ {
390
+ UInt16 val = (UInt16)(PPMD_BIN_SCALE - kInitBinEsc[k] / (i + 1));
391
+ UInt16 *dest = p->BinSumm[m] + k;
392
+ for (r = 0; r < 64; r += 8)
393
+ dest[r] = val;
394
+ }
395
+ }
396
+
397
+ for (i = m = 0; m < 24; m++)
398
+ {
399
+ while (p->NS2Indx[i + 3] == m + 3)
400
+ i++;
401
+ for (k = 0; k < 32; k++)
402
+ {
403
+ CPpmd_See *s = &p->See[m][k];
404
+ s->Summ = (UInt16)((2 * i + 5) << (s->Shift = PPMD_PERIOD_BITS - 4));
405
+ s->Count = 7;
406
+ }
407
+ }
408
+ }
409
+
410
+ static void Ppmd8_Init(CPpmd8 *p, unsigned maxOrder, unsigned restoreMethod)
411
+ {
412
+ p->MaxOrder = maxOrder;
413
+ p->RestoreMethod = restoreMethod;
414
+ RestartModel(p);
415
+ p->DummySee.Shift = PPMD_PERIOD_BITS;
416
+ p->DummySee.Summ = 0; /* unused */
417
+ p->DummySee.Count = 64; /* unused */
418
+ }
419
+
420
+ static void Refresh(CPpmd8 *p, CTX_PTR ctx, unsigned oldNU, unsigned scale)
421
+ {
422
+ unsigned i = ctx->NumStats, escFreq, sumFreq, flags;
423
+ CPpmd_State *s = (CPpmd_State *)ShrinkUnits(p, STATS(ctx), oldNU, (i + 2) >> 1);
424
+ ctx->Stats = REF(s);
425
+ #ifdef PPMD8_FREEZE_SUPPORT
426
+ /* fixed over Shkarin's code. Fixed code is not compatible with original code for some files in FREEZE mode. */
427
+ scale |= (ctx->SummFreq >= ((UInt32)1 << 15));
428
+ #endif
429
+ flags = (ctx->Flags & (0x10 + 0x04 * scale)) + 0x08 * (s->Symbol >= 0x40);
430
+ escFreq = ctx->SummFreq - s->Freq;
431
+ sumFreq = (s->Freq = (Byte)((s->Freq + scale) >> scale));
432
+ do
433
+ {
434
+ escFreq -= (++s)->Freq;
435
+ sumFreq += (s->Freq = (Byte)((s->Freq + scale) >> scale));
436
+ flags |= 0x08 * (s->Symbol >= 0x40);
437
+ }
438
+ while (--i);
439
+ ctx->SummFreq = (UInt16)(sumFreq + ((escFreq + scale) >> scale));
440
+ ctx->Flags = (Byte)flags;
441
+ }
442
+
443
+ static void SwapStates(CPpmd_State *t1, CPpmd_State *t2)
444
+ {
445
+ CPpmd_State tmp = *t1;
446
+ *t1 = *t2;
447
+ *t2 = tmp;
448
+ }
449
+
450
+ static CPpmd_Void_Ref CutOff(CPpmd8 *p, CTX_PTR ctx, unsigned order)
451
+ {
452
+ int i;
453
+ unsigned tmp;
454
+ CPpmd_State *s;
455
+
456
+ if (!ctx->NumStats)
457
+ {
458
+ s = ONE_STATE(ctx);
459
+ if ((Byte *)Ppmd8_GetPtr(p, SUCCESSOR(s)) >= p->UnitsStart)
460
+ {
461
+ if (order < p->MaxOrder)
462
+ SetSuccessor(s, CutOff(p, CTX(SUCCESSOR(s)), order + 1));
463
+ else
464
+ SetSuccessor(s, 0);
465
+ if (SUCCESSOR(s) || order <= 9) /* O_BOUND */
466
+ return REF(ctx);
467
+ }
468
+ SpecialFreeUnit(p, ctx);
469
+ return 0;
470
+ }
471
+
472
+ ctx->Stats = STATS_REF(MoveUnitsUp(p, STATS(ctx), tmp = ((unsigned)ctx->NumStats + 2) >> 1));
473
+
474
+ for (s = STATS(ctx) + (i = ctx->NumStats); s >= STATS(ctx); s--)
475
+ if ((Byte *)Ppmd8_GetPtr(p, SUCCESSOR(s)) < p->UnitsStart)
476
+ {
477
+ CPpmd_State *s2 = STATS(ctx) + (i--);
478
+ SetSuccessor(s, 0);
479
+ SwapStates(s, s2);
480
+ }
481
+ else if (order < p->MaxOrder)
482
+ SetSuccessor(s, CutOff(p, CTX(SUCCESSOR(s)), order + 1));
483
+ else
484
+ SetSuccessor(s, 0);
485
+
486
+ if (i != ctx->NumStats && order)
487
+ {
488
+ ctx->NumStats = (Byte)i;
489
+ s = STATS(ctx);
490
+ if (i < 0)
491
+ {
492
+ FreeUnits(p, s, tmp);
493
+ SpecialFreeUnit(p, ctx);
494
+ return 0;
495
+ }
496
+ if (i == 0)
497
+ {
498
+ ctx->Flags = (Byte)((ctx->Flags & 0x10) + 0x08 * (s->Symbol >= 0x40));
499
+ *ONE_STATE(ctx) = *s;
500
+ FreeUnits(p, s, tmp);
501
+ /* 9.31: the code was fixed. It's was not BUG, if Freq <= MAX_FREQ = 124 */
502
+ ONE_STATE(ctx)->Freq = (Byte)(((unsigned)ONE_STATE(ctx)->Freq + 11) >> 3);
503
+ }
504
+ else
505
+ Refresh(p, ctx, tmp, ctx->SummFreq > 16 * i);
506
+ }
507
+ return REF(ctx);
508
+ }
509
+
510
+ #ifdef PPMD8_FREEZE_SUPPORT
511
+ static CPpmd_Void_Ref RemoveBinContexts(CPpmd8 *p, CTX_PTR ctx, unsigned order)
512
+ {
513
+ CPpmd_State *s;
514
+ if (!ctx->NumStats)
515
+ {
516
+ s = ONE_STATE(ctx);
517
+ if ((Byte *)Ppmd8_GetPtr(p, SUCCESSOR(s)) >= p->UnitsStart && order < p->MaxOrder)
518
+ SetSuccessor(s, RemoveBinContexts(p, CTX(SUCCESSOR(s)), order + 1));
519
+ else
520
+ SetSuccessor(s, 0);
521
+ /* Suffix context can be removed already, since different (high-order)
522
+ Successors may refer to same context. So we check Flags == 0xFF (Stamp == EMPTY_NODE) */
523
+ if (!SUCCESSOR(s) && (!SUFFIX(ctx)->NumStats || SUFFIX(ctx)->Flags == 0xFF))
524
+ {
525
+ FreeUnits(p, ctx, 1);
526
+ return 0;
527
+ }
528
+ else
529
+ return REF(ctx);
530
+ }
531
+
532
+ for (s = STATS(ctx) + ctx->NumStats; s >= STATS(ctx); s--)
533
+ if ((Byte *)Ppmd8_GetPtr(p, SUCCESSOR(s)) >= p->UnitsStart && order < p->MaxOrder)
534
+ SetSuccessor(s, RemoveBinContexts(p, CTX(SUCCESSOR(s)), order + 1));
535
+ else
536
+ SetSuccessor(s, 0);
537
+
538
+ return REF(ctx);
539
+ }
540
+ #endif
541
+
542
+ static UInt32 GetUsedMemory(const CPpmd8 *p)
543
+ {
544
+ UInt32 v = 0;
545
+ unsigned i;
546
+ for (i = 0; i < PPMD_NUM_INDEXES; i++)
547
+ v += p->Stamps[i] * I2U(i);
548
+ return p->Size - (UInt32)(p->HiUnit - p->LoUnit) - (UInt32)(p->UnitsStart - p->Text) - U2B(v);
549
+ }
550
+
551
+ #ifdef PPMD8_FREEZE_SUPPORT
552
+ #define RESTORE_MODEL(c1, fSuccessor) RestoreModel(p, c1, fSuccessor)
553
+ #else
554
+ #define RESTORE_MODEL(c1, fSuccessor) RestoreModel(p, c1)
555
+ #endif
556
+
557
+ static void RestoreModel(CPpmd8 *p, CTX_PTR c1
558
+ #ifdef PPMD8_FREEZE_SUPPORT
559
+ , CTX_PTR fSuccessor
560
+ #endif
561
+ )
562
+ {
563
+ CTX_PTR c;
564
+ CPpmd_State *s;
565
+ RESET_TEXT(0);
566
+ for (c = p->MaxContext; c != c1; c = SUFFIX(c))
567
+ if (--(c->NumStats) == 0)
568
+ {
569
+ s = STATS(c);
570
+ c->Flags = (Byte)((c->Flags & 0x10) + 0x08 * (s->Symbol >= 0x40));
571
+ *ONE_STATE(c) = *s;
572
+ SpecialFreeUnit(p, s);
573
+ ONE_STATE(c)->Freq = (Byte)(((unsigned)ONE_STATE(c)->Freq + 11) >> 3);
574
+ }
575
+ else
576
+ Refresh(p, c, (c->NumStats+3) >> 1, 0);
577
+
578
+ for (; c != p->MinContext; c = SUFFIX(c))
579
+ if (!c->NumStats)
580
+ ONE_STATE(c)->Freq = (Byte)(ONE_STATE(c)->Freq - (ONE_STATE(c)->Freq >> 1));
581
+ else if ((c->SummFreq += 4) > 128 + 4 * c->NumStats)
582
+ Refresh(p, c, (c->NumStats + 2) >> 1, 1);
583
+
584
+ #ifdef PPMD8_FREEZE_SUPPORT
585
+ if (p->RestoreMethod > PPMD8_RESTORE_METHOD_FREEZE)
586
+ {
587
+ p->MaxContext = fSuccessor;
588
+ p->GlueCount += !(p->Stamps[1] & 1);
589
+ }
590
+ else if (p->RestoreMethod == PPMD8_RESTORE_METHOD_FREEZE)
591
+ {
592
+ while (p->MaxContext->Suffix)
593
+ p->MaxContext = SUFFIX(p->MaxContext);
594
+ RemoveBinContexts(p, p->MaxContext, 0);
595
+ p->RestoreMethod++;
596
+ p->GlueCount = 0;
597
+ p->OrderFall = p->MaxOrder;
598
+ }
599
+ else
600
+ #endif
601
+ if (p->RestoreMethod == PPMD8_RESTORE_METHOD_RESTART || GetUsedMemory(p) < (p->Size >> 1))
602
+ RestartModel(p);
603
+ else
604
+ {
605
+ while (p->MaxContext->Suffix)
606
+ p->MaxContext = SUFFIX(p->MaxContext);
607
+ do
608
+ {
609
+ CutOff(p, p->MaxContext, 0);
610
+ ExpandTextArea(p);
611
+ }
612
+ while (GetUsedMemory(p) > 3 * (p->Size >> 2));
613
+ p->GlueCount = 0;
614
+ p->OrderFall = p->MaxOrder;
615
+ }
616
+ }
617
+
618
+ static CTX_PTR CreateSuccessors(CPpmd8 *p, Bool skip, CPpmd_State *s1, CTX_PTR c)
619
+ {
620
+ CPpmd_State upState;
621
+ Byte flags;
622
+ CPpmd_Byte_Ref upBranch = (CPpmd_Byte_Ref)SUCCESSOR(p->FoundState);
623
+ /* fixed over Shkarin's code. Maybe it could work without + 1 too. */
624
+ CPpmd_State *ps[PPMD8_MAX_ORDER + 1];
625
+ unsigned numPs = 0;
626
+
627
+ if (!skip)
628
+ ps[numPs++] = p->FoundState;
629
+
630
+ while (c->Suffix)
631
+ {
632
+ CPpmd_Void_Ref successor;
633
+ CPpmd_State *s;
634
+ c = SUFFIX(c);
635
+ if (s1)
636
+ {
637
+ s = s1;
638
+ s1 = NULL;
639
+ }
640
+ else if (c->NumStats != 0)
641
+ {
642
+ for (s = STATS(c); s->Symbol != p->FoundState->Symbol; s++);
643
+ if (s->Freq < MAX_FREQ - 9)
644
+ {
645
+ s->Freq++;
646
+ c->SummFreq++;
647
+ }
648
+ }
649
+ else
650
+ {
651
+ s = ONE_STATE(c);
652
+ s->Freq = (Byte)(s->Freq + (!SUFFIX(c)->NumStats & (s->Freq < 24)));
653
+ }
654
+ successor = SUCCESSOR(s);
655
+ if (successor != upBranch)
656
+ {
657
+ c = CTX(successor);
658
+ if (numPs == 0)
659
+ return c;
660
+ break;
661
+ }
662
+ ps[numPs++] = s;
663
+ }
664
+
665
+ upState.Symbol = *(const Byte *)Ppmd8_GetPtr(p, upBranch);
666
+ SetSuccessor(&upState, upBranch + 1);
667
+ flags = (Byte)(0x10 * (p->FoundState->Symbol >= 0x40) + 0x08 * (upState.Symbol >= 0x40));
668
+
669
+ if (c->NumStats == 0)
670
+ upState.Freq = ONE_STATE(c)->Freq;
671
+ else
672
+ {
673
+ UInt32 cf, s0;
674
+ CPpmd_State *s;
675
+ for (s = STATS(c); s->Symbol != upState.Symbol; s++);
676
+ cf = s->Freq - 1;
677
+ s0 = c->SummFreq - c->NumStats - cf;
678
+ upState.Freq = (Byte)(1 + ((2 * cf <= s0) ? (5 * cf > s0) : ((cf + 2 * s0 - 3) / s0)));
679
+ }
680
+
681
+ while (numPs != 0)
682
+ {
683
+ /* Create Child */
684
+ CTX_PTR c1; /* = AllocContext(p); */
685
+ if (p->HiUnit != p->LoUnit)
686
+ c1 = (CTX_PTR)(p->HiUnit -= UNIT_SIZE);
687
+ else if (p->FreeList[0] != 0)
688
+ c1 = (CTX_PTR)RemoveNode(p, 0);
689
+ else
690
+ {
691
+ c1 = (CTX_PTR)AllocUnitsRare(p, 0);
692
+ if (!c1)
693
+ return NULL;
694
+ }
695
+ c1->NumStats = 0;
696
+ c1->Flags = flags;
697
+ *ONE_STATE(c1) = upState;
698
+ c1->Suffix = REF(c);
699
+ SetSuccessor(ps[--numPs], REF(c1));
700
+ c = c1;
701
+ }
702
+
703
+ return c;
704
+ }
705
+
706
+ static CTX_PTR ReduceOrder(CPpmd8 *p, CPpmd_State *s1, CTX_PTR c)
707
+ {
708
+ CPpmd_State *s = NULL;
709
+ CTX_PTR c1 = c;
710
+ CPpmd_Void_Ref upBranch = REF(p->Text);
711
+
712
+ #ifdef PPMD8_FREEZE_SUPPORT
713
+ /* The BUG in Shkarin's code was fixed: ps could overflow in CUT_OFF mode. */
714
+ CPpmd_State *ps[PPMD8_MAX_ORDER + 1];
715
+ unsigned numPs = 0;
716
+ ps[numPs++] = p->FoundState;
717
+ #endif
718
+
719
+ SetSuccessor(p->FoundState, upBranch);
720
+ p->OrderFall++;
721
+
722
+ for (;;)
723
+ {
724
+ if (s1)
725
+ {
726
+ c = SUFFIX(c);
727
+ s = s1;
728
+ s1 = NULL;
729
+ }
730
+ else
731
+ {
732
+ if (!c->Suffix)
733
+ {
734
+ #ifdef PPMD8_FREEZE_SUPPORT
735
+ if (p->RestoreMethod > PPMD8_RESTORE_METHOD_FREEZE)
736
+ {
737
+ do { SetSuccessor(ps[--numPs], REF(c)); } while (numPs);
738
+ RESET_TEXT(1);
739
+ p->OrderFall = 1;
740
+ }
741
+ #endif
742
+ return c;
743
+ }
744
+ c = SUFFIX(c);
745
+ if (c->NumStats)
746
+ {
747
+ if ((s = STATS(c))->Symbol != p->FoundState->Symbol)
748
+ do { s++; } while (s->Symbol != p->FoundState->Symbol);
749
+ if (s->Freq < MAX_FREQ - 9)
750
+ {
751
+ s->Freq += 2;
752
+ c->SummFreq += 2;
753
+ }
754
+ }
755
+ else
756
+ {
757
+ s = ONE_STATE(c);
758
+ s->Freq = (Byte)(s->Freq + (s->Freq < 32));
759
+ }
760
+ }
761
+ if (SUCCESSOR(s))
762
+ break;
763
+ #ifdef PPMD8_FREEZE_SUPPORT
764
+ ps[numPs++] = s;
765
+ #endif
766
+ SetSuccessor(s, upBranch);
767
+ p->OrderFall++;
768
+ }
769
+
770
+ #ifdef PPMD8_FREEZE_SUPPORT
771
+ if (p->RestoreMethod > PPMD8_RESTORE_METHOD_FREEZE)
772
+ {
773
+ c = CTX(SUCCESSOR(s));
774
+ do { SetSuccessor(ps[--numPs], REF(c)); } while (numPs);
775
+ RESET_TEXT(1);
776
+ p->OrderFall = 1;
777
+ return c;
778
+ }
779
+ else
780
+ #endif
781
+ if (SUCCESSOR(s) <= upBranch)
782
+ {
783
+ CTX_PTR successor;
784
+ CPpmd_State *s2 = p->FoundState;
785
+ p->FoundState = s;
786
+
787
+ successor = CreateSuccessors(p, False, NULL, c);
788
+ if (successor == NULL)
789
+ SetSuccessor(s, 0);
790
+ else
791
+ SetSuccessor(s, REF(successor));
792
+ p->FoundState = s2;
793
+ }
794
+
795
+ if (p->OrderFall == 1 && c1 == p->MaxContext)
796
+ {
797
+ SetSuccessor(p->FoundState, SUCCESSOR(s));
798
+ p->Text--;
799
+ }
800
+ if (SUCCESSOR(s) == 0)
801
+ return NULL;
802
+ return CTX(SUCCESSOR(s));
803
+ }
804
+
805
+ static void UpdateModel(CPpmd8 *p)
806
+ {
807
+ CPpmd_Void_Ref successor, fSuccessor = SUCCESSOR(p->FoundState);
808
+ CTX_PTR c;
809
+ unsigned s0, ns, fFreq = p->FoundState->Freq;
810
+ Byte flag, fSymbol = p->FoundState->Symbol;
811
+ CPpmd_State *s = NULL;
812
+
813
+ if (p->FoundState->Freq < MAX_FREQ / 4 && p->MinContext->Suffix != 0)
814
+ {
815
+ c = SUFFIX(p->MinContext);
816
+
817
+ if (c->NumStats == 0)
818
+ {
819
+ s = ONE_STATE(c);
820
+ if (s->Freq < 32)
821
+ s->Freq++;
822
+ }
823
+ else
824
+ {
825
+ s = STATS(c);
826
+ if (s->Symbol != p->FoundState->Symbol)
827
+ {
828
+ do { s++; } while (s->Symbol != p->FoundState->Symbol);
829
+ if (s[0].Freq >= s[-1].Freq)
830
+ {
831
+ SwapStates(&s[0], &s[-1]);
832
+ s--;
833
+ }
834
+ }
835
+ if (s->Freq < MAX_FREQ - 9)
836
+ {
837
+ s->Freq += 2;
838
+ c->SummFreq += 2;
839
+ }
840
+ }
841
+ }
842
+
843
+ c = p->MaxContext;
844
+ if (p->OrderFall == 0 && fSuccessor)
845
+ {
846
+ CTX_PTR cs = CreateSuccessors(p, True, s, p->MinContext);
847
+ if (cs == 0)
848
+ {
849
+ SetSuccessor(p->FoundState, 0);
850
+ RESTORE_MODEL(c, CTX(fSuccessor));
851
+ }
852
+ else
853
+ {
854
+ SetSuccessor(p->FoundState, REF(cs));
855
+ p->MaxContext = cs;
856
+ }
857
+ return;
858
+ }
859
+
860
+ *p->Text++ = p->FoundState->Symbol;
861
+ successor = REF(p->Text);
862
+ if (p->Text >= p->UnitsStart)
863
+ {
864
+ RESTORE_MODEL(c, CTX(fSuccessor)); /* check it */
865
+ return;
866
+ }
867
+
868
+ if (!fSuccessor)
869
+ {
870
+ CTX_PTR cs = ReduceOrder(p, s, p->MinContext);
871
+ if (cs == NULL)
872
+ {
873
+ RESTORE_MODEL(c, 0);
874
+ return;
875
+ }
876
+ fSuccessor = REF(cs);
877
+ }
878
+ else if ((Byte *)Ppmd8_GetPtr(p, fSuccessor) < p->UnitsStart)
879
+ {
880
+ CTX_PTR cs = CreateSuccessors(p, False, s, p->MinContext);
881
+ if (cs == NULL)
882
+ {
883
+ RESTORE_MODEL(c, 0);
884
+ return;
885
+ }
886
+ fSuccessor = REF(cs);
887
+ }
888
+
889
+ if (--p->OrderFall == 0)
890
+ {
891
+ successor = fSuccessor;
892
+ p->Text -= (p->MaxContext != p->MinContext);
893
+ }
894
+ #ifdef PPMD8_FREEZE_SUPPORT
895
+ else if (p->RestoreMethod > PPMD8_RESTORE_METHOD_FREEZE)
896
+ {
897
+ successor = fSuccessor;
898
+ RESET_TEXT(0);
899
+ p->OrderFall = 0;
900
+ }
901
+ #endif
902
+
903
+ s0 = p->MinContext->SummFreq - (ns = p->MinContext->NumStats) - fFreq;
904
+ flag = (Byte)(0x08 * (fSymbol >= 0x40));
905
+
906
+ for (; c != p->MinContext; c = SUFFIX(c))
907
+ {
908
+ unsigned ns1;
909
+ UInt32 cf, sf;
910
+ if ((ns1 = c->NumStats) != 0)
911
+ {
912
+ if ((ns1 & 1) != 0)
913
+ {
914
+ /* Expand for one UNIT */
915
+ unsigned oldNU = (ns1 + 1) >> 1;
916
+ unsigned i = U2I(oldNU);
917
+ if (i != U2I(oldNU + 1))
918
+ {
919
+ void *ptr = AllocUnits(p, i + 1);
920
+ void *oldPtr;
921
+ if (!ptr)
922
+ {
923
+ RESTORE_MODEL(c, CTX(fSuccessor));
924
+ return;
925
+ }
926
+ oldPtr = STATS(c);
927
+ MyMem12Cpy(ptr, oldPtr, oldNU);
928
+ InsertNode(p, oldPtr, i);
929
+ c->Stats = STATS_REF(ptr);
930
+ }
931
+ }
932
+ c->SummFreq = (UInt16)(c->SummFreq + (3 * ns1 + 1 < ns));
933
+ }
934
+ else
935
+ {
936
+ CPpmd_State *s2 = (CPpmd_State*)AllocUnits(p, 0);
937
+ if (!s2)
938
+ {
939
+ RESTORE_MODEL(c, CTX(fSuccessor));
940
+ return;
941
+ }
942
+ *s2 = *ONE_STATE(c);
943
+ c->Stats = REF(s2);
944
+ if (s2->Freq < MAX_FREQ / 4 - 1)
945
+ s2->Freq <<= 1;
946
+ else
947
+ s2->Freq = MAX_FREQ - 4;
948
+ c->SummFreq = (UInt16)(s2->Freq + p->InitEsc + (ns > 2));
949
+ }
950
+ cf = 2 * fFreq * (c->SummFreq + 6);
951
+ sf = (UInt32)s0 + c->SummFreq;
952
+ if (cf < 6 * sf)
953
+ {
954
+ cf = 1 + (cf > sf) + (cf >= 4 * sf);
955
+ c->SummFreq += 4;
956
+ }
957
+ else
958
+ {
959
+ cf = 4 + (cf > 9 * sf) + (cf > 12 * sf) + (cf > 15 * sf);
960
+ c->SummFreq = (UInt16)(c->SummFreq + cf);
961
+ }
962
+ {
963
+ CPpmd_State *s2 = STATS(c) + ns1 + 1;
964
+ SetSuccessor(s2, successor);
965
+ s2->Symbol = fSymbol;
966
+ s2->Freq = (Byte)cf;
967
+ c->Flags |= flag;
968
+ c->NumStats = (Byte)(ns1 + 1);
969
+ }
970
+ }
971
+ p->MaxContext = p->MinContext = CTX(fSuccessor);
972
+ }
973
+
974
+ static void Rescale(CPpmd8 *p)
975
+ {
976
+ unsigned i, adder, sumFreq, escFreq;
977
+ CPpmd_State *stats = STATS(p->MinContext);
978
+ CPpmd_State *s = p->FoundState;
979
+ {
980
+ CPpmd_State tmp = *s;
981
+ for (; s != stats; s--)
982
+ s[0] = s[-1];
983
+ *s = tmp;
984
+ }
985
+ escFreq = p->MinContext->SummFreq - s->Freq;
986
+ s->Freq += 4;
987
+ adder = (p->OrderFall != 0
988
+ #ifdef PPMD8_FREEZE_SUPPORT
989
+ || p->RestoreMethod > PPMD8_RESTORE_METHOD_FREEZE
990
+ #endif
991
+ );
992
+ s->Freq = (Byte)((s->Freq + adder) >> 1);
993
+ sumFreq = s->Freq;
994
+
995
+ i = p->MinContext->NumStats;
996
+ do
997
+ {
998
+ escFreq -= (++s)->Freq;
999
+ s->Freq = (Byte)((s->Freq + adder) >> 1);
1000
+ sumFreq += s->Freq;
1001
+ if (s[0].Freq > s[-1].Freq)
1002
+ {
1003
+ CPpmd_State *s1 = s;
1004
+ CPpmd_State tmp = *s1;
1005
+ do
1006
+ s1[0] = s1[-1];
1007
+ while (--s1 != stats && tmp.Freq > s1[-1].Freq);
1008
+ *s1 = tmp;
1009
+ }
1010
+ }
1011
+ while (--i);
1012
+
1013
+ if (s->Freq == 0)
1014
+ {
1015
+ unsigned numStats = p->MinContext->NumStats;
1016
+ unsigned n0, n1;
1017
+ do { i++; } while ((--s)->Freq == 0);
1018
+ escFreq += i;
1019
+ p->MinContext->NumStats = (Byte)(p->MinContext->NumStats - i);
1020
+ if (p->MinContext->NumStats == 0)
1021
+ {
1022
+ CPpmd_State tmp = *stats;
1023
+ tmp.Freq = (Byte)((2 * tmp.Freq + escFreq - 1) / escFreq);
1024
+ if (tmp.Freq > MAX_FREQ / 3)
1025
+ tmp.Freq = MAX_FREQ / 3;
1026
+ InsertNode(p, stats, U2I((numStats + 2) >> 1));
1027
+ p->MinContext->Flags = (Byte)((p->MinContext->Flags & 0x10) + 0x08 * (tmp.Symbol >= 0x40));
1028
+ *(p->FoundState = ONE_STATE(p->MinContext)) = tmp;
1029
+ return;
1030
+ }
1031
+ n0 = (numStats + 2) >> 1;
1032
+ n1 = (p->MinContext->NumStats + 2) >> 1;
1033
+ if (n0 != n1)
1034
+ p->MinContext->Stats = STATS_REF(ShrinkUnits(p, stats, n0, n1));
1035
+ p->MinContext->Flags &= ~0x08;
1036
+ p->MinContext->Flags |= 0x08 * ((s = STATS(p->MinContext))->Symbol >= 0x40);
1037
+ i = p->MinContext->NumStats;
1038
+ do { p->MinContext->Flags |= 0x08*((++s)->Symbol >= 0x40); } while (--i);
1039
+ }
1040
+ p->MinContext->SummFreq = (UInt16)(sumFreq + escFreq - (escFreq >> 1));
1041
+ p->MinContext->Flags |= 0x4;
1042
+ p->FoundState = STATS(p->MinContext);
1043
+ }
1044
+
1045
+ static CPpmd_See *Ppmd8_MakeEscFreq(CPpmd8 *p, unsigned numMasked1, UInt32 *escFreq)
1046
+ {
1047
+ CPpmd_See *see;
1048
+ if (p->MinContext->NumStats != 0xFF)
1049
+ {
1050
+ see = p->See[(unsigned)p->NS2Indx[(unsigned)p->MinContext->NumStats + 2] - 3] +
1051
+ (p->MinContext->SummFreq > 11 * ((unsigned)p->MinContext->NumStats + 1)) +
1052
+ 2 * (unsigned)(2 * (unsigned)p->MinContext->NumStats <
1053
+ ((unsigned)SUFFIX(p->MinContext)->NumStats + numMasked1)) +
1054
+ p->MinContext->Flags;
1055
+ {
1056
+ unsigned r = (see->Summ >> see->Shift);
1057
+ see->Summ = (UInt16)(see->Summ - r);
1058
+ *escFreq = r + (r == 0);
1059
+ }
1060
+ }
1061
+ else
1062
+ {
1063
+ see = &p->DummySee;
1064
+ *escFreq = 1;
1065
+ }
1066
+ return see;
1067
+ }
1068
+
1069
+ static void NextContext(CPpmd8 *p)
1070
+ {
1071
+ CTX_PTR c = CTX(SUCCESSOR(p->FoundState));
1072
+ if (p->OrderFall == 0 && (Byte *)c >= p->UnitsStart)
1073
+ p->MinContext = p->MaxContext = c;
1074
+ else
1075
+ {
1076
+ UpdateModel(p);
1077
+ p->MinContext = p->MaxContext;
1078
+ }
1079
+ }
1080
+
1081
+ static void Ppmd8_Update1(CPpmd8 *p)
1082
+ {
1083
+ CPpmd_State *s = p->FoundState;
1084
+ s->Freq += 4;
1085
+ p->MinContext->SummFreq += 4;
1086
+ if (s[0].Freq > s[-1].Freq)
1087
+ {
1088
+ SwapStates(&s[0], &s[-1]);
1089
+ p->FoundState = --s;
1090
+ if (s->Freq > MAX_FREQ)
1091
+ Rescale(p);
1092
+ }
1093
+ NextContext(p);
1094
+ }
1095
+
1096
+ static void Ppmd8_Update1_0(CPpmd8 *p)
1097
+ {
1098
+ p->PrevSuccess = (2 * p->FoundState->Freq >= p->MinContext->SummFreq);
1099
+ p->RunLength += p->PrevSuccess;
1100
+ p->MinContext->SummFreq += 4;
1101
+ if ((p->FoundState->Freq += 4) > MAX_FREQ)
1102
+ Rescale(p);
1103
+ NextContext(p);
1104
+ }
1105
+
1106
+ static void Ppmd8_UpdateBin(CPpmd8 *p)
1107
+ {
1108
+ p->FoundState->Freq = (Byte)(p->FoundState->Freq + (p->FoundState->Freq < 196));
1109
+ p->PrevSuccess = 1;
1110
+ p->RunLength++;
1111
+ NextContext(p);
1112
+ }
1113
+
1114
+ static void Ppmd8_Update2(CPpmd8 *p)
1115
+ {
1116
+ p->MinContext->SummFreq += 4;
1117
+ if ((p->FoundState->Freq += 4) > MAX_FREQ)
1118
+ Rescale(p);
1119
+ p->RunLength = p->InitRL;
1120
+ UpdateModel(p);
1121
+ p->MinContext = p->MaxContext;
1122
+ }
1123
+
1124
+ /* Ppmd8Dec.c -- PPMdI Decoder
1125
+ 2010-04-16 : Igor Pavlov : Public domain
1126
+ This code is based on:
1127
+ PPMd var.I (2002): Dmitry Shkarin : Public domain
1128
+ Carryless rangecoder (1999): Dmitry Subbotin : Public domain */
1129
+
1130
+ static Bool Ppmd8_RangeDec_Init(CPpmd8 *p)
1131
+ {
1132
+ unsigned i;
1133
+ p->Low = 0;
1134
+ p->Range = 0xFFFFFFFF;
1135
+ p->Code = 0;
1136
+ for (i = 0; i < 4; i++)
1137
+ p->Code = (p->Code << 8) | p->Stream.In->Read(p->Stream.In);
1138
+ return (p->Code < 0xFFFFFFFF);
1139
+ }
1140
+
1141
+ static UInt32 RangeDec_GetThreshold(CPpmd8 *p, UInt32 total)
1142
+ {
1143
+ return p->Code / (p->Range /= total);
1144
+ }
1145
+
1146
+ static void RangeDec_Decode(CPpmd8 *p, UInt32 start, UInt32 size)
1147
+ {
1148
+ start *= p->Range;
1149
+ p->Low += start;
1150
+ p->Code -= start;
1151
+ p->Range *= size;
1152
+
1153
+ while ((p->Low ^ (p->Low + p->Range)) < kTop ||
1154
+ (p->Range < kBot && ((p->Range = (0 - p->Low) & (kBot - 1)), 1)))
1155
+ {
1156
+ p->Code = (p->Code << 8) | p->Stream.In->Read(p->Stream.In);
1157
+ p->Range <<= 8;
1158
+ p->Low <<= 8;
1159
+ }
1160
+ }
1161
+
1162
+ #define MASK(sym) ((signed char *)charMask)[sym]
1163
+
1164
+ static int Ppmd8_DecodeSymbol(CPpmd8 *p)
1165
+ {
1166
+ size_t charMask[256 / sizeof(size_t)];
1167
+ if (p->MinContext->NumStats != 0)
1168
+ {
1169
+ CPpmd_State *s = Ppmd8_GetStats(p, p->MinContext);
1170
+ unsigned i;
1171
+ UInt32 count, hiCnt;
1172
+ if ((count = RangeDec_GetThreshold(p, p->MinContext->SummFreq)) < (hiCnt = s->Freq))
1173
+ {
1174
+ Byte symbol;
1175
+ RangeDec_Decode(p, 0, s->Freq);
1176
+ p->FoundState = s;
1177
+ symbol = s->Symbol;
1178
+ Ppmd8_Update1_0(p);
1179
+ return symbol;
1180
+ }
1181
+ p->PrevSuccess = 0;
1182
+ i = p->MinContext->NumStats;
1183
+ do
1184
+ {
1185
+ if ((hiCnt += (++s)->Freq) > count)
1186
+ {
1187
+ Byte symbol;
1188
+ RangeDec_Decode(p, hiCnt - s->Freq, s->Freq);
1189
+ p->FoundState = s;
1190
+ symbol = s->Symbol;
1191
+ Ppmd8_Update1(p);
1192
+ return symbol;
1193
+ }
1194
+ }
1195
+ while (--i);
1196
+ if (count >= p->MinContext->SummFreq)
1197
+ return -2;
1198
+ RangeDec_Decode(p, hiCnt, p->MinContext->SummFreq - hiCnt);
1199
+ PPMD_SetAllBitsIn256Bytes(charMask);
1200
+ MASK(s->Symbol) = 0;
1201
+ i = p->MinContext->NumStats;
1202
+ do { MASK((--s)->Symbol) = 0; } while (--i);
1203
+ }
1204
+ else
1205
+ {
1206
+ UInt16 *prob = Ppmd8_GetBinSumm(p);
1207
+ if (((p->Code / (p->Range >>= 14)) < *prob))
1208
+ {
1209
+ Byte symbol;
1210
+ RangeDec_Decode(p, 0, *prob);
1211
+ *prob = (UInt16)PPMD_UPDATE_PROB_0(*prob);
1212
+ symbol = (p->FoundState = Ppmd8Context_OneState(p->MinContext))->Symbol;
1213
+ Ppmd8_UpdateBin(p);
1214
+ return symbol;
1215
+ }
1216
+ RangeDec_Decode(p, *prob, (1 << 14) - *prob);
1217
+ *prob = (UInt16)PPMD_UPDATE_PROB_1(*prob);
1218
+ p->InitEsc = PPMD8_kExpEscape[*prob >> 10];
1219
+ PPMD_SetAllBitsIn256Bytes(charMask);
1220
+ MASK(Ppmd8Context_OneState(p->MinContext)->Symbol) = 0;
1221
+ p->PrevSuccess = 0;
1222
+ }
1223
+ for (;;)
1224
+ {
1225
+ CPpmd_State *ps[256], *s;
1226
+ UInt32 freqSum, count, hiCnt;
1227
+ CPpmd_See *see;
1228
+ unsigned i, num, numMasked = p->MinContext->NumStats;
1229
+ do
1230
+ {
1231
+ p->OrderFall++;
1232
+ if (!p->MinContext->Suffix)
1233
+ return -1;
1234
+ p->MinContext = Ppmd8_GetContext(p, p->MinContext->Suffix);
1235
+ }
1236
+ while (p->MinContext->NumStats == numMasked);
1237
+ hiCnt = 0;
1238
+ s = Ppmd8_GetStats(p, p->MinContext);
1239
+ i = 0;
1240
+ num = p->MinContext->NumStats - numMasked;
1241
+ do
1242
+ {
1243
+ int k = (int)(MASK(s->Symbol));
1244
+ hiCnt += (s->Freq & k);
1245
+ ps[i] = s++;
1246
+ i -= k;
1247
+ }
1248
+ while (i != num);
1249
+
1250
+ see = Ppmd8_MakeEscFreq(p, numMasked, &freqSum);
1251
+ freqSum += hiCnt;
1252
+ count = RangeDec_GetThreshold(p, freqSum);
1253
+
1254
+ if (count < hiCnt)
1255
+ {
1256
+ Byte symbol;
1257
+ CPpmd_State **pps = ps;
1258
+ for (hiCnt = 0; (hiCnt += (*pps)->Freq) <= count; pps++);
1259
+ s = *pps;
1260
+ RangeDec_Decode(p, hiCnt - s->Freq, s->Freq);
1261
+ Ppmd_See_Update(see);
1262
+ p->FoundState = s;
1263
+ symbol = s->Symbol;
1264
+ Ppmd8_Update2(p);
1265
+ return symbol;
1266
+ }
1267
+ if (count >= freqSum)
1268
+ return -2;
1269
+ RangeDec_Decode(p, hiCnt, freqSum - hiCnt);
1270
+ see->Summ = (UInt16)(see->Summ + freqSum);
1271
+ do { MASK(ps[--i]->Symbol) = 0; } while (i != 0);
1272
+ }
1273
+ }
1274
+
1275
+ /* H->I changes:
1276
+ NS2Indx
1277
+ GlewCount, and Glue method
1278
+ BinSum
1279
+ See / EscFreq
1280
+ CreateSuccessors updates more suffix contexts
1281
+ UpdateModel consts.
1282
+ PrevSuccess Update
1283
+ */
1284
+
1285
+ const IPpmd8 __archive_ppmd8_functions =
1286
+ {
1287
+ &Ppmd8_Construct,
1288
+ &Ppmd8_Alloc,
1289
+ &Ppmd8_Free,
1290
+ &Ppmd8_Init,
1291
+ &Ppmd8_RangeDec_Init,
1292
+ &Ppmd8_DecodeSymbol,
1293
+ };