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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (276) hide show
  1. package/PapyrusNativeEngine.podspec +45 -0
  2. package/android/build.gradle +1 -0
  3. package/android/src/main/cpp/CMakeLists.txt +137 -0
  4. package/android/src/main/cpp/PapyrusComicArchiveJni.cpp +68 -0
  5. package/android/src/main/cpp/PapyrusComicArchiveTest.cpp +55 -0
  6. package/android/src/main/cpp/papyrus_outline_loader_test.cpp +240 -0
  7. package/android/src/main/cpp/papyrus_text_word_test.cpp +72 -0
  8. package/android/src/main/java/com/papyrus/engine/PapyrusComicArchiveNative.java +14 -0
  9. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentView.java +491 -0
  10. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentViewManager.java +32 -0
  11. package/android/src/main/java/com/papyrus/engine/PapyrusComicPageLayout.java +38 -0
  12. package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +305 -35
  13. package/android/src/main/java/com/papyrus/engine/PapyrusNativeTextModel.java +195 -0
  14. package/android/src/main/java/com/papyrus/engine/PapyrusPackage.java +3 -1
  15. package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerView.java +195 -5
  16. package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerViewManager.java +20 -1
  17. package/android/src/main/java/com/papyrus/engine/PapyrusSelectionMenuIcon.java +48 -0
  18. package/android/src/main/java/com/papyrus/engine/PapyrusTextAnnotationModel.java +22 -0
  19. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentView.java +661 -0
  20. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentViewManager.java +49 -0
  21. package/android/src/main/java/com/papyrus/engine/PapyrusTextStore.java +43 -0
  22. package/dist/index.d.mts +614 -4
  23. package/dist/index.d.ts +614 -4
  24. package/dist/index.js +484 -8
  25. package/dist/index.js.map +1 -1
  26. package/dist/index.mjs +477 -7
  27. package/dist/index.mjs.map +1 -1
  28. package/ios/PapyrusComicDocumentView.h +21 -0
  29. package/ios/PapyrusComicDocumentView.m +554 -0
  30. package/ios/PapyrusComicDocumentViewManager.m +23 -0
  31. package/ios/PapyrusEngineStore.h +3 -0
  32. package/ios/PapyrusEngineStore.m +32 -6
  33. package/ios/PapyrusNativeEngine.m +684 -5
  34. package/ios/PapyrusPageRotationRegistry.h +29 -0
  35. package/ios/PapyrusPageRotationRegistry.m +179 -0
  36. package/ios/PapyrusPageView.m +38 -11
  37. package/ios/PapyrusPdfDocumentView.h +55 -0
  38. package/ios/PapyrusPdfDocumentView.m +2370 -0
  39. package/ios/PapyrusPdfDocumentViewManager.m +61 -0
  40. package/ios/PapyrusPdfPageTheme.h +15 -0
  41. package/ios/PapyrusPdfPageTheme.m +218 -0
  42. package/ios/PapyrusSelectionMenu.h +27 -0
  43. package/ios/PapyrusTextAnnotationRange.h +2 -0
  44. package/ios/PapyrusTextAnnotationRange.m +29 -0
  45. package/ios/PapyrusTextDocumentView.h +32 -0
  46. package/ios/PapyrusTextDocumentView.m +518 -0
  47. package/ios/PapyrusTextDocumentViewManager.m +34 -0
  48. package/ios/PapyrusTextStore.h +13 -0
  49. package/ios/PapyrusTextStore.m +73 -0
  50. package/package.json +12 -10
  51. package/react-native.config.js +1 -1
  52. package/third_party_licenses/BLAKE2-NOTICE.txt +11 -0
  53. package/third_party_licenses/libarchive-COPYING +65 -0
  54. package/vendor/libarchive/PapyrusComicArchive.cpp +376 -0
  55. package/vendor/libarchive/PapyrusComicArchive.h +30 -0
  56. package/vendor/libarchive/README.papyrus.md +15 -0
  57. package/vendor/libarchive/android.h +192 -0
  58. package/vendor/libarchive/config/PapyrusArchiveConfig.h +71 -0
  59. package/vendor/libarchive/libarchive/CMakeLists.txt +293 -0
  60. package/vendor/libarchive/libarchive/android_lf.h +36 -0
  61. package/vendor/libarchive/libarchive/archive.h +1266 -0
  62. package/vendor/libarchive/libarchive/archive_acl.c +2025 -0
  63. package/vendor/libarchive/libarchive/archive_acl_private.h +85 -0
  64. package/vendor/libarchive/libarchive/archive_blake2.h +197 -0
  65. package/vendor/libarchive/libarchive/archive_blake2_impl.h +161 -0
  66. package/vendor/libarchive/libarchive/archive_blake2s_ref.c +369 -0
  67. package/vendor/libarchive/libarchive/archive_blake2sp_ref.c +361 -0
  68. package/vendor/libarchive/libarchive/archive_check_magic.c +183 -0
  69. package/vendor/libarchive/libarchive/archive_cmdline.c +211 -0
  70. package/vendor/libarchive/libarchive/archive_cmdline_private.h +45 -0
  71. package/vendor/libarchive/libarchive/archive_crc32.h +123 -0
  72. package/vendor/libarchive/libarchive/archive_cryptor.c +534 -0
  73. package/vendor/libarchive/libarchive/archive_cryptor_private.h +199 -0
  74. package/vendor/libarchive/libarchive/archive_digest.c +1584 -0
  75. package/vendor/libarchive/libarchive/archive_digest_private.h +431 -0
  76. package/vendor/libarchive/libarchive/archive_disk_acl_darwin.c +558 -0
  77. package/vendor/libarchive/libarchive/archive_disk_acl_freebsd.c +712 -0
  78. package/vendor/libarchive/libarchive/archive_disk_acl_linux.c +760 -0
  79. package/vendor/libarchive/libarchive/archive_disk_acl_sunos.c +824 -0
  80. package/vendor/libarchive/libarchive/archive_endian.h +234 -0
  81. package/vendor/libarchive/libarchive/archive_entry.3 +147 -0
  82. package/vendor/libarchive/libarchive/archive_entry.c +2297 -0
  83. package/vendor/libarchive/libarchive/archive_entry.h +763 -0
  84. package/vendor/libarchive/libarchive/archive_entry_acl.3 +458 -0
  85. package/vendor/libarchive/libarchive/archive_entry_copy_bhfi.c +55 -0
  86. package/vendor/libarchive/libarchive/archive_entry_copy_stat.c +82 -0
  87. package/vendor/libarchive/libarchive/archive_entry_link_resolver.c +472 -0
  88. package/vendor/libarchive/libarchive/archive_entry_linkify.3 +224 -0
  89. package/vendor/libarchive/libarchive/archive_entry_locale.h +90 -0
  90. package/vendor/libarchive/libarchive/archive_entry_misc.3 +63 -0
  91. package/vendor/libarchive/libarchive/archive_entry_paths.3 +153 -0
  92. package/vendor/libarchive/libarchive/archive_entry_perms.3 +210 -0
  93. package/vendor/libarchive/libarchive/archive_entry_private.h +205 -0
  94. package/vendor/libarchive/libarchive/archive_entry_sparse.c +156 -0
  95. package/vendor/libarchive/libarchive/archive_entry_stat.3 +274 -0
  96. package/vendor/libarchive/libarchive/archive_entry_stat.c +121 -0
  97. package/vendor/libarchive/libarchive/archive_entry_strmode.c +69 -0
  98. package/vendor/libarchive/libarchive/archive_entry_time.3 +127 -0
  99. package/vendor/libarchive/libarchive/archive_entry_xattr.c +155 -0
  100. package/vendor/libarchive/libarchive/archive_hmac.c +345 -0
  101. package/vendor/libarchive/libarchive/archive_hmac_private.h +119 -0
  102. package/vendor/libarchive/libarchive/archive_integer.h +267 -0
  103. package/vendor/libarchive/libarchive/archive_match.c +1869 -0
  104. package/vendor/libarchive/libarchive/archive_openssl_evp_private.h +54 -0
  105. package/vendor/libarchive/libarchive/archive_openssl_hmac_private.h +54 -0
  106. package/vendor/libarchive/libarchive/archive_options.c +219 -0
  107. package/vendor/libarchive/libarchive/archive_options_private.h +49 -0
  108. package/vendor/libarchive/libarchive/archive_pack_dev.c +330 -0
  109. package/vendor/libarchive/libarchive/archive_pack_dev.h +49 -0
  110. package/vendor/libarchive/libarchive/archive_parse_date.c +1173 -0
  111. package/vendor/libarchive/libarchive/archive_pathmatch.c +480 -0
  112. package/vendor/libarchive/libarchive/archive_pathmatch.h +50 -0
  113. package/vendor/libarchive/libarchive/archive_platform.h +213 -0
  114. package/vendor/libarchive/libarchive/archive_platform_acl.h +53 -0
  115. package/vendor/libarchive/libarchive/archive_platform_stat.h +45 -0
  116. package/vendor/libarchive/libarchive/archive_platform_xattr.h +45 -0
  117. package/vendor/libarchive/libarchive/archive_ppmd7.c +1173 -0
  118. package/vendor/libarchive/libarchive/archive_ppmd7_private.h +119 -0
  119. package/vendor/libarchive/libarchive/archive_ppmd8.c +1293 -0
  120. package/vendor/libarchive/libarchive/archive_ppmd8_private.h +123 -0
  121. package/vendor/libarchive/libarchive/archive_ppmd_private.h +159 -0
  122. package/vendor/libarchive/libarchive/archive_private.h +209 -0
  123. package/vendor/libarchive/libarchive/archive_random.c +274 -0
  124. package/vendor/libarchive/libarchive/archive_random_private.h +36 -0
  125. package/vendor/libarchive/libarchive/archive_rb.c +709 -0
  126. package/vendor/libarchive/libarchive/archive_rb.h +113 -0
  127. package/vendor/libarchive/libarchive/archive_read.3 +250 -0
  128. package/vendor/libarchive/libarchive/archive_read.c +1814 -0
  129. package/vendor/libarchive/libarchive/archive_read_add_passphrase.3 +72 -0
  130. package/vendor/libarchive/libarchive/archive_read_add_passphrase.c +189 -0
  131. package/vendor/libarchive/libarchive/archive_read_append_filter.c +209 -0
  132. package/vendor/libarchive/libarchive/archive_read_data.3 +128 -0
  133. package/vendor/libarchive/libarchive/archive_read_data_into_fd.c +207 -0
  134. package/vendor/libarchive/libarchive/archive_read_disk.3 +422 -0
  135. package/vendor/libarchive/libarchive/archive_read_disk_entry_from_file.c +1086 -0
  136. package/vendor/libarchive/libarchive/archive_read_disk_posix.c +2639 -0
  137. package/vendor/libarchive/libarchive/archive_read_disk_private.h +97 -0
  138. package/vendor/libarchive/libarchive/archive_read_disk_set_standard_lookup.c +315 -0
  139. package/vendor/libarchive/libarchive/archive_read_disk_windows.c +2603 -0
  140. package/vendor/libarchive/libarchive/archive_read_extract.3 +135 -0
  141. package/vendor/libarchive/libarchive/archive_read_extract.c +59 -0
  142. package/vendor/libarchive/libarchive/archive_read_extract2.c +154 -0
  143. package/vendor/libarchive/libarchive/archive_read_filter.3 +163 -0
  144. package/vendor/libarchive/libarchive/archive_read_format.3 +190 -0
  145. package/vendor/libarchive/libarchive/archive_read_free.3 +91 -0
  146. package/vendor/libarchive/libarchive/archive_read_header.3 +89 -0
  147. package/vendor/libarchive/libarchive/archive_read_new.3 +57 -0
  148. package/vendor/libarchive/libarchive/archive_read_open.3 +247 -0
  149. package/vendor/libarchive/libarchive/archive_read_open_fd.c +236 -0
  150. package/vendor/libarchive/libarchive/archive_read_open_file.c +257 -0
  151. package/vendor/libarchive/libarchive/archive_read_open_filename.c +652 -0
  152. package/vendor/libarchive/libarchive/archive_read_open_memory.c +188 -0
  153. package/vendor/libarchive/libarchive/archive_read_private.h +274 -0
  154. package/vendor/libarchive/libarchive/archive_read_set_format.c +116 -0
  155. package/vendor/libarchive/libarchive/archive_read_set_options.3 +290 -0
  156. package/vendor/libarchive/libarchive/archive_read_set_options.c +132 -0
  157. package/vendor/libarchive/libarchive/archive_read_support_filter_all.c +84 -0
  158. package/vendor/libarchive/libarchive/archive_read_support_filter_by_code.c +68 -0
  159. package/vendor/libarchive/libarchive/archive_read_support_filter_bzip2.c +360 -0
  160. package/vendor/libarchive/libarchive/archive_read_support_filter_compress.c +457 -0
  161. package/vendor/libarchive/libarchive/archive_read_support_filter_grzip.c +108 -0
  162. package/vendor/libarchive/libarchive/archive_read_support_filter_gzip.c +538 -0
  163. package/vendor/libarchive/libarchive/archive_read_support_filter_lrzip.c +119 -0
  164. package/vendor/libarchive/libarchive/archive_read_support_filter_lz4.c +829 -0
  165. package/vendor/libarchive/libarchive/archive_read_support_filter_lzop.c +493 -0
  166. package/vendor/libarchive/libarchive/archive_read_support_filter_none.c +51 -0
  167. package/vendor/libarchive/libarchive/archive_read_support_filter_program.c +497 -0
  168. package/vendor/libarchive/libarchive/archive_read_support_filter_rpm.c +249 -0
  169. package/vendor/libarchive/libarchive/archive_read_support_filter_uu.c +739 -0
  170. package/vendor/libarchive/libarchive/archive_read_support_filter_xz.c +792 -0
  171. package/vendor/libarchive/libarchive/archive_read_support_filter_zstd.c +356 -0
  172. package/vendor/libarchive/libarchive/archive_read_support_format_7zip.c +4650 -0
  173. package/vendor/libarchive/libarchive/archive_read_support_format_all.c +88 -0
  174. package/vendor/libarchive/libarchive/archive_read_support_format_ar.c +597 -0
  175. package/vendor/libarchive/libarchive/archive_read_support_format_by_code.c +76 -0
  176. package/vendor/libarchive/libarchive/archive_read_support_format_cab.c +3410 -0
  177. package/vendor/libarchive/libarchive/archive_read_support_format_cpio.c +1152 -0
  178. package/vendor/libarchive/libarchive/archive_read_support_format_empty.c +95 -0
  179. package/vendor/libarchive/libarchive/archive_read_support_format_iso9660.c +3516 -0
  180. package/vendor/libarchive/libarchive/archive_read_support_format_lha.c +2931 -0
  181. package/vendor/libarchive/libarchive/archive_read_support_format_mtree.c +2113 -0
  182. package/vendor/libarchive/libarchive/archive_read_support_format_rar.c +3932 -0
  183. package/vendor/libarchive/libarchive/archive_read_support_format_rar5.c +4501 -0
  184. package/vendor/libarchive/libarchive/archive_read_support_format_raw.c +194 -0
  185. package/vendor/libarchive/libarchive/archive_read_support_format_tar.c +3785 -0
  186. package/vendor/libarchive/libarchive/archive_read_support_format_warc.c +875 -0
  187. package/vendor/libarchive/libarchive/archive_read_support_format_xar.c +3770 -0
  188. package/vendor/libarchive/libarchive/archive_read_support_format_zip.c +4710 -0
  189. package/vendor/libarchive/libarchive/archive_string.c +4391 -0
  190. package/vendor/libarchive/libarchive/archive_string.h +244 -0
  191. package/vendor/libarchive/libarchive/archive_string_composition.h +2289 -0
  192. package/vendor/libarchive/libarchive/archive_string_sprintf.c +200 -0
  193. package/vendor/libarchive/libarchive/archive_time.c +168 -0
  194. package/vendor/libarchive/libarchive/archive_time_private.h +47 -0
  195. package/vendor/libarchive/libarchive/archive_util.3 +239 -0
  196. package/vendor/libarchive/libarchive/archive_util.c +668 -0
  197. package/vendor/libarchive/libarchive/archive_version_details.c +516 -0
  198. package/vendor/libarchive/libarchive/archive_virtual.c +162 -0
  199. package/vendor/libarchive/libarchive/archive_windows.c +1035 -0
  200. package/vendor/libarchive/libarchive/archive_windows.h +332 -0
  201. package/vendor/libarchive/libarchive/archive_write.3 +266 -0
  202. package/vendor/libarchive/libarchive/archive_write.c +896 -0
  203. package/vendor/libarchive/libarchive/archive_write_add_filter.c +71 -0
  204. package/vendor/libarchive/libarchive/archive_write_add_filter_b64encode.c +329 -0
  205. package/vendor/libarchive/libarchive/archive_write_add_filter_by_name.c +76 -0
  206. package/vendor/libarchive/libarchive/archive_write_add_filter_bzip2.c +411 -0
  207. package/vendor/libarchive/libarchive/archive_write_add_filter_compress.c +459 -0
  208. package/vendor/libarchive/libarchive/archive_write_add_filter_grzip.c +143 -0
  209. package/vendor/libarchive/libarchive/archive_write_add_filter_gzip.c +493 -0
  210. package/vendor/libarchive/libarchive/archive_write_add_filter_lrzip.c +214 -0
  211. package/vendor/libarchive/libarchive/archive_write_add_filter_lz4.c +710 -0
  212. package/vendor/libarchive/libarchive/archive_write_add_filter_lzop.c +500 -0
  213. package/vendor/libarchive/libarchive/archive_write_add_filter_none.c +42 -0
  214. package/vendor/libarchive/libarchive/archive_write_add_filter_program.c +398 -0
  215. package/vendor/libarchive/libarchive/archive_write_add_filter_uuencode.c +320 -0
  216. package/vendor/libarchive/libarchive/archive_write_add_filter_xz.c +547 -0
  217. package/vendor/libarchive/libarchive/archive_write_add_filter_zstd.c +608 -0
  218. package/vendor/libarchive/libarchive/archive_write_blocksize.3 +112 -0
  219. package/vendor/libarchive/libarchive/archive_write_data.3 +88 -0
  220. package/vendor/libarchive/libarchive/archive_write_disk.3 +360 -0
  221. package/vendor/libarchive/libarchive/archive_write_disk_posix.c +4789 -0
  222. package/vendor/libarchive/libarchive/archive_write_disk_private.h +43 -0
  223. package/vendor/libarchive/libarchive/archive_write_disk_set_standard_lookup.c +265 -0
  224. package/vendor/libarchive/libarchive/archive_write_disk_windows.c +2952 -0
  225. package/vendor/libarchive/libarchive/archive_write_filter.3 +140 -0
  226. package/vendor/libarchive/libarchive/archive_write_finish_entry.3 +77 -0
  227. package/vendor/libarchive/libarchive/archive_write_format.3 +187 -0
  228. package/vendor/libarchive/libarchive/archive_write_free.3 +94 -0
  229. package/vendor/libarchive/libarchive/archive_write_header.3 +71 -0
  230. package/vendor/libarchive/libarchive/archive_write_new.3 +56 -0
  231. package/vendor/libarchive/libarchive/archive_write_open.3 +270 -0
  232. package/vendor/libarchive/libarchive/archive_write_open_fd.c +141 -0
  233. package/vendor/libarchive/libarchive/archive_write_open_file.c +102 -0
  234. package/vendor/libarchive/libarchive/archive_write_open_filename.c +272 -0
  235. package/vendor/libarchive/libarchive/archive_write_open_memory.c +110 -0
  236. package/vendor/libarchive/libarchive/archive_write_private.h +166 -0
  237. package/vendor/libarchive/libarchive/archive_write_set_format.c +124 -0
  238. package/vendor/libarchive/libarchive/archive_write_set_format_7zip.c +2571 -0
  239. package/vendor/libarchive/libarchive/archive_write_set_format_ar.c +540 -0
  240. package/vendor/libarchive/libarchive/archive_write_set_format_by_name.c +93 -0
  241. package/vendor/libarchive/libarchive/archive_write_set_format_cpio.c +11 -0
  242. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_binary.c +618 -0
  243. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_newc.c +457 -0
  244. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_odc.c +508 -0
  245. package/vendor/libarchive/libarchive/archive_write_set_format_filter_by_ext.c +141 -0
  246. package/vendor/libarchive/libarchive/archive_write_set_format_gnutar.c +790 -0
  247. package/vendor/libarchive/libarchive/archive_write_set_format_iso9660.c +8216 -0
  248. package/vendor/libarchive/libarchive/archive_write_set_format_mtree.c +2307 -0
  249. package/vendor/libarchive/libarchive/archive_write_set_format_pax.c +2249 -0
  250. package/vendor/libarchive/libarchive/archive_write_set_format_private.h +40 -0
  251. package/vendor/libarchive/libarchive/archive_write_set_format_raw.c +123 -0
  252. package/vendor/libarchive/libarchive/archive_write_set_format_shar.c +650 -0
  253. package/vendor/libarchive/libarchive/archive_write_set_format_ustar.c +768 -0
  254. package/vendor/libarchive/libarchive/archive_write_set_format_v7tar.c +648 -0
  255. package/vendor/libarchive/libarchive/archive_write_set_format_warc.c +491 -0
  256. package/vendor/libarchive/libarchive/archive_write_set_format_xar.c +3799 -0
  257. package/vendor/libarchive/libarchive/archive_write_set_format_zip.c +2548 -0
  258. package/vendor/libarchive/libarchive/archive_write_set_options.3 +779 -0
  259. package/vendor/libarchive/libarchive/archive_write_set_options.c +129 -0
  260. package/vendor/libarchive/libarchive/archive_write_set_passphrase.3 +72 -0
  261. package/vendor/libarchive/libarchive/archive_write_set_passphrase.c +95 -0
  262. package/vendor/libarchive/libarchive/archive_xxhash.h +48 -0
  263. package/vendor/libarchive/libarchive/config_freebsd.h +275 -0
  264. package/vendor/libarchive/libarchive/cpio.5 +389 -0
  265. package/vendor/libarchive/libarchive/filter_fork.h +44 -0
  266. package/vendor/libarchive/libarchive/filter_fork_posix.c +269 -0
  267. package/vendor/libarchive/libarchive/filter_fork_windows.c +270 -0
  268. package/vendor/libarchive/libarchive/libarchive-formats.5 +478 -0
  269. package/vendor/libarchive/libarchive/libarchive.3 +297 -0
  270. package/vendor/libarchive/libarchive/libarchive_changes.3 +339 -0
  271. package/vendor/libarchive/libarchive/libarchive_internals.3 +363 -0
  272. package/vendor/libarchive/libarchive/module.modulemap +34 -0
  273. package/vendor/libarchive/libarchive/mtree.5 +323 -0
  274. package/vendor/libarchive/libarchive/tar.5 +947 -0
  275. package/vendor/libarchive/libarchive/xxhash.c +531 -0
  276. package/ios/PapyrusNativeEngine.podspec +0 -15
@@ -0,0 +1,4710 @@
1
+ /*-
2
+ * Copyright (c) 2004-2013 Tim Kientzle
3
+ * Copyright (c) 2011-2012,2014 Michihiro NAKAJIMA
4
+ * Copyright (c) 2013 Konrad Kleine
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions
9
+ * are met:
10
+ * 1. Redistributions of source code must retain the above copyright
11
+ * notice, this list of conditions and the following disclaimer.
12
+ * 2. Redistributions in binary form must reproduce the above copyright
13
+ * notice, this list of conditions and the following disclaimer in the
14
+ * documentation and/or other materials provided with the distribution.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ #include "archive_platform.h"
29
+
30
+ /*
31
+ * The definitive documentation of the Zip file format is:
32
+ * http://www.pkware.com/documents/casestudies/APPNOTE.TXT
33
+ *
34
+ * The Info-Zip project has pioneered various extensions to better
35
+ * support Zip on Unix, including the 0x5455 "UT", 0x5855 "UX", 0x7855
36
+ * "Ux", and 0x7875 "ux" extensions for time and ownership
37
+ * information.
38
+ *
39
+ * History of this code: The streaming Zip reader was first added to
40
+ * libarchive in January 2005. Support for seekable input sources was
41
+ * added in Nov 2011. Zip64 support (including a significant code
42
+ * refactoring) was added in 2014.
43
+ */
44
+
45
+ #ifdef HAVE_ERRNO_H
46
+ #include <errno.h>
47
+ #endif
48
+ #ifdef HAVE_STDLIB_H
49
+ #include <stdlib.h>
50
+ #endif
51
+ #ifdef HAVE_ZLIB_H
52
+ #include <zlib.h>
53
+ #endif
54
+ #ifdef HAVE_BZLIB_H
55
+ #include <bzlib.h>
56
+ #endif
57
+ #ifdef HAVE_LZMA_H
58
+ #include <lzma.h>
59
+ #endif
60
+ #ifdef HAVE_ZSTD_H
61
+ #include <zstd.h>
62
+ #endif
63
+
64
+ #include "archive.h"
65
+ #include "archive_digest_private.h"
66
+ #include "archive_cryptor_private.h"
67
+ #include "archive_endian.h"
68
+ #include "archive_entry.h"
69
+ #include "archive_entry_locale.h"
70
+ #include "archive_hmac_private.h"
71
+ #include "archive_private.h"
72
+ #include "archive_rb.h"
73
+ #include "archive_read_private.h"
74
+ #include "archive_time_private.h"
75
+ #include "archive_ppmd8_private.h"
76
+
77
+ #ifndef HAVE_ZLIB_H
78
+ #include "archive_crc32.h"
79
+ #endif
80
+
81
+ /* length of local file header, not including filename and extra */
82
+ #define ZIP_LOCHDR_LEN 30U
83
+
84
+ /* maximum length of Mac metadata in MiB */
85
+ #define ZIP_MAX_METADATA 10U
86
+
87
+ struct zip_entry {
88
+ struct archive_rb_node node;
89
+ struct zip_entry *next;
90
+ int64_t local_header_offset;
91
+ int64_t compressed_size;
92
+ int64_t uncompressed_size;
93
+ int64_t gid;
94
+ int64_t uid;
95
+ struct archive_string rsrcname;
96
+ time_t mtime;
97
+ time_t atime;
98
+ time_t ctime;
99
+ uint32_t crc32;
100
+ uint16_t mode;
101
+ uint16_t zip_flags; /* From GP Flags Field */
102
+ unsigned char compression;
103
+ unsigned char system; /* From "version written by" */
104
+ unsigned char flags; /* Our extra markers. */
105
+ unsigned char decdat;/* Used for Decryption check */
106
+
107
+ /* WinZip AES encryption extra field should be available
108
+ * when compression is 99. */
109
+ struct {
110
+ /* Vendor version: AE-1 - 0x0001, AE-2 - 0x0002 */
111
+ unsigned vendor;
112
+ #define AES_VENDOR_AE_1 0x0001
113
+ #define AES_VENDOR_AE_2 0x0002
114
+ /* AES encryption strength:
115
+ * 1 - 128 bits, 2 - 192 bits, 2 - 256 bits. */
116
+ unsigned strength;
117
+ /* Actual compression method. */
118
+ unsigned char compression;
119
+ } aes_extra;
120
+ };
121
+
122
+ struct trad_enc_ctx {
123
+ uint32_t keys[3];
124
+ };
125
+
126
+ /* Bits used in zip_flags. */
127
+ #define ZIP_ENCRYPTED (1 << 0)
128
+ #define ZIP_LENGTH_AT_END (1 << 3) /* Also called "Streaming bit" */
129
+ #define ZIP_STRONG_ENCRYPTED (1 << 6)
130
+ #define ZIP_UTF8_NAME (1 << 11)
131
+ /* See "7.2 Single Password Symmetric Encryption Method"
132
+ in http://www.pkware.com/documents/casestudies/APPNOTE.TXT */
133
+ #define ZIP_CENTRAL_DIRECTORY_ENCRYPTED (1 << 13)
134
+
135
+ /* Bits used in flags. */
136
+ #define LA_USED_ZIP64 (1 << 0)
137
+ #define LA_FROM_CENTRAL_DIRECTORY (1 << 1)
138
+
139
+ /*
140
+ * See "WinZip - AES Encryption Information"
141
+ * http://www.winzip.com/aes_info.htm
142
+ */
143
+ /* Value used in compression method. */
144
+ #define WINZIP_AES_ENCRYPTION 99
145
+ /* Authentication code size. */
146
+ #define AUTH_CODE_SIZE 10
147
+ /**/
148
+ #define MAX_DERIVED_KEY_BUF_SIZE (AES_MAX_KEY_SIZE * 2 + 2)
149
+
150
+ struct zip {
151
+ /* Structural information about the archive. */
152
+ struct archive_string format_name;
153
+ int64_t central_directory_offset;
154
+ int64_t central_directory_offset_adjusted;
155
+ int has_encrypted_entries;
156
+
157
+ /* List of entries (seekable Zip only) */
158
+ struct zip_entry *zip_entries;
159
+ struct archive_rb_tree tree;
160
+ struct archive_rb_tree tree_rsrc;
161
+
162
+ /* Bytes read but not yet consumed via __archive_read_consume() */
163
+ size_t unconsumed;
164
+
165
+ /* Information about entry we're currently reading. */
166
+ struct zip_entry *entry;
167
+ int64_t entry_bytes_remaining;
168
+
169
+ /* These count the number of bytes actually read for the entry. */
170
+ int64_t entry_compressed_bytes_read;
171
+ int64_t entry_uncompressed_bytes_read;
172
+
173
+ /* Running CRC32 of the decompressed and decrypted data */
174
+ unsigned long computed_crc32;
175
+ unsigned long (*crc32func)(unsigned long, const void *,
176
+ size_t);
177
+ char ignore_crc32;
178
+
179
+ /* Flags to mark progress of decompression. */
180
+ char decompress_init;
181
+ char end_of_entry;
182
+
183
+ unsigned char *uncompressed_buffer;
184
+ size_t uncompressed_buffer_size;
185
+
186
+ #ifdef HAVE_ZLIB_H
187
+ z_stream stream;
188
+ char stream_valid;
189
+ #endif
190
+
191
+ #if HAVE_LZMA_H && HAVE_LIBLZMA
192
+ lzma_stream zipx_lzma_stream;
193
+ char zipx_lzma_valid;
194
+ #endif
195
+
196
+ #ifdef HAVE_BZLIB_H
197
+ bz_stream bzstream;
198
+ char bzstream_valid;
199
+ #endif
200
+
201
+ #if HAVE_ZSTD_H && HAVE_LIBZSTD
202
+ ZSTD_DStream *zstdstream;
203
+ char zstdstream_valid;
204
+ #endif
205
+
206
+ IByteIn zipx_ppmd_stream;
207
+ ssize_t zipx_ppmd_read_compressed;
208
+ CPpmd8 ppmd8;
209
+ char ppmd8_valid;
210
+ char ppmd8_stream_failed;
211
+
212
+ struct archive_string_conv *sconv;
213
+ struct archive_string_conv *sconv_default;
214
+ struct archive_string_conv *sconv_utf8;
215
+ int init_default_conversion;
216
+ int process_mac_extensions;
217
+
218
+ char init_decryption;
219
+
220
+ /* Decryption buffer. */
221
+ /*
222
+ * The decrypted data starts at decrypted_ptr and
223
+ * extends for decrypted_bytes_remaining. Decryption
224
+ * adds new data to the end of this block, data is returned
225
+ * to clients from the beginning. When the block hits the
226
+ * end of decrypted_buffer, it has to be shuffled back to
227
+ * the beginning of the buffer.
228
+ */
229
+ unsigned char *decrypted_buffer;
230
+ unsigned char *decrypted_ptr;
231
+ size_t decrypted_buffer_size;
232
+ size_t decrypted_bytes_remaining;
233
+ size_t decrypted_unconsumed_bytes;
234
+
235
+ /* Traditional PKWARE decryption. */
236
+ struct trad_enc_ctx tctx;
237
+ char tctx_valid;
238
+
239
+ /* WinZip AES decryption. */
240
+ /* Contexts used for AES decryption. */
241
+ archive_crypto_ctx cctx;
242
+ char cctx_valid;
243
+ archive_hmac_sha1_ctx hctx;
244
+ char hctx_valid;
245
+
246
+ /* Strong encryption's decryption header information. */
247
+ unsigned iv_size;
248
+ unsigned alg_id;
249
+ unsigned bit_len;
250
+ unsigned flags;
251
+ unsigned erd_size;
252
+ unsigned v_size;
253
+ unsigned v_crc32;
254
+ uint8_t *iv;
255
+ uint8_t *erd;
256
+ uint8_t *v_data;
257
+ };
258
+
259
+ /* Many systems define min or MIN, but not all. */
260
+ #define zipmin(a,b) ((a) < (b) ? (a) : (b))
261
+
262
+ /* True if this entry has declared a specific uncompressed size. */
263
+ static int
264
+ zip_entry_size_is_set(const struct zip_entry *zip_entry)
265
+ {
266
+ return (0 == (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
267
+ || (zip_entry->uncompressed_size > 0
268
+ && zip_entry->uncompressed_size != 0xffffffff));
269
+ }
270
+
271
+ #ifdef HAVE_ZLIB_H
272
+ static int
273
+ zip_read_data_deflate(struct archive_read *a, const void **buff,
274
+ size_t *size, int64_t *offset);
275
+ #endif
276
+ #if HAVE_LZMA_H && HAVE_LIBLZMA
277
+ static int
278
+ zip_read_data_zipx_lzma_alone(struct archive_read *a, const void **buff,
279
+ size_t *size, int64_t *offset);
280
+ #endif
281
+
282
+ static void
283
+ trad_enc_decrypt_update(struct trad_enc_ctx *, const uint8_t *, size_t,
284
+ uint8_t *, size_t);
285
+
286
+ /* This function is used by Ppmd8_DecodeSymbol during decompression of Ppmd8
287
+ * streams inside ZIP files. It has 2 purposes: one is to fetch the next
288
+ * compressed byte from the stream, second one is to increase the counter how
289
+ * many compressed bytes were read. */
290
+ static Byte
291
+ ppmd_read(void* p) {
292
+ /* Get the handle to current decompression context. */
293
+ struct archive_read *a = ((IByteIn*)p)->a;
294
+ struct zip *zip = a->format->data;
295
+
296
+ /* Fetch next byte. */
297
+ const uint8_t* data = __archive_read_ahead(a, 1, NULL);
298
+ if(data == NULL) {
299
+ zip->ppmd8_stream_failed = 1;
300
+ return 0;
301
+ }
302
+
303
+ if (zip->tctx_valid || zip->cctx_valid) {
304
+ uint8_t val;
305
+ if (zip->tctx_valid) {
306
+ trad_enc_decrypt_update(&zip->tctx,
307
+ data, 1, &val, 1);
308
+ } else {
309
+ size_t dsize = 1;
310
+ archive_decrypto_aes_ctr_update(&zip->cctx,
311
+ data, 1, &val, &dsize);
312
+ }
313
+ if (zip->hctx_valid)
314
+ archive_hmac_sha1_update(&zip->hctx, data, 1);
315
+
316
+ __archive_read_consume(a, 1);
317
+ ++zip->zipx_ppmd_read_compressed;
318
+ return val;
319
+ }
320
+
321
+ __archive_read_consume(a, 1);
322
+
323
+ /* Increment the counter. */
324
+ ++zip->zipx_ppmd_read_compressed;
325
+
326
+ /* Return the next compressed byte. */
327
+ return data[0];
328
+ }
329
+
330
+ /* ------------------------------------------------------------------------ */
331
+
332
+ /*
333
+ Traditional PKWARE Decryption functions.
334
+ */
335
+
336
+ static void
337
+ trad_enc_update_keys(struct trad_enc_ctx *ctx, uint8_t c)
338
+ {
339
+ uint8_t t;
340
+ #define CRC32(c, b) (crc32(c ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL)
341
+
342
+ ctx->keys[0] = CRC32(ctx->keys[0], c);
343
+ ctx->keys[1] = (ctx->keys[1] + (ctx->keys[0] & 0xff)) * 134775813L + 1;
344
+ t = (ctx->keys[1] >> 24) & 0xff;
345
+ ctx->keys[2] = CRC32(ctx->keys[2], t);
346
+ #undef CRC32
347
+ }
348
+
349
+ static uint8_t
350
+ trad_enc_decrypt_byte(struct trad_enc_ctx *ctx)
351
+ {
352
+ unsigned temp = ctx->keys[2] | 2;
353
+ return (uint8_t)((temp * (temp ^ 1)) >> 8) & 0xff;
354
+ }
355
+
356
+ static void
357
+ trad_enc_decrypt_update(struct trad_enc_ctx *ctx, const uint8_t *in,
358
+ size_t in_len, uint8_t *out, size_t out_len)
359
+ {
360
+ unsigned i, max;
361
+
362
+ max = (unsigned)((in_len < out_len)? in_len: out_len);
363
+
364
+ for (i = 0; i < max; i++) {
365
+ uint8_t t = in[i] ^ trad_enc_decrypt_byte(ctx);
366
+ out[i] = t;
367
+ trad_enc_update_keys(ctx, t);
368
+ }
369
+ }
370
+
371
+ static int
372
+ trad_enc_init(struct trad_enc_ctx *ctx, const char *pw, size_t pw_len,
373
+ const uint8_t *key, size_t key_len, uint8_t *crcchk)
374
+ {
375
+ uint8_t header[12];
376
+
377
+ if (key_len < 12) {
378
+ *crcchk = 0xff;
379
+ return -1;
380
+ }
381
+
382
+ ctx->keys[0] = 305419896L;
383
+ ctx->keys[1] = 591751049L;
384
+ ctx->keys[2] = 878082192L;
385
+
386
+ for (;pw_len; --pw_len)
387
+ trad_enc_update_keys(ctx, *pw++);
388
+
389
+ trad_enc_decrypt_update(ctx, key, 12, header, 12);
390
+ /* Return the last byte for CRC check. */
391
+ *crcchk = header[11];
392
+ return 0;
393
+ }
394
+
395
+ #if 0
396
+ static void
397
+ crypt_derive_key_sha1(const void *p, int size, unsigned char *key,
398
+ int key_size)
399
+ {
400
+ #define MD_SIZE 20
401
+ archive_sha1_ctx ctx;
402
+ unsigned char md1[MD_SIZE];
403
+ unsigned char md2[MD_SIZE * 2];
404
+ unsigned char mkb[64];
405
+ int i;
406
+
407
+ archive_sha1_init(&ctx);
408
+ archive_sha1_update(&ctx, p, size);
409
+ archive_sha1_final(&ctx, md1);
410
+
411
+ memset(mkb, 0x36, sizeof(mkb));
412
+ for (i = 0; i < MD_SIZE; i++)
413
+ mkb[i] ^= md1[i];
414
+ archive_sha1_init(&ctx);
415
+ archive_sha1_update(&ctx, mkb, sizeof(mkb));
416
+ archive_sha1_final(&ctx, md2);
417
+
418
+ memset(mkb, 0x5C, sizeof(mkb));
419
+ for (i = 0; i < MD_SIZE; i++)
420
+ mkb[i] ^= md1[i];
421
+ archive_sha1_init(&ctx);
422
+ archive_sha1_update(&ctx, mkb, sizeof(mkb));
423
+ archive_sha1_final(&ctx, md2 + MD_SIZE);
424
+
425
+ if (key_size > 32)
426
+ key_size = 32;
427
+ memcpy(key, md2, key_size);
428
+ #undef MD_SIZE
429
+ }
430
+ #endif
431
+
432
+ /* Read and decrypt bytes for zipx init headers.
433
+ * Used by format-specific init functions (lzma, ppmd) that need to
434
+ * read a small header from the compressed stream. When encryption is
435
+ * active the bytes are decrypted in-place into the decryption buffer. */
436
+ static int
437
+ zipx_read_header_and_decrypt(struct archive_read *a, const void **buf, size_t in_len,
438
+ size_t *out_len, size_t *consumed)
439
+ {
440
+ struct zip *zip = a->format->data;
441
+ const void *raw;
442
+ ssize_t bytes_avail;
443
+ size_t to_decrypt;
444
+
445
+ raw = __archive_read_ahead(a, in_len, &bytes_avail);
446
+ if (raw == NULL || bytes_avail < (ssize_t)in_len) {
447
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
448
+ "Truncated ZIP file data");
449
+ return (ARCHIVE_FATAL);
450
+ }
451
+
452
+ if (zip->tctx_valid || zip->cctx_valid) {
453
+ to_decrypt = in_len;
454
+ if (to_decrypt > zip->decrypted_buffer_size)
455
+ to_decrypt = zip->decrypted_buffer_size;
456
+
457
+ if (zip->tctx_valid) {
458
+ trad_enc_decrypt_update(&zip->tctx,
459
+ raw, to_decrypt,
460
+ zip->decrypted_buffer, to_decrypt);
461
+ } else {
462
+ size_t dsize = to_decrypt;
463
+ archive_decrypto_aes_ctr_update(&zip->cctx,
464
+ raw, to_decrypt,
465
+ zip->decrypted_buffer, &dsize);
466
+ }
467
+ if (zip->hctx_valid)
468
+ archive_hmac_sha1_update(&zip->hctx,
469
+ raw, to_decrypt);
470
+
471
+ *buf = zip->decrypted_buffer;
472
+ *out_len = to_decrypt;
473
+ *consumed = to_decrypt;
474
+ } else {
475
+ *buf = raw;
476
+ *out_len = in_len;
477
+ *consumed = in_len;
478
+ }
479
+ return (ARCHIVE_OK);
480
+ }
481
+
482
+ /* Decrypt bulk compressed data for zipx decompression.
483
+ * Manages the decryption buffer, handles partial fills, and returns decrypted
484
+ * data pointer + length. `sp` is set to the raw pointer for HMAC accounting. */
485
+ static void
486
+ zip_read_decrypt(struct zip *zip, const void *compressed_buff,
487
+ ssize_t bytes_avail, const void **result_buff, ssize_t *result_avail,
488
+ const void **sp)
489
+ {
490
+ *sp = compressed_buff;
491
+
492
+ /* Safety check to prevent potential OOB reads if something went wrong
493
+ * previously. We should not have a negative bytes_avail count here.
494
+ * If we do, set them to zero so that reading the ZIP will fail later,
495
+ * safely as corrupted instead of crashing. */
496
+ if (bytes_avail < 0)
497
+ bytes_avail = 0;
498
+
499
+ if (zip->tctx_valid || zip->cctx_valid) {
500
+ if (zip->decrypted_bytes_remaining < (size_t)bytes_avail) {
501
+ size_t buff_remaining =
502
+ (zip->decrypted_buffer +
503
+ zip->decrypted_buffer_size)
504
+ - (zip->decrypted_ptr +
505
+ zip->decrypted_bytes_remaining);
506
+ /* The new bytes to decrypt start after decrypted_bytes_remaining
507
+ * in the raw stream: those leading bytes were already
508
+ * decrypted on a previous call but have not yet been consumed. */
509
+ size_t new_bytes =
510
+ (size_t)bytes_avail
511
+ - zip->decrypted_bytes_remaining;
512
+
513
+ if (buff_remaining > new_bytes)
514
+ buff_remaining = new_bytes;
515
+
516
+ if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END) &&
517
+ zip->entry_bytes_remaining > 0) {
518
+ if ((int64_t)(zip->decrypted_bytes_remaining
519
+ + buff_remaining)
520
+ > zip->entry_bytes_remaining) {
521
+ if (zip->entry_bytes_remaining <
522
+ (int64_t)zip->decrypted_bytes_remaining)
523
+ buff_remaining = 0;
524
+ else
525
+ buff_remaining =
526
+ (size_t)zip->entry_bytes_remaining
527
+ - zip->decrypted_bytes_remaining;
528
+ }
529
+ }
530
+ if (buff_remaining > 0) {
531
+ if (zip->tctx_valid) {
532
+ trad_enc_decrypt_update(&zip->tctx,
533
+ (const uint8_t *)compressed_buff
534
+ + zip->decrypted_bytes_remaining,
535
+ buff_remaining,
536
+ zip->decrypted_ptr
537
+ + zip->decrypted_bytes_remaining,
538
+ buff_remaining);
539
+ } else {
540
+ size_t dsize = buff_remaining;
541
+ archive_decrypto_aes_ctr_update(
542
+ &zip->cctx,
543
+ (const uint8_t *)compressed_buff
544
+ + zip->decrypted_bytes_remaining,
545
+ buff_remaining,
546
+ zip->decrypted_ptr
547
+ + zip->decrypted_bytes_remaining,
548
+ &dsize);
549
+ }
550
+ zip->decrypted_bytes_remaining +=
551
+ buff_remaining;
552
+ }
553
+ }
554
+ *result_avail = zip->decrypted_bytes_remaining;
555
+ *result_buff = (const char *)zip->decrypted_ptr;
556
+ } else {
557
+ *result_buff = compressed_buff;
558
+ *result_avail = bytes_avail;
559
+ }
560
+ }
561
+
562
+ /* Post-decompression decrypt state update.
563
+ * Updates decrypt buffer pointers and HMAC after the decompressor
564
+ * has consumed `to_consume` bytes. */
565
+ static void
566
+ zip_read_decrypt_update(struct zip *zip, ssize_t to_consume, const void *sp)
567
+ {
568
+ if (zip->tctx_valid || zip->cctx_valid) {
569
+ zip->decrypted_bytes_remaining -= to_consume;
570
+ if (zip->decrypted_bytes_remaining == 0)
571
+ zip->decrypted_ptr = zip->decrypted_buffer;
572
+ else
573
+ zip->decrypted_ptr += to_consume;
574
+ }
575
+ if (zip->hctx_valid)
576
+ archive_hmac_sha1_update(&zip->hctx, sp, to_consume);
577
+ }
578
+
579
+ /*
580
+ * Common code for streaming or seeking modes.
581
+ *
582
+ * Includes code to read local file headers, decompress data
583
+ * from entry bodies, and common API.
584
+ */
585
+
586
+ static unsigned long
587
+ real_crc32(unsigned long crc, const void *buff, size_t len)
588
+ {
589
+ return crc32(crc, buff, (unsigned int)len);
590
+ }
591
+
592
+ /* Used by "ignorecrc32" option to speed up tests. */
593
+ static unsigned long
594
+ fake_crc32(unsigned long crc, const void *buff, size_t len)
595
+ {
596
+ (void)crc; /* UNUSED */
597
+ (void)buff; /* UNUSED */
598
+ (void)len; /* UNUSED */
599
+ return 0;
600
+ }
601
+
602
+ static const struct {
603
+ int id;
604
+ const char * name;
605
+ } compression_methods[] = {
606
+ {0, "uncompressed"}, /* The file is stored (no compression) */
607
+ {1, "shrinking"}, /* The file is Shrunk */
608
+ {2, "reduced-1"}, /* The file is Reduced with compression factor 1 */
609
+ {3, "reduced-2"}, /* The file is Reduced with compression factor 2 */
610
+ {4, "reduced-3"}, /* The file is Reduced with compression factor 3 */
611
+ {5, "reduced-4"}, /* The file is Reduced with compression factor 4 */
612
+ {6, "imploded"}, /* The file is Imploded */
613
+ {7, "reserved"}, /* Reserved for Tokenizing compression algorithm */
614
+ {8, "deflation"}, /* The file is Deflated */
615
+ {9, "deflation-64-bit"}, /* Enhanced Deflating using Deflate64(tm) */
616
+ {10, "ibm-terse"},/* PKWARE Data Compression Library Imploding
617
+ * (old IBM TERSE) */
618
+ {11, "reserved"}, /* Reserved by PKWARE */
619
+ {12, "bzip"}, /* File is compressed using BZIP2 algorithm */
620
+ {13, "reserved"}, /* Reserved by PKWARE */
621
+ {14, "lzma"}, /* LZMA (EFS) */
622
+ {15, "reserved"}, /* Reserved by PKWARE */
623
+ {16, "reserved"}, /* Reserved by PKWARE */
624
+ {17, "reserved"}, /* Reserved by PKWARE */
625
+ {18, "ibm-terse-new"}, /* File is compressed using IBM TERSE (new) */
626
+ {19, "ibm-lz777"},/* IBM LZ77 z Architecture (PFS) */
627
+ {93, "zstd"}, /* Zstandard (zstd) Compression */
628
+ {95, "xz"}, /* XZ compressed data */
629
+ {96, "jpeg"}, /* JPEG compressed data */
630
+ {97, "wav-pack"}, /* WavPack compressed data */
631
+ {98, "ppmd-1"}, /* PPMd version I, Rev 1 */
632
+ {99, "aes"} /* WinZip AES encryption */
633
+ };
634
+
635
+ static const char *
636
+ compression_name(const int compression)
637
+ {
638
+ static const int num_compression_methods =
639
+ sizeof(compression_methods)/sizeof(compression_methods[0]);
640
+ int i=0;
641
+
642
+ while(compression >= 0 && i < num_compression_methods) {
643
+ if (compression_methods[i].id == compression)
644
+ return compression_methods[i].name;
645
+ i++;
646
+ }
647
+ return "??";
648
+ }
649
+
650
+ /*
651
+ * The extra data is stored as a list of
652
+ * id1+size1+data1 + id2+size2+data2 ...
653
+ * triplets. id and size are 2 bytes each.
654
+ */
655
+ static int
656
+ process_extra(struct archive_read *a, struct archive_entry *entry,
657
+ const char *p, size_t extra_length, struct zip_entry* zip_entry)
658
+ {
659
+ struct zip *zip = a->format->data;
660
+ unsigned offset = 0;
661
+
662
+ if (extra_length == 0) {
663
+ return ARCHIVE_OK;
664
+ }
665
+
666
+ if (extra_length < 4) {
667
+ size_t i = 0;
668
+ /* Some ZIP files may have trailing 0 bytes. Let's check they
669
+ * are all 0 and ignore them instead of returning an error.
670
+ *
671
+ * This is not technically correct, but some ZIP files look
672
+ * like this and other tools support those files - so let's
673
+ * also support them.
674
+ */
675
+ for (; i < extra_length; i++) {
676
+ if (p[i] != 0) {
677
+ archive_set_error(&a->archive,
678
+ ARCHIVE_ERRNO_FILE_FORMAT,
679
+ "Too-small extra data: "
680
+ "Need at least 4 bytes, "
681
+ "but only found %d bytes",
682
+ (int)extra_length);
683
+ return ARCHIVE_FAILED;
684
+ }
685
+ }
686
+
687
+ return ARCHIVE_OK;
688
+ }
689
+
690
+ while (offset <= extra_length - 4) {
691
+ unsigned short headerid = archive_le16dec(p + offset);
692
+ unsigned short datasize = archive_le16dec(p + offset + 2);
693
+
694
+ offset += 4;
695
+ if (offset + datasize > extra_length) {
696
+ archive_set_error(&a->archive,
697
+ ARCHIVE_ERRNO_FILE_FORMAT, "Extra data overflow: "
698
+ "Need %d bytes but only found %d bytes",
699
+ (int)datasize, (int)(extra_length - offset));
700
+ return ARCHIVE_FAILED;
701
+ }
702
+ #ifdef DEBUG
703
+ fprintf(stderr, "Header id 0x%04x, length %d\n",
704
+ headerid, datasize);
705
+ #endif
706
+ switch (headerid) {
707
+ case 0x0001:
708
+ /* Zip64 extended information extra field. */
709
+ zip_entry->flags |= LA_USED_ZIP64;
710
+ if (zip_entry->uncompressed_size == 0xffffffff) {
711
+ uint64_t t = 0;
712
+ if (datasize < 8
713
+ || (t = archive_le64dec(p + offset)) >
714
+ INT64_MAX) {
715
+ archive_set_error(&a->archive,
716
+ ARCHIVE_ERRNO_FILE_FORMAT,
717
+ "Malformed 64-bit "
718
+ "uncompressed size");
719
+ return ARCHIVE_FAILED;
720
+ }
721
+ zip_entry->uncompressed_size = t;
722
+ offset += 8;
723
+ datasize -= 8;
724
+ }
725
+ if (zip_entry->compressed_size == 0xffffffff) {
726
+ uint64_t t = 0;
727
+ if (datasize < 8
728
+ || (t = archive_le64dec(p + offset)) >
729
+ INT64_MAX) {
730
+ archive_set_error(&a->archive,
731
+ ARCHIVE_ERRNO_FILE_FORMAT,
732
+ "Malformed 64-bit "
733
+ "compressed size");
734
+ return ARCHIVE_FAILED;
735
+ }
736
+ zip_entry->compressed_size = t;
737
+ offset += 8;
738
+ datasize -= 8;
739
+ }
740
+ if (zip_entry->local_header_offset == 0xffffffff) {
741
+ uint64_t t = 0;
742
+ if (datasize < 8
743
+ || (t = archive_le64dec(p + offset)) >
744
+ INT64_MAX) {
745
+ archive_set_error(&a->archive,
746
+ ARCHIVE_ERRNO_FILE_FORMAT,
747
+ "Malformed 64-bit "
748
+ "local header offset");
749
+ return ARCHIVE_FAILED;
750
+ }
751
+ zip_entry->local_header_offset = t;
752
+ offset += 8;
753
+ datasize -= 8;
754
+ }
755
+ /* archive_le32dec(p + offset) gives disk
756
+ * on which file starts, but we don't handle
757
+ * multi-volume Zip files. */
758
+ break;
759
+ case 0x000d:
760
+ /* PKWARE Unix Extra Field fixed metadata. */
761
+ if (datasize >= 12) {
762
+ zip_entry->atime = archive_le32dec(p + offset);
763
+ zip_entry->mtime =
764
+ archive_le32dec(p + offset + 4);
765
+ zip_entry->uid =
766
+ archive_le16dec(p + offset + 8);
767
+ zip_entry->gid =
768
+ archive_le16dec(p + offset + 10);
769
+ /*
770
+ * APPNOTE.TXT also defines additional data after
771
+ * this fixed metadata, depending on file type.
772
+ */
773
+ }
774
+ break;
775
+ #ifdef DEBUG
776
+ case 0x0017:
777
+ {
778
+ /* Strong encryption field. */
779
+ if (archive_le16dec(p + offset) == 2) {
780
+ unsigned algId =
781
+ archive_le16dec(p + offset + 2);
782
+ unsigned bitLen =
783
+ archive_le16dec(p + offset + 4);
784
+ int flags =
785
+ archive_le16dec(p + offset + 6);
786
+ fprintf(stderr, "algId=0x%04x, bitLen=%u, "
787
+ "flgas=%d\n", algId, bitLen,flags);
788
+ }
789
+ break;
790
+ }
791
+ #endif
792
+ case 0x5455:
793
+ {
794
+ /* Extended time field "UT". */
795
+ int flags;
796
+ if (datasize == 0) {
797
+ archive_set_error(&a->archive,
798
+ ARCHIVE_ERRNO_FILE_FORMAT,
799
+ "Incomplete extended time field");
800
+ return ARCHIVE_FAILED;
801
+ }
802
+ flags = p[offset];
803
+ offset++;
804
+ datasize--;
805
+ /* Flag bits indicate which dates are present. */
806
+ if (flags & 0x01)
807
+ {
808
+ #ifdef DEBUG
809
+ fprintf(stderr, "mtime: %lld -> %d\n",
810
+ (long long)zip_entry->mtime,
811
+ archive_le32dec(p + offset));
812
+ #endif
813
+ if (datasize < 4)
814
+ break;
815
+ zip_entry->mtime = archive_le32dec(p + offset);
816
+ offset += 4;
817
+ datasize -= 4;
818
+ }
819
+ if (flags & 0x02)
820
+ {
821
+ if (datasize < 4)
822
+ break;
823
+ zip_entry->atime = archive_le32dec(p + offset);
824
+ offset += 4;
825
+ datasize -= 4;
826
+ }
827
+ if (flags & 0x04)
828
+ {
829
+ if (datasize < 4)
830
+ break;
831
+ zip_entry->ctime = archive_le32dec(p + offset);
832
+ offset += 4;
833
+ datasize -= 4;
834
+ }
835
+ break;
836
+ }
837
+ case 0x5855:
838
+ {
839
+ /* Info-ZIP Unix Extra Field (old version) "UX". */
840
+ if (datasize >= 8) {
841
+ zip_entry->atime = archive_le32dec(p + offset);
842
+ zip_entry->mtime =
843
+ archive_le32dec(p + offset + 4);
844
+ }
845
+ if (datasize >= 12) {
846
+ zip_entry->uid =
847
+ archive_le16dec(p + offset + 8);
848
+ zip_entry->gid =
849
+ archive_le16dec(p + offset + 10);
850
+ }
851
+ break;
852
+ }
853
+ case 0x6c78:
854
+ {
855
+ /* Experimental 'xl' field */
856
+ /*
857
+ * Introduced Dec 2013 to provide a way to
858
+ * include external file attributes (and other
859
+ * fields that ordinarily appear only in
860
+ * central directory) in local file header.
861
+ * This provides file type and permission
862
+ * information necessary to support full
863
+ * streaming extraction. Currently being
864
+ * discussed with other Zip developers
865
+ * ... subject to change.
866
+ *
867
+ * Format:
868
+ * The field starts with a bitmap that specifies
869
+ * which additional fields are included. The
870
+ * bitmap is variable length and can be extended in
871
+ * the future.
872
+ *
873
+ * n bytes - feature bitmap: first byte has low-order
874
+ * 7 bits. If high-order bit is set, a subsequent
875
+ * byte holds the next 7 bits, etc.
876
+ *
877
+ * if bitmap & 1, 2 byte "version made by"
878
+ * if bitmap & 2, 2 byte "internal file attributes"
879
+ * if bitmap & 4, 4 byte "external file attributes"
880
+ * if bitmap & 8, 2 byte comment length + n byte
881
+ * comment
882
+ */
883
+ int bitmap, bitmap_last;
884
+
885
+ if (datasize < 1)
886
+ break;
887
+ bitmap_last = bitmap = 0xff & p[offset];
888
+ offset += 1;
889
+ datasize -= 1;
890
+
891
+ /* We only support first 7 bits of bitmap; skip rest. */
892
+ while ((bitmap_last & 0x80) != 0
893
+ && datasize >= 1) {
894
+ bitmap_last = p[offset];
895
+ offset += 1;
896
+ datasize -= 1;
897
+ }
898
+
899
+ if (bitmap & 1) {
900
+ /* 2 byte "version made by" */
901
+ if (datasize < 2)
902
+ break;
903
+ zip_entry->system
904
+ = archive_le16dec(p + offset) >> 8;
905
+ offset += 2;
906
+ datasize -= 2;
907
+ }
908
+ if (bitmap & 2) {
909
+ /* 2 byte "internal file attributes" */
910
+ uint32_t internal_attributes;
911
+ if (datasize < 2)
912
+ break;
913
+ internal_attributes
914
+ = archive_le16dec(p + offset);
915
+ /* Not used by libarchive at present. */
916
+ (void)internal_attributes; /* UNUSED */
917
+ offset += 2;
918
+ datasize -= 2;
919
+ }
920
+ if (bitmap & 4) {
921
+ /* 4 byte "external file attributes" */
922
+ uint32_t external_attributes;
923
+ if (datasize < 4)
924
+ break;
925
+ external_attributes
926
+ = archive_le32dec(p + offset);
927
+ if (zip_entry->system == 3) {
928
+ zip_entry->mode
929
+ = external_attributes >> 16;
930
+ } else if (zip_entry->system == 0) {
931
+ // Interpret MSDOS directory bit
932
+ if (0x10 == (external_attributes &
933
+ 0x10)) {
934
+ zip_entry->mode =
935
+ AE_IFDIR | 0775;
936
+ } else {
937
+ zip_entry->mode =
938
+ AE_IFREG | 0664;
939
+ }
940
+ if (0x01 == (external_attributes &
941
+ 0x01)) {
942
+ /* Read-only bit;
943
+ * strip write permissions */
944
+ zip_entry->mode &= 0555;
945
+ }
946
+ } else {
947
+ zip_entry->mode = 0;
948
+ }
949
+ offset += 4;
950
+ datasize -= 4;
951
+ }
952
+ if (bitmap & 8) {
953
+ /* 2 byte comment length + comment */
954
+ uint32_t comment_length;
955
+ if (datasize < 2)
956
+ break;
957
+ comment_length
958
+ = archive_le16dec(p + offset);
959
+ offset += 2;
960
+ datasize -= 2;
961
+
962
+ if (datasize < comment_length)
963
+ break;
964
+ /* Comment is not supported by libarchive */
965
+ offset += comment_length;
966
+ datasize -= comment_length;
967
+ }
968
+ break;
969
+ }
970
+ case 0x7075:
971
+ {
972
+ /* Info-ZIP Unicode Path Extra Field. */
973
+ if (datasize < 5 || entry == NULL)
974
+ break;
975
+ offset += 5;
976
+ datasize -= 5;
977
+
978
+ /* The path name in this field is always encoded
979
+ * in UTF-8. */
980
+ if (zip->sconv_utf8 == NULL) {
981
+ zip->sconv_utf8 =
982
+ archive_string_conversion_from_charset(
983
+ &a->archive, "UTF-8", 1);
984
+ /* If the converter from UTF-8 is not
985
+ * available, then the path name from the main
986
+ * field will more likely be correct. */
987
+ if (zip->sconv_utf8 == NULL)
988
+ break;
989
+ }
990
+
991
+ /* Make sure the CRC32 of the filename matches. */
992
+ if (!zip->ignore_crc32) {
993
+ const char *cp = archive_entry_pathname(entry);
994
+ if (cp) {
995
+ unsigned long file_crc =
996
+ zip->crc32func(0, cp, strlen(cp));
997
+ unsigned long utf_crc =
998
+ archive_le32dec(p + offset - 4);
999
+ if (file_crc != utf_crc) {
1000
+ #ifdef DEBUG
1001
+ fprintf(stderr,
1002
+ "CRC filename mismatch; "
1003
+ "CDE is %lx, but UTF8 "
1004
+ "is outdated with %lx\n",
1005
+ file_crc, utf_crc);
1006
+ #endif
1007
+ break;
1008
+ }
1009
+ }
1010
+ }
1011
+
1012
+ if (archive_entry_copy_pathname_l(entry,
1013
+ p + offset, datasize, zip->sconv_utf8) != 0) {
1014
+ /* Ignore the error, and fallback to the path
1015
+ * name from the main field. */
1016
+ #ifdef DEBUG
1017
+ fprintf(stderr, "Failed to read the ZIP "
1018
+ "0x7075 extra field path.\n");
1019
+ #endif
1020
+ }
1021
+ break;
1022
+ }
1023
+ case 0x7855:
1024
+ /* Info-ZIP Unix Extra Field (type 2) "Ux". */
1025
+ #ifdef DEBUG
1026
+ fprintf(stderr, "uid %d gid %d\n",
1027
+ archive_le16dec(p + offset),
1028
+ archive_le16dec(p + offset + 2));
1029
+ #endif
1030
+ if (datasize >= 2)
1031
+ zip_entry->uid = archive_le16dec(p + offset);
1032
+ if (datasize >= 4)
1033
+ zip_entry->gid =
1034
+ archive_le16dec(p + offset + 2);
1035
+ break;
1036
+ case 0x7875:
1037
+ {
1038
+ /* Info-Zip Unix Extra Field (type 3) "ux". */
1039
+ int uidsize = 0, gidsize = 0;
1040
+
1041
+ /* TODO: support arbitrary uidsize/gidsize. */
1042
+ if (datasize >= 1 && p[offset] == 1) {/* version=1 */
1043
+ if (datasize >= 4) {
1044
+ /* get a uid size. */
1045
+ uidsize = 0xff & (int)p[offset+1];
1046
+ if (uidsize == 2)
1047
+ zip_entry->uid =
1048
+ archive_le16dec(
1049
+ p + offset + 2);
1050
+ else if (uidsize == 4 && datasize >= 6)
1051
+ zip_entry->uid =
1052
+ archive_le32dec(
1053
+ p + offset + 2);
1054
+ }
1055
+ if (datasize >= (2 + uidsize + 3)) {
1056
+ /* get a gid size. */
1057
+ gidsize = 0xff &
1058
+ (int)p[offset+2+uidsize];
1059
+ if (gidsize == 2)
1060
+ zip_entry->gid =
1061
+ archive_le16dec(
1062
+ p+offset+2+uidsize+1);
1063
+ else if (gidsize == 4 &&
1064
+ datasize >= (2 + uidsize + 5))
1065
+ zip_entry->gid =
1066
+ archive_le32dec(
1067
+ p+offset+2+uidsize+1);
1068
+ }
1069
+ }
1070
+ break;
1071
+ }
1072
+ case 0x9901:
1073
+ /* WinZip AES extra data field. */
1074
+ if (datasize < 6) {
1075
+ archive_set_error(&a->archive,
1076
+ ARCHIVE_ERRNO_FILE_FORMAT,
1077
+ "Incomplete AES field");
1078
+ return ARCHIVE_FAILED;
1079
+ }
1080
+ if (p[offset + 2] == 'A' && p[offset + 3] == 'E') {
1081
+ /* Vendor version. */
1082
+ zip_entry->aes_extra.vendor =
1083
+ archive_le16dec(p + offset);
1084
+ /* AES encryption strength. */
1085
+ zip_entry->aes_extra.strength = p[offset + 4];
1086
+ /* Actual compression method. */
1087
+ zip_entry->aes_extra.compression =
1088
+ p[offset + 5];
1089
+ }
1090
+ break;
1091
+ default:
1092
+ break;
1093
+ }
1094
+ offset += datasize;
1095
+ }
1096
+ return ARCHIVE_OK;
1097
+ }
1098
+
1099
+ /*
1100
+ * Assumes file pointer is at beginning of local file header.
1101
+ */
1102
+ static int
1103
+ zip_read_local_file_header(struct archive_read *a, struct archive_entry *entry,
1104
+ struct zip *zip)
1105
+ {
1106
+ const char *p;
1107
+ const void *h;
1108
+ const wchar_t *wp;
1109
+ const char *cp;
1110
+ size_t len, filename_length, extra_length;
1111
+ struct archive_string_conv *sconv;
1112
+ struct zip_entry *zip_entry = zip->entry;
1113
+ struct zip_entry zip_entry_central_dir;
1114
+ int ret = ARCHIVE_OK;
1115
+ char version;
1116
+
1117
+ /* Save a copy of the original for consistency checks. */
1118
+ zip_entry_central_dir = *zip_entry;
1119
+
1120
+ zip->decompress_init = 0;
1121
+ zip->end_of_entry = 0;
1122
+ zip->entry_uncompressed_bytes_read = 0;
1123
+ zip->entry_compressed_bytes_read = 0;
1124
+ zip->computed_crc32 = zip->crc32func(0, NULL, 0);
1125
+
1126
+ /* Setup default conversion. */
1127
+ if (zip->sconv == NULL && !zip->init_default_conversion) {
1128
+ zip->sconv_default =
1129
+ archive_string_default_conversion_for_read(&(a->archive));
1130
+ zip->init_default_conversion = 1;
1131
+ }
1132
+
1133
+ if ((p = __archive_read_ahead(a, ZIP_LOCHDR_LEN, NULL)) == NULL) {
1134
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1135
+ "Truncated ZIP file header");
1136
+ return (ARCHIVE_FATAL);
1137
+ }
1138
+
1139
+ if (memcmp(p, "PK\003\004", 4) != 0) {
1140
+ archive_set_error(&a->archive, -1, "Damaged Zip archive");
1141
+ return ARCHIVE_FATAL;
1142
+ }
1143
+ version = p[4];
1144
+ zip_entry->system = p[5];
1145
+ zip_entry->zip_flags = archive_le16dec(p + 6);
1146
+ if (zip_entry->zip_flags & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)) {
1147
+ zip->has_encrypted_entries = 1;
1148
+ archive_entry_set_is_data_encrypted(entry, 1);
1149
+ if (zip_entry->zip_flags & ZIP_CENTRAL_DIRECTORY_ENCRYPTED &&
1150
+ zip_entry->zip_flags & ZIP_ENCRYPTED &&
1151
+ zip_entry->zip_flags & ZIP_STRONG_ENCRYPTED) {
1152
+ archive_entry_set_is_metadata_encrypted(entry, 1);
1153
+ return ARCHIVE_FATAL;
1154
+ }
1155
+ }
1156
+ zip->init_decryption = (zip_entry->zip_flags & ZIP_ENCRYPTED);
1157
+ zip_entry->compression = (char)archive_le16dec(p + 8);
1158
+ zip_entry->mtime = dos_to_unix(archive_le32dec(p + 10));
1159
+ zip_entry->crc32 = archive_le32dec(p + 14);
1160
+ if (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
1161
+ zip_entry->decdat = p[11];
1162
+ else
1163
+ zip_entry->decdat = p[17];
1164
+ zip_entry->compressed_size = archive_le32dec(p + 18);
1165
+ zip_entry->uncompressed_size = archive_le32dec(p + 22);
1166
+ filename_length = archive_le16dec(p + 26);
1167
+ extra_length = archive_le16dec(p + 28);
1168
+
1169
+ __archive_read_consume(a, ZIP_LOCHDR_LEN);
1170
+
1171
+ /* Read the filename. */
1172
+ if ((h = __archive_read_ahead(a, filename_length, NULL)) == NULL) {
1173
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1174
+ "Truncated ZIP file header");
1175
+ return (ARCHIVE_FATAL);
1176
+ }
1177
+ if (zip_entry->zip_flags & ZIP_UTF8_NAME) {
1178
+ /* The filename is stored to be UTF-8. */
1179
+ if (zip->sconv_utf8 == NULL) {
1180
+ zip->sconv_utf8 =
1181
+ archive_string_conversion_from_charset(
1182
+ &a->archive, "UTF-8", 1);
1183
+ if (zip->sconv_utf8 == NULL)
1184
+ return (ARCHIVE_FATAL);
1185
+ }
1186
+ sconv = zip->sconv_utf8;
1187
+ } else if (zip->sconv != NULL)
1188
+ sconv = zip->sconv;
1189
+ else
1190
+ sconv = zip->sconv_default;
1191
+
1192
+ if (archive_entry_copy_pathname_l(entry,
1193
+ h, filename_length, sconv) != 0) {
1194
+ if (errno == ENOMEM) {
1195
+ archive_set_error(&a->archive, ENOMEM,
1196
+ "Can't allocate memory for Pathname");
1197
+ return (ARCHIVE_FATAL);
1198
+ }
1199
+ archive_set_error(&a->archive,
1200
+ ARCHIVE_ERRNO_FILE_FORMAT,
1201
+ "Pathname cannot be converted "
1202
+ "from %s to current locale",
1203
+ archive_string_conversion_charset_name(sconv));
1204
+ ret = ARCHIVE_WARN;
1205
+ }
1206
+ __archive_read_consume(a, filename_length);
1207
+
1208
+ /* Read the extra data. */
1209
+ if ((h = __archive_read_ahead(a, extra_length, NULL)) == NULL) {
1210
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1211
+ "Truncated ZIP file header");
1212
+ return (ARCHIVE_FATAL);
1213
+ }
1214
+
1215
+ if (ARCHIVE_OK != process_extra(a, entry, h, extra_length,
1216
+ zip_entry)) {
1217
+ return ARCHIVE_FATAL;
1218
+ }
1219
+ __archive_read_consume(a, extra_length);
1220
+
1221
+ /* Work around a bug in Info-Zip: When reading from a pipe, it
1222
+ * stats the pipe instead of synthesizing a file entry. */
1223
+ if ((zip_entry->mode & AE_IFMT) == AE_IFIFO) {
1224
+ zip_entry->mode &= ~ AE_IFMT;
1225
+ zip_entry->mode |= AE_IFREG;
1226
+ }
1227
+
1228
+ /* If the mode is totally empty, set some sane default. */
1229
+ if (zip_entry->mode == 0) {
1230
+ zip_entry->mode |= 0664;
1231
+ }
1232
+
1233
+ /* Windows archivers sometimes use backslash as the directory
1234
+ * separator. Normalize to slash. */
1235
+ if (zip_entry->system == 0 &&
1236
+ (wp = archive_entry_pathname_w(entry)) != NULL) {
1237
+ if (wcschr(wp, L'/') == NULL && wcschr(wp, L'\\') != NULL) {
1238
+ size_t i;
1239
+ struct archive_wstring s;
1240
+ archive_string_init(&s);
1241
+ archive_wstrcpy(&s, wp);
1242
+ for (i = 0; i < archive_strlen(&s); i++) {
1243
+ if (s.s[i] == '\\')
1244
+ s.s[i] = '/';
1245
+ }
1246
+ archive_entry_copy_pathname_w(entry, s.s);
1247
+ archive_wstring_free(&s);
1248
+ }
1249
+ }
1250
+
1251
+ /* Make sure that entries with a trailing '/' are marked as directories
1252
+ * even if the External File Attributes contains bogus values. If this
1253
+ * is not a directory and there is no type, assume a regular file. */
1254
+ if ((zip_entry->mode & AE_IFMT) != AE_IFDIR) {
1255
+ int has_slash;
1256
+
1257
+ wp = archive_entry_pathname_w(entry);
1258
+ if (wp != NULL) {
1259
+ len = wcslen(wp);
1260
+ has_slash = len > 0 && wp[len - 1] == L'/';
1261
+ } else {
1262
+ cp = archive_entry_pathname(entry);
1263
+ len = (cp != NULL)?strlen(cp):0;
1264
+ has_slash = len > 0 && cp[len - 1] == '/';
1265
+ }
1266
+ /* Correct file type as needed. */
1267
+ if (has_slash) {
1268
+ zip_entry->mode &= ~AE_IFMT;
1269
+ zip_entry->mode |= AE_IFDIR;
1270
+ zip_entry->mode |= 0111;
1271
+ } else if ((zip_entry->mode & AE_IFMT) == 0) {
1272
+ zip_entry->mode |= AE_IFREG;
1273
+ }
1274
+ }
1275
+
1276
+ /* Make sure directories end in '/' */
1277
+ if ((zip_entry->mode & AE_IFMT) == AE_IFDIR) {
1278
+ wp = archive_entry_pathname_w(entry);
1279
+ if (wp != NULL) {
1280
+ len = wcslen(wp);
1281
+ if (len > 0 && wp[len - 1] != L'/') {
1282
+ struct archive_wstring s;
1283
+ archive_string_init(&s);
1284
+ archive_wstrcat(&s, wp);
1285
+ archive_wstrappend_wchar(&s, L'/');
1286
+ archive_entry_copy_pathname_w(entry, s.s);
1287
+ archive_wstring_free(&s);
1288
+ }
1289
+ } else {
1290
+ cp = archive_entry_pathname(entry);
1291
+ len = (cp != NULL)?strlen(cp):0;
1292
+ if (len > 0 && cp[len - 1] != '/') {
1293
+ struct archive_string s;
1294
+ archive_string_init(&s);
1295
+ archive_strcat(&s, cp);
1296
+ archive_strappend_char(&s, '/');
1297
+ archive_entry_set_pathname(entry, s.s);
1298
+ archive_string_free(&s);
1299
+ }
1300
+ }
1301
+ }
1302
+
1303
+ if (zip_entry->flags & LA_FROM_CENTRAL_DIRECTORY) {
1304
+ /* If this came from the central dir, its size info
1305
+ * is definitive, so ignore the length-at-end flag. */
1306
+ zip_entry->zip_flags &= ~ZIP_LENGTH_AT_END;
1307
+ /* If local header is missing a value, use the one from
1308
+ the central directory. If both have it, warn about
1309
+ mismatches. */
1310
+ if (zip_entry->crc32 == 0) {
1311
+ zip_entry->crc32 = zip_entry_central_dir.crc32;
1312
+ } else if (!zip->ignore_crc32
1313
+ && zip_entry->crc32 != zip_entry_central_dir.crc32) {
1314
+ archive_set_error(&a->archive,
1315
+ ARCHIVE_ERRNO_FILE_FORMAT,
1316
+ "Inconsistent CRC32 values");
1317
+ ret = ARCHIVE_WARN;
1318
+ }
1319
+ if (zip_entry->compressed_size == 0
1320
+ || zip_entry->compressed_size == 0xffffffff) {
1321
+ zip_entry->compressed_size
1322
+ = zip_entry_central_dir.compressed_size;
1323
+ } else if (zip_entry->compressed_size
1324
+ != zip_entry_central_dir.compressed_size) {
1325
+ archive_set_error(&a->archive,
1326
+ ARCHIVE_ERRNO_FILE_FORMAT,
1327
+ "Inconsistent compressed size: "
1328
+ "%jd in central directory, %jd in local header",
1329
+ (intmax_t)zip_entry_central_dir.compressed_size,
1330
+ (intmax_t)zip_entry->compressed_size);
1331
+ ret = ARCHIVE_WARN;
1332
+ }
1333
+ if (zip_entry->uncompressed_size == 0 ||
1334
+ zip_entry->uncompressed_size == 0xffffffff) {
1335
+ zip_entry->uncompressed_size
1336
+ = zip_entry_central_dir.uncompressed_size;
1337
+ } else if (zip_entry->uncompressed_size
1338
+ != zip_entry_central_dir.uncompressed_size) {
1339
+ archive_set_error(&a->archive,
1340
+ ARCHIVE_ERRNO_FILE_FORMAT,
1341
+ "Inconsistent uncompressed size: "
1342
+ "%jd in central directory, %jd in local header",
1343
+ (intmax_t)zip_entry_central_dir.uncompressed_size,
1344
+ (intmax_t)zip_entry->uncompressed_size);
1345
+ ret = ARCHIVE_WARN;
1346
+ }
1347
+ }
1348
+
1349
+ /* Populate some additional entry fields: */
1350
+ archive_entry_set_mode(entry, zip_entry->mode);
1351
+ archive_entry_set_uid(entry, zip_entry->uid);
1352
+ archive_entry_set_gid(entry, zip_entry->gid);
1353
+ archive_entry_set_mtime(entry, zip_entry->mtime, 0);
1354
+ archive_entry_set_ctime(entry, zip_entry->ctime, 0);
1355
+ archive_entry_set_atime(entry, zip_entry->atime, 0);
1356
+
1357
+ if ((zip->entry->mode & AE_IFMT) == AE_IFLNK) {
1358
+ size_t linkname_length;
1359
+
1360
+ if (zip_entry->compressed_size > 64 * 1024) {
1361
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1362
+ "Zip file with oversized link entry");
1363
+ return ARCHIVE_FATAL;
1364
+ }
1365
+
1366
+ linkname_length = (size_t)zip_entry->compressed_size;
1367
+
1368
+ archive_entry_set_size(entry, 0);
1369
+
1370
+ // take into account link compression if any
1371
+ size_t linkname_full_length = linkname_length;
1372
+ if (zip->entry->compression != 0)
1373
+ {
1374
+ // symlink target string appeared to be compressed
1375
+ int status = ARCHIVE_FATAL;
1376
+ const void *uncompressed_buffer = NULL;
1377
+
1378
+ switch (zip->entry->compression)
1379
+ {
1380
+ #if HAVE_ZLIB_H
1381
+ case 8: /* Deflate compression. */
1382
+ zip->entry_bytes_remaining = zip_entry->compressed_size;
1383
+ status = zip_read_data_deflate(a, &uncompressed_buffer,
1384
+ &linkname_full_length, NULL);
1385
+ break;
1386
+ #endif
1387
+ #if HAVE_LZMA_H && HAVE_LIBLZMA
1388
+ case 14: /* ZIPx LZMA compression. */
1389
+ /*(see zip file format specification, section 4.4.5)*/
1390
+ zip->entry_bytes_remaining = zip_entry->compressed_size;
1391
+ status = zip_read_data_zipx_lzma_alone(a, &uncompressed_buffer,
1392
+ &linkname_full_length, NULL);
1393
+ break;
1394
+ #endif
1395
+ default: /* Unsupported compression. */
1396
+ break;
1397
+ }
1398
+ if (status == ARCHIVE_OK)
1399
+ {
1400
+ p = uncompressed_buffer;
1401
+ }
1402
+ else
1403
+ {
1404
+ archive_set_error(&a->archive,
1405
+ ARCHIVE_ERRNO_FILE_FORMAT,
1406
+ "Unsupported ZIP compression method "
1407
+ "during decompression of link entry (%d: %s)",
1408
+ zip->entry->compression,
1409
+ compression_name(zip->entry->compression));
1410
+ return ARCHIVE_FAILED;
1411
+ }
1412
+ }
1413
+ else
1414
+ {
1415
+ p = __archive_read_ahead(a, linkname_length, NULL);
1416
+ }
1417
+
1418
+ if (p == NULL) {
1419
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1420
+ "Truncated Zip file");
1421
+ return ARCHIVE_FATAL;
1422
+ }
1423
+
1424
+ sconv = zip->sconv;
1425
+ if (sconv == NULL && (zip->entry->zip_flags & ZIP_UTF8_NAME))
1426
+ sconv = zip->sconv_utf8;
1427
+ if (sconv == NULL)
1428
+ sconv = zip->sconv_default;
1429
+ if (archive_entry_copy_symlink_l(entry, p, linkname_full_length,
1430
+ sconv) != 0) {
1431
+ if (errno != ENOMEM && sconv == zip->sconv_utf8 &&
1432
+ (zip->entry->zip_flags & ZIP_UTF8_NAME))
1433
+ archive_entry_copy_symlink_l(entry, p,
1434
+ linkname_full_length, NULL);
1435
+ if (errno == ENOMEM) {
1436
+ archive_set_error(&a->archive, ENOMEM,
1437
+ "Can't allocate memory for Symlink");
1438
+ return (ARCHIVE_FATAL);
1439
+ }
1440
+ /*
1441
+ * Since there is no character-set regulation for
1442
+ * symlink name, do not report the conversion error
1443
+ * in an automatic conversion.
1444
+ */
1445
+ if (sconv != zip->sconv_utf8 ||
1446
+ (zip->entry->zip_flags & ZIP_UTF8_NAME) == 0) {
1447
+ archive_set_error(&a->archive,
1448
+ ARCHIVE_ERRNO_FILE_FORMAT,
1449
+ "Symlink cannot be converted "
1450
+ "from %s to current locale",
1451
+ archive_string_conversion_charset_name(
1452
+ sconv));
1453
+ ret = ARCHIVE_WARN;
1454
+ }
1455
+ }
1456
+ zip_entry->uncompressed_size = zip_entry->compressed_size = 0;
1457
+
1458
+ if (__archive_read_consume(a, linkname_length) < 0) {
1459
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1460
+ "Read error skipping symlink target name");
1461
+ return ARCHIVE_FATAL;
1462
+ }
1463
+ } else if (zip_entry_size_is_set(zip_entry)) {
1464
+ archive_entry_set_size(entry, zip_entry->uncompressed_size);
1465
+ }
1466
+ zip->entry_bytes_remaining = zip_entry->compressed_size;
1467
+
1468
+ /* If there's no body, force read_data() to return EOF immediately. */
1469
+ if (0 == (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
1470
+ && zip->entry_bytes_remaining < 1)
1471
+ zip->end_of_entry = 1;
1472
+
1473
+ /* Set up a more descriptive format name. */
1474
+ archive_string_empty(&zip->format_name);
1475
+ archive_string_sprintf(&zip->format_name, "ZIP %d.%d (%s)",
1476
+ version / 10, version % 10,
1477
+ compression_name(zip->entry->compression));
1478
+ a->archive.archive_format_name = zip->format_name.s;
1479
+
1480
+ return (ret);
1481
+ }
1482
+
1483
+ static int
1484
+ check_authentication_code(struct archive_read *a, const void *_p)
1485
+ {
1486
+ struct zip *zip = a->format->data;
1487
+
1488
+ /* Check authentication code. */
1489
+ if (zip->hctx_valid) {
1490
+ const void *p;
1491
+ uint8_t hmac[20];
1492
+ size_t hmac_len = 20;
1493
+ int cmp;
1494
+
1495
+ archive_hmac_sha1_final(&zip->hctx, hmac, &hmac_len);
1496
+ if (_p == NULL) {
1497
+ /* Read authentication code. */
1498
+ p = __archive_read_ahead(a, AUTH_CODE_SIZE, NULL);
1499
+ if (p == NULL) {
1500
+ archive_set_error(&a->archive,
1501
+ ARCHIVE_ERRNO_FILE_FORMAT,
1502
+ "Truncated ZIP file data");
1503
+ return (ARCHIVE_FATAL);
1504
+ }
1505
+ } else {
1506
+ p = _p;
1507
+ }
1508
+ cmp = memcmp(hmac, p, AUTH_CODE_SIZE);
1509
+ __archive_read_consume(a, AUTH_CODE_SIZE);
1510
+ if (cmp != 0) {
1511
+ archive_set_error(&a->archive,
1512
+ ARCHIVE_ERRNO_MISC,
1513
+ "ZIP bad Authentication code");
1514
+ return (ARCHIVE_WARN);
1515
+ }
1516
+ }
1517
+ return (ARCHIVE_OK);
1518
+ }
1519
+
1520
+ /*
1521
+ * The Zip end-of-file marker is inherently ambiguous. The specification
1522
+ * in APPNOTE.TXT allows any of four possible formats, and there is no
1523
+ * guaranteed-correct way for a reader to know a priori which one the writer
1524
+ * will have used. The four formats are:
1525
+ * 1. 32-bit format with an initial PK78 marker
1526
+ * 2. 32-bit format without that marker
1527
+ * 3. 64-bit format with the marker
1528
+ * 4. 64-bit format without the marker
1529
+ *
1530
+ * Mark Adler's `sunzip` streaming unzip program solved this ambiguity
1531
+ * by just looking at every possible combination and accepting the
1532
+ * longest one that matches the expected values. His approach always
1533
+ * consumes the longest possible matching EOF marker, based on an
1534
+ * analysis of all the possible failures and how the values could
1535
+ * overlap.
1536
+ *
1537
+ * For example, suppose both of the first two formats listed
1538
+ * above match. In that case, we know the next four
1539
+ * 32-bit words match this pattern:
1540
+ * ```
1541
+ * [PK\07\08] [CRC32] [compressed size] [uncompressed size]
1542
+ * ```
1543
+ * but we know they must also match this pattern:
1544
+ * ```
1545
+ * [CRC32] [compressed size] [uncompressed size] [other PK marker]
1546
+ * ```
1547
+ *
1548
+ * Since the first word here matches both the PK78 signature in the
1549
+ * first form and the CRC32 in the second, we know those two values
1550
+ * are equal, the CRC32 must be exactly 0x08074b50. Similarly, the
1551
+ * compressed and uncompressed size must also be exactly this value.
1552
+ * So we know these four words are all 0x08074b50. If we were to
1553
+ * accept the shorter pattern, it would be immediately followed by
1554
+ * another PK78 marker, which is not possible in a well-formed ZIP
1555
+ * archive unless there is garbage between entries. This implies we
1556
+ * should not accept the shorter form in such a case; we should accept
1557
+ * the longer form.
1558
+ *
1559
+ * If the second and third possibilities above both match, we
1560
+ * have a slightly different situation. The following words
1561
+ * must match both the 32-bit format
1562
+ * ```
1563
+ * [CRC32] [compressed size] [uncompressed size] [other PK marker]
1564
+ * ```
1565
+ * and the 64-bit format
1566
+ * ```
1567
+ * [CRC32] [compressed low] [compressed high] [uncompressed low] [uncompressed high] [other PK marker]
1568
+ * ```
1569
+ * Since the 32-bit and 64-bit compressed sizes both match, the
1570
+ * actual size must fit in 32 bits, which implies the high-order
1571
+ * word of the compressed size is zero. So we know the uncompressed
1572
+ * low word is zero, which again implies that if we accept the shorter
1573
+ * format, there will not be a valid PK marker following it.
1574
+ *
1575
+ * Similar considerations rule out the shorter form in every other
1576
+ * possibly-ambiguous pair. So if two of the four possible formats
1577
+ * match, we should accept the longer option.
1578
+ *
1579
+ * If none of the four formats matches, we know the archive must be
1580
+ * corrupted in some fashion. In particular, it's possible that the
1581
+ * length-at-end bit was incorrect and we should not really be looking
1582
+ * for an EOF marker at all. To allow for this possibility, we
1583
+ * evaluate the following words to collect data for a later error
1584
+ * report but do not consume any bytes. We instead rely on the later
1585
+ * search for a new PK marker to re-sync to the next well-formed
1586
+ * entry.
1587
+ */
1588
+ static void
1589
+ consume_end_of_file_marker(struct archive_read *a, struct zip *zip)
1590
+ {
1591
+ const char *marker;
1592
+ const char *p;
1593
+ uint64_t compressed32, uncompressed32;
1594
+ uint64_t compressed64, uncompressed64;
1595
+ uint64_t compressed_actual, uncompressed_actual;
1596
+ uint32_t crc32_actual;
1597
+ const uint32_t PK78 = 0x08074B50ULL;
1598
+ uint8_t crc32_ignored, crc32_may_be_zero;
1599
+
1600
+ /* If there shouldn't be a marker, don't consume it. */
1601
+ if ((zip->entry->zip_flags & ZIP_LENGTH_AT_END) == 0) {
1602
+ return;
1603
+ }
1604
+
1605
+ /* The longest Zip end-of-file record is 24 bytes. Since an
1606
+ * end-of-file record can never appear at the end of the
1607
+ * archive, we know 24 bytes will be available unless
1608
+ * the archive is severely truncated. */
1609
+ if (NULL == (marker = __archive_read_ahead(a, 24, NULL))) {
1610
+ return;
1611
+ }
1612
+ p = marker;
1613
+
1614
+ /* The end-of-file record comprises:
1615
+ * = Optional PK\007\010 marker
1616
+ * = 4-byte CRC32
1617
+ * = Compressed size
1618
+ * = Uncompressed size
1619
+ *
1620
+ * The last two fields are either both 32 bits or both 64
1621
+ * bits. We check all possible layouts and accept any one
1622
+ * that gives us a complete match, else we make a best-effort
1623
+ * attempt to parse out the pieces.
1624
+ */
1625
+
1626
+ /* CRC32 checking can be tricky:
1627
+ * * Test suites sometimes ignore the CRC32
1628
+ * * AES AE-2 always writes zero for the CRC32
1629
+ * * AES AE-1 sometimes writes zero for the CRC32
1630
+ */
1631
+ crc32_ignored = zip->ignore_crc32;
1632
+ crc32_may_be_zero = 0;
1633
+ crc32_actual = zip->computed_crc32;
1634
+ if (zip->hctx_valid) {
1635
+ switch (zip->entry->aes_extra.vendor) {
1636
+ case AES_VENDOR_AE_2:
1637
+ crc32_actual = 0;
1638
+ break;
1639
+ case AES_VENDOR_AE_1:
1640
+ default:
1641
+ crc32_may_be_zero = 1;
1642
+ break;
1643
+ }
1644
+ }
1645
+
1646
+ /* Values computed from the actual data in the archive. */
1647
+ compressed_actual = (uint64_t)zip->entry_compressed_bytes_read;
1648
+ uncompressed_actual = (uint64_t)zip->entry_uncompressed_bytes_read;
1649
+
1650
+
1651
+ /* Longest: PK78 marker, all 64-bit fields (24 bytes total) */
1652
+ if (archive_le32dec(p) == PK78
1653
+ && ((archive_le32dec(p + 4) == crc32_actual)
1654
+ || (crc32_may_be_zero && (archive_le32dec(p + 4) == 0))
1655
+ || crc32_ignored)
1656
+ && (archive_le64dec(p + 8) == compressed_actual)
1657
+ && (archive_le64dec(p + 16) == uncompressed_actual)) {
1658
+ if (!crc32_ignored) {
1659
+ zip->entry->crc32 = crc32_actual;
1660
+ }
1661
+ zip->entry->compressed_size = compressed_actual;
1662
+ zip->entry->uncompressed_size = uncompressed_actual;
1663
+ zip->unconsumed += 24;
1664
+ return;
1665
+ }
1666
+
1667
+ /* No PK78 marker, 64-bit fields (20 bytes total) */
1668
+ if (((archive_le32dec(p) == crc32_actual)
1669
+ || (crc32_may_be_zero && (archive_le32dec(p + 4) == 0))
1670
+ || crc32_ignored)
1671
+ && (archive_le64dec(p + 4) == compressed_actual)
1672
+ && (archive_le64dec(p + 12) == uncompressed_actual)) {
1673
+ if (!crc32_ignored) {
1674
+ zip->entry->crc32 = crc32_actual;
1675
+ }
1676
+ zip->entry->compressed_size = compressed_actual;
1677
+ zip->entry->uncompressed_size = uncompressed_actual;
1678
+ zip->unconsumed += 20;
1679
+ return;
1680
+ }
1681
+
1682
+ /* PK78 marker and 32-bit fields (16 bytes total) */
1683
+ if (archive_le32dec(p) == PK78
1684
+ && ((archive_le32dec(p + 4) == crc32_actual)
1685
+ || (crc32_may_be_zero && (archive_le32dec(p + 4) == 0))
1686
+ || crc32_ignored)
1687
+ && (archive_le32dec(p + 8) == compressed_actual)
1688
+ && (archive_le32dec(p + 12) == uncompressed_actual)) {
1689
+ if (!crc32_ignored) {
1690
+ zip->entry->crc32 = crc32_actual;
1691
+ }
1692
+ zip->entry->compressed_size = compressed_actual;
1693
+ zip->entry->uncompressed_size = uncompressed_actual;
1694
+ zip->unconsumed += 16;
1695
+ return;
1696
+ }
1697
+
1698
+ /* Shortest: No PK78 marker, all 32-bit fields (12 bytes total) */
1699
+ if (((archive_le32dec(p) == crc32_actual)
1700
+ || (crc32_may_be_zero && (archive_le32dec(p + 4) == 0))
1701
+ || crc32_ignored)
1702
+ && (archive_le32dec(p + 4) == compressed_actual)
1703
+ && (archive_le32dec(p + 8) == uncompressed_actual)) {
1704
+ if (!crc32_ignored) {
1705
+ zip->entry->crc32 = crc32_actual;
1706
+ }
1707
+ zip->entry->compressed_size = compressed_actual;
1708
+ zip->entry->uncompressed_size = uncompressed_actual;
1709
+ zip->unconsumed += 12;
1710
+ return;
1711
+ }
1712
+
1713
+ /* None of the exact patterns matched. If entry size was unknown
1714
+ * (ZIP_LENGTH_AT_END flag), before treating this as
1715
+ * corruption, check whether the next ZIP record follows the data
1716
+ * immediately: a length-at-end entry whose compression format has
1717
+ * its own end-of-stream marker (e.g. PPMd) may be written with no
1718
+ * data descriptor at all. In that case the byte counts we measured
1719
+ * during decompression are authoritative, so trust them and leave
1720
+ * the stream untouched. */
1721
+ if (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
1722
+ {
1723
+ const uint32_t sig = archive_le32dec(p);
1724
+ if (sig == 0x04034b50U /* Local file header */
1725
+ || sig == 0x02014b50U /* Central directory record */
1726
+ || sig == 0x06054b50U) /* End of central directory */ {
1727
+ zip->entry->compressed_size = compressed_actual;
1728
+ zip->entry->uncompressed_size = uncompressed_actual;
1729
+ return;
1730
+ }
1731
+ }
1732
+
1733
+ /* If none of the above patterns gives us a full exact match,
1734
+ * then there's something definitely amiss. The fallback code
1735
+ * below will parse out some plausible values for error
1736
+ * reporting purposes. Note that this won't actually
1737
+ * consume anything:
1738
+ *
1739
+ * = If there really is a marker here, the logic to resync to
1740
+ * the next entry will suffice to skip it.
1741
+ *
1742
+ * = There might not really be a marker: Corruption or bugs
1743
+ * may have set the length-at-end bit without a marker ever
1744
+ * having actually been written. In this case, we
1745
+ * explicitly should not consume any bytes, since that would
1746
+ * prevent us from correctly reading the next entry.
1747
+ */
1748
+ if (archive_le32dec(p) == PK78) {
1749
+ p += 4; /* Ignore PK78 if it appears to be present */
1750
+ }
1751
+ zip->entry->crc32 = archive_le32dec(p); /* Parse CRC32 */
1752
+ p += 4;
1753
+
1754
+ /* Consider both 32- and 64-bit interpretations */
1755
+ compressed32 = archive_le32dec(p);
1756
+ uncompressed32 = archive_le32dec(p + 4);
1757
+ compressed64 = archive_le64dec(p);
1758
+ uncompressed64 = archive_le64dec(p + 8);
1759
+
1760
+ /* The earlier patterns may have failed because of CRC32
1761
+ * mismatch, so it's still possible that both sizes match.
1762
+ * Try to match as many as we can...
1763
+ */
1764
+ if (compressed32 == compressed_actual
1765
+ && uncompressed32 == uncompressed_actual) {
1766
+ /* Both 32-bit fields match */
1767
+ zip->entry->compressed_size = compressed32;
1768
+ zip->entry->uncompressed_size = uncompressed32;
1769
+ } else if (compressed64 == compressed_actual
1770
+ || uncompressed64 == uncompressed_actual) {
1771
+ /* One or both 64-bit fields match */
1772
+ zip->entry->compressed_size = compressed64;
1773
+ zip->entry->uncompressed_size = uncompressed64;
1774
+ } else {
1775
+ /* Zero or one 32-bit fields match */
1776
+ zip->entry->compressed_size = compressed32;
1777
+ zip->entry->uncompressed_size = uncompressed32;
1778
+ }
1779
+ }
1780
+
1781
+ /*
1782
+ * Read "uncompressed" data.
1783
+ *
1784
+ * This is straightforward if we know the size of the data. This is
1785
+ * always true for the seeking reader (we've examined the Central
1786
+ * Directory already), and will often be true for the streaming reader
1787
+ * (the writer was writing uncompressed so probably knows the size).
1788
+ *
1789
+ * If we don't know the size, then life is more interesting. Note
1790
+ * that a careful reading of the Zip specification says that a writer
1791
+ * must use ZIP_LENGTH_AT_END if it cannot write the CRC into the
1792
+ * local header. And if it uses ZIP_LENGTH_AT_END, then it is
1793
+ * prohibited from storing the sizes in the local header. This
1794
+ * prevents fully-compliant streaming writers from providing any size
1795
+ * clues to a streaming reader. In this case, we have to scan the
1796
+ * data as we read to try to locate the end-of-file marker.
1797
+ *
1798
+ * We assume here that the end-of-file marker always has the
1799
+ * PK\007\010 signature. Although it's technically optional, newer
1800
+ * writers seem to provide it pretty consistently, and it's not clear
1801
+ * how to efficiently recognize an end-of-file marker that lacks it.
1802
+ *
1803
+ * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
1804
+ * zip->end_of_entry if it consumes all of the data.
1805
+ */
1806
+ static int
1807
+ zip_read_data_none(struct archive_read *a, const void **_buff,
1808
+ size_t *size, int64_t *offset)
1809
+ {
1810
+ struct zip *zip = a->format->data;
1811
+ const char *buff;
1812
+ ssize_t bytes_avail;
1813
+ ssize_t trailing_extra;
1814
+ int r;
1815
+
1816
+ (void)offset; /* UNUSED */
1817
+
1818
+ trailing_extra = zip->hctx_valid ? AUTH_CODE_SIZE : 0;
1819
+
1820
+ if (zip->entry->zip_flags & ZIP_LENGTH_AT_END) {
1821
+ const char *p;
1822
+ ssize_t grabbing_bytes = 24 + trailing_extra;
1823
+
1824
+ /* Grab at least 24 bytes. */
1825
+ buff = __archive_read_ahead(a, grabbing_bytes, &bytes_avail);
1826
+ if (bytes_avail < grabbing_bytes) {
1827
+ /* Zip archives have end-of-archive markers
1828
+ that are longer than this, so a failure to get at
1829
+ least 24 bytes really does indicate a truncated
1830
+ file. */
1831
+ archive_set_error(&a->archive,
1832
+ ARCHIVE_ERRNO_FILE_FORMAT,
1833
+ "Truncated ZIP file data");
1834
+ return (ARCHIVE_FATAL);
1835
+ }
1836
+ /* Check for a complete PK\007\010 signature, followed
1837
+ * by the correct 4-byte CRC. */
1838
+ p = buff + trailing_extra;
1839
+ if (p[0] == 'P' && p[1] == 'K'
1840
+ && p[2] == '\007' && p[3] == '\010'
1841
+ && (archive_le32dec(p + 4) == zip->computed_crc32
1842
+ || zip->ignore_crc32
1843
+ || (zip->hctx_valid
1844
+ && zip->entry->aes_extra.vendor == AES_VENDOR_AE_2))) {
1845
+ zip->end_of_entry = 1;
1846
+ if (zip->hctx_valid) {
1847
+ r = check_authentication_code(a, buff);
1848
+ if (r != ARCHIVE_OK)
1849
+ return (r);
1850
+ }
1851
+ return (ARCHIVE_OK);
1852
+ }
1853
+ /* If not at EOF, ensure we consume at least one byte. */
1854
+ ++p;
1855
+
1856
+ /* Scan forward until we see where a PK\007\010 signature
1857
+ * might be. */
1858
+ /* Return bytes up until that point. On the next call,
1859
+ * the code above will verify the data descriptor. */
1860
+ while (p < buff + bytes_avail - 4) {
1861
+ if (p[3] == 'P') { p += 3; }
1862
+ else if (p[3] == 'K') { p += 2; }
1863
+ else if (p[3] == '\007') { p += 1; }
1864
+ else if (p[3] == '\010' && p[2] == '\007'
1865
+ && p[1] == 'K' && p[0] == 'P') {
1866
+ break;
1867
+ } else { p += 4; }
1868
+ }
1869
+ p -= trailing_extra;
1870
+ bytes_avail = p - buff;
1871
+ } else {
1872
+ if (zip->entry_bytes_remaining == 0) {
1873
+ zip->end_of_entry = 1;
1874
+ if (zip->hctx_valid) {
1875
+ r = check_authentication_code(a, NULL);
1876
+ if (r != ARCHIVE_OK)
1877
+ return (r);
1878
+ }
1879
+ return (ARCHIVE_OK);
1880
+ }
1881
+ /* Grab a bunch of bytes. */
1882
+ buff = __archive_read_ahead(a, 1, &bytes_avail);
1883
+ if (bytes_avail <= 0) {
1884
+ archive_set_error(&a->archive,
1885
+ ARCHIVE_ERRNO_FILE_FORMAT,
1886
+ "Truncated ZIP file data");
1887
+ return (ARCHIVE_FATAL);
1888
+ }
1889
+ if (bytes_avail > zip->entry_bytes_remaining)
1890
+ bytes_avail = (ssize_t)zip->entry_bytes_remaining;
1891
+ }
1892
+ if (zip->tctx_valid || zip->cctx_valid) {
1893
+ size_t dec_size = bytes_avail;
1894
+
1895
+ if (dec_size > zip->decrypted_buffer_size)
1896
+ dec_size = zip->decrypted_buffer_size;
1897
+ if (zip->tctx_valid) {
1898
+ trad_enc_decrypt_update(&zip->tctx,
1899
+ (const uint8_t *)buff, dec_size,
1900
+ zip->decrypted_buffer, dec_size);
1901
+ } else {
1902
+ size_t dsize = dec_size;
1903
+ archive_hmac_sha1_update(&zip->hctx,
1904
+ (const uint8_t *)buff, dec_size);
1905
+ archive_decrypto_aes_ctr_update(&zip->cctx,
1906
+ (const uint8_t *)buff, dec_size,
1907
+ zip->decrypted_buffer, &dsize);
1908
+ }
1909
+ bytes_avail = dec_size;
1910
+ buff = (const char *)zip->decrypted_buffer;
1911
+ }
1912
+ zip->entry_bytes_remaining -= bytes_avail;
1913
+ zip->entry_uncompressed_bytes_read += bytes_avail;
1914
+ zip->entry_compressed_bytes_read += bytes_avail;
1915
+ zip->unconsumed += bytes_avail;
1916
+ *size = bytes_avail;
1917
+ *_buff = buff;
1918
+ return (ARCHIVE_OK);
1919
+ }
1920
+
1921
+ #if HAVE_LZMA_H && HAVE_LIBLZMA
1922
+ static int
1923
+ zipx_xz_init(struct archive_read *a, struct zip *zip)
1924
+ {
1925
+ lzma_ret r;
1926
+
1927
+ if(zip->zipx_lzma_valid) {
1928
+ lzma_end(&zip->zipx_lzma_stream);
1929
+ zip->zipx_lzma_valid = 0;
1930
+ }
1931
+
1932
+ memset(&zip->zipx_lzma_stream, 0, sizeof(zip->zipx_lzma_stream));
1933
+ r = lzma_stream_decoder(&zip->zipx_lzma_stream, UINT64_MAX, 0);
1934
+ if (r != LZMA_OK) {
1935
+ archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
1936
+ "xz initialization failed (%d)",
1937
+ r);
1938
+
1939
+ return (ARCHIVE_FAILED);
1940
+ }
1941
+
1942
+ zip->zipx_lzma_valid = 1;
1943
+
1944
+ free(zip->uncompressed_buffer);
1945
+
1946
+ zip->uncompressed_buffer_size = 256 * 1024;
1947
+ zip->uncompressed_buffer = malloc(zip->uncompressed_buffer_size);
1948
+ if (zip->uncompressed_buffer == NULL) {
1949
+ archive_set_error(&a->archive, ENOMEM,
1950
+ "No memory for xz decompression");
1951
+ return (ARCHIVE_FATAL);
1952
+ }
1953
+
1954
+ zip->decompress_init = 1;
1955
+ return (ARCHIVE_OK);
1956
+ }
1957
+
1958
+ static int
1959
+ zipx_lzma_alone_init(struct archive_read *a, struct zip *zip)
1960
+ {
1961
+ lzma_ret r;
1962
+ const uint8_t* p;
1963
+
1964
+ #pragma pack(push)
1965
+ #pragma pack(1)
1966
+ struct _alone_header {
1967
+ uint8_t bytes[5];
1968
+ uint64_t uncompressed_size;
1969
+ } alone_header;
1970
+ #pragma pack(pop)
1971
+
1972
+ if(zip->zipx_lzma_valid) {
1973
+ lzma_end(&zip->zipx_lzma_stream);
1974
+ zip->zipx_lzma_valid = 0;
1975
+ }
1976
+
1977
+ /* To unpack ZIPX's "LZMA" (id 14) stream we can use standard liblzma
1978
+ * that is a part of XZ Utils. The stream format stored inside ZIPX
1979
+ * file is a modified "lzma alone" file format, that was used by the
1980
+ * `lzma` utility which was later deprecated in favour of `xz` utility.
1981
+ * Since those formats are nearly the same, we can use a standard
1982
+ * "lzma alone" decoder from XZ Utils. */
1983
+
1984
+ memset(&zip->zipx_lzma_stream, 0, sizeof(zip->zipx_lzma_stream));
1985
+ r = lzma_alone_decoder(&zip->zipx_lzma_stream, 576 * ((uint64_t)1 << 20));
1986
+ if (r != LZMA_OK) {
1987
+ archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
1988
+ "lzma initialization failed (%d)", r);
1989
+
1990
+ return (ARCHIVE_FAILED);
1991
+ }
1992
+
1993
+ /* Flag the cleanup function that we want our lzma-related structures
1994
+ * to be freed later. */
1995
+ zip->zipx_lzma_valid = 1;
1996
+
1997
+ /* The "lzma alone" file format and the stream format inside ZIPx are
1998
+ * almost the same. Here's an example of a structure of "lzma alone"
1999
+ * format:
2000
+ *
2001
+ * $ cat /bin/ls | lzma | xxd | head -n 1
2002
+ * 00000000: 5d00 0080 00ff ffff ffff ffff ff00 2814
2003
+ *
2004
+ * 5 bytes 8 bytes n bytes
2005
+ * <lzma_params><uncompressed_size><data...>
2006
+ *
2007
+ * lzma_params is a 5-byte blob that has to be decoded to extract
2008
+ * parameters of this LZMA stream. The uncompressed_size field is an
2009
+ * uint64_t value that contains information about the size of the
2010
+ * uncompressed file, or UINT64_MAX if this value is unknown.
2011
+ * The <data...> part is the actual lzma-compressed data stream.
2012
+ *
2013
+ * Now here's the structure of the stream inside the ZIPX file:
2014
+ *
2015
+ * $ cat stream_inside_zipx | xxd | head -n 1
2016
+ * 00000000: 0914 0500 5d00 8000 0000 2814 .... ....
2017
+ *
2018
+ * 2byte 2byte 5 bytes n bytes
2019
+ * <magic1><magic2><lzma_params><data...>
2020
+ *
2021
+ * This means that the ZIPX file contains an additional magic1 and
2022
+ * magic2 headers, the lzma_params field contains the same parameter
2023
+ * set as in the "lzma alone" format, and the <data...> field is the
2024
+ * same as in the "lzma alone" format as well. Note that also the zipx
2025
+ * format is missing the uncompressed_size field.
2026
+ *
2027
+ * So, in order to use the "lzma alone" decoder for the zipx lzma
2028
+ * stream, we simply need to shuffle around some fields, prepare a new
2029
+ * lzma alone header, feed it into lzma alone decoder so it will
2030
+ * initialize itself properly, and then we can start feeding normal
2031
+ * zipx lzma stream into the decoder.
2032
+ */
2033
+
2034
+ /* Read magic1,magic2,lzma_params from the ZIPX stream. */
2035
+ /* When the compressed size is unknown (e.g. ZIP_LENGTH_AT_END read
2036
+ * from a non-seekable source), entry_bytes_remaining is 0 or negative
2037
+ * here. We can still attempt to read the 9-byte header; if the data
2038
+ * is truly truncated, the __archive_read_ahead calls below will catch
2039
+ * it. */
2040
+ if(zip->entry_bytes_remaining > 0
2041
+ && zip->entry_bytes_remaining < 9) {
2042
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2043
+ "Truncated lzma data");
2044
+ return (ARCHIVE_FATAL);
2045
+ }
2046
+
2047
+ if (zip->tctx_valid || zip->cctx_valid) {
2048
+ const void *decrypted;
2049
+ size_t out_len;
2050
+ size_t consumed;
2051
+ int ret;
2052
+
2053
+ ret = zipx_read_header_and_decrypt(a, &decrypted, 9, &out_len, &consumed);
2054
+ if (ret != ARCHIVE_OK)
2055
+ return ret;
2056
+ p = decrypted;
2057
+ } else {
2058
+ p = __archive_read_ahead(a, 9, NULL);
2059
+ if (p == NULL) {
2060
+ archive_set_error(&a->archive,
2061
+ ARCHIVE_ERRNO_FILE_FORMAT,
2062
+ "Truncated lzma data");
2063
+ return (ARCHIVE_FATAL);
2064
+ }
2065
+ }
2066
+
2067
+ if(p[2] != 0x05 || p[3] != 0x00) {
2068
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2069
+ "Invalid lzma data");
2070
+ return (ARCHIVE_FATAL);
2071
+ }
2072
+
2073
+ /* Prepare an lzma alone header: copy the lzma_params blob into
2074
+ * a proper place into the lzma alone header. */
2075
+ memcpy(&alone_header.bytes[0], p + 4, 5);
2076
+
2077
+ /* Initialize the 'uncompressed size' field to unknown; we'll manually
2078
+ * monitor how many bytes there are still to be uncompressed. */
2079
+ alone_header.uncompressed_size = UINT64_MAX;
2080
+
2081
+ if(!zip->uncompressed_buffer) {
2082
+ zip->uncompressed_buffer_size = 256 * 1024;
2083
+ zip->uncompressed_buffer = malloc(zip->uncompressed_buffer_size);
2084
+
2085
+ if (zip->uncompressed_buffer == NULL) {
2086
+ archive_set_error(&a->archive, ENOMEM,
2087
+ "No memory for lzma decompression");
2088
+ return (ARCHIVE_FATAL);
2089
+ }
2090
+ }
2091
+
2092
+ zip->zipx_lzma_stream.next_in = (void*) &alone_header;
2093
+ zip->zipx_lzma_stream.avail_in = sizeof(alone_header);
2094
+ zip->zipx_lzma_stream.total_in = 0;
2095
+ zip->zipx_lzma_stream.next_out = zip->uncompressed_buffer;
2096
+ zip->zipx_lzma_stream.avail_out = zip->uncompressed_buffer_size;
2097
+ zip->zipx_lzma_stream.total_out = 0;
2098
+
2099
+ /* Feed only the header into the lzma alone decoder. This will
2100
+ * effectively initialize the decoder, and will not produce any
2101
+ * output bytes yet. */
2102
+ r = lzma_code(&zip->zipx_lzma_stream, LZMA_RUN);
2103
+ if (r != LZMA_OK) {
2104
+ if (r == LZMA_MEMLIMIT_ERROR)
2105
+ archive_set_error(&a->archive, ENOMEM,
2106
+ "lzma stream requires too much memory");
2107
+ else
2108
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
2109
+ "lzma stream initialization error");
2110
+ return ARCHIVE_FATAL;
2111
+ }
2112
+
2113
+ /* We've already consumed some bytes, so take this into account. */
2114
+ __archive_read_consume(a, 9);
2115
+ zip->entry_compressed_bytes_read += 9;
2116
+ if (zip->entry_bytes_remaining > 0) {
2117
+ zip->entry_bytes_remaining -= 9;
2118
+ }
2119
+
2120
+ zip->decompress_init = 1;
2121
+ return (ARCHIVE_OK);
2122
+ }
2123
+
2124
+ static int
2125
+ zip_read_data_zipx_xz(struct archive_read *a, const void **buff,
2126
+ size_t *size, int64_t *offset)
2127
+ {
2128
+ struct zip *zip = a->format->data;
2129
+ int ret;
2130
+ lzma_ret lz_ret;
2131
+ const void* compressed_buf;
2132
+ const void* sp;
2133
+ ssize_t bytes_avail, to_consume = 0;
2134
+
2135
+ (void) offset; /* UNUSED */
2136
+
2137
+ /* Initialize decompressor if not yet initialized. */
2138
+ if (!zip->decompress_init) {
2139
+ ret = zipx_xz_init(a, zip);
2140
+ if (ret != ARCHIVE_OK)
2141
+ return (ret);
2142
+ }
2143
+
2144
+ compressed_buf = sp = __archive_read_ahead(a, 1, &bytes_avail);
2145
+ if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
2146
+ && bytes_avail > zip->entry_bytes_remaining) {
2147
+ bytes_avail = (ssize_t)zip->entry_bytes_remaining;
2148
+ }
2149
+ if (bytes_avail < 0) {
2150
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2151
+ "Truncated xz file body");
2152
+ return (ARCHIVE_FATAL);
2153
+ }
2154
+
2155
+ zip_read_decrypt(zip, compressed_buf, bytes_avail,
2156
+ &compressed_buf, &bytes_avail, &sp);
2157
+
2158
+ zip->zipx_lzma_stream.next_in = compressed_buf;
2159
+ zip->zipx_lzma_stream.avail_in = bytes_avail;
2160
+ zip->zipx_lzma_stream.total_in = 0;
2161
+ zip->zipx_lzma_stream.next_out = zip->uncompressed_buffer;
2162
+ zip->zipx_lzma_stream.avail_out = zip->uncompressed_buffer_size;
2163
+ zip->zipx_lzma_stream.total_out = 0;
2164
+
2165
+ /* Perform the decompression. */
2166
+ lz_ret = lzma_code(&zip->zipx_lzma_stream, LZMA_RUN);
2167
+ switch(lz_ret) {
2168
+ case LZMA_DATA_ERROR:
2169
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2170
+ "xz data error (%d)", (int) lz_ret);
2171
+ return (ARCHIVE_FATAL);
2172
+
2173
+ case LZMA_NO_CHECK:
2174
+ case LZMA_OK:
2175
+ break;
2176
+
2177
+ default:
2178
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2179
+ "xz unknown error (%d)", (int) lz_ret);
2180
+ return (ARCHIVE_FATAL);
2181
+
2182
+ case LZMA_STREAM_END:
2183
+ lzma_end(&zip->zipx_lzma_stream);
2184
+ zip->zipx_lzma_valid = 0;
2185
+
2186
+ /* This assertion is only possible if the size of the compressed data
2187
+ * stream is known -> !ZIP_LENGTH_AT_END */
2188
+ if((int64_t) zip->zipx_lzma_stream.total_in != zip->entry_bytes_remaining
2189
+ && !(zip->entry->zip_flags & ZIP_LENGTH_AT_END))
2190
+ {
2191
+ archive_set_error(&a->archive,
2192
+ ARCHIVE_ERRNO_MISC,
2193
+ "xz premature end of stream");
2194
+ return (ARCHIVE_FATAL);
2195
+ }
2196
+
2197
+ zip->end_of_entry = 1;
2198
+ break;
2199
+ }
2200
+
2201
+ to_consume = (ssize_t)zip->zipx_lzma_stream.total_in;
2202
+
2203
+ __archive_read_consume(a, to_consume);
2204
+ zip->entry_bytes_remaining -= to_consume;
2205
+ zip->entry_compressed_bytes_read += to_consume;
2206
+ zip->entry_uncompressed_bytes_read += zip->zipx_lzma_stream.total_out;
2207
+
2208
+ zip_read_decrypt_update(zip, to_consume, sp);
2209
+
2210
+ if (zip->end_of_entry) {
2211
+ if (zip->hctx_valid) {
2212
+ ret = check_authentication_code(a, NULL);
2213
+ if (ret != ARCHIVE_OK)
2214
+ return ret;
2215
+ }
2216
+ }
2217
+
2218
+ *size = (size_t)zip->zipx_lzma_stream.total_out;
2219
+ *buff = zip->uncompressed_buffer;
2220
+
2221
+ return (ARCHIVE_OK);
2222
+ }
2223
+
2224
+ static int
2225
+ zip_read_data_zipx_lzma_alone(struct archive_read *a, const void **buff,
2226
+ size_t *size, int64_t *offset)
2227
+ {
2228
+ struct zip *zip = a->format->data;
2229
+ int ret;
2230
+ lzma_ret lz_ret;
2231
+ const void* compressed_buf;
2232
+ const void* sp;
2233
+ ssize_t bytes_avail, to_consume;
2234
+
2235
+ (void) offset; /* UNUSED */
2236
+
2237
+ /* Initialize decompressor if not yet initialized. */
2238
+ if (!zip->decompress_init) {
2239
+ ret = zipx_lzma_alone_init(a, zip);
2240
+ if (ret != ARCHIVE_OK)
2241
+ return (ret);
2242
+ }
2243
+
2244
+ /* Fetch more compressed data. The same note as in deflate handler
2245
+ * applies here as well:
2246
+ *
2247
+ * Note: '1' here is a performance optimization. Recall that the
2248
+ * decompression layer returns a count of available bytes; asking for
2249
+ * more than that forces the decompressor to combine reads by copying
2250
+ * data.
2251
+ */
2252
+ compressed_buf = __archive_read_ahead(a, 1, &bytes_avail);
2253
+ if (zip->entry_bytes_remaining > 0
2254
+ && bytes_avail > zip->entry_bytes_remaining) {
2255
+ bytes_avail = (ssize_t)zip->entry_bytes_remaining;
2256
+ }
2257
+ if (bytes_avail < 0) {
2258
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2259
+ "Truncated lzma file body");
2260
+ return (ARCHIVE_FATAL);
2261
+ }
2262
+
2263
+ zip_read_decrypt(zip, compressed_buf, bytes_avail,
2264
+ &compressed_buf, &bytes_avail, &sp);
2265
+
2266
+ zip->zipx_lzma_stream.next_in = compressed_buf;
2267
+ zip->zipx_lzma_stream.avail_in = bytes_avail;
2268
+ zip->zipx_lzma_stream.total_in = 0;
2269
+ zip->zipx_lzma_stream.next_out = zip->uncompressed_buffer;
2270
+ /* These lzma_alone streams lack an end of stream marker in some
2271
+ * cases, so when the uncompressed size is known we cap avail_out to
2272
+ * make sure the unpacker won't try to unpack more than it's supposed
2273
+ * to. When the compressed size is unknown (entry_bytes_remaining <= 0,
2274
+ * e.g. ZIP_LENGTH_AT_END from a non-seekable source) we must use the
2275
+ * full buffer and rely on the LZMA stream end marker to detect the end
2276
+ * of the entry. */
2277
+ if (zip->entry_bytes_remaining <= 0) {
2278
+ zip->zipx_lzma_stream.avail_out = zip->uncompressed_buffer_size;
2279
+ } else {
2280
+ zip->zipx_lzma_stream.avail_out =
2281
+ (size_t)zipmin((int64_t) zip->uncompressed_buffer_size,
2282
+ zip->entry->uncompressed_size -
2283
+ zip->entry_uncompressed_bytes_read);
2284
+ }
2285
+ zip->zipx_lzma_stream.total_out = 0;
2286
+
2287
+ /* Perform the decompression. */
2288
+ lz_ret = lzma_code(&zip->zipx_lzma_stream, LZMA_RUN);
2289
+ switch(lz_ret) {
2290
+ case LZMA_DATA_ERROR:
2291
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2292
+ "lzma data error (%d)", (int) lz_ret);
2293
+ return (ARCHIVE_FATAL);
2294
+
2295
+ /* This case is optional in lzma alone format. It can happen,
2296
+ * but most of the files don't have it. (GitHub #1257) */
2297
+ case LZMA_STREAM_END:
2298
+ /* This assertion is only possible if the size of the
2299
+ * compressed data stream is known. */
2300
+ if((int64_t) zip->zipx_lzma_stream.total_in !=
2301
+ zip->entry_bytes_remaining
2302
+ && zip->entry_bytes_remaining > 0)
2303
+ {
2304
+ archive_set_error(&a->archive,
2305
+ ARCHIVE_ERRNO_MISC,
2306
+ "lzma alone premature end of stream");
2307
+ return (ARCHIVE_FATAL);
2308
+ }
2309
+
2310
+ zip->end_of_entry = 1;
2311
+ break;
2312
+
2313
+ case LZMA_OK:
2314
+ break;
2315
+
2316
+ case LZMA_BUF_ERROR:
2317
+ if (zip->zipx_lzma_stream.avail_out == 0) {
2318
+ /* The output buffer was filled exactly. When
2319
+ * the uncompressed size is known this means we
2320
+ * have decompressed all expected bytes. When
2321
+ * the size is unknown a full buffer just means
2322
+ * we need another iteration. */
2323
+ if (zip->entry_bytes_remaining > 0)
2324
+ zip->end_of_entry = 1;
2325
+ break;
2326
+ }
2327
+ /* FALL THROUGH */
2328
+ default:
2329
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2330
+ "lzma unknown error (%d)", (int) lz_ret);
2331
+ return (ARCHIVE_FATAL);
2332
+ }
2333
+
2334
+ to_consume = (ssize_t)zip->zipx_lzma_stream.total_in;
2335
+
2336
+ /* Update pointers. */
2337
+ __archive_read_consume(a, to_consume);
2338
+ zip->entry_compressed_bytes_read += to_consume;
2339
+ zip->entry_uncompressed_bytes_read += zip->zipx_lzma_stream.total_out;
2340
+
2341
+ zip_read_decrypt_update(zip, to_consume, sp);
2342
+
2343
+ if(zip->entry_bytes_remaining > 0) {
2344
+ zip->entry_bytes_remaining -= to_consume;
2345
+ if(zip->entry_bytes_remaining == 0) {
2346
+ zip->end_of_entry = 1;
2347
+ }
2348
+ }
2349
+
2350
+ if(zip->end_of_entry && zip->entry_bytes_remaining > 0) {
2351
+ ssize_t remaining = (ssize_t)zip->entry_bytes_remaining;
2352
+ const void *p = __archive_read_ahead(a, remaining, NULL);
2353
+ if (p != NULL) {
2354
+ if (zip->hctx_valid)
2355
+ archive_hmac_sha1_update(&zip->hctx,
2356
+ p, remaining);
2357
+ __archive_read_consume(a, remaining);
2358
+ zip->entry_compressed_bytes_read += remaining;
2359
+ zip->entry_bytes_remaining = 0;
2360
+ }
2361
+ }
2362
+
2363
+ /* Free lzma decoder handle because we'll no longer need it. */
2364
+ /* This cannot be folded into LZMA_STREAM_END handling above
2365
+ * because the stream end marker is not required in this format. */
2366
+ if(zip->end_of_entry) {
2367
+ lzma_end(&zip->zipx_lzma_stream);
2368
+ zip->zipx_lzma_valid = 0;
2369
+
2370
+ if (zip->hctx_valid) {
2371
+ ret = check_authentication_code(a, NULL);
2372
+ if (ret != ARCHIVE_OK)
2373
+ return ret;
2374
+ }
2375
+ }
2376
+
2377
+ /* Return values. */
2378
+ *size = (size_t)zip->zipx_lzma_stream.total_out;
2379
+ *buff = zip->uncompressed_buffer;
2380
+
2381
+ /* If we're here, then we're good! */
2382
+ return (ARCHIVE_OK);
2383
+ }
2384
+ #endif /* HAVE_LZMA_H && HAVE_LIBLZMA */
2385
+
2386
+ static int
2387
+ zipx_ppmd8_init(struct archive_read *a, struct zip *zip)
2388
+ {
2389
+ const void* p;
2390
+ uint32_t val;
2391
+ uint32_t order;
2392
+ uint32_t mem;
2393
+ uint32_t restore_method;
2394
+
2395
+ /* Remove previous decompression context if it exists. */
2396
+ if(zip->ppmd8_valid) {
2397
+ __archive_ppmd8_functions.Ppmd8_Free(&zip->ppmd8);
2398
+ zip->ppmd8_valid = 0;
2399
+ }
2400
+
2401
+ /* Create a new decompression context. */
2402
+ __archive_ppmd8_functions.Ppmd8_Construct(&zip->ppmd8);
2403
+ zip->ppmd8_stream_failed = 0;
2404
+
2405
+ /* Setup function pointers required by Ppmd8 decompressor. The
2406
+ * 'ppmd_read' function will feed new bytes to the decompressor,
2407
+ * and will increment the 'zip->zipx_ppmd_read_compressed' counter. */
2408
+ zip->ppmd8.Stream.In = &zip->zipx_ppmd_stream;
2409
+ zip->zipx_ppmd_stream.a = a;
2410
+ zip->zipx_ppmd_stream.Read = &ppmd_read;
2411
+
2412
+ /* Reset number of read bytes to 0. */
2413
+ zip->zipx_ppmd_read_compressed = 0;
2414
+
2415
+ /* Read Ppmd8 header (2 bytes). */
2416
+ if (zip->tctx_valid || zip->cctx_valid) {
2417
+ size_t out_len;
2418
+ size_t consumed;
2419
+ int ret;
2420
+
2421
+ ret = zipx_read_header_and_decrypt(a, &p, 2, &out_len, &consumed);
2422
+ if (ret != ARCHIVE_OK)
2423
+ return ret;
2424
+ } else {
2425
+ p = __archive_read_ahead(a, 2, NULL);
2426
+ if(!p) {
2427
+ archive_set_error(&a->archive,
2428
+ ARCHIVE_ERRNO_FILE_FORMAT,
2429
+ "Truncated file data in PPMd8 stream");
2430
+ return (ARCHIVE_FATAL);
2431
+ }
2432
+ }
2433
+ __archive_read_consume(a, 2);
2434
+
2435
+ /* Decode the stream's compression parameters. */
2436
+ val = archive_le16dec(p);
2437
+ order = (val & 15) + 1;
2438
+ mem = ((val >> 4) & 0xff) + 1;
2439
+ restore_method = (val >> 12);
2440
+
2441
+ if(order < 2 || restore_method > 2) {
2442
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2443
+ "Invalid parameter set in PPMd8 stream (order=%" PRIu32 ", "
2444
+ "restore=%" PRIu32 ")", order, restore_method);
2445
+ return (ARCHIVE_FAILED);
2446
+ }
2447
+
2448
+ /* Allocate the memory needed to properly decompress the file. */
2449
+ if(!__archive_ppmd8_functions.Ppmd8_Alloc(&zip->ppmd8, mem << 20)) {
2450
+ archive_set_error(&a->archive, ENOMEM,
2451
+ "Unable to allocate memory for PPMd8 stream: %" PRIu32 " bytes",
2452
+ mem << 20);
2453
+ return (ARCHIVE_FATAL);
2454
+ }
2455
+
2456
+ /* Signal the cleanup function to release Ppmd8 context in the
2457
+ * cleanup phase. */
2458
+ zip->ppmd8_valid = 1;
2459
+
2460
+ /* Perform further Ppmd8 initialization. */
2461
+ if(!__archive_ppmd8_functions.Ppmd8_RangeDec_Init(&zip->ppmd8)) {
2462
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
2463
+ "PPMd8 stream range decoder initialization error");
2464
+ return (ARCHIVE_FATAL);
2465
+ }
2466
+
2467
+ __archive_ppmd8_functions.Ppmd8_Init(&zip->ppmd8, order,
2468
+ restore_method);
2469
+
2470
+ /* Allocate the buffer that will hold uncompressed data. */
2471
+ free(zip->uncompressed_buffer);
2472
+
2473
+ zip->uncompressed_buffer_size = 256 * 1024;
2474
+ zip->uncompressed_buffer = malloc(zip->uncompressed_buffer_size);
2475
+
2476
+ if(zip->uncompressed_buffer == NULL) {
2477
+ archive_set_error(&a->archive, ENOMEM,
2478
+ "No memory for PPMd8 decompression");
2479
+ return ARCHIVE_FATAL;
2480
+ }
2481
+
2482
+ /* Ppmd8 initialization is done. */
2483
+ zip->decompress_init = 1;
2484
+
2485
+ /* We've already read 2 bytes in the output stream. Additionally,
2486
+ * Ppmd8 initialization code could read some data as well. So we
2487
+ * are advancing the stream by 2 bytes plus whatever number of
2488
+ * bytes Ppmd8 init function used. */
2489
+ zip->entry_compressed_bytes_read += 2 + zip->zipx_ppmd_read_compressed;
2490
+
2491
+ return ARCHIVE_OK;
2492
+ }
2493
+
2494
+ static int
2495
+ zip_read_data_zipx_ppmd(struct archive_read *a, const void **buff,
2496
+ size_t *size, int64_t *offset)
2497
+ {
2498
+ struct zip *zip = a->format->data;
2499
+ int ret;
2500
+ size_t consumed_bytes = 0;
2501
+
2502
+ (void) offset; /* UNUSED */
2503
+
2504
+ /* If we're here for the first time, initialize Ppmd8 decompression
2505
+ * context first. */
2506
+ if(!zip->decompress_init) {
2507
+ ret = zipx_ppmd8_init(a, zip);
2508
+ if(ret != ARCHIVE_OK)
2509
+ return ret;
2510
+ }
2511
+
2512
+ /* Fetch for more data. We're reading 1 byte here, but libarchive
2513
+ * should prefetch more bytes. */
2514
+ if(__archive_read_ahead(a, 1, NULL) == NULL) {
2515
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2516
+ "Truncated PPMd8 file body");
2517
+ return (ARCHIVE_FATAL);
2518
+ }
2519
+
2520
+ /* This counter will be updated inside ppmd_read(), which at one
2521
+ * point will be called by Ppmd8_DecodeSymbol. */
2522
+ zip->zipx_ppmd_read_compressed = 0;
2523
+
2524
+ /* Decompression loop. */
2525
+ do {
2526
+ int sym = __archive_ppmd8_functions.Ppmd8_DecodeSymbol(
2527
+ &zip->ppmd8);
2528
+ if(sym < 0) {
2529
+ zip->end_of_entry = 1;
2530
+ break;
2531
+ }
2532
+
2533
+ /* This field is set by ppmd_read() when there was no more data
2534
+ * to be read. */
2535
+ if(zip->ppmd8_stream_failed) {
2536
+ archive_set_error(&a->archive,
2537
+ ARCHIVE_ERRNO_FILE_FORMAT,
2538
+ "Truncated PPMd8 file body");
2539
+ return (ARCHIVE_FATAL);
2540
+ }
2541
+
2542
+ zip->uncompressed_buffer[consumed_bytes] = (uint8_t) sym;
2543
+ ++consumed_bytes;
2544
+ } while(consumed_bytes < zip->uncompressed_buffer_size);
2545
+
2546
+ /* Update pointers so we can continue decompression in another call. */
2547
+ zip->entry_bytes_remaining -= zip->zipx_ppmd_read_compressed;
2548
+ zip->entry_compressed_bytes_read += zip->zipx_ppmd_read_compressed;
2549
+ zip->entry_uncompressed_bytes_read += consumed_bytes;
2550
+
2551
+ /* If we're at the end of stream, deinitialize Ppmd8 context. */
2552
+ if(zip->end_of_entry) {
2553
+ __archive_ppmd8_functions.Ppmd8_Free(&zip->ppmd8);
2554
+ zip->ppmd8_valid = 0;
2555
+
2556
+ if (zip->hctx_valid) {
2557
+ int r = check_authentication_code(a, NULL);
2558
+ if (r != ARCHIVE_OK)
2559
+ return (r);
2560
+ }
2561
+ }
2562
+
2563
+ /* Update pointers for libarchive. */
2564
+ *buff = zip->uncompressed_buffer;
2565
+ *size = consumed_bytes;
2566
+
2567
+ return ARCHIVE_OK;
2568
+ }
2569
+
2570
+ #ifdef HAVE_BZLIB_H
2571
+ static int
2572
+ zipx_bzip2_init(struct archive_read *a, struct zip *zip)
2573
+ {
2574
+ int r;
2575
+
2576
+ /* Deallocate already existing BZ2 decompression context if it
2577
+ * exists. */
2578
+ if(zip->bzstream_valid) {
2579
+ BZ2_bzDecompressEnd(&zip->bzstream);
2580
+ zip->bzstream_valid = 0;
2581
+ }
2582
+
2583
+ /* Allocate a new BZ2 decompression context. */
2584
+ memset(&zip->bzstream, 0, sizeof(bz_stream));
2585
+ r = BZ2_bzDecompressInit(&zip->bzstream, 0, 1);
2586
+ if(r != BZ_OK) {
2587
+ archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
2588
+ "bzip2 initialization failed (%d)",
2589
+ r);
2590
+
2591
+ return ARCHIVE_FAILED;
2592
+ }
2593
+
2594
+ /* Mark the bzstream field to be released in cleanup phase. */
2595
+ zip->bzstream_valid = 1;
2596
+
2597
+ /* (Re)allocate the buffer that will contain decompressed bytes. */
2598
+ free(zip->uncompressed_buffer);
2599
+
2600
+ zip->uncompressed_buffer_size = 256 * 1024;
2601
+ zip->uncompressed_buffer = malloc(zip->uncompressed_buffer_size);
2602
+ if (zip->uncompressed_buffer == NULL) {
2603
+ archive_set_error(&a->archive, ENOMEM,
2604
+ "No memory for bzip2 decompression");
2605
+ return ARCHIVE_FATAL;
2606
+ }
2607
+
2608
+ /* Initialization done. */
2609
+ zip->decompress_init = 1;
2610
+ return ARCHIVE_OK;
2611
+ }
2612
+
2613
+ static int
2614
+ zip_read_data_zipx_bzip2(struct archive_read *a, const void **buff,
2615
+ size_t *size, int64_t *offset)
2616
+ {
2617
+ struct zip *zip = a->format->data;
2618
+ ssize_t bytes_avail = 0, to_consume;
2619
+ const void *compressed_buff;
2620
+ const void *sp;
2621
+ int r;
2622
+ uint64_t total_out;
2623
+
2624
+ (void) offset; /* UNUSED */
2625
+
2626
+ /* Initialize decompression context if we're here for the first time. */
2627
+ if(!zip->decompress_init) {
2628
+ r = zipx_bzip2_init(a, zip);
2629
+ if(r != ARCHIVE_OK)
2630
+ return r;
2631
+ }
2632
+
2633
+ /* Fetch more compressed bytes. */
2634
+ compressed_buff = __archive_read_ahead(a, 1, &bytes_avail);
2635
+ if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
2636
+ && bytes_avail > zip->entry_bytes_remaining) {
2637
+ bytes_avail = (ssize_t)zip->entry_bytes_remaining;
2638
+ }
2639
+ if(bytes_avail < 1) {
2640
+ /* libbz2 doesn't complain when caller feeds avail_in == 0.
2641
+ * It will actually return success in this case, which is
2642
+ * undesirable. This is why we need to make this check
2643
+ * manually. */
2644
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2645
+ "Truncated bzip2 file body");
2646
+ return (ARCHIVE_FATAL);
2647
+ }
2648
+
2649
+ zip_read_decrypt(zip, compressed_buff, bytes_avail,
2650
+ &compressed_buff, &bytes_avail, &sp);
2651
+
2652
+ /* Setup buffer boundaries. */
2653
+ zip->bzstream.next_in = (char*)(uintptr_t) compressed_buff;
2654
+ zip->bzstream.avail_in = (uint32_t)bytes_avail;
2655
+ zip->bzstream.total_in_hi32 = 0;
2656
+ zip->bzstream.total_in_lo32 = 0;
2657
+ zip->bzstream.next_out = (char*) zip->uncompressed_buffer;
2658
+ zip->bzstream.avail_out = (uint32_t)zip->uncompressed_buffer_size;
2659
+ zip->bzstream.total_out_hi32 = 0;
2660
+ zip->bzstream.total_out_lo32 = 0;
2661
+
2662
+ /* Perform the decompression. */
2663
+ r = BZ2_bzDecompress(&zip->bzstream);
2664
+ switch(r) {
2665
+ case BZ_STREAM_END:
2666
+ /* If we're at the end of the stream, deinitialize the
2667
+ * decompression context now. */
2668
+ switch(BZ2_bzDecompressEnd(&zip->bzstream)) {
2669
+ case BZ_OK:
2670
+ break;
2671
+ default:
2672
+ archive_set_error(&a->archive,
2673
+ ARCHIVE_ERRNO_MISC,
2674
+ "Failed to clean up bzip2 "
2675
+ "decompressor");
2676
+ return ARCHIVE_FATAL;
2677
+ }
2678
+
2679
+ zip->end_of_entry = 1;
2680
+ break;
2681
+ case BZ_OK:
2682
+ /* The decompressor has successfully decoded this
2683
+ * chunk of data, but more data is still in queue. */
2684
+ break;
2685
+ default:
2686
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2687
+ "bzip2 decompression failed");
2688
+ return ARCHIVE_FATAL;
2689
+ }
2690
+
2691
+ /* Update the pointers so decompressor can continue decoding. */
2692
+ to_consume = zip->bzstream.total_in_lo32;
2693
+ __archive_read_consume(a, to_consume);
2694
+
2695
+ total_out = ((uint64_t) zip->bzstream.total_out_hi32 << 32) |
2696
+ zip->bzstream.total_out_lo32;
2697
+
2698
+ zip->entry_bytes_remaining -= to_consume;
2699
+ zip->entry_compressed_bytes_read += to_consume;
2700
+ zip->entry_uncompressed_bytes_read += total_out;
2701
+
2702
+ zip_read_decrypt_update(zip, to_consume, sp);
2703
+
2704
+ if (zip->end_of_entry && zip->hctx_valid) {
2705
+ r = check_authentication_code(a, NULL);
2706
+ if (r != ARCHIVE_OK)
2707
+ return r;
2708
+ }
2709
+
2710
+ /* Give libarchive its due. */
2711
+ *size = (size_t)total_out;
2712
+ *buff = zip->uncompressed_buffer;
2713
+
2714
+ return ARCHIVE_OK;
2715
+ }
2716
+
2717
+ #endif
2718
+
2719
+ #if HAVE_ZSTD_H && HAVE_LIBZSTD
2720
+ static int
2721
+ zipx_zstd_init(struct archive_read *a, struct zip *zip)
2722
+ {
2723
+ size_t r;
2724
+
2725
+ /* Deallocate already existing Zstd decompression context if it
2726
+ * exists. */
2727
+ if(zip->zstdstream_valid) {
2728
+ ZSTD_freeDStream(zip->zstdstream);
2729
+ zip->zstdstream_valid = 0;
2730
+ }
2731
+
2732
+ /* Allocate a new Zstd decompression context. */
2733
+ zip->zstdstream = ZSTD_createDStream();
2734
+
2735
+ r = ZSTD_initDStream(zip->zstdstream);
2736
+ if (ZSTD_isError(r)) {
2737
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2738
+ "Error initializing zstd decompressor: %s",
2739
+ ZSTD_getErrorName(r));
2740
+
2741
+ return ARCHIVE_FAILED;
2742
+ }
2743
+
2744
+ /* Mark the zstdstream field to be released in cleanup phase. */
2745
+ zip->zstdstream_valid = 1;
2746
+
2747
+ /* (Re)allocate the buffer that will contain decompressed bytes. */
2748
+ free(zip->uncompressed_buffer);
2749
+
2750
+ zip->uncompressed_buffer_size = ZSTD_DStreamOutSize();
2751
+ zip->uncompressed_buffer = malloc(zip->uncompressed_buffer_size);
2752
+ if (zip->uncompressed_buffer == NULL) {
2753
+ archive_set_error(&a->archive, ENOMEM,
2754
+ "No memory for Zstd decompression");
2755
+
2756
+ return ARCHIVE_FATAL;
2757
+ }
2758
+
2759
+ /* Initialization done. */
2760
+ zip->decompress_init = 1;
2761
+ return ARCHIVE_OK;
2762
+ }
2763
+
2764
+ static int
2765
+ zip_read_data_zipx_zstd(struct archive_read *a, const void **buff,
2766
+ size_t *size, int64_t *offset)
2767
+ {
2768
+ struct zip *zip = a->format->data;
2769
+ ssize_t bytes_avail = 0, to_consume;
2770
+ const void *compressed_buff;
2771
+ const void *sp;
2772
+ int r;
2773
+ size_t ret;
2774
+ uint64_t total_out;
2775
+ ZSTD_outBuffer out;
2776
+ ZSTD_inBuffer in;
2777
+
2778
+ (void) offset; /* UNUSED */
2779
+
2780
+ /* Initialize decompression context if we're here for the first time. */
2781
+ if(!zip->decompress_init) {
2782
+ r = zipx_zstd_init(a, zip);
2783
+ if(r != ARCHIVE_OK)
2784
+ return r;
2785
+ }
2786
+
2787
+ /* Fetch more compressed bytes */
2788
+ compressed_buff = sp = __archive_read_ahead(a, 1, &bytes_avail);
2789
+ if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
2790
+ && bytes_avail > zip->entry_bytes_remaining) {
2791
+ bytes_avail = (ssize_t)zip->entry_bytes_remaining;
2792
+ }
2793
+ if(bytes_avail < 1) {
2794
+ /* zstd doesn't complain when caller feeds avail_in == 0.
2795
+ * It will actually return success in this case, which is
2796
+ * undesirable. This is why we need to make this check
2797
+ * manually. */
2798
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2799
+ "Truncated zstd file body");
2800
+ return (ARCHIVE_FATAL);
2801
+ }
2802
+
2803
+ zip_read_decrypt(zip, compressed_buff, bytes_avail,
2804
+ &compressed_buff, &bytes_avail, &sp);
2805
+
2806
+ /* Setup buffer boundaries */
2807
+ in.src = compressed_buff;
2808
+ in.size = bytes_avail;
2809
+ in.pos = 0;
2810
+ out = (ZSTD_outBuffer) { zip->uncompressed_buffer, zip->uncompressed_buffer_size, 0 };
2811
+
2812
+ /* Perform the decompression. */
2813
+ ret = ZSTD_decompressStream(zip->zstdstream, &out, &in);
2814
+ if (ZSTD_isError(ret)) {
2815
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2816
+ "Error during zstd decompression: %s",
2817
+ ZSTD_getErrorName(ret));
2818
+ return (ARCHIVE_FATAL);
2819
+ }
2820
+ /* End of stream handling for zips with ZIP_LENGTH_AT_END flag */
2821
+ if (ret == 0 && (zip->entry->zip_flags & ZIP_LENGTH_AT_END)) {
2822
+ zip->end_of_entry = 1;
2823
+ }
2824
+
2825
+ /* Check end of the stream. */
2826
+ if (ret == 0) {
2827
+ if ((in.pos == in.size) && (out.pos < out.size)) {
2828
+ zip->end_of_entry = 1;
2829
+ ZSTD_freeDStream(zip->zstdstream);
2830
+ zip->zstdstream_valid = 0;
2831
+ }
2832
+ }
2833
+
2834
+ /* Update the pointers so decompressor can continue decoding. */
2835
+ to_consume = in.pos;
2836
+ __archive_read_consume(a, to_consume);
2837
+
2838
+ total_out = out.pos;
2839
+
2840
+ zip->entry_bytes_remaining -= to_consume;
2841
+ zip->entry_compressed_bytes_read += to_consume;
2842
+ zip->entry_uncompressed_bytes_read += total_out;
2843
+
2844
+ zip_read_decrypt_update(zip, to_consume, sp);
2845
+
2846
+ if (zip->end_of_entry && zip->hctx_valid) {
2847
+ r = check_authentication_code(a, NULL);
2848
+ if (r != ARCHIVE_OK)
2849
+ return r;
2850
+ }
2851
+
2852
+ /* Give libarchive its due. */
2853
+ *size = (size_t)total_out;
2854
+ *buff = zip->uncompressed_buffer;
2855
+
2856
+ return ARCHIVE_OK;
2857
+ }
2858
+ #endif
2859
+
2860
+ #ifdef HAVE_ZLIB_H
2861
+ static int
2862
+ zip_deflate_init(struct archive_read *a, struct zip *zip)
2863
+ {
2864
+ int r;
2865
+
2866
+ /* If we haven't yet read any data, initialize the decompressor. */
2867
+ if (!zip->decompress_init) {
2868
+ if (zip->stream_valid)
2869
+ r = inflateReset(&zip->stream);
2870
+ else
2871
+ r = inflateInit2(&zip->stream,
2872
+ -15 /* Don't check for zlib header */);
2873
+ if (r != Z_OK) {
2874
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2875
+ "Can't initialize ZIP decompression");
2876
+ return (ARCHIVE_FATAL);
2877
+ }
2878
+ /* Stream structure has been set up. */
2879
+ zip->stream_valid = 1;
2880
+ /* We've initialized decompression for this stream. */
2881
+ zip->decompress_init = 1;
2882
+ }
2883
+ return (ARCHIVE_OK);
2884
+ }
2885
+
2886
+ static int
2887
+ zip_read_data_deflate(struct archive_read *a, const void **buff,
2888
+ size_t *size, int64_t *offset)
2889
+ {
2890
+ struct zip *zip = a->format->data;
2891
+ ssize_t bytes_avail, to_consume = 0;
2892
+ const void *compressed_buff;
2893
+ const void *sp;
2894
+ int r;
2895
+
2896
+ (void)offset; /* UNUSED */
2897
+
2898
+ /* If the buffer hasn't been allocated, allocate it now. */
2899
+ if (zip->uncompressed_buffer == NULL) {
2900
+ zip->uncompressed_buffer_size = 256 * 1024;
2901
+ zip->uncompressed_buffer
2902
+ = malloc(zip->uncompressed_buffer_size);
2903
+ if (zip->uncompressed_buffer == NULL) {
2904
+ archive_set_error(&a->archive, ENOMEM,
2905
+ "No memory for ZIP decompression");
2906
+ return (ARCHIVE_FATAL);
2907
+ }
2908
+ }
2909
+
2910
+ r = zip_deflate_init(a, zip);
2911
+ if (r != ARCHIVE_OK)
2912
+ return (r);
2913
+
2914
+ /*
2915
+ * Note: '1' here is a performance optimization.
2916
+ * Recall that the decompression layer returns a count of
2917
+ * available bytes; asking for more than that forces the
2918
+ * decompressor to combine reads by copying data.
2919
+ */
2920
+ compressed_buff = sp = __archive_read_ahead(a, 1, &bytes_avail);
2921
+ if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
2922
+ && bytes_avail > zip->entry_bytes_remaining) {
2923
+ bytes_avail = (ssize_t)zip->entry_bytes_remaining;
2924
+ }
2925
+ if (bytes_avail < 0) {
2926
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2927
+ "Truncated ZIP file body");
2928
+ return (ARCHIVE_FATAL);
2929
+ }
2930
+
2931
+ zip_read_decrypt(zip, compressed_buff, bytes_avail,
2932
+ &compressed_buff, &bytes_avail, &sp);
2933
+
2934
+ /*
2935
+ * A bug in zlib.h: stream.next_in should be marked 'const'
2936
+ * but isn't (the library never alters data through the
2937
+ * next_in pointer, only reads it). The result: this ugly
2938
+ * cast to remove 'const'.
2939
+ */
2940
+ zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff;
2941
+ zip->stream.avail_in = (uInt)bytes_avail;
2942
+ zip->stream.total_in = 0;
2943
+ zip->stream.next_out = zip->uncompressed_buffer;
2944
+ zip->stream.avail_out = (uInt)zip->uncompressed_buffer_size;
2945
+ zip->stream.total_out = 0;
2946
+
2947
+ r = inflate(&zip->stream, 0);
2948
+ switch (r) {
2949
+ case Z_OK:
2950
+ break;
2951
+ case Z_STREAM_END:
2952
+ zip->end_of_entry = 1;
2953
+ break;
2954
+ case Z_MEM_ERROR:
2955
+ archive_set_error(&a->archive, ENOMEM,
2956
+ "Out of memory for ZIP decompression");
2957
+ return (ARCHIVE_FATAL);
2958
+ default:
2959
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2960
+ "ZIP decompression failed (%d)", r);
2961
+ return (ARCHIVE_FATAL);
2962
+ }
2963
+
2964
+ /* Consume as much as the compressor actually used. */
2965
+ to_consume = zip->stream.total_in;
2966
+ __archive_read_consume(a, to_consume);
2967
+ zip->entry_bytes_remaining -= to_consume;
2968
+ zip->entry_compressed_bytes_read += to_consume;
2969
+ zip->entry_uncompressed_bytes_read += zip->stream.total_out;
2970
+
2971
+ zip_read_decrypt_update(zip, to_consume, sp);
2972
+
2973
+ if (zip->end_of_entry && zip->hctx_valid) {
2974
+ r = check_authentication_code(a, NULL);
2975
+ if (r != ARCHIVE_OK) {
2976
+ return r;
2977
+ }
2978
+ }
2979
+
2980
+ *size = zip->stream.total_out;
2981
+ *buff = zip->uncompressed_buffer;
2982
+
2983
+ return (ARCHIVE_OK);
2984
+ }
2985
+ #endif
2986
+
2987
+ static int
2988
+ read_decryption_header(struct archive_read *a)
2989
+ {
2990
+ struct zip *zip = a->format->data;
2991
+ const char *p;
2992
+ unsigned int remaining_size;
2993
+ unsigned int ts;
2994
+
2995
+ /*
2996
+ * Read an initialization vector data field.
2997
+ */
2998
+ p = __archive_read_ahead(a, 2, NULL);
2999
+ if (p == NULL)
3000
+ goto truncated;
3001
+ ts = zip->iv_size;
3002
+ zip->iv_size = archive_le16dec(p);
3003
+ __archive_read_consume(a, 2);
3004
+ if (ts < zip->iv_size) {
3005
+ free(zip->iv);
3006
+ zip->iv = NULL;
3007
+ }
3008
+ p = __archive_read_ahead(a, zip->iv_size, NULL);
3009
+ if (p == NULL)
3010
+ goto truncated;
3011
+ if (zip->iv == NULL) {
3012
+ zip->iv = malloc(zip->iv_size);
3013
+ if (zip->iv == NULL)
3014
+ goto nomem;
3015
+ }
3016
+ memcpy(zip->iv, p, zip->iv_size);
3017
+ __archive_read_consume(a, zip->iv_size);
3018
+
3019
+ /*
3020
+ * Read a size of remaining decryption header field.
3021
+ */
3022
+ p = __archive_read_ahead(a, 14, NULL);
3023
+ if (p == NULL)
3024
+ goto truncated;
3025
+ remaining_size = archive_le32dec(p);
3026
+ if (remaining_size < 16 || remaining_size > (1 << 18))
3027
+ goto corrupted;
3028
+
3029
+ /* Check if format version is supported. */
3030
+ if (archive_le16dec(p+4) != 3) {
3031
+ archive_set_error(&a->archive,
3032
+ ARCHIVE_ERRNO_FILE_FORMAT,
3033
+ "Unsupported encryption format version: %u",
3034
+ archive_le16dec(p+4));
3035
+ return (ARCHIVE_FAILED);
3036
+ }
3037
+
3038
+ /*
3039
+ * Read an encryption algorithm field.
3040
+ */
3041
+ zip->alg_id = archive_le16dec(p+6);
3042
+ switch (zip->alg_id) {
3043
+ case 0x6601:/* DES */
3044
+ case 0x6602:/* RC2 */
3045
+ case 0x6603:/* 3DES 168 */
3046
+ case 0x6609:/* 3DES 112 */
3047
+ case 0x660E:/* AES 128 */
3048
+ case 0x660F:/* AES 192 */
3049
+ case 0x6610:/* AES 256 */
3050
+ case 0x6702:/* RC2 (version >= 5.2) */
3051
+ case 0x6720:/* Blowfish */
3052
+ case 0x6721:/* Twofish */
3053
+ case 0x6801:/* RC4 */
3054
+ /* Supported encryption algorithm. */
3055
+ break;
3056
+ default:
3057
+ archive_set_error(&a->archive,
3058
+ ARCHIVE_ERRNO_FILE_FORMAT,
3059
+ "Unknown encryption algorithm: %u", zip->alg_id);
3060
+ return (ARCHIVE_FAILED);
3061
+ }
3062
+
3063
+ /*
3064
+ * Read a bit length field.
3065
+ */
3066
+ zip->bit_len = archive_le16dec(p+8);
3067
+
3068
+ /*
3069
+ * Read a flags field.
3070
+ */
3071
+ zip->flags = archive_le16dec(p+10);
3072
+ switch (zip->flags & 0xf000) {
3073
+ case 0x0001: /* Password is required to decrypt. */
3074
+ case 0x0002: /* Certificates only. */
3075
+ case 0x0003: /* Password or certificate required to decrypt. */
3076
+ break;
3077
+ default:
3078
+ archive_set_error(&a->archive,
3079
+ ARCHIVE_ERRNO_FILE_FORMAT,
3080
+ "Unknown encryption flag: %u", zip->flags);
3081
+ return (ARCHIVE_FAILED);
3082
+ }
3083
+ if ((zip->flags & 0xf000) == 0 ||
3084
+ (zip->flags & 0xf000) == 0x4000) {
3085
+ archive_set_error(&a->archive,
3086
+ ARCHIVE_ERRNO_FILE_FORMAT,
3087
+ "Unknown encryption flag: %u", zip->flags);
3088
+ return (ARCHIVE_FAILED);
3089
+ }
3090
+
3091
+ /*
3092
+ * Read an encrypted random data field.
3093
+ */
3094
+ ts = zip->erd_size;
3095
+ zip->erd_size = archive_le16dec(p+12);
3096
+ __archive_read_consume(a, 14);
3097
+ if ((zip->erd_size & 0xf) != 0 ||
3098
+ (zip->erd_size + 16) > remaining_size ||
3099
+ (zip->erd_size + 16) < zip->erd_size)
3100
+ goto corrupted;
3101
+
3102
+ if (ts < zip->erd_size) {
3103
+ free(zip->erd);
3104
+ zip->erd = NULL;
3105
+ }
3106
+ p = __archive_read_ahead(a, zip->erd_size, NULL);
3107
+ if (p == NULL)
3108
+ goto truncated;
3109
+ if (zip->erd == NULL) {
3110
+ zip->erd = malloc(zip->erd_size);
3111
+ if (zip->erd == NULL)
3112
+ goto nomem;
3113
+ }
3114
+ memcpy(zip->erd, p, zip->erd_size);
3115
+ __archive_read_consume(a, zip->erd_size);
3116
+
3117
+ /*
3118
+ * Read a reserved data field.
3119
+ */
3120
+ p = __archive_read_ahead(a, 4, NULL);
3121
+ if (p == NULL)
3122
+ goto truncated;
3123
+ /* Reserved data size should be zero. */
3124
+ if (archive_le32dec(p) != 0)
3125
+ goto corrupted;
3126
+ __archive_read_consume(a, 4);
3127
+
3128
+ /*
3129
+ * Read a password validation data field.
3130
+ */
3131
+ p = __archive_read_ahead(a, 2, NULL);
3132
+ if (p == NULL)
3133
+ goto truncated;
3134
+ ts = zip->v_size;
3135
+ zip->v_size = archive_le16dec(p);
3136
+ __archive_read_consume(a, 2);
3137
+ if ((zip->v_size & 0x0f) != 0 ||
3138
+ (zip->erd_size + zip->v_size + 16) > remaining_size ||
3139
+ (zip->erd_size + zip->v_size + 16) < (zip->erd_size + zip->v_size))
3140
+ goto corrupted;
3141
+ if (ts < zip->v_size) {
3142
+ free(zip->v_data);
3143
+ zip->v_data = NULL;
3144
+ }
3145
+ p = __archive_read_ahead(a, zip->v_size, NULL);
3146
+ if (p == NULL)
3147
+ goto truncated;
3148
+ if (zip->v_data == NULL) {
3149
+ zip->v_data = malloc(zip->v_size);
3150
+ if (zip->v_data == NULL)
3151
+ goto nomem;
3152
+ }
3153
+ memcpy(zip->v_data, p, zip->v_size);
3154
+ __archive_read_consume(a, zip->v_size);
3155
+
3156
+ p = __archive_read_ahead(a, 4, NULL);
3157
+ if (p == NULL)
3158
+ goto truncated;
3159
+ zip->v_crc32 = archive_le32dec(p);
3160
+ __archive_read_consume(a, 4);
3161
+
3162
+ /*return (ARCHIVE_OK);
3163
+ * This is not fully implemented yet.*/
3164
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3165
+ "Encrypted file is unsupported");
3166
+ return (ARCHIVE_FAILED);
3167
+ truncated:
3168
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3169
+ "Truncated ZIP file data");
3170
+ return (ARCHIVE_FATAL);
3171
+ corrupted:
3172
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3173
+ "Corrupted ZIP file data");
3174
+ return (ARCHIVE_FATAL);
3175
+ nomem:
3176
+ archive_set_error(&a->archive, ENOMEM,
3177
+ "No memory for ZIP decryption");
3178
+ return (ARCHIVE_FATAL);
3179
+ }
3180
+
3181
+ static int
3182
+ zip_alloc_decryption_buffer(struct archive_read *a)
3183
+ {
3184
+ struct zip *zip = a->format->data;
3185
+ size_t bs = 256 * 1024;
3186
+
3187
+ if (zip->decrypted_buffer == NULL) {
3188
+ zip->decrypted_buffer_size = bs;
3189
+ zip->decrypted_buffer = malloc(bs);
3190
+ if (zip->decrypted_buffer == NULL) {
3191
+ archive_set_error(&a->archive, ENOMEM,
3192
+ "No memory for ZIP decryption");
3193
+ return (ARCHIVE_FATAL);
3194
+ }
3195
+ }
3196
+ zip->decrypted_ptr = zip->decrypted_buffer;
3197
+ return (ARCHIVE_OK);
3198
+ }
3199
+
3200
+ static int
3201
+ init_traditional_PKWARE_decryption(struct archive_read *a)
3202
+ {
3203
+ struct zip *zip = a->format->data;
3204
+ const void *p;
3205
+ int retry;
3206
+ int r;
3207
+
3208
+ if (zip->tctx_valid)
3209
+ return (ARCHIVE_OK);
3210
+
3211
+ /*
3212
+ Read the 12 bytes encryption header stored at
3213
+ the start of the data area.
3214
+ */
3215
+ #define ENC_HEADER_SIZE 12
3216
+ if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
3217
+ && zip->entry_bytes_remaining < ENC_HEADER_SIZE) {
3218
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3219
+ "Truncated Zip encrypted body: only %jd bytes available",
3220
+ (intmax_t)zip->entry_bytes_remaining);
3221
+ return (ARCHIVE_FATAL);
3222
+ }
3223
+
3224
+ p = __archive_read_ahead(a, ENC_HEADER_SIZE, NULL);
3225
+ if (p == NULL) {
3226
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3227
+ "Truncated ZIP file data");
3228
+ return (ARCHIVE_FATAL);
3229
+ }
3230
+
3231
+ for (retry = 0;; retry++) {
3232
+ const char *passphrase;
3233
+ uint8_t crcchk;
3234
+
3235
+ passphrase = __archive_read_next_passphrase(a);
3236
+ if (passphrase == NULL) {
3237
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3238
+ (retry > 0)?
3239
+ "Incorrect passphrase":
3240
+ "Passphrase required for this entry");
3241
+ return (ARCHIVE_FAILED);
3242
+ }
3243
+
3244
+ /*
3245
+ * Initialize ctx for Traditional PKWARE Decryption.
3246
+ */
3247
+ r = trad_enc_init(&zip->tctx, passphrase, strlen(passphrase),
3248
+ p, ENC_HEADER_SIZE, &crcchk);
3249
+ if (r == 0 && crcchk == zip->entry->decdat)
3250
+ break;/* The passphrase is OK. */
3251
+ if (retry > 10000) {
3252
+ /* Avoid infinity loop. */
3253
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3254
+ "Too many incorrect passphrases");
3255
+ return (ARCHIVE_FAILED);
3256
+ }
3257
+ }
3258
+
3259
+ __archive_read_consume(a, ENC_HEADER_SIZE);
3260
+ zip->tctx_valid = 1;
3261
+ if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)) {
3262
+ zip->entry_bytes_remaining -= ENC_HEADER_SIZE;
3263
+ }
3264
+ /*zip->entry_uncompressed_bytes_read += ENC_HEADER_SIZE;*/
3265
+ zip->entry_compressed_bytes_read += ENC_HEADER_SIZE;
3266
+ zip->decrypted_bytes_remaining = 0;
3267
+
3268
+ return (zip_alloc_decryption_buffer(a));
3269
+ #undef ENC_HEADER_SIZE
3270
+ }
3271
+
3272
+ static int
3273
+ init_WinZip_AES_decryption(struct archive_read *a)
3274
+ {
3275
+ struct zip *zip = a->format->data;
3276
+ const void *p;
3277
+ const uint8_t *pv;
3278
+ size_t key_len, salt_len;
3279
+ uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
3280
+ int retry;
3281
+ int r;
3282
+
3283
+ if (zip->cctx_valid || zip->hctx_valid)
3284
+ return (ARCHIVE_OK);
3285
+
3286
+ switch (zip->entry->aes_extra.strength) {
3287
+ case 1: salt_len = 8; key_len = 16; break;
3288
+ case 2: salt_len = 12; key_len = 24; break;
3289
+ case 3: salt_len = 16; key_len = 32; break;
3290
+ default: goto corrupted;
3291
+ }
3292
+ p = __archive_read_ahead(a, salt_len + 2, NULL);
3293
+ if (p == NULL)
3294
+ goto truncated;
3295
+
3296
+ for (retry = 0;; retry++) {
3297
+ const char *passphrase;
3298
+
3299
+ passphrase = __archive_read_next_passphrase(a);
3300
+ if (passphrase == NULL) {
3301
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3302
+ (retry > 0)?
3303
+ "Incorrect passphrase":
3304
+ "Passphrase required for this entry");
3305
+ return (ARCHIVE_FAILED);
3306
+ }
3307
+ memset(derived_key, 0, sizeof(derived_key));
3308
+ r = archive_pbkdf2_sha1(passphrase, strlen(passphrase),
3309
+ p, salt_len, 1000, derived_key, key_len * 2 + 2);
3310
+ if (r != 0) {
3311
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3312
+ r == CRYPTOR_STUB_FUNCTION ? "Decryption is unsupported due "
3313
+ "to lack of crypto library" : "Failed to process passphrase");
3314
+ return (ARCHIVE_FAILED);
3315
+ }
3316
+
3317
+ /* Check password verification value. */
3318
+ pv = ((const uint8_t *)p) + salt_len;
3319
+ if (derived_key[key_len * 2] == pv[0] &&
3320
+ derived_key[key_len * 2 + 1] == pv[1])
3321
+ break;/* The passphrase is OK. */
3322
+ if (retry > 10000) {
3323
+ /* Avoid infinity loop. */
3324
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3325
+ "Too many incorrect passphrases");
3326
+ return (ARCHIVE_FAILED);
3327
+ }
3328
+ }
3329
+
3330
+ r = archive_decrypto_aes_ctr_init(&zip->cctx, derived_key, key_len);
3331
+ if (r != 0) {
3332
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3333
+ "Decryption is unsupported due to lack of crypto library");
3334
+ return (ARCHIVE_FAILED);
3335
+ }
3336
+ r = archive_hmac_sha1_init(&zip->hctx, derived_key + key_len, key_len);
3337
+ if (r != 0) {
3338
+ archive_decrypto_aes_ctr_release(&zip->cctx);
3339
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3340
+ "Failed to initialize HMAC-SHA1");
3341
+ return (ARCHIVE_FAILED);
3342
+ }
3343
+ zip->cctx_valid = zip->hctx_valid = 1;
3344
+ __archive_read_consume(a, salt_len + 2);
3345
+ zip->entry_bytes_remaining -= salt_len + 2 + AUTH_CODE_SIZE;
3346
+ if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
3347
+ && zip->entry_bytes_remaining < 0)
3348
+ goto corrupted;
3349
+ zip->entry_compressed_bytes_read += salt_len + 2 + AUTH_CODE_SIZE;
3350
+ zip->decrypted_bytes_remaining = 0;
3351
+
3352
+ zip->entry->compression = zip->entry->aes_extra.compression;
3353
+ return (zip_alloc_decryption_buffer(a));
3354
+
3355
+ truncated:
3356
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3357
+ "Truncated ZIP file data");
3358
+ return (ARCHIVE_FATAL);
3359
+ corrupted:
3360
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3361
+ "Corrupted ZIP file data");
3362
+ return (ARCHIVE_FATAL);
3363
+ }
3364
+
3365
+ static int
3366
+ archive_read_format_zip_read_data(struct archive_read *a,
3367
+ const void **buff, size_t *size, int64_t *offset)
3368
+ {
3369
+ struct zip *zip = a->format->data;
3370
+ int r;
3371
+
3372
+ if (zip->has_encrypted_entries ==
3373
+ ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
3374
+ zip->has_encrypted_entries = 0;
3375
+ }
3376
+
3377
+ *offset = zip->entry_uncompressed_bytes_read;
3378
+ *size = 0;
3379
+ *buff = NULL;
3380
+
3381
+ /* If we hit end-of-entry last time, return ARCHIVE_EOF. */
3382
+ if (zip->end_of_entry)
3383
+ return (ARCHIVE_EOF);
3384
+
3385
+ /* Return EOF immediately if this is a non-regular file. */
3386
+ if (AE_IFREG != (zip->entry->mode & AE_IFMT))
3387
+ return (ARCHIVE_EOF);
3388
+
3389
+ __archive_read_consume(a, zip->unconsumed);
3390
+ zip->unconsumed = 0;
3391
+
3392
+ if (zip->init_decryption) {
3393
+ zip->has_encrypted_entries = 1;
3394
+ if (zip->entry->zip_flags & ZIP_STRONG_ENCRYPTED)
3395
+ r = read_decryption_header(a);
3396
+ else if (zip->entry->compression == WINZIP_AES_ENCRYPTION)
3397
+ r = init_WinZip_AES_decryption(a);
3398
+ else
3399
+ r = init_traditional_PKWARE_decryption(a);
3400
+ if (r != ARCHIVE_OK)
3401
+ return (r);
3402
+ zip->init_decryption = 0;
3403
+ }
3404
+
3405
+ switch(zip->entry->compression) {
3406
+ case 0: /* No compression. */
3407
+ r = zip_read_data_none(a, buff, size, offset);
3408
+ break;
3409
+ #ifdef HAVE_BZLIB_H
3410
+ case 12: /* ZIPx bzip2 compression. */
3411
+ r = zip_read_data_zipx_bzip2(a, buff, size, offset);
3412
+ break;
3413
+ #endif
3414
+ #if HAVE_LZMA_H && HAVE_LIBLZMA
3415
+ case 14: /* ZIPx LZMA compression. */
3416
+ r = zip_read_data_zipx_lzma_alone(a, buff, size, offset);
3417
+ break;
3418
+ case 95: /* ZIPx XZ compression. */
3419
+ r = zip_read_data_zipx_xz(a, buff, size, offset);
3420
+ break;
3421
+ #endif
3422
+ #if HAVE_ZSTD_H && HAVE_LIBZSTD
3423
+ case 93: /* ZIPx Zstd compression. */
3424
+ r = zip_read_data_zipx_zstd(a, buff, size, offset);
3425
+ break;
3426
+ #endif
3427
+ /* PPMd support is built-in, so we don't need any #if guards. */
3428
+ case 98: /* ZIPx PPMd compression. */
3429
+ r = zip_read_data_zipx_ppmd(a, buff, size, offset);
3430
+ break;
3431
+
3432
+ #ifdef HAVE_ZLIB_H
3433
+ case 8: /* Deflate compression. */
3434
+ r = zip_read_data_deflate(a, buff, size, offset);
3435
+ break;
3436
+ #endif
3437
+ default: /* Unsupported compression. */
3438
+ /* Return a warning. */
3439
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3440
+ "Unsupported ZIP compression method (%d: %s)",
3441
+ zip->entry->compression, compression_name(zip->entry->compression));
3442
+ /* We can't decompress this entry, but we will
3443
+ * be able to skip() it and try the next entry. */
3444
+ return (ARCHIVE_FAILED);
3445
+ }
3446
+ if (r != ARCHIVE_OK)
3447
+ return (r);
3448
+
3449
+ /*
3450
+ * FAIL if there are more uncompressed bytes than were
3451
+ * initially advertised. The end-of-entry check below also
3452
+ * compares these values, but only once decoding reaches its
3453
+ * own natural end. Fail the entry so a caller relying on
3454
+ * archive_entry_size() as a hard boundary is never misled;
3455
+ * later entries in the archive can still be read normally.
3456
+ */
3457
+ if (*size > 0 && zip_entry_size_is_set(zip->entry) &&
3458
+ zip->entry_uncompressed_bytes_read > zip->entry->uncompressed_size) {
3459
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3460
+ "ZIP uncompressed data is larger than the declared "
3461
+ "entry size (read at least %jd, expected %jd)",
3462
+ (intmax_t)zip->entry_uncompressed_bytes_read,
3463
+ (intmax_t)zip->entry->uncompressed_size);
3464
+ return (ARCHIVE_FAILED);
3465
+ }
3466
+
3467
+ if (*size > 0) {
3468
+ zip->computed_crc32 = zip->crc32func(zip->computed_crc32, *buff,
3469
+ (unsigned)*size);
3470
+ }
3471
+ /* If we hit the end, swallow any end-of-data marker and
3472
+ * verify the final check values. */
3473
+ if (zip->end_of_entry) {
3474
+ consume_end_of_file_marker(a, zip);
3475
+
3476
+ /* Check computed CRC against header */
3477
+ if ((!zip->hctx_valid ||
3478
+ zip->entry->aes_extra.vendor != AES_VENDOR_AE_2) &&
3479
+ zip->entry->crc32 != zip->computed_crc32
3480
+ && !zip->ignore_crc32) {
3481
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3482
+ "ZIP bad CRC: 0x%lx should be 0x%lx",
3483
+ (unsigned long)zip->computed_crc32,
3484
+ (unsigned long)zip->entry->crc32);
3485
+ return (ARCHIVE_FAILED);
3486
+ }
3487
+ /* Check file size against header. */
3488
+ if (zip->entry->compressed_size !=
3489
+ zip->entry_compressed_bytes_read) {
3490
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3491
+ "ZIP compressed data is wrong size "
3492
+ "(read %jd, expected %jd)",
3493
+ (intmax_t)zip->entry_compressed_bytes_read,
3494
+ (intmax_t)zip->entry->compressed_size);
3495
+ return (ARCHIVE_FAILED);
3496
+ }
3497
+ if (zip->entry->uncompressed_size !=
3498
+ zip->entry_uncompressed_bytes_read) {
3499
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3500
+ "ZIP uncompressed data is wrong size "
3501
+ "(read %jd, expected %jd)",
3502
+ (intmax_t)zip->entry_uncompressed_bytes_read,
3503
+ (intmax_t)zip->entry->uncompressed_size);
3504
+ return (ARCHIVE_FAILED);
3505
+ }
3506
+ }
3507
+
3508
+ return (ARCHIVE_OK);
3509
+ }
3510
+
3511
+ static int
3512
+ archive_read_format_zip_cleanup(struct archive_read *a)
3513
+ {
3514
+ struct zip *zip = a->format->data;
3515
+ struct zip_entry *zip_entry, *next_zip_entry;
3516
+
3517
+ #ifdef HAVE_ZLIB_H
3518
+ if (zip->stream_valid)
3519
+ inflateEnd(&zip->stream);
3520
+ #endif
3521
+
3522
+ #if HAVE_LZMA_H && HAVE_LIBLZMA
3523
+ if (zip->zipx_lzma_valid) {
3524
+ lzma_end(&zip->zipx_lzma_stream);
3525
+ }
3526
+ #endif
3527
+
3528
+ #ifdef HAVE_BZLIB_H
3529
+ if (zip->bzstream_valid) {
3530
+ BZ2_bzDecompressEnd(&zip->bzstream);
3531
+ }
3532
+ #endif
3533
+
3534
+ #if HAVE_ZSTD_H && HAVE_LIBZSTD
3535
+ if (zip->zstdstream_valid) {
3536
+ ZSTD_freeDStream(zip->zstdstream);
3537
+ }
3538
+ #endif
3539
+
3540
+ free(zip->uncompressed_buffer);
3541
+
3542
+ if (zip->ppmd8_valid)
3543
+ __archive_ppmd8_functions.Ppmd8_Free(&zip->ppmd8);
3544
+
3545
+ if (zip->zip_entries) {
3546
+ zip_entry = zip->zip_entries;
3547
+ while (zip_entry != NULL) {
3548
+ next_zip_entry = zip_entry->next;
3549
+ archive_string_free(&zip_entry->rsrcname);
3550
+ free(zip_entry);
3551
+ zip_entry = next_zip_entry;
3552
+ }
3553
+ }
3554
+ free(zip->decrypted_buffer);
3555
+ if (zip->cctx_valid)
3556
+ archive_decrypto_aes_ctr_release(&zip->cctx);
3557
+ if (zip->hctx_valid)
3558
+ archive_hmac_sha1_cleanup(&zip->hctx);
3559
+ free(zip->iv);
3560
+ free(zip->erd);
3561
+ free(zip->v_data);
3562
+ archive_string_free(&zip->format_name);
3563
+ free(zip);
3564
+ a->format->data = NULL;
3565
+ return (ARCHIVE_OK);
3566
+ }
3567
+
3568
+ static int
3569
+ archive_read_format_zip_has_encrypted_entries(struct archive_read *_a)
3570
+ {
3571
+ if (_a && _a->format) {
3572
+ struct zip *zip = _a->format->data;
3573
+ if (zip) {
3574
+ return zip->has_encrypted_entries;
3575
+ }
3576
+ }
3577
+ return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
3578
+ }
3579
+
3580
+ static int
3581
+ archive_read_format_zip_options(struct archive_read *a,
3582
+ const char *key, const char *val)
3583
+ {
3584
+ struct zip *zip = a->format->data;
3585
+ int ret = ARCHIVE_FAILED;
3586
+
3587
+ if (strcmp(key, "compat-2x") == 0) {
3588
+ /* Handle filenames as libarchive 2.x */
3589
+ zip->init_default_conversion = (val != NULL) ? 1 : 0;
3590
+ return (ARCHIVE_OK);
3591
+ } else if (strcmp(key, "hdrcharset") == 0) {
3592
+ if (val == NULL || val[0] == 0)
3593
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3594
+ "zip: hdrcharset option needs a character-set name"
3595
+ );
3596
+ else {
3597
+ zip->sconv = archive_string_conversion_from_charset(
3598
+ &a->archive, val, 0);
3599
+ if (zip->sconv != NULL) {
3600
+ if (strcmp(val, "UTF-8") == 0)
3601
+ zip->sconv_utf8 = zip->sconv;
3602
+ ret = ARCHIVE_OK;
3603
+ } else
3604
+ ret = ARCHIVE_FATAL;
3605
+ }
3606
+ return (ret);
3607
+ } else if (strcmp(key, "ignorecrc32") == 0) {
3608
+ /* Mostly useful for testing. */
3609
+ if (val == NULL || val[0] == 0) {
3610
+ zip->crc32func = real_crc32;
3611
+ zip->ignore_crc32 = 0;
3612
+ } else {
3613
+ zip->crc32func = fake_crc32;
3614
+ zip->ignore_crc32 = 1;
3615
+ }
3616
+ return (ARCHIVE_OK);
3617
+ } else if (strcmp(key, "mac-ext") == 0) {
3618
+ zip->process_mac_extensions = (val != NULL && val[0] != 0);
3619
+ return (ARCHIVE_OK);
3620
+ }
3621
+
3622
+ /* Note: The "warn" return is just to inform the options
3623
+ * supervisor that we didn't handle it. It will generate
3624
+ * a suitable error if no one used this option. */
3625
+ return (ARCHIVE_WARN);
3626
+ }
3627
+
3628
+ int
3629
+ archive_read_support_format_zip(struct archive *a)
3630
+ {
3631
+ int r;
3632
+ r = archive_read_support_format_zip_streamable(a);
3633
+ if (r != ARCHIVE_OK)
3634
+ return r;
3635
+ return (archive_read_support_format_zip_seekable(a));
3636
+ }
3637
+
3638
+ /* ------------------------------------------------------------------------ */
3639
+
3640
+ /*
3641
+ * Streaming-mode support
3642
+ */
3643
+
3644
+
3645
+ static int
3646
+ archive_read_support_format_zip_capabilities_streamable(struct archive_read * a)
3647
+ {
3648
+ (void)a; /* UNUSED */
3649
+ return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA |
3650
+ ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
3651
+ }
3652
+
3653
+ static int
3654
+ archive_read_format_zip_streamable_bid(struct archive_read *a, int best_bid)
3655
+ {
3656
+ const char *p;
3657
+
3658
+ (void)best_bid; /* UNUSED */
3659
+
3660
+ if ((p = __archive_read_ahead(a, 4, NULL)) == NULL)
3661
+ return (-1);
3662
+
3663
+ /*
3664
+ * Bid of 29 here comes from:
3665
+ * + 16 bits for "PK",
3666
+ * + next 16-bit field has 6 options so contributes
3667
+ * about 16 - log_2(6) ~= 16 - 2.6 ~= 13 bits
3668
+ *
3669
+ * So we've effectively verified ~29 total bits of check data.
3670
+ */
3671
+ if (p[0] == 'P' && p[1] == 'K') {
3672
+ if ((p[2] == '\001' && p[3] == '\002')
3673
+ || (p[2] == '\003' && p[3] == '\004')
3674
+ || (p[2] == '\005' && p[3] == '\006')
3675
+ || (p[2] == '\006' && p[3] == '\006')
3676
+ || (p[2] == '\007' && p[3] == '\010')
3677
+ || (p[2] == '0' && p[3] == '0'))
3678
+ return (29);
3679
+ }
3680
+
3681
+ /* TODO: It's worth looking ahead a little bit for a valid
3682
+ * PK signature. In particular, that would make it possible
3683
+ * to read some UUEncoded SFX files or SFX files coming from
3684
+ * a network socket. */
3685
+
3686
+ return (0);
3687
+ }
3688
+
3689
+ static int
3690
+ archive_read_format_zip_streamable_read_header(struct archive_read *a,
3691
+ struct archive_entry *entry)
3692
+ {
3693
+ struct zip *zip = a->format->data;
3694
+
3695
+ a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
3696
+ if (a->archive.archive_format_name == NULL)
3697
+ a->archive.archive_format_name = "ZIP";
3698
+
3699
+ /*
3700
+ * It should be sufficient to call archive_read_next_header() for
3701
+ * a reader to determine if an entry is encrypted or not. If the
3702
+ * encryption of an entry is only detectable when calling
3703
+ * archive_read_data(), so be it. We'll do the same check there
3704
+ * as well.
3705
+ */
3706
+ if (zip->has_encrypted_entries ==
3707
+ ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW)
3708
+ zip->has_encrypted_entries = 0;
3709
+
3710
+ /* Make sure we have a zip_entry structure to use. */
3711
+ if (zip->zip_entries == NULL) {
3712
+ zip->zip_entries = malloc(sizeof(struct zip_entry));
3713
+ if (zip->zip_entries == NULL) {
3714
+ archive_set_error(&a->archive, ENOMEM,
3715
+ "Out of memory");
3716
+ return ARCHIVE_FATAL;
3717
+ }
3718
+ }
3719
+ zip->entry = zip->zip_entries;
3720
+ memset(zip->entry, 0, sizeof(struct zip_entry));
3721
+
3722
+ if (zip->cctx_valid)
3723
+ archive_decrypto_aes_ctr_release(&zip->cctx);
3724
+ if (zip->hctx_valid)
3725
+ archive_hmac_sha1_cleanup(&zip->hctx);
3726
+ zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
3727
+ __archive_read_reset_passphrase(a);
3728
+
3729
+ /* Search ahead for the next local file header. */
3730
+ __archive_read_consume(a, zip->unconsumed);
3731
+ zip->unconsumed = 0;
3732
+ for (;;) {
3733
+ int64_t skipped = 0;
3734
+ const char *p, *end;
3735
+ ssize_t bytes;
3736
+
3737
+ p = __archive_read_ahead(a, 4, &bytes);
3738
+ if (p == NULL)
3739
+ return (ARCHIVE_FATAL);
3740
+ end = p + bytes;
3741
+
3742
+ while (p + 4 <= end) {
3743
+ if (p[0] == 'P' && p[1] == 'K') {
3744
+ if (p[2] == '\003' && p[3] == '\004') {
3745
+ /* Regular file entry. */
3746
+ __archive_read_consume(a, skipped);
3747
+ return zip_read_local_file_header(a,
3748
+ entry, zip);
3749
+ }
3750
+
3751
+ /*
3752
+ * TODO: We cannot restore permissions
3753
+ * based only on the local file headers.
3754
+ * Consider scanning the central
3755
+ * directory and returning additional
3756
+ * entries for at least directories.
3757
+ * This would allow us to properly set
3758
+ * directory permissions.
3759
+ *
3760
+ * This won't help us fix symlinks
3761
+ * and may not help with regular file
3762
+ * permissions, either. <sigh>
3763
+ */
3764
+ if (p[2] == '\001' && p[3] == '\002') {
3765
+ return (ARCHIVE_EOF);
3766
+ }
3767
+
3768
+ /* End of central directory? Must be an
3769
+ * empty archive. */
3770
+ if ((p[2] == '\005' && p[3] == '\006')
3771
+ || (p[2] == '\006' && p[3] == '\006'))
3772
+ return (ARCHIVE_EOF);
3773
+ }
3774
+ ++p;
3775
+ ++skipped;
3776
+ }
3777
+ __archive_read_consume(a, skipped);
3778
+ }
3779
+ }
3780
+
3781
+ static int
3782
+ archive_read_format_zip_read_data_skip_streamable(struct archive_read *a)
3783
+ {
3784
+ struct zip *zip = a->format->data;
3785
+ int64_t bytes_skipped;
3786
+
3787
+ bytes_skipped = __archive_read_consume(a, zip->unconsumed);
3788
+ zip->unconsumed = 0;
3789
+ if (bytes_skipped < 0)
3790
+ return (ARCHIVE_FATAL);
3791
+
3792
+ /* If we've already read to end of data, we're done. */
3793
+ if (zip->end_of_entry)
3794
+ return (ARCHIVE_OK);
3795
+
3796
+ /* So we know we're streaming... */
3797
+ if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
3798
+ || zip->entry->compressed_size > 0) {
3799
+ /* We know the compressed length, so we can just skip. */
3800
+ bytes_skipped = __archive_read_consume(a,
3801
+ zip->entry_bytes_remaining);
3802
+ if (bytes_skipped < 0)
3803
+ return (ARCHIVE_FATAL);
3804
+ return (ARCHIVE_OK);
3805
+ }
3806
+
3807
+ if (zip->init_decryption) {
3808
+ int r;
3809
+
3810
+ zip->has_encrypted_entries = 1;
3811
+ if (zip->entry->zip_flags & ZIP_STRONG_ENCRYPTED)
3812
+ r = read_decryption_header(a);
3813
+ else if (zip->entry->compression == WINZIP_AES_ENCRYPTION)
3814
+ r = init_WinZip_AES_decryption(a);
3815
+ else
3816
+ r = init_traditional_PKWARE_decryption(a);
3817
+ if (r != ARCHIVE_OK)
3818
+ return (r);
3819
+ zip->init_decryption = 0;
3820
+ }
3821
+
3822
+ /* We're streaming and we don't know the length. */
3823
+ /* If the body is compressed and we know the format, we can
3824
+ * find an exact end-of-entry by decompressing it. */
3825
+ switch (zip->entry->compression) {
3826
+ #ifdef HAVE_ZLIB_H
3827
+ case 8: /* Deflate compression. */
3828
+ while (!zip->end_of_entry) {
3829
+ int64_t offset = 0;
3830
+ const void *buff = NULL;
3831
+ size_t size = 0;
3832
+ int r;
3833
+ r = zip_read_data_deflate(a, &buff, &size, &offset);
3834
+ if (r != ARCHIVE_OK)
3835
+ return (r);
3836
+ }
3837
+ return ARCHIVE_OK;
3838
+ #endif
3839
+ default: /* Uncompressed or unknown. */
3840
+ /* Scan for a PK\007\010 signature. */
3841
+ for (;;) {
3842
+ const char *p, *buff;
3843
+ ssize_t bytes_avail;
3844
+ buff = __archive_read_ahead(a, 16, &bytes_avail);
3845
+ if (bytes_avail < 16) {
3846
+ archive_set_error(&a->archive,
3847
+ ARCHIVE_ERRNO_FILE_FORMAT,
3848
+ "Truncated ZIP file data");
3849
+ return (ARCHIVE_FATAL);
3850
+ }
3851
+ p = buff;
3852
+ while (p <= buff + bytes_avail - 16) {
3853
+ if (p[3] == 'P') { p += 3; }
3854
+ else if (p[3] == 'K') { p += 2; }
3855
+ else if (p[3] == '\007') { p += 1; }
3856
+ else if (p[3] == '\010' && p[2] == '\007'
3857
+ && p[1] == 'K' && p[0] == 'P') {
3858
+ if (zip->entry->flags & LA_USED_ZIP64)
3859
+ __archive_read_consume(a,
3860
+ p - buff + 24);
3861
+ else
3862
+ __archive_read_consume(a,
3863
+ p - buff + 16);
3864
+ return ARCHIVE_OK;
3865
+ } else { p += 4; }
3866
+ }
3867
+ __archive_read_consume(a, p - buff);
3868
+ }
3869
+ }
3870
+ }
3871
+
3872
+ int
3873
+ archive_read_support_format_zip_streamable(struct archive *_a)
3874
+ {
3875
+ struct archive_read *a = (struct archive_read *)_a;
3876
+ struct zip *zip;
3877
+ int r;
3878
+
3879
+ archive_check_magic(_a, ARCHIVE_READ_MAGIC,
3880
+ ARCHIVE_STATE_NEW, "archive_read_support_format_zip");
3881
+
3882
+ zip = calloc(1, sizeof(*zip));
3883
+ if (zip == NULL) {
3884
+ archive_set_error(&a->archive, ENOMEM,
3885
+ "Can't allocate zip data");
3886
+ return (ARCHIVE_FATAL);
3887
+ }
3888
+
3889
+ /* Streamable reader doesn't support mac extensions. */
3890
+ zip->process_mac_extensions = 0;
3891
+
3892
+ /*
3893
+ * Until enough data has been read, we cannot tell about
3894
+ * any encrypted entries yet.
3895
+ */
3896
+ zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
3897
+ zip->crc32func = real_crc32;
3898
+
3899
+ r = __archive_read_register_format(a,
3900
+ zip,
3901
+ "zip",
3902
+ archive_read_format_zip_streamable_bid,
3903
+ archive_read_format_zip_options,
3904
+ archive_read_format_zip_streamable_read_header,
3905
+ archive_read_format_zip_read_data,
3906
+ archive_read_format_zip_read_data_skip_streamable,
3907
+ NULL,
3908
+ archive_read_format_zip_cleanup,
3909
+ archive_read_support_format_zip_capabilities_streamable,
3910
+ archive_read_format_zip_has_encrypted_entries);
3911
+
3912
+ if (r != ARCHIVE_OK)
3913
+ free(zip);
3914
+ return (ARCHIVE_OK);
3915
+ }
3916
+
3917
+ /* ------------------------------------------------------------------------ */
3918
+
3919
+ /*
3920
+ * Seeking-mode support
3921
+ */
3922
+
3923
+ static int
3924
+ archive_read_support_format_zip_capabilities_seekable(struct archive_read * a)
3925
+ {
3926
+ (void)a; /* UNUSED */
3927
+ return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA |
3928
+ ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
3929
+ }
3930
+
3931
+ /*
3932
+ * TODO: This is a performance sink because it forces the read core to
3933
+ * drop buffered data from the start of file, which will then have to
3934
+ * be re-read again if this bidder loses.
3935
+ *
3936
+ * We workaround this a little by passing in the best bid so far so
3937
+ * that later bidders can do nothing if they know they'll never
3938
+ * outbid. But we can certainly do better...
3939
+ */
3940
+ static int
3941
+ read_eocd(struct zip *zip, const char *p, int64_t current_offset)
3942
+ {
3943
+ uint16_t disk_num;
3944
+ uint32_t cd_size, cd_offset;
3945
+
3946
+ disk_num = archive_le16dec(p + 4);
3947
+ cd_size = archive_le32dec(p + 12);
3948
+ cd_offset = archive_le32dec(p + 16);
3949
+
3950
+ /* Sanity-check the EOCD we've found. */
3951
+
3952
+ /* This must be the first volume. */
3953
+ if (disk_num != 0)
3954
+ return 0;
3955
+ /* Central directory must be on this volume. */
3956
+ if (disk_num != archive_le16dec(p + 6))
3957
+ return 0;
3958
+ /* All central directory entries must be on this volume. */
3959
+ if (archive_le16dec(p + 10) != archive_le16dec(p + 8))
3960
+ return 0;
3961
+ /* Central directory can't extend beyond start of EOCD record. */
3962
+ if ((int64_t)cd_offset + cd_size > current_offset)
3963
+ return 0;
3964
+
3965
+ /* Save the central directory location for later use. */
3966
+ zip->central_directory_offset = cd_offset;
3967
+ zip->central_directory_offset_adjusted = current_offset - cd_size;
3968
+
3969
+ /* This is just a tiny bit higher than the maximum
3970
+ returned by the streaming Zip bidder. This ensures
3971
+ that the more accurate seeking Zip parser wins
3972
+ whenever seek is available. */
3973
+ return 32;
3974
+ }
3975
+
3976
+ /*
3977
+ * Examine Zip64 EOCD locator: If it's valid, store the information
3978
+ * from it.
3979
+ */
3980
+ static int
3981
+ read_zip64_eocd(struct archive_read *a, struct zip *zip, const char *p)
3982
+ {
3983
+ int64_t eocd64_offset;
3984
+ int64_t eocd64_size;
3985
+
3986
+ /* Sanity-check the locator record. */
3987
+
3988
+ /* Central dir must be on first volume. */
3989
+ if (archive_le32dec(p + 4) != 0)
3990
+ return 0;
3991
+ /* Must be only a single volume. */
3992
+ if (archive_le32dec(p + 16) != 1)
3993
+ return 0;
3994
+
3995
+ /* Find the Zip64 EOCD record. */
3996
+ eocd64_offset = archive_le64dec(p + 8);
3997
+ if (__archive_read_seek(a, eocd64_offset, SEEK_SET) < 0)
3998
+ return 0;
3999
+ if ((p = __archive_read_ahead(a, 56, NULL)) == NULL)
4000
+ return 0;
4001
+ /* Make sure we can read all of it. */
4002
+ eocd64_size = archive_le64dec(p + 4) + 12;
4003
+ if (eocd64_size < 56 || eocd64_size > 16384)
4004
+ return 0;
4005
+ if ((p = __archive_read_ahead(a, (size_t)eocd64_size, NULL)) == NULL)
4006
+ return 0;
4007
+
4008
+ /* Sanity-check the EOCD64 */
4009
+ if (archive_le32dec(p + 16) != 0) /* Must be disk #0 */
4010
+ return 0;
4011
+ if (archive_le32dec(p + 20) != 0) /* CD must be on disk #0 */
4012
+ return 0;
4013
+ /* CD can't be split. */
4014
+ if (archive_le64dec(p + 24) != archive_le64dec(p + 32))
4015
+ return 0;
4016
+
4017
+ /* Save the central directory offset for later use. */
4018
+ zip->central_directory_offset = archive_le64dec(p + 48);
4019
+ /* TODO: Needs scanning backwards to find the eocd64 instead of assuming */
4020
+ zip->central_directory_offset_adjusted = zip->central_directory_offset;
4021
+
4022
+ return 32;
4023
+ }
4024
+
4025
+ static int
4026
+ archive_read_format_zip_seekable_bid(struct archive_read *a, int best_bid)
4027
+ {
4028
+ struct zip *zip = a->format->data;
4029
+ int64_t file_size, current_offset;
4030
+ const char *p;
4031
+ int i, tail;
4032
+
4033
+ /* If someone has already bid more than 32, then avoid
4034
+ trashing the look-ahead buffers with a seek. */
4035
+ if (best_bid > 32)
4036
+ return (-1);
4037
+
4038
+ file_size = __archive_read_seek(a, 0, SEEK_END);
4039
+ if (file_size <= 0)
4040
+ return 0;
4041
+
4042
+ /* Search last 16k of file for end-of-central-directory
4043
+ * record (which starts with PK\005\006) */
4044
+ tail = (int)zipmin(1024 * 16, file_size);
4045
+ current_offset = __archive_read_seek(a, -tail, SEEK_END);
4046
+ if (current_offset < 0)
4047
+ return 0;
4048
+ if ((p = __archive_read_ahead(a, (size_t)tail, NULL)) == NULL)
4049
+ return 0;
4050
+ /* Boyer-Moore search backwards from the end, since we want
4051
+ * to match the last EOCD in the file (there can be more than
4052
+ * one if there is an uncompressed Zip archive as a member
4053
+ * within this Zip archive). */
4054
+ for (i = tail - 22; i > 0;) {
4055
+ switch (p[i]) {
4056
+ case 'P':
4057
+ if (memcmp(p + i, "PK\005\006", 4) == 0) {
4058
+ int ret = read_eocd(zip, p + i,
4059
+ current_offset + i);
4060
+ /* Zip64 EOCD locator precedes
4061
+ * regular EOCD if present. */
4062
+ if (i >= 20 && memcmp(p + i - 20, "PK\006\007", 4) == 0) {
4063
+ int ret_zip64 = read_zip64_eocd(a, zip, p + i - 20);
4064
+ if (ret_zip64 > ret)
4065
+ ret = ret_zip64;
4066
+ }
4067
+ return (ret);
4068
+ }
4069
+ i -= 4;
4070
+ break;
4071
+ case 'K': i -= 1; break;
4072
+ case 005: i -= 2; break;
4073
+ case 006: i -= 3; break;
4074
+ default: i -= 4; break;
4075
+ }
4076
+ }
4077
+ return 0;
4078
+ }
4079
+
4080
+ /* The red-black trees are only used in seeking mode to manage
4081
+ * the in-memory copy of the central directory. */
4082
+
4083
+ static int
4084
+ cmp_node(const struct archive_rb_node *n1, const struct archive_rb_node *n2)
4085
+ {
4086
+ const struct zip_entry *e1 = (const struct zip_entry *)n1;
4087
+ const struct zip_entry *e2 = (const struct zip_entry *)n2;
4088
+
4089
+ if (e1->local_header_offset > e2->local_header_offset)
4090
+ return -1;
4091
+ if (e1->local_header_offset < e2->local_header_offset)
4092
+ return 1;
4093
+ return 0;
4094
+ }
4095
+
4096
+ static int
4097
+ cmp_key(const struct archive_rb_node *n, const void *key)
4098
+ {
4099
+ /* This function won't be called */
4100
+ (void)n; /* UNUSED */
4101
+ (void)key; /* UNUSED */
4102
+ return 1;
4103
+ }
4104
+
4105
+ static const struct archive_rb_tree_ops rb_ops = {
4106
+ &cmp_node, &cmp_key
4107
+ };
4108
+
4109
+ static int
4110
+ rsrc_cmp_node(const struct archive_rb_node *n1,
4111
+ const struct archive_rb_node *n2)
4112
+ {
4113
+ const struct zip_entry *e1 = (const struct zip_entry *)n1;
4114
+ const struct zip_entry *e2 = (const struct zip_entry *)n2;
4115
+
4116
+ return (strcmp(e2->rsrcname.s, e1->rsrcname.s));
4117
+ }
4118
+
4119
+ static int
4120
+ rsrc_cmp_key(const struct archive_rb_node *n, const void *key)
4121
+ {
4122
+ const struct zip_entry *e = (const struct zip_entry *)n;
4123
+ return (strcmp((const char *)key, e->rsrcname.s));
4124
+ }
4125
+
4126
+ static const struct archive_rb_tree_ops rb_rsrc_ops = {
4127
+ &rsrc_cmp_node, &rsrc_cmp_key
4128
+ };
4129
+
4130
+ static const char *
4131
+ rsrc_basename(const char *name, size_t name_length)
4132
+ {
4133
+ const char *s, *r;
4134
+
4135
+ r = s = name;
4136
+ for (;;) {
4137
+ s = memchr(s, '/', name_length - (s - name));
4138
+ if (s == NULL)
4139
+ break;
4140
+ r = ++s;
4141
+ }
4142
+ return (r);
4143
+ }
4144
+
4145
+ static void
4146
+ expose_parent_dirs(struct zip *zip, const char *name, size_t name_length)
4147
+ {
4148
+ struct archive_string str;
4149
+ struct zip_entry *dir;
4150
+ char *s;
4151
+
4152
+ archive_string_init(&str);
4153
+ archive_strncpy(&str, name, name_length);
4154
+ for (;;) {
4155
+ s = strrchr(str.s, '/');
4156
+ if (s == NULL)
4157
+ break;
4158
+ *s = '\0';
4159
+ /* Transfer the parent directory from zip->tree_rsrc RB
4160
+ * tree to zip->tree RB tree to expose. */
4161
+ dir = (struct zip_entry *)
4162
+ __archive_rb_tree_find_node(&zip->tree_rsrc, str.s);
4163
+ if (dir == NULL)
4164
+ break;
4165
+ __archive_rb_tree_remove_node(&zip->tree_rsrc, &dir->node);
4166
+ archive_string_free(&dir->rsrcname);
4167
+ __archive_rb_tree_insert_node(&zip->tree, &dir->node);
4168
+ }
4169
+ archive_string_free(&str);
4170
+ }
4171
+
4172
+ static int
4173
+ slurp_central_directory(struct archive_read *a, struct archive_entry* entry,
4174
+ struct zip *zip)
4175
+ {
4176
+ ssize_t i;
4177
+ unsigned found;
4178
+ int64_t correction;
4179
+ ssize_t bytes_avail;
4180
+ const char *p;
4181
+
4182
+ /*
4183
+ * Find the start of the central directory. The end-of-CD
4184
+ * record has our starting point, but there are lots of
4185
+ * Zip archives which have had other data prepended to the
4186
+ * file, which makes the recorded offsets all too small.
4187
+ * So we search forward from the specified offset until we
4188
+ * find the real start of the central directory. Then we
4189
+ * know the correction we need to apply to account for leading
4190
+ * padding.
4191
+ */
4192
+ if (__archive_read_seek(a, zip->central_directory_offset_adjusted, SEEK_SET)
4193
+ < 0)
4194
+ return ARCHIVE_FATAL;
4195
+
4196
+ found = 0;
4197
+ while (!found) {
4198
+ if ((p = __archive_read_ahead(a, 20, &bytes_avail)) == NULL)
4199
+ return ARCHIVE_FATAL;
4200
+ for (found = 0, i = 0; !found && i < bytes_avail - 4;) {
4201
+ switch (p[i + 3]) {
4202
+ case 'P': i += 3; break;
4203
+ case 'K': i += 2; break;
4204
+ case 001: i += 1; break;
4205
+ case 002:
4206
+ if (memcmp(p + i, "PK\001\002", 4) == 0) {
4207
+ p += i;
4208
+ found = 1;
4209
+ } else
4210
+ i += 4;
4211
+ break;
4212
+ case 005: i += 1; break;
4213
+ case 006:
4214
+ if (memcmp(p + i, "PK\005\006", 4) == 0) {
4215
+ p += i;
4216
+ found = 1;
4217
+ } else if (memcmp(p + i, "PK\006\006", 4) == 0) {
4218
+ p += i;
4219
+ found = 1;
4220
+ } else
4221
+ i += 1;
4222
+ break;
4223
+ default: i += 4; break;
4224
+ }
4225
+ }
4226
+ __archive_read_consume(a, i);
4227
+ }
4228
+ correction = archive_filter_bytes(&a->archive, 0)
4229
+ - zip->central_directory_offset;
4230
+
4231
+ __archive_rb_tree_init(&zip->tree, &rb_ops);
4232
+ __archive_rb_tree_init(&zip->tree_rsrc, &rb_rsrc_ops);
4233
+
4234
+ while (1) {
4235
+ struct zip_entry *zip_entry;
4236
+ size_t filename_length, extra_length, comment_length;
4237
+ uint32_t external_attributes;
4238
+ const char *name, *r;
4239
+
4240
+ if ((p = __archive_read_ahead(a, 4, NULL)) == NULL)
4241
+ return ARCHIVE_FATAL;
4242
+ if (memcmp(p, "PK\006\006", 4) == 0
4243
+ || memcmp(p, "PK\005\006", 4) == 0) {
4244
+ break;
4245
+ } else if (memcmp(p, "PK\001\002", 4) != 0) {
4246
+ archive_set_error(&a->archive,
4247
+ -1, "Invalid central directory signature");
4248
+ return ARCHIVE_FATAL;
4249
+ }
4250
+ if ((p = __archive_read_ahead(a, 46, NULL)) == NULL)
4251
+ return ARCHIVE_FATAL;
4252
+
4253
+ zip_entry = calloc(1, sizeof(struct zip_entry));
4254
+ if (zip_entry == NULL) {
4255
+ archive_set_error(&a->archive, ENOMEM,
4256
+ "Can't allocate zip entry");
4257
+ return ARCHIVE_FATAL;
4258
+ }
4259
+ zip_entry->next = zip->zip_entries;
4260
+ zip_entry->flags |= LA_FROM_CENTRAL_DIRECTORY;
4261
+ zip->zip_entries = zip_entry;
4262
+
4263
+ /* version = p[4]; */
4264
+ zip_entry->system = p[5];
4265
+ /* version_required = archive_le16dec(p + 6); */
4266
+ zip_entry->zip_flags = archive_le16dec(p + 8);
4267
+ if (zip_entry->zip_flags
4268
+ & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)){
4269
+ zip->has_encrypted_entries = 1;
4270
+ }
4271
+ zip_entry->compression = (char)archive_le16dec(p + 10);
4272
+ zip_entry->mtime = dos_to_unix(archive_le32dec(p + 12));
4273
+ zip_entry->crc32 = archive_le32dec(p + 16);
4274
+ if (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
4275
+ zip_entry->decdat = p[13];
4276
+ else
4277
+ zip_entry->decdat = p[19];
4278
+ zip_entry->compressed_size = archive_le32dec(p + 20);
4279
+ zip_entry->uncompressed_size = archive_le32dec(p + 24);
4280
+ filename_length = archive_le16dec(p + 28);
4281
+ extra_length = archive_le16dec(p + 30);
4282
+ comment_length = archive_le16dec(p + 32);
4283
+ /* disk_start = archive_le16dec(p + 34);
4284
+ * Better be zero.
4285
+ * internal_attributes = archive_le16dec(p + 36);
4286
+ * text bit */
4287
+ external_attributes = archive_le32dec(p + 38);
4288
+ zip_entry->local_header_offset =
4289
+ archive_le32dec(p + 42) + correction;
4290
+
4291
+ /* If we can't guess the mode, leave it zero here;
4292
+ when we read the local file header we might get
4293
+ more information. */
4294
+ if (zip_entry->system == 3) {
4295
+ zip_entry->mode = external_attributes >> 16;
4296
+ } else if (zip_entry->system == 0) {
4297
+ // Interpret MSDOS directory bit
4298
+ if (0x10 == (external_attributes & 0x10)) {
4299
+ zip_entry->mode = AE_IFDIR | 0775;
4300
+ } else {
4301
+ zip_entry->mode = AE_IFREG | 0664;
4302
+ }
4303
+ if (0x01 == (external_attributes & 0x01)) {
4304
+ // Read-only bit; strip write permissions
4305
+ zip_entry->mode &= 0555;
4306
+ }
4307
+ } else {
4308
+ zip_entry->mode = 0;
4309
+ }
4310
+
4311
+ /* We're done with the regular data; get the filename and
4312
+ * extra data. */
4313
+ __archive_read_consume(a, 46);
4314
+ p = __archive_read_ahead(a, filename_length + extra_length,
4315
+ NULL);
4316
+ if (p == NULL) {
4317
+ archive_set_error(&a->archive,
4318
+ ARCHIVE_ERRNO_FILE_FORMAT,
4319
+ "Truncated ZIP file header");
4320
+ return ARCHIVE_FATAL;
4321
+ }
4322
+ if (ARCHIVE_OK != process_extra(a, entry, p + filename_length,
4323
+ extra_length, zip_entry)) {
4324
+ return ARCHIVE_FATAL;
4325
+ }
4326
+
4327
+ /*
4328
+ * Mac resource fork files are stored under the
4329
+ * "__MACOSX/" directory, so we should check if
4330
+ * it is.
4331
+ */
4332
+ if (!zip->process_mac_extensions) {
4333
+ /* Treat every entry as a regular entry. */
4334
+ __archive_rb_tree_insert_node(&zip->tree,
4335
+ &zip_entry->node);
4336
+ } else {
4337
+ name = p;
4338
+ r = rsrc_basename(name, filename_length);
4339
+ if (filename_length >= 9 &&
4340
+ strncmp("__MACOSX/", name, 9) == 0) {
4341
+ const char *name_end;
4342
+
4343
+ name_end = name + filename_length;
4344
+ /* If this file is not a resource fork nor
4345
+ * a directory. We should treat it as a non
4346
+ * resource fork file to expose it. */
4347
+ if (name[filename_length-1] != '/' &&
4348
+ (name_end - r < 2 || r[0] != '.' || r[1] != '_')) {
4349
+ __archive_rb_tree_insert_node(
4350
+ &zip->tree, &zip_entry->node);
4351
+ /* Expose its parent directories. */
4352
+ expose_parent_dirs(zip, name,
4353
+ filename_length);
4354
+ } else {
4355
+ /* This file is a resource fork file or
4356
+ * a directory. */
4357
+ archive_strncpy(&(zip_entry->rsrcname),
4358
+ name, filename_length);
4359
+ __archive_rb_tree_insert_node(
4360
+ &zip->tree_rsrc, &zip_entry->node);
4361
+ }
4362
+ } else {
4363
+ /* Generate resource fork name to find its
4364
+ * resource file at zip->tree_rsrc. */
4365
+
4366
+ /* If this is an entry ending with slash,
4367
+ * make the resource for name slash-less
4368
+ * as the actual resource fork doesn't end with '/'.
4369
+ */
4370
+ size_t tmp_length = filename_length;
4371
+ if (tmp_length > 0 && name[tmp_length - 1] == '/') {
4372
+ tmp_length--;
4373
+ r = rsrc_basename(name, tmp_length);
4374
+ }
4375
+
4376
+ archive_strcpy(&(zip_entry->rsrcname),
4377
+ "__MACOSX/");
4378
+ archive_strncat(&(zip_entry->rsrcname),
4379
+ name, r - name);
4380
+ archive_strcat(&(zip_entry->rsrcname), "._");
4381
+ archive_strncat(&(zip_entry->rsrcname),
4382
+ name + (r - name),
4383
+ tmp_length - (r - name));
4384
+ /* Register an entry to RB tree to sort it by
4385
+ * file offset. */
4386
+ __archive_rb_tree_insert_node(&zip->tree,
4387
+ &zip_entry->node);
4388
+ }
4389
+ }
4390
+
4391
+ /* Skip the comment too ... */
4392
+ __archive_read_consume(a,
4393
+ filename_length + extra_length + comment_length);
4394
+ }
4395
+
4396
+ return ARCHIVE_OK;
4397
+ }
4398
+
4399
+ static ssize_t
4400
+ zip_get_local_file_header_size(struct archive_read *a, size_t extra)
4401
+ {
4402
+ const char *p;
4403
+ ssize_t filename_length, extra_length;
4404
+
4405
+ if ((p = __archive_read_ahead(a, extra + ZIP_LOCHDR_LEN, NULL)) == NULL) {
4406
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
4407
+ "Truncated ZIP file header");
4408
+ return (ARCHIVE_WARN);
4409
+ }
4410
+ p += extra;
4411
+
4412
+ if (memcmp(p, "PK\003\004", 4) != 0) {
4413
+ archive_set_error(&a->archive, -1, "Damaged Zip archive");
4414
+ return ARCHIVE_WARN;
4415
+ }
4416
+ filename_length = archive_le16dec(p + 26);
4417
+ extra_length = archive_le16dec(p + 28);
4418
+
4419
+ return (ZIP_LOCHDR_LEN + filename_length + extra_length);
4420
+ }
4421
+
4422
+ static int
4423
+ zip_read_mac_metadata(struct archive_read *a, struct archive_entry *entry,
4424
+ struct zip_entry *rsrc)
4425
+ {
4426
+ struct zip *zip = a->format->data;
4427
+ unsigned char *metadata, *mp;
4428
+ int64_t offset = archive_filter_bytes(&a->archive, 0);
4429
+ size_t remaining_bytes, metadata_bytes;
4430
+ ssize_t hsize;
4431
+ int ret = ARCHIVE_OK, eof;
4432
+
4433
+ switch(rsrc->compression) {
4434
+ case 0: /* No compression. */
4435
+ if (rsrc->uncompressed_size != rsrc->compressed_size) {
4436
+ archive_set_error(&a->archive,
4437
+ ARCHIVE_ERRNO_FILE_FORMAT,
4438
+ "Malformed OS X metadata entry: "
4439
+ "inconsistent size");
4440
+ return (ARCHIVE_FATAL);
4441
+ }
4442
+ #ifdef HAVE_ZLIB_H
4443
+ case 8: /* Deflate compression. */
4444
+ #endif
4445
+ break;
4446
+ default: /* Unsupported compression. */
4447
+ /* Return a warning. */
4448
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
4449
+ "Unsupported ZIP compression method (%s)",
4450
+ compression_name(rsrc->compression));
4451
+ /* We can't decompress this entry, but we will
4452
+ * be able to skip() it and try the next entry. */
4453
+ return (ARCHIVE_WARN);
4454
+ }
4455
+
4456
+ if (rsrc->uncompressed_size > ZIP_MAX_METADATA * 1048576U) {
4457
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
4458
+ "Mac metadata is too large: %jd > %u MiB",
4459
+ (intmax_t)rsrc->uncompressed_size, ZIP_MAX_METADATA);
4460
+ return (ARCHIVE_WARN);
4461
+ }
4462
+ if (rsrc->compressed_size > ZIP_MAX_METADATA * 1048576U) {
4463
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
4464
+ "Mac metadata is too large: %jd > %u MiB",
4465
+ (intmax_t)rsrc->compressed_size, ZIP_MAX_METADATA);
4466
+ return (ARCHIVE_WARN);
4467
+ }
4468
+
4469
+ metadata = malloc((size_t)rsrc->uncompressed_size);
4470
+ if (metadata == NULL) {
4471
+ archive_set_error(&a->archive, ENOMEM,
4472
+ "Can't allocate memory for Mac metadata");
4473
+ return (ARCHIVE_FATAL);
4474
+ }
4475
+
4476
+ if (offset < rsrc->local_header_offset)
4477
+ __archive_read_consume(a, rsrc->local_header_offset - offset);
4478
+ else if (offset != rsrc->local_header_offset) {
4479
+ __archive_read_seek(a, rsrc->local_header_offset, SEEK_SET);
4480
+ }
4481
+
4482
+ hsize = zip_get_local_file_header_size(a, 0);
4483
+ __archive_read_consume(a, hsize);
4484
+
4485
+ remaining_bytes = (size_t)rsrc->compressed_size;
4486
+ metadata_bytes = (size_t)rsrc->uncompressed_size;
4487
+ mp = metadata;
4488
+ eof = 0;
4489
+ while (!eof && remaining_bytes) {
4490
+ const unsigned char *p;
4491
+ ssize_t bytes_avail;
4492
+ size_t bytes_used;
4493
+
4494
+ p = __archive_read_ahead(a, 1, &bytes_avail);
4495
+ if (p == NULL) {
4496
+ archive_set_error(&a->archive,
4497
+ ARCHIVE_ERRNO_FILE_FORMAT,
4498
+ "Truncated ZIP file header");
4499
+ ret = ARCHIVE_WARN;
4500
+ goto exit_mac_metadata;
4501
+ }
4502
+ if ((size_t)bytes_avail > remaining_bytes)
4503
+ bytes_avail = remaining_bytes;
4504
+ switch(rsrc->compression) {
4505
+ case 0: /* No compression. */
4506
+ if ((size_t)bytes_avail > metadata_bytes)
4507
+ bytes_avail = metadata_bytes;
4508
+ memcpy(mp, p, bytes_avail);
4509
+ bytes_used = (size_t)bytes_avail;
4510
+ metadata_bytes -= bytes_used;
4511
+ mp += bytes_used;
4512
+ if (metadata_bytes == 0)
4513
+ eof = 1;
4514
+ break;
4515
+ #ifdef HAVE_ZLIB_H
4516
+ case 8: /* Deflate compression. */
4517
+ {
4518
+ int r;
4519
+
4520
+ ret = zip_deflate_init(a, zip);
4521
+ if (ret != ARCHIVE_OK)
4522
+ goto exit_mac_metadata;
4523
+ zip->stream.next_in =
4524
+ (Bytef *)(uintptr_t)(const void *)p;
4525
+ zip->stream.avail_in = (uInt)bytes_avail;
4526
+ zip->stream.total_in = 0;
4527
+ zip->stream.next_out = mp;
4528
+ zip->stream.avail_out = (uInt)metadata_bytes;
4529
+ zip->stream.total_out = 0;
4530
+
4531
+ r = inflate(&zip->stream, 0);
4532
+ switch (r) {
4533
+ case Z_OK:
4534
+ break;
4535
+ case Z_STREAM_END:
4536
+ eof = 1;
4537
+ break;
4538
+ case Z_MEM_ERROR:
4539
+ archive_set_error(&a->archive, ENOMEM,
4540
+ "Out of memory for ZIP decompression");
4541
+ ret = ARCHIVE_FATAL;
4542
+ goto exit_mac_metadata;
4543
+ default:
4544
+ archive_set_error(&a->archive,
4545
+ ARCHIVE_ERRNO_MISC,
4546
+ "ZIP decompression failed (%d)", r);
4547
+ ret = ARCHIVE_FATAL;
4548
+ goto exit_mac_metadata;
4549
+ }
4550
+ bytes_used = zip->stream.total_in;
4551
+ metadata_bytes -= zip->stream.total_out;
4552
+ mp += zip->stream.total_out;
4553
+ break;
4554
+ }
4555
+ #endif
4556
+ default:
4557
+ bytes_used = 0;
4558
+ break;
4559
+ }
4560
+ __archive_read_consume(a, bytes_used);
4561
+ remaining_bytes -= bytes_used;
4562
+ }
4563
+ archive_entry_copy_mac_metadata(entry, metadata,
4564
+ (size_t)rsrc->uncompressed_size - metadata_bytes);
4565
+
4566
+ exit_mac_metadata:
4567
+ __archive_read_seek(a, offset, SEEK_SET);
4568
+ zip->decompress_init = 0;
4569
+ free(metadata);
4570
+ return (ret);
4571
+ }
4572
+
4573
+ static int
4574
+ archive_read_format_zip_seekable_read_header(struct archive_read *a,
4575
+ struct archive_entry *entry)
4576
+ {
4577
+ struct zip *zip = a->format->data;
4578
+ struct zip_entry *rsrc;
4579
+ int64_t offset;
4580
+ int r, ret = ARCHIVE_OK;
4581
+
4582
+ /*
4583
+ * It should be sufficient to call archive_read_next_header() for
4584
+ * a reader to determine if an entry is encrypted or not. If the
4585
+ * encryption of an entry is only detectable when calling
4586
+ * archive_read_data(), so be it. We'll do the same check there
4587
+ * as well.
4588
+ */
4589
+ if (zip->has_encrypted_entries ==
4590
+ ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW)
4591
+ zip->has_encrypted_entries = 0;
4592
+
4593
+ a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
4594
+ if (a->archive.archive_format_name == NULL)
4595
+ a->archive.archive_format_name = "ZIP";
4596
+
4597
+ if (zip->zip_entries == NULL) {
4598
+ r = slurp_central_directory(a, entry, zip);
4599
+ if (r != ARCHIVE_OK)
4600
+ return r;
4601
+ /* Get first entry whose local header offset is lower than
4602
+ * other entries in the archive file. */
4603
+ zip->entry =
4604
+ (struct zip_entry *)ARCHIVE_RB_TREE_MIN(&zip->tree);
4605
+ } else if (zip->entry != NULL) {
4606
+ /* Get next entry in local header offset order. */
4607
+ zip->entry = (struct zip_entry *)__archive_rb_tree_iterate(
4608
+ &zip->tree, &zip->entry->node, ARCHIVE_RB_DIR_RIGHT);
4609
+ }
4610
+
4611
+ if (zip->entry == NULL)
4612
+ return ARCHIVE_EOF;
4613
+
4614
+ if (zip->entry->rsrcname.s)
4615
+ rsrc = (struct zip_entry *)__archive_rb_tree_find_node(
4616
+ &zip->tree_rsrc, zip->entry->rsrcname.s);
4617
+ else
4618
+ rsrc = NULL;
4619
+
4620
+ if (zip->cctx_valid)
4621
+ archive_decrypto_aes_ctr_release(&zip->cctx);
4622
+ if (zip->hctx_valid)
4623
+ archive_hmac_sha1_cleanup(&zip->hctx);
4624
+ zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
4625
+ __archive_read_reset_passphrase(a);
4626
+
4627
+ /* File entries are sorted by the header offset, we should mostly
4628
+ * use __archive_read_consume to advance a read point to avoid
4629
+ * redundant data reading. */
4630
+ offset = archive_filter_bytes(&a->archive, 0);
4631
+ if (offset < zip->entry->local_header_offset)
4632
+ __archive_read_consume(a,
4633
+ zip->entry->local_header_offset - offset);
4634
+ else if (offset != zip->entry->local_header_offset) {
4635
+ __archive_read_seek(a, zip->entry->local_header_offset,
4636
+ SEEK_SET);
4637
+ }
4638
+ zip->unconsumed = 0;
4639
+ r = zip_read_local_file_header(a, entry, zip);
4640
+ if (r != ARCHIVE_OK)
4641
+ return r;
4642
+ if (rsrc) {
4643
+ int ret2 = zip_read_mac_metadata(a, entry, rsrc);
4644
+ if (ret2 < ret)
4645
+ ret = ret2;
4646
+ }
4647
+ return (ret);
4648
+ }
4649
+
4650
+ /*
4651
+ * We're going to seek for the next header anyway, so we don't
4652
+ * need to bother doing anything here.
4653
+ */
4654
+ static int
4655
+ archive_read_format_zip_read_data_skip_seekable(struct archive_read *a)
4656
+ {
4657
+ struct zip *zip = a->format->data;
4658
+
4659
+ zip->unconsumed = 0;
4660
+ return (ARCHIVE_OK);
4661
+ }
4662
+
4663
+ int
4664
+ archive_read_support_format_zip_seekable(struct archive *_a)
4665
+ {
4666
+ struct archive_read *a = (struct archive_read *)_a;
4667
+ struct zip *zip;
4668
+ int r;
4669
+
4670
+ archive_check_magic(_a, ARCHIVE_READ_MAGIC,
4671
+ ARCHIVE_STATE_NEW, "archive_read_support_format_zip_seekable");
4672
+
4673
+ zip = calloc(1, sizeof(*zip));
4674
+ if (zip == NULL) {
4675
+ archive_set_error(&a->archive, ENOMEM,
4676
+ "Can't allocate zip data");
4677
+ return (ARCHIVE_FATAL);
4678
+ }
4679
+
4680
+ #ifdef HAVE_COPYFILE_H
4681
+ /* Set this by default on Mac OS. */
4682
+ zip->process_mac_extensions = 1;
4683
+ #endif
4684
+
4685
+ /*
4686
+ * Until enough data has been read, we cannot tell about
4687
+ * any encrypted entries yet.
4688
+ */
4689
+ zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
4690
+ zip->crc32func = real_crc32;
4691
+
4692
+ r = __archive_read_register_format(a,
4693
+ zip,
4694
+ "zip",
4695
+ archive_read_format_zip_seekable_bid,
4696
+ archive_read_format_zip_options,
4697
+ archive_read_format_zip_seekable_read_header,
4698
+ archive_read_format_zip_read_data,
4699
+ archive_read_format_zip_read_data_skip_seekable,
4700
+ NULL,
4701
+ archive_read_format_zip_cleanup,
4702
+ archive_read_support_format_zip_capabilities_seekable,
4703
+ archive_read_format_zip_has_encrypted_entries);
4704
+
4705
+ if (r != ARCHIVE_OK)
4706
+ free(zip);
4707
+ return (ARCHIVE_OK);
4708
+ }
4709
+
4710
+ /*# vim:set noet:*/