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

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 +10 -8
  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,3516 @@
1
+ /*-
2
+ * Copyright (c) 2003-2007 Tim Kientzle
3
+ * Copyright (c) 2009 Andreas Henriksson <andreas@fatal.se>
4
+ * Copyright (c) 2009-2012 Michihiro NAKAJIMA
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
+ #ifdef HAVE_ERRNO_H
31
+ #include <errno.h>
32
+ #endif
33
+ /* #include <stdint.h> */ /* See archive_platform.h */
34
+ #include <stdio.h>
35
+ #ifdef HAVE_STDLIB_H
36
+ #include <stdlib.h>
37
+ #endif
38
+ #ifdef HAVE_STRING_H
39
+ #include <string.h>
40
+ #endif
41
+ #include <time.h>
42
+ #ifdef HAVE_ZLIB_H
43
+ #include <zlib.h>
44
+ #endif
45
+
46
+ #include "archive.h"
47
+ #include "archive_endian.h"
48
+ #include "archive_entry.h"
49
+ #include "archive_entry_locale.h"
50
+ #include "archive_integer.h"
51
+ #include "archive_private.h"
52
+ #include "archive_read_private.h"
53
+ #include "archive_string.h"
54
+
55
+ /*
56
+ * An overview of ISO 9660 format:
57
+ *
58
+ * Each disk is laid out as follows:
59
+ * * 32k reserved for private use
60
+ * * Volume descriptor table. Each volume descriptor
61
+ * is 2k and specifies basic format information.
62
+ * The "Primary Volume Descriptor" (PVD) is defined by the
63
+ * standard and should always be present; other volume
64
+ * descriptors include various vendor-specific extensions.
65
+ * * Files and directories. Each file/dir is specified by
66
+ * an "extent" (starting sector and length in bytes).
67
+ * Dirs are just files with directory records packed one
68
+ * after another. The PVD contains a single dir entry
69
+ * specifying the location of the root directory. Everything
70
+ * else follows from there.
71
+ *
72
+ * This module works by first reading the volume descriptors, then
73
+ * building a list of directory entries, sorted by starting
74
+ * sector. At each step, I look for the earliest dir entry that
75
+ * hasn't yet been read, seek forward to that location and read
76
+ * that entry. If it's a dir, I slurp in the new dir entries and
77
+ * add them to the heap; if it's a regular file, I return the
78
+ * corresponding archive_entry and wait for the client to request
79
+ * the file body. This strategy allows us to read most compliant
80
+ * CDs with a single pass through the data, as required by libarchive.
81
+ */
82
+ #define LOGICAL_BLOCK_SIZE 2048
83
+ #define SYSTEM_AREA_BLOCK 16
84
+
85
+ /* Structure of on-disk primary volume descriptor. */
86
+ #define PVD_type_offset 0
87
+ #define PVD_type_size 1
88
+ #define PVD_id_offset (PVD_type_offset + PVD_type_size)
89
+ #define PVD_id_size 5
90
+ #define PVD_version_offset (PVD_id_offset + PVD_id_size)
91
+ #define PVD_version_size 1
92
+ #define PVD_reserved1_offset (PVD_version_offset + PVD_version_size)
93
+ #define PVD_reserved1_size 1
94
+ #define PVD_system_id_offset (PVD_reserved1_offset + PVD_reserved1_size)
95
+ #define PVD_system_id_size 32
96
+ #define PVD_volume_id_offset (PVD_system_id_offset + PVD_system_id_size)
97
+ #define PVD_volume_id_size 32
98
+ #define PVD_reserved2_offset (PVD_volume_id_offset + PVD_volume_id_size)
99
+ #define PVD_reserved2_size 8
100
+ #define PVD_volume_space_size_offset (PVD_reserved2_offset + PVD_reserved2_size)
101
+ #define PVD_volume_space_size_size 8
102
+ #define PVD_reserved3_offset (PVD_volume_space_size_offset + PVD_volume_space_size_size)
103
+ #define PVD_reserved3_size 32
104
+ #define PVD_volume_set_size_offset (PVD_reserved3_offset + PVD_reserved3_size)
105
+ #define PVD_volume_set_size_size 4
106
+ #define PVD_volume_sequence_number_offset (PVD_volume_set_size_offset + PVD_volume_set_size_size)
107
+ #define PVD_volume_sequence_number_size 4
108
+ #define PVD_logical_block_size_offset (PVD_volume_sequence_number_offset + PVD_volume_sequence_number_size)
109
+ #define PVD_logical_block_size_size 4
110
+ #define PVD_path_table_size_offset (PVD_logical_block_size_offset + PVD_logical_block_size_size)
111
+ #define PVD_path_table_size_size 8
112
+ #define PVD_type_1_path_table_offset (PVD_path_table_size_offset + PVD_path_table_size_size)
113
+ #define PVD_type_1_path_table_size 4
114
+ #define PVD_opt_type_1_path_table_offset (PVD_type_1_path_table_offset + PVD_type_1_path_table_size)
115
+ #define PVD_opt_type_1_path_table_size 4
116
+ #define PVD_type_m_path_table_offset (PVD_opt_type_1_path_table_offset + PVD_opt_type_1_path_table_size)
117
+ #define PVD_type_m_path_table_size 4
118
+ #define PVD_opt_type_m_path_table_offset (PVD_type_m_path_table_offset + PVD_type_m_path_table_size)
119
+ #define PVD_opt_type_m_path_table_size 4
120
+ #define PVD_root_directory_record_offset (PVD_opt_type_m_path_table_offset + PVD_opt_type_m_path_table_size)
121
+ #define PVD_root_directory_record_size 34
122
+ #define PVD_volume_set_id_offset (PVD_root_directory_record_offset + PVD_root_directory_record_size)
123
+ #define PVD_volume_set_id_size 128
124
+ #define PVD_publisher_id_offset (PVD_volume_set_id_offset + PVD_volume_set_id_size)
125
+ #define PVD_publisher_id_size 128
126
+ #define PVD_preparer_id_offset (PVD_publisher_id_offset + PVD_publisher_id_size)
127
+ #define PVD_preparer_id_size 128
128
+ #define PVD_application_id_offset (PVD_preparer_id_offset + PVD_preparer_id_size)
129
+ #define PVD_application_id_size 128
130
+ #define PVD_copyright_file_id_offset (PVD_application_id_offset + PVD_application_id_size)
131
+ #define PVD_copyright_file_id_size 37
132
+ #define PVD_abstract_file_id_offset (PVD_copyright_file_id_offset + PVD_copyright_file_id_size)
133
+ #define PVD_abstract_file_id_size 37
134
+ #define PVD_bibliographic_file_id_offset (PVD_abstract_file_id_offset + PVD_abstract_file_id_size)
135
+ #define PVD_bibliographic_file_id_size 37
136
+ #define PVD_creation_date_offset (PVD_bibliographic_file_id_offset + PVD_bibliographic_file_id_size)
137
+ #define PVD_creation_date_size 17
138
+ #define PVD_modification_date_offset (PVD_creation_date_offset + PVD_creation_date_size)
139
+ #define PVD_modification_date_size 17
140
+ #define PVD_expiration_date_offset (PVD_modification_date_offset + PVD_modification_date_size)
141
+ #define PVD_expiration_date_size 17
142
+ #define PVD_effective_date_offset (PVD_expiration_date_offset + PVD_expiration_date_size)
143
+ #define PVD_effective_date_size 17
144
+ #define PVD_file_structure_version_offset (PVD_effective_date_offset + PVD_effective_date_size)
145
+ #define PVD_file_structure_version_size 1
146
+ #define PVD_reserved4_offset (PVD_file_structure_version_offset + PVD_file_structure_version_size)
147
+ #define PVD_reserved4_size 1
148
+ #define PVD_application_data_offset (PVD_reserved4_offset + PVD_reserved4_size)
149
+ #define PVD_application_data_size 512
150
+ #define PVD_reserved5_offset (PVD_application_data_offset + PVD_application_data_size)
151
+ #define PVD_reserved5_size (2048 - PVD_reserved5_offset)
152
+
153
+ /* TODO: It would make future maintenance easier to just hardcode the
154
+ * above values. In particular, ECMA119 states the offsets as part of
155
+ * the standard. That would eliminate the need for the following check.*/
156
+ #if PVD_reserved5_offset != 1395
157
+ #error PVD offset and size definitions are wrong.
158
+ #endif
159
+
160
+
161
+ /* Structure of optional on-disk supplementary volume descriptor. */
162
+ #define SVD_type_offset 0
163
+ #define SVD_type_size 1
164
+ #define SVD_id_offset (SVD_type_offset + SVD_type_size)
165
+ #define SVD_id_size 5
166
+ #define SVD_version_offset (SVD_id_offset + SVD_id_size)
167
+ #define SVD_version_size 1
168
+ /* ... */
169
+ #define SVD_reserved1_offset 72
170
+ #define SVD_reserved1_size 8
171
+ #define SVD_volume_space_size_offset 80
172
+ #define SVD_volume_space_size_size 8
173
+ #define SVD_escape_sequences_offset (SVD_volume_space_size_offset + SVD_volume_space_size_size)
174
+ #define SVD_escape_sequences_size 32
175
+ /* ... */
176
+ #define SVD_logical_block_size_offset 128
177
+ #define SVD_logical_block_size_size 4
178
+ #define SVD_type_L_path_table_offset 140
179
+ #define SVD_type_M_path_table_offset 148
180
+ /* ... */
181
+ #define SVD_root_directory_record_offset 156
182
+ #define SVD_root_directory_record_size 34
183
+ #define SVD_file_structure_version_offset 881
184
+ #define SVD_reserved2_offset 882
185
+ #define SVD_reserved2_size 1
186
+ #define SVD_reserved3_offset 1395
187
+ #define SVD_reserved3_size 653
188
+ /* ... */
189
+ /* FIXME: validate correctness of last SVD entry offset. */
190
+
191
+ /* Structure of an on-disk directory record. */
192
+ /* Note: ISO9660 stores each multi-byte integer twice, once in
193
+ * each byte order. The sizes here are the size of just one
194
+ * of the two integers. (This is why the offset of a field isn't
195
+ * the same as the offset+size of the previous field.) */
196
+ #define DR_length_offset 0
197
+ #define DR_length_size 1
198
+ #define DR_ext_attr_length_offset 1
199
+ #define DR_ext_attr_length_size 1
200
+ #define DR_extent_offset 2
201
+ #define DR_extent_size 4
202
+ #define DR_size_offset 10
203
+ #define DR_size_size 4
204
+ #define DR_date_offset 18
205
+ #define DR_date_size 7
206
+ #define DR_flags_offset 25
207
+ #define DR_flags_size 1
208
+ #define DR_file_unit_size_offset 26
209
+ #define DR_file_unit_size_size 1
210
+ #define DR_interleave_offset 27
211
+ #define DR_interleave_size 1
212
+ #define DR_volume_sequence_number_offset 28
213
+ #define DR_volume_sequence_number_size 2
214
+ #define DR_name_len_offset 32
215
+ #define DR_name_len_size 1
216
+ #define DR_name_offset 33
217
+
218
+ #ifdef HAVE_ZLIB_H
219
+ static const unsigned char zisofs_magic[8] = {
220
+ 0x37, 0xE4, 0x53, 0x96, 0xC9, 0xDB, 0xD6, 0x07
221
+ };
222
+
223
+ struct zisofs {
224
+ /* Set 1 if this file compressed by paged zlib */
225
+ int pz;
226
+ int pz_log2_bs; /* Log2 of block size */
227
+ uint64_t pz_uncompressed_size;
228
+
229
+ int initialized;
230
+ unsigned char *uncompressed_buffer;
231
+ size_t uncompressed_buffer_size;
232
+
233
+ uint32_t pz_offset;
234
+ unsigned char header[16];
235
+ size_t header_avail;
236
+ int header_passed;
237
+ unsigned char *block_pointers;
238
+ size_t block_pointers_alloc;
239
+ size_t block_pointers_size;
240
+ size_t block_pointers_avail;
241
+ size_t block_off;
242
+ uint32_t block_avail;
243
+
244
+ z_stream stream;
245
+ int stream_valid;
246
+ };
247
+ #else
248
+ struct zisofs {
249
+ /* Set 1 if this file compressed by paged zlib */
250
+ int pz;
251
+ };
252
+ #endif
253
+
254
+ struct content {
255
+ uint64_t offset;/* Offset on disk. */
256
+ uint64_t size; /* File size in bytes. */
257
+ struct content *next;
258
+ };
259
+
260
+ /* In-memory storage for a directory record. */
261
+ struct file_info {
262
+ struct file_info *use_next;
263
+ struct file_info *parent;
264
+ struct file_info *next;
265
+ struct file_info *re_next;
266
+ int subdirs;
267
+ uint64_t key; /* Heap Key. */
268
+ uint64_t offset; /* Offset on disk. */
269
+ uint64_t size; /* File size in bytes. */
270
+ uint32_t ce_offset; /* Offset of CE. */
271
+ uint32_t ce_size; /* Size of CE. */
272
+ uint64_t ce_processed_end;/* End offset of processed CE. */
273
+ char rr_moved; /* Flag to rr_moved. */
274
+ char rr_moved_has_re_only;
275
+ char re; /* Having RRIP "RE" extension. */
276
+ char re_descendant;
277
+ uint64_t cl_offset; /* Having RRIP "CL" extension. */
278
+ int time_is_set; /* Bitmask indicating which times are known */
279
+ time_t birthtime; /* File created time. */
280
+ time_t mtime; /* File last modified time. */
281
+ time_t atime; /* File last accessed time. */
282
+ time_t ctime; /* File attribute change time. */
283
+ uint64_t rdev; /* Device number. */
284
+ mode_t mode;
285
+ uid_t uid;
286
+ gid_t gid;
287
+ int64_t number;
288
+ int nlinks;
289
+ struct archive_string name; /* Pathname */
290
+ unsigned char *utf16be_name;
291
+ size_t utf16be_bytes;
292
+ char name_continues; /* Non-zero if name continues */
293
+ struct archive_string symlink;
294
+ char symlink_continues; /* Non-zero if link continues */
295
+ /* Set 1 if this file compressed by paged zlib(zisofs) */
296
+ int pz;
297
+ int pz_log2_bs; /* Log2 of block size */
298
+ uint64_t pz_uncompressed_size;
299
+ /* Set 1 if this file is multi extent. */
300
+ int multi_extent;
301
+ struct {
302
+ struct content *first;
303
+ struct content **last;
304
+ } contents;
305
+ struct {
306
+ struct file_info *first;
307
+ struct file_info **last;
308
+ } rede_files;
309
+ };
310
+
311
+ #define BIRTHTIME_IS_SET 1
312
+ #define MTIME_IS_SET 2
313
+ #define ATIME_IS_SET 4
314
+ #define CTIME_IS_SET 8
315
+
316
+ struct heap_queue {
317
+ struct file_info **files;
318
+ size_t allocated;
319
+ size_t used;
320
+ };
321
+
322
+ struct iso9660 {
323
+ int magic;
324
+ #define ISO9660_MAGIC 0x96609660
325
+
326
+ int opt_support_joliet;
327
+ int opt_support_rockridge;
328
+
329
+ struct archive_string pathname;
330
+ char seenRockridge; /* Set true if RR extensions are used. */
331
+ char seenSUSP; /* Set true if SUSP is being used. */
332
+ char seenJoliet;
333
+
334
+ unsigned char suspOffset;
335
+ struct file_info *rr_moved;
336
+ struct read_ce_queue {
337
+ struct read_ce_req {
338
+ uint64_t offset;/* Offset of CE on disk. */
339
+ struct file_info *file;
340
+ } *reqs;
341
+ size_t cnt;
342
+ size_t allocated;
343
+ } read_ce_req;
344
+
345
+ int64_t previous_number;
346
+ struct archive_string previous_pathname;
347
+
348
+ struct file_info *use_files;
349
+ struct heap_queue pending_files;
350
+ struct {
351
+ struct file_info *first;
352
+ struct file_info **last;
353
+ } cache_files;
354
+ struct {
355
+ struct file_info *first;
356
+ struct file_info **last;
357
+ } re_files;
358
+
359
+ uint64_t current_position;
360
+ ssize_t logical_block_size;
361
+ uint64_t volume_size; /* Total size of volume in bytes. */
362
+ int32_t volume_block;/* Total size of volume in logical blocks. */
363
+
364
+ struct vd {
365
+ int location; /* Location of Extent. */
366
+ uint32_t size;
367
+ } primary, joliet;
368
+
369
+ int64_t entry_sparse_offset;
370
+ int64_t entry_bytes_remaining;
371
+ size_t entry_bytes_unconsumed;
372
+ struct zisofs entry_zisofs;
373
+ struct content *entry_content;
374
+ struct archive_string_conv *sconv_utf16be;
375
+ /*
376
+ * Buffers for a full pathname in UTF-16BE in Joliet extensions.
377
+ */
378
+ #define UTF16_NAME_MAX 1024
379
+ unsigned char *utf16be_path;
380
+ size_t utf16be_path_len;
381
+ unsigned char *utf16be_previous_path;
382
+ size_t utf16be_previous_path_len;
383
+ };
384
+
385
+ static int archive_read_format_iso9660_bid(struct archive_read *, int);
386
+ static int archive_read_format_iso9660_options(struct archive_read *,
387
+ const char *, const char *);
388
+ static int archive_read_format_iso9660_cleanup(struct archive_read *);
389
+ static int archive_read_format_iso9660_read_data(struct archive_read *,
390
+ const void **, size_t *, int64_t *);
391
+ static int archive_read_format_iso9660_read_data_skip(struct archive_read *);
392
+ static int archive_read_format_iso9660_read_header(struct archive_read *,
393
+ struct archive_entry *);
394
+ static const char *build_pathname(struct archive_string *, struct file_info *, int);
395
+ static int build_pathname_utf16be(unsigned char *, size_t, size_t *,
396
+ struct file_info *);
397
+ #if DEBUG
398
+ static void dump_isodirrec(FILE *, const unsigned char *isodirrec);
399
+ #endif
400
+ static time_t time_from_tm(struct tm *);
401
+ static time_t isodate17(const unsigned char *);
402
+ static int isodate17_valid(const unsigned char *);
403
+ static time_t isodate7(const unsigned char *);
404
+ static int isodate7_valid(const unsigned char *);
405
+ static int isBootRecord(const unsigned char *);
406
+ static int isVolumePartition(struct iso9660 *, const unsigned char *);
407
+ static int isVDSetTerminator(const unsigned char *);
408
+ static int isJolietSVD(struct iso9660 *, const unsigned char *);
409
+ static int isSVD(const unsigned char *);
410
+ static int isEVD(const unsigned char *);
411
+ static int isPVD(struct iso9660 *, const unsigned char *);
412
+ static int isRootDirectoryRecord(const unsigned char *);
413
+ static int isValid723Integer(const unsigned char *);
414
+ static int isValid733Integer(const unsigned char *);
415
+ static int next_cache_entry(struct archive_read *, struct iso9660 *,
416
+ struct file_info **);
417
+ static int next_entry_seek(struct archive_read *, struct iso9660 *,
418
+ struct file_info **);
419
+ static struct file_info *
420
+ parse_file_info(struct archive_read *a,
421
+ struct file_info *parent, const unsigned char *isodirrec,
422
+ size_t reclen);
423
+ static int parse_rockridge(struct archive_read *a,
424
+ struct file_info *file, const unsigned char *start,
425
+ const unsigned char *end);
426
+ static int register_CE(struct archive_read *a, int32_t location,
427
+ struct file_info *file);
428
+ static int read_CE(struct archive_read *a, struct iso9660 *iso9660);
429
+ static void parse_rockridge_NM1(struct file_info *,
430
+ const unsigned char *, int);
431
+ static void parse_rockridge_SL1(struct file_info *,
432
+ const unsigned char *, int);
433
+ static void parse_rockridge_TF1(struct file_info *,
434
+ const unsigned char *, int);
435
+ static void parse_rockridge_ZF1(struct file_info *,
436
+ const unsigned char *, int);
437
+ static void register_file(struct iso9660 *, struct file_info *);
438
+ static void release_files(struct iso9660 *);
439
+ static inline void re_add_entry(struct iso9660 *, struct file_info *);
440
+ static inline struct file_info * re_get_entry(struct iso9660 *);
441
+ static inline int rede_add_entry(struct file_info *);
442
+ static inline struct file_info * rede_get_entry(struct file_info *);
443
+ static inline void cache_add_entry(struct iso9660 *iso9660,
444
+ struct file_info *file);
445
+ static inline struct file_info *cache_get_entry(struct iso9660 *iso9660);
446
+ static int heap_add_entry(struct archive_read *a, struct heap_queue *heap,
447
+ struct file_info *file, uint64_t key);
448
+ static struct file_info *heap_get_entry(struct heap_queue *heap);
449
+
450
+ #define add_entry(arch, iso9660, file) \
451
+ heap_add_entry(arch, &((iso9660)->pending_files), file, file->offset)
452
+ #define next_entry(iso9660) \
453
+ heap_get_entry(&((iso9660)->pending_files))
454
+
455
+ int
456
+ archive_read_support_format_iso9660(struct archive *_a)
457
+ {
458
+ struct archive_read *a = (struct archive_read *)_a;
459
+ struct iso9660 *iso9660;
460
+ int r;
461
+
462
+ archive_check_magic(_a, ARCHIVE_READ_MAGIC,
463
+ ARCHIVE_STATE_NEW, "archive_read_support_format_iso9660");
464
+
465
+ iso9660 = calloc(1, sizeof(*iso9660));
466
+ if (iso9660 == NULL) {
467
+ archive_set_error(&a->archive, ENOMEM,
468
+ "Can't allocate iso9660 data");
469
+ return (ARCHIVE_FATAL);
470
+ }
471
+ iso9660->magic = ISO9660_MAGIC;
472
+ iso9660->cache_files.first = NULL;
473
+ iso9660->cache_files.last = &(iso9660->cache_files.first);
474
+ iso9660->re_files.first = NULL;
475
+ iso9660->re_files.last = &(iso9660->re_files.first);
476
+ /* Enable to support Joliet extensions by default. */
477
+ iso9660->opt_support_joliet = 1;
478
+ /* Enable to support Rock Ridge extensions by default. */
479
+ iso9660->opt_support_rockridge = 1;
480
+
481
+ r = __archive_read_register_format(a,
482
+ iso9660,
483
+ "iso9660",
484
+ archive_read_format_iso9660_bid,
485
+ archive_read_format_iso9660_options,
486
+ archive_read_format_iso9660_read_header,
487
+ archive_read_format_iso9660_read_data,
488
+ archive_read_format_iso9660_read_data_skip,
489
+ NULL,
490
+ archive_read_format_iso9660_cleanup,
491
+ NULL,
492
+ NULL);
493
+
494
+ if (r != ARCHIVE_OK) {
495
+ free(iso9660);
496
+ return (r);
497
+ }
498
+ return (ARCHIVE_OK);
499
+ }
500
+
501
+
502
+ static int
503
+ archive_read_format_iso9660_bid(struct archive_read *a, int best_bid)
504
+ {
505
+ struct iso9660 *iso9660 = a->format->data;
506
+ ssize_t bytes_read;
507
+ const unsigned char *p;
508
+ int seenTerminator;
509
+
510
+ /* If there's already a better bid than we can ever
511
+ make, don't bother testing. */
512
+ if (best_bid > 48)
513
+ return (-1);
514
+
515
+ /*
516
+ * Skip the first 32k (reserved area) and get the first
517
+ * 8 sectors of the volume descriptor table. Of course,
518
+ * if the I/O layer gives us more, we'll take it.
519
+ */
520
+ #define RESERVED_AREA (SYSTEM_AREA_BLOCK * LOGICAL_BLOCK_SIZE)
521
+ p = __archive_read_ahead(a,
522
+ RESERVED_AREA + 8 * LOGICAL_BLOCK_SIZE,
523
+ &bytes_read);
524
+ if (p == NULL)
525
+ return (-1);
526
+
527
+ /* Skip the reserved area. */
528
+ bytes_read -= RESERVED_AREA;
529
+ p += RESERVED_AREA;
530
+
531
+ /* Check each volume descriptor. */
532
+ seenTerminator = 0;
533
+ for (; bytes_read > LOGICAL_BLOCK_SIZE;
534
+ bytes_read -= LOGICAL_BLOCK_SIZE, p += LOGICAL_BLOCK_SIZE) {
535
+ /* Do not handle undefined Volume Descriptor Type. */
536
+ if (p[0] >= 4 && p[0] <= 254)
537
+ return (0);
538
+ /* Standard Identifier must be "CD001" */
539
+ if (memcmp(p + 1, "CD001", 5) != 0)
540
+ return (0);
541
+ if (isPVD(iso9660, p))
542
+ continue;
543
+ if (!iso9660->joliet.location) {
544
+ if (isJolietSVD(iso9660, p))
545
+ continue;
546
+ }
547
+ if (isBootRecord(p))
548
+ continue;
549
+ if (isEVD(p))
550
+ continue;
551
+ if (isSVD(p))
552
+ continue;
553
+ if (isVolumePartition(iso9660, p))
554
+ continue;
555
+ if (isVDSetTerminator(p)) {
556
+ seenTerminator = 1;
557
+ break;
558
+ }
559
+ return (0);
560
+ }
561
+ /*
562
+ * ISO 9660 format must have Primary Volume Descriptor and
563
+ * Volume Descriptor Set Terminator.
564
+ */
565
+ if (seenTerminator && iso9660->primary.location > 16)
566
+ return (48);
567
+
568
+ /* We didn't find a valid PVD; return a bid of zero. */
569
+ return (0);
570
+ }
571
+
572
+ static int
573
+ archive_read_format_iso9660_options(struct archive_read *a,
574
+ const char *key, const char *val)
575
+ {
576
+ struct iso9660 *iso9660 = a->format->data;
577
+
578
+ if (strcmp(key, "joliet") == 0) {
579
+ if (val == NULL || strcmp(val, "off") == 0 ||
580
+ strcmp(val, "ignore") == 0 ||
581
+ strcmp(val, "disable") == 0 ||
582
+ strcmp(val, "0") == 0)
583
+ iso9660->opt_support_joliet = 0;
584
+ else
585
+ iso9660->opt_support_joliet = 1;
586
+ return (ARCHIVE_OK);
587
+ }
588
+ if (strcmp(key, "rockridge") == 0 ||
589
+ strcmp(key, "Rockridge") == 0) {
590
+ iso9660->opt_support_rockridge = val != NULL;
591
+ return (ARCHIVE_OK);
592
+ }
593
+
594
+ /* Note: The "warn" return is just to inform the options
595
+ * supervisor that we didn't handle it. It will generate
596
+ * a suitable error if no one used this option. */
597
+ return (ARCHIVE_WARN);
598
+ }
599
+
600
+ static int
601
+ isNull(const unsigned char *h, unsigned offset, unsigned bytes)
602
+ {
603
+ if (bytes == 0)
604
+ return (1);
605
+
606
+ /*
607
+ * If the first byte is zero and every byte equals the following
608
+ * byte, the entire range is zero.
609
+ */
610
+ return (h[offset] == 0 &&
611
+ (bytes == 1 ||
612
+ memcmp(h + offset, h + offset + 1, bytes - 1) == 0));
613
+ }
614
+
615
+ static int
616
+ isBootRecord(const unsigned char *h)
617
+ {
618
+
619
+ /* Type of the Volume Descriptor Boot Record must be 0. */
620
+ if (h[0] != 0)
621
+ return (0);
622
+
623
+ /* Volume Descriptor Version must be 1. */
624
+ if (h[6] != 1)
625
+ return (0);
626
+
627
+ return (1);
628
+ }
629
+
630
+ static int
631
+ isVolumePartition(struct iso9660 *iso9660, const unsigned char *h)
632
+ {
633
+ int32_t location;
634
+
635
+ /* Type of the Volume Partition Descriptor must be 3. */
636
+ if (h[0] != 3)
637
+ return (0);
638
+
639
+ /* Volume Descriptor Version must be 1. */
640
+ if (h[6] != 1)
641
+ return (0);
642
+ /* Unused Field */
643
+ if (h[7] != 0)
644
+ return (0);
645
+
646
+ location = archive_le32dec(h + 72);
647
+ if (location <= SYSTEM_AREA_BLOCK ||
648
+ location >= iso9660->volume_block)
649
+ return (0);
650
+ if ((uint32_t)location != archive_be32dec(h + 76))
651
+ return (0);
652
+
653
+ return (1);
654
+ }
655
+
656
+ static int
657
+ isVDSetTerminator(const unsigned char *h)
658
+ {
659
+
660
+ /* Type of the Volume Descriptor Set Terminator must be 255. */
661
+ if (h[0] != 255)
662
+ return (0);
663
+
664
+ /* Volume Descriptor Version must be 1. */
665
+ if (h[6] != 1)
666
+ return (0);
667
+
668
+ /* Reserved field must be 0. */
669
+ if (!isNull(h, 7, 2048-7))
670
+ return (0);
671
+
672
+ return (1);
673
+ }
674
+
675
+ static int
676
+ isJolietSVD(struct iso9660 *iso9660, const unsigned char *h)
677
+ {
678
+ const unsigned char *p;
679
+ ssize_t logical_block_size;
680
+ int32_t volume_block;
681
+
682
+ /* Check if current sector is a kind of Supplementary Volume
683
+ * Descriptor. */
684
+ if (!isSVD(h))
685
+ return (0);
686
+
687
+ /* FIXME: do more validations according to joliet spec. */
688
+
689
+ /* check if this SVD contains joliet extension! */
690
+ p = h + SVD_escape_sequences_offset;
691
+ /* N.B. Joliet spec says p[1] == '\\', but.... */
692
+ if (p[0] == '%' && p[1] == '/') {
693
+ int level = 0;
694
+
695
+ if (p[2] == '@')
696
+ level = 1;
697
+ else if (p[2] == 'C')
698
+ level = 2;
699
+ else if (p[2] == 'E')
700
+ level = 3;
701
+ else /* not joliet */
702
+ return (0);
703
+
704
+ iso9660->seenJoliet = level;
705
+
706
+ } else /* not joliet */
707
+ return (0);
708
+
709
+ logical_block_size =
710
+ archive_le16dec(h + SVD_logical_block_size_offset);
711
+ volume_block = archive_le32dec(h + SVD_volume_space_size_offset);
712
+
713
+ iso9660->logical_block_size = logical_block_size;
714
+ iso9660->volume_block = volume_block;
715
+ iso9660->volume_size = logical_block_size * (uint64_t)volume_block;
716
+ /* Read Root Directory Record in Volume Descriptor. */
717
+ p = h + SVD_root_directory_record_offset;
718
+ iso9660->joliet.location = archive_le32dec(p + DR_extent_offset);
719
+ iso9660->joliet.size = archive_le32dec(p + DR_size_offset);
720
+
721
+ return (48);
722
+ }
723
+
724
+ static int
725
+ isSVD(const unsigned char *h)
726
+ {
727
+ const unsigned char *p;
728
+ ssize_t logical_block_size;
729
+ int32_t volume_block;
730
+ int32_t location;
731
+
732
+
733
+ /* Type 2 means it's a SVD. */
734
+ if (h[SVD_type_offset] != 2)
735
+ return (0);
736
+
737
+ /* Reserved field must be 0. */
738
+ if (!isNull(h, SVD_reserved1_offset, SVD_reserved1_size))
739
+ return (0);
740
+ if (!isNull(h, SVD_reserved2_offset, SVD_reserved2_size))
741
+ return (0);
742
+ if (!isNull(h, SVD_reserved3_offset, SVD_reserved3_size))
743
+ return (0);
744
+
745
+ /* File structure version must be 1 for ISO9660/ECMA119. */
746
+ if (h[SVD_file_structure_version_offset] != 1)
747
+ return (0);
748
+
749
+ logical_block_size =
750
+ archive_le16dec(h + SVD_logical_block_size_offset);
751
+ if (logical_block_size <= 0)
752
+ return (0);
753
+
754
+ volume_block = archive_le32dec(h + SVD_volume_space_size_offset);
755
+ if (volume_block <= SYSTEM_AREA_BLOCK+4)
756
+ return (0);
757
+
758
+ /* Location of Occurrence of Type L Path Table must be
759
+ * available location,
760
+ * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
761
+ location = archive_le32dec(h+SVD_type_L_path_table_offset);
762
+ if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
763
+ return (0);
764
+
765
+ /* The Type M Path Table must be at a valid location (WinISO
766
+ * and probably other programs omit this, so we allow zero)
767
+ *
768
+ * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
769
+ location = archive_be32dec(h+SVD_type_M_path_table_offset);
770
+ if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
771
+ || location >= volume_block)
772
+ return (0);
773
+
774
+ /* Read Root Directory Record in Volume Descriptor. */
775
+ p = h + SVD_root_directory_record_offset;
776
+ if (!isRootDirectoryRecord(p)) {
777
+ return (0);
778
+ }
779
+
780
+ return (48);
781
+ }
782
+
783
+ static int
784
+ isEVD(const unsigned char *h)
785
+ {
786
+ const unsigned char *p;
787
+ ssize_t logical_block_size;
788
+ int32_t volume_block;
789
+ int32_t location;
790
+
791
+
792
+ /* Type of the Enhanced Volume Descriptor must be 2. */
793
+ if (h[PVD_type_offset] != 2)
794
+ return (0);
795
+
796
+ /* EVD version must be 2. */
797
+ if (h[PVD_version_offset] != 2)
798
+ return (0);
799
+
800
+ /* Reserved field must be 0. */
801
+ if (h[PVD_reserved1_offset] != 0)
802
+ return (0);
803
+
804
+ /* Reserved field must be 0. */
805
+ if (!isNull(h, PVD_reserved2_offset, PVD_reserved2_size))
806
+ return (0);
807
+
808
+ /* Reserved field must be 0. */
809
+ if (!isNull(h, PVD_reserved3_offset, PVD_reserved3_size))
810
+ return (0);
811
+
812
+ /* Logical block size must be > 0. */
813
+ /* I've looked at Ecma 119 and can't find any stronger
814
+ * restriction on this field. */
815
+ logical_block_size =
816
+ archive_le16dec(h + PVD_logical_block_size_offset);
817
+ if (logical_block_size <= 0)
818
+ return (0);
819
+
820
+ volume_block =
821
+ archive_le32dec(h + PVD_volume_space_size_offset);
822
+ if (volume_block <= SYSTEM_AREA_BLOCK+4)
823
+ return (0);
824
+
825
+ /* File structure version must be 2 for ISO9660:1999. */
826
+ if (h[PVD_file_structure_version_offset] != 2)
827
+ return (0);
828
+
829
+ /* Location of Occurrence of Type L Path Table must be
830
+ * available location,
831
+ * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
832
+ location = archive_le32dec(h+PVD_type_1_path_table_offset);
833
+ if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
834
+ return (0);
835
+
836
+ /* Location of Occurrence of Type M Path Table must be
837
+ * available location,
838
+ * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
839
+ location = archive_be32dec(h+PVD_type_m_path_table_offset);
840
+ if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
841
+ || location >= volume_block)
842
+ return (0);
843
+
844
+ /* Reserved field must be 0. */
845
+ if (!isNull(h, PVD_reserved4_offset, PVD_reserved4_size))
846
+ return (0);
847
+
848
+ /* Reserved field must be 0. */
849
+ if (!isNull(h, PVD_reserved5_offset, PVD_reserved5_size))
850
+ return (0);
851
+
852
+ /* Read Root Directory Record in Volume Descriptor. */
853
+ p = h + PVD_root_directory_record_offset;
854
+ if (!isRootDirectoryRecord(p)) {
855
+ return (0);
856
+ }
857
+
858
+ return (48);
859
+ }
860
+
861
+ static int
862
+ isPVD(struct iso9660 *iso9660, const unsigned char *h)
863
+ {
864
+ const unsigned char *p;
865
+ ssize_t logical_block_size;
866
+ int32_t volume_block;
867
+ int32_t location;
868
+ int i;
869
+
870
+ /* Type of the Primary Volume Descriptor must be 1. */
871
+ if (h[PVD_type_offset] != 1)
872
+ return (0);
873
+
874
+ /* PVD version must be 1. */
875
+ if (h[PVD_version_offset] != 1)
876
+ return (0);
877
+
878
+ /* Reserved field must be 0. */
879
+ if (h[PVD_reserved1_offset] != 0)
880
+ return (0);
881
+
882
+ /* Reserved field must be 0. */
883
+ if (!isNull(h, PVD_reserved2_offset, PVD_reserved2_size))
884
+ return (0);
885
+
886
+ /* Volume space size must be encoded according to 7.3.3 */
887
+ if (!isValid733Integer(h + PVD_volume_space_size_offset)) {
888
+ return (0);
889
+ }
890
+ volume_block = archive_le32dec(h + PVD_volume_space_size_offset);
891
+ if (volume_block <= SYSTEM_AREA_BLOCK+4)
892
+ return (0);
893
+
894
+ /* Reserved field must be 0. */
895
+ if (!isNull(h, PVD_reserved3_offset, PVD_reserved3_size))
896
+ return (0);
897
+
898
+ /* Volume set size must be encoded according to 7.2.3 */
899
+ if (!isValid723Integer(h + PVD_volume_set_size_offset)) {
900
+ return (0);
901
+ }
902
+
903
+ /* Volume sequence number must be encoded according to 7.2.3 */
904
+ if (!isValid723Integer(h + PVD_volume_sequence_number_offset)) {
905
+ return (0);
906
+ }
907
+
908
+ /* Logical block size must be > 0. */
909
+ /* I've looked at Ecma 119 and can't find any stronger
910
+ * restriction on this field. */
911
+ if (!isValid723Integer(h + PVD_logical_block_size_offset)) {
912
+ return (0);
913
+ }
914
+ logical_block_size =
915
+ archive_le16dec(h + PVD_logical_block_size_offset);
916
+ if (logical_block_size <= 0)
917
+ return (0);
918
+
919
+ /* Path Table size must be encoded according to 7.3.3 */
920
+ if (!isValid733Integer(h + PVD_path_table_size_offset)) {
921
+ return (0);
922
+ }
923
+
924
+ /* File structure version must be 1 for ISO9660/ECMA119. */
925
+ if (h[PVD_file_structure_version_offset] != 1)
926
+ return (0);
927
+
928
+ /* Location of Occurrence of Type L Path Table must be
929
+ * available location,
930
+ * > SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
931
+ location = archive_le32dec(h+PVD_type_1_path_table_offset);
932
+ if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
933
+ return (0);
934
+
935
+ /* The Type M Path Table must also be at a valid location
936
+ * (although ECMA 119 requires a Type M Path Table, WinISO and
937
+ * probably other programs omit it, so we permit a zero here)
938
+ *
939
+ * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
940
+ location = archive_be32dec(h+PVD_type_m_path_table_offset);
941
+ if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
942
+ || location >= volume_block)
943
+ return (0);
944
+
945
+ /* Reserved field must be 0. */
946
+ /* But accept NetBSD/FreeBSD "makefs" images with 0x20 here. */
947
+ for (i = 0; i < PVD_reserved4_size; ++i)
948
+ if (h[PVD_reserved4_offset + i] != 0
949
+ && h[PVD_reserved4_offset + i] != 0x20)
950
+ return (0);
951
+
952
+ /* Reserved field must be 0. */
953
+ if (!isNull(h, PVD_reserved5_offset, PVD_reserved5_size))
954
+ return (0);
955
+
956
+ /* XXX TODO: Check other values for sanity; reject more
957
+ * malformed PVDs. XXX */
958
+
959
+ /* Read Root Directory Record in Volume Descriptor. */
960
+ p = h + PVD_root_directory_record_offset;
961
+ if (!isRootDirectoryRecord(p)) {
962
+ return (0);
963
+ }
964
+
965
+ if (!iso9660->primary.location) {
966
+ iso9660->logical_block_size = logical_block_size;
967
+ iso9660->volume_block = volume_block;
968
+ iso9660->volume_size =
969
+ logical_block_size * (uint64_t)volume_block;
970
+ iso9660->primary.location =
971
+ archive_le32dec(p + DR_extent_offset);
972
+ iso9660->primary.size = archive_le32dec(p + DR_size_offset);
973
+ }
974
+
975
+ return (48);
976
+ }
977
+
978
+ static int
979
+ isRootDirectoryRecord(const unsigned char *p) {
980
+ int flags;
981
+
982
+ /* ECMA119/ISO9660 requires that the root directory record be _exactly_ 34 bytes.
983
+ * However, we've seen images that have root directory records up to 68 bytes. */
984
+ if (p[DR_length_offset] < 34 || p[DR_length_offset] > 68) {
985
+ return (0);
986
+ }
987
+
988
+ /* The root directory location must be a 7.3.3 32-bit integer. */
989
+ if (!isValid733Integer(p + DR_extent_offset)) {
990
+ return (0);
991
+ }
992
+
993
+ /* The root directory size must be a 7.3.3 integer. */
994
+ if (!isValid733Integer(p + DR_size_offset)) {
995
+ return (0);
996
+ }
997
+
998
+ /* According to the standard, certain bits must be one or zero:
999
+ * Bit 1: must be 1 (this is a directory)
1000
+ * Bit 2: must be 0 (not an associated file)
1001
+ * Bit 3: must be 0 (doesn't use extended attribute record)
1002
+ * Bit 7: must be 0 (final directory record for this file)
1003
+ */
1004
+ flags = p[DR_flags_offset];
1005
+ if ((flags & 0x8E) != 0x02) {
1006
+ return (0);
1007
+ }
1008
+
1009
+ /* Volume sequence number must be a 7.2.3 integer. */
1010
+ if (!isValid723Integer(p + DR_volume_sequence_number_offset)) {
1011
+ return (0);
1012
+ }
1013
+
1014
+ /* Root directory name is a single zero byte... */
1015
+ if (p[DR_name_len_offset] != 1 || p[DR_name_offset] != 0) {
1016
+ return (0);
1017
+ }
1018
+
1019
+ /* Nothing looked wrong, so let's accept it. */
1020
+ return (1);
1021
+ }
1022
+
1023
+ static int
1024
+ read_children(struct archive_read *a, struct file_info *parent)
1025
+ {
1026
+ struct iso9660 *iso9660 = a->format->data;
1027
+ const unsigned char *b, *p;
1028
+ struct file_info *multi;
1029
+ size_t step, skip_size;
1030
+
1031
+ /* flush any remaining bytes from the last round to ensure
1032
+ * we're positioned */
1033
+ if (iso9660->entry_bytes_unconsumed) {
1034
+ __archive_read_consume(a, iso9660->entry_bytes_unconsumed);
1035
+ iso9660->entry_bytes_unconsumed = 0;
1036
+ }
1037
+ if (iso9660->current_position > parent->offset) {
1038
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1039
+ "Ignoring out-of-order directory (%s) %jd > %jd",
1040
+ parent->name.s,
1041
+ (intmax_t)iso9660->current_position,
1042
+ (intmax_t)parent->offset);
1043
+ return (ARCHIVE_WARN);
1044
+ }
1045
+ if (parent->offset + parent->size > iso9660->volume_size) {
1046
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1047
+ "Directory is beyond end-of-media: %s",
1048
+ parent->name.s);
1049
+ return (ARCHIVE_WARN);
1050
+ }
1051
+ if (iso9660->current_position < parent->offset) {
1052
+ int64_t skipsize;
1053
+
1054
+ skipsize = parent->offset - iso9660->current_position;
1055
+ skipsize = __archive_read_consume(a, skipsize);
1056
+ if (skipsize < 0)
1057
+ return ((int)skipsize);
1058
+ iso9660->current_position = parent->offset;
1059
+ }
1060
+
1061
+ step = (size_t)(((parent->size + iso9660->logical_block_size -1) /
1062
+ iso9660->logical_block_size) * iso9660->logical_block_size);
1063
+ b = __archive_read_ahead(a, step, NULL);
1064
+ if (b == NULL) {
1065
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1066
+ "Failed to read full block when scanning "
1067
+ "ISO9660 directory list");
1068
+ return (ARCHIVE_FATAL);
1069
+ }
1070
+ iso9660->current_position += step;
1071
+ multi = NULL;
1072
+ skip_size = step;
1073
+ while (step) {
1074
+ p = b;
1075
+ b += iso9660->logical_block_size;
1076
+ step -= iso9660->logical_block_size;
1077
+ for (; p < b && b - p > DR_name_offset && *p != 0 && *p <= b - p;
1078
+ p += *p) {
1079
+ struct file_info *child;
1080
+
1081
+ /* N.B.: these special directory identifiers
1082
+ * are 8 bit "values" even on a
1083
+ * Joliet CD with UCS-2 (16bit) encoding.
1084
+ */
1085
+
1086
+ /* Skip '.' entry. */
1087
+ if (*(p + DR_name_len_offset) == 1
1088
+ && *(p + DR_name_offset) == '\0')
1089
+ continue;
1090
+ /* Skip '..' entry. */
1091
+ if (*(p + DR_name_len_offset) == 1
1092
+ && *(p + DR_name_offset) == '\001')
1093
+ continue;
1094
+ child = parse_file_info(a, parent, p, b - p);
1095
+ if (child == NULL) {
1096
+ __archive_read_consume(a, skip_size);
1097
+ return (ARCHIVE_FATAL);
1098
+ }
1099
+ if (child->cl_offset == 0 &&
1100
+ (child->multi_extent || multi != NULL)) {
1101
+ struct content *con;
1102
+
1103
+ if (multi == NULL) {
1104
+ multi = child;
1105
+ multi->contents.first = NULL;
1106
+ multi->contents.last =
1107
+ &(multi->contents.first);
1108
+ }
1109
+ con = malloc(sizeof(struct content));
1110
+ if (con == NULL) {
1111
+ archive_set_error(
1112
+ &a->archive, ENOMEM,
1113
+ "No memory for multi extent");
1114
+ __archive_read_consume(a, skip_size);
1115
+ return (ARCHIVE_FATAL);
1116
+ }
1117
+ con->offset = child->offset;
1118
+ con->size = child->size;
1119
+ con->next = NULL;
1120
+ *multi->contents.last = con;
1121
+ multi->contents.last = &(con->next);
1122
+ if (multi == child) {
1123
+ if (add_entry(a, iso9660, child)
1124
+ != ARCHIVE_OK)
1125
+ return (ARCHIVE_FATAL);
1126
+ } else {
1127
+ multi->size += child->size;
1128
+ if (!child->multi_extent)
1129
+ multi = NULL;
1130
+ }
1131
+ } else
1132
+ if (add_entry(a, iso9660, child) != ARCHIVE_OK)
1133
+ return (ARCHIVE_FATAL);
1134
+ }
1135
+ }
1136
+
1137
+ __archive_read_consume(a, skip_size);
1138
+
1139
+ /* Read data which recorded by RRIP "CE" extension. */
1140
+ if (read_CE(a, iso9660) != ARCHIVE_OK)
1141
+ return (ARCHIVE_FATAL);
1142
+
1143
+ return (ARCHIVE_OK);
1144
+ }
1145
+
1146
+ static int
1147
+ choose_volume(struct archive_read *a, struct iso9660 *iso9660)
1148
+ {
1149
+ struct file_info *file;
1150
+ int64_t skipsize;
1151
+ struct vd *vd;
1152
+ const void *block;
1153
+ char seenJoliet;
1154
+
1155
+ vd = &(iso9660->primary);
1156
+ if (!iso9660->opt_support_joliet)
1157
+ iso9660->seenJoliet = 0;
1158
+ if (iso9660->seenJoliet &&
1159
+ vd->location > iso9660->joliet.location)
1160
+ /* This condition is unlikely; by way of caution. */
1161
+ vd = &(iso9660->joliet);
1162
+
1163
+ skipsize = LOGICAL_BLOCK_SIZE * (int64_t)vd->location;
1164
+ skipsize = __archive_read_consume(a, skipsize);
1165
+ if (skipsize < 0)
1166
+ return ((int)skipsize);
1167
+ iso9660->current_position = skipsize;
1168
+
1169
+ block = __archive_read_ahead(a, vd->size, NULL);
1170
+ if (block == NULL) {
1171
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1172
+ "Failed to read full block when scanning "
1173
+ "ISO9660 directory list");
1174
+ return (ARCHIVE_FATAL);
1175
+ }
1176
+
1177
+ /*
1178
+ * While reading Root Directory, flag seenJoliet must be zero to
1179
+ * avoid converting special name 0x00(Current Directory) and
1180
+ * next byte to UCS2.
1181
+ */
1182
+ seenJoliet = iso9660->seenJoliet;/* Save flag. */
1183
+ iso9660->seenJoliet = 0;
1184
+ file = parse_file_info(a, NULL, block, vd->size);
1185
+ if (file == NULL)
1186
+ return (ARCHIVE_FATAL);
1187
+ iso9660->seenJoliet = seenJoliet;
1188
+
1189
+ /*
1190
+ * If the iso image has both RockRidge and Joliet, we preferentially
1191
+ * use RockRidge Extensions rather than Joliet ones.
1192
+ */
1193
+ if (vd == &(iso9660->primary) && iso9660->seenRockridge
1194
+ && iso9660->seenJoliet)
1195
+ iso9660->seenJoliet = 0;
1196
+
1197
+ if (vd == &(iso9660->primary) && !iso9660->seenRockridge
1198
+ && iso9660->seenJoliet) {
1199
+ /* Switch reading data from primary to joliet. */
1200
+ vd = &(iso9660->joliet);
1201
+ skipsize = LOGICAL_BLOCK_SIZE * (int64_t)vd->location;
1202
+ skipsize -= iso9660->current_position;
1203
+ skipsize = __archive_read_consume(a, skipsize);
1204
+ if (skipsize < 0)
1205
+ return ((int)skipsize);
1206
+ iso9660->current_position += skipsize;
1207
+
1208
+ block = __archive_read_ahead(a, vd->size, NULL);
1209
+ if (block == NULL) {
1210
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1211
+ "Failed to read full block when scanning "
1212
+ "ISO9660 directory list");
1213
+ return (ARCHIVE_FATAL);
1214
+ }
1215
+ iso9660->seenJoliet = 0;
1216
+ file = parse_file_info(a, NULL, block, vd->size);
1217
+ if (file == NULL)
1218
+ return (ARCHIVE_FATAL);
1219
+ iso9660->seenJoliet = seenJoliet;
1220
+ }
1221
+
1222
+ /* Store the root directory in the pending list. */
1223
+ if (add_entry(a, iso9660, file) != ARCHIVE_OK)
1224
+ return (ARCHIVE_FATAL);
1225
+ if (iso9660->seenRockridge) {
1226
+ a->archive.archive_format = ARCHIVE_FORMAT_ISO9660_ROCKRIDGE;
1227
+ a->archive.archive_format_name =
1228
+ "ISO9660 with Rockridge extensions";
1229
+ }
1230
+
1231
+ return (ARCHIVE_OK);
1232
+ }
1233
+
1234
+ static int
1235
+ archive_read_format_iso9660_read_header(struct archive_read *a,
1236
+ struct archive_entry *entry)
1237
+ {
1238
+ struct iso9660 *iso9660 = a->format->data;
1239
+ struct file_info *file;
1240
+ int r, rd_r = ARCHIVE_OK;
1241
+
1242
+ if (!a->archive.archive_format) {
1243
+ a->archive.archive_format = ARCHIVE_FORMAT_ISO9660;
1244
+ a->archive.archive_format_name = "ISO9660";
1245
+ }
1246
+
1247
+ if (iso9660->current_position == 0) {
1248
+ r = choose_volume(a, iso9660);
1249
+ if (r != ARCHIVE_OK)
1250
+ return (r);
1251
+ }
1252
+
1253
+ file = NULL;/* Eliminate a warning. */
1254
+ /* Get the next entry that appears after the current offset. */
1255
+ r = next_entry_seek(a, iso9660, &file);
1256
+ if (r != ARCHIVE_OK)
1257
+ return (r);
1258
+
1259
+ if (iso9660->seenJoliet) {
1260
+ /*
1261
+ * Convert UTF-16BE of a filename to local locale MBS
1262
+ * and store the result into a filename field.
1263
+ */
1264
+ if (iso9660->sconv_utf16be == NULL) {
1265
+ iso9660->sconv_utf16be =
1266
+ archive_string_conversion_from_charset(
1267
+ &(a->archive), "UTF-16BE", 1);
1268
+ if (iso9660->sconv_utf16be == NULL)
1269
+ /* Couldn't allocate memory */
1270
+ return (ARCHIVE_FATAL);
1271
+ }
1272
+ if (iso9660->utf16be_path == NULL) {
1273
+ iso9660->utf16be_path = malloc(UTF16_NAME_MAX);
1274
+ if (iso9660->utf16be_path == NULL) {
1275
+ archive_set_error(&a->archive, ENOMEM,
1276
+ "No memory");
1277
+ return (ARCHIVE_FATAL);
1278
+ }
1279
+ }
1280
+ if (iso9660->utf16be_previous_path == NULL) {
1281
+ iso9660->utf16be_previous_path = calloc(1, UTF16_NAME_MAX);
1282
+ if (iso9660->utf16be_previous_path == NULL) {
1283
+ archive_set_error(&a->archive, ENOMEM,
1284
+ "No memory");
1285
+ return (ARCHIVE_FATAL);
1286
+ }
1287
+ }
1288
+
1289
+ iso9660->utf16be_path_len = 0;
1290
+ if (build_pathname_utf16be(iso9660->utf16be_path,
1291
+ UTF16_NAME_MAX, &(iso9660->utf16be_path_len), file) != 0) {
1292
+ archive_set_error(&a->archive,
1293
+ ARCHIVE_ERRNO_FILE_FORMAT,
1294
+ "Pathname is too long");
1295
+ return (ARCHIVE_FATAL);
1296
+ }
1297
+
1298
+ r = archive_entry_copy_pathname_l(entry,
1299
+ (const char *)iso9660->utf16be_path,
1300
+ iso9660->utf16be_path_len,
1301
+ iso9660->sconv_utf16be);
1302
+ if (r != 0) {
1303
+ if (errno == ENOMEM) {
1304
+ archive_set_error(&a->archive, ENOMEM,
1305
+ "No memory for Pathname");
1306
+ return (ARCHIVE_FATAL);
1307
+ }
1308
+ archive_set_error(&a->archive,
1309
+ ARCHIVE_ERRNO_FILE_FORMAT,
1310
+ "Pathname cannot be converted "
1311
+ "from %s to current locale",
1312
+ archive_string_conversion_charset_name(
1313
+ iso9660->sconv_utf16be));
1314
+
1315
+ rd_r = ARCHIVE_WARN;
1316
+ }
1317
+ } else {
1318
+ const char *path = build_pathname(&iso9660->pathname, file, 0);
1319
+ if (path == NULL) {
1320
+ archive_set_error(&a->archive,
1321
+ ARCHIVE_ERRNO_FILE_FORMAT,
1322
+ "Pathname is too long");
1323
+ return (ARCHIVE_FATAL);
1324
+ } else {
1325
+ archive_string_empty(&iso9660->pathname);
1326
+ archive_entry_set_pathname(entry, path);
1327
+ }
1328
+ }
1329
+
1330
+ iso9660->entry_bytes_remaining = file->size;
1331
+ /* Offset for sparse-file-aware clients. */
1332
+ iso9660->entry_sparse_offset = 0;
1333
+
1334
+ if (file->offset + file->size > iso9660->volume_size) {
1335
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1336
+ "File is beyond end-of-media: %s",
1337
+ archive_entry_pathname(entry));
1338
+ iso9660->entry_bytes_remaining = 0;
1339
+ return (ARCHIVE_WARN);
1340
+ }
1341
+
1342
+ /* Set up the entry structure with information about this entry. */
1343
+ archive_entry_set_mode(entry, file->mode);
1344
+ archive_entry_set_uid(entry, file->uid);
1345
+ archive_entry_set_gid(entry, file->gid);
1346
+ archive_entry_set_nlink(entry, file->nlinks);
1347
+ if ((file->time_is_set & BIRTHTIME_IS_SET))
1348
+ archive_entry_set_birthtime(entry, file->birthtime, 0);
1349
+ else
1350
+ archive_entry_unset_birthtime(entry);
1351
+ if ((file->time_is_set & MTIME_IS_SET))
1352
+ archive_entry_set_mtime(entry, file->mtime, 0);
1353
+ else
1354
+ archive_entry_unset_mtime(entry);
1355
+ if ((file->time_is_set & CTIME_IS_SET))
1356
+ archive_entry_set_ctime(entry, file->ctime, 0);
1357
+ else
1358
+ archive_entry_unset_ctime(entry);
1359
+ if ((file->time_is_set & ATIME_IS_SET))
1360
+ archive_entry_set_atime(entry, file->atime, 0);
1361
+ else
1362
+ archive_entry_unset_atime(entry);
1363
+ /* N.B.: Rock Ridge supports 64-bit device numbers. */
1364
+ archive_entry_set_rdev(entry, (dev_t)file->rdev);
1365
+ archive_entry_set_size(entry, iso9660->entry_bytes_remaining);
1366
+ if (file->symlink.s != NULL)
1367
+ archive_entry_copy_symlink(entry, file->symlink.s);
1368
+
1369
+ /* Note: If the input isn't seekable, we can't rewind to
1370
+ * return the same body again, so if the next entry refers to
1371
+ * the same data, we have to return it as a hardlink to the
1372
+ * original entry. */
1373
+ if (file->number != -1 &&
1374
+ file->number == iso9660->previous_number) {
1375
+ if (iso9660->seenJoliet) {
1376
+ r = archive_entry_copy_hardlink_l(entry,
1377
+ (const char *)iso9660->utf16be_previous_path,
1378
+ iso9660->utf16be_previous_path_len,
1379
+ iso9660->sconv_utf16be);
1380
+ if (r != 0) {
1381
+ if (errno == ENOMEM) {
1382
+ archive_set_error(&a->archive, ENOMEM,
1383
+ "No memory for Linkname");
1384
+ return (ARCHIVE_FATAL);
1385
+ }
1386
+ archive_set_error(&a->archive,
1387
+ ARCHIVE_ERRNO_FILE_FORMAT,
1388
+ "Linkname cannot be converted "
1389
+ "from %s to current locale",
1390
+ archive_string_conversion_charset_name(
1391
+ iso9660->sconv_utf16be));
1392
+ rd_r = ARCHIVE_WARN;
1393
+ }
1394
+ } else
1395
+ archive_entry_set_hardlink(entry,
1396
+ iso9660->previous_pathname.s);
1397
+ archive_entry_unset_size(entry);
1398
+ iso9660->entry_bytes_remaining = 0;
1399
+ return (rd_r);
1400
+ }
1401
+
1402
+ if ((file->mode & AE_IFMT) != AE_IFDIR &&
1403
+ file->offset < iso9660->current_position) {
1404
+ int64_t r64;
1405
+
1406
+ r64 = __archive_read_seek(a, file->offset, SEEK_SET);
1407
+ if (r64 != (int64_t)file->offset) {
1408
+ /* We can't seek backwards to extract it, so issue
1409
+ * a warning. Note that this can only happen if
1410
+ * this entry was added to the heap after we passed
1411
+ * this offset, that is, only if the directory
1412
+ * mentioning this entry is later than the body of
1413
+ * the entry. Such layouts are very unusual; most
1414
+ * ISO9660 writers lay out and record all directory
1415
+ * information first, then store all file bodies. */
1416
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1417
+ "Ignoring out-of-order file @%jx (%s) %jd < %jd",
1418
+ (uintmax_t)file->number,
1419
+ iso9660->pathname.s,
1420
+ (intmax_t)file->offset,
1421
+ (intmax_t)iso9660->current_position);
1422
+ iso9660->entry_bytes_remaining = 0;
1423
+ return (ARCHIVE_WARN);
1424
+ }
1425
+ iso9660->current_position = (uint64_t)r64;
1426
+ }
1427
+
1428
+ /* Initialize zisofs variables. */
1429
+ iso9660->entry_zisofs.pz = file->pz;
1430
+ if (file->pz) {
1431
+ #ifdef HAVE_ZLIB_H
1432
+ struct zisofs *zisofs;
1433
+
1434
+ zisofs = &iso9660->entry_zisofs;
1435
+ zisofs->initialized = 0;
1436
+ zisofs->pz_log2_bs = file->pz_log2_bs;
1437
+ zisofs->pz_uncompressed_size = file->pz_uncompressed_size;
1438
+ zisofs->pz_offset = 0;
1439
+ zisofs->header_avail = 0;
1440
+ zisofs->header_passed = 0;
1441
+ zisofs->block_pointers_avail = 0;
1442
+ #endif
1443
+ archive_entry_set_size(entry, file->pz_uncompressed_size);
1444
+ }
1445
+
1446
+ iso9660->previous_number = file->number;
1447
+ if (iso9660->seenJoliet) {
1448
+ memcpy(iso9660->utf16be_previous_path, iso9660->utf16be_path,
1449
+ iso9660->utf16be_path_len);
1450
+ iso9660->utf16be_previous_path_len = iso9660->utf16be_path_len;
1451
+ } else
1452
+ archive_strcpy(
1453
+ &iso9660->previous_pathname, iso9660->pathname.s);
1454
+
1455
+ /* Reset entry_bytes_remaining if the file is multi extent. */
1456
+ iso9660->entry_content = file->contents.first;
1457
+ if (iso9660->entry_content != NULL)
1458
+ iso9660->entry_bytes_remaining = iso9660->entry_content->size;
1459
+
1460
+ if (archive_entry_filetype(entry) == AE_IFDIR) {
1461
+ /* Overwrite nlinks by proper link number which is
1462
+ * calculated from number of sub directories. */
1463
+ archive_entry_set_nlink(entry, 2 + file->subdirs);
1464
+ /* Directory data has been read completely. */
1465
+ iso9660->entry_bytes_remaining = 0;
1466
+ }
1467
+
1468
+ if (rd_r != ARCHIVE_OK)
1469
+ return (rd_r);
1470
+ return (ARCHIVE_OK);
1471
+ }
1472
+
1473
+ static int
1474
+ archive_read_format_iso9660_read_data_skip(struct archive_read *a)
1475
+ {
1476
+ /* Because read_next_header always does an explicit skip
1477
+ * to the next entry, we don't need to do anything here. */
1478
+ (void)a; /* UNUSED */
1479
+ return (ARCHIVE_OK);
1480
+ }
1481
+
1482
+ #ifdef HAVE_ZLIB_H
1483
+
1484
+ static int
1485
+ zisofs_read_data(struct archive_read *a,
1486
+ const void **buff, size_t *size, int64_t *offset)
1487
+ {
1488
+ struct iso9660 *iso9660 = a->format->data;
1489
+ struct zisofs *zisofs;
1490
+ const unsigned char *p;
1491
+ size_t avail;
1492
+ ssize_t bytes_read;
1493
+ size_t uncompressed_size;
1494
+ int r;
1495
+
1496
+ zisofs = &iso9660->entry_zisofs;
1497
+
1498
+ p = __archive_read_ahead(a, 1, &bytes_read);
1499
+ if (bytes_read <= 0) {
1500
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1501
+ "Truncated zisofs file body");
1502
+ return (ARCHIVE_FATAL);
1503
+ }
1504
+ if (bytes_read > iso9660->entry_bytes_remaining)
1505
+ bytes_read = (ssize_t)iso9660->entry_bytes_remaining;
1506
+ avail = bytes_read;
1507
+ uncompressed_size = 0;
1508
+
1509
+ if (!zisofs->initialized) {
1510
+ size_t ceil, xsize;
1511
+
1512
+ /* Allocate block pointers buffer. */
1513
+ ceil = (size_t)((zisofs->pz_uncompressed_size +
1514
+ (((int64_t)1) << zisofs->pz_log2_bs) - 1)
1515
+ >> zisofs->pz_log2_bs);
1516
+ xsize = (ceil + 1) * 4;
1517
+ if (zisofs->block_pointers_alloc < xsize) {
1518
+ size_t alloc;
1519
+
1520
+ if (zisofs->block_pointers != NULL)
1521
+ free(zisofs->block_pointers);
1522
+ alloc = ((xsize >> 10) + 1) << 10;
1523
+ zisofs->block_pointers = malloc(alloc);
1524
+ if (zisofs->block_pointers == NULL) {
1525
+ archive_set_error(&a->archive, ENOMEM,
1526
+ "No memory for zisofs decompression");
1527
+ return (ARCHIVE_FATAL);
1528
+ }
1529
+ zisofs->block_pointers_alloc = alloc;
1530
+ }
1531
+ zisofs->block_pointers_size = xsize;
1532
+
1533
+ /* Allocate uncompressed data buffer. */
1534
+ xsize = (size_t)1UL << zisofs->pz_log2_bs;
1535
+ if (zisofs->uncompressed_buffer_size < xsize) {
1536
+ if (zisofs->uncompressed_buffer != NULL)
1537
+ free(zisofs->uncompressed_buffer);
1538
+ zisofs->uncompressed_buffer = malloc(xsize);
1539
+ if (zisofs->uncompressed_buffer == NULL) {
1540
+ archive_set_error(&a->archive, ENOMEM,
1541
+ "No memory for zisofs decompression");
1542
+ return (ARCHIVE_FATAL);
1543
+ }
1544
+ }
1545
+ zisofs->uncompressed_buffer_size = xsize;
1546
+
1547
+ /*
1548
+ * Read the file header, and check the magic code of zisofs.
1549
+ */
1550
+ if (zisofs->header_avail < sizeof(zisofs->header)) {
1551
+ xsize = sizeof(zisofs->header) - zisofs->header_avail;
1552
+ if (avail < xsize)
1553
+ xsize = avail;
1554
+ memcpy(zisofs->header + zisofs->header_avail, p, xsize);
1555
+ zisofs->header_avail += xsize;
1556
+ avail -= xsize;
1557
+ p += xsize;
1558
+ }
1559
+ if (!zisofs->header_passed &&
1560
+ zisofs->header_avail == sizeof(zisofs->header)) {
1561
+ int err = 0;
1562
+
1563
+ if (memcmp(zisofs->header, zisofs_magic,
1564
+ sizeof(zisofs_magic)) != 0)
1565
+ err = 1;
1566
+ if (archive_le32dec(zisofs->header + 8)
1567
+ != zisofs->pz_uncompressed_size)
1568
+ err = 1;
1569
+ if (zisofs->header[12] != 4)
1570
+ err = 1;
1571
+ if (zisofs->header[13] != zisofs->pz_log2_bs)
1572
+ err = 1;
1573
+ if (err) {
1574
+ archive_set_error(&a->archive,
1575
+ ARCHIVE_ERRNO_FILE_FORMAT,
1576
+ "Illegal zisofs file body");
1577
+ return (ARCHIVE_FATAL);
1578
+ }
1579
+ zisofs->header_passed = 1;
1580
+ }
1581
+ /*
1582
+ * Read block pointers.
1583
+ */
1584
+ if (zisofs->header_passed &&
1585
+ zisofs->block_pointers_avail < zisofs->block_pointers_size) {
1586
+ xsize = zisofs->block_pointers_size
1587
+ - zisofs->block_pointers_avail;
1588
+ if (avail < xsize)
1589
+ xsize = avail;
1590
+ memcpy(zisofs->block_pointers
1591
+ + zisofs->block_pointers_avail, p, xsize);
1592
+ zisofs->block_pointers_avail += xsize;
1593
+ avail -= xsize;
1594
+ p += xsize;
1595
+ if (zisofs->block_pointers_avail
1596
+ == zisofs->block_pointers_size) {
1597
+ /* We've got all block pointers and initialize
1598
+ * related variables. */
1599
+ zisofs->block_off = 0;
1600
+ zisofs->block_avail = 0;
1601
+ /* Complete a initialization */
1602
+ zisofs->initialized = 1;
1603
+ }
1604
+ }
1605
+
1606
+ if (!zisofs->initialized)
1607
+ goto next_data; /* We need more data. */
1608
+ }
1609
+
1610
+ /*
1611
+ * Get block offsets from block pointers.
1612
+ */
1613
+ if (zisofs->block_avail == 0) {
1614
+ uint32_t bst, bed;
1615
+
1616
+ if (zisofs->block_off + 4 >= zisofs->block_pointers_size) {
1617
+ /* There isn't a pair of offsets. */
1618
+ archive_set_error(&a->archive,
1619
+ ARCHIVE_ERRNO_FILE_FORMAT,
1620
+ "Illegal zisofs block pointers");
1621
+ return (ARCHIVE_FATAL);
1622
+ }
1623
+ bst = archive_le32dec(
1624
+ zisofs->block_pointers + zisofs->block_off);
1625
+ if (bst != zisofs->pz_offset + (bytes_read - avail)) {
1626
+ /* TODO: Should we seek offset of current file
1627
+ * by bst ? */
1628
+ archive_set_error(&a->archive,
1629
+ ARCHIVE_ERRNO_FILE_FORMAT,
1630
+ "Illegal zisofs block pointers(cannot seek)");
1631
+ return (ARCHIVE_FATAL);
1632
+ }
1633
+ bed = archive_le32dec(
1634
+ zisofs->block_pointers + zisofs->block_off + 4);
1635
+ if (bed < bst) {
1636
+ archive_set_error(&a->archive,
1637
+ ARCHIVE_ERRNO_FILE_FORMAT,
1638
+ "Illegal zisofs block pointers");
1639
+ return (ARCHIVE_FATAL);
1640
+ }
1641
+ zisofs->block_avail = bed - bst;
1642
+ zisofs->block_off += 4;
1643
+
1644
+ /* Initialize compression library for new block. */
1645
+ if (zisofs->stream_valid)
1646
+ r = inflateReset(&zisofs->stream);
1647
+ else
1648
+ r = inflateInit(&zisofs->stream);
1649
+ if (r != Z_OK) {
1650
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1651
+ "Can't initialize zisofs decompression");
1652
+ return (ARCHIVE_FATAL);
1653
+ }
1654
+ zisofs->stream_valid = 1;
1655
+ zisofs->stream.total_in = 0;
1656
+ zisofs->stream.total_out = 0;
1657
+ }
1658
+
1659
+ /*
1660
+ * Make uncompressed data.
1661
+ */
1662
+ if (zisofs->block_avail == 0) {
1663
+ memset(zisofs->uncompressed_buffer, 0,
1664
+ zisofs->uncompressed_buffer_size);
1665
+ uncompressed_size = zisofs->uncompressed_buffer_size;
1666
+ } else {
1667
+ zisofs->stream.next_in = (Bytef *)(uintptr_t)(const void *)p;
1668
+ if (avail > zisofs->block_avail)
1669
+ zisofs->stream.avail_in = zisofs->block_avail;
1670
+ else
1671
+ zisofs->stream.avail_in = (uInt)avail;
1672
+ zisofs->stream.next_out = zisofs->uncompressed_buffer;
1673
+ zisofs->stream.avail_out =
1674
+ (uInt)zisofs->uncompressed_buffer_size;
1675
+
1676
+ r = inflate(&zisofs->stream, 0);
1677
+ switch (r) {
1678
+ case Z_OK: /* Decompressor made some progress.*/
1679
+ case Z_STREAM_END: /* Found end of stream. */
1680
+ break;
1681
+ default:
1682
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1683
+ "zisofs decompression failed (%d)", r);
1684
+ return (ARCHIVE_FATAL);
1685
+ }
1686
+ uncompressed_size =
1687
+ zisofs->uncompressed_buffer_size - zisofs->stream.avail_out;
1688
+ avail -= zisofs->stream.next_in - p;
1689
+ zisofs->block_avail -= (uint32_t)(zisofs->stream.next_in - p);
1690
+ }
1691
+ next_data:
1692
+ bytes_read -= avail;
1693
+ *buff = zisofs->uncompressed_buffer;
1694
+ *size = uncompressed_size;
1695
+ *offset = iso9660->entry_sparse_offset;
1696
+ iso9660->entry_sparse_offset += uncompressed_size;
1697
+ iso9660->entry_bytes_remaining -= bytes_read;
1698
+ iso9660->current_position += bytes_read;
1699
+ zisofs->pz_offset += (uint32_t)bytes_read;
1700
+ iso9660->entry_bytes_unconsumed += bytes_read;
1701
+
1702
+ return (ARCHIVE_OK);
1703
+ }
1704
+
1705
+ #else /* HAVE_ZLIB_H */
1706
+
1707
+ static int
1708
+ zisofs_read_data(struct archive_read *a,
1709
+ const void **buff, size_t *size, int64_t *offset)
1710
+ {
1711
+
1712
+ (void)buff;/* UNUSED */
1713
+ (void)size;/* UNUSED */
1714
+ (void)offset;/* UNUSED */
1715
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1716
+ "zisofs is not supported on this platform");
1717
+ return (ARCHIVE_FAILED);
1718
+ }
1719
+
1720
+ #endif /* HAVE_ZLIB_H */
1721
+
1722
+ static int
1723
+ archive_read_format_iso9660_read_data(struct archive_read *a,
1724
+ const void **buff, size_t *size, int64_t *offset)
1725
+ {
1726
+ struct iso9660 *iso9660 = a->format->data;
1727
+ ssize_t bytes_read;
1728
+
1729
+ if (iso9660->entry_bytes_unconsumed) {
1730
+ __archive_read_consume(a, iso9660->entry_bytes_unconsumed);
1731
+ iso9660->entry_bytes_unconsumed = 0;
1732
+ }
1733
+
1734
+ if (iso9660->entry_bytes_remaining <= 0) {
1735
+ if (iso9660->entry_content != NULL)
1736
+ iso9660->entry_content = iso9660->entry_content->next;
1737
+ if (iso9660->entry_content == NULL) {
1738
+ *buff = NULL;
1739
+ *size = 0;
1740
+ *offset = iso9660->entry_sparse_offset;
1741
+ return (ARCHIVE_EOF);
1742
+ }
1743
+ /* Seek forward to the start of the entry. */
1744
+ if (iso9660->current_position < iso9660->entry_content->offset) {
1745
+ int64_t step;
1746
+
1747
+ step = iso9660->entry_content->offset -
1748
+ iso9660->current_position;
1749
+ step = __archive_read_consume(a, step);
1750
+ if (step < 0)
1751
+ return ((int)step);
1752
+ iso9660->current_position =
1753
+ iso9660->entry_content->offset;
1754
+ }
1755
+ if (iso9660->entry_content->offset < iso9660->current_position) {
1756
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1757
+ "Ignoring out-of-order file (%s) %jd < %jd",
1758
+ iso9660->pathname.s,
1759
+ (intmax_t)iso9660->entry_content->offset,
1760
+ (intmax_t)iso9660->current_position);
1761
+ *buff = NULL;
1762
+ *size = 0;
1763
+ *offset = iso9660->entry_sparse_offset;
1764
+ return (ARCHIVE_WARN);
1765
+ }
1766
+ iso9660->entry_bytes_remaining = iso9660->entry_content->size;
1767
+ }
1768
+ if (iso9660->entry_zisofs.pz)
1769
+ return (zisofs_read_data(a, buff, size, offset));
1770
+
1771
+ *buff = __archive_read_ahead(a, 1, &bytes_read);
1772
+ if (bytes_read == 0)
1773
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1774
+ "Truncated input file");
1775
+ if (*buff == NULL)
1776
+ return (ARCHIVE_FATAL);
1777
+ if (bytes_read > iso9660->entry_bytes_remaining)
1778
+ bytes_read = (ssize_t)iso9660->entry_bytes_remaining;
1779
+ *size = bytes_read;
1780
+ *offset = iso9660->entry_sparse_offset;
1781
+ iso9660->entry_sparse_offset += bytes_read;
1782
+ iso9660->entry_bytes_remaining -= bytes_read;
1783
+ iso9660->entry_bytes_unconsumed = bytes_read;
1784
+ iso9660->current_position += bytes_read;
1785
+ return (ARCHIVE_OK);
1786
+ }
1787
+
1788
+ static int
1789
+ archive_read_format_iso9660_cleanup(struct archive_read *a)
1790
+ {
1791
+ struct iso9660 *iso9660 = a->format->data;
1792
+ int r = ARCHIVE_OK;
1793
+
1794
+ release_files(iso9660);
1795
+ free(iso9660->read_ce_req.reqs);
1796
+ archive_string_free(&iso9660->pathname);
1797
+ archive_string_free(&iso9660->previous_pathname);
1798
+ free(iso9660->pending_files.files);
1799
+ #ifdef HAVE_ZLIB_H
1800
+ free(iso9660->entry_zisofs.uncompressed_buffer);
1801
+ free(iso9660->entry_zisofs.block_pointers);
1802
+ if (iso9660->entry_zisofs.stream_valid) {
1803
+ if (inflateEnd(&iso9660->entry_zisofs.stream) != Z_OK) {
1804
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1805
+ "Failed to clean up zlib decompressor");
1806
+ r = ARCHIVE_FATAL;
1807
+ }
1808
+ }
1809
+ #endif
1810
+ free(iso9660->utf16be_path);
1811
+ free(iso9660->utf16be_previous_path);
1812
+ free(iso9660);
1813
+ a->format->data = NULL;
1814
+ return (r);
1815
+ }
1816
+
1817
+ /*
1818
+ * This routine parses a single ISO directory record, makes sense
1819
+ * of any extensions, and stores the result in memory.
1820
+ */
1821
+ static struct file_info *
1822
+ parse_file_info(struct archive_read *a, struct file_info *parent,
1823
+ const unsigned char *isodirrec, size_t reclen)
1824
+ {
1825
+ struct iso9660 *iso9660 = a->format->data;
1826
+ struct file_info *file, *filep;
1827
+ size_t name_len;
1828
+ const unsigned char *rr_start, *rr_end;
1829
+ const unsigned char *p;
1830
+ size_t dr_len = 0;
1831
+ uint64_t fsize, offset;
1832
+ int32_t location;
1833
+ int flags;
1834
+
1835
+ if (reclen != 0)
1836
+ dr_len = (size_t)isodirrec[DR_length_offset];
1837
+ /*
1838
+ * Sanity check that reclen is not zero and dr_len is greater than
1839
+ * reclen but at least 34
1840
+ */
1841
+ if (reclen == 0 || reclen < dr_len || dr_len < 34) {
1842
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1843
+ "Invalid length of directory record");
1844
+ return (NULL);
1845
+ }
1846
+ name_len = (size_t)isodirrec[DR_name_len_offset];
1847
+ location = archive_le32dec(isodirrec + DR_extent_offset);
1848
+ fsize = archive_le32dec(isodirrec + DR_size_offset);
1849
+ /* Sanity check that name_len doesn't exceed dr_len. */
1850
+ if (dr_len - 33 < name_len || name_len == 0) {
1851
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1852
+ "Invalid length of file identifier");
1853
+ return (NULL);
1854
+ }
1855
+ /* Sanity check that location doesn't exceed volume block.
1856
+ * Don't check lower limit of location; it's possibility
1857
+ * the location has negative value when file type is symbolic
1858
+ * link or file size is zero. As far as I know latest mkisofs
1859
+ * do that.
1860
+ */
1861
+ if (location > 0 &&
1862
+ (location + ((fsize + iso9660->logical_block_size -1)
1863
+ / iso9660->logical_block_size))
1864
+ > (uint32_t)iso9660->volume_block) {
1865
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1866
+ "Invalid location of extent of file");
1867
+ return (NULL);
1868
+ }
1869
+ /* Sanity check that location doesn't have a negative value
1870
+ * when the file is not empty. it's too large. */
1871
+ if (fsize != 0 && location < 0) {
1872
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1873
+ "Invalid location of extent of file");
1874
+ return (NULL);
1875
+ }
1876
+
1877
+ /* Sanity check that this entry does not create a cycle. */
1878
+ offset = iso9660->logical_block_size * (uint64_t)location;
1879
+ for (filep = parent; filep != NULL; filep = filep->parent) {
1880
+ if (filep->offset == offset) {
1881
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1882
+ "Directory structure contains loop");
1883
+ return (NULL);
1884
+ }
1885
+ }
1886
+
1887
+ /* Create a new file entry and copy data from the ISO dir record. */
1888
+ file = calloc(1, sizeof(*file));
1889
+ if (file == NULL) {
1890
+ archive_set_error(&a->archive, ENOMEM,
1891
+ "No memory for file entry");
1892
+ return (NULL);
1893
+ }
1894
+ file->parent = parent;
1895
+ file->offset = offset;
1896
+ file->size = fsize;
1897
+ if (isodate7_valid(isodirrec + DR_date_offset)) {
1898
+ file->time_is_set |= MTIME_IS_SET | ATIME_IS_SET | CTIME_IS_SET;
1899
+ file->mtime = isodate7(isodirrec + DR_date_offset);
1900
+ file->ctime = file->atime = file->mtime;
1901
+ }
1902
+ file->rede_files.first = NULL;
1903
+ file->rede_files.last = &(file->rede_files.first);
1904
+
1905
+ p = isodirrec + DR_name_offset;
1906
+ /* Rockridge extensions (if any) follow name. Compute this
1907
+ * before fidgeting the name_len below. */
1908
+ rr_start = p + name_len + (name_len & 1 ? 0 : 1);
1909
+ rr_end = isodirrec + dr_len;
1910
+
1911
+ if (iso9660->seenJoliet) {
1912
+ /* Joliet names are max 64 chars (128 bytes) according to spec,
1913
+ * but genisoimage/mkisofs allows recording longer Joliet
1914
+ * names which are 103 UCS2 characters(206 bytes) by their
1915
+ * option '-joliet-long'.
1916
+ */
1917
+ if (name_len > 206)
1918
+ name_len = 206;
1919
+ name_len &= ~1;
1920
+
1921
+ /* trim trailing first version and dot from filename.
1922
+ *
1923
+ * Remember we were in UTF-16BE land!
1924
+ * SEPARATOR 1 (.) and SEPARATOR 2 (;) are both
1925
+ * 16 bits big endian characters on Joliet.
1926
+ *
1927
+ * TODO: sanitize filename?
1928
+ * Joliet allows any UCS-2 char except:
1929
+ * *, /, :, ;, ? and \.
1930
+ */
1931
+ /* Chop off trailing ';1' from files. */
1932
+ if (name_len > 4 && p[name_len-4] == 0 && p[name_len-3] == ';'
1933
+ && p[name_len-2] == 0 && p[name_len-1] == '1')
1934
+ name_len -= 4;
1935
+ #if 0 /* XXX: this somehow manages to strip of single-character file extensions, like '.c'. */
1936
+ /* Chop off trailing '.' from filenames. */
1937
+ if (name_len > 2 && p[name_len-2] == 0 && p[name_len-1] == '.')
1938
+ name_len -= 2;
1939
+ #endif
1940
+ if ((file->utf16be_name = malloc(name_len)) == NULL) {
1941
+ archive_set_error(&a->archive, ENOMEM,
1942
+ "No memory for file name");
1943
+ goto fail;
1944
+ }
1945
+ memcpy(file->utf16be_name, p, name_len);
1946
+ file->utf16be_bytes = name_len;
1947
+ } else {
1948
+ /* Chop off trailing ';1' from files. */
1949
+ if (name_len > 2 && p[name_len - 2] == ';' &&
1950
+ p[name_len - 1] == '1')
1951
+ name_len -= 2;
1952
+ /* Chop off trailing '.' from filenames. */
1953
+ if (name_len > 1 && p[name_len - 1] == '.')
1954
+ --name_len;
1955
+
1956
+ archive_strncpy(&file->name, (const char *)p, name_len);
1957
+ }
1958
+
1959
+ flags = isodirrec[DR_flags_offset];
1960
+ if (flags & 0x02)
1961
+ file->mode = AE_IFDIR | 0700;
1962
+ else
1963
+ file->mode = AE_IFREG | 0400;
1964
+ if (flags & 0x80)
1965
+ file->multi_extent = 1;
1966
+ else
1967
+ file->multi_extent = 0;
1968
+ /*
1969
+ * Use a location for the file number, which is treated as an inode
1970
+ * number to find out hardlink target. If Rockridge extensions is
1971
+ * being used, the file number will be overwritten by FILE SERIAL
1972
+ * NUMBER of RRIP "PX" extension.
1973
+ * Note: Old mkisofs did not record that FILE SERIAL NUMBER
1974
+ * in ISO images.
1975
+ * Note2: xorriso set 0 to the location of a symlink file.
1976
+ */
1977
+ if (file->size == 0 && location >= 0) {
1978
+ /* If file->size is zero, its location points wrong place,
1979
+ * and so we should not use it for the file number.
1980
+ * When the location has negative value, it can be used
1981
+ * for the file number.
1982
+ */
1983
+ file->number = -1;
1984
+ /* Do not appear before any directory entries. */
1985
+ file->offset = -1;
1986
+ } else
1987
+ file->number = (int64_t)(uint32_t)location;
1988
+
1989
+ /* Rockridge extensions overwrite information from above. */
1990
+ if (iso9660->opt_support_rockridge) {
1991
+ if (parent == NULL && rr_end - rr_start >= 7) {
1992
+ p = rr_start;
1993
+ if (memcmp(p, "SP\x07\x01\xbe\xef", 6) == 0) {
1994
+ /*
1995
+ * SP extension stores the suspOffset
1996
+ * (Number of bytes to skip between
1997
+ * filename and SUSP records.)
1998
+ * It is mandatory by the SUSP standard
1999
+ * (IEEE 1281).
2000
+ *
2001
+ * It allows SUSP to coexist with
2002
+ * non-SUSP uses of the System
2003
+ * Use Area by placing non-SUSP data
2004
+ * before SUSP data.
2005
+ *
2006
+ * SP extension must be in the root
2007
+ * directory entry, disable all SUSP
2008
+ * processing if not found.
2009
+ */
2010
+ iso9660->suspOffset = p[6];
2011
+ iso9660->seenSUSP = 1;
2012
+ rr_start += 7;
2013
+ }
2014
+ }
2015
+ if (iso9660->seenSUSP) {
2016
+ int r;
2017
+
2018
+ file->name_continues = 0;
2019
+ file->symlink_continues = 0;
2020
+ rr_start += iso9660->suspOffset;
2021
+ r = parse_rockridge(a, file, rr_start, rr_end);
2022
+ if (r != ARCHIVE_OK)
2023
+ goto fail;
2024
+ /*
2025
+ * A file size of symbolic link files in ISO images
2026
+ * made by makefs is not zero and its location is
2027
+ * the same as those of next regular file. That is
2028
+ * the same as hard like file and it causes unexpected
2029
+ * error.
2030
+ */
2031
+ if (file->size > 0 &&
2032
+ (file->mode & AE_IFMT) == AE_IFLNK) {
2033
+ file->size = 0;
2034
+ file->number = -1;
2035
+ file->offset = -1;
2036
+ }
2037
+ } else
2038
+ /* If there isn't SUSP, disable parsing
2039
+ * rock ridge extensions. */
2040
+ iso9660->opt_support_rockridge = 0;
2041
+ }
2042
+
2043
+ file->nlinks = 1;/* Reset nlink. we'll calculate it later. */
2044
+ /* Tell file's parent how many children that parent has. */
2045
+ if (parent != NULL && (flags & 0x02))
2046
+ parent->subdirs++;
2047
+
2048
+ if (iso9660->seenRockridge) {
2049
+ if (parent != NULL && parent->parent == NULL &&
2050
+ (flags & 0x02) && iso9660->rr_moved == NULL &&
2051
+ file->name.s &&
2052
+ (strcmp(file->name.s, "rr_moved") == 0 ||
2053
+ strcmp(file->name.s, ".rr_moved") == 0)) {
2054
+ iso9660->rr_moved = file;
2055
+ file->rr_moved = 1;
2056
+ file->rr_moved_has_re_only = 1;
2057
+ file->re = 0;
2058
+ parent->subdirs--;
2059
+ } else if (file->re) {
2060
+ /*
2061
+ * Sanity check: file's parent is rr_moved.
2062
+ */
2063
+ if (parent == NULL || parent->rr_moved == 0) {
2064
+ archive_set_error(&a->archive,
2065
+ ARCHIVE_ERRNO_MISC,
2066
+ "Invalid Rockridge RE");
2067
+ goto fail;
2068
+ }
2069
+ /*
2070
+ * Sanity check: file does not have "CL" extension.
2071
+ */
2072
+ if (file->cl_offset) {
2073
+ archive_set_error(&a->archive,
2074
+ ARCHIVE_ERRNO_MISC,
2075
+ "Invalid Rockridge RE and CL");
2076
+ goto fail;
2077
+ }
2078
+ /*
2079
+ * Sanity check: The file type must be a directory.
2080
+ */
2081
+ if ((flags & 0x02) == 0) {
2082
+ archive_set_error(&a->archive,
2083
+ ARCHIVE_ERRNO_MISC,
2084
+ "Invalid Rockridge RE");
2085
+ goto fail;
2086
+ }
2087
+ } else if (parent != NULL && parent->rr_moved)
2088
+ file->rr_moved_has_re_only = 0;
2089
+ else if (parent != NULL && (flags & 0x02) &&
2090
+ (parent->re || parent->re_descendant))
2091
+ file->re_descendant = 1;
2092
+ if (file->cl_offset) {
2093
+ struct file_info *r;
2094
+
2095
+ if (parent == NULL || parent->parent == NULL) {
2096
+ archive_set_error(&a->archive,
2097
+ ARCHIVE_ERRNO_MISC,
2098
+ "Invalid Rockridge CL");
2099
+ goto fail;
2100
+ }
2101
+ /*
2102
+ * Sanity check: The file type must be a regular file.
2103
+ */
2104
+ if ((flags & 0x02) != 0) {
2105
+ archive_set_error(&a->archive,
2106
+ ARCHIVE_ERRNO_MISC,
2107
+ "Invalid Rockridge CL");
2108
+ goto fail;
2109
+ }
2110
+ parent->subdirs++;
2111
+ /* Overwrite an offset and a number of this "CL" entry
2112
+ * to appear before other dirs. "+1" to those is to
2113
+ * make sure to appear after "RE" entry which this
2114
+ * "CL" entry should be connected with. */
2115
+ file->offset = file->number = file->cl_offset + 1;
2116
+
2117
+ /*
2118
+ * Sanity check: cl_offset does not point at its
2119
+ * the parents or itself.
2120
+ */
2121
+ for (r = parent; r; r = r->parent) {
2122
+ if (r->offset == file->cl_offset) {
2123
+ archive_set_error(&a->archive,
2124
+ ARCHIVE_ERRNO_MISC,
2125
+ "Invalid Rockridge CL");
2126
+ goto fail;
2127
+ }
2128
+ }
2129
+ if (file->cl_offset == file->offset ||
2130
+ parent->rr_moved) {
2131
+ archive_set_error(&a->archive,
2132
+ ARCHIVE_ERRNO_MISC,
2133
+ "Invalid Rockridge CL");
2134
+ goto fail;
2135
+ }
2136
+ }
2137
+ }
2138
+
2139
+ #if DEBUG
2140
+ /* DEBUGGING: Warn about attributes I don't yet fully support. */
2141
+ if ((flags & ~0x02) != 0) {
2142
+ fprintf(stderr, "\n ** Unrecognized flag: ");
2143
+ dump_isodirrec(stderr, isodirrec);
2144
+ fprintf(stderr, "\n");
2145
+ } else if (archive_le16dec(isodirrec + DR_volume_sequence_number_offset) != 1) {
2146
+ fprintf(stderr, "\n ** Unrecognized sequence number: ");
2147
+ dump_isodirrec(stderr, isodirrec);
2148
+ fprintf(stderr, "\n");
2149
+ } else if (*(isodirrec + DR_file_unit_size_offset) != 0) {
2150
+ fprintf(stderr, "\n ** Unexpected file unit size: ");
2151
+ dump_isodirrec(stderr, isodirrec);
2152
+ fprintf(stderr, "\n");
2153
+ } else if (*(isodirrec + DR_interleave_offset) != 0) {
2154
+ fprintf(stderr, "\n ** Unexpected interleave: ");
2155
+ dump_isodirrec(stderr, isodirrec);
2156
+ fprintf(stderr, "\n");
2157
+ } else if (*(isodirrec + DR_ext_attr_length_offset) != 0) {
2158
+ fprintf(stderr, "\n ** Unexpected extended attribute length: ");
2159
+ dump_isodirrec(stderr, isodirrec);
2160
+ fprintf(stderr, "\n");
2161
+ }
2162
+ #endif
2163
+ register_file(iso9660, file);
2164
+ return (file);
2165
+ fail:
2166
+ archive_string_free(&file->name);
2167
+ free(file);
2168
+ return (NULL);
2169
+ }
2170
+
2171
+ static int
2172
+ parse_rockridge(struct archive_read *a, struct file_info *file,
2173
+ const unsigned char *p, const unsigned char *end)
2174
+ {
2175
+ struct iso9660 *iso9660 = a->format->data;
2176
+ int entry_seen = 0;
2177
+
2178
+ while (p + 4 <= end /* Enough space for another entry. */
2179
+ && p[0] >= 'A' && p[0] <= 'Z' /* Sanity-check 1st char of name. */
2180
+ && p[1] >= 'A' && p[1] <= 'Z' /* Sanity-check 2nd char of name. */
2181
+ && p[2] >= 4 /* Sanity-check length. */
2182
+ && p + p[2] <= end) { /* Sanity-check length. */
2183
+ const unsigned char *data = p + 4;
2184
+ int data_length = p[2] - 4;
2185
+ int version = p[3];
2186
+
2187
+ switch(p[0]) {
2188
+ case 'C':
2189
+ if (p[1] == 'E') {
2190
+ if (version == 1 && data_length == 24) {
2191
+ /*
2192
+ * CE extension comprises:
2193
+ * 8 byte sector containing extension
2194
+ * 8 byte offset w/in above sector
2195
+ * 8 byte length of continuation
2196
+ */
2197
+ int32_t location =
2198
+ archive_le32dec(data);
2199
+ file->ce_offset =
2200
+ archive_le32dec(data+8);
2201
+ file->ce_size =
2202
+ archive_le32dec(data+16);
2203
+ if (register_CE(a, location, file)
2204
+ != ARCHIVE_OK)
2205
+ return (ARCHIVE_FATAL);
2206
+ }
2207
+ }
2208
+ else if (p[1] == 'L') {
2209
+ if (version == 1 && data_length == 8) {
2210
+ file->cl_offset = (uint64_t)
2211
+ iso9660->logical_block_size *
2212
+ (uint64_t)archive_le32dec(data);
2213
+ iso9660->seenRockridge = 1;
2214
+ }
2215
+ }
2216
+ break;
2217
+ case 'N':
2218
+ if (p[1] == 'M') {
2219
+ if (version == 1) {
2220
+ parse_rockridge_NM1(file,
2221
+ data, data_length);
2222
+ iso9660->seenRockridge = 1;
2223
+ }
2224
+ }
2225
+ break;
2226
+ case 'P':
2227
+ /*
2228
+ * PD extension is padding;
2229
+ * contents are always ignored.
2230
+ *
2231
+ * PL extension won't appear;
2232
+ * contents are always ignored.
2233
+ */
2234
+ if (p[1] == 'N') {
2235
+ if (version == 1 && data_length == 16) {
2236
+ file->rdev = archive_le32dec(data);
2237
+ file->rdev <<= 32;
2238
+ file->rdev |=
2239
+ archive_le32dec(data + 8);
2240
+ iso9660->seenRockridge = 1;
2241
+ }
2242
+ }
2243
+ else if (p[1] == 'X') {
2244
+ /*
2245
+ * PX extension comprises:
2246
+ * 8 bytes for mode,
2247
+ * 8 bytes for nlinks,
2248
+ * 8 bytes for uid,
2249
+ * 8 bytes for gid,
2250
+ * 8 bytes for inode.
2251
+ */
2252
+ if (version == 1) {
2253
+ if (data_length >= 8)
2254
+ file->mode = (__LA_MODE_T)
2255
+ archive_le32dec(data);
2256
+ if (data_length >= 16)
2257
+ file->nlinks =
2258
+ archive_le32dec(data + 8);
2259
+ if (data_length >= 24)
2260
+ file->uid =
2261
+ archive_le32dec(data + 16);
2262
+ if (data_length >= 32)
2263
+ file->gid =
2264
+ archive_le32dec(data + 24);
2265
+ if (data_length >= 40)
2266
+ file->number =
2267
+ archive_le32dec(data + 32);
2268
+ iso9660->seenRockridge = 1;
2269
+ }
2270
+ }
2271
+ break;
2272
+ case 'R':
2273
+ if (p[1] == 'E' && version == 1) {
2274
+ file->re = 1;
2275
+ iso9660->seenRockridge = 1;
2276
+ }
2277
+ else if (p[1] == 'R' && version == 1) {
2278
+ /*
2279
+ * RR extension comprises:
2280
+ * one byte flag value
2281
+ * This extension is obsolete,
2282
+ * so contents are always ignored.
2283
+ */
2284
+ }
2285
+ break;
2286
+ case 'S':
2287
+ if (p[1] == 'L') {
2288
+ if (version == 1) {
2289
+ parse_rockridge_SL1(file,
2290
+ data, data_length);
2291
+ iso9660->seenRockridge = 1;
2292
+ }
2293
+ }
2294
+ else if (p[1] == 'T'
2295
+ && data_length == 0 && version == 1) {
2296
+ /*
2297
+ * ST extension marks end of this
2298
+ * block of SUSP entries.
2299
+ *
2300
+ * It allows SUSP to coexist with
2301
+ * non-SUSP uses of the System
2302
+ * Use Area by placing non-SUSP data
2303
+ * after SUSP data.
2304
+ */
2305
+ iso9660->seenSUSP = 0;
2306
+ iso9660->seenRockridge = 0;
2307
+ return (ARCHIVE_OK);
2308
+ }
2309
+ break;
2310
+ case 'T':
2311
+ if (p[1] == 'F') {
2312
+ if (version == 1) {
2313
+ parse_rockridge_TF1(file,
2314
+ data, data_length);
2315
+ iso9660->seenRockridge = 1;
2316
+ }
2317
+ }
2318
+ break;
2319
+ case 'Z':
2320
+ if (p[1] == 'F') {
2321
+ if (version == 1)
2322
+ parse_rockridge_ZF1(file,
2323
+ data, data_length);
2324
+ }
2325
+ break;
2326
+ default:
2327
+ break;
2328
+ }
2329
+
2330
+ p += p[2];
2331
+ entry_seen = 1;
2332
+ }
2333
+
2334
+ if (entry_seen)
2335
+ return (ARCHIVE_OK);
2336
+ else {
2337
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2338
+ "Tried to parse Rockridge extensions, but none found");
2339
+ return (ARCHIVE_WARN);
2340
+ }
2341
+ }
2342
+
2343
+ static int
2344
+ register_CE(struct archive_read *a, int32_t location,
2345
+ struct file_info *file)
2346
+ {
2347
+ struct iso9660 *iso9660 = a->format->data;
2348
+ struct read_ce_queue *heap;
2349
+ uint64_t offset, parent_offset;
2350
+ size_t hole, parent;
2351
+
2352
+ offset = ((uint64_t)location) * (uint64_t)iso9660->logical_block_size;
2353
+ if (((file->mode & AE_IFMT) == AE_IFREG &&
2354
+ offset >= file->offset) ||
2355
+ offset < iso9660->current_position ||
2356
+ (((uint64_t)file->ce_offset) + file->ce_size)
2357
+ > (uint64_t)iso9660->logical_block_size ||
2358
+ offset + file->ce_offset + file->ce_size
2359
+ > iso9660->volume_size) {
2360
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2361
+ "Invalid parameter in SUSP \"CE\" extension");
2362
+ return (ARCHIVE_FATAL);
2363
+ }
2364
+
2365
+ /* Expand our CE list as necessary. */
2366
+ heap = &(iso9660->read_ce_req);
2367
+ if (heap->cnt >= heap->allocated) {
2368
+ struct read_ce_req *p;
2369
+ size_t new_size;
2370
+
2371
+ if (heap->allocated < 16)
2372
+ new_size = 16;
2373
+ else if (archive_ckd_mul_size(&new_size, heap->allocated, 2)) {
2374
+ /* Overflow keeps us from growing the list. */
2375
+ archive_set_error(&a->archive, ENOMEM, "Out of memory");
2376
+ return (ARCHIVE_FATAL);
2377
+ }
2378
+ p = calloc(new_size, sizeof(p[0]));
2379
+ if (p == NULL) {
2380
+ archive_set_error(&a->archive, ENOMEM, "Out of memory");
2381
+ return (ARCHIVE_FATAL);
2382
+ }
2383
+ if (heap->reqs != NULL) {
2384
+ memcpy(p, heap->reqs, heap->cnt * sizeof(*p));
2385
+ free(heap->reqs);
2386
+ }
2387
+ heap->reqs = p;
2388
+ heap->allocated = new_size;
2389
+ }
2390
+
2391
+ /*
2392
+ * Start with hole at end, walk it up tree to find insertion point.
2393
+ */
2394
+ hole = heap->cnt++;
2395
+ while (hole > 0) {
2396
+ parent = (hole - 1)/2;
2397
+ parent_offset = heap->reqs[parent].offset;
2398
+ if (offset >= parent_offset) {
2399
+ heap->reqs[hole].offset = offset;
2400
+ heap->reqs[hole].file = file;
2401
+ return (ARCHIVE_OK);
2402
+ }
2403
+ /* Move parent into hole <==> move hole up tree. */
2404
+ heap->reqs[hole] = heap->reqs[parent];
2405
+ hole = parent;
2406
+ }
2407
+ heap->reqs[0].offset = offset;
2408
+ heap->reqs[0].file = file;
2409
+ return (ARCHIVE_OK);
2410
+ }
2411
+
2412
+ static void
2413
+ next_CE(struct read_ce_queue *heap)
2414
+ {
2415
+ uint64_t a_offset, b_offset, c_offset;
2416
+ size_t a, b, c;
2417
+ struct read_ce_req tmp;
2418
+
2419
+ if (heap->cnt < 1)
2420
+ return;
2421
+
2422
+ /*
2423
+ * Move the last item in the heap to the root of the tree
2424
+ */
2425
+ heap->reqs[0] = heap->reqs[--(heap->cnt)];
2426
+
2427
+ /*
2428
+ * Rebalance the heap.
2429
+ */
2430
+ a = 0; /* Starting element and its offset */
2431
+ a_offset = heap->reqs[a].offset;
2432
+ for (;;) {
2433
+ b = a + a + 1; /* First child */
2434
+ if (b >= heap->cnt)
2435
+ return;
2436
+ b_offset = heap->reqs[b].offset;
2437
+ c = b + 1; /* Use second child if it is smaller. */
2438
+ if (c < heap->cnt) {
2439
+ c_offset = heap->reqs[c].offset;
2440
+ if (c_offset < b_offset) {
2441
+ b = c;
2442
+ b_offset = c_offset;
2443
+ }
2444
+ }
2445
+ if (a_offset <= b_offset)
2446
+ return;
2447
+ tmp = heap->reqs[a];
2448
+ heap->reqs[a] = heap->reqs[b];
2449
+ heap->reqs[b] = tmp;
2450
+ a = b;
2451
+ }
2452
+ }
2453
+
2454
+
2455
+ static int
2456
+ read_CE(struct archive_read *a, struct iso9660 *iso9660)
2457
+ {
2458
+ struct read_ce_queue *heap;
2459
+ const unsigned char *b, *p, *end;
2460
+ struct file_info *file;
2461
+ size_t step;
2462
+ uint64_t ce_start, ce_end;
2463
+ int r;
2464
+
2465
+ /* Read data which RRIP "CE" extension points. */
2466
+ heap = &(iso9660->read_ce_req);
2467
+ step = iso9660->logical_block_size;
2468
+ while (heap->cnt &&
2469
+ heap->reqs[0].offset == iso9660->current_position) {
2470
+ b = __archive_read_ahead(a, step, NULL);
2471
+ if (b == NULL) {
2472
+ archive_set_error(&a->archive,
2473
+ ARCHIVE_ERRNO_MISC,
2474
+ "Failed to read full block when scanning "
2475
+ "ISO9660 directory list");
2476
+ return (ARCHIVE_FATAL);
2477
+ }
2478
+ do {
2479
+ file = heap->reqs[0].file;
2480
+ if (file->ce_offset + file->ce_size > step) {
2481
+ archive_set_error(&a->archive,
2482
+ ARCHIVE_ERRNO_FILE_FORMAT,
2483
+ "Malformed CE information");
2484
+ return (ARCHIVE_FATAL);
2485
+ }
2486
+ ce_start = heap->reqs[0].offset + file->ce_offset;
2487
+ ce_end = ce_start + file->ce_size;
2488
+ if (ce_start < file->ce_processed_end) {
2489
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2490
+ "Invalid parameter in SUSP \"CE\" extension");
2491
+ return (ARCHIVE_FATAL);
2492
+ }
2493
+ p = b + file->ce_offset;
2494
+ end = p + file->ce_size;
2495
+ file->ce_processed_end = ce_end;
2496
+ next_CE(heap);
2497
+ r = parse_rockridge(a, file, p, end);
2498
+ if (r != ARCHIVE_OK)
2499
+ return (ARCHIVE_FATAL);
2500
+ } while (heap->cnt &&
2501
+ heap->reqs[0].offset == iso9660->current_position);
2502
+ /* NOTE: Do not move this consume's code to front of
2503
+ * do-while loop. Registration of nested CE extension
2504
+ * might cause error because of current position. */
2505
+ __archive_read_consume(a, step);
2506
+ iso9660->current_position += step;
2507
+ }
2508
+ return (ARCHIVE_OK);
2509
+ }
2510
+
2511
+ static void
2512
+ parse_rockridge_NM1(struct file_info *file,
2513
+ const unsigned char *data, int data_length)
2514
+ {
2515
+ if (!file->name_continues)
2516
+ archive_string_empty(&file->name);
2517
+ file->name_continues = 0;
2518
+ if (data_length < 1)
2519
+ return;
2520
+ /*
2521
+ * NM version 1 extension comprises:
2522
+ * 1 byte flag, value is one of:
2523
+ * = 0: remainder is name
2524
+ * = 1: remainder is name, next NM entry continues name
2525
+ * = 2: "."
2526
+ * = 4: ".."
2527
+ * = 32: Implementation specific
2528
+ * All other values are reserved.
2529
+ */
2530
+ switch(data[0]) {
2531
+ case 0:
2532
+ if (data_length < 2)
2533
+ return;
2534
+ archive_strncat(&file->name,
2535
+ (const char *)data + 1, data_length - 1);
2536
+ break;
2537
+ case 1:
2538
+ if (data_length < 2)
2539
+ return;
2540
+ archive_strncat(&file->name,
2541
+ (const char *)data + 1, data_length - 1);
2542
+ file->name_continues = 1;
2543
+ break;
2544
+ case 2:
2545
+ archive_strcat(&file->name, ".");
2546
+ break;
2547
+ case 4:
2548
+ archive_strcat(&file->name, "..");
2549
+ break;
2550
+ default:
2551
+ return;
2552
+ }
2553
+
2554
+ }
2555
+
2556
+ static void
2557
+ parse_rockridge_TF1(struct file_info *file, const unsigned char *data,
2558
+ int data_length)
2559
+ {
2560
+ char flag;
2561
+ /*
2562
+ * TF extension comprises:
2563
+ * one byte flag
2564
+ * create time (optional)
2565
+ * modify time (optional)
2566
+ * access time (optional)
2567
+ * attribute time (optional)
2568
+ * Time format and presence of fields
2569
+ * is controlled by flag bits.
2570
+ */
2571
+ if (data_length < 1)
2572
+ return;
2573
+ flag = data[0];
2574
+ ++data;
2575
+ --data_length;
2576
+ if (flag & 0x80) {
2577
+ /* Use 17-byte time format. */
2578
+ if ((flag & 1) && data_length >= 17) {
2579
+ /* Create time. */
2580
+ if (isodate17_valid(data)) {
2581
+ file->time_is_set |= BIRTHTIME_IS_SET;
2582
+ file->birthtime = isodate17(data);
2583
+ }
2584
+ data += 17;
2585
+ data_length -= 17;
2586
+ }
2587
+ if ((flag & 2) && data_length >= 17) {
2588
+ /* Modify time. */
2589
+ if (isodate17_valid(data)) {
2590
+ file->time_is_set |= MTIME_IS_SET;
2591
+ file->mtime = isodate17(data);
2592
+ }
2593
+ data += 17;
2594
+ data_length -= 17;
2595
+ }
2596
+ if ((flag & 4) && data_length >= 17) {
2597
+ /* Access time. */
2598
+ if (isodate17_valid(data)) {
2599
+ file->time_is_set |= ATIME_IS_SET;
2600
+ file->atime = isodate17(data);
2601
+ }
2602
+ data += 17;
2603
+ data_length -= 17;
2604
+ }
2605
+ if ((flag & 8) && data_length >= 17) {
2606
+ /* Attribute change time. */
2607
+ if (isodate17_valid(data)) {
2608
+ file->time_is_set |= CTIME_IS_SET;
2609
+ file->ctime = isodate17(data);
2610
+ }
2611
+ }
2612
+ } else {
2613
+ /* Use 7-byte time format. */
2614
+ if ((flag & 1) && data_length >= 7) {
2615
+ /* Create time. */
2616
+ if (isodate7_valid(data)) {
2617
+ file->time_is_set |= BIRTHTIME_IS_SET;
2618
+ file->birthtime = isodate7(data);
2619
+ }
2620
+ data += 7;
2621
+ data_length -= 7;
2622
+ }
2623
+ if ((flag & 2) && data_length >= 7) {
2624
+ /* Modify time. */
2625
+ if (isodate7_valid(data)) {
2626
+ file->time_is_set |= MTIME_IS_SET;
2627
+ file->mtime = isodate7(data);
2628
+ }
2629
+ data += 7;
2630
+ data_length -= 7;
2631
+ }
2632
+ if ((flag & 4) && data_length >= 7) {
2633
+ /* Access time. */
2634
+ if (isodate7_valid(data)) {
2635
+ file->time_is_set |= ATIME_IS_SET;
2636
+ file->atime = isodate7(data);
2637
+ }
2638
+ data += 7;
2639
+ data_length -= 7;
2640
+ }
2641
+ if ((flag & 8) && data_length >= 7) {
2642
+ /* Attribute change time. */
2643
+ if (isodate7_valid(data)) {
2644
+ file->time_is_set |= CTIME_IS_SET;
2645
+ file->ctime = isodate7(data);
2646
+ }
2647
+ }
2648
+ }
2649
+ }
2650
+
2651
+ static void
2652
+ parse_rockridge_SL1(struct file_info *file, const unsigned char *data,
2653
+ int data_length)
2654
+ {
2655
+ const char *separator = "";
2656
+
2657
+ if (!file->symlink_continues || file->symlink.length < 1)
2658
+ archive_string_empty(&file->symlink);
2659
+ file->symlink_continues = 0;
2660
+
2661
+ /*
2662
+ * Defined flag values:
2663
+ * 0: This is the last SL record for this symbolic link
2664
+ * 1: this symbolic link field continues in next SL entry
2665
+ * All other values are reserved.
2666
+ */
2667
+ if (data_length < 1)
2668
+ return;
2669
+ switch(*data) {
2670
+ case 0:
2671
+ break;
2672
+ case 1:
2673
+ file->symlink_continues = 1;
2674
+ break;
2675
+ default:
2676
+ return;
2677
+ }
2678
+ ++data; /* Skip flag byte. */
2679
+ --data_length;
2680
+
2681
+ /*
2682
+ * SL extension body stores "components".
2683
+ * Basically, this is a complicated way of storing
2684
+ * a POSIX path. It also interferes with using
2685
+ * symlinks for storing non-path data. <sigh>
2686
+ *
2687
+ * Each component is 2 bytes (flag and length)
2688
+ * possibly followed by name data.
2689
+ */
2690
+ while (data_length >= 2) {
2691
+ unsigned char flag = *data++;
2692
+ unsigned char nlen = *data++;
2693
+ data_length -= 2;
2694
+
2695
+ archive_strcat(&file->symlink, separator);
2696
+ separator = "/";
2697
+
2698
+ switch(flag) {
2699
+ case 0: /* Usual case, this is text. */
2700
+ if (data_length < nlen)
2701
+ return;
2702
+ archive_strncat(&file->symlink,
2703
+ (const char *)data, nlen);
2704
+ break;
2705
+ case 0x01: /* Text continues in next component. */
2706
+ if (data_length < nlen)
2707
+ return;
2708
+ archive_strncat(&file->symlink,
2709
+ (const char *)data, nlen);
2710
+ separator = "";
2711
+ break;
2712
+ case 0x02: /* Current dir. */
2713
+ archive_strcat(&file->symlink, ".");
2714
+ break;
2715
+ case 0x04: /* Parent dir. */
2716
+ archive_strcat(&file->symlink, "..");
2717
+ break;
2718
+ case 0x08: /* Root of filesystem. */
2719
+ archive_strcat(&file->symlink, "/");
2720
+ separator = "";
2721
+ break;
2722
+ case 0x10: /* Undefined (historically "volume root" */
2723
+ archive_string_empty(&file->symlink);
2724
+ archive_strcat(&file->symlink, "ROOT");
2725
+ break;
2726
+ case 0x20: /* Undefined (historically "hostname") */
2727
+ archive_strcat(&file->symlink, "hostname");
2728
+ break;
2729
+ default:
2730
+ /* TODO: issue a warning ? */
2731
+ return;
2732
+ }
2733
+ data += nlen;
2734
+ data_length -= nlen;
2735
+ }
2736
+ }
2737
+
2738
+ static void
2739
+ parse_rockridge_ZF1(struct file_info *file, const unsigned char *data,
2740
+ int data_length)
2741
+ {
2742
+
2743
+ if (data_length == 12 && data[0] == 0x70 && data[1] == 0x7a) {
2744
+ /* paged zlib */
2745
+ file->pz = 1;
2746
+ file->pz_log2_bs = data[3];
2747
+ if (file->pz_log2_bs < 15 || file->pz_log2_bs > 17) {
2748
+ /* TODO: Return an error here instead of silently
2749
+ * disabling zisofs. That requires propagating an
2750
+ * error return through parse_rockridge() and its
2751
+ * callers. */
2752
+ file->pz = 0;
2753
+ return;
2754
+ }
2755
+ file->pz_uncompressed_size = archive_le32dec(&data[4]);
2756
+ }
2757
+ }
2758
+
2759
+ static void
2760
+ register_file(struct iso9660 *iso9660, struct file_info *file)
2761
+ {
2762
+
2763
+ file->use_next = iso9660->use_files;
2764
+ iso9660->use_files = file;
2765
+ }
2766
+
2767
+ static void
2768
+ release_files(struct iso9660 *iso9660)
2769
+ {
2770
+ struct content *con, *connext;
2771
+ struct file_info *file;
2772
+
2773
+ file = iso9660->use_files;
2774
+ while (file != NULL) {
2775
+ struct file_info *next = file->use_next;
2776
+
2777
+ archive_string_free(&file->name);
2778
+ archive_string_free(&file->symlink);
2779
+ free(file->utf16be_name);
2780
+ con = file->contents.first;
2781
+ while (con != NULL) {
2782
+ connext = con->next;
2783
+ free(con);
2784
+ con = connext;
2785
+ }
2786
+ free(file);
2787
+ file = next;
2788
+ }
2789
+ }
2790
+
2791
+ static int
2792
+ next_entry_seek(struct archive_read *a, struct iso9660 *iso9660,
2793
+ struct file_info **pfile)
2794
+ {
2795
+ struct file_info *file;
2796
+ int r;
2797
+
2798
+ r = next_cache_entry(a, iso9660, pfile);
2799
+ if (r != ARCHIVE_OK)
2800
+ return (r);
2801
+ file = *pfile;
2802
+
2803
+ /* Don't waste time seeking for zero-length bodies. */
2804
+ if (file->size == 0)
2805
+ file->offset = iso9660->current_position;
2806
+
2807
+ /* flush any remaining bytes from the last round to ensure
2808
+ * we're positioned */
2809
+ if (iso9660->entry_bytes_unconsumed) {
2810
+ __archive_read_consume(a, iso9660->entry_bytes_unconsumed);
2811
+ iso9660->entry_bytes_unconsumed = 0;
2812
+ }
2813
+
2814
+ /* Seek forward to the start of the entry. */
2815
+ if (iso9660->current_position < file->offset) {
2816
+ int64_t step;
2817
+
2818
+ step = file->offset - iso9660->current_position;
2819
+ step = __archive_read_consume(a, step);
2820
+ if (step < 0)
2821
+ return ((int)step);
2822
+ iso9660->current_position = file->offset;
2823
+ }
2824
+
2825
+ /* We found body of file; handle it now. */
2826
+ return (ARCHIVE_OK);
2827
+ }
2828
+
2829
+ static int
2830
+ next_cache_entry(struct archive_read *a, struct iso9660 *iso9660,
2831
+ struct file_info **pfile)
2832
+ {
2833
+ struct file_info *file;
2834
+ struct {
2835
+ struct file_info *first;
2836
+ struct file_info **last;
2837
+ } empty_files;
2838
+ int64_t number;
2839
+ int count;
2840
+
2841
+ file = cache_get_entry(iso9660);
2842
+ if (file != NULL) {
2843
+ *pfile = file;
2844
+ return (ARCHIVE_OK);
2845
+ }
2846
+
2847
+ for (;;) {
2848
+ struct file_info *re, *d;
2849
+
2850
+ *pfile = file = next_entry(iso9660);
2851
+ if (file == NULL) {
2852
+ /*
2853
+ * If directory entries all which are descendant of
2854
+ * rr_moved are still remaining, expose their.
2855
+ */
2856
+ if (iso9660->re_files.first != NULL &&
2857
+ iso9660->rr_moved != NULL &&
2858
+ iso9660->rr_moved->rr_moved_has_re_only)
2859
+ /* Expose "rr_moved" entry. */
2860
+ cache_add_entry(iso9660, iso9660->rr_moved);
2861
+ while ((re = re_get_entry(iso9660)) != NULL) {
2862
+ /* Expose its descendant dirs. */
2863
+ while ((d = rede_get_entry(re)) != NULL)
2864
+ cache_add_entry(iso9660, d);
2865
+ }
2866
+ if (iso9660->cache_files.first != NULL)
2867
+ return (next_cache_entry(a, iso9660, pfile));
2868
+ return (ARCHIVE_EOF);
2869
+ }
2870
+
2871
+ if (file->cl_offset) {
2872
+ struct file_info *first_re = NULL;
2873
+ int nexted_re = 0;
2874
+
2875
+ /*
2876
+ * Find "RE" dir for the current file, which
2877
+ * has "CL" flag.
2878
+ */
2879
+ while ((re = re_get_entry(iso9660))
2880
+ != first_re) {
2881
+ if (first_re == NULL)
2882
+ first_re = re;
2883
+ if (re->offset == file->cl_offset) {
2884
+ re->parent->subdirs--;
2885
+ re->parent = file->parent;
2886
+ re->re = 0;
2887
+ if (re->parent->re_descendant) {
2888
+ nexted_re = 1;
2889
+ re->re_descendant = 1;
2890
+ if (rede_add_entry(re) < 0)
2891
+ goto fatal_rr;
2892
+ /* Move a list of descendants
2893
+ * to a new ancestor. */
2894
+ while ((d = rede_get_entry(
2895
+ re)) != NULL)
2896
+ if (rede_add_entry(d)
2897
+ < 0)
2898
+ goto fatal_rr;
2899
+ break;
2900
+ }
2901
+ /* Replace the current file
2902
+ * with "RE" dir */
2903
+ *pfile = file = re;
2904
+ /* Expose its descendant */
2905
+ while ((d = rede_get_entry(
2906
+ file)) != NULL)
2907
+ cache_add_entry(
2908
+ iso9660, d);
2909
+ break;
2910
+ } else
2911
+ re_add_entry(iso9660, re);
2912
+ }
2913
+ if (nexted_re) {
2914
+ /*
2915
+ * Do not expose this at this time
2916
+ * because we have not gotten its full-path
2917
+ * name yet.
2918
+ */
2919
+ continue;
2920
+ }
2921
+ } else if ((file->mode & AE_IFMT) == AE_IFDIR) {
2922
+ int r;
2923
+
2924
+ /* Read file entries in this dir. */
2925
+ r = read_children(a, file);
2926
+ if (r != ARCHIVE_OK)
2927
+ return (r);
2928
+
2929
+ /*
2930
+ * Handle a special dir of Rockridge extensions,
2931
+ * "rr_moved".
2932
+ */
2933
+ if (file->rr_moved) {
2934
+ /*
2935
+ * If this has only the subdirectories which
2936
+ * have "RE" flags, do not expose at this time.
2937
+ */
2938
+ if (file->rr_moved_has_re_only)
2939
+ continue;
2940
+ /* Otherwise expose "rr_moved" entry. */
2941
+ } else if (file->re) {
2942
+ /*
2943
+ * Do not expose this at this time
2944
+ * because we have not gotten its full-path
2945
+ * name yet.
2946
+ */
2947
+ re_add_entry(iso9660, file);
2948
+ continue;
2949
+ } else if (file->re_descendant) {
2950
+ /*
2951
+ * If the top level "RE" entry of this entry
2952
+ * is not exposed, we, accordingly, should not
2953
+ * expose this entry at this time because
2954
+ * we cannot make its proper full-path name.
2955
+ */
2956
+ if (rede_add_entry(file) == 0)
2957
+ continue;
2958
+ /* Otherwise we can expose this entry because
2959
+ * it seems its top level "RE" has already been
2960
+ * exposed. */
2961
+ }
2962
+ }
2963
+ break;
2964
+ }
2965
+
2966
+ if ((file->mode & AE_IFMT) != AE_IFREG || file->number == -1)
2967
+ return (ARCHIVE_OK);
2968
+
2969
+ count = 0;
2970
+ number = file->number;
2971
+ iso9660->cache_files.first = NULL;
2972
+ iso9660->cache_files.last = &(iso9660->cache_files.first);
2973
+ empty_files.first = NULL;
2974
+ empty_files.last = &empty_files.first;
2975
+ /* Collect files which has the same file serial number.
2976
+ * Peek pending_files so that file which number is different
2977
+ * is not put back. */
2978
+ while (iso9660->pending_files.used > 0 &&
2979
+ (iso9660->pending_files.files[0]->number == -1 ||
2980
+ iso9660->pending_files.files[0]->number == number)) {
2981
+ if (file->number == -1) {
2982
+ /* This file has the same offset
2983
+ * but it's wrong offset which empty files
2984
+ * and symlink files have.
2985
+ * NOTE: This wrong offset was recorded by
2986
+ * old mkisofs utility. If ISO images is
2987
+ * created by latest mkisofs, this does not
2988
+ * happen.
2989
+ */
2990
+ file->next = NULL;
2991
+ *empty_files.last = file;
2992
+ empty_files.last = &(file->next);
2993
+ } else {
2994
+ count++;
2995
+ cache_add_entry(iso9660, file);
2996
+ }
2997
+ file = next_entry(iso9660);
2998
+ }
2999
+
3000
+ if (count == 0) {
3001
+ *pfile = file;
3002
+ return ((file == NULL)?ARCHIVE_EOF:ARCHIVE_OK);
3003
+ }
3004
+ if (file->number == -1) {
3005
+ file->next = NULL;
3006
+ *empty_files.last = file;
3007
+ empty_files.last = &(file->next);
3008
+ } else {
3009
+ count++;
3010
+ cache_add_entry(iso9660, file);
3011
+ }
3012
+
3013
+ if (count > 1) {
3014
+ /* The count is the same as number of hardlink,
3015
+ * so much so that each nlinks of files in cache_file
3016
+ * is overwritten by value of the count.
3017
+ */
3018
+ for (file = iso9660->cache_files.first;
3019
+ file != NULL; file = file->next)
3020
+ file->nlinks = count;
3021
+ }
3022
+ /* If there are empty files, that files are added
3023
+ * to the tail of the cache_files. */
3024
+ if (empty_files.first != NULL) {
3025
+ *iso9660->cache_files.last = empty_files.first;
3026
+ iso9660->cache_files.last = empty_files.last;
3027
+ }
3028
+ *pfile = cache_get_entry(iso9660);
3029
+ return ((*pfile == NULL)?ARCHIVE_EOF:ARCHIVE_OK);
3030
+
3031
+ fatal_rr:
3032
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3033
+ "Failed to connect 'CL' pointer to 'RE' rr_moved pointer of "
3034
+ "Rockridge extensions: current position = %jd, CL offset = %jd",
3035
+ (intmax_t)iso9660->current_position, (intmax_t)file->cl_offset);
3036
+ return (ARCHIVE_FATAL);
3037
+ }
3038
+
3039
+ static inline void
3040
+ re_add_entry(struct iso9660 *iso9660, struct file_info *file)
3041
+ {
3042
+ file->re_next = NULL;
3043
+ *iso9660->re_files.last = file;
3044
+ iso9660->re_files.last = &(file->re_next);
3045
+ }
3046
+
3047
+ static inline struct file_info *
3048
+ re_get_entry(struct iso9660 *iso9660)
3049
+ {
3050
+ struct file_info *file;
3051
+
3052
+ if ((file = iso9660->re_files.first) != NULL) {
3053
+ iso9660->re_files.first = file->re_next;
3054
+ if (iso9660->re_files.first == NULL)
3055
+ iso9660->re_files.last =
3056
+ &(iso9660->re_files.first);
3057
+ }
3058
+ return (file);
3059
+ }
3060
+
3061
+ static inline int
3062
+ rede_add_entry(struct file_info *file)
3063
+ {
3064
+ struct file_info *re;
3065
+
3066
+ /*
3067
+ * Find "RE" entry.
3068
+ */
3069
+ re = file->parent;
3070
+ while (re != NULL && !re->re)
3071
+ re = re->parent;
3072
+ if (re == NULL)
3073
+ return (-1);
3074
+
3075
+ file->re_next = NULL;
3076
+ *re->rede_files.last = file;
3077
+ re->rede_files.last = &(file->re_next);
3078
+ return (0);
3079
+ }
3080
+
3081
+ static inline struct file_info *
3082
+ rede_get_entry(struct file_info *re)
3083
+ {
3084
+ struct file_info *file;
3085
+
3086
+ if ((file = re->rede_files.first) != NULL) {
3087
+ re->rede_files.first = file->re_next;
3088
+ if (re->rede_files.first == NULL)
3089
+ re->rede_files.last =
3090
+ &(re->rede_files.first);
3091
+ }
3092
+ return (file);
3093
+ }
3094
+
3095
+ static inline void
3096
+ cache_add_entry(struct iso9660 *iso9660, struct file_info *file)
3097
+ {
3098
+ file->next = NULL;
3099
+ *iso9660->cache_files.last = file;
3100
+ iso9660->cache_files.last = &(file->next);
3101
+ }
3102
+
3103
+ static inline struct file_info *
3104
+ cache_get_entry(struct iso9660 *iso9660)
3105
+ {
3106
+ struct file_info *file;
3107
+
3108
+ if ((file = iso9660->cache_files.first) != NULL) {
3109
+ iso9660->cache_files.first = file->next;
3110
+ if (iso9660->cache_files.first == NULL)
3111
+ iso9660->cache_files.last =
3112
+ &(iso9660->cache_files.first);
3113
+ }
3114
+ return (file);
3115
+ }
3116
+
3117
+ static int
3118
+ heap_add_entry(struct archive_read *a, struct heap_queue *heap,
3119
+ struct file_info *file, uint64_t key)
3120
+ {
3121
+ uint64_t file_key, parent_key;
3122
+ size_t hole, parent;
3123
+
3124
+ /* Reserve 16 bits for possible key collisions (needed for linked items) */
3125
+ /* For ISO files with more than 65535 entries, reordering will still occur */
3126
+ key <<= 16;
3127
+ key += heap->used & 0xFFFF;
3128
+
3129
+ /* Expand our pending files list as necessary. */
3130
+ if (heap->used >= heap->allocated) {
3131
+ struct file_info **new_pending_files;
3132
+ size_t new_size;
3133
+
3134
+ if (heap->allocated < 1024)
3135
+ new_size = 1024;
3136
+ else if (archive_ckd_mul_size(&new_size, heap->allocated, 2)) {
3137
+ /* Overflow keeps us from growing the list. */
3138
+ archive_set_error(&a->archive,
3139
+ ENOMEM, "Out of memory");
3140
+ return (ARCHIVE_FATAL);
3141
+ }
3142
+ new_pending_files = (struct file_info **)
3143
+ calloc(new_size, sizeof(new_pending_files[0]));
3144
+ if (new_pending_files == NULL) {
3145
+ archive_set_error(&a->archive,
3146
+ ENOMEM, "Out of memory");
3147
+ return (ARCHIVE_FATAL);
3148
+ }
3149
+ if (heap->allocated)
3150
+ memcpy(new_pending_files, heap->files,
3151
+ heap->allocated * sizeof(new_pending_files[0]));
3152
+ free(heap->files);
3153
+ heap->files = new_pending_files;
3154
+ heap->allocated = new_size;
3155
+ }
3156
+
3157
+ file_key = file->key = key;
3158
+
3159
+ /*
3160
+ * Start with hole at end, walk it up tree to find insertion point.
3161
+ */
3162
+ hole = heap->used++;
3163
+ while (hole > 0) {
3164
+ parent = (hole - 1) / 2;
3165
+ parent_key = heap->files[parent]->key;
3166
+ if (file_key >= parent_key) {
3167
+ heap->files[hole] = file;
3168
+ return (ARCHIVE_OK);
3169
+ }
3170
+ /* Move parent into hole <==> move hole up tree. */
3171
+ heap->files[hole] = heap->files[parent];
3172
+ hole = parent;
3173
+ }
3174
+ heap->files[0] = file;
3175
+
3176
+ return (ARCHIVE_OK);
3177
+ }
3178
+
3179
+ static struct file_info *
3180
+ heap_get_entry(struct heap_queue *heap)
3181
+ {
3182
+ uint64_t a_key, b_key, c_key;
3183
+ size_t a, b, c;
3184
+ struct file_info *r, *tmp;
3185
+
3186
+ if (heap->used < 1)
3187
+ return (NULL);
3188
+
3189
+ /*
3190
+ * The first file in the list is the earliest; we'll return this.
3191
+ */
3192
+ r = heap->files[0];
3193
+
3194
+ /*
3195
+ * Move the last item in the heap to the root of the tree
3196
+ */
3197
+ heap->files[0] = heap->files[--(heap->used)];
3198
+
3199
+ /*
3200
+ * Rebalance the heap.
3201
+ */
3202
+ a = 0; /* Starting element and its heap key */
3203
+ a_key = heap->files[a]->key;
3204
+ for (;;) {
3205
+ b = a + a + 1; /* First child */
3206
+ if (b >= heap->used)
3207
+ return (r);
3208
+ b_key = heap->files[b]->key;
3209
+ c = b + 1; /* Use second child if it is smaller. */
3210
+ if (c < heap->used) {
3211
+ c_key = heap->files[c]->key;
3212
+ if (c_key < b_key) {
3213
+ b = c;
3214
+ b_key = c_key;
3215
+ }
3216
+ }
3217
+ if (a_key <= b_key)
3218
+ return (r);
3219
+ tmp = heap->files[a];
3220
+ heap->files[a] = heap->files[b];
3221
+ heap->files[b] = tmp;
3222
+ a = b;
3223
+ }
3224
+ }
3225
+
3226
+ /*
3227
+ * ECMA119/ISO9660 stores multi-byte integers in one of
3228
+ * three different formats:
3229
+ * * Little-endian (specified in section 7.2.1 and 7.3.1)
3230
+ * * Big-endian (specified in section 7.2.2 and 7.3.2)
3231
+ * * Both (specified in section 7.2.3 and 7.3.3)
3232
+ *
3233
+ * For values that follow section 7.2.3 (16-bit) or 7.3.3 (32-bit), we
3234
+ * can check that the little-endian and big-endian forms agree with
3235
+ * each other. This helps us avoid trying to decode files that are
3236
+ * not really ISO images.
3237
+ */
3238
+ static int
3239
+ isValid723Integer(const unsigned char *p) {
3240
+ return (p[0] == p[3] && p[1] == p[2]);
3241
+ }
3242
+
3243
+ static int
3244
+ isValid733Integer(const unsigned char *p)
3245
+ {
3246
+ return (p[0] == p[7]
3247
+ && p[1] == p[6]
3248
+ && p[2] == p[5]
3249
+ && p[3] == p[4]);
3250
+ }
3251
+
3252
+ static int
3253
+ isodate7_valid(const unsigned char *v)
3254
+ {
3255
+ int year = v[0];
3256
+ int month = v[1];
3257
+ int day = v[2];
3258
+ int hour = v[3];
3259
+ int minute = v[4];
3260
+ int second = v[5];
3261
+ int gmt_off = (signed char)v[6];
3262
+
3263
+ /* ECMA-119 9.1.5 "If all seven values are zero, it shall mean
3264
+ * that the date is unspecified" */
3265
+ if (year == 0
3266
+ && month == 0
3267
+ && day == 0
3268
+ && hour == 0
3269
+ && minute == 0
3270
+ && second == 0
3271
+ && gmt_off == 0)
3272
+ return 0;
3273
+ /*
3274
+ * Sanity-test each individual field
3275
+ */
3276
+ /* Year can have any value */
3277
+ /* Month must be 1-12 */
3278
+ if (month < 1 || month > 12)
3279
+ return 0;
3280
+ /* Day must be 1-31 */
3281
+ if (day < 1 || day > 31)
3282
+ return 0;
3283
+ /* Hour must be 0-23 */
3284
+ if (hour > 23)
3285
+ return 0;
3286
+ /* Minute must be 0-59 */
3287
+ if (minute > 59)
3288
+ return 0;
3289
+ /* second must be 0-59 according to ECMA-119 9.1.5 */
3290
+ /* BUT: we should probably allow for the time being in UTC, which
3291
+ allows up to 61 seconds in a minute in certain cases */
3292
+ if (second > 61)
3293
+ return 0;
3294
+ /* Offset from GMT must be -48 to +52 */
3295
+ if (gmt_off < -48 || gmt_off > +52)
3296
+ return 0;
3297
+
3298
+ /* All tests pass, this is OK */
3299
+ return 1;
3300
+ }
3301
+
3302
+ static time_t
3303
+ isodate7(const unsigned char *v)
3304
+ {
3305
+ struct tm tm;
3306
+ int offset;
3307
+ time_t t;
3308
+
3309
+ memset(&tm, 0, sizeof(tm));
3310
+ tm.tm_year = v[0];
3311
+ tm.tm_mon = v[1] - 1;
3312
+ tm.tm_mday = v[2];
3313
+ tm.tm_hour = v[3];
3314
+ tm.tm_min = v[4];
3315
+ tm.tm_sec = v[5];
3316
+ /* v[6] is the signed timezone offset, in 1/4-hour increments. */
3317
+ offset = ((const signed char *)v)[6];
3318
+ if (offset > -48 && offset < 52) {
3319
+ tm.tm_hour -= offset / 4;
3320
+ tm.tm_min -= (offset % 4) * 15;
3321
+ }
3322
+ t = time_from_tm(&tm);
3323
+ if (t == (time_t)-1)
3324
+ return ((time_t)0);
3325
+ return (t);
3326
+ }
3327
+
3328
+ static int
3329
+ isodate17_valid(const unsigned char *v)
3330
+ {
3331
+ /* First 16 bytes are all ASCII digits */
3332
+ for (int i = 0; i < 16; i++) {
3333
+ if (v[i] < '0' || v[i] > '9')
3334
+ return 0;
3335
+ }
3336
+
3337
+ int year = (v[0] - '0') * 1000 + (v[1] - '0') * 100
3338
+ + (v[2] - '0') * 10 + (v[3] - '0');
3339
+ int month = (v[4] - '0') * 10 + (v[5] - '0');
3340
+ int day = (v[6] - '0') * 10 + (v[7] - '0');
3341
+ int hour = (v[8] - '0') * 10 + (v[9] - '0');
3342
+ int minute = (v[10] - '0') * 10 + (v[11] - '0');
3343
+ int second = (v[12] - '0') * 10 + (v[13] - '0');
3344
+ int hundredths = (v[14] - '0') * 10 + (v[15] - '0');
3345
+ int gmt_off = (signed char)v[16];
3346
+
3347
+ if (year == 0 && month == 0 && day == 0
3348
+ && hour == 0 && minute == 0 && second == 0
3349
+ && hundredths == 0 && gmt_off == 0)
3350
+ return 0;
3351
+ /*
3352
+ * Sanity-test each individual field
3353
+ */
3354
+
3355
+ /* Year must be 1900-2300 */
3356
+ /* (Not specified in ECMA-119, but these seem
3357
+ like reasonable limits. */
3358
+ if (year < 1900 || year > 2300)
3359
+ return 0;
3360
+ /* Month must be 1-12 */
3361
+ if (month < 1 || month > 12)
3362
+ return 0;
3363
+ /* Day must be 1-31 */
3364
+ if (day < 1 || day > 31)
3365
+ return 0;
3366
+ /* Hour must be 0-23 */
3367
+ if (hour > 23)
3368
+ return 0;
3369
+ /* Minute must be 0-59 */
3370
+ if (minute > 59)
3371
+ return 0;
3372
+ /* second must be 0-59 according to ECMA-119 9.1.5 */
3373
+ /* BUT: we should probably allow for the time being in UTC, which
3374
+ allows up to 61 seconds in a minute in certain cases */
3375
+ if (second > 61)
3376
+ return 0;
3377
+ /* Hundredths must be 0-99 */
3378
+ if (hundredths > 99)
3379
+ return 0;
3380
+ /* Offset from GMT must be -48 to +52 */
3381
+ if (gmt_off < -48 || gmt_off > +52)
3382
+ return 0;
3383
+
3384
+ /* All tests pass, this is OK */
3385
+ return 1;
3386
+
3387
+ }
3388
+
3389
+ static time_t
3390
+ isodate17(const unsigned char *v)
3391
+ {
3392
+ struct tm tm;
3393
+ int offset;
3394
+ time_t t;
3395
+
3396
+ memset(&tm, 0, sizeof(tm));
3397
+ tm.tm_year = (v[0] - '0') * 1000 + (v[1] - '0') * 100
3398
+ + (v[2] - '0') * 10 + (v[3] - '0')
3399
+ - 1900;
3400
+ tm.tm_mon = (v[4] - '0') * 10 + (v[5] - '0') - 1;
3401
+ tm.tm_mday = (v[6] - '0') * 10 + (v[7] - '0');
3402
+ tm.tm_hour = (v[8] - '0') * 10 + (v[9] - '0');
3403
+ tm.tm_min = (v[10] - '0') * 10 + (v[11] - '0');
3404
+ tm.tm_sec = (v[12] - '0') * 10 + (v[13] - '0');
3405
+ /* v[16] is the signed timezone offset, in 1/4-hour increments. */
3406
+ offset = ((const signed char *)v)[16];
3407
+ if (offset > -48 && offset < 52) {
3408
+ tm.tm_hour -= offset / 4;
3409
+ tm.tm_min -= (offset % 4) * 15;
3410
+ }
3411
+ t = time_from_tm(&tm);
3412
+ if (t == (time_t)-1)
3413
+ return ((time_t)0);
3414
+ return (t);
3415
+ }
3416
+
3417
+ static time_t
3418
+ time_from_tm(struct tm *t)
3419
+ {
3420
+ #if HAVE__MKGMTIME
3421
+ return _mkgmtime(t);
3422
+ #elif HAVE_TIMEGM
3423
+ /* Use platform timegm() if available. */
3424
+ return (timegm(t));
3425
+ #else
3426
+ /* Else use direct calculation using POSIX assumptions. */
3427
+ /* First, fix up tm_yday based on the year/month/day. */
3428
+ if (mktime(t) == (time_t)-1)
3429
+ return ((time_t)-1);
3430
+ /* Then we can compute timegm() from first principles. */
3431
+ return (t->tm_sec
3432
+ + t->tm_min * 60
3433
+ + t->tm_hour * 3600
3434
+ + t->tm_yday * 86400
3435
+ + (t->tm_year - 70) * 31536000
3436
+ + ((t->tm_year - 69) / 4) * 86400
3437
+ - ((t->tm_year - 1) / 100) * 86400
3438
+ + ((t->tm_year + 299) / 400) * 86400);
3439
+ #endif
3440
+ }
3441
+
3442
+ static const char *
3443
+ build_pathname(struct archive_string *as, struct file_info *file, int depth)
3444
+ {
3445
+ // Plain ISO9660 only allows 8 dir levels; if we get
3446
+ // to 1000, then something is very, very wrong.
3447
+ if (depth > 1000) {
3448
+ return NULL;
3449
+ }
3450
+ if (file->parent != NULL && archive_strlen(&file->parent->name) > 0) {
3451
+ if (build_pathname(as, file->parent, depth + 1) == NULL) {
3452
+ return NULL;
3453
+ }
3454
+ archive_strcat(as, "/");
3455
+ }
3456
+ if (archive_strlen(&file->name) == 0)
3457
+ archive_strcat(as, ".");
3458
+ else
3459
+ archive_string_concat(as, &file->name);
3460
+ return (as->s);
3461
+ }
3462
+
3463
+ static int
3464
+ build_pathname_utf16be(unsigned char *p, size_t max, size_t *len,
3465
+ struct file_info *file)
3466
+ {
3467
+ if (file->parent != NULL && file->parent->utf16be_bytes > 0) {
3468
+ if (build_pathname_utf16be(p, max, len, file->parent) != 0)
3469
+ return (-1);
3470
+ if (*len + 2 > max)
3471
+ return (-1);/* Path is too long! */
3472
+ p[*len] = 0;
3473
+ p[*len + 1] = '/';
3474
+ *len += 2;
3475
+ }
3476
+ if (file->utf16be_bytes == 0) {
3477
+ if (*len + 2 > max)
3478
+ return (-1);/* Path is too long! */
3479
+ p[*len] = 0;
3480
+ p[*len + 1] = '.';
3481
+ *len += 2;
3482
+ } else {
3483
+ if (*len + file->utf16be_bytes > max)
3484
+ return (-1);/* Path is too long! */
3485
+ memcpy(p + *len, file->utf16be_name, file->utf16be_bytes);
3486
+ *len += file->utf16be_bytes;
3487
+ }
3488
+ return (0);
3489
+ }
3490
+
3491
+ #if DEBUG
3492
+ static void
3493
+ dump_isodirrec(FILE *out, const unsigned char *isodirrec)
3494
+ {
3495
+ fprintf(out, " l %d,",
3496
+ isodirrec[DR_length_offset]);
3497
+ fprintf(out, " a %d,",
3498
+ isodirrec[DR_ext_attr_length_offset]);
3499
+ fprintf(out, " ext 0x%x,",
3500
+ archive_le32dec(isodirrec + DR_extent_offset));
3501
+ fprintf(out, " s %d,",
3502
+ archive_le32dec(isodirrec + DR_size_offset));
3503
+ fprintf(out, " f 0x%x,",
3504
+ isodirrec[DR_flags_offset]);
3505
+ fprintf(out, " u %d,",
3506
+ isodirrec[DR_file_unit_size_offset]);
3507
+ fprintf(out, " ilv %d,",
3508
+ isodirrec[DR_interleave_offset]);
3509
+ fprintf(out, " seq %d,",
3510
+ archive_le16dec(isodirrec + DR_volume_sequence_number_offset));
3511
+ fprintf(out, " nl %d:",
3512
+ isodirrec[DR_name_len_offset]);
3513
+ fprintf(out, " `%.*s'",
3514
+ isodirrec[DR_name_len_offset], isodirrec + DR_name_offset);
3515
+ }
3516
+ #endif