@papyrus-sdk/engine-native 0.2.22-beta.2 → 0.2.22-beta.3

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 (266) hide show
  1. package/android/build.gradle +1 -0
  2. package/android/src/main/cpp/CMakeLists.txt +137 -0
  3. package/android/src/main/cpp/PapyrusComicArchiveJni.cpp +68 -0
  4. package/android/src/main/cpp/PapyrusComicArchiveTest.cpp +55 -0
  5. package/android/src/main/java/com/papyrus/engine/PapyrusComicArchiveNative.java +14 -0
  6. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentView.java +353 -0
  7. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentViewManager.java +32 -0
  8. package/android/src/main/java/com/papyrus/engine/PapyrusComicPageLayout.java +17 -0
  9. package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +298 -34
  10. package/android/src/main/java/com/papyrus/engine/PapyrusNativeTextModel.java +195 -0
  11. package/android/src/main/java/com/papyrus/engine/PapyrusPackage.java +3 -1
  12. package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerView.java +24 -0
  13. package/android/src/main/java/com/papyrus/engine/PapyrusRenderMath.java +9 -0
  14. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentView.java +439 -0
  15. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentViewManager.java +43 -0
  16. package/android/src/main/java/com/papyrus/engine/PapyrusTextStore.java +43 -0
  17. package/dist/index.d.mts +439 -3
  18. package/dist/index.d.ts +439 -3
  19. package/dist/index.js +412 -4
  20. package/dist/index.js.map +1 -1
  21. package/dist/index.mjs +404 -3
  22. package/dist/index.mjs.map +1 -1
  23. package/ios/PapyrusComicDocumentView.h +21 -0
  24. package/ios/PapyrusComicDocumentView.m +374 -0
  25. package/ios/PapyrusComicDocumentViewManager.m +23 -0
  26. package/ios/PapyrusEngineStore.m +4 -0
  27. package/ios/PapyrusNativeEngine.m +664 -0
  28. package/ios/PapyrusNativeEngine.podspec +26 -1
  29. package/ios/PapyrusPageRotationRegistry.h +29 -0
  30. package/ios/PapyrusPageRotationRegistry.m +179 -0
  31. package/ios/PapyrusPageView.m +38 -11
  32. package/ios/PapyrusPdfDocumentView.h +2 -0
  33. package/ios/PapyrusPdfDocumentView.m +116 -9
  34. package/ios/PapyrusPdfDocumentViewManager.m +2 -0
  35. package/ios/PapyrusPdfPageTheme.h +15 -0
  36. package/ios/PapyrusPdfPageTheme.m +218 -0
  37. package/ios/PapyrusTextDocumentView.h +27 -0
  38. package/ios/PapyrusTextDocumentView.m +300 -0
  39. package/ios/PapyrusTextDocumentViewManager.m +29 -0
  40. package/ios/PapyrusTextStore.h +13 -0
  41. package/ios/PapyrusTextStore.m +73 -0
  42. package/package.json +5 -3
  43. package/third_party_licenses/BLAKE2-NOTICE.txt +11 -0
  44. package/third_party_licenses/libarchive-COPYING +65 -0
  45. package/vendor/libarchive/PapyrusComicArchive.cpp +376 -0
  46. package/vendor/libarchive/PapyrusComicArchive.h +30 -0
  47. package/vendor/libarchive/README.papyrus.md +15 -0
  48. package/vendor/libarchive/android.h +192 -0
  49. package/vendor/libarchive/config/PapyrusArchiveConfig.h +65 -0
  50. package/vendor/libarchive/libarchive/CMakeLists.txt +293 -0
  51. package/vendor/libarchive/libarchive/android_lf.h +36 -0
  52. package/vendor/libarchive/libarchive/archive.h +1266 -0
  53. package/vendor/libarchive/libarchive/archive_acl.c +2025 -0
  54. package/vendor/libarchive/libarchive/archive_acl_private.h +85 -0
  55. package/vendor/libarchive/libarchive/archive_blake2.h +197 -0
  56. package/vendor/libarchive/libarchive/archive_blake2_impl.h +161 -0
  57. package/vendor/libarchive/libarchive/archive_blake2s_ref.c +369 -0
  58. package/vendor/libarchive/libarchive/archive_blake2sp_ref.c +361 -0
  59. package/vendor/libarchive/libarchive/archive_check_magic.c +183 -0
  60. package/vendor/libarchive/libarchive/archive_cmdline.c +211 -0
  61. package/vendor/libarchive/libarchive/archive_cmdline_private.h +45 -0
  62. package/vendor/libarchive/libarchive/archive_crc32.h +123 -0
  63. package/vendor/libarchive/libarchive/archive_cryptor.c +534 -0
  64. package/vendor/libarchive/libarchive/archive_cryptor_private.h +199 -0
  65. package/vendor/libarchive/libarchive/archive_digest.c +1584 -0
  66. package/vendor/libarchive/libarchive/archive_digest_private.h +431 -0
  67. package/vendor/libarchive/libarchive/archive_disk_acl_darwin.c +558 -0
  68. package/vendor/libarchive/libarchive/archive_disk_acl_freebsd.c +712 -0
  69. package/vendor/libarchive/libarchive/archive_disk_acl_linux.c +760 -0
  70. package/vendor/libarchive/libarchive/archive_disk_acl_sunos.c +824 -0
  71. package/vendor/libarchive/libarchive/archive_endian.h +234 -0
  72. package/vendor/libarchive/libarchive/archive_entry.3 +147 -0
  73. package/vendor/libarchive/libarchive/archive_entry.c +2297 -0
  74. package/vendor/libarchive/libarchive/archive_entry.h +763 -0
  75. package/vendor/libarchive/libarchive/archive_entry_acl.3 +458 -0
  76. package/vendor/libarchive/libarchive/archive_entry_copy_bhfi.c +55 -0
  77. package/vendor/libarchive/libarchive/archive_entry_copy_stat.c +82 -0
  78. package/vendor/libarchive/libarchive/archive_entry_link_resolver.c +472 -0
  79. package/vendor/libarchive/libarchive/archive_entry_linkify.3 +224 -0
  80. package/vendor/libarchive/libarchive/archive_entry_locale.h +90 -0
  81. package/vendor/libarchive/libarchive/archive_entry_misc.3 +63 -0
  82. package/vendor/libarchive/libarchive/archive_entry_paths.3 +153 -0
  83. package/vendor/libarchive/libarchive/archive_entry_perms.3 +210 -0
  84. package/vendor/libarchive/libarchive/archive_entry_private.h +205 -0
  85. package/vendor/libarchive/libarchive/archive_entry_sparse.c +156 -0
  86. package/vendor/libarchive/libarchive/archive_entry_stat.3 +274 -0
  87. package/vendor/libarchive/libarchive/archive_entry_stat.c +121 -0
  88. package/vendor/libarchive/libarchive/archive_entry_strmode.c +69 -0
  89. package/vendor/libarchive/libarchive/archive_entry_time.3 +127 -0
  90. package/vendor/libarchive/libarchive/archive_entry_xattr.c +155 -0
  91. package/vendor/libarchive/libarchive/archive_hmac.c +345 -0
  92. package/vendor/libarchive/libarchive/archive_hmac_private.h +119 -0
  93. package/vendor/libarchive/libarchive/archive_integer.h +267 -0
  94. package/vendor/libarchive/libarchive/archive_match.c +1869 -0
  95. package/vendor/libarchive/libarchive/archive_openssl_evp_private.h +54 -0
  96. package/vendor/libarchive/libarchive/archive_openssl_hmac_private.h +54 -0
  97. package/vendor/libarchive/libarchive/archive_options.c +219 -0
  98. package/vendor/libarchive/libarchive/archive_options_private.h +49 -0
  99. package/vendor/libarchive/libarchive/archive_pack_dev.c +330 -0
  100. package/vendor/libarchive/libarchive/archive_pack_dev.h +49 -0
  101. package/vendor/libarchive/libarchive/archive_parse_date.c +1173 -0
  102. package/vendor/libarchive/libarchive/archive_pathmatch.c +480 -0
  103. package/vendor/libarchive/libarchive/archive_pathmatch.h +50 -0
  104. package/vendor/libarchive/libarchive/archive_platform.h +213 -0
  105. package/vendor/libarchive/libarchive/archive_platform_acl.h +53 -0
  106. package/vendor/libarchive/libarchive/archive_platform_stat.h +45 -0
  107. package/vendor/libarchive/libarchive/archive_platform_xattr.h +45 -0
  108. package/vendor/libarchive/libarchive/archive_ppmd7.c +1173 -0
  109. package/vendor/libarchive/libarchive/archive_ppmd7_private.h +119 -0
  110. package/vendor/libarchive/libarchive/archive_ppmd8.c +1293 -0
  111. package/vendor/libarchive/libarchive/archive_ppmd8_private.h +123 -0
  112. package/vendor/libarchive/libarchive/archive_ppmd_private.h +159 -0
  113. package/vendor/libarchive/libarchive/archive_private.h +209 -0
  114. package/vendor/libarchive/libarchive/archive_random.c +274 -0
  115. package/vendor/libarchive/libarchive/archive_random_private.h +36 -0
  116. package/vendor/libarchive/libarchive/archive_rb.c +709 -0
  117. package/vendor/libarchive/libarchive/archive_rb.h +113 -0
  118. package/vendor/libarchive/libarchive/archive_read.3 +250 -0
  119. package/vendor/libarchive/libarchive/archive_read.c +1814 -0
  120. package/vendor/libarchive/libarchive/archive_read_add_passphrase.3 +72 -0
  121. package/vendor/libarchive/libarchive/archive_read_add_passphrase.c +189 -0
  122. package/vendor/libarchive/libarchive/archive_read_append_filter.c +209 -0
  123. package/vendor/libarchive/libarchive/archive_read_data.3 +128 -0
  124. package/vendor/libarchive/libarchive/archive_read_data_into_fd.c +207 -0
  125. package/vendor/libarchive/libarchive/archive_read_disk.3 +422 -0
  126. package/vendor/libarchive/libarchive/archive_read_disk_entry_from_file.c +1086 -0
  127. package/vendor/libarchive/libarchive/archive_read_disk_posix.c +2639 -0
  128. package/vendor/libarchive/libarchive/archive_read_disk_private.h +97 -0
  129. package/vendor/libarchive/libarchive/archive_read_disk_set_standard_lookup.c +315 -0
  130. package/vendor/libarchive/libarchive/archive_read_disk_windows.c +2603 -0
  131. package/vendor/libarchive/libarchive/archive_read_extract.3 +135 -0
  132. package/vendor/libarchive/libarchive/archive_read_extract.c +59 -0
  133. package/vendor/libarchive/libarchive/archive_read_extract2.c +154 -0
  134. package/vendor/libarchive/libarchive/archive_read_filter.3 +163 -0
  135. package/vendor/libarchive/libarchive/archive_read_format.3 +190 -0
  136. package/vendor/libarchive/libarchive/archive_read_free.3 +91 -0
  137. package/vendor/libarchive/libarchive/archive_read_header.3 +89 -0
  138. package/vendor/libarchive/libarchive/archive_read_new.3 +57 -0
  139. package/vendor/libarchive/libarchive/archive_read_open.3 +247 -0
  140. package/vendor/libarchive/libarchive/archive_read_open_fd.c +236 -0
  141. package/vendor/libarchive/libarchive/archive_read_open_file.c +257 -0
  142. package/vendor/libarchive/libarchive/archive_read_open_filename.c +652 -0
  143. package/vendor/libarchive/libarchive/archive_read_open_memory.c +188 -0
  144. package/vendor/libarchive/libarchive/archive_read_private.h +274 -0
  145. package/vendor/libarchive/libarchive/archive_read_set_format.c +116 -0
  146. package/vendor/libarchive/libarchive/archive_read_set_options.3 +290 -0
  147. package/vendor/libarchive/libarchive/archive_read_set_options.c +132 -0
  148. package/vendor/libarchive/libarchive/archive_read_support_filter_all.c +84 -0
  149. package/vendor/libarchive/libarchive/archive_read_support_filter_by_code.c +68 -0
  150. package/vendor/libarchive/libarchive/archive_read_support_filter_bzip2.c +360 -0
  151. package/vendor/libarchive/libarchive/archive_read_support_filter_compress.c +457 -0
  152. package/vendor/libarchive/libarchive/archive_read_support_filter_grzip.c +108 -0
  153. package/vendor/libarchive/libarchive/archive_read_support_filter_gzip.c +538 -0
  154. package/vendor/libarchive/libarchive/archive_read_support_filter_lrzip.c +119 -0
  155. package/vendor/libarchive/libarchive/archive_read_support_filter_lz4.c +829 -0
  156. package/vendor/libarchive/libarchive/archive_read_support_filter_lzop.c +493 -0
  157. package/vendor/libarchive/libarchive/archive_read_support_filter_none.c +51 -0
  158. package/vendor/libarchive/libarchive/archive_read_support_filter_program.c +497 -0
  159. package/vendor/libarchive/libarchive/archive_read_support_filter_rpm.c +249 -0
  160. package/vendor/libarchive/libarchive/archive_read_support_filter_uu.c +739 -0
  161. package/vendor/libarchive/libarchive/archive_read_support_filter_xz.c +792 -0
  162. package/vendor/libarchive/libarchive/archive_read_support_filter_zstd.c +356 -0
  163. package/vendor/libarchive/libarchive/archive_read_support_format_7zip.c +4650 -0
  164. package/vendor/libarchive/libarchive/archive_read_support_format_all.c +88 -0
  165. package/vendor/libarchive/libarchive/archive_read_support_format_ar.c +597 -0
  166. package/vendor/libarchive/libarchive/archive_read_support_format_by_code.c +76 -0
  167. package/vendor/libarchive/libarchive/archive_read_support_format_cab.c +3410 -0
  168. package/vendor/libarchive/libarchive/archive_read_support_format_cpio.c +1152 -0
  169. package/vendor/libarchive/libarchive/archive_read_support_format_empty.c +95 -0
  170. package/vendor/libarchive/libarchive/archive_read_support_format_iso9660.c +3516 -0
  171. package/vendor/libarchive/libarchive/archive_read_support_format_lha.c +2931 -0
  172. package/vendor/libarchive/libarchive/archive_read_support_format_mtree.c +2113 -0
  173. package/vendor/libarchive/libarchive/archive_read_support_format_rar.c +3932 -0
  174. package/vendor/libarchive/libarchive/archive_read_support_format_rar5.c +4501 -0
  175. package/vendor/libarchive/libarchive/archive_read_support_format_raw.c +194 -0
  176. package/vendor/libarchive/libarchive/archive_read_support_format_tar.c +3785 -0
  177. package/vendor/libarchive/libarchive/archive_read_support_format_warc.c +875 -0
  178. package/vendor/libarchive/libarchive/archive_read_support_format_xar.c +3770 -0
  179. package/vendor/libarchive/libarchive/archive_read_support_format_zip.c +4710 -0
  180. package/vendor/libarchive/libarchive/archive_string.c +4391 -0
  181. package/vendor/libarchive/libarchive/archive_string.h +244 -0
  182. package/vendor/libarchive/libarchive/archive_string_composition.h +2289 -0
  183. package/vendor/libarchive/libarchive/archive_string_sprintf.c +200 -0
  184. package/vendor/libarchive/libarchive/archive_time.c +168 -0
  185. package/vendor/libarchive/libarchive/archive_time_private.h +47 -0
  186. package/vendor/libarchive/libarchive/archive_util.3 +239 -0
  187. package/vendor/libarchive/libarchive/archive_util.c +668 -0
  188. package/vendor/libarchive/libarchive/archive_version_details.c +516 -0
  189. package/vendor/libarchive/libarchive/archive_virtual.c +162 -0
  190. package/vendor/libarchive/libarchive/archive_windows.c +1035 -0
  191. package/vendor/libarchive/libarchive/archive_windows.h +332 -0
  192. package/vendor/libarchive/libarchive/archive_write.3 +266 -0
  193. package/vendor/libarchive/libarchive/archive_write.c +896 -0
  194. package/vendor/libarchive/libarchive/archive_write_add_filter.c +71 -0
  195. package/vendor/libarchive/libarchive/archive_write_add_filter_b64encode.c +329 -0
  196. package/vendor/libarchive/libarchive/archive_write_add_filter_by_name.c +76 -0
  197. package/vendor/libarchive/libarchive/archive_write_add_filter_bzip2.c +411 -0
  198. package/vendor/libarchive/libarchive/archive_write_add_filter_compress.c +459 -0
  199. package/vendor/libarchive/libarchive/archive_write_add_filter_grzip.c +143 -0
  200. package/vendor/libarchive/libarchive/archive_write_add_filter_gzip.c +493 -0
  201. package/vendor/libarchive/libarchive/archive_write_add_filter_lrzip.c +214 -0
  202. package/vendor/libarchive/libarchive/archive_write_add_filter_lz4.c +710 -0
  203. package/vendor/libarchive/libarchive/archive_write_add_filter_lzop.c +500 -0
  204. package/vendor/libarchive/libarchive/archive_write_add_filter_none.c +42 -0
  205. package/vendor/libarchive/libarchive/archive_write_add_filter_program.c +398 -0
  206. package/vendor/libarchive/libarchive/archive_write_add_filter_uuencode.c +320 -0
  207. package/vendor/libarchive/libarchive/archive_write_add_filter_xz.c +547 -0
  208. package/vendor/libarchive/libarchive/archive_write_add_filter_zstd.c +608 -0
  209. package/vendor/libarchive/libarchive/archive_write_blocksize.3 +112 -0
  210. package/vendor/libarchive/libarchive/archive_write_data.3 +88 -0
  211. package/vendor/libarchive/libarchive/archive_write_disk.3 +360 -0
  212. package/vendor/libarchive/libarchive/archive_write_disk_posix.c +4789 -0
  213. package/vendor/libarchive/libarchive/archive_write_disk_private.h +43 -0
  214. package/vendor/libarchive/libarchive/archive_write_disk_set_standard_lookup.c +265 -0
  215. package/vendor/libarchive/libarchive/archive_write_disk_windows.c +2952 -0
  216. package/vendor/libarchive/libarchive/archive_write_filter.3 +140 -0
  217. package/vendor/libarchive/libarchive/archive_write_finish_entry.3 +77 -0
  218. package/vendor/libarchive/libarchive/archive_write_format.3 +187 -0
  219. package/vendor/libarchive/libarchive/archive_write_free.3 +94 -0
  220. package/vendor/libarchive/libarchive/archive_write_header.3 +71 -0
  221. package/vendor/libarchive/libarchive/archive_write_new.3 +56 -0
  222. package/vendor/libarchive/libarchive/archive_write_open.3 +270 -0
  223. package/vendor/libarchive/libarchive/archive_write_open_fd.c +141 -0
  224. package/vendor/libarchive/libarchive/archive_write_open_file.c +102 -0
  225. package/vendor/libarchive/libarchive/archive_write_open_filename.c +272 -0
  226. package/vendor/libarchive/libarchive/archive_write_open_memory.c +110 -0
  227. package/vendor/libarchive/libarchive/archive_write_private.h +166 -0
  228. package/vendor/libarchive/libarchive/archive_write_set_format.c +124 -0
  229. package/vendor/libarchive/libarchive/archive_write_set_format_7zip.c +2571 -0
  230. package/vendor/libarchive/libarchive/archive_write_set_format_ar.c +540 -0
  231. package/vendor/libarchive/libarchive/archive_write_set_format_by_name.c +93 -0
  232. package/vendor/libarchive/libarchive/archive_write_set_format_cpio.c +11 -0
  233. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_binary.c +618 -0
  234. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_newc.c +457 -0
  235. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_odc.c +508 -0
  236. package/vendor/libarchive/libarchive/archive_write_set_format_filter_by_ext.c +141 -0
  237. package/vendor/libarchive/libarchive/archive_write_set_format_gnutar.c +790 -0
  238. package/vendor/libarchive/libarchive/archive_write_set_format_iso9660.c +8216 -0
  239. package/vendor/libarchive/libarchive/archive_write_set_format_mtree.c +2307 -0
  240. package/vendor/libarchive/libarchive/archive_write_set_format_pax.c +2249 -0
  241. package/vendor/libarchive/libarchive/archive_write_set_format_private.h +40 -0
  242. package/vendor/libarchive/libarchive/archive_write_set_format_raw.c +123 -0
  243. package/vendor/libarchive/libarchive/archive_write_set_format_shar.c +650 -0
  244. package/vendor/libarchive/libarchive/archive_write_set_format_ustar.c +768 -0
  245. package/vendor/libarchive/libarchive/archive_write_set_format_v7tar.c +648 -0
  246. package/vendor/libarchive/libarchive/archive_write_set_format_warc.c +491 -0
  247. package/vendor/libarchive/libarchive/archive_write_set_format_xar.c +3799 -0
  248. package/vendor/libarchive/libarchive/archive_write_set_format_zip.c +2548 -0
  249. package/vendor/libarchive/libarchive/archive_write_set_options.3 +779 -0
  250. package/vendor/libarchive/libarchive/archive_write_set_options.c +129 -0
  251. package/vendor/libarchive/libarchive/archive_write_set_passphrase.3 +72 -0
  252. package/vendor/libarchive/libarchive/archive_write_set_passphrase.c +95 -0
  253. package/vendor/libarchive/libarchive/archive_xxhash.h +48 -0
  254. package/vendor/libarchive/libarchive/config_freebsd.h +275 -0
  255. package/vendor/libarchive/libarchive/cpio.5 +389 -0
  256. package/vendor/libarchive/libarchive/filter_fork.h +44 -0
  257. package/vendor/libarchive/libarchive/filter_fork_posix.c +269 -0
  258. package/vendor/libarchive/libarchive/filter_fork_windows.c +270 -0
  259. package/vendor/libarchive/libarchive/libarchive-formats.5 +478 -0
  260. package/vendor/libarchive/libarchive/libarchive.3 +297 -0
  261. package/vendor/libarchive/libarchive/libarchive_changes.3 +339 -0
  262. package/vendor/libarchive/libarchive/libarchive_internals.3 +363 -0
  263. package/vendor/libarchive/libarchive/module.modulemap +34 -0
  264. package/vendor/libarchive/libarchive/mtree.5 +323 -0
  265. package/vendor/libarchive/libarchive/tar.5 +947 -0
  266. package/vendor/libarchive/libarchive/xxhash.c +531 -0
@@ -0,0 +1,3932 @@
1
+ /*-
2
+ * Copyright (c) 2003-2007 Tim Kientzle
3
+ * Copyright (c) 2011 Andres Mejia
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions
8
+ * are met:
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ *
15
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ */
26
+
27
+ #include "archive_platform.h"
28
+
29
+ #ifdef HAVE_ERRNO_H
30
+ #include <errno.h>
31
+ #endif
32
+ #include <time.h>
33
+ #include <limits.h>
34
+ #ifdef HAVE_ZLIB_H
35
+ #include <zlib.h> /* crc32 */
36
+ #endif
37
+
38
+ #include "archive.h"
39
+ #ifndef HAVE_ZLIB_H
40
+ #include "archive_crc32.h"
41
+ #endif
42
+ #include "archive_endian.h"
43
+ #include "archive_entry.h"
44
+ #include "archive_entry_locale.h"
45
+ #include "archive_integer.h"
46
+ #include "archive_ppmd7_private.h"
47
+ #include "archive_private.h"
48
+ #include "archive_read_private.h"
49
+
50
+ /* RAR signature, also known as the mark header */
51
+ #define RAR_SIGNATURE "\x52\x61\x72\x21\x1A\x07\x00"
52
+
53
+ /* Header types */
54
+ #define MARK_HEAD 0x72
55
+ #define MAIN_HEAD 0x73
56
+ #define FILE_HEAD 0x74
57
+ #define COMM_HEAD 0x75
58
+ #define AV_HEAD 0x76
59
+ #define SUB_HEAD 0x77
60
+ #define PROTECT_HEAD 0x78
61
+ #define SIGN_HEAD 0x79
62
+ #define NEWSUB_HEAD 0x7a
63
+ #define ENDARC_HEAD 0x7b
64
+
65
+ /* Main Header Flags */
66
+ #define MHD_VOLUME 0x0001
67
+ #define MHD_COMMENT 0x0002
68
+ #define MHD_LOCK 0x0004
69
+ #define MHD_SOLID 0x0008
70
+ #define MHD_NEWNUMBERING 0x0010
71
+ #define MHD_AV 0x0020
72
+ #define MHD_PROTECT 0x0040
73
+ #define MHD_PASSWORD 0x0080
74
+ #define MHD_FIRSTVOLUME 0x0100
75
+ #define MHD_ENCRYPTVER 0x0200
76
+
77
+ /* Flags common to all headers */
78
+ #define HD_MARKDELETION 0x4000
79
+ #define HD_ADD_SIZE_PRESENT 0x8000
80
+
81
+ /* File Header Flags */
82
+ #define FHD_SPLIT_BEFORE 0x0001
83
+ #define FHD_SPLIT_AFTER 0x0002
84
+ #define FHD_PASSWORD 0x0004
85
+ #define FHD_COMMENT 0x0008
86
+ #define FHD_SOLID 0x0010
87
+ #define FHD_LARGE 0x0100
88
+ #define FHD_UNICODE 0x0200
89
+ #define FHD_SALT 0x0400
90
+ #define FHD_VERSION 0x0800
91
+ #define FHD_EXTTIME 0x1000
92
+ #define FHD_EXTFLAGS 0x2000
93
+
94
+ /* File dictionary sizes */
95
+ #define DICTIONARY_SIZE_64 0x00
96
+ #define DICTIONARY_SIZE_128 0x20
97
+ #define DICTIONARY_SIZE_256 0x40
98
+ #define DICTIONARY_SIZE_512 0x60
99
+ #define DICTIONARY_SIZE_1024 0x80
100
+ #define DICTIONARY_SIZE_2048 0xA0
101
+ #define DICTIONARY_SIZE_4096 0xC0
102
+ #define FILE_IS_DIRECTORY 0xE0
103
+ #define DICTIONARY_MASK FILE_IS_DIRECTORY
104
+
105
+ /* OS Flags */
106
+ #define OS_MSDOS 0
107
+ #define OS_OS2 1
108
+ #define OS_WIN32 2
109
+ #define OS_UNIX 3
110
+ #define OS_MAC_OS 4
111
+ #define OS_BEOS 5
112
+
113
+ /* Compression Methods */
114
+ #define COMPRESS_METHOD_STORE 0x30
115
+ /* LZSS */
116
+ #define COMPRESS_METHOD_FASTEST 0x31
117
+ #define COMPRESS_METHOD_FAST 0x32
118
+ #define COMPRESS_METHOD_NORMAL 0x33
119
+ /* PPMd Variant H */
120
+ #define COMPRESS_METHOD_GOOD 0x34
121
+ #define COMPRESS_METHOD_BEST 0x35
122
+
123
+ #define CRC_POLYNOMIAL 0xEDB88320
124
+
125
+ #define NS_UNIT 10000000
126
+
127
+ #define DICTIONARY_MAX_SIZE 0x400000
128
+
129
+ #define MAINCODE_SIZE 299
130
+ #define OFFSETCODE_SIZE 60
131
+ #define LOWOFFSETCODE_SIZE 17
132
+ #define LENGTHCODE_SIZE 28
133
+ #define HUFFMAN_TABLE_SIZE \
134
+ MAINCODE_SIZE + OFFSETCODE_SIZE + LOWOFFSETCODE_SIZE + LENGTHCODE_SIZE
135
+
136
+ #define MAX_SYMBOL_LENGTH 0xF
137
+ #define MAX_SYMBOLS 20
138
+
139
+ /* Virtual Machine Properties */
140
+ #define VM_MEMORY_SIZE 0x40000
141
+ #define VM_MEMORY_MASK (VM_MEMORY_SIZE - 1)
142
+ #define PROGRAM_WORK_SIZE 0x3C000
143
+ #define PROGRAM_GLOBAL_SIZE 0x2000
144
+ #define PROGRAM_SYSTEM_GLOBAL_ADDRESS PROGRAM_WORK_SIZE
145
+ #define PROGRAM_SYSTEM_GLOBAL_SIZE 0x40
146
+ #define PROGRAM_USER_GLOBAL_ADDRESS (PROGRAM_SYSTEM_GLOBAL_ADDRESS + PROGRAM_SYSTEM_GLOBAL_SIZE)
147
+ #define PROGRAM_USER_GLOBAL_SIZE (PROGRAM_GLOBAL_SIZE - PROGRAM_SYSTEM_GLOBAL_SIZE)
148
+
149
+ /*
150
+ * Considering L1,L2 cache miss and a calling of write system-call,
151
+ * the best size of the output buffer(uncompressed buffer) is 128K.
152
+ * If the structure of extracting process is changed, this value
153
+ * might be researched again.
154
+ */
155
+ #define UNP_BUFFER_SIZE (128 * 1024)
156
+
157
+ /* Define this here for non-Windows platforms */
158
+ #ifndef FILE_ATTRIBUTE_DIRECTORY
159
+ #define FILE_ATTRIBUTE_DIRECTORY 0x10
160
+ #endif
161
+
162
+ #undef minimum
163
+ #define minimum(a, b) ((a)<(b)?(a):(b))
164
+
165
+ /* Stack overflow check */
166
+ #define MAX_COMPRESS_DEPTH 1024
167
+
168
+ /* Fields common to all headers */
169
+ struct rar_header
170
+ {
171
+ char crc[2];
172
+ char type;
173
+ char flags[2];
174
+ char size[2];
175
+ };
176
+
177
+ /* Fields common to all file headers */
178
+ struct rar_file_header
179
+ {
180
+ char pack_size[4];
181
+ char unp_size[4];
182
+ char host_os;
183
+ char file_crc[4];
184
+ char file_time[4];
185
+ char unp_ver;
186
+ char method;
187
+ char name_size[2];
188
+ char file_attr[4];
189
+ };
190
+
191
+ struct huffman_tree_node
192
+ {
193
+ int branches[2];
194
+ };
195
+
196
+ struct huffman_table_entry
197
+ {
198
+ unsigned int length;
199
+ int value;
200
+ };
201
+
202
+ struct huffman_code
203
+ {
204
+ struct huffman_tree_node *tree;
205
+ size_t numentries;
206
+ size_t numallocatedentries;
207
+ int minlength;
208
+ int maxlength;
209
+ int tablesize;
210
+ struct huffman_table_entry *table;
211
+ };
212
+
213
+ struct lzss
214
+ {
215
+ unsigned char *window;
216
+ int mask;
217
+ int64_t position;
218
+ };
219
+
220
+ struct data_block_offsets
221
+ {
222
+ int64_t header_size;
223
+ int64_t start_offset;
224
+ int64_t end_offset;
225
+ };
226
+
227
+ struct rar_program_code
228
+ {
229
+ uint8_t *staticdata;
230
+ uint32_t staticdatalen;
231
+ uint64_t fingerprint;
232
+ uint32_t usagecount;
233
+ uint32_t oldfilterlength;
234
+ struct rar_program_code *next;
235
+ };
236
+
237
+ struct rar_filter
238
+ {
239
+ struct rar_program_code *prog;
240
+ uint32_t initialregisters[8];
241
+ uint8_t *globaldata;
242
+ uint32_t globaldatalen;
243
+ size_t blockstartpos;
244
+ uint32_t blocklength;
245
+ uint32_t filteredblockaddress;
246
+ uint32_t filteredblocklength;
247
+ struct rar_filter *next;
248
+ };
249
+
250
+ struct memory_bit_reader
251
+ {
252
+ const uint8_t *bytes;
253
+ size_t length;
254
+ size_t offset;
255
+ uint64_t bits;
256
+ int available;
257
+ int at_eof;
258
+ };
259
+
260
+ struct rar_virtual_machine
261
+ {
262
+ uint32_t registers[8];
263
+ uint8_t memory[VM_MEMORY_SIZE + sizeof(uint32_t)];
264
+ };
265
+
266
+ struct rar_filters
267
+ {
268
+ struct rar_virtual_machine *vm;
269
+ struct rar_program_code *progs;
270
+ struct rar_filter *stack;
271
+ int64_t filterstart;
272
+ uint32_t lastfilternum;
273
+ int64_t lastend;
274
+ uint8_t *bytes;
275
+ size_t bytes_ready;
276
+ };
277
+
278
+ struct audio_state
279
+ {
280
+ int8_t weight[5];
281
+ int16_t delta[4];
282
+ int8_t lastdelta;
283
+ int error[11];
284
+ int count;
285
+ uint8_t lastbyte;
286
+ };
287
+
288
+ struct rar
289
+ {
290
+ /* Entries from main RAR header */
291
+ unsigned main_flags;
292
+ unsigned long file_crc;
293
+ char reserved1[2];
294
+ char reserved2[4];
295
+ char encryptver;
296
+
297
+ /* File header entries */
298
+ char compression_method;
299
+ unsigned file_flags;
300
+ int64_t packed_size;
301
+ int64_t unp_size;
302
+ time_t mtime;
303
+ long mnsec;
304
+ mode_t mode;
305
+ char *filename;
306
+ char *filename_save;
307
+ size_t filename_save_size;
308
+ size_t filename_allocated;
309
+
310
+ /* File header optional entries */
311
+ char salt[8];
312
+ time_t atime;
313
+ long ansec;
314
+ time_t ctime;
315
+ long cnsec;
316
+ time_t arctime;
317
+ long arcnsec;
318
+
319
+ /* Fields to help with tracking decompression of files. */
320
+ int64_t bytes_unconsumed;
321
+ int64_t bytes_remaining;
322
+ int64_t bytes_uncopied;
323
+ int64_t offset;
324
+ int64_t offset_outgoing;
325
+ int64_t offset_seek;
326
+ char valid;
327
+ unsigned int unp_offset;
328
+ unsigned int unp_buffer_size;
329
+ unsigned char *unp_buffer;
330
+ unsigned int dictionary_size;
331
+ char start_new_block;
332
+ char entry_eof;
333
+ unsigned long crc_calculated;
334
+ int found_first_header;
335
+ char has_endarc_header;
336
+ struct data_block_offsets *dbo;
337
+ size_t cursor;
338
+ size_t nodes;
339
+ char filename_must_match;
340
+
341
+ /* LZSS members */
342
+ struct huffman_code maincode;
343
+ struct huffman_code offsetcode;
344
+ struct huffman_code lowoffsetcode;
345
+ struct huffman_code lengthcode;
346
+ unsigned char lengthtable[HUFFMAN_TABLE_SIZE];
347
+ struct lzss lzss;
348
+ unsigned int lastlength;
349
+ unsigned int lastoffset;
350
+ unsigned int oldoffset[4];
351
+ unsigned int lastlowoffset;
352
+ unsigned int numlowoffsetrepeats;
353
+ char start_new_table;
354
+
355
+ /* Filters */
356
+ struct rar_filters filters;
357
+
358
+ /* PPMd Variant H members */
359
+ char ppmd_valid;
360
+ char ppmd_eod;
361
+ char is_ppmd_block;
362
+ int ppmd_escape;
363
+ CPpmd7 ppmd7_context;
364
+ CPpmd7z_RangeDec range_dec;
365
+ IByteIn bytein;
366
+
367
+ /*
368
+ * String conversion object.
369
+ */
370
+ int init_default_conversion;
371
+ struct archive_string_conv *sconv_default;
372
+ struct archive_string_conv *opt_sconv;
373
+ struct archive_string_conv *sconv_utf8;
374
+ struct archive_string_conv *sconv_utf16be;
375
+
376
+ /*
377
+ * Bit stream reader.
378
+ */
379
+ struct rar_br {
380
+ #define CACHE_TYPE uint64_t
381
+ #define CACHE_BITS (8 * sizeof(CACHE_TYPE))
382
+ /* Cache buffer. */
383
+ CACHE_TYPE cache_buffer;
384
+ /* Indicates how many bits avail in cache_buffer. */
385
+ int cache_avail;
386
+ ssize_t avail_in;
387
+ const unsigned char *next_in;
388
+ } br;
389
+
390
+ /*
391
+ * Custom field to denote that this archive contains encrypted entries
392
+ */
393
+ int has_encrypted_entries;
394
+ };
395
+
396
+ static int archive_read_support_format_rar_capabilities(struct archive_read *);
397
+ static int archive_read_format_rar_has_encrypted_entries(struct archive_read *);
398
+ static int archive_read_format_rar_bid(struct archive_read *, int);
399
+ static int archive_read_format_rar_options(struct archive_read *,
400
+ const char *, const char *);
401
+ static int archive_read_format_rar_read_header(struct archive_read *,
402
+ struct archive_entry *);
403
+ static int archive_read_format_rar_read_data(struct archive_read *,
404
+ const void **, size_t *, int64_t *);
405
+ static int archive_read_format_rar_read_data_skip(struct archive_read *a);
406
+ static int64_t archive_read_format_rar_seek_data(struct archive_read *, int64_t,
407
+ int);
408
+ static int archive_read_format_rar_cleanup(struct archive_read *);
409
+
410
+ /* Support functions */
411
+ static int read_header(struct archive_read *, struct archive_entry *, char);
412
+ static time_t get_time(int);
413
+ static int read_exttime(const char *, struct rar *, const char *);
414
+ static int read_symlink_stored(struct archive_read *, struct archive_entry *,
415
+ struct archive_string_conv *);
416
+ static int read_data_stored(struct archive_read *, const void **, size_t *,
417
+ int64_t *);
418
+ static int read_data_compressed(struct archive_read *, const void **, size_t *,
419
+ int64_t *, size_t);
420
+ static int rar_br_preparation(struct archive_read *, struct rar_br *);
421
+ static int parse_codes(struct archive_read *);
422
+ static void free_codes(struct archive_read *);
423
+ static int read_next_symbol(struct archive_read *, struct huffman_code *);
424
+ static int create_code(struct archive_read *, struct huffman_code *,
425
+ unsigned char *, int, char);
426
+ static int add_value(struct archive_read *, struct huffman_code *, int, int,
427
+ int);
428
+ static int new_node(struct huffman_code *);
429
+ static int make_table(struct archive_read *, struct huffman_code *);
430
+ static int make_table_recurse(struct archive_read *, struct huffman_code *, int,
431
+ struct huffman_table_entry *, int, int);
432
+ static int expand(struct archive_read *, int64_t *);
433
+ static int copy_from_lzss_window_to_unp(struct archive_read *, const void **,
434
+ int64_t, size_t);
435
+ static const void *rar_read_ahead(struct archive_read *, size_t, ssize_t *);
436
+ static int parse_filter(struct archive_read *, const uint8_t *, uint16_t,
437
+ uint8_t);
438
+ static int run_filters(struct archive_read *);
439
+ static void clear_filters(struct rar_filters *);
440
+ static struct rar_filter *create_filter(struct rar_program_code *,
441
+ const uint8_t *, uint32_t,
442
+ uint32_t[8], size_t, uint32_t);
443
+ static void delete_filter(struct rar_filter *filter);
444
+ static struct rar_program_code *compile_program(const uint8_t *, size_t);
445
+ static void delete_program_code(struct rar_program_code *prog);
446
+ static uint32_t membr_next_rarvm_number(struct memory_bit_reader *br);
447
+ static inline uint32_t membr_bits(struct memory_bit_reader *br, int bits);
448
+ static int membr_fill(struct memory_bit_reader *br, int bits);
449
+ static int read_filter(struct archive_read *, int64_t *);
450
+ static int rar_decode_byte(struct archive_read*, uint8_t *);
451
+ static int execute_filter(struct archive_read*, struct rar_filter *,
452
+ struct rar_virtual_machine *, size_t);
453
+ static int copy_from_lzss_window(struct archive_read *, uint8_t *, int64_t, int);
454
+ static inline void vm_write_32(struct rar_virtual_machine*, size_t, uint32_t);
455
+ static inline uint32_t vm_read_32(struct rar_virtual_machine*, size_t);
456
+
457
+ /*
458
+ * Bit stream reader.
459
+ */
460
+ /* Check that the cache buffer has enough bits. */
461
+ #define rar_br_has(br, n) ((br)->cache_avail >= n)
462
+ /* Get compressed data by bit. */
463
+ #define rar_br_bits(br, n) \
464
+ (((uint32_t)((br)->cache_buffer >> \
465
+ ((br)->cache_avail - (n)))) & cache_masks[n])
466
+ #define rar_br_bits_forced(br, n) \
467
+ (((uint32_t)((br)->cache_buffer << \
468
+ ((n) - (br)->cache_avail))) & cache_masks[n])
469
+ /* Read ahead to make sure the cache buffer has enough compressed data we
470
+ * will use.
471
+ * True : completed, there is enough data in the cache buffer.
472
+ * False : there is no data in the stream. */
473
+ #define rar_br_read_ahead(a, br, n) \
474
+ ((rar_br_has(br, (n)) || rar_br_fillup(a, br)) || rar_br_has(br, (n)))
475
+ /* Notify how many bits we consumed. */
476
+ #define rar_br_consume(br, n) ((br)->cache_avail -= (n))
477
+ #define rar_br_consume_unaligned_bits(br) ((br)->cache_avail &= ~7)
478
+
479
+ static const uint32_t cache_masks[] = {
480
+ 0x00000000, 0x00000001, 0x00000003, 0x00000007,
481
+ 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
482
+ 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
483
+ 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
484
+ 0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,
485
+ 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
486
+ 0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,
487
+ 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
488
+ 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
489
+ };
490
+
491
+ /*
492
+ * Shift away used bits in the cache data and fill it up with following bits.
493
+ * Call this when cache buffer does not have enough bits you need.
494
+ *
495
+ * Returns 1 if the cache buffer is full.
496
+ * Returns 0 if the cache buffer is not full; input buffer is empty.
497
+ */
498
+ static int
499
+ rar_br_fillup(struct archive_read *a, struct rar_br *br)
500
+ {
501
+ struct rar *rar = a->format->data;
502
+ int n = CACHE_BITS - br->cache_avail;
503
+
504
+ for (;;) {
505
+ switch (n >> 3) {
506
+ case 8:
507
+ if (br->avail_in >= 8) {
508
+ br->cache_buffer =
509
+ ((uint64_t)br->next_in[0]) << 56 |
510
+ ((uint64_t)br->next_in[1]) << 48 |
511
+ ((uint64_t)br->next_in[2]) << 40 |
512
+ ((uint64_t)br->next_in[3]) << 32 |
513
+ ((uint32_t)br->next_in[4]) << 24 |
514
+ ((uint32_t)br->next_in[5]) << 16 |
515
+ ((uint32_t)br->next_in[6]) << 8 |
516
+ (uint32_t)br->next_in[7];
517
+ br->next_in += 8;
518
+ br->avail_in -= 8;
519
+ br->cache_avail += 8 * 8;
520
+ rar->bytes_unconsumed += 8;
521
+ rar->bytes_remaining -= 8;
522
+ return (1);
523
+ }
524
+ break;
525
+ case 7:
526
+ if (br->avail_in >= 7) {
527
+ br->cache_buffer =
528
+ (br->cache_buffer << 56) |
529
+ ((uint64_t)br->next_in[0]) << 48 |
530
+ ((uint64_t)br->next_in[1]) << 40 |
531
+ ((uint64_t)br->next_in[2]) << 32 |
532
+ ((uint32_t)br->next_in[3]) << 24 |
533
+ ((uint32_t)br->next_in[4]) << 16 |
534
+ ((uint32_t)br->next_in[5]) << 8 |
535
+ (uint32_t)br->next_in[6];
536
+ br->next_in += 7;
537
+ br->avail_in -= 7;
538
+ br->cache_avail += 7 * 8;
539
+ rar->bytes_unconsumed += 7;
540
+ rar->bytes_remaining -= 7;
541
+ return (1);
542
+ }
543
+ break;
544
+ case 6:
545
+ if (br->avail_in >= 6) {
546
+ br->cache_buffer =
547
+ (br->cache_buffer << 48) |
548
+ ((uint64_t)br->next_in[0]) << 40 |
549
+ ((uint64_t)br->next_in[1]) << 32 |
550
+ ((uint32_t)br->next_in[2]) << 24 |
551
+ ((uint32_t)br->next_in[3]) << 16 |
552
+ ((uint32_t)br->next_in[4]) << 8 |
553
+ (uint32_t)br->next_in[5];
554
+ br->next_in += 6;
555
+ br->avail_in -= 6;
556
+ br->cache_avail += 6 * 8;
557
+ rar->bytes_unconsumed += 6;
558
+ rar->bytes_remaining -= 6;
559
+ return (1);
560
+ }
561
+ break;
562
+ case 0:
563
+ /* We have enough compressed data in
564
+ * the cache buffer.*/
565
+ return (1);
566
+ default:
567
+ break;
568
+ }
569
+ if (br->avail_in <= 0) {
570
+
571
+ if (rar->bytes_unconsumed > 0) {
572
+ /* Consume as much as the decompressor
573
+ * actually used. */
574
+ __archive_read_consume(a, rar->bytes_unconsumed);
575
+ rar->bytes_unconsumed = 0;
576
+ }
577
+ br->next_in = rar_read_ahead(a, 1, &(br->avail_in));
578
+ if (br->next_in == NULL)
579
+ return (0);
580
+ if (br->avail_in == 0)
581
+ return (0);
582
+ }
583
+ br->cache_buffer =
584
+ (br->cache_buffer << 8) | *br->next_in++;
585
+ br->avail_in--;
586
+ br->cache_avail += 8;
587
+ n -= 8;
588
+ rar->bytes_unconsumed++;
589
+ rar->bytes_remaining--;
590
+ }
591
+ }
592
+
593
+ static int
594
+ rar_br_preparation(struct archive_read *a, struct rar_br *br)
595
+ {
596
+ struct rar *rar = a->format->data;
597
+
598
+ if (rar->bytes_remaining > 0) {
599
+ br->next_in = rar_read_ahead(a, 1, &(br->avail_in));
600
+ if (br->next_in == NULL) {
601
+ archive_set_error(&a->archive,
602
+ ARCHIVE_ERRNO_FILE_FORMAT,
603
+ "Truncated RAR file data");
604
+ return (ARCHIVE_FATAL);
605
+ }
606
+ if (br->cache_avail == 0)
607
+ (void)rar_br_fillup(a, br);
608
+ }
609
+ return (ARCHIVE_OK);
610
+ }
611
+
612
+ /* Find last bit set */
613
+ static inline int
614
+ rar_fls(unsigned int word)
615
+ {
616
+ word |= (word >> 1);
617
+ word |= (word >> 2);
618
+ word |= (word >> 4);
619
+ word |= (word >> 8);
620
+ word |= (word >> 16);
621
+ return word - (word >> 1);
622
+ }
623
+
624
+ /* LZSS functions */
625
+ static inline int64_t
626
+ lzss_position(struct lzss *lzss)
627
+ {
628
+ return lzss->position;
629
+ }
630
+
631
+ static inline int
632
+ lzss_mask(struct lzss *lzss)
633
+ {
634
+ return lzss->mask;
635
+ }
636
+
637
+ static inline int
638
+ lzss_size(struct lzss *lzss)
639
+ {
640
+ return lzss->mask + 1;
641
+ }
642
+
643
+ static inline int
644
+ lzss_offset_for_position(struct lzss *lzss, int64_t pos)
645
+ {
646
+ return (int)(pos & lzss->mask);
647
+ }
648
+
649
+ static inline unsigned char *
650
+ lzss_pointer_for_position(struct lzss *lzss, int64_t pos)
651
+ {
652
+ return &lzss->window[lzss_offset_for_position(lzss, pos)];
653
+ }
654
+
655
+ static inline int
656
+ lzss_current_offset(struct lzss *lzss)
657
+ {
658
+ return lzss_offset_for_position(lzss, lzss->position);
659
+ }
660
+
661
+ static inline uint8_t *
662
+ lzss_current_pointer(struct lzss *lzss)
663
+ {
664
+ return lzss_pointer_for_position(lzss, lzss->position);
665
+ }
666
+
667
+ static inline void
668
+ lzss_emit_literal(struct rar *rar, uint8_t literal)
669
+ {
670
+ *lzss_current_pointer(&rar->lzss) = literal;
671
+ rar->lzss.position++;
672
+ }
673
+
674
+ static inline void
675
+ lzss_emit_match(struct rar *rar, int offset, int length)
676
+ {
677
+ int dstoffs = lzss_current_offset(&rar->lzss);
678
+ int srcoffs = (dstoffs - offset) & lzss_mask(&rar->lzss);
679
+ int l, li, remaining;
680
+ unsigned char *d, *s;
681
+
682
+ remaining = length;
683
+ while (remaining > 0) {
684
+ l = remaining;
685
+ if (dstoffs > srcoffs) {
686
+ if (l > lzss_size(&rar->lzss) - dstoffs)
687
+ l = lzss_size(&rar->lzss) - dstoffs;
688
+ } else {
689
+ if (l > lzss_size(&rar->lzss) - srcoffs)
690
+ l = lzss_size(&rar->lzss) - srcoffs;
691
+ }
692
+ d = &(rar->lzss.window[dstoffs]);
693
+ s = &(rar->lzss.window[srcoffs]);
694
+ if ((dstoffs + l < srcoffs) || (srcoffs + l < dstoffs))
695
+ memcpy(d, s, l);
696
+ else {
697
+ for (li = 0; li < l; li++)
698
+ d[li] = s[li];
699
+ }
700
+ remaining -= l;
701
+ dstoffs = (dstoffs + l) & lzss_mask(&(rar->lzss));
702
+ srcoffs = (srcoffs + l) & lzss_mask(&(rar->lzss));
703
+ }
704
+ rar->lzss.position += length;
705
+ }
706
+
707
+ static Byte
708
+ ppmd_read(void *p)
709
+ {
710
+ struct archive_read *a = ((IByteIn*)p)->a;
711
+ struct rar *rar = a->format->data;
712
+ struct rar_br *br = &(rar->br);
713
+ Byte b;
714
+ if (!rar_br_read_ahead(a, br, 8))
715
+ {
716
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
717
+ "Truncated RAR file data");
718
+ rar->valid = 0;
719
+ return 0;
720
+ }
721
+ b = rar_br_bits(br, 8);
722
+ rar_br_consume(br, 8);
723
+ return b;
724
+ }
725
+
726
+ int
727
+ archive_read_support_format_rar(struct archive *_a)
728
+ {
729
+ struct archive_read *a = (struct archive_read *)_a;
730
+ struct rar *rar;
731
+ int r;
732
+
733
+ archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
734
+ "archive_read_support_format_rar");
735
+
736
+ rar = calloc(1, sizeof(*rar));
737
+ if (rar == NULL)
738
+ {
739
+ archive_set_error(&a->archive, ENOMEM, "Can't allocate rar data");
740
+ return (ARCHIVE_FATAL);
741
+ }
742
+
743
+ /*
744
+ * Until enough data has been read, we cannot tell about
745
+ * any encrypted entries yet.
746
+ */
747
+ rar->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
748
+
749
+ r = __archive_read_register_format(a,
750
+ rar,
751
+ "rar",
752
+ archive_read_format_rar_bid,
753
+ archive_read_format_rar_options,
754
+ archive_read_format_rar_read_header,
755
+ archive_read_format_rar_read_data,
756
+ archive_read_format_rar_read_data_skip,
757
+ archive_read_format_rar_seek_data,
758
+ archive_read_format_rar_cleanup,
759
+ archive_read_support_format_rar_capabilities,
760
+ archive_read_format_rar_has_encrypted_entries);
761
+
762
+ if (r != ARCHIVE_OK)
763
+ free(rar);
764
+ return (r);
765
+ }
766
+
767
+ static int
768
+ archive_read_support_format_rar_capabilities(struct archive_read * a)
769
+ {
770
+ (void)a; /* UNUSED */
771
+ return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA
772
+ | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
773
+ }
774
+
775
+ static int
776
+ archive_read_format_rar_has_encrypted_entries(struct archive_read *_a)
777
+ {
778
+ if (_a && _a->format) {
779
+ struct rar *rar = _a->format->data;
780
+ if (rar) {
781
+ return rar->has_encrypted_entries;
782
+ }
783
+ }
784
+ return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
785
+ }
786
+
787
+
788
+ static int
789
+ archive_read_format_rar_bid(struct archive_read *a, int best_bid)
790
+ {
791
+ const char *p;
792
+
793
+ /* If there's already a bid > 30, we'll never win. */
794
+ if (best_bid > 30)
795
+ return (-1);
796
+
797
+ if ((p = __archive_read_ahead(a, 7, NULL)) == NULL)
798
+ return (-1);
799
+
800
+ if (memcmp(p, RAR_SIGNATURE, 7) == 0)
801
+ return (30);
802
+
803
+ if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
804
+ /* This is a PE file */
805
+ ssize_t offset = 0x10000;
806
+ ssize_t window = 4096;
807
+ ssize_t bytes_avail;
808
+ while (offset + window <= (1024 * 128)) {
809
+ const char *buff = __archive_read_ahead(a, offset + window, &bytes_avail);
810
+ if (buff == NULL) {
811
+ /* Remaining bytes are less than window. */
812
+ window >>= 1;
813
+ if (window < 0x40)
814
+ return (0);
815
+ continue;
816
+ }
817
+ p = buff + offset;
818
+ while (p + 7 < buff + bytes_avail) {
819
+ if (memcmp(p, RAR_SIGNATURE, 7) == 0)
820
+ return (30);
821
+ p += 0x10;
822
+ }
823
+ offset = p - buff;
824
+ }
825
+ }
826
+ return (0);
827
+ }
828
+
829
+ static int
830
+ skip_sfx(struct archive_read *a)
831
+ {
832
+ const void *h;
833
+ const char *p, *q;
834
+ size_t skip, total;
835
+ ssize_t bytes, window;
836
+
837
+ total = 0;
838
+ window = 4096;
839
+ while (total + window <= (1024 * 128)) {
840
+ h = __archive_read_ahead(a, window, &bytes);
841
+ if (h == NULL) {
842
+ /* Remaining bytes are less than window. */
843
+ window >>= 1;
844
+ if (window < 0x40)
845
+ goto fatal;
846
+ continue;
847
+ }
848
+ if (bytes < 0x40)
849
+ goto fatal;
850
+ p = h;
851
+ q = p + bytes;
852
+
853
+ /*
854
+ * Scan ahead until we find something that looks
855
+ * like the RAR header.
856
+ */
857
+ while (p + 7 < q) {
858
+ if (memcmp(p, RAR_SIGNATURE, 7) == 0) {
859
+ skip = p - (const char *)h;
860
+ __archive_read_consume(a, skip);
861
+ return (ARCHIVE_OK);
862
+ }
863
+ p += 0x10;
864
+ }
865
+ skip = p - (const char *)h;
866
+ __archive_read_consume(a, skip);
867
+ total += skip;
868
+ }
869
+ fatal:
870
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
871
+ "Couldn't find out RAR header");
872
+ return (ARCHIVE_FATAL);
873
+ }
874
+
875
+ static int
876
+ archive_read_format_rar_options(struct archive_read *a,
877
+ const char *key, const char *val)
878
+ {
879
+ struct rar *rar = a->format->data;
880
+ int ret = ARCHIVE_FAILED;
881
+
882
+ if (strcmp(key, "hdrcharset") == 0) {
883
+ if (val == NULL || val[0] == 0)
884
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
885
+ "rar: hdrcharset option needs a character-set name");
886
+ else {
887
+ rar->opt_sconv =
888
+ archive_string_conversion_from_charset(
889
+ &a->archive, val, 0);
890
+ if (rar->opt_sconv != NULL)
891
+ ret = ARCHIVE_OK;
892
+ else
893
+ ret = ARCHIVE_FATAL;
894
+ }
895
+ return (ret);
896
+ }
897
+
898
+ /* Note: The "warn" return is just to inform the options
899
+ * supervisor that we didn't handle it. It will generate
900
+ * a suitable error if no one used this option. */
901
+ return (ARCHIVE_WARN);
902
+ }
903
+
904
+ static int
905
+ archive_read_format_rar_read_header(struct archive_read *a,
906
+ struct archive_entry *entry)
907
+ {
908
+ struct rar *rar = a->format->data;
909
+ const void *h;
910
+ const char *p;
911
+ int64_t skip;
912
+ char head_type;
913
+ int ret;
914
+ unsigned flags;
915
+ unsigned long crc32_expected;
916
+
917
+ a->archive.archive_format = ARCHIVE_FORMAT_RAR;
918
+ if (a->archive.archive_format_name == NULL)
919
+ a->archive.archive_format_name = "RAR";
920
+
921
+ /*
922
+ * It should be sufficient to call archive_read_next_header() for
923
+ * a reader to determine if an entry is encrypted or not. If the
924
+ * encryption of an entry is only detectable when calling
925
+ * archive_read_data(), so be it. We'll do the same check there
926
+ * as well.
927
+ */
928
+ if (rar->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
929
+ rar->has_encrypted_entries = 0;
930
+ }
931
+
932
+ /* RAR files can be generated without EOF headers, so return ARCHIVE_EOF if
933
+ * this fails.
934
+ */
935
+ if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
936
+ return (ARCHIVE_EOF);
937
+
938
+ p = h;
939
+ if (rar->found_first_header == 0 &&
940
+ ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0)) {
941
+ /* This is an executable ? Must be self-extracting... */
942
+ ret = skip_sfx(a);
943
+ if (ret < ARCHIVE_WARN)
944
+ return (ret);
945
+ }
946
+ rar->found_first_header = 1;
947
+
948
+ while (1)
949
+ {
950
+ unsigned long crc32_val;
951
+
952
+ if ((h = __archive_read_ahead(a, 7, NULL)) == NULL) {
953
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
954
+ "Failed to read next header");
955
+ return (ARCHIVE_FATAL);
956
+ }
957
+ p = h;
958
+
959
+ head_type = p[2];
960
+ switch(head_type)
961
+ {
962
+ case MARK_HEAD:
963
+ if (memcmp(p, RAR_SIGNATURE, 7) != 0) {
964
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
965
+ "Invalid marker header");
966
+ return (ARCHIVE_FATAL);
967
+ }
968
+ __archive_read_consume(a, 7);
969
+ break;
970
+
971
+ case MAIN_HEAD:
972
+ rar->main_flags = archive_le16dec(p + 3);
973
+ skip = archive_le16dec(p + 5);
974
+ if ((size_t)skip < 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2)) {
975
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
976
+ "Invalid header size");
977
+ return (ARCHIVE_FATAL);
978
+ }
979
+ if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
980
+ return (ARCHIVE_FATAL);
981
+ p = h;
982
+ memcpy(rar->reserved1, p + 7, sizeof(rar->reserved1));
983
+ memcpy(rar->reserved2, p + 7 + sizeof(rar->reserved1),
984
+ sizeof(rar->reserved2));
985
+ if (rar->main_flags & MHD_ENCRYPTVER) {
986
+ if ((size_t)skip <
987
+ 7 + sizeof(rar->reserved1) + sizeof(rar->reserved2) + 1) {
988
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
989
+ "Invalid header size");
990
+ return (ARCHIVE_FATAL);
991
+ }
992
+ rar->encryptver = *(p + 7 + sizeof(rar->reserved1) +
993
+ sizeof(rar->reserved2));
994
+ }
995
+
996
+ /* Main header is password encrypted, so we cannot read any
997
+ file names or any other info about files from the header. */
998
+ if (rar->main_flags & MHD_PASSWORD)
999
+ {
1000
+ archive_entry_set_is_metadata_encrypted(entry, 1);
1001
+ archive_entry_set_is_data_encrypted(entry, 1);
1002
+ rar->has_encrypted_entries = 1;
1003
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1004
+ "RAR encryption support unavailable");
1005
+ return (ARCHIVE_FATAL);
1006
+ }
1007
+
1008
+ crc32_val = crc32(0, (const unsigned char *)p + 2, (unsigned)skip - 2);
1009
+ if ((crc32_val & 0xffff) != archive_le16dec(p)) {
1010
+ #ifndef DONT_FAIL_ON_CRC_ERROR
1011
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1012
+ "Header CRC error");
1013
+ return (ARCHIVE_FATAL);
1014
+ #endif
1015
+ }
1016
+ __archive_read_consume(a, skip);
1017
+ break;
1018
+
1019
+ case FILE_HEAD:
1020
+ return read_header(a, entry, head_type);
1021
+
1022
+ case COMM_HEAD:
1023
+ case AV_HEAD:
1024
+ case SUB_HEAD:
1025
+ case PROTECT_HEAD:
1026
+ case SIGN_HEAD:
1027
+ case ENDARC_HEAD:
1028
+ flags = archive_le16dec(p + 3);
1029
+ skip = archive_le16dec(p + 5);
1030
+ if (skip < 7) {
1031
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1032
+ "Invalid header size too small");
1033
+ return (ARCHIVE_FATAL);
1034
+ }
1035
+ if (flags & HD_ADD_SIZE_PRESENT)
1036
+ {
1037
+ if (skip < 7 + 4) {
1038
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1039
+ "Invalid header size too small");
1040
+ return (ARCHIVE_FATAL);
1041
+ }
1042
+ if ((h = __archive_read_ahead(a, skip, NULL)) == NULL)
1043
+ return (ARCHIVE_FATAL);
1044
+ p = h;
1045
+ skip += archive_le32dec(p + 7);
1046
+ }
1047
+
1048
+ /* Skip over the 2-byte CRC at the beginning of the header. */
1049
+ crc32_expected = archive_le16dec(p);
1050
+ __archive_read_consume(a, 2);
1051
+ skip -= 2;
1052
+
1053
+ /* Skim the entire header and compute the CRC. */
1054
+ crc32_val = 0;
1055
+ while (skip > 0) {
1056
+ unsigned to_read;
1057
+ if (skip > 32 * 1024)
1058
+ to_read = 32 * 1024;
1059
+ else
1060
+ to_read = (unsigned)skip;
1061
+ if ((h = __archive_read_ahead(a, to_read, NULL)) == NULL) {
1062
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1063
+ "Bad RAR file");
1064
+ return (ARCHIVE_FATAL);
1065
+ }
1066
+ p = h;
1067
+ crc32_val = crc32(crc32_val, (const unsigned char *)p, to_read);
1068
+ __archive_read_consume(a, to_read);
1069
+ skip -= to_read;
1070
+ }
1071
+ if ((crc32_val & 0xffff) != crc32_expected) {
1072
+ #ifndef DONT_FAIL_ON_CRC_ERROR
1073
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1074
+ "Header CRC error");
1075
+ return (ARCHIVE_FATAL);
1076
+ #endif
1077
+ }
1078
+ if (head_type == ENDARC_HEAD)
1079
+ return (ARCHIVE_EOF);
1080
+ break;
1081
+
1082
+ case NEWSUB_HEAD:
1083
+ if ((ret = read_header(a, entry, head_type)) < ARCHIVE_WARN)
1084
+ return ret;
1085
+ break;
1086
+
1087
+ default:
1088
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1089
+ "Bad RAR file");
1090
+ return (ARCHIVE_FATAL);
1091
+ }
1092
+ }
1093
+ }
1094
+
1095
+ static int
1096
+ archive_read_format_rar_read_data(struct archive_read *a, const void **buff,
1097
+ size_t *size, int64_t *offset)
1098
+ {
1099
+ struct rar *rar = a->format->data;
1100
+ int ret;
1101
+
1102
+ if (rar->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
1103
+ rar->has_encrypted_entries = 0;
1104
+ }
1105
+
1106
+ if (rar->bytes_unconsumed > 0) {
1107
+ /* Consume as much as the decompressor actually used. */
1108
+ __archive_read_consume(a, rar->bytes_unconsumed);
1109
+ rar->bytes_unconsumed = 0;
1110
+ }
1111
+
1112
+ *buff = NULL;
1113
+ if (rar->entry_eof || rar->offset_seek >= rar->unp_size) {
1114
+ *size = 0;
1115
+ *offset = rar->offset;
1116
+ return (ARCHIVE_EOF);
1117
+ }
1118
+
1119
+ switch (rar->compression_method)
1120
+ {
1121
+ case COMPRESS_METHOD_STORE:
1122
+ ret = read_data_stored(a, buff, size, offset);
1123
+ break;
1124
+
1125
+ case COMPRESS_METHOD_FASTEST:
1126
+ case COMPRESS_METHOD_FAST:
1127
+ case COMPRESS_METHOD_NORMAL:
1128
+ case COMPRESS_METHOD_GOOD:
1129
+ case COMPRESS_METHOD_BEST:
1130
+ ret = read_data_compressed(a, buff, size, offset, 0);
1131
+ if (ret != ARCHIVE_OK && ret != ARCHIVE_WARN) {
1132
+ __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
1133
+ rar->start_new_table = 1;
1134
+ rar->ppmd_valid = 0;
1135
+ }
1136
+ break;
1137
+
1138
+ default:
1139
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1140
+ "Unsupported compression method for RAR file");
1141
+ ret = ARCHIVE_FAILED;
1142
+ break;
1143
+ }
1144
+ return (ret);
1145
+ }
1146
+
1147
+ static int
1148
+ archive_read_format_rar_read_data_skip(struct archive_read *a)
1149
+ {
1150
+ struct rar *rar = a->format->data;
1151
+ int64_t bytes_skipped;
1152
+ int ret;
1153
+
1154
+ if (rar->bytes_unconsumed > 0) {
1155
+ /* Consume as much as the decompressor actually used. */
1156
+ __archive_read_consume(a, rar->bytes_unconsumed);
1157
+ rar->bytes_unconsumed = 0;
1158
+ }
1159
+
1160
+ if (rar->bytes_remaining > 0) {
1161
+ bytes_skipped = __archive_read_consume(a, rar->bytes_remaining);
1162
+ if (bytes_skipped < 0)
1163
+ return (ARCHIVE_FATAL);
1164
+ }
1165
+
1166
+ /* Compressed data to skip must be read from each header in a multivolume
1167
+ * archive.
1168
+ */
1169
+ if (rar->main_flags & MHD_VOLUME && rar->file_flags & FHD_SPLIT_AFTER)
1170
+ {
1171
+ ret = archive_read_format_rar_read_header(a, a->entry);
1172
+ if (ret == (ARCHIVE_EOF))
1173
+ ret = archive_read_format_rar_read_header(a, a->entry);
1174
+ if (ret != (ARCHIVE_OK))
1175
+ return ret;
1176
+ return archive_read_format_rar_read_data_skip(a);
1177
+ }
1178
+
1179
+ return (ARCHIVE_OK);
1180
+ }
1181
+
1182
+ static int64_t
1183
+ archive_read_format_rar_seek_data(struct archive_read *a, int64_t offset,
1184
+ int whence)
1185
+ {
1186
+ struct rar *rar = a->format->data;
1187
+ int64_t client_offset, ret;
1188
+ size_t i;
1189
+
1190
+ if (rar->compression_method == COMPRESS_METHOD_STORE)
1191
+ {
1192
+ /* Modify the offset for use with SEEK_SET */
1193
+ switch (whence)
1194
+ {
1195
+ case SEEK_CUR:
1196
+ client_offset = rar->offset_seek;
1197
+ break;
1198
+ case SEEK_END:
1199
+ client_offset = rar->unp_size;
1200
+ break;
1201
+ case SEEK_SET:
1202
+ default:
1203
+ client_offset = 0;
1204
+ }
1205
+ client_offset += offset;
1206
+ if (client_offset < 0)
1207
+ {
1208
+ /* Can't seek past beginning of data block */
1209
+ return -1;
1210
+ }
1211
+ else if (client_offset > rar->unp_size)
1212
+ {
1213
+ /*
1214
+ * Set the returned offset but only seek to the end of
1215
+ * the data block.
1216
+ */
1217
+ rar->offset_seek = client_offset;
1218
+ client_offset = rar->unp_size;
1219
+ }
1220
+
1221
+ client_offset += rar->dbo[0].start_offset;
1222
+ i = 0;
1223
+ while (i < rar->cursor)
1224
+ {
1225
+ i++;
1226
+ client_offset += rar->dbo[i].start_offset - rar->dbo[i-1].end_offset;
1227
+ }
1228
+ if (rar->main_flags & MHD_VOLUME)
1229
+ {
1230
+ /* Find the appropriate offset among the multivolume archive */
1231
+ while (1)
1232
+ {
1233
+ if (client_offset < rar->dbo[rar->cursor].start_offset &&
1234
+ rar->file_flags & FHD_SPLIT_BEFORE)
1235
+ {
1236
+ /* Search backwards for the correct data block */
1237
+ if (rar->cursor == 0)
1238
+ {
1239
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1240
+ "Attempt to seek past beginning of RAR data block");
1241
+ return (ARCHIVE_FAILED);
1242
+ }
1243
+ rar->cursor--;
1244
+ client_offset -= rar->dbo[rar->cursor+1].start_offset -
1245
+ rar->dbo[rar->cursor].end_offset;
1246
+ if (client_offset < rar->dbo[rar->cursor].start_offset)
1247
+ continue;
1248
+ ret = __archive_read_seek(a, rar->dbo[rar->cursor].start_offset -
1249
+ rar->dbo[rar->cursor].header_size, SEEK_SET);
1250
+ if (ret < (ARCHIVE_OK))
1251
+ return ret;
1252
+ ret = archive_read_format_rar_read_header(a, a->entry);
1253
+ if (ret != (ARCHIVE_OK))
1254
+ {
1255
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1256
+ "Error during seek of RAR file");
1257
+ return (ARCHIVE_FAILED);
1258
+ }
1259
+ rar->cursor--;
1260
+ break;
1261
+ }
1262
+ else if (client_offset > rar->dbo[rar->cursor].end_offset &&
1263
+ rar->file_flags & FHD_SPLIT_AFTER)
1264
+ {
1265
+ /* Search forward for the correct data block */
1266
+ rar->cursor++;
1267
+ if (rar->cursor < rar->nodes &&
1268
+ client_offset > rar->dbo[rar->cursor].end_offset)
1269
+ {
1270
+ client_offset += rar->dbo[rar->cursor].start_offset -
1271
+ rar->dbo[rar->cursor-1].end_offset;
1272
+ continue;
1273
+ }
1274
+ rar->cursor--;
1275
+ ret = __archive_read_seek(a, rar->dbo[rar->cursor].end_offset,
1276
+ SEEK_SET);
1277
+ if (ret < (ARCHIVE_OK))
1278
+ return ret;
1279
+ ret = archive_read_format_rar_read_header(a, a->entry);
1280
+ if (ret == (ARCHIVE_EOF))
1281
+ {
1282
+ rar->has_endarc_header = 1;
1283
+ ret = archive_read_format_rar_read_header(a, a->entry);
1284
+ }
1285
+ if (ret != (ARCHIVE_OK))
1286
+ {
1287
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1288
+ "Error during seek of RAR file");
1289
+ return (ARCHIVE_FAILED);
1290
+ }
1291
+ if (rar->cursor == 0)
1292
+ {
1293
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1294
+ "Invalid RAR seek cursor state");
1295
+ return (ARCHIVE_FAILED);
1296
+ }
1297
+ client_offset += rar->dbo[rar->cursor].start_offset -
1298
+ rar->dbo[rar->cursor-1].end_offset;
1299
+ continue;
1300
+ }
1301
+ break;
1302
+ }
1303
+ }
1304
+
1305
+ ret = __archive_read_seek(a, client_offset, SEEK_SET);
1306
+ if (ret < (ARCHIVE_OK))
1307
+ return ret;
1308
+ rar->bytes_remaining = rar->dbo[rar->cursor].end_offset - ret;
1309
+ i = rar->cursor;
1310
+ while (i > 0)
1311
+ {
1312
+ i--;
1313
+ ret -= rar->dbo[i+1].start_offset - rar->dbo[i].end_offset;
1314
+ }
1315
+ ret -= rar->dbo[0].start_offset;
1316
+
1317
+ /* Always restart reading the file after a seek */
1318
+ __archive_reset_read_data(&a->archive);
1319
+
1320
+ rar->bytes_unconsumed = 0;
1321
+ rar->offset = 0;
1322
+
1323
+ /*
1324
+ * If a seek past the end of file was requested, return the requested
1325
+ * offset.
1326
+ */
1327
+ if (ret == rar->unp_size && rar->offset_seek > rar->unp_size)
1328
+ return rar->offset_seek;
1329
+
1330
+ /* Return the new offset */
1331
+ rar->offset_seek = ret;
1332
+ return rar->offset_seek;
1333
+ }
1334
+ else
1335
+ {
1336
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1337
+ "Seeking of compressed RAR files is unsupported");
1338
+ }
1339
+ return (ARCHIVE_FAILED);
1340
+ }
1341
+
1342
+ static int
1343
+ archive_read_format_rar_cleanup(struct archive_read *a)
1344
+ {
1345
+ struct rar *rar = a->format->data;
1346
+
1347
+ free_codes(a);
1348
+ clear_filters(&rar->filters);
1349
+ free(rar->filename);
1350
+ free(rar->filename_save);
1351
+ free(rar->dbo);
1352
+ free(rar->unp_buffer);
1353
+ free(rar->lzss.window);
1354
+ __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
1355
+ free(rar);
1356
+ a->format->data = NULL;
1357
+ return (ARCHIVE_OK);
1358
+ }
1359
+
1360
+ static int
1361
+ read_header(struct archive_read *a, struct archive_entry *entry,
1362
+ char head_type)
1363
+ {
1364
+ struct rar *rar = a->format->data;
1365
+ const void *h;
1366
+ const char *p, *endp;
1367
+ struct rar_header rar_header;
1368
+ struct rar_file_header file_header;
1369
+ int64_t header_size;
1370
+ unsigned filename_size, end;
1371
+ char *filename;
1372
+ char *strp;
1373
+ char packed_size[8];
1374
+ char unp_size[8];
1375
+ int ttime;
1376
+ struct archive_string_conv *sconv, *fn_sconv;
1377
+ uint32_t crc32_computed, crc32_read;
1378
+ int ret = (ARCHIVE_OK), ret2;
1379
+ char *newptr;
1380
+ size_t newsize;
1381
+
1382
+ /* Setup a string conversion object for non-rar-unicode filenames. */
1383
+ sconv = rar->opt_sconv;
1384
+ if (sconv == NULL) {
1385
+ if (!rar->init_default_conversion) {
1386
+ rar->sconv_default =
1387
+ archive_string_default_conversion_for_read(
1388
+ &(a->archive));
1389
+ rar->init_default_conversion = 1;
1390
+ }
1391
+ sconv = rar->sconv_default;
1392
+ }
1393
+
1394
+
1395
+ if ((h = __archive_read_ahead(a, 7, NULL)) == NULL)
1396
+ return (ARCHIVE_FATAL);
1397
+ p = h;
1398
+ memcpy(&rar_header, p, sizeof(rar_header));
1399
+ rar->file_flags = archive_le16dec(rar_header.flags);
1400
+ header_size = archive_le16dec(rar_header.size);
1401
+ if (header_size < (int64_t)sizeof(file_header) + 7) {
1402
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1403
+ "Invalid header size");
1404
+ return (ARCHIVE_FATAL);
1405
+ }
1406
+ crc32_computed = crc32(0, (const unsigned char *)p + 2, 7 - 2);
1407
+ __archive_read_consume(a, 7);
1408
+
1409
+ if (!(rar->file_flags & FHD_SOLID))
1410
+ {
1411
+ rar->compression_method = 0;
1412
+ rar->packed_size = 0;
1413
+ rar->unp_size = 0;
1414
+ rar->mtime = 0;
1415
+ rar->ctime = 0;
1416
+ rar->atime = 0;
1417
+ rar->arctime = 0;
1418
+ rar->mode = 0;
1419
+ memset(&rar->salt, 0, sizeof(rar->salt));
1420
+ rar->atime = 0;
1421
+ rar->ansec = 0;
1422
+ rar->ctime = 0;
1423
+ rar->cnsec = 0;
1424
+ rar->mtime = 0;
1425
+ rar->mnsec = 0;
1426
+ rar->arctime = 0;
1427
+ rar->arcnsec = 0;
1428
+ }
1429
+ else
1430
+ {
1431
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1432
+ "RAR solid archive support unavailable");
1433
+ return (ARCHIVE_FATAL);
1434
+ }
1435
+
1436
+ if ((h = __archive_read_ahead(a, (size_t)header_size - 7, NULL)) == NULL)
1437
+ {
1438
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1439
+ "Failed to read full header content");
1440
+ return (ARCHIVE_FATAL);
1441
+ }
1442
+
1443
+ /* File Header CRC check. */
1444
+ crc32_computed = crc32(crc32_computed, h, (unsigned)(header_size - 7));
1445
+ crc32_read = archive_le16dec(rar_header.crc);
1446
+ if ((crc32_computed & 0xffff) != crc32_read) {
1447
+ #ifndef DONT_FAIL_ON_CRC_ERROR
1448
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1449
+ "Header CRC error");
1450
+ return (ARCHIVE_FATAL);
1451
+ #endif
1452
+ }
1453
+ /* If no CRC error, go on parsing File Header. */
1454
+ p = h;
1455
+ endp = p + header_size - 7;
1456
+ memcpy(&file_header, p, sizeof(file_header));
1457
+ p += sizeof(file_header);
1458
+
1459
+ rar->compression_method = file_header.method;
1460
+
1461
+ ttime = archive_le32dec(file_header.file_time);
1462
+ rar->mtime = get_time(ttime);
1463
+
1464
+ rar->file_crc = archive_le32dec(file_header.file_crc);
1465
+
1466
+ if (rar->file_flags & FHD_PASSWORD)
1467
+ {
1468
+ archive_entry_set_is_data_encrypted(entry, 1);
1469
+ rar->has_encrypted_entries = 1;
1470
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1471
+ "RAR encryption support unavailable");
1472
+ /* Since it is only the data part itself that is encrypted we can at least
1473
+ extract information about the currently processed entry and don't need
1474
+ to return ARCHIVE_FATAL here. */
1475
+ /*return (ARCHIVE_FATAL);*/
1476
+ }
1477
+
1478
+ if (rar->file_flags & FHD_LARGE)
1479
+ {
1480
+ if (p + 8 > endp) {
1481
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1482
+ "Invalid header size");
1483
+ return (ARCHIVE_FATAL);
1484
+ }
1485
+ memcpy(packed_size, file_header.pack_size, 4);
1486
+ memcpy(packed_size + 4, p, 4); /* High pack size */
1487
+ p += 4;
1488
+ memcpy(unp_size, file_header.unp_size, 4);
1489
+ memcpy(unp_size + 4, p, 4); /* High unpack size */
1490
+ p += 4;
1491
+ rar->packed_size = archive_le64dec(&packed_size);
1492
+ rar->unp_size = archive_le64dec(&unp_size);
1493
+ }
1494
+ else
1495
+ {
1496
+ rar->packed_size = archive_le32dec(file_header.pack_size);
1497
+ rar->unp_size = archive_le32dec(file_header.unp_size);
1498
+ }
1499
+
1500
+ if (rar->packed_size < 0 || rar->unp_size < 0)
1501
+ {
1502
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1503
+ "Invalid sizes specified");
1504
+ return (ARCHIVE_FATAL);
1505
+ }
1506
+
1507
+ rar->bytes_remaining = rar->packed_size;
1508
+
1509
+ /* TODO: RARv3 subblocks contain comments. For now the complete block is
1510
+ * consumed at the end.
1511
+ */
1512
+ if (head_type == NEWSUB_HEAD) {
1513
+ if (rar->packed_size > INT64_MAX - header_size) {
1514
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1515
+ "Invalid RAR file: Overlarge extended header");
1516
+ return (ARCHIVE_FATAL);
1517
+ }
1518
+ if (__archive_read_consume(a, header_size + rar->packed_size - 7) < 0) {
1519
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1520
+ "Invalid RAR file: Cannot read extended header data");
1521
+ return (ARCHIVE_FATAL);
1522
+ }
1523
+
1524
+ /*
1525
+ * NEWSUB records are metadata-only in this reader. The block header
1526
+ * has already been validated, so it is safe to skip exactly the
1527
+ * remaining header bytes and the associated data payload.
1528
+ */
1529
+ return ret;
1530
+ }
1531
+
1532
+ filename_size = archive_le16dec(file_header.name_size);
1533
+ if (p + filename_size > endp) {
1534
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1535
+ "Invalid filename size");
1536
+ return (ARCHIVE_FATAL);
1537
+ }
1538
+ if (rar->filename_allocated < filename_size * 2 + 2) {
1539
+ newsize = filename_size * 2 + 2;
1540
+ newptr = realloc(rar->filename, newsize);
1541
+ if (newptr == NULL) {
1542
+ archive_set_error(&a->archive, ENOMEM,
1543
+ "Couldn't allocate memory");
1544
+ return (ARCHIVE_FATAL);
1545
+ }
1546
+ rar->filename = newptr;
1547
+ rar->filename_allocated = newsize;
1548
+ }
1549
+ filename = rar->filename;
1550
+ memcpy(filename, p, filename_size);
1551
+ filename[filename_size] = '\0';
1552
+ if (rar->file_flags & FHD_UNICODE)
1553
+ {
1554
+ if (filename_size != strlen(filename))
1555
+ {
1556
+ unsigned char highbyte, flagbits, flagbyte;
1557
+ unsigned fn_end, offset;
1558
+
1559
+ end = filename_size;
1560
+ fn_end = filename_size * 2;
1561
+ filename_size = 0;
1562
+ offset = (unsigned)strlen(filename) + 1;
1563
+ highbyte = offset >= end ? 0 : *(p + offset++);
1564
+ flagbits = 0;
1565
+ flagbyte = 0;
1566
+ while (offset < end && filename_size < fn_end)
1567
+ {
1568
+ if (!flagbits)
1569
+ {
1570
+ flagbyte = *(p + offset++);
1571
+ flagbits = 8;
1572
+ }
1573
+
1574
+ flagbits -= 2;
1575
+ switch((flagbyte >> flagbits) & 3)
1576
+ {
1577
+ case 0:
1578
+ if (offset >= end)
1579
+ continue;
1580
+ filename[filename_size++] = '\0';
1581
+ filename[filename_size++] = *(p + offset++);
1582
+ break;
1583
+ case 1:
1584
+ if (offset >= end)
1585
+ continue;
1586
+ filename[filename_size++] = highbyte;
1587
+ filename[filename_size++] = *(p + offset++);
1588
+ break;
1589
+ case 2:
1590
+ if (offset >= end - 1) {
1591
+ offset = end;
1592
+ continue;
1593
+ }
1594
+ filename[filename_size++] = *(p + offset + 1);
1595
+ filename[filename_size++] = *(p + offset);
1596
+ offset += 2;
1597
+ break;
1598
+ case 3:
1599
+ {
1600
+ char extra, high;
1601
+ uint8_t length;
1602
+
1603
+ if (offset >= end)
1604
+ continue;
1605
+
1606
+ length = *(p + offset++);
1607
+ if (length & 0x80) {
1608
+ if (offset >= end)
1609
+ continue;
1610
+ extra = *(p + offset++);
1611
+ high = (char)highbyte;
1612
+ } else
1613
+ extra = high = 0;
1614
+ length = (length & 0x7f) + 2;
1615
+ while (length && filename_size < fn_end) {
1616
+ unsigned cp = filename_size >> 1;
1617
+ filename[filename_size++] = high;
1618
+ filename[filename_size++] = p[cp] + extra;
1619
+ length--;
1620
+ }
1621
+ }
1622
+ break;
1623
+ }
1624
+ }
1625
+ if (filename_size > fn_end) {
1626
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1627
+ "Invalid filename");
1628
+ return (ARCHIVE_FATAL);
1629
+ }
1630
+ filename[filename_size++] = '\0';
1631
+ /*
1632
+ * Do not increment filename_size here as the computations below
1633
+ * add the space for the terminating NUL explicitly.
1634
+ */
1635
+ filename[filename_size] = '\0';
1636
+
1637
+ /* Decoded unicode form is UTF-16BE, so we have to update a string
1638
+ * conversion object for it. */
1639
+ if (rar->sconv_utf16be == NULL) {
1640
+ rar->sconv_utf16be = archive_string_conversion_from_charset(
1641
+ &a->archive, "UTF-16BE", 1);
1642
+ if (rar->sconv_utf16be == NULL)
1643
+ return (ARCHIVE_FATAL);
1644
+ }
1645
+ fn_sconv = rar->sconv_utf16be;
1646
+
1647
+ strp = filename;
1648
+ while (memcmp(strp, "\x00\x00", 2))
1649
+ {
1650
+ if (!memcmp(strp, "\x00\\", 2))
1651
+ *(strp + 1) = '/';
1652
+ strp += 2;
1653
+ }
1654
+ p += offset;
1655
+ } else {
1656
+ /*
1657
+ * If FHD_UNICODE is set but no unicode data, this file name form
1658
+ * is UTF-8, so we have to update a string conversion object for
1659
+ * it accordingly.
1660
+ */
1661
+ if (rar->sconv_utf8 == NULL) {
1662
+ rar->sconv_utf8 = archive_string_conversion_from_charset(
1663
+ &a->archive, "UTF-8", 1);
1664
+ if (rar->sconv_utf8 == NULL)
1665
+ return (ARCHIVE_FATAL);
1666
+ }
1667
+ fn_sconv = rar->sconv_utf8;
1668
+ strp = filename;
1669
+ while ((strp = strchr(strp, '\\')) != NULL)
1670
+ *strp++ = '/';
1671
+ p += filename_size;
1672
+ }
1673
+ }
1674
+ else
1675
+ {
1676
+ fn_sconv = sconv;
1677
+ strp = filename;
1678
+ while ((strp = strchr(strp, '\\')) != NULL)
1679
+ *strp++ = '/';
1680
+ p += filename_size;
1681
+ }
1682
+
1683
+ /* Split file in multivolume RAR. No more need to process header. */
1684
+ if (rar->filename_save &&
1685
+ filename_size == rar->filename_save_size &&
1686
+ !memcmp(rar->filename, rar->filename_save, filename_size + 1))
1687
+ {
1688
+ __archive_read_consume(a, header_size - 7);
1689
+ rar->br.avail_in = 0;
1690
+ rar->br.next_in = NULL;
1691
+ rar->cursor++;
1692
+ if (rar->cursor >= rar->nodes)
1693
+ {
1694
+ struct data_block_offsets *newdbo;
1695
+
1696
+ newsize = sizeof(*rar->dbo) * (rar->nodes + 1);
1697
+ if ((newdbo = realloc(rar->dbo, newsize)) == NULL)
1698
+ {
1699
+ archive_set_error(&a->archive, ENOMEM, "Couldn't allocate memory");
1700
+ return (ARCHIVE_FATAL);
1701
+ }
1702
+ rar->dbo = newdbo;
1703
+ rar->nodes++;
1704
+ rar->dbo[rar->cursor].header_size = header_size;
1705
+ rar->dbo[rar->cursor].start_offset = -1;
1706
+ rar->dbo[rar->cursor].end_offset = -1;
1707
+ }
1708
+ if (rar->dbo[rar->cursor].start_offset < 0)
1709
+ {
1710
+ if (rar->packed_size > INT64_MAX - a->filter->position)
1711
+ {
1712
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1713
+ "Unable to store offsets");
1714
+ return (ARCHIVE_FATAL);
1715
+ }
1716
+ rar->dbo[rar->cursor].start_offset = a->filter->position;
1717
+ rar->dbo[rar->cursor].end_offset = rar->dbo[rar->cursor].start_offset +
1718
+ rar->packed_size;
1719
+ }
1720
+ return ret;
1721
+ }
1722
+ else if (rar->filename_must_match)
1723
+ {
1724
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1725
+ "Mismatch of file parts split across multi-volume archive");
1726
+ return (ARCHIVE_FATAL);
1727
+ }
1728
+
1729
+ newsize = filename_size + 1;
1730
+ if ((newptr = realloc(rar->filename_save, newsize)) == NULL)
1731
+ {
1732
+ archive_set_error(&a->archive, ENOMEM, "Couldn't allocate memory");
1733
+ return (ARCHIVE_FATAL);
1734
+ }
1735
+ rar->filename_save = newptr;
1736
+ memcpy(rar->filename_save, rar->filename, newsize);
1737
+ rar->filename_save_size = filename_size;
1738
+
1739
+ /* Set info for seeking */
1740
+ free(rar->dbo);
1741
+ if ((rar->dbo = calloc(1, sizeof(*rar->dbo))) == NULL)
1742
+ {
1743
+ archive_set_error(&a->archive, ENOMEM, "Couldn't allocate memory");
1744
+ return (ARCHIVE_FATAL);
1745
+ }
1746
+ rar->dbo[0].header_size = header_size;
1747
+ rar->dbo[0].start_offset = -1;
1748
+ rar->dbo[0].end_offset = -1;
1749
+ rar->cursor = 0;
1750
+ rar->nodes = 1;
1751
+
1752
+ if (rar->file_flags & FHD_SALT)
1753
+ {
1754
+ if (p + 8 > endp) {
1755
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1756
+ "Invalid header size");
1757
+ return (ARCHIVE_FATAL);
1758
+ }
1759
+ memcpy(rar->salt, p, 8);
1760
+ p += 8;
1761
+ }
1762
+
1763
+ if (rar->file_flags & FHD_EXTTIME) {
1764
+ if (read_exttime(p, rar, endp) < 0) {
1765
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1766
+ "Invalid header size");
1767
+ return (ARCHIVE_FATAL);
1768
+ }
1769
+ }
1770
+
1771
+ __archive_read_consume(a, header_size - 7);
1772
+ if (rar->packed_size > INT64_MAX - a->filter->position) {
1773
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1774
+ "Unable to store offsets");
1775
+ return (ARCHIVE_FATAL);
1776
+ }
1777
+ rar->dbo[0].start_offset = a->filter->position;
1778
+ rar->dbo[0].end_offset = rar->dbo[0].start_offset + rar->packed_size;
1779
+
1780
+ switch(file_header.host_os)
1781
+ {
1782
+ case OS_MSDOS:
1783
+ case OS_OS2:
1784
+ case OS_WIN32:
1785
+ rar->mode = (__LA_MODE_T)archive_le32dec(file_header.file_attr);
1786
+ if (rar->mode & FILE_ATTRIBUTE_DIRECTORY)
1787
+ rar->mode = AE_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
1788
+ else
1789
+ rar->mode = AE_IFREG;
1790
+ rar->mode |= S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1791
+ break;
1792
+
1793
+ case OS_UNIX:
1794
+ case OS_MAC_OS:
1795
+ case OS_BEOS:
1796
+ rar->mode = (__LA_MODE_T)archive_le32dec(file_header.file_attr);
1797
+ break;
1798
+
1799
+ default:
1800
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1801
+ "Unknown file attributes from RAR file's host OS");
1802
+ return (ARCHIVE_FATAL);
1803
+ }
1804
+
1805
+ rar->bytes_uncopied = rar->bytes_unconsumed = 0;
1806
+ rar->lzss.position = rar->offset = 0;
1807
+ rar->offset_seek = 0;
1808
+ rar->dictionary_size = 0;
1809
+ rar->offset_outgoing = 0;
1810
+ rar->br.cache_avail = 0;
1811
+ rar->br.avail_in = 0;
1812
+ rar->br.next_in = NULL;
1813
+ rar->crc_calculated = 0;
1814
+ rar->entry_eof = 0;
1815
+ rar->valid = 1;
1816
+ rar->is_ppmd_block = 0;
1817
+ rar->start_new_table = 1;
1818
+ free(rar->unp_buffer);
1819
+ rar->unp_buffer = NULL;
1820
+ rar->unp_offset = 0;
1821
+ rar->unp_buffer_size = UNP_BUFFER_SIZE;
1822
+ memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
1823
+ __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
1824
+ rar->ppmd_valid = rar->ppmd_eod = 0;
1825
+ rar->filters.filterstart = INT64_MAX;
1826
+
1827
+ archive_entry_set_mtime(entry, rar->mtime, rar->mnsec);
1828
+ archive_entry_set_ctime(entry, rar->ctime, rar->cnsec);
1829
+ archive_entry_set_atime(entry, rar->atime, rar->ansec);
1830
+ archive_entry_set_size(entry, rar->unp_size);
1831
+ archive_entry_set_mode(entry, rar->mode);
1832
+
1833
+ if (archive_entry_copy_pathname_l(entry, filename, filename_size, fn_sconv))
1834
+ {
1835
+ if (errno == ENOMEM)
1836
+ {
1837
+ archive_set_error(&a->archive, ENOMEM,
1838
+ "Can't allocate memory for Pathname");
1839
+ return (ARCHIVE_FATAL);
1840
+ }
1841
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1842
+ "Pathname cannot be converted from %s to current locale",
1843
+ archive_string_conversion_charset_name(fn_sconv));
1844
+ ret = (ARCHIVE_WARN);
1845
+ }
1846
+
1847
+ if (((rar->mode) & AE_IFMT) == AE_IFLNK)
1848
+ {
1849
+ /* Make sure a symbolic-link file does not have its body. */
1850
+ rar->bytes_remaining = 0;
1851
+ archive_entry_set_size(entry, 0);
1852
+
1853
+ /* Read a symbolic-link name. */
1854
+ if ((ret2 = read_symlink_stored(a, entry, sconv)) < (ARCHIVE_WARN))
1855
+ return ret2;
1856
+ if (ret > ret2)
1857
+ ret = ret2;
1858
+ }
1859
+
1860
+ if (rar->bytes_remaining == 0)
1861
+ rar->entry_eof = 1;
1862
+
1863
+ return ret;
1864
+ }
1865
+
1866
+ static time_t
1867
+ get_time(int ttime)
1868
+ {
1869
+ struct tm tm;
1870
+ tm.tm_sec = 2 * (ttime & 0x1f);
1871
+ tm.tm_min = (ttime >> 5) & 0x3f;
1872
+ tm.tm_hour = (ttime >> 11) & 0x1f;
1873
+ tm.tm_mday = (ttime >> 16) & 0x1f;
1874
+ tm.tm_mon = ((ttime >> 21) & 0x0f) - 1;
1875
+ tm.tm_year = ((ttime >> 25) & 0x7f) + 80;
1876
+ tm.tm_isdst = -1;
1877
+ return mktime(&tm);
1878
+ }
1879
+
1880
+ static int
1881
+ read_exttime(const char *p, struct rar *rar, const char *endp)
1882
+ {
1883
+ unsigned rmode, flags, rem, j, count;
1884
+ int ttime, i;
1885
+ struct tm *tm;
1886
+ time_t t;
1887
+ long nsec;
1888
+ #if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S)
1889
+ struct tm tmbuf;
1890
+ #endif
1891
+
1892
+ if (p + 2 > endp)
1893
+ return (-1);
1894
+ flags = archive_le16dec(p);
1895
+ p += 2;
1896
+
1897
+ for (i = 3; i >= 0; i--)
1898
+ {
1899
+ t = 0;
1900
+ if (i == 3)
1901
+ t = rar->mtime;
1902
+ rmode = flags >> i * 4;
1903
+ if (rmode & 8)
1904
+ {
1905
+ if (!t)
1906
+ {
1907
+ if (p + 4 > endp)
1908
+ return (-1);
1909
+ ttime = archive_le32dec(p);
1910
+ t = get_time(ttime);
1911
+ p += 4;
1912
+ }
1913
+ rem = 0;
1914
+ count = rmode & 3;
1915
+ if (p + count > endp)
1916
+ return (-1);
1917
+ for (j = 0; j < count; j++)
1918
+ {
1919
+ rem = (((unsigned)(unsigned char)*p) << 16) | (rem >> 8);
1920
+ p++;
1921
+ }
1922
+ #if defined(HAVE_LOCALTIME_S)
1923
+ tm = localtime_s(&tmbuf, &t) ? NULL : &tmbuf;
1924
+ #elif defined(HAVE_LOCALTIME_R)
1925
+ tm = localtime_r(&t, &tmbuf);
1926
+ #else
1927
+ tm = localtime(&t);
1928
+ #endif
1929
+ nsec = tm->tm_sec + rem / NS_UNIT;
1930
+ if (rmode & 4)
1931
+ {
1932
+ tm->tm_sec++;
1933
+ t = mktime(tm);
1934
+ }
1935
+ if (i == 3)
1936
+ {
1937
+ rar->mtime = t;
1938
+ rar->mnsec = nsec;
1939
+ }
1940
+ else if (i == 2)
1941
+ {
1942
+ rar->ctime = t;
1943
+ rar->cnsec = nsec;
1944
+ }
1945
+ else if (i == 1)
1946
+ {
1947
+ rar->atime = t;
1948
+ rar->ansec = nsec;
1949
+ }
1950
+ else
1951
+ {
1952
+ rar->arctime = t;
1953
+ rar->arcnsec = nsec;
1954
+ }
1955
+ }
1956
+ }
1957
+ return (0);
1958
+ }
1959
+
1960
+ static int
1961
+ read_symlink_stored(struct archive_read *a, struct archive_entry *entry,
1962
+ struct archive_string_conv *sconv)
1963
+ {
1964
+ struct rar *rar = a->format->data;
1965
+ const void *h;
1966
+ const char *p;
1967
+ int ret = (ARCHIVE_OK);
1968
+
1969
+ if ((uintmax_t)rar->packed_size > SIZE_MAX)
1970
+ {
1971
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1972
+ "Unable to read link");
1973
+ return (ARCHIVE_FATAL);
1974
+ }
1975
+ if ((h = rar_read_ahead(a, (size_t)rar->packed_size, NULL)) == NULL)
1976
+ {
1977
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1978
+ "Failed to read link");
1979
+ return (ARCHIVE_FATAL);
1980
+ }
1981
+ p = h;
1982
+
1983
+ if (archive_entry_copy_symlink_l(entry,
1984
+ p, (size_t)rar->packed_size, sconv))
1985
+ {
1986
+ if (errno == ENOMEM)
1987
+ {
1988
+ archive_set_error(&a->archive, ENOMEM,
1989
+ "Can't allocate memory for link");
1990
+ return (ARCHIVE_FATAL);
1991
+ }
1992
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1993
+ "link cannot be converted from %s to current locale",
1994
+ archive_string_conversion_charset_name(sconv));
1995
+ ret = (ARCHIVE_WARN);
1996
+ }
1997
+ __archive_read_consume(a, rar->packed_size);
1998
+ return ret;
1999
+ }
2000
+
2001
+ static int
2002
+ read_data_stored(struct archive_read *a, const void **buff, size_t *size,
2003
+ int64_t *offset)
2004
+ {
2005
+ struct rar *rar = a->format->data;
2006
+ ssize_t bytes_avail;
2007
+
2008
+ if (rar->bytes_remaining == 0 &&
2009
+ !(rar->main_flags & MHD_VOLUME && rar->file_flags & FHD_SPLIT_AFTER))
2010
+ {
2011
+ *buff = NULL;
2012
+ *size = 0;
2013
+ *offset = rar->offset;
2014
+ if (rar->file_crc != rar->crc_calculated) {
2015
+ #ifndef DONT_FAIL_ON_CRC_ERROR
2016
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2017
+ "File CRC error");
2018
+ return (ARCHIVE_FAILED);
2019
+ #endif
2020
+ }
2021
+ rar->entry_eof = 1;
2022
+ return (ARCHIVE_EOF);
2023
+ }
2024
+
2025
+ *buff = rar_read_ahead(a, 1, &bytes_avail);
2026
+ if (bytes_avail <= 0)
2027
+ {
2028
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2029
+ "Truncated RAR file data");
2030
+ return (ARCHIVE_FAILED);
2031
+ }
2032
+
2033
+ *size = bytes_avail;
2034
+ *offset = rar->offset;
2035
+ rar->offset += bytes_avail;
2036
+ rar->offset_seek += bytes_avail;
2037
+ rar->bytes_remaining -= bytes_avail;
2038
+ rar->bytes_unconsumed = bytes_avail;
2039
+ /* Calculate File CRC. */
2040
+ rar->crc_calculated = crc32(rar->crc_calculated, *buff,
2041
+ (unsigned)bytes_avail);
2042
+ return (ARCHIVE_OK);
2043
+ }
2044
+
2045
+ static int
2046
+ read_data_compressed(struct archive_read *a, const void **buff, size_t *size,
2047
+ int64_t *offset, size_t looper)
2048
+ {
2049
+ if (looper++ > MAX_COMPRESS_DEPTH)
2050
+ return (ARCHIVE_FAILED);
2051
+
2052
+ struct rar *rar = a->format->data;
2053
+ int64_t start, end;
2054
+ size_t bs;
2055
+ int ret = (ARCHIVE_OK), sym, code, lzss_offset, length, i;
2056
+
2057
+ do {
2058
+ if (!rar->valid)
2059
+ return (ARCHIVE_FAILED);
2060
+
2061
+ if (rar->filters.bytes_ready > 0)
2062
+ {
2063
+ /* Flush unp_buffer first */
2064
+ if (rar->unp_offset > 0)
2065
+ {
2066
+ *buff = rar->unp_buffer;
2067
+ *size = rar->unp_offset;
2068
+ rar->unp_offset = 0;
2069
+ *offset = rar->offset_outgoing;
2070
+ rar->offset_outgoing += *size;
2071
+ }
2072
+ else
2073
+ {
2074
+ *buff = rar->filters.bytes;
2075
+ *size = rar->filters.bytes_ready;
2076
+
2077
+ rar->offset += *size;
2078
+ *offset = rar->offset_outgoing;
2079
+ rar->offset_outgoing += *size;
2080
+
2081
+ rar->filters.bytes_ready -= *size;
2082
+ rar->filters.bytes += *size;
2083
+ }
2084
+ goto ending_block;
2085
+ }
2086
+
2087
+ if (rar->ppmd_eod ||
2088
+ (rar->dictionary_size && rar->offset >= rar->unp_size))
2089
+ {
2090
+ if (rar->unp_offset > 0) {
2091
+ /*
2092
+ * We have unprocessed extracted data. write it out.
2093
+ */
2094
+ *buff = rar->unp_buffer;
2095
+ *size = rar->unp_offset;
2096
+ *offset = rar->offset_outgoing;
2097
+ rar->offset_outgoing += *size;
2098
+ /* Calculate File CRC. */
2099
+ rar->crc_calculated = crc32(rar->crc_calculated, *buff,
2100
+ (unsigned)*size);
2101
+ rar->unp_offset = 0;
2102
+ return (ARCHIVE_OK);
2103
+ }
2104
+ *buff = NULL;
2105
+ *size = 0;
2106
+ *offset = rar->offset;
2107
+ if (rar->file_crc != rar->crc_calculated) {
2108
+ #ifndef DONT_FAIL_ON_CRC_ERROR
2109
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2110
+ "File CRC error");
2111
+ return (ARCHIVE_FAILED);
2112
+ #endif
2113
+ }
2114
+ rar->entry_eof = 1;
2115
+ return (ARCHIVE_EOF);
2116
+ }
2117
+
2118
+ if (!rar->is_ppmd_block && rar->dictionary_size && rar->bytes_uncopied > 0)
2119
+ {
2120
+ if (rar->bytes_uncopied > (rar->unp_buffer_size - rar->unp_offset))
2121
+ bs = rar->unp_buffer_size - rar->unp_offset;
2122
+ else
2123
+ bs = (size_t)rar->bytes_uncopied;
2124
+ ret = copy_from_lzss_window_to_unp(a, buff, rar->offset, bs);
2125
+ if (ret != ARCHIVE_OK)
2126
+ return (ret);
2127
+ rar->offset += bs;
2128
+ rar->bytes_uncopied -= bs;
2129
+ if (*buff != NULL) {
2130
+ rar->unp_offset = 0;
2131
+ *size = rar->unp_buffer_size;
2132
+ *offset = rar->offset_outgoing;
2133
+ rar->offset_outgoing += *size;
2134
+ /* Calculate File CRC. */
2135
+ rar->crc_calculated = crc32(rar->crc_calculated, *buff,
2136
+ (unsigned)*size);
2137
+ return (ret);
2138
+ }
2139
+ continue;
2140
+ }
2141
+
2142
+ if (rar->filters.lastend == rar->filters.filterstart)
2143
+ {
2144
+ if (!run_filters(a))
2145
+ return (ARCHIVE_FAILED);
2146
+ continue;
2147
+ }
2148
+
2149
+ if (!rar->br.next_in &&
2150
+ (ret = rar_br_preparation(a, &(rar->br))) < ARCHIVE_WARN)
2151
+ return (ret);
2152
+ if (rar->start_new_table && ((ret = parse_codes(a)) < (ARCHIVE_WARN)))
2153
+ return (ret);
2154
+
2155
+ if (rar->is_ppmd_block)
2156
+ {
2157
+ if ((sym = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
2158
+ &rar->ppmd7_context, &rar->range_dec.p)) < 0)
2159
+ {
2160
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2161
+ "Invalid symbol");
2162
+ return (ARCHIVE_FAILED);
2163
+ }
2164
+ if(sym != rar->ppmd_escape)
2165
+ {
2166
+ lzss_emit_literal(rar, sym);
2167
+ rar->bytes_uncopied++;
2168
+ }
2169
+ else
2170
+ {
2171
+ if ((code = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
2172
+ &rar->ppmd7_context, &rar->range_dec.p)) < 0)
2173
+ {
2174
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2175
+ "Invalid symbol");
2176
+ return (ARCHIVE_FAILED);
2177
+ }
2178
+
2179
+ switch(code)
2180
+ {
2181
+ case 0:
2182
+ rar->start_new_table = 1;
2183
+ return read_data_compressed(a, buff, size, offset, looper);
2184
+
2185
+ case 2:
2186
+ rar->ppmd_eod = 1;/* End Of ppmd Data. */
2187
+ continue;
2188
+
2189
+ case 3:
2190
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2191
+ "Parsing filters is unsupported");
2192
+ return (ARCHIVE_FAILED);
2193
+
2194
+ case 4:
2195
+ lzss_offset = 0;
2196
+ for (i = 2; i >= 0; i--)
2197
+ {
2198
+ if ((code = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
2199
+ &rar->ppmd7_context, &rar->range_dec.p)) < 0)
2200
+ {
2201
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2202
+ "Invalid symbol");
2203
+ return (ARCHIVE_FAILED);
2204
+ }
2205
+ lzss_offset |= code << (i * 8);
2206
+ }
2207
+ if ((length = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
2208
+ &rar->ppmd7_context, &rar->range_dec.p)) < 0)
2209
+ {
2210
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2211
+ "Invalid symbol");
2212
+ return (ARCHIVE_FAILED);
2213
+ }
2214
+ lzss_emit_match(rar, lzss_offset + 2, length + 32);
2215
+ rar->bytes_uncopied += length + 32;
2216
+ break;
2217
+
2218
+ case 5:
2219
+ if ((length = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
2220
+ &rar->ppmd7_context, &rar->range_dec.p)) < 0)
2221
+ {
2222
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2223
+ "Invalid symbol");
2224
+ return (ARCHIVE_FAILED);
2225
+ }
2226
+ lzss_emit_match(rar, 1, length + 4);
2227
+ rar->bytes_uncopied += length + 4;
2228
+ break;
2229
+
2230
+ default:
2231
+ lzss_emit_literal(rar, sym);
2232
+ rar->bytes_uncopied++;
2233
+ }
2234
+ }
2235
+ }
2236
+ else
2237
+ {
2238
+ start = rar->offset;
2239
+ end = start + rar->dictionary_size;
2240
+
2241
+ /* We don't want to overflow the window and overwrite data that we write
2242
+ * at 'start'. Therefore, reduce the end length by the maximum match size,
2243
+ * which is 260 bytes. You can compute this maximum by looking at the
2244
+ * definition of 'expand', in particular when 'symbol >= 271'. */
2245
+ /* NOTE: It's possible for 'dictionary_size' to be less than this 260
2246
+ * value, however that will only be the case when 'unp_size' is small,
2247
+ * which should only happen when the entry size is small and there's no
2248
+ * risk of overflowing the buffer */
2249
+ if (rar->dictionary_size > 260) {
2250
+ end -= 260;
2251
+ }
2252
+
2253
+ if (rar->filters.filterstart < end) {
2254
+ end = rar->filters.filterstart;
2255
+ }
2256
+
2257
+ ret = expand(a, &end);
2258
+ if (ret != ARCHIVE_OK)
2259
+ return (ret);
2260
+
2261
+ rar->bytes_uncopied = end - start;
2262
+ rar->filters.lastend = end;
2263
+ if (rar->filters.lastend != rar->filters.filterstart && rar->bytes_uncopied == 0) {
2264
+ /* Broken RAR files cause this case.
2265
+ * NOTE: If this case were possible on a normal RAR file
2266
+ * we would find out where it was actually bad and
2267
+ * what we would do to solve it. */
2268
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2269
+ "Internal error extracting RAR file");
2270
+ return (ARCHIVE_FAILED);
2271
+ }
2272
+ }
2273
+ if (rar->bytes_uncopied > (rar->unp_buffer_size - rar->unp_offset))
2274
+ bs = rar->unp_buffer_size - rar->unp_offset;
2275
+ else
2276
+ bs = (size_t)rar->bytes_uncopied;
2277
+ ret = copy_from_lzss_window_to_unp(a, buff, rar->offset, bs);
2278
+ if (ret != ARCHIVE_OK)
2279
+ return (ret);
2280
+ rar->offset += bs;
2281
+ rar->bytes_uncopied -= bs;
2282
+ /*
2283
+ * If *buff is NULL, it means unp_buffer is not full.
2284
+ * So we have to continue extracting a RAR file.
2285
+ */
2286
+ } while (*buff == NULL);
2287
+
2288
+ rar->unp_offset = 0;
2289
+ *size = rar->unp_buffer_size;
2290
+ *offset = rar->offset_outgoing;
2291
+ rar->offset_outgoing += *size;
2292
+ ending_block:
2293
+ /* Calculate File CRC. */
2294
+ rar->crc_calculated = crc32(rar->crc_calculated, *buff, (unsigned)*size);
2295
+ return ret;
2296
+ }
2297
+
2298
+ static int
2299
+ parse_codes(struct archive_read *a)
2300
+ {
2301
+ struct rar *rar = a->format->data;
2302
+ int i, j, val, n, r;
2303
+ unsigned char bitlengths[MAX_SYMBOLS], zerocount, ppmd_flags;
2304
+ unsigned int maxorder;
2305
+ struct huffman_code precode;
2306
+ struct rar_br *br = &(rar->br);
2307
+
2308
+ free_codes(a);
2309
+
2310
+ /* Skip to the next byte */
2311
+ rar_br_consume_unaligned_bits(br);
2312
+
2313
+ /* PPMd block flag */
2314
+ if (!rar_br_read_ahead(a, br, 1))
2315
+ goto truncated_data;
2316
+ if ((rar->is_ppmd_block = rar_br_bits(br, 1)) != 0)
2317
+ {
2318
+ rar_br_consume(br, 1);
2319
+ if (!rar_br_read_ahead(a, br, 7))
2320
+ goto truncated_data;
2321
+ ppmd_flags = rar_br_bits(br, 7);
2322
+ rar_br_consume(br, 7);
2323
+
2324
+ /* Memory is allocated in MB */
2325
+ if (ppmd_flags & 0x20)
2326
+ {
2327
+ if (!rar_br_read_ahead(a, br, 8))
2328
+ goto truncated_data;
2329
+ rar->dictionary_size = (rar_br_bits(br, 8) + 1) << 20;
2330
+ rar_br_consume(br, 8);
2331
+ }
2332
+
2333
+ if (ppmd_flags & 0x40)
2334
+ {
2335
+ if (!rar_br_read_ahead(a, br, 8))
2336
+ goto truncated_data;
2337
+ rar->ppmd_escape = rar->ppmd7_context.InitEsc = rar_br_bits(br, 8);
2338
+ rar_br_consume(br, 8);
2339
+ }
2340
+ else
2341
+ rar->ppmd_escape = 2;
2342
+
2343
+ if (ppmd_flags & 0x20)
2344
+ {
2345
+ maxorder = (ppmd_flags & 0x1F) + 1;
2346
+ if(maxorder > 16)
2347
+ maxorder = 16 + (maxorder - 16) * 3;
2348
+
2349
+ if (maxorder == 1)
2350
+ {
2351
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2352
+ "Truncated RAR file data");
2353
+ return (ARCHIVE_FAILED);
2354
+ }
2355
+
2356
+ /* Make sure ppmd7_context is freed before Ppmd7_Construct
2357
+ * because reading a broken file causes this abnormal sequence. */
2358
+ __archive_ppmd7_functions.Ppmd7_Free(&rar->ppmd7_context);
2359
+
2360
+ rar->bytein.a = a;
2361
+ rar->bytein.Read = &ppmd_read;
2362
+ __archive_ppmd7_functions.PpmdRAR_RangeDec_CreateVTable(&rar->range_dec);
2363
+ rar->range_dec.Stream = &rar->bytein;
2364
+ __archive_ppmd7_functions.Ppmd7_Construct(&rar->ppmd7_context);
2365
+
2366
+ if (rar->dictionary_size == 0) {
2367
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2368
+ "Invalid zero dictionary size");
2369
+ return (ARCHIVE_FAILED);
2370
+ }
2371
+
2372
+ if (!__archive_ppmd7_functions.Ppmd7_Alloc(&rar->ppmd7_context,
2373
+ rar->dictionary_size))
2374
+ {
2375
+ archive_set_error(&a->archive, ENOMEM,
2376
+ "Out of memory");
2377
+ return (ARCHIVE_FATAL);
2378
+ }
2379
+ if (!__archive_ppmd7_functions.PpmdRAR_RangeDec_Init(&rar->range_dec))
2380
+ {
2381
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2382
+ "Unable to initialize PPMd range decoder");
2383
+ return (ARCHIVE_FAILED);
2384
+ }
2385
+ __archive_ppmd7_functions.Ppmd7_Init(&rar->ppmd7_context, maxorder);
2386
+ rar->ppmd_valid = 1;
2387
+ }
2388
+ else
2389
+ {
2390
+ if (!rar->ppmd_valid) {
2391
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2392
+ "Invalid PPMd sequence");
2393
+ return (ARCHIVE_FAILED);
2394
+ }
2395
+ if (!__archive_ppmd7_functions.PpmdRAR_RangeDec_Init(&rar->range_dec))
2396
+ {
2397
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2398
+ "Unable to initialize PPMd range decoder");
2399
+ return (ARCHIVE_FAILED);
2400
+ }
2401
+ }
2402
+ }
2403
+ else
2404
+ {
2405
+ rar_br_consume(br, 1);
2406
+
2407
+ /*
2408
+ * Low-distance repeat state belongs to the current LZ table and
2409
+ * must not be reused after starting a new table.
2410
+ */
2411
+ rar->lastlowoffset = 0;
2412
+ rar->numlowoffsetrepeats = 0;
2413
+
2414
+ /* Keep existing table flag */
2415
+ if (!rar_br_read_ahead(a, br, 1))
2416
+ goto truncated_data;
2417
+ if (!rar_br_bits(br, 1))
2418
+ memset(rar->lengthtable, 0, sizeof(rar->lengthtable));
2419
+ rar_br_consume(br, 1);
2420
+
2421
+ memset(&bitlengths, 0, sizeof(bitlengths));
2422
+ for (i = 0; i < MAX_SYMBOLS;)
2423
+ {
2424
+ if (!rar_br_read_ahead(a, br, 4))
2425
+ goto truncated_data;
2426
+ bitlengths[i++] = rar_br_bits(br, 4);
2427
+ rar_br_consume(br, 4);
2428
+ if (bitlengths[i-1] == 0xF)
2429
+ {
2430
+ if (!rar_br_read_ahead(a, br, 4))
2431
+ goto truncated_data;
2432
+ zerocount = rar_br_bits(br, 4);
2433
+ rar_br_consume(br, 4);
2434
+ if (zerocount)
2435
+ {
2436
+ i--;
2437
+ for (j = 0; j < zerocount + 2 && i < MAX_SYMBOLS; j++)
2438
+ bitlengths[i++] = 0;
2439
+ }
2440
+ }
2441
+ }
2442
+
2443
+ memset(&precode, 0, sizeof(precode));
2444
+ r = create_code(a, &precode, bitlengths, MAX_SYMBOLS, MAX_SYMBOL_LENGTH);
2445
+ if (r != ARCHIVE_OK) {
2446
+ free(precode.tree);
2447
+ free(precode.table);
2448
+ return (r);
2449
+ }
2450
+
2451
+ for (i = 0; i < HUFFMAN_TABLE_SIZE;)
2452
+ {
2453
+ if ((val = read_next_symbol(a, &precode)) < 0) {
2454
+ free(precode.tree);
2455
+ free(precode.table);
2456
+ return (ARCHIVE_FAILED);
2457
+ }
2458
+ if (val < 16)
2459
+ {
2460
+ rar->lengthtable[i] = (rar->lengthtable[i] + val) & 0xF;
2461
+ i++;
2462
+ }
2463
+ else if (val < 18)
2464
+ {
2465
+ if (i == 0)
2466
+ {
2467
+ free(precode.tree);
2468
+ free(precode.table);
2469
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2470
+ "Internal error extracting RAR file");
2471
+ return (ARCHIVE_FAILED);
2472
+ }
2473
+
2474
+ if(val == 16) {
2475
+ if (!rar_br_read_ahead(a, br, 3)) {
2476
+ free(precode.tree);
2477
+ free(precode.table);
2478
+ goto truncated_data;
2479
+ }
2480
+ n = rar_br_bits(br, 3) + 3;
2481
+ rar_br_consume(br, 3);
2482
+ } else {
2483
+ if (!rar_br_read_ahead(a, br, 7)) {
2484
+ free(precode.tree);
2485
+ free(precode.table);
2486
+ goto truncated_data;
2487
+ }
2488
+ n = rar_br_bits(br, 7) + 11;
2489
+ rar_br_consume(br, 7);
2490
+ }
2491
+
2492
+ for (j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
2493
+ {
2494
+ rar->lengthtable[i] = rar->lengthtable[i-1];
2495
+ i++;
2496
+ }
2497
+ }
2498
+ else
2499
+ {
2500
+ if(val == 18) {
2501
+ if (!rar_br_read_ahead(a, br, 3)) {
2502
+ free(precode.tree);
2503
+ free(precode.table);
2504
+ goto truncated_data;
2505
+ }
2506
+ n = rar_br_bits(br, 3) + 3;
2507
+ rar_br_consume(br, 3);
2508
+ } else {
2509
+ if (!rar_br_read_ahead(a, br, 7)) {
2510
+ free(precode.tree);
2511
+ free(precode.table);
2512
+ goto truncated_data;
2513
+ }
2514
+ n = rar_br_bits(br, 7) + 11;
2515
+ rar_br_consume(br, 7);
2516
+ }
2517
+
2518
+ for(j = 0; j < n && i < HUFFMAN_TABLE_SIZE; j++)
2519
+ rar->lengthtable[i++] = 0;
2520
+ }
2521
+ }
2522
+ free(precode.tree);
2523
+ free(precode.table);
2524
+
2525
+ r = create_code(a, &rar->maincode, &rar->lengthtable[0], MAINCODE_SIZE,
2526
+ MAX_SYMBOL_LENGTH);
2527
+ if (r != ARCHIVE_OK)
2528
+ return (r);
2529
+ r = create_code(a, &rar->offsetcode, &rar->lengthtable[MAINCODE_SIZE],
2530
+ OFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
2531
+ if (r != ARCHIVE_OK)
2532
+ return (r);
2533
+ r = create_code(a, &rar->lowoffsetcode,
2534
+ &rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE],
2535
+ LOWOFFSETCODE_SIZE, MAX_SYMBOL_LENGTH);
2536
+ if (r != ARCHIVE_OK)
2537
+ return (r);
2538
+ r = create_code(a, &rar->lengthcode,
2539
+ &rar->lengthtable[MAINCODE_SIZE + OFFSETCODE_SIZE +
2540
+ LOWOFFSETCODE_SIZE], LENGTHCODE_SIZE, MAX_SYMBOL_LENGTH);
2541
+ if (r != ARCHIVE_OK)
2542
+ return (r);
2543
+ }
2544
+
2545
+ if (!rar->dictionary_size || !rar->lzss.window ||
2546
+ (unsigned int)(rar->lzss.mask + 1) < rar->dictionary_size)
2547
+ {
2548
+ /* Seems as though dictionary sizes are not used. Even so, minimize
2549
+ * memory usage as much as possible.
2550
+ */
2551
+ void *new_window;
2552
+ unsigned int new_size;
2553
+
2554
+ if (rar->unp_size >= DICTIONARY_MAX_SIZE)
2555
+ new_size = DICTIONARY_MAX_SIZE;
2556
+ else
2557
+ new_size = rar_fls((unsigned int)rar->unp_size) << 1;
2558
+ if (new_size == 0) {
2559
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2560
+ "Zero window size is invalid");
2561
+ return (ARCHIVE_FAILED);
2562
+ }
2563
+ new_window = realloc(rar->lzss.window, new_size);
2564
+ if (new_window == NULL) {
2565
+ archive_set_error(&a->archive, ENOMEM,
2566
+ "Unable to allocate memory for uncompressed data");
2567
+ return (ARCHIVE_FATAL);
2568
+ }
2569
+ rar->lzss.window = (unsigned char *)new_window;
2570
+ rar->dictionary_size = new_size;
2571
+ memset(rar->lzss.window, 0, rar->dictionary_size);
2572
+ rar->lzss.mask = rar->dictionary_size - 1;
2573
+ }
2574
+
2575
+ rar->start_new_table = 0;
2576
+ return (ARCHIVE_OK);
2577
+ truncated_data:
2578
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2579
+ "Truncated RAR file data");
2580
+ rar->valid = 0;
2581
+ return (ARCHIVE_FAILED);
2582
+ }
2583
+
2584
+ static void
2585
+ free_codes(struct archive_read *a)
2586
+ {
2587
+ struct rar *rar = a->format->data;
2588
+ free(rar->maincode.tree);
2589
+ free(rar->offsetcode.tree);
2590
+ free(rar->lowoffsetcode.tree);
2591
+ free(rar->lengthcode.tree);
2592
+ free(rar->maincode.table);
2593
+ free(rar->offsetcode.table);
2594
+ free(rar->lowoffsetcode.table);
2595
+ free(rar->lengthcode.table);
2596
+ memset(&rar->maincode, 0, sizeof(rar->maincode));
2597
+ memset(&rar->offsetcode, 0, sizeof(rar->offsetcode));
2598
+ memset(&rar->lowoffsetcode, 0, sizeof(rar->lowoffsetcode));
2599
+ memset(&rar->lengthcode, 0, sizeof(rar->lengthcode));
2600
+ }
2601
+
2602
+
2603
+ static int
2604
+ read_next_symbol(struct archive_read *a, struct huffman_code *code)
2605
+ {
2606
+ struct rar *rar = a->format->data;
2607
+ unsigned char bit;
2608
+ unsigned int bits;
2609
+ int length, value, node;
2610
+ struct rar_br *br;
2611
+
2612
+ if (!code->table)
2613
+ {
2614
+ if (make_table(a, code) != (ARCHIVE_OK))
2615
+ return -1;
2616
+ }
2617
+
2618
+ br = &(rar->br);
2619
+
2620
+ /* Look ahead (peek) at bits */
2621
+ if (!rar_br_read_ahead(a, br, code->tablesize)) {
2622
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2623
+ "Truncated RAR file data");
2624
+ rar->valid = 0;
2625
+ return -1;
2626
+ }
2627
+ bits = rar_br_bits(br, code->tablesize);
2628
+
2629
+ length = code->table[bits].length;
2630
+ value = code->table[bits].value;
2631
+
2632
+ if (length < 0)
2633
+ {
2634
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2635
+ "Invalid prefix code in bitstream");
2636
+ return -1;
2637
+ }
2638
+
2639
+ if (length <= code->tablesize)
2640
+ {
2641
+ /* Skip length bits */
2642
+ rar_br_consume(br, length);
2643
+ return value;
2644
+ }
2645
+
2646
+ /* Skip tablesize bits */
2647
+ rar_br_consume(br, code->tablesize);
2648
+
2649
+ node = value;
2650
+ while (code->tree[node].branches[0] != code->tree[node].branches[1])
2651
+ {
2652
+ if (!rar_br_read_ahead(a, br, 1)) {
2653
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2654
+ "Truncated RAR file data");
2655
+ rar->valid = 0;
2656
+ return -1;
2657
+ }
2658
+ bit = rar_br_bits(br, 1);
2659
+ rar_br_consume(br, 1);
2660
+
2661
+ if (code->tree[node].branches[bit] < 0)
2662
+ {
2663
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2664
+ "Invalid prefix code in bitstream");
2665
+ return -1;
2666
+ }
2667
+ node = code->tree[node].branches[bit];
2668
+ }
2669
+
2670
+ return code->tree[node].branches[0];
2671
+ }
2672
+
2673
+ static int
2674
+ create_code(struct archive_read *a, struct huffman_code *code,
2675
+ unsigned char *lengths, int numsymbols, char maxlength)
2676
+ {
2677
+ int i, j, codebits = 0, symbolsleft = numsymbols;
2678
+
2679
+ code->numentries = 0;
2680
+ code->numallocatedentries = 0;
2681
+ if (new_node(code) < 0) {
2682
+ archive_set_error(&a->archive, ENOMEM,
2683
+ "Unable to allocate memory for node data");
2684
+ return (ARCHIVE_FATAL);
2685
+ }
2686
+ code->numentries = 1;
2687
+ code->minlength = INT_MAX;
2688
+ code->maxlength = INT_MIN;
2689
+ codebits = 0;
2690
+ for(i = 1; i <= maxlength; i++)
2691
+ {
2692
+ for(j = 0; j < numsymbols; j++)
2693
+ {
2694
+ if (lengths[j] != i) continue;
2695
+ if (add_value(a, code, j, codebits, i) != ARCHIVE_OK)
2696
+ return (ARCHIVE_FAILED);
2697
+ codebits++;
2698
+ if (--symbolsleft <= 0)
2699
+ break;
2700
+ }
2701
+ if (symbolsleft <= 0)
2702
+ break;
2703
+ codebits <<= 1;
2704
+ }
2705
+ return (ARCHIVE_OK);
2706
+ }
2707
+
2708
+ static int
2709
+ add_value(struct archive_read *a, struct huffman_code *code, int value,
2710
+ int codebits, int length)
2711
+ {
2712
+ int lastnode, bitpos, bit;
2713
+ /* int repeatpos, repeatnode, nextnode; */
2714
+
2715
+ free(code->table);
2716
+ code->table = NULL;
2717
+
2718
+ if(length > code->maxlength)
2719
+ code->maxlength = length;
2720
+ if(length < code->minlength)
2721
+ code->minlength = length;
2722
+
2723
+ /*
2724
+ * Dead code, repeatpos was is -1
2725
+ *
2726
+ repeatpos = -1;
2727
+ if (repeatpos == 0 || (repeatpos >= 0
2728
+ && (((codebits >> (repeatpos - 1)) & 3) == 0
2729
+ || ((codebits >> (repeatpos - 1)) & 3) == 3)))
2730
+ {
2731
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2732
+ "Invalid repeat position");
2733
+ return (ARCHIVE_FATAL);
2734
+ }
2735
+ */
2736
+
2737
+ lastnode = 0;
2738
+ for (bitpos = length - 1; bitpos >= 0; bitpos--)
2739
+ {
2740
+ bit = (codebits >> bitpos) & 1;
2741
+
2742
+ /* Leaf node check */
2743
+ if (code->tree[lastnode].branches[0] ==
2744
+ code->tree[lastnode].branches[1])
2745
+ {
2746
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2747
+ "Prefix found");
2748
+ return (ARCHIVE_FAILED);
2749
+ }
2750
+
2751
+ /*
2752
+ * Dead code, repeatpos was -1, bitpos >=0
2753
+ *
2754
+ if (bitpos == repeatpos)
2755
+ {
2756
+ * Open branch check *
2757
+ if (!(code->tree[lastnode].branches[bit] < 0))
2758
+ {
2759
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2760
+ "Invalid repeating code");
2761
+ return (ARCHIVE_FATAL);
2762
+ }
2763
+
2764
+ if ((repeatnode = new_node(code)) < 0) {
2765
+ archive_set_error(&a->archive, ENOMEM,
2766
+ "Unable to allocate memory for node data");
2767
+ return (ARCHIVE_FATAL);
2768
+ }
2769
+ if ((nextnode = new_node(code)) < 0) {
2770
+ archive_set_error(&a->archive, ENOMEM,
2771
+ "Unable to allocate memory for node data");
2772
+ return (ARCHIVE_FATAL);
2773
+ }
2774
+
2775
+ * Set branches *
2776
+ code->tree[lastnode].branches[bit] = repeatnode;
2777
+ code->tree[repeatnode].branches[bit] = repeatnode;
2778
+ code->tree[repeatnode].branches[bit^1] = nextnode;
2779
+ lastnode = nextnode;
2780
+
2781
+ bitpos++; * terminating bit already handled, skip it *
2782
+ }
2783
+ else
2784
+ {
2785
+ */
2786
+ /* Open branch check */
2787
+ if (code->tree[lastnode].branches[bit] < 0)
2788
+ {
2789
+ if (new_node(code) < 0) {
2790
+ archive_set_error(&a->archive, ENOMEM,
2791
+ "Unable to allocate memory for node data");
2792
+ return (ARCHIVE_FATAL);
2793
+ }
2794
+ code->tree[lastnode].branches[bit] = code->numentries++;
2795
+ }
2796
+
2797
+ /* set to branch */
2798
+ lastnode = code->tree[lastnode].branches[bit];
2799
+ /* } */
2800
+ }
2801
+
2802
+ if (!(code->tree[lastnode].branches[0] == -1
2803
+ && code->tree[lastnode].branches[1] == -2))
2804
+ {
2805
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2806
+ "Prefix found");
2807
+ return (ARCHIVE_FAILED);
2808
+ }
2809
+
2810
+ /* Set leaf value */
2811
+ code->tree[lastnode].branches[0] = value;
2812
+ code->tree[lastnode].branches[1] = value;
2813
+
2814
+ return (ARCHIVE_OK);
2815
+ }
2816
+
2817
+ static int
2818
+ new_node(struct huffman_code *code)
2819
+ {
2820
+ void *new_tree;
2821
+ if (code->numallocatedentries == code->numentries) {
2822
+ size_t size, new_num_entries;
2823
+
2824
+ if (code->numentries == 0)
2825
+ new_num_entries = 256;
2826
+ else if (archive_ckd_mul_size(&new_num_entries, code->numentries, 2)
2827
+ || new_num_entries > INT_MAX)
2828
+ return -1;
2829
+ if (archive_ckd_mul_size(&size, new_num_entries, sizeof(*code->tree)))
2830
+ return -1;
2831
+ new_tree = realloc(code->tree, size);
2832
+ if (new_tree == NULL)
2833
+ return (-1);
2834
+ code->tree = (struct huffman_tree_node *)new_tree;
2835
+ code->numallocatedentries = new_num_entries;
2836
+ }
2837
+ code->tree[code->numentries].branches[0] = -1;
2838
+ code->tree[code->numentries].branches[1] = -2;
2839
+ return 1;
2840
+ }
2841
+
2842
+ static int
2843
+ make_table(struct archive_read *a, struct huffman_code *code)
2844
+ {
2845
+ if (code->maxlength < code->minlength || code->maxlength > 10)
2846
+ code->tablesize = 10;
2847
+ else
2848
+ code->tablesize = code->maxlength;
2849
+
2850
+ code->table = calloc(((size_t)1U) << code->tablesize, sizeof(*code->table));
2851
+ if (code->table == NULL) {
2852
+ archive_set_error(&a->archive, ENOMEM, "Can't allocate memory");
2853
+ return (ARCHIVE_FATAL);
2854
+ }
2855
+
2856
+ return make_table_recurse(a, code, 0, code->table, 0, code->tablesize);
2857
+ }
2858
+
2859
+ static int
2860
+ make_table_recurse(struct archive_read *a, struct huffman_code *code, int node,
2861
+ struct huffman_table_entry *table, int depth,
2862
+ int maxdepth)
2863
+ {
2864
+ int currtablesize, i, ret = (ARCHIVE_OK);
2865
+
2866
+ if (!code->tree)
2867
+ {
2868
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2869
+ "Huffman tree was not created");
2870
+ return (ARCHIVE_FAILED);
2871
+ }
2872
+ if (node < 0 || (size_t)node >= code->numentries)
2873
+ {
2874
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2875
+ "Invalid location to Huffman tree specified");
2876
+ return (ARCHIVE_FAILED);
2877
+ }
2878
+
2879
+ currtablesize = 1 << (maxdepth - depth);
2880
+
2881
+ if (code->tree[node].branches[0] ==
2882
+ code->tree[node].branches[1])
2883
+ {
2884
+ for(i = 0; i < currtablesize; i++)
2885
+ {
2886
+ table[i].length = depth;
2887
+ table[i].value = code->tree[node].branches[0];
2888
+ }
2889
+ }
2890
+ /*
2891
+ * Dead code, node >= 0
2892
+ *
2893
+ else if (node < 0)
2894
+ {
2895
+ for(i = 0; i < currtablesize; i++)
2896
+ table[i].length = -1;
2897
+ }
2898
+ */
2899
+ else
2900
+ {
2901
+ if(depth == maxdepth)
2902
+ {
2903
+ table[0].length = maxdepth + 1;
2904
+ table[0].value = node;
2905
+ }
2906
+ else
2907
+ {
2908
+ ret |= make_table_recurse(a, code, code->tree[node].branches[0], table,
2909
+ depth + 1, maxdepth);
2910
+ ret |= make_table_recurse(a, code, code->tree[node].branches[1],
2911
+ table + currtablesize / 2, depth + 1, maxdepth);
2912
+ }
2913
+ }
2914
+ return ret;
2915
+ }
2916
+
2917
+ static int
2918
+ expand(struct archive_read *a, int64_t *end)
2919
+ {
2920
+ static const unsigned char lengthbases[] =
2921
+ { 0, 1, 2, 3, 4, 5, 6,
2922
+ 7, 8, 10, 12, 14, 16, 20,
2923
+ 24, 28, 32, 40, 48, 56, 64,
2924
+ 80, 96, 112, 128, 160, 192, 224 };
2925
+ static const unsigned char lengthbits[] =
2926
+ { 0, 0, 0, 0, 0, 0, 0,
2927
+ 0, 1, 1, 1, 1, 2, 2,
2928
+ 2, 2, 3, 3, 3, 3, 4,
2929
+ 4, 4, 4, 5, 5, 5, 5 };
2930
+ static const int lengthb_min = minimum(
2931
+ (int)(sizeof(lengthbases)/sizeof(lengthbases[0])),
2932
+ (int)(sizeof(lengthbits)/sizeof(lengthbits[0]))
2933
+ );
2934
+ static const unsigned int offsetbases[] =
2935
+ { 0, 1, 2, 3, 4, 6,
2936
+ 8, 12, 16, 24, 32, 48,
2937
+ 64, 96, 128, 192, 256, 384,
2938
+ 512, 768, 1024, 1536, 2048, 3072,
2939
+ 4096, 6144, 8192, 12288, 16384, 24576,
2940
+ 32768, 49152, 65536, 98304, 131072, 196608,
2941
+ 262144, 327680, 393216, 458752, 524288, 589824,
2942
+ 655360, 720896, 786432, 851968, 917504, 983040,
2943
+ 1048576, 1310720, 1572864, 1835008, 2097152, 2359296,
2944
+ 2621440, 2883584, 3145728, 3407872, 3670016, 3932160 };
2945
+ static const unsigned char offsetbits[] =
2946
+ { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
2947
+ 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
2948
+ 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16,
2949
+ 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
2950
+ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18 };
2951
+ static const int offsetb_min = minimum(
2952
+ (int)(sizeof(offsetbases)/sizeof(offsetbases[0])),
2953
+ (int)(sizeof(offsetbits)/sizeof(offsetbits[0]))
2954
+ );
2955
+ static const unsigned char shortbases[] =
2956
+ { 0, 4, 8, 16, 32, 64, 128, 192 };
2957
+ static const unsigned char shortbits[] =
2958
+ { 2, 2, 3, 4, 5, 6, 6, 6 };
2959
+
2960
+ struct rar *rar = a->format->data;
2961
+ int symbol, offs, len, offsindex, lensymbol, i, offssymbol, lowoffsetsymbol;
2962
+ unsigned char newfile;
2963
+ struct rar_br *br = &(rar->br);
2964
+
2965
+ if (rar->filters.filterstart < *end)
2966
+ *end = rar->filters.filterstart;
2967
+
2968
+ while (1)
2969
+ {
2970
+ if(lzss_position(&rar->lzss) >= *end) {
2971
+ return (ARCHIVE_OK);
2972
+ }
2973
+
2974
+ if(rar->is_ppmd_block) {
2975
+ *end = lzss_position(&rar->lzss);
2976
+ return (ARCHIVE_OK);
2977
+ }
2978
+
2979
+ if ((symbol = read_next_symbol(a, &rar->maincode)) < 0)
2980
+ goto bad_data;
2981
+
2982
+ if (symbol < 256)
2983
+ {
2984
+ lzss_emit_literal(rar, (uint8_t)symbol);
2985
+ continue;
2986
+ }
2987
+ else if (symbol == 256)
2988
+ {
2989
+ if (!rar_br_read_ahead(a, br, 1))
2990
+ goto truncated_data;
2991
+ newfile = !rar_br_bits(br, 1);
2992
+ rar_br_consume(br, 1);
2993
+
2994
+ if(newfile)
2995
+ {
2996
+ rar->start_new_block = 1;
2997
+ if (!rar_br_read_ahead(a, br, 1))
2998
+ goto truncated_data;
2999
+ rar->start_new_table = rar_br_bits(br, 1);
3000
+ rar_br_consume(br, 1);
3001
+ *end = lzss_position(&rar->lzss);
3002
+ return (ARCHIVE_OK);
3003
+ }
3004
+ else
3005
+ {
3006
+ if (parse_codes(a) != ARCHIVE_OK)
3007
+ goto bad_data;
3008
+ continue;
3009
+ }
3010
+ }
3011
+ else if(symbol==257)
3012
+ {
3013
+ if (!read_filter(a, end))
3014
+ goto bad_data;
3015
+ continue;
3016
+ }
3017
+ else if(symbol==258)
3018
+ {
3019
+ if(rar->lastlength == 0)
3020
+ continue;
3021
+
3022
+ offs = rar->lastoffset;
3023
+ len = rar->lastlength;
3024
+ }
3025
+ else if (symbol <= 262)
3026
+ {
3027
+ offsindex = symbol - 259;
3028
+ offs = rar->oldoffset[offsindex];
3029
+
3030
+ if ((lensymbol = read_next_symbol(a, &rar->lengthcode)) < 0)
3031
+ goto bad_data;
3032
+ if (lensymbol >= lengthb_min)
3033
+ goto bad_data;
3034
+ len = lengthbases[lensymbol] + 2;
3035
+ if (lengthbits[lensymbol] > 0) {
3036
+ if (!rar_br_read_ahead(a, br, lengthbits[lensymbol]))
3037
+ goto truncated_data;
3038
+ len += rar_br_bits(br, lengthbits[lensymbol]);
3039
+ rar_br_consume(br, lengthbits[lensymbol]);
3040
+ }
3041
+
3042
+ for (i = offsindex; i > 0; i--)
3043
+ rar->oldoffset[i] = rar->oldoffset[i-1];
3044
+ rar->oldoffset[0] = offs;
3045
+ }
3046
+ else if(symbol<=270)
3047
+ {
3048
+ offs = shortbases[symbol-263] + 1;
3049
+ if(shortbits[symbol-263] > 0) {
3050
+ if (!rar_br_read_ahead(a, br, shortbits[symbol-263]))
3051
+ goto truncated_data;
3052
+ offs += rar_br_bits(br, shortbits[symbol-263]);
3053
+ rar_br_consume(br, shortbits[symbol-263]);
3054
+ }
3055
+
3056
+ len = 2;
3057
+
3058
+ for(i = 3; i > 0; i--)
3059
+ rar->oldoffset[i] = rar->oldoffset[i-1];
3060
+ rar->oldoffset[0] = offs;
3061
+ }
3062
+ else
3063
+ {
3064
+ if (symbol-271 >= lengthb_min)
3065
+ goto bad_data;
3066
+ len = lengthbases[symbol-271]+3;
3067
+ if(lengthbits[symbol-271] > 0) {
3068
+ if (!rar_br_read_ahead(a, br, lengthbits[symbol-271]))
3069
+ goto truncated_data;
3070
+ len += rar_br_bits(br, lengthbits[symbol-271]);
3071
+ rar_br_consume(br, lengthbits[symbol-271]);
3072
+ }
3073
+
3074
+ if ((offssymbol = read_next_symbol(a, &rar->offsetcode)) < 0)
3075
+ goto bad_data;
3076
+ if (offssymbol >= offsetb_min)
3077
+ goto bad_data;
3078
+ offs = offsetbases[offssymbol]+1;
3079
+ if(offsetbits[offssymbol] > 0)
3080
+ {
3081
+ if(offssymbol > 9)
3082
+ {
3083
+ if(offsetbits[offssymbol] > 4) {
3084
+ if (!rar_br_read_ahead(a, br, offsetbits[offssymbol] - 4))
3085
+ goto truncated_data;
3086
+ offs += rar_br_bits(br, offsetbits[offssymbol] - 4) << 4;
3087
+ rar_br_consume(br, offsetbits[offssymbol] - 4);
3088
+ }
3089
+
3090
+ if(rar->numlowoffsetrepeats > 0)
3091
+ {
3092
+ rar->numlowoffsetrepeats--;
3093
+ offs += rar->lastlowoffset;
3094
+ }
3095
+ else
3096
+ {
3097
+ if ((lowoffsetsymbol =
3098
+ read_next_symbol(a, &rar->lowoffsetcode)) < 0)
3099
+ goto bad_data;
3100
+ if(lowoffsetsymbol == 16)
3101
+ {
3102
+ rar->numlowoffsetrepeats = 15;
3103
+ offs += rar->lastlowoffset;
3104
+ }
3105
+ else
3106
+ {
3107
+ offs += lowoffsetsymbol;
3108
+ rar->lastlowoffset = lowoffsetsymbol;
3109
+ }
3110
+ }
3111
+ }
3112
+ else {
3113
+ if (!rar_br_read_ahead(a, br, offsetbits[offssymbol]))
3114
+ goto truncated_data;
3115
+ offs += rar_br_bits(br, offsetbits[offssymbol]);
3116
+ rar_br_consume(br, offsetbits[offssymbol]);
3117
+ }
3118
+ }
3119
+
3120
+ if (offs >= 0x40000)
3121
+ len++;
3122
+ if (offs >= 0x2000)
3123
+ len++;
3124
+
3125
+ for(i = 3; i > 0; i--)
3126
+ rar->oldoffset[i] = rar->oldoffset[i-1];
3127
+ rar->oldoffset[0] = offs;
3128
+ }
3129
+
3130
+ rar->lastoffset = offs;
3131
+ rar->lastlength = len;
3132
+
3133
+ lzss_emit_match(rar, rar->lastoffset, rar->lastlength);
3134
+ }
3135
+ truncated_data:
3136
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3137
+ "Truncated RAR file data");
3138
+ rar->valid = 0;
3139
+ return (ARCHIVE_FAILED);
3140
+ bad_data:
3141
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3142
+ "Bad RAR file data");
3143
+ return (ARCHIVE_FAILED);
3144
+ }
3145
+
3146
+ static int
3147
+ copy_from_lzss_window(struct archive_read *a, uint8_t *buffer,
3148
+ int64_t startpos, int length)
3149
+ {
3150
+ struct rar *rar = a->format->data;
3151
+ int windowoffs, firstpart;
3152
+
3153
+ windowoffs = lzss_offset_for_position(&rar->lzss, startpos);
3154
+ firstpart = lzss_size(&rar->lzss) - windowoffs;
3155
+ if (length > lzss_size(&rar->lzss)) {
3156
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3157
+ "Bad RAR file data");
3158
+ return (ARCHIVE_FAILED);
3159
+ }
3160
+ if (firstpart < 0) {
3161
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3162
+ "Bad RAR file data");
3163
+ return (ARCHIVE_FAILED);
3164
+ }
3165
+ if (firstpart < length) {
3166
+ memcpy(buffer, &rar->lzss.window[windowoffs], firstpart);
3167
+ memcpy(buffer + firstpart, &rar->lzss.window[0], length - firstpart);
3168
+ } else {
3169
+ memcpy(buffer, &rar->lzss.window[windowoffs], length);
3170
+ }
3171
+ return (ARCHIVE_OK);
3172
+ }
3173
+
3174
+ static int
3175
+ copy_from_lzss_window_to_unp(struct archive_read *a, const void **buffer,
3176
+ int64_t startpos, size_t length)
3177
+ {
3178
+ struct rar *rar = a->format->data;
3179
+ int windowoffs, firstpart;
3180
+
3181
+ if (length > rar->unp_buffer_size)
3182
+ {
3183
+ goto fatal;
3184
+ }
3185
+
3186
+ if (!rar->unp_buffer)
3187
+ {
3188
+ if ((rar->unp_buffer = malloc(rar->unp_buffer_size)) == NULL)
3189
+ {
3190
+ archive_set_error(&a->archive, ENOMEM,
3191
+ "Unable to allocate memory for uncompressed data");
3192
+ return (ARCHIVE_FATAL);
3193
+ }
3194
+ }
3195
+
3196
+ windowoffs = lzss_offset_for_position(&rar->lzss, startpos);
3197
+ if(windowoffs + length <= (size_t)lzss_size(&rar->lzss)) {
3198
+ memcpy(&rar->unp_buffer[rar->unp_offset], &rar->lzss.window[windowoffs],
3199
+ length);
3200
+ } else if (length <= (size_t)lzss_size(&rar->lzss)) {
3201
+ firstpart = lzss_size(&rar->lzss) - windowoffs;
3202
+ if (firstpart < 0) {
3203
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3204
+ "Bad RAR file data");
3205
+ return (ARCHIVE_FAILED);
3206
+ }
3207
+ if ((size_t)firstpart < length) {
3208
+ memcpy(&rar->unp_buffer[rar->unp_offset],
3209
+ &rar->lzss.window[windowoffs], firstpart);
3210
+ memcpy(&rar->unp_buffer[rar->unp_offset + firstpart],
3211
+ &rar->lzss.window[0], length - firstpart);
3212
+ } else {
3213
+ memcpy(&rar->unp_buffer[rar->unp_offset],
3214
+ &rar->lzss.window[windowoffs], length);
3215
+ }
3216
+ } else {
3217
+ goto fatal;
3218
+ }
3219
+ rar->unp_offset += (unsigned int) length;
3220
+ if (rar->unp_offset >= rar->unp_buffer_size)
3221
+ *buffer = rar->unp_buffer;
3222
+ else
3223
+ *buffer = NULL;
3224
+ return (ARCHIVE_OK);
3225
+
3226
+ fatal:
3227
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3228
+ "Bad RAR file data");
3229
+ return (ARCHIVE_FAILED);
3230
+ }
3231
+
3232
+ static const void *
3233
+ rar_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
3234
+ {
3235
+ struct rar *rar = a->format->data;
3236
+ const void *h;
3237
+ int ret;
3238
+
3239
+ again:
3240
+ h = __archive_read_ahead(a, min, avail);
3241
+
3242
+ if (avail)
3243
+ {
3244
+ if (a->archive.read_data_is_posix_read && *avail > (ssize_t)a->archive.read_data_requested)
3245
+ *avail = a->archive.read_data_requested;
3246
+ if (*avail > rar->bytes_remaining)
3247
+ *avail = (ssize_t)rar->bytes_remaining;
3248
+ if (*avail < 0)
3249
+ return NULL;
3250
+ else if (*avail == 0 && rar->main_flags & MHD_VOLUME &&
3251
+ rar->file_flags & FHD_SPLIT_AFTER)
3252
+ {
3253
+ rar->filename_must_match = 1;
3254
+ ret = archive_read_format_rar_read_header(a, a->entry);
3255
+ if (ret == (ARCHIVE_EOF))
3256
+ {
3257
+ rar->has_endarc_header = 1;
3258
+ ret = archive_read_format_rar_read_header(a, a->entry);
3259
+ }
3260
+ rar->filename_must_match = 0;
3261
+ if (ret != (ARCHIVE_OK))
3262
+ return NULL;
3263
+ goto again;
3264
+ }
3265
+ }
3266
+ return h;
3267
+ }
3268
+
3269
+ static int
3270
+ parse_filter(struct archive_read *a, const uint8_t *bytes, uint16_t length, uint8_t flags)
3271
+ {
3272
+ struct rar *rar = a->format->data;
3273
+ struct rar_filters *filters = &rar->filters;
3274
+
3275
+ struct memory_bit_reader br = { 0 };
3276
+ struct rar_program_code *prog;
3277
+ struct rar_filter *filter, **nextfilter;
3278
+
3279
+ uint32_t numprogs, num, blocklength, globaldatalen;
3280
+ uint8_t *globaldata;
3281
+ size_t blockstartpos;
3282
+ uint32_t registers[8] = { 0 };
3283
+ uint32_t i;
3284
+
3285
+ br.bytes = bytes;
3286
+ br.length = length;
3287
+
3288
+ numprogs = 0;
3289
+ for (prog = filters->progs; prog; prog = prog->next)
3290
+ numprogs++;
3291
+
3292
+ if ((flags & 0x80))
3293
+ {
3294
+ num = membr_next_rarvm_number(&br);
3295
+ if (num == 0)
3296
+ {
3297
+ delete_filter(filters->stack);
3298
+ filters->stack = NULL;
3299
+ delete_program_code(filters->progs);
3300
+ filters->progs = NULL;
3301
+ }
3302
+ else
3303
+ num--;
3304
+ if (num > numprogs) {
3305
+ return 0;
3306
+ }
3307
+ filters->lastfilternum = num;
3308
+ }
3309
+ else
3310
+ num = filters->lastfilternum;
3311
+
3312
+ prog = filters->progs;
3313
+ for (i = 0; i < num; i++)
3314
+ prog = prog->next;
3315
+ if (prog)
3316
+ prog->usagecount++;
3317
+
3318
+ blockstartpos = membr_next_rarvm_number(&br) + (size_t)lzss_position(&rar->lzss);
3319
+ if ((flags & 0x40))
3320
+ blockstartpos += 258;
3321
+ if ((flags & 0x20))
3322
+ blocklength = membr_next_rarvm_number(&br);
3323
+ else
3324
+ blocklength = prog ? prog->oldfilterlength : 0;
3325
+
3326
+ if (blocklength > rar->dictionary_size ||
3327
+ blocklength > (uint32_t)(rar->lzss.mask + 1))
3328
+ return 0;
3329
+
3330
+ registers[3] = PROGRAM_SYSTEM_GLOBAL_ADDRESS;
3331
+ registers[4] = blocklength;
3332
+ registers[5] = prog ? prog->usagecount : 0;
3333
+ registers[7] = VM_MEMORY_SIZE;
3334
+
3335
+ if ((flags & 0x10))
3336
+ {
3337
+ uint8_t mask = (uint8_t)membr_bits(&br, 7);
3338
+ for (i = 0; i < 7; i++)
3339
+ if ((mask & (1 << i)))
3340
+ registers[i] = membr_next_rarvm_number(&br);
3341
+ }
3342
+
3343
+ if (!prog)
3344
+ {
3345
+ uint32_t len = membr_next_rarvm_number(&br);
3346
+ uint8_t *bytecode;
3347
+ struct rar_program_code **next;
3348
+
3349
+ if (len == 0 || len > 0x10000)
3350
+ return 0;
3351
+ bytecode = malloc(len);
3352
+ if (!bytecode)
3353
+ return 0;
3354
+ for (i = 0; i < len; i++)
3355
+ bytecode[i] = (uint8_t)membr_bits(&br, 8);
3356
+ prog = compile_program(bytecode, len);
3357
+ if (!prog) {
3358
+ free(bytecode);
3359
+ return 0;
3360
+ }
3361
+ free(bytecode);
3362
+ next = &filters->progs;
3363
+ while (*next)
3364
+ next = &(*next)->next;
3365
+ *next = prog;
3366
+ }
3367
+ prog->oldfilterlength = blocklength;
3368
+
3369
+ globaldata = NULL;
3370
+ globaldatalen = 0;
3371
+ if ((flags & 0x08))
3372
+ {
3373
+ globaldatalen = membr_next_rarvm_number(&br);
3374
+ if (globaldatalen > PROGRAM_USER_GLOBAL_SIZE)
3375
+ return 0;
3376
+ globaldata = malloc(globaldatalen + PROGRAM_SYSTEM_GLOBAL_SIZE);
3377
+ if (!globaldata)
3378
+ return 0;
3379
+ for (i = 0; i < globaldatalen; i++)
3380
+ globaldata[i + PROGRAM_SYSTEM_GLOBAL_SIZE] = (uint8_t)membr_bits(&br, 8);
3381
+ }
3382
+
3383
+ if (br.at_eof)
3384
+ {
3385
+ free(globaldata);
3386
+ return 0;
3387
+ }
3388
+
3389
+ filter = create_filter(prog, globaldata, globaldatalen, registers, blockstartpos, blocklength);
3390
+ free(globaldata);
3391
+ if (!filter)
3392
+ return 0;
3393
+
3394
+ for (i = 0; i < 7; i++)
3395
+ archive_le32enc(&filter->globaldata[i * 4], registers[i]);
3396
+ archive_le32enc(&filter->globaldata[0x1C], blocklength);
3397
+ archive_le32enc(&filter->globaldata[0x20], 0);
3398
+ archive_le32enc(&filter->globaldata[0x2C], prog->usagecount);
3399
+
3400
+ nextfilter = &filters->stack;
3401
+ while (*nextfilter)
3402
+ nextfilter = &(*nextfilter)->next;
3403
+ *nextfilter = filter;
3404
+
3405
+ if (!filters->stack->next)
3406
+ filters->filterstart = blockstartpos;
3407
+
3408
+ return 1;
3409
+ }
3410
+
3411
+ static struct rar_filter *
3412
+ create_filter(struct rar_program_code *prog, const uint8_t *globaldata, uint32_t globaldatalen, uint32_t registers[8], size_t startpos, uint32_t length)
3413
+ {
3414
+ struct rar_filter *filter;
3415
+
3416
+ filter = calloc(1, sizeof(*filter));
3417
+ if (!filter)
3418
+ return NULL;
3419
+ filter->prog = prog;
3420
+ filter->globaldatalen = globaldatalen > PROGRAM_SYSTEM_GLOBAL_SIZE ? globaldatalen : PROGRAM_SYSTEM_GLOBAL_SIZE;
3421
+ filter->globaldata = calloc(1, filter->globaldatalen);
3422
+ if (!filter->globaldata)
3423
+ {
3424
+ free(filter);
3425
+ return NULL;
3426
+ }
3427
+ if (globaldata)
3428
+ memcpy(filter->globaldata, globaldata, globaldatalen);
3429
+ if (registers)
3430
+ memcpy(filter->initialregisters, registers, sizeof(filter->initialregisters));
3431
+ filter->blockstartpos = startpos;
3432
+ filter->blocklength = length;
3433
+
3434
+ return filter;
3435
+ }
3436
+
3437
+ static int
3438
+ run_filters(struct archive_read *a)
3439
+ {
3440
+ struct rar *rar = a->format->data;
3441
+ struct rar_filters *filters = &rar->filters;
3442
+ struct rar_filter *filter = filters->stack;
3443
+ struct rar_filter *f;
3444
+ size_t start, end;
3445
+ int64_t tend;
3446
+ uint32_t lastfilteraddress;
3447
+ uint32_t lastfilterlength;
3448
+ int ret;
3449
+
3450
+ if (filters == NULL || filter == NULL)
3451
+ return (0);
3452
+
3453
+ start = (size_t)filters->filterstart;
3454
+ end = start + filter->blocklength;
3455
+
3456
+ filters->filterstart = INT64_MAX;
3457
+ tend = (int64_t)end;
3458
+ ret = expand(a, &tend);
3459
+ if (ret != ARCHIVE_OK)
3460
+ return 0;
3461
+
3462
+ /* Check if filter stack was modified in expand() */
3463
+ ret = ARCHIVE_FATAL;
3464
+ f = filters->stack;
3465
+ while (f)
3466
+ {
3467
+ if (f == filter)
3468
+ {
3469
+ ret = ARCHIVE_OK;
3470
+ break;
3471
+ }
3472
+ f = f->next;
3473
+ }
3474
+ if (ret != ARCHIVE_OK)
3475
+ return 0;
3476
+
3477
+ if (tend < 0)
3478
+ return 0;
3479
+ end = (size_t)tend;
3480
+ if (end != start + filter->blocklength)
3481
+ return 0;
3482
+
3483
+ if (!filters->vm)
3484
+ {
3485
+ filters->vm = calloc(1, sizeof(*filters->vm));
3486
+ if (!filters->vm)
3487
+ return 0;
3488
+ }
3489
+
3490
+ if (filter->blocklength > VM_MEMORY_SIZE)
3491
+ {
3492
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Bad RAR file data");
3493
+ return 0;
3494
+ }
3495
+
3496
+ ret = copy_from_lzss_window(a, filters->vm->memory, start, filter->blocklength);
3497
+ if (ret != ARCHIVE_OK)
3498
+ return 0;
3499
+ if (!execute_filter(a, filter, filters->vm, (size_t)rar->offset))
3500
+ return 0;
3501
+
3502
+ lastfilteraddress = filter->filteredblockaddress;
3503
+ lastfilterlength = filter->filteredblocklength;
3504
+ filters->stack = filter->next;
3505
+ filter->next = NULL;
3506
+ delete_filter(filter);
3507
+
3508
+ while ((filter = filters->stack) != NULL && (int64_t)filter->blockstartpos == filters->filterstart && filter->blocklength == lastfilterlength)
3509
+ {
3510
+ memmove(&filters->vm->memory[0], &filters->vm->memory[lastfilteraddress], lastfilterlength);
3511
+ if (!execute_filter(a, filter, filters->vm, (size_t)rar->offset))
3512
+ return 0;
3513
+
3514
+ lastfilteraddress = filter->filteredblockaddress;
3515
+ lastfilterlength = filter->filteredblocklength;
3516
+ filters->stack = filter->next;
3517
+ filter->next = NULL;
3518
+ delete_filter(filter);
3519
+ }
3520
+
3521
+ if (filters->stack)
3522
+ {
3523
+ if (filters->stack->blockstartpos < end)
3524
+ return 0;
3525
+ filters->filterstart = filters->stack->blockstartpos;
3526
+ }
3527
+
3528
+ filters->lastend = end;
3529
+ filters->bytes = &filters->vm->memory[lastfilteraddress];
3530
+ filters->bytes_ready = lastfilterlength;
3531
+
3532
+ return 1;
3533
+ }
3534
+
3535
+ static struct rar_program_code *
3536
+ compile_program(const uint8_t *bytes, size_t length)
3537
+ {
3538
+ struct memory_bit_reader br = { 0 };
3539
+ struct rar_program_code *prog;
3540
+ // uint32_t instrcount = 0;
3541
+ uint8_t xor;
3542
+ size_t i;
3543
+
3544
+ xor = 0;
3545
+ for (i = 1; i < length; i++)
3546
+ xor ^= bytes[i];
3547
+ if (!length || xor != bytes[0])
3548
+ return NULL;
3549
+
3550
+ br.bytes = bytes;
3551
+ br.length = length;
3552
+ br.offset = 1;
3553
+
3554
+ prog = calloc(1, sizeof(*prog));
3555
+ if (!prog)
3556
+ return NULL;
3557
+ prog->fingerprint = crc32(0, bytes, (unsigned int)length) | ((uint64_t)length << 32);
3558
+
3559
+ if (membr_bits(&br, 1))
3560
+ {
3561
+ uint32_t staticdatalen = membr_next_rarvm_number(&br);
3562
+ if (staticdatalen >= VM_MEMORY_SIZE)
3563
+ {
3564
+ delete_program_code(prog);
3565
+ return NULL;
3566
+ }
3567
+ prog->staticdatalen = staticdatalen + 1;
3568
+ prog->staticdata = malloc(prog->staticdatalen);
3569
+ if (!prog->staticdata)
3570
+ {
3571
+ delete_program_code(prog);
3572
+ return NULL;
3573
+ }
3574
+ for (i = 0; i < prog->staticdatalen; i++)
3575
+ prog->staticdata[i] = (uint8_t)membr_bits(&br, 8);
3576
+ }
3577
+
3578
+ return prog;
3579
+ }
3580
+
3581
+ static void
3582
+ delete_filter(struct rar_filter *filter)
3583
+ {
3584
+ while (filter)
3585
+ {
3586
+ struct rar_filter *next = filter->next;
3587
+ free(filter->globaldata);
3588
+ free(filter);
3589
+ filter = next;
3590
+ }
3591
+ }
3592
+
3593
+ static void
3594
+ clear_filters(struct rar_filters *filters)
3595
+ {
3596
+ delete_filter(filters->stack);
3597
+ delete_program_code(filters->progs);
3598
+ free(filters->vm);
3599
+ }
3600
+
3601
+ static void
3602
+ delete_program_code(struct rar_program_code *prog)
3603
+ {
3604
+ while (prog)
3605
+ {
3606
+ struct rar_program_code *next = prog->next;
3607
+ free(prog->staticdata);
3608
+ free(prog);
3609
+ prog = next;
3610
+ }
3611
+ }
3612
+
3613
+ static uint32_t
3614
+ membr_next_rarvm_number(struct memory_bit_reader *br)
3615
+ {
3616
+ uint32_t val;
3617
+ switch (membr_bits(br, 2))
3618
+ {
3619
+ case 0:
3620
+ return membr_bits(br, 4);
3621
+ case 1:
3622
+ val = membr_bits(br, 8);
3623
+ if (val >= 16)
3624
+ return val;
3625
+ return 0xFFFFFF00 | (val << 4) | membr_bits(br, 4);
3626
+ case 2:
3627
+ return membr_bits(br, 16);
3628
+ default:
3629
+ return membr_bits(br, 32);
3630
+ }
3631
+ }
3632
+
3633
+ static inline uint32_t
3634
+ membr_bits(struct memory_bit_reader *br, int bits)
3635
+ {
3636
+ if (bits > br->available && (br->at_eof || !membr_fill(br, bits)))
3637
+ return 0;
3638
+ return (uint32_t)((br->bits >> (br->available -= bits)) & (((uint64_t)1 << bits) - 1));
3639
+ }
3640
+
3641
+ static int
3642
+ membr_fill(struct memory_bit_reader *br, int bits)
3643
+ {
3644
+ while (br->available < bits && br->offset < br->length)
3645
+ {
3646
+ br->bits = (br->bits << 8) | br->bytes[br->offset++];
3647
+ br->available += 8;
3648
+ }
3649
+ if (bits > br->available)
3650
+ {
3651
+ br->at_eof = 1;
3652
+ return 0;
3653
+ }
3654
+ return 1;
3655
+ }
3656
+
3657
+ static int
3658
+ read_filter(struct archive_read *a, int64_t *end)
3659
+ {
3660
+ struct rar *rar = a->format->data;
3661
+ uint8_t flags, val, *code;
3662
+ uint16_t length, i;
3663
+
3664
+ if (!rar_decode_byte(a, &flags))
3665
+ return 0;
3666
+ length = (flags & 0x07) + 1;
3667
+ if (length == 7)
3668
+ {
3669
+ if (!rar_decode_byte(a, &val))
3670
+ return 0;
3671
+ length = val + 7;
3672
+ }
3673
+ else if (length == 8)
3674
+ {
3675
+ if (!rar_decode_byte(a, &val))
3676
+ return 0;
3677
+ length = val << 8;
3678
+ if (!rar_decode_byte(a, &val))
3679
+ return 0;
3680
+ length |= val;
3681
+ }
3682
+
3683
+ code = malloc(length);
3684
+ if (!code)
3685
+ return 0;
3686
+ for (i = 0; i < length; i++)
3687
+ {
3688
+ if (!rar_decode_byte(a, &code[i]))
3689
+ {
3690
+ free(code);
3691
+ return 0;
3692
+ }
3693
+ }
3694
+ if (!parse_filter(a, code, length, flags))
3695
+ {
3696
+ free(code);
3697
+ return 0;
3698
+ }
3699
+ free(code);
3700
+
3701
+ if (rar->filters.filterstart < *end)
3702
+ *end = rar->filters.filterstart;
3703
+
3704
+ return 1;
3705
+ }
3706
+
3707
+ static int
3708
+ execute_filter_delta(struct rar_filter *filter, struct rar_virtual_machine *vm)
3709
+ {
3710
+ uint32_t length = filter->initialregisters[4];
3711
+ uint32_t numchannels = filter->initialregisters[0];
3712
+ uint8_t *src, *dst;
3713
+ uint32_t i, idx;
3714
+
3715
+ if (length > PROGRAM_WORK_SIZE / 2)
3716
+ return 0;
3717
+
3718
+ src = &vm->memory[0];
3719
+ dst = &vm->memory[length];
3720
+ for (i = 0; i < numchannels; i++)
3721
+ {
3722
+ uint8_t lastbyte = 0;
3723
+ for (idx = i; idx < length; idx += numchannels)
3724
+ {
3725
+ /*
3726
+ * The src block should not overlap with the dst block.
3727
+ * If so it would be better to consider this archive is broken.
3728
+ */
3729
+ if (src >= dst)
3730
+ return 0;
3731
+ lastbyte = dst[idx] = lastbyte - *src++;
3732
+ }
3733
+ }
3734
+
3735
+ filter->filteredblockaddress = length;
3736
+ filter->filteredblocklength = length;
3737
+
3738
+ return 1;
3739
+ }
3740
+
3741
+ static int
3742
+ execute_filter_e8(struct rar_filter *filter, struct rar_virtual_machine *vm, size_t pos, int e9also)
3743
+ {
3744
+ uint32_t length = filter->initialregisters[4];
3745
+ uint32_t filesize = 0x1000000;
3746
+ uint32_t i;
3747
+
3748
+ if (length > PROGRAM_WORK_SIZE || length <= 4)
3749
+ return 0;
3750
+
3751
+ for (i = 0; i <= length - 5; i++)
3752
+ {
3753
+ if (vm->memory[i] == 0xE8 || (e9also && vm->memory[i] == 0xE9))
3754
+ {
3755
+ uint32_t currpos = (uint32_t)pos + i + 1;
3756
+ int32_t address = (int32_t)vm_read_32(vm, i + 1);
3757
+ if (address < 0 && currpos >= (~(uint32_t)address + 1))
3758
+ vm_write_32(vm, i + 1, address + filesize);
3759
+ else if (address >= 0 && (uint32_t)address < filesize)
3760
+ vm_write_32(vm, i + 1, address - currpos);
3761
+ i += 4;
3762
+ }
3763
+ }
3764
+
3765
+ filter->filteredblockaddress = 0;
3766
+ filter->filteredblocklength = length;
3767
+
3768
+ return 1;
3769
+ }
3770
+
3771
+ static int
3772
+ execute_filter_rgb(struct rar_filter *filter, struct rar_virtual_machine *vm)
3773
+ {
3774
+ uint32_t stride = filter->initialregisters[0];
3775
+ uint32_t byteoffset = filter->initialregisters[1];
3776
+ uint32_t blocklength = filter->initialregisters[4];
3777
+ uint8_t *src, *dst;
3778
+ uint32_t i, j;
3779
+
3780
+ if (blocklength > PROGRAM_WORK_SIZE / 2 || stride > blocklength || blocklength < 3 || byteoffset > 2)
3781
+ return 0;
3782
+
3783
+ src = &vm->memory[0];
3784
+ dst = &vm->memory[blocklength];
3785
+ for (i = 0; i < 3; i++) {
3786
+ uint8_t byte = 0;
3787
+ uint8_t *prev = dst + i - stride;
3788
+ for (j = i; j < blocklength; j += 3)
3789
+ {
3790
+ /*
3791
+ * The src block should not overlap with the dst block.
3792
+ * If so it would be better to consider this archive is broken.
3793
+ */
3794
+ if (src >= dst)
3795
+ return 0;
3796
+
3797
+ if (prev >= dst)
3798
+ {
3799
+ uint32_t delta1 = abs(prev[3] - prev[0]);
3800
+ uint32_t delta2 = abs(byte - prev[0]);
3801
+ uint32_t delta3 = abs(prev[3] - prev[0] + byte - prev[0]);
3802
+ if (delta1 > delta2 || delta1 > delta3)
3803
+ byte = delta2 <= delta3 ? prev[3] : prev[0];
3804
+ }
3805
+ byte -= *src++;
3806
+ dst[j] = byte;
3807
+ prev += 3;
3808
+ }
3809
+ }
3810
+ for (i = byteoffset; i < blocklength - 2; i += 3)
3811
+ {
3812
+ dst[i] += dst[i + 1];
3813
+ dst[i + 2] += dst[i + 1];
3814
+ }
3815
+
3816
+ filter->filteredblockaddress = blocklength;
3817
+ filter->filteredblocklength = blocklength;
3818
+
3819
+ return 1;
3820
+ }
3821
+
3822
+ static int
3823
+ execute_filter_audio(struct rar_filter *filter, struct rar_virtual_machine *vm)
3824
+ {
3825
+ uint32_t length = filter->initialregisters[4];
3826
+ uint32_t numchannels = filter->initialregisters[0];
3827
+ uint8_t *src, *dst;
3828
+ uint32_t i, j;
3829
+
3830
+ if (length > PROGRAM_WORK_SIZE / 2)
3831
+ return 0;
3832
+
3833
+ src = &vm->memory[0];
3834
+ dst = &vm->memory[length];
3835
+ for (i = 0; i < numchannels; i++)
3836
+ {
3837
+ struct audio_state state;
3838
+ memset(&state, 0, sizeof(state));
3839
+ for (j = i; j < length; j += numchannels)
3840
+ {
3841
+ /*
3842
+ * The src block should not overlap with the dst block.
3843
+ * If so it would be better to consider this archive is broken.
3844
+ */
3845
+ if (src >= dst)
3846
+ return 0;
3847
+
3848
+ int8_t delta = (int8_t)*src++;
3849
+ uint8_t predbyte, byte;
3850
+ int prederror;
3851
+ state.delta[2] = state.delta[1];
3852
+ state.delta[1] = state.lastdelta - state.delta[0];
3853
+ state.delta[0] = state.lastdelta;
3854
+ predbyte = ((8 * state.lastbyte + state.weight[0] * state.delta[0] + state.weight[1] * state.delta[1] + state.weight[2] * state.delta[2]) >> 3) & 0xFF;
3855
+ byte = (predbyte - delta) & 0xFF;
3856
+ prederror = delta * 8;
3857
+ state.error[0] += abs(prederror);
3858
+ state.error[1] += abs(prederror - state.delta[0]); state.error[2] += abs(prederror + state.delta[0]);
3859
+ state.error[3] += abs(prederror - state.delta[1]); state.error[4] += abs(prederror + state.delta[1]);
3860
+ state.error[5] += abs(prederror - state.delta[2]); state.error[6] += abs(prederror + state.delta[2]);
3861
+ state.lastdelta = (int8_t)(byte - state.lastbyte);
3862
+ dst[j] = state.lastbyte = byte;
3863
+ if (!(state.count++ & 0x1F))
3864
+ {
3865
+ uint8_t k, idx = 0;
3866
+ for (k = 1; k < 7; k++)
3867
+ {
3868
+ if (state.error[k] < state.error[idx])
3869
+ idx = k;
3870
+ }
3871
+ memset(state.error, 0, sizeof(state.error));
3872
+ switch (idx)
3873
+ {
3874
+ case 1: if (state.weight[0] >= -16) state.weight[0]--; break;
3875
+ case 2: if (state.weight[0] < 16) state.weight[0]++; break;
3876
+ case 3: if (state.weight[1] >= -16) state.weight[1]--; break;
3877
+ case 4: if (state.weight[1] < 16) state.weight[1]++; break;
3878
+ case 5: if (state.weight[2] >= -16) state.weight[2]--; break;
3879
+ case 6: if (state.weight[2] < 16) state.weight[2]++; break;
3880
+ }
3881
+ }
3882
+ }
3883
+ }
3884
+
3885
+ filter->filteredblockaddress = length;
3886
+ filter->filteredblocklength = length;
3887
+
3888
+ return 1;
3889
+ }
3890
+
3891
+
3892
+ static int
3893
+ execute_filter(struct archive_read *a, struct rar_filter *filter, struct rar_virtual_machine *vm, size_t pos)
3894
+ {
3895
+ if (filter->prog->fingerprint == 0x1D0E06077D)
3896
+ return execute_filter_delta(filter, vm);
3897
+ if (filter->prog->fingerprint == 0x35AD576887)
3898
+ return execute_filter_e8(filter, vm, pos, 0);
3899
+ if (filter->prog->fingerprint == 0x393CD7E57E)
3900
+ return execute_filter_e8(filter, vm, pos, 1);
3901
+ if (filter->prog->fingerprint == 0x951C2C5DC8)
3902
+ return execute_filter_rgb(filter, vm);
3903
+ if (filter->prog->fingerprint == 0xD8BC85E701)
3904
+ return execute_filter_audio(filter, vm);
3905
+
3906
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "No support for RAR VM program filter");
3907
+ return 0;
3908
+ }
3909
+
3910
+ static int
3911
+ rar_decode_byte(struct archive_read *a, uint8_t *byte)
3912
+ {
3913
+ struct rar *rar = a->format->data;
3914
+ struct rar_br *br = &(rar->br);
3915
+ if (!rar_br_read_ahead(a, br, 8))
3916
+ return 0;
3917
+ *byte = (uint8_t)rar_br_bits(br, 8);
3918
+ rar_br_consume(br, 8);
3919
+ return 1;
3920
+ }
3921
+
3922
+ static inline void
3923
+ vm_write_32(struct rar_virtual_machine* vm, size_t offset, uint32_t u32)
3924
+ {
3925
+ archive_le32enc(vm->memory + offset, u32);
3926
+ }
3927
+
3928
+ static inline uint32_t
3929
+ vm_read_32(struct rar_virtual_machine* vm, size_t offset)
3930
+ {
3931
+ return archive_le32dec(vm->memory + offset);
3932
+ }