@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,2249 @@
1
+ /*-
2
+ * Copyright (c) 2003-2007 Tim Kientzle
3
+ * Copyright (c) 2010-2012 Michihiro NAKAJIMA
4
+ * Copyright (c) 2016 Martin Matuska
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
+ #ifdef HAVE_STDLIB_H
34
+ #include <stdlib.h>
35
+ #endif
36
+ #ifdef HAVE_STRING_H
37
+ #include <string.h>
38
+ #endif
39
+
40
+ #include "archive.h"
41
+ #include "archive_entry.h"
42
+ #include "archive_entry_locale.h"
43
+ #include "archive_integer.h"
44
+ #include "archive_private.h"
45
+ #include "archive_write_private.h"
46
+ #include "archive_write_set_format_private.h"
47
+
48
+ #define PAX_MAX_ALIGN (1U << 20)
49
+
50
+ struct sparse_block {
51
+ struct sparse_block *next;
52
+ int is_hole;
53
+ uint64_t offset;
54
+ uint64_t remaining;
55
+ };
56
+
57
+ struct pax {
58
+ uint64_t entry_bytes_remaining;
59
+ uint64_t entry_padding;
60
+ struct archive_string l_url_encoded_name;
61
+ struct archive_string pax_header;
62
+ struct archive_string sparse_map;
63
+ size_t sparse_map_padding;
64
+ struct sparse_block *sparse_list;
65
+ struct sparse_block *sparse_tail;
66
+ struct archive_string_conv *sconv_utf8;
67
+ int opt_binary;
68
+
69
+ /* If non-zero, align regular file data to this many bytes in the
70
+ * uncompressed stream (power of two, multiple of 512). */
71
+ size_t align;
72
+
73
+ unsigned flags;
74
+ #define WRITE_SCHILY_XATTR (1 << 0)
75
+ #define WRITE_LIBARCHIVE_XATTR (1 << 1)
76
+ };
77
+
78
+ static void add_pax_attr(struct archive_string *, const char *key,
79
+ const char *value);
80
+ static void add_pax_attr_binary(struct archive_string *,
81
+ const char *key,
82
+ const char *value, size_t value_len);
83
+ static void add_pax_attr_int(struct archive_string *,
84
+ const char *key, int64_t value);
85
+ static void add_pax_attr_time(struct archive_string *,
86
+ const char *key, int64_t sec,
87
+ unsigned long nanos);
88
+ static size_t add_pax_padding_prefix(struct archive_string *,
89
+ size_t cur_len, size_t target_len);
90
+ static int add_pax_acl(struct archive_write *,
91
+ struct archive_entry *, struct pax *, int);
92
+ static ssize_t archive_write_pax_data(struct archive_write *,
93
+ const void *, size_t);
94
+ static int archive_write_pax_close(struct archive_write *);
95
+ static int archive_write_pax_free(struct archive_write *);
96
+ static int archive_write_pax_finish_entry(struct archive_write *);
97
+ static int archive_write_pax_header(struct archive_write *,
98
+ struct archive_entry *);
99
+ static int archive_write_pax_options(struct archive_write *,
100
+ const char *, const char *);
101
+ static int write_pax_padding(struct archive_write *, size_t);
102
+ static char *base64_encode(const char *src, size_t len);
103
+ static char *build_gnu_sparse_name(char *dest, const char *src);
104
+ static char *build_pax_attribute_name(char *dest, const char *src);
105
+ static char *build_ustar_entry_name(char *dest, const char *src,
106
+ size_t src_length, const char *insert);
107
+ static char *format_int(char *dest, int64_t);
108
+ static int has_non_ASCII(const char *);
109
+ static void sparse_list_clear(struct pax *);
110
+ static int sparse_list_add(struct pax *, int64_t, int64_t);
111
+ static char *url_encode(const char *in);
112
+ static time_t get_ustar_max_mtime(void);
113
+
114
+ /*
115
+ * Set output format to 'restricted pax' format.
116
+ *
117
+ * This is the same as normal 'pax', but tries to suppress
118
+ * the pax header whenever possible. This is the default for
119
+ * bsdtar, for instance.
120
+ */
121
+ int
122
+ archive_write_set_format_pax_restricted(struct archive *_a)
123
+ {
124
+ struct archive_write *a = (struct archive_write *)_a;
125
+ int r;
126
+
127
+ archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
128
+ ARCHIVE_STATE_NEW, "archive_write_set_format_pax_restricted");
129
+
130
+ r = archive_write_set_format_pax(&a->archive);
131
+ a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
132
+ a->archive.archive_format_name = "restricted POSIX pax interchange";
133
+ return (r);
134
+ }
135
+
136
+ /*
137
+ * Set output format to 'pax' format.
138
+ */
139
+ int
140
+ archive_write_set_format_pax(struct archive *_a)
141
+ {
142
+ struct archive_write *a = (struct archive_write *)_a;
143
+ struct pax *pax;
144
+
145
+ archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
146
+ ARCHIVE_STATE_NEW, "archive_write_set_format_pax");
147
+
148
+ (void)__archive_write_unregister_format(a);
149
+
150
+ pax = calloc(1, sizeof(*pax));
151
+ if (pax == NULL) {
152
+ archive_set_error(&a->archive, ENOMEM,
153
+ "Can't allocate pax data");
154
+ return (ARCHIVE_FATAL);
155
+ }
156
+ pax->flags = WRITE_LIBARCHIVE_XATTR | WRITE_SCHILY_XATTR;
157
+
158
+ a->format_data = pax;
159
+ a->format_name = "pax";
160
+ a->format_options = archive_write_pax_options;
161
+ a->format_write_header = archive_write_pax_header;
162
+ a->format_write_data = archive_write_pax_data;
163
+ a->format_close = archive_write_pax_close;
164
+ a->format_free = archive_write_pax_free;
165
+ a->format_finish_entry = archive_write_pax_finish_entry;
166
+ a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
167
+ a->archive.archive_format_name = "POSIX pax interchange";
168
+ return (ARCHIVE_OK);
169
+ }
170
+
171
+ static int
172
+ archive_write_pax_options(struct archive_write *a, const char *key,
173
+ const char *val)
174
+ {
175
+ struct pax *pax = a->format_data;
176
+ int ret = ARCHIVE_FAILED;
177
+
178
+ if (strcmp(key, "hdrcharset") == 0) {
179
+ /*
180
+ * The character-set we can use are defined in
181
+ * IEEE Std 1003.1-2001
182
+ */
183
+ if (val == NULL || val[0] == 0)
184
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
185
+ "pax: hdrcharset option needs a character-set name");
186
+ else if (strcmp(val, "BINARY") == 0 ||
187
+ strcmp(val, "binary") == 0) {
188
+ /*
189
+ * Specify binary mode. We will not convert
190
+ * filenames, uname and gname to any charsets.
191
+ */
192
+ pax->opt_binary = 1;
193
+ ret = ARCHIVE_OK;
194
+ } else if (strcmp(val, "UTF-8") == 0) {
195
+ /*
196
+ * Specify UTF-8 character-set to be used for
197
+ * filenames. This is almost the test that
198
+ * running platform supports the string conversion.
199
+ * Especially libarchive_test needs this trick for
200
+ * its test.
201
+ */
202
+ pax->sconv_utf8 = archive_string_conversion_to_charset(
203
+ &(a->archive), "UTF-8", 0);
204
+ if (pax->sconv_utf8 == NULL)
205
+ ret = ARCHIVE_FATAL;
206
+ else
207
+ ret = ARCHIVE_OK;
208
+ } else
209
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
210
+ "pax: invalid charset name");
211
+ return (ret);
212
+ } else if (strcmp(key, "xattrheader") == 0) {
213
+ if (val == NULL || val[0] == 0) {
214
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
215
+ "pax: xattrheader requires a value");
216
+ } else if (strcmp(val, "ALL") == 0 ||
217
+ strcmp(val, "all") == 0) {
218
+ pax->flags |= WRITE_LIBARCHIVE_XATTR | WRITE_SCHILY_XATTR;
219
+ ret = ARCHIVE_OK;
220
+ } else if (strcmp(val, "SCHILY") == 0 ||
221
+ strcmp(val, "schily") == 0) {
222
+ pax->flags |= WRITE_SCHILY_XATTR;
223
+ pax->flags &= ~WRITE_LIBARCHIVE_XATTR;
224
+ ret = ARCHIVE_OK;
225
+ } else if (strcmp(val, "LIBARCHIVE") == 0 ||
226
+ strcmp(val, "libarchive") == 0) {
227
+ pax->flags |= WRITE_LIBARCHIVE_XATTR;
228
+ pax->flags &= ~WRITE_SCHILY_XATTR;
229
+ ret = ARCHIVE_OK;
230
+ } else
231
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
232
+ "pax: invalid xattr header name");
233
+ return (ret);
234
+ } else if (strcmp(key, "align") == 0) {
235
+ unsigned long v;
236
+ char *end;
237
+
238
+ if (val == NULL || val[0] == 0) {
239
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
240
+ "pax: align option needs a value");
241
+ return (ARCHIVE_FAILED);
242
+ }
243
+ v = strtoul(val, &end, 10);
244
+ if (*end != '\0' || v == 0 || (v % 512) != 0 ||
245
+ (v & (v - 1)) != 0 || v > PAX_MAX_ALIGN) {
246
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
247
+ "pax: align must be a power of two, a multiple "
248
+ "of 512, and no larger than 1 MiB");
249
+ return (ARCHIVE_FAILED);
250
+ }
251
+ pax->align = (size_t)v;
252
+ return (ARCHIVE_OK);
253
+ }
254
+
255
+ /* Note: The "warn" return is just to inform the options
256
+ * supervisor that we didn't handle it. It will generate
257
+ * a suitable error if no one used this option. */
258
+ return (ARCHIVE_WARN);
259
+ }
260
+
261
+ /*
262
+ * Note: This code assumes that 'nanos' has the same sign as 'sec',
263
+ * which implies that sec=-1, nanos=200000000 represents -1.2 seconds
264
+ * and not -0.8 seconds. This is a pretty pedantic point, as we're
265
+ * unlikely to encounter many real files created before Jan 1, 1970,
266
+ * much less ones with timestamps recorded to sub-second resolution.
267
+ */
268
+ static void
269
+ add_pax_attr_time(struct archive_string *as, const char *key,
270
+ int64_t sec, unsigned long nanos)
271
+ {
272
+ int digit, i;
273
+ char *t;
274
+ /*
275
+ * Note that each byte contributes fewer than 3 base-10
276
+ * digits, so this will always be big enough.
277
+ */
278
+ char tmp[1 + 3*sizeof(sec) + 1 + 3*sizeof(nanos)];
279
+
280
+ tmp[sizeof(tmp) - 1] = 0;
281
+ t = tmp + sizeof(tmp) - 1;
282
+
283
+ /* Skip trailing zeros in the fractional part. */
284
+ for (digit = 0, i = 10; i > 0 && digit == 0; i--) {
285
+ digit = nanos % 10;
286
+ nanos /= 10;
287
+ }
288
+
289
+ /* Only format the fraction if it's non-zero. */
290
+ if (i > 0) {
291
+ while (i > 0) {
292
+ *--t = "0123456789"[digit];
293
+ digit = nanos % 10;
294
+ nanos /= 10;
295
+ i--;
296
+ }
297
+ *--t = '.';
298
+ }
299
+ t = format_int(t, sec);
300
+
301
+ add_pax_attr(as, key, t);
302
+ }
303
+
304
+ static char *
305
+ format_int(char *t, int64_t i)
306
+ {
307
+ uint64_t ui;
308
+
309
+ if (i < 0)
310
+ ui = (i == INT64_MIN) ? (uint64_t)(INT64_MAX) + 1 : (uint64_t)(-i);
311
+ else
312
+ ui = i;
313
+
314
+ do {
315
+ *--t = "0123456789"[ui % 10];
316
+ } while (ui /= 10);
317
+ if (i < 0)
318
+ *--t = '-';
319
+ return (t);
320
+ }
321
+
322
+ static void
323
+ add_pax_attr_int(struct archive_string *as, const char *key, int64_t value)
324
+ {
325
+ char tmp[1 + 3 * sizeof(value)];
326
+
327
+ tmp[sizeof(tmp) - 1] = 0;
328
+ add_pax_attr(as, key, format_int(tmp + sizeof(tmp) - 1, value));
329
+ }
330
+
331
+ /*
332
+ * Add a key/value attribute to the pax header. This function handles
333
+ * the length field and various other syntactic requirements.
334
+ */
335
+ static void
336
+ add_pax_attr(struct archive_string *as, const char *key, const char *value)
337
+ {
338
+ add_pax_attr_binary(as, key, value, strlen(value));
339
+ }
340
+
341
+ /*
342
+ * Add a key/value attribute to the pax header. This function handles
343
+ * binary values.
344
+ */
345
+ static void
346
+ add_pax_attr_binary(struct archive_string *as, const char *key,
347
+ const char *value, size_t value_len)
348
+ {
349
+ int digits, i, len, next_ten;
350
+ char tmp[1 + 3 * sizeof(int)]; /* < 3 base-10 digits per byte */
351
+
352
+ /*-
353
+ * PAX attributes have the following layout:
354
+ * <len> <space> <key> <=> <value> <nl>
355
+ */
356
+ len = 1 + (int)strlen(key) + 1 + (int)value_len + 1;
357
+
358
+ /*
359
+ * The <len> field includes the length of the <len> field, so
360
+ * computing the correct length is tricky. I start by
361
+ * counting the number of base-10 digits in 'len' and
362
+ * computing the next higher power of 10.
363
+ */
364
+ next_ten = 1;
365
+ digits = 0;
366
+ i = len;
367
+ while (i > 0) {
368
+ i = i / 10;
369
+ digits++;
370
+ next_ten = next_ten * 10;
371
+ }
372
+ /*
373
+ * For example, if string without the length field is 99
374
+ * chars, then adding the 2 digit length "99" will force the
375
+ * total length past 100, requiring an extra digit. The next
376
+ * statement adjusts for this effect.
377
+ */
378
+ if (len + digits >= next_ten)
379
+ digits++;
380
+
381
+ /* Now, we have the right length so we can build the line. */
382
+ tmp[sizeof(tmp) - 1] = 0; /* Null-terminate the work area. */
383
+ archive_strcat(as, format_int(tmp + sizeof(tmp) - 1, len + digits));
384
+ archive_strappend_char(as, ' ');
385
+ archive_strcat(as, key);
386
+ archive_strappend_char(as, '=');
387
+ archive_array_append(as, value, value_len);
388
+ archive_strappend_char(as, '\n');
389
+ }
390
+
391
+ /*
392
+ * Append the length/key prefix of an ignorable "LIBARCHIVE.pad" record whose
393
+ * full length rounds the pax body up to 'target_len'. Return the number of
394
+ * value/newline bytes that must be emitted after the in-memory header.
395
+ */
396
+ static size_t
397
+ add_pax_padding_prefix(struct archive_string *as, size_t cur_len,
398
+ size_t target_len)
399
+ {
400
+ static const char key[] = "LIBARCHIVE.pad";
401
+ char tmp[1 + 3 * sizeof(int64_t)];
402
+ char *length;
403
+ size_t prefix_len, record_len;
404
+
405
+ if (target_len <= cur_len)
406
+ return (0);
407
+ record_len = target_len - cur_len;
408
+ tmp[sizeof(tmp) - 1] = 0;
409
+ length = format_int(tmp + sizeof(tmp) - 1, (int64_t)record_len);
410
+ prefix_len = strlen(length) + 1 + sizeof(key) - 1 + 1;
411
+ if (record_len <= prefix_len)
412
+ return (0); /* not enough room; leave unaligned */
413
+
414
+ archive_strcat(as, length);
415
+ archive_strappend_char(as, ' ');
416
+ archive_strcat(as, key);
417
+ archive_strappend_char(as, '=');
418
+ return (record_len - prefix_len);
419
+ }
420
+
421
+ /* Emit a deferred pax padding value followed by its record newline. */
422
+ static int
423
+ write_pax_padding(struct archive_write *a, size_t length)
424
+ {
425
+ char padding[1024];
426
+
427
+ if (length == 0)
428
+ return (ARCHIVE_OK);
429
+ memset(padding, 'X', sizeof(padding));
430
+ while (length > 1) {
431
+ size_t to_write = length - 1;
432
+ int r;
433
+
434
+ if (to_write > sizeof(padding))
435
+ to_write = sizeof(padding);
436
+ r = __archive_write_output(a, padding, to_write);
437
+ if (r != ARCHIVE_OK)
438
+ return (r);
439
+ length -= to_write;
440
+ }
441
+ return (__archive_write_output(a, "\n", 1));
442
+ }
443
+
444
+ /*
445
+ * For the pax "align" option: given entry offset 'off' (a multiple of 512) and
446
+ * current extended-header body length 'l0', return the body length needed so
447
+ * the file data (after the x header) lands on 'align', or 'l0' if already
448
+ * aligned.
449
+ */
450
+ static size_t
451
+ pax_align_body_len(uint64_t off, size_t l0, size_t align)
452
+ {
453
+ uint64_t amask = (uint64_t)align - 1;
454
+ /* Data offset with no extra padding (no x header when l0 == 0). */
455
+ uint64_t natural_off = (l0 == 0) ? off + 512
456
+ : off + 1024 + (((uint64_t)l0 + 511) & ~(uint64_t)511);
457
+
458
+ if ((natural_off & amask) == 0)
459
+ return (l0);
460
+
461
+ /* Grow the body (past l0 plus pad-record room) to land data on align. */
462
+ uint64_t base = off + 1024;
463
+ return ((size_t)(((base + l0 + 64 + amask) & ~amask) - base));
464
+ }
465
+
466
+ static void
467
+ archive_write_pax_header_xattr(struct pax *pax, const char *encoded_name,
468
+ const void *value, size_t value_len)
469
+ {
470
+ struct archive_string s;
471
+ char *encoded_value;
472
+
473
+ if (encoded_name == NULL)
474
+ return;
475
+
476
+ if (pax->flags & WRITE_LIBARCHIVE_XATTR) {
477
+ encoded_value = base64_encode((const char *)value, value_len);
478
+ if (encoded_value != NULL) {
479
+ archive_string_init(&s);
480
+ archive_strcpy(&s, "LIBARCHIVE.xattr.");
481
+ archive_strcat(&s, encoded_name);
482
+ add_pax_attr(&(pax->pax_header), s.s, encoded_value);
483
+ archive_string_free(&s);
484
+ }
485
+ free(encoded_value);
486
+ }
487
+ if (pax->flags & WRITE_SCHILY_XATTR) {
488
+ archive_string_init(&s);
489
+ archive_strcpy(&s, "SCHILY.xattr.");
490
+ archive_strcat(&s, encoded_name);
491
+ add_pax_attr_binary(&(pax->pax_header), s.s, value, value_len);
492
+ archive_string_free(&s);
493
+ }
494
+ }
495
+
496
+ static int
497
+ archive_write_pax_header_xattrs(struct archive_write *a,
498
+ struct pax *pax, struct archive_entry *entry)
499
+ {
500
+ int i = archive_entry_xattr_reset(entry);
501
+
502
+ while (i--) {
503
+ const char *name;
504
+ const void *value;
505
+ char *url_encoded_name = NULL, *encoded_name = NULL;
506
+ size_t size;
507
+ int r;
508
+
509
+ archive_entry_xattr_next(entry, &name, &value, &size);
510
+ url_encoded_name = url_encode(name);
511
+ if (url_encoded_name == NULL)
512
+ goto malloc_error;
513
+ else {
514
+ /* Convert narrow-character to UTF-8. */
515
+ r = archive_strcpy_l(&(pax->l_url_encoded_name),
516
+ url_encoded_name, pax->sconv_utf8);
517
+ free(url_encoded_name); /* Done with this. */
518
+ if (r == 0)
519
+ encoded_name = pax->l_url_encoded_name.s;
520
+ else if (r == -1)
521
+ goto malloc_error;
522
+ else {
523
+ archive_set_error(&a->archive,
524
+ ARCHIVE_ERRNO_MISC,
525
+ "Error encoding pax extended attribute");
526
+ return (ARCHIVE_FAILED);
527
+ }
528
+ }
529
+
530
+ archive_write_pax_header_xattr(pax, encoded_name,
531
+ value, size);
532
+
533
+ }
534
+ return (ARCHIVE_OK);
535
+ malloc_error:
536
+ archive_set_error(&a->archive, ENOMEM, "Can't allocate memory");
537
+ return (ARCHIVE_FATAL);
538
+ }
539
+
540
+ static int
541
+ get_entry_hardlink(struct archive_write *a, struct archive_entry *entry,
542
+ const char **name, size_t *length, struct archive_string_conv *sc)
543
+ {
544
+ int r;
545
+
546
+ r = archive_entry_hardlink_l(entry, name, length, sc);
547
+ if (r != 0) {
548
+ if (errno == ENOMEM) {
549
+ archive_set_error(&a->archive, ENOMEM,
550
+ "Can't allocate memory for Linkname");
551
+ return (ARCHIVE_FATAL);
552
+ }
553
+ return (ARCHIVE_WARN);
554
+ }
555
+ return (ARCHIVE_OK);
556
+ }
557
+
558
+ static int
559
+ get_entry_pathname(struct archive_write *a, struct archive_entry *entry,
560
+ const char **name, size_t *length, struct archive_string_conv *sc)
561
+ {
562
+ int r;
563
+
564
+ r = archive_entry_pathname_l(entry, name, length, sc);
565
+ if (r != 0) {
566
+ if (errno == ENOMEM) {
567
+ archive_set_error(&a->archive, ENOMEM,
568
+ "Can't allocate memory for Pathname");
569
+ return (ARCHIVE_FATAL);
570
+ }
571
+ return (ARCHIVE_WARN);
572
+ }
573
+ return (ARCHIVE_OK);
574
+ }
575
+
576
+ static int
577
+ get_entry_uname(struct archive_write *a, struct archive_entry *entry,
578
+ const char **name, size_t *length, struct archive_string_conv *sc)
579
+ {
580
+ int r;
581
+
582
+ r = archive_entry_uname_l(entry, name, length, sc);
583
+ if (r != 0) {
584
+ if (errno == ENOMEM) {
585
+ archive_set_error(&a->archive, ENOMEM,
586
+ "Can't allocate memory for Uname");
587
+ return (ARCHIVE_FATAL);
588
+ }
589
+ return (ARCHIVE_WARN);
590
+ }
591
+ return (ARCHIVE_OK);
592
+ }
593
+
594
+ static int
595
+ get_entry_gname(struct archive_write *a, struct archive_entry *entry,
596
+ const char **name, size_t *length, struct archive_string_conv *sc)
597
+ {
598
+ int r;
599
+
600
+ r = archive_entry_gname_l(entry, name, length, sc);
601
+ if (r != 0) {
602
+ if (errno == ENOMEM) {
603
+ archive_set_error(&a->archive, ENOMEM,
604
+ "Can't allocate memory for Gname");
605
+ return (ARCHIVE_FATAL);
606
+ }
607
+ return (ARCHIVE_WARN);
608
+ }
609
+ return (ARCHIVE_OK);
610
+ }
611
+
612
+ static int
613
+ get_entry_symlink(struct archive_write *a, struct archive_entry *entry,
614
+ const char **name, size_t *length, struct archive_string_conv *sc)
615
+ {
616
+ int r;
617
+
618
+ r = archive_entry_symlink_l(entry, name, length, sc);
619
+ if (r != 0) {
620
+ if (errno == ENOMEM) {
621
+ archive_set_error(&a->archive, ENOMEM,
622
+ "Can't allocate memory for Linkname");
623
+ return (ARCHIVE_FATAL);
624
+ }
625
+ return (ARCHIVE_WARN);
626
+ }
627
+ return (ARCHIVE_OK);
628
+ }
629
+
630
+ /* Add ACL to pax header */
631
+ static int
632
+ add_pax_acl(struct archive_write *a,
633
+ struct archive_entry *entry, struct pax *pax, int flags)
634
+ {
635
+ char *p;
636
+ const char *attr;
637
+ int acl_types;
638
+
639
+ acl_types = archive_entry_acl_types(entry);
640
+
641
+ if ((acl_types & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0)
642
+ attr = "SCHILY.acl.ace";
643
+ else if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
644
+ attr = "SCHILY.acl.access";
645
+ else if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0)
646
+ attr = "SCHILY.acl.default";
647
+ else
648
+ return (ARCHIVE_FATAL);
649
+
650
+ p = archive_entry_acl_to_text_l(entry, NULL, flags, pax->sconv_utf8);
651
+ if (p == NULL) {
652
+ if (errno == ENOMEM) {
653
+ archive_set_error(&a->archive, ENOMEM, "%s %s",
654
+ "Can't allocate memory for ", attr);
655
+ return (ARCHIVE_FATAL);
656
+ }
657
+ archive_set_error(&a->archive,
658
+ ARCHIVE_ERRNO_FILE_FORMAT, "%s %s %s",
659
+ "Can't translate ", attr, " to UTF-8");
660
+ return(ARCHIVE_WARN);
661
+ }
662
+
663
+ if (*p != '\0') {
664
+ add_pax_attr(&(pax->pax_header),
665
+ attr, p);
666
+ }
667
+ free(p);
668
+ return(ARCHIVE_OK);
669
+ }
670
+
671
+ /*
672
+ * TODO: Consider adding 'comment' and 'charset' fields to
673
+ * archive_entry so that clients can specify them. Also, consider
674
+ * adding generic key/value tags so clients can add arbitrary
675
+ * key/value data.
676
+ *
677
+ * TODO: Break up this 700-line function!!!! Yowza!
678
+ */
679
+ static int
680
+ archive_write_pax_header(struct archive_write *a,
681
+ struct archive_entry *entry_original)
682
+ {
683
+ struct pax *pax = a->format_data;
684
+ struct archive_entry *entry_main;
685
+ const char *p;
686
+ const char *suffix;
687
+ int need_extension, r, ret;
688
+ int acl_types;
689
+ int sparse_count;
690
+ uint64_t sparse_total, real_size;
691
+ const char *hardlink;
692
+ const char *path = NULL, *linkpath = NULL;
693
+ const char *uname = NULL, *gname = NULL;
694
+ const void *mac_metadata;
695
+ size_t mac_metadata_size;
696
+ struct archive_string_conv *sconv;
697
+ size_t hardlink_length, path_length, linkpath_length;
698
+ size_t pax_header_padding = 0;
699
+ size_t uname_length, gname_length;
700
+
701
+ char paxbuff[512];
702
+ char ustarbuff[512];
703
+ char ustar_entry_name[256];
704
+ char pax_entry_name[256];
705
+ char gnu_sparse_name[256];
706
+ struct archive_string entry_name;
707
+
708
+ ret = ARCHIVE_OK;
709
+ need_extension = 0;
710
+
711
+ const time_t ustar_max_mtime = get_ustar_max_mtime();
712
+
713
+ /* Sanity check. */
714
+ #if defined(_WIN32) && !defined(__CYGWIN__)
715
+ /* NOTE: If the caller supplied a pathname that fails WCS conversion (e.g.
716
+ * if it is invalid UTF-8), we are expected to return ARCHIVE_WARN later on
717
+ * in execution, hence the check for both pointers */
718
+ if ((archive_entry_pathname_w(entry_original) == NULL) &&
719
+ (archive_entry_pathname(entry_original) == NULL)) {
720
+ #else
721
+ if (archive_entry_pathname(entry_original) == NULL) {
722
+ #endif
723
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
724
+ "Can't record entry in tar file without pathname");
725
+ return (ARCHIVE_FAILED);
726
+ }
727
+
728
+ /*
729
+ * Choose a header encoding.
730
+ */
731
+ if (pax->opt_binary)
732
+ sconv = NULL;/* Binary mode. */
733
+ else {
734
+ /* Header encoding is UTF-8. */
735
+ if (pax->sconv_utf8 == NULL) {
736
+ /* Initialize the string conversion object
737
+ * we must need */
738
+ pax->sconv_utf8 = archive_string_conversion_to_charset(
739
+ &(a->archive), "UTF-8", 1);
740
+ if (pax->sconv_utf8 == NULL)
741
+ /* Couldn't allocate memory */
742
+ return (ARCHIVE_FAILED);
743
+ }
744
+ sconv = pax->sconv_utf8;
745
+ }
746
+
747
+ r = get_entry_hardlink(a, entry_original, &hardlink,
748
+ &hardlink_length, sconv);
749
+ if (r == ARCHIVE_FATAL)
750
+ return (r);
751
+ else if (r != ARCHIVE_OK) {
752
+ r = get_entry_hardlink(a, entry_original, &hardlink,
753
+ &hardlink_length, NULL);
754
+ if (r == ARCHIVE_FATAL)
755
+ return (r);
756
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
757
+ "Can't translate linkname '%s' to %s", hardlink,
758
+ archive_string_conversion_charset_name(sconv));
759
+ ret = ARCHIVE_WARN;
760
+ sconv = NULL;/* The header charset switches to binary mode. */
761
+ }
762
+
763
+ /* Make sure this is a type of entry that we can handle here */
764
+ if (hardlink == NULL) {
765
+ switch (archive_entry_filetype(entry_original)) {
766
+ case AE_IFBLK:
767
+ case AE_IFCHR:
768
+ case AE_IFIFO:
769
+ case AE_IFLNK:
770
+ case AE_IFREG:
771
+ break;
772
+ case AE_IFDIR:
773
+ {
774
+ /*
775
+ * Ensure a trailing '/'. Modify the original
776
+ * entry so the client sees the change.
777
+ */
778
+ #if defined(_WIN32) && !defined(__CYGWIN__)
779
+ const wchar_t *wp;
780
+
781
+ wp = archive_entry_pathname_w(entry_original);
782
+ if (wp != NULL && wp[0] != L'\0' &&
783
+ wp[wcslen(wp) - 1] != L'/') {
784
+ struct archive_wstring ws;
785
+
786
+ archive_string_init(&ws);
787
+ path_length = wcslen(wp);
788
+ if (archive_wstring_ensure(&ws,
789
+ path_length + 2) == NULL) {
790
+ archive_set_error(&a->archive, ENOMEM,
791
+ "Can't allocate pax data");
792
+ archive_wstring_free(&ws);
793
+ return(ARCHIVE_FATAL);
794
+ }
795
+ /* Should we keep '\' ? */
796
+ if (wp[path_length -1] == L'\\')
797
+ path_length--;
798
+ archive_wstrncpy(&ws, wp, path_length);
799
+ archive_wstrappend_wchar(&ws, L'/');
800
+ archive_entry_copy_pathname_w(
801
+ entry_original, ws.s);
802
+ archive_wstring_free(&ws);
803
+ p = NULL;
804
+ } else
805
+ #endif
806
+ p = archive_entry_pathname(entry_original);
807
+ /*
808
+ * On Windows, this is a backup operation just in
809
+ * case getting WCS failed. On POSIX, this is a
810
+ * normal operation.
811
+ */
812
+ if (p != NULL && p[0] != '\0' && p[strlen(p) - 1] != '/') {
813
+ struct archive_string as;
814
+
815
+ archive_string_init(&as);
816
+ path_length = strlen(p);
817
+ if (archive_string_ensure(&as,
818
+ path_length + 2) == NULL) {
819
+ archive_set_error(&a->archive, ENOMEM,
820
+ "Can't allocate pax data");
821
+ archive_string_free(&as);
822
+ return(ARCHIVE_FATAL);
823
+ }
824
+ #if defined(_WIN32) && !defined(__CYGWIN__)
825
+ /* NOTE: This might break the pathname
826
+ * if the current code page is CP932 and
827
+ * the pathname includes a character '\'
828
+ * as a part of its multibyte pathname. */
829
+ if (p[strlen(p) -1] == '\\')
830
+ path_length--;
831
+ else
832
+ #endif
833
+ archive_strncpy(&as, p, path_length);
834
+ archive_strappend_char(&as, '/');
835
+ archive_entry_copy_pathname(
836
+ entry_original, as.s);
837
+ archive_string_free(&as);
838
+ }
839
+ break;
840
+ }
841
+ default: /* AE_IFSOCK and unknown */
842
+ __archive_write_entry_filetype_unsupported(
843
+ &a->archive, entry_original, "pax");
844
+ return (ARCHIVE_FAILED);
845
+ }
846
+ }
847
+
848
+ /*
849
+ * If Mac OS metadata blob is here, recurse to write that
850
+ * as a separate entry. This is really a pretty poor design:
851
+ * In particular, it doubles the overhead for long filenames.
852
+ * TODO: Help Apple folks design something better and figure
853
+ * out how to transition from this legacy format.
854
+ *
855
+ * Note that this code is present on every platform; clients
856
+ * on non-Mac are unlikely to ever provide this data, but
857
+ * applications that copy entries from one archive to another
858
+ * should not lose data just because the local filesystem
859
+ * can't store it.
860
+ */
861
+ mac_metadata =
862
+ archive_entry_mac_metadata(entry_original, &mac_metadata_size);
863
+ if (mac_metadata != NULL) {
864
+ const char *oname;
865
+ char *name, *bname;
866
+ size_t name_length;
867
+ struct archive_entry *extra = archive_entry_new2(&a->archive);
868
+
869
+ oname = archive_entry_pathname(entry_original);
870
+ name_length = strlen(oname);
871
+ name = malloc(name_length + 3);
872
+ if (name == NULL || extra == NULL) {
873
+ /* XXX error message */
874
+ archive_entry_free(extra);
875
+ free(name);
876
+ return (ARCHIVE_FAILED);
877
+ }
878
+ strcpy(name, oname);
879
+ /* Find last '/'; strip trailing '/' characters */
880
+ bname = strrchr(name, '/');
881
+ while (bname != NULL && bname[1] == '\0') {
882
+ *bname = '\0';
883
+ bname = strrchr(name, '/');
884
+ }
885
+ if (bname == NULL) {
886
+ memmove(name + 2, name, name_length + 1);
887
+ memmove(name, "._", 2);
888
+ } else {
889
+ bname += 1;
890
+ memmove(bname + 2, bname, strlen(bname) + 1);
891
+ memmove(bname, "._", 2);
892
+ }
893
+ archive_entry_copy_pathname(extra, name);
894
+ free(name);
895
+
896
+ archive_entry_set_size(extra, mac_metadata_size);
897
+ archive_entry_set_filetype(extra, AE_IFREG);
898
+ archive_entry_set_perm(extra,
899
+ archive_entry_perm(entry_original));
900
+ archive_entry_set_mtime(extra,
901
+ archive_entry_mtime(entry_original),
902
+ archive_entry_mtime_nsec(entry_original));
903
+ archive_entry_set_gid(extra,
904
+ archive_entry_gid(entry_original));
905
+ archive_entry_set_gname(extra,
906
+ archive_entry_gname(entry_original));
907
+ archive_entry_set_uid(extra,
908
+ archive_entry_uid(entry_original));
909
+ archive_entry_set_uname(extra,
910
+ archive_entry_uname(entry_original));
911
+
912
+ /* Recurse to write the special copyfile entry. */
913
+ r = archive_write_pax_header(a, extra);
914
+ archive_entry_free(extra);
915
+ if (r < ARCHIVE_WARN)
916
+ return (r);
917
+ if (r < ret)
918
+ ret = r;
919
+ r = (int)archive_write_pax_data(a, mac_metadata,
920
+ mac_metadata_size);
921
+ if (r < ARCHIVE_WARN)
922
+ return (r);
923
+ if (r < ret)
924
+ ret = r;
925
+ r = archive_write_pax_finish_entry(a);
926
+ if (r < ARCHIVE_WARN)
927
+ return (r);
928
+ if (r < ret)
929
+ ret = r;
930
+ }
931
+
932
+ /* Copy entry so we can modify it as needed. */
933
+ #if defined(_WIN32) && !defined(__CYGWIN__)
934
+ /* Make sure the path separators in pathname, hardlink and symlink
935
+ * are all slash '/', not the Windows path separator '\'. */
936
+ entry_main = __la_win_entry_in_posix_pathseparator(entry_original);
937
+ if (entry_main == entry_original)
938
+ entry_main = archive_entry_clone(entry_original);
939
+ #else
940
+ entry_main = archive_entry_clone(entry_original);
941
+ #endif
942
+ if (entry_main == NULL) {
943
+ archive_set_error(&a->archive, ENOMEM,
944
+ "Can't allocate pax data");
945
+ return(ARCHIVE_FATAL);
946
+ }
947
+ archive_string_empty(&(pax->pax_header)); /* Blank our work area. */
948
+ archive_string_empty(&(pax->sparse_map));
949
+ sparse_total = 0;
950
+ sparse_list_clear(pax);
951
+
952
+ if (hardlink == NULL &&
953
+ archive_entry_filetype(entry_main) == AE_IFREG)
954
+ sparse_count = archive_entry_sparse_reset(entry_main);
955
+ else
956
+ sparse_count = 0;
957
+ if (sparse_count) {
958
+ int64_t offset, length, last_offset = 0;
959
+ /* Get the last entry of sparse block. */
960
+ while (archive_entry_sparse_next(
961
+ entry_main, &offset, &length) == ARCHIVE_OK)
962
+ last_offset = offset + length;
963
+
964
+ /* If the last sparse block does not reach the end of file,
965
+ * We have to add a empty sparse block as the last entry to
966
+ * manage storing file data. */
967
+ if (last_offset < archive_entry_size(entry_main))
968
+ archive_entry_sparse_add_entry(entry_main,
969
+ archive_entry_size(entry_main), 0);
970
+ sparse_count = archive_entry_sparse_reset(entry_main);
971
+ }
972
+
973
+ /*
974
+ * First, check the name fields and see if any of them
975
+ * require binary coding. If any of them does, then all of
976
+ * them do.
977
+ */
978
+ r = get_entry_pathname(a, entry_main, &path, &path_length, sconv);
979
+ if (r == ARCHIVE_FATAL) {
980
+ archive_entry_free(entry_main);
981
+ return (r);
982
+ } else if (r != ARCHIVE_OK) {
983
+ r = get_entry_pathname(a, entry_main, &path,
984
+ &path_length, NULL);
985
+ if (r == ARCHIVE_FATAL) {
986
+ archive_entry_free(entry_main);
987
+ return (r);
988
+ }
989
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
990
+ "Can't translate pathname '%s' to %s", path,
991
+ archive_string_conversion_charset_name(sconv));
992
+ ret = ARCHIVE_WARN;
993
+ sconv = NULL;/* The header charset switches to binary mode. */
994
+ }
995
+ r = get_entry_uname(a, entry_main, &uname, &uname_length, sconv);
996
+ if (r == ARCHIVE_FATAL) {
997
+ archive_entry_free(entry_main);
998
+ return (r);
999
+ } else if (r != ARCHIVE_OK) {
1000
+ r = get_entry_uname(a, entry_main, &uname, &uname_length, NULL);
1001
+ if (r == ARCHIVE_FATAL) {
1002
+ archive_entry_free(entry_main);
1003
+ return (r);
1004
+ }
1005
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1006
+ "Can't translate uname '%s' to %s", uname,
1007
+ archive_string_conversion_charset_name(sconv));
1008
+ ret = ARCHIVE_WARN;
1009
+ sconv = NULL;/* The header charset switches to binary mode. */
1010
+ }
1011
+ r = get_entry_gname(a, entry_main, &gname, &gname_length, sconv);
1012
+ if (r == ARCHIVE_FATAL) {
1013
+ archive_entry_free(entry_main);
1014
+ return (r);
1015
+ } else if (r != ARCHIVE_OK) {
1016
+ r = get_entry_gname(a, entry_main, &gname, &gname_length, NULL);
1017
+ if (r == ARCHIVE_FATAL) {
1018
+ archive_entry_free(entry_main);
1019
+ return (r);
1020
+ }
1021
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1022
+ "Can't translate gname '%s' to %s", gname,
1023
+ archive_string_conversion_charset_name(sconv));
1024
+ ret = ARCHIVE_WARN;
1025
+ sconv = NULL;/* The header charset switches to binary mode. */
1026
+ }
1027
+ linkpath = hardlink;
1028
+ linkpath_length = hardlink_length;
1029
+ if (linkpath == NULL) {
1030
+ r = get_entry_symlink(a, entry_main, &linkpath,
1031
+ &linkpath_length, sconv);
1032
+ if (r == ARCHIVE_FATAL) {
1033
+ archive_entry_free(entry_main);
1034
+ return (r);
1035
+ } else if (r != ARCHIVE_OK) {
1036
+ r = get_entry_symlink(a, entry_main, &linkpath,
1037
+ &linkpath_length, NULL);
1038
+ if (r == ARCHIVE_FATAL) {
1039
+ archive_entry_free(entry_main);
1040
+ return (r);
1041
+ }
1042
+ archive_set_error(&a->archive,
1043
+ ARCHIVE_ERRNO_FILE_FORMAT,
1044
+ "Can't translate linkname '%s' to %s", linkpath,
1045
+ archive_string_conversion_charset_name(sconv));
1046
+ ret = ARCHIVE_WARN;
1047
+ sconv = NULL;
1048
+ }
1049
+ }
1050
+
1051
+ /* If any string conversions failed, get all attributes
1052
+ * in binary-mode. */
1053
+ if (sconv == NULL && !pax->opt_binary) {
1054
+ if (hardlink != NULL) {
1055
+ r = get_entry_hardlink(a, entry_main, &hardlink,
1056
+ &hardlink_length, NULL);
1057
+ if (r == ARCHIVE_FATAL) {
1058
+ archive_entry_free(entry_main);
1059
+ return (r);
1060
+ }
1061
+ linkpath = hardlink;
1062
+ linkpath_length = hardlink_length;
1063
+ }
1064
+ r = get_entry_pathname(a, entry_main, &path,
1065
+ &path_length, NULL);
1066
+ if (r == ARCHIVE_FATAL) {
1067
+ archive_entry_free(entry_main);
1068
+ return (r);
1069
+ }
1070
+ r = get_entry_uname(a, entry_main, &uname, &uname_length, NULL);
1071
+ if (r == ARCHIVE_FATAL) {
1072
+ archive_entry_free(entry_main);
1073
+ return (r);
1074
+ }
1075
+ r = get_entry_gname(a, entry_main, &gname, &gname_length, NULL);
1076
+ if (r == ARCHIVE_FATAL) {
1077
+ archive_entry_free(entry_main);
1078
+ return (r);
1079
+ }
1080
+ }
1081
+
1082
+ /* Store the header encoding first, to be nice to readers. */
1083
+ if (sconv == NULL)
1084
+ add_pax_attr(&(pax->pax_header), "hdrcharset", "BINARY");
1085
+
1086
+
1087
+ /*
1088
+ * If name is too long, or has non-ASCII characters, add
1089
+ * 'path' to pax extended attrs. (Note that an unconvertible
1090
+ * name must have non-ASCII characters.)
1091
+ */
1092
+ if (has_non_ASCII(path)) {
1093
+ /* We have non-ASCII characters. */
1094
+ add_pax_attr(&(pax->pax_header), "path", path);
1095
+ archive_entry_set_pathname(entry_main,
1096
+ build_ustar_entry_name(ustar_entry_name,
1097
+ path, path_length, NULL));
1098
+ need_extension = 1;
1099
+ } else {
1100
+ /* We have an all-ASCII path; we'd like to just store
1101
+ * it in the ustar header if it will fit. Yes, this
1102
+ * duplicates some of the logic in
1103
+ * archive_write_set_format_ustar.c
1104
+ */
1105
+ if (path_length <= 100) {
1106
+ /* Fits in the old 100-char tar name field. */
1107
+ } else {
1108
+ /* Find largest suffix that will fit. */
1109
+ /* Note: strlen() > 100, so strlen() - 100 - 1 >= 0 */
1110
+ suffix = strchr(path + path_length - 100 - 1, '/');
1111
+ /* Don't attempt an empty prefix. */
1112
+ if (suffix == path)
1113
+ suffix = strchr(suffix + 1, '/');
1114
+ /* We can put it in the ustar header if it's
1115
+ * all ASCII and it's either <= 100 characters
1116
+ * or can be split at a '/' into a prefix <=
1117
+ * 155 chars and a suffix <= 100 chars. (Note
1118
+ * the strchr() above will return NULL exactly
1119
+ * when the path can't be split.)
1120
+ */
1121
+ if (suffix == NULL /* Suffix > 100 chars. */
1122
+ || suffix[1] == '\0' /* empty suffix */
1123
+ || suffix - path > 155) /* Prefix > 155 chars */
1124
+ {
1125
+ add_pax_attr(&(pax->pax_header), "path", path);
1126
+ archive_entry_set_pathname(entry_main,
1127
+ build_ustar_entry_name(ustar_entry_name,
1128
+ path, path_length, NULL));
1129
+ need_extension = 1;
1130
+ }
1131
+ }
1132
+ }
1133
+
1134
+ if (linkpath != NULL) {
1135
+ /* If link name is too long or has non-ASCII characters, add
1136
+ * 'linkpath' to pax extended attrs. */
1137
+ if (linkpath_length > 100 || has_non_ASCII(linkpath)) {
1138
+ add_pax_attr(&(pax->pax_header), "linkpath", linkpath);
1139
+ if (linkpath_length > 100) {
1140
+ if (hardlink != NULL)
1141
+ archive_entry_set_hardlink(entry_main,
1142
+ "././@LongHardLink");
1143
+ else
1144
+ archive_entry_set_symlink(entry_main,
1145
+ "././@LongSymLink");
1146
+ }
1147
+ else {
1148
+ /* Otherwise, has non-ASCII characters; update the paths to
1149
+ * however they got decoded above */
1150
+ if (hardlink != NULL)
1151
+ archive_entry_set_hardlink(entry_main, linkpath);
1152
+ else
1153
+ archive_entry_set_symlink(entry_main, linkpath);
1154
+ }
1155
+ need_extension = 1;
1156
+ }
1157
+ }
1158
+ /* Save a pathname since it will be renamed if `entry_main` has
1159
+ * sparse blocks. */
1160
+ archive_string_init(&entry_name);
1161
+ archive_strcpy(&entry_name, archive_entry_pathname(entry_main));
1162
+
1163
+ /* If file size is too large, we need pax extended attrs. */
1164
+ if (archive_entry_size(entry_main) >= (((int64_t)1) << 33)) {
1165
+ need_extension = 1;
1166
+ }
1167
+
1168
+ /* If numeric GID is too large, add 'gid' to pax extended attrs. */
1169
+ if (archive_entry_gid(entry_main) >= (1 << 18)) {
1170
+ add_pax_attr_int(&(pax->pax_header), "gid",
1171
+ archive_entry_gid(entry_main));
1172
+ need_extension = 1;
1173
+ }
1174
+
1175
+ /* If group name is too large or has non-ASCII characters, add
1176
+ * 'gname' to pax extended attrs. */
1177
+ if (gname != NULL) {
1178
+ if (gname_length > 31 || has_non_ASCII(gname)) {
1179
+ add_pax_attr(&(pax->pax_header), "gname", gname);
1180
+ need_extension = 1;
1181
+ }
1182
+ }
1183
+
1184
+ /* If numeric UID is too large, add 'uid' to pax extended attrs. */
1185
+ if (archive_entry_uid(entry_main) >= (1 << 18)) {
1186
+ add_pax_attr_int(&(pax->pax_header), "uid",
1187
+ archive_entry_uid(entry_main));
1188
+ need_extension = 1;
1189
+ }
1190
+
1191
+ /* Add 'uname' to pax extended attrs if necessary. */
1192
+ if (uname != NULL) {
1193
+ if (uname_length > 31 || has_non_ASCII(uname)) {
1194
+ add_pax_attr(&(pax->pax_header), "uname", uname);
1195
+ need_extension = 1;
1196
+ }
1197
+ }
1198
+
1199
+ /*
1200
+ * POSIX/SUSv3 doesn't provide a standard key for large device
1201
+ * numbers. I use the same keys here that Joerg Schilling
1202
+ * used for 'star.' (Which, somewhat confusingly, are called
1203
+ * "devXXX" even though they code "rdev" values.) No doubt,
1204
+ * other implementations use other keys. Note that there's no
1205
+ * reason we can't write the same information into a number of
1206
+ * different keys.
1207
+ *
1208
+ * Of course, this is only needed for block or char device entries.
1209
+ */
1210
+ if (archive_entry_filetype(entry_main) == AE_IFBLK
1211
+ || archive_entry_filetype(entry_main) == AE_IFCHR) {
1212
+ /*
1213
+ * If rdevmajor is too large, add 'SCHILY.devmajor' to
1214
+ * extended attributes.
1215
+ */
1216
+ int rdevmajor, rdevminor;
1217
+ rdevmajor = archive_entry_rdevmajor(entry_main);
1218
+ rdevminor = archive_entry_rdevminor(entry_main);
1219
+ if (rdevmajor >= (1 << 18)) {
1220
+ add_pax_attr_int(&(pax->pax_header), "SCHILY.devmajor",
1221
+ rdevmajor);
1222
+ /*
1223
+ * Non-strict formatting below means we don't
1224
+ * have to truncate here. Not truncating improves
1225
+ * the chance that some more modern tar archivers
1226
+ * (such as GNU tar 1.13) can restore the full
1227
+ * value even if they don't understand the pax
1228
+ * extended attributes. See my rant below about
1229
+ * file size fields for additional details.
1230
+ */
1231
+ /* archive_entry_set_rdevmajor(entry_main,
1232
+ rdevmajor & ((1 << 18) - 1)); */
1233
+ need_extension = 1;
1234
+ }
1235
+
1236
+ /*
1237
+ * If devminor is too large, add 'SCHILY.devminor' to
1238
+ * extended attributes.
1239
+ */
1240
+ if (rdevminor >= (1 << 18)) {
1241
+ add_pax_attr_int(&(pax->pax_header), "SCHILY.devminor",
1242
+ rdevminor);
1243
+ /* Truncation is not necessary here, either. */
1244
+ /* archive_entry_set_rdevminor(entry_main,
1245
+ rdevminor & ((1 << 18) - 1)); */
1246
+ need_extension = 1;
1247
+ }
1248
+ }
1249
+
1250
+ /*
1251
+ * Yes, this check is duplicated just below; this helps to
1252
+ * avoid writing an mtime attribute just to handle a
1253
+ * high-resolution timestamp in "restricted pax" mode.
1254
+ */
1255
+ if (!need_extension &&
1256
+ ((archive_entry_mtime(entry_main) < 0)
1257
+ || (archive_entry_mtime(entry_main) >= ustar_max_mtime)))
1258
+ need_extension = 1;
1259
+
1260
+ /* I use a star-compatible file flag attribute. */
1261
+ p = archive_entry_fflags_text(entry_main);
1262
+ if (!need_extension && p != NULL && *p != '\0')
1263
+ need_extension = 1;
1264
+
1265
+ /* If there are extended attributes, we need an extension */
1266
+ if (!need_extension && archive_entry_xattr_count(entry_original) > 0)
1267
+ need_extension = 1;
1268
+
1269
+ /* If there are sparse info, we need an extension */
1270
+ if (!need_extension && sparse_count > 0)
1271
+ need_extension = 1;
1272
+
1273
+ acl_types = archive_entry_acl_types(entry_original);
1274
+
1275
+ /* If there are any ACL entries, we need an extension */
1276
+ if (!need_extension && acl_types != 0)
1277
+ need_extension = 1;
1278
+
1279
+ /* If the symlink type is defined, we need an extension */
1280
+ if (!need_extension && archive_entry_symlink_type(entry_main) > 0)
1281
+ need_extension = 1;
1282
+
1283
+ /*
1284
+ * Libarchive used to include these in extended headers for
1285
+ * restricted pax format, but that confused people who
1286
+ * expected ustar-like time semantics. So now we only include
1287
+ * them in full pax format.
1288
+ */
1289
+ if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_RESTRICTED) {
1290
+ if (archive_entry_ctime(entry_main) != 0 ||
1291
+ archive_entry_ctime_nsec(entry_main) != 0)
1292
+ add_pax_attr_time(&(pax->pax_header), "ctime",
1293
+ archive_entry_ctime(entry_main),
1294
+ archive_entry_ctime_nsec(entry_main));
1295
+
1296
+ if (archive_entry_atime(entry_main) != 0 ||
1297
+ archive_entry_atime_nsec(entry_main) != 0)
1298
+ add_pax_attr_time(&(pax->pax_header), "atime",
1299
+ archive_entry_atime(entry_main),
1300
+ archive_entry_atime_nsec(entry_main));
1301
+
1302
+ /* Store birth/creationtime only if it's earlier than mtime */
1303
+ if (archive_entry_birthtime_is_set(entry_main) &&
1304
+ archive_entry_birthtime(entry_main)
1305
+ < archive_entry_mtime(entry_main))
1306
+ add_pax_attr_time(&(pax->pax_header),
1307
+ "LIBARCHIVE.creationtime",
1308
+ archive_entry_birthtime(entry_main),
1309
+ archive_entry_birthtime_nsec(entry_main));
1310
+ }
1311
+
1312
+ /*
1313
+ * The following items are handled differently in "pax
1314
+ * restricted" format. In particular, in "pax restricted"
1315
+ * format they won't be added unless need_extension is
1316
+ * already set (we're already generating an extended header, so
1317
+ * may as well include these).
1318
+ */
1319
+ if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_RESTRICTED ||
1320
+ need_extension) {
1321
+ if (archive_entry_mtime(entry_main) < 0 ||
1322
+ archive_entry_mtime(entry_main) >= ustar_max_mtime ||
1323
+ archive_entry_mtime_nsec(entry_main) != 0)
1324
+ add_pax_attr_time(&(pax->pax_header), "mtime",
1325
+ archive_entry_mtime(entry_main),
1326
+ archive_entry_mtime_nsec(entry_main));
1327
+
1328
+ /* I use a star-compatible file flag attribute. */
1329
+ p = archive_entry_fflags_text(entry_main);
1330
+ if (p != NULL && *p != '\0')
1331
+ add_pax_attr(&(pax->pax_header), "SCHILY.fflags", p);
1332
+
1333
+ /* I use star-compatible ACL attributes. */
1334
+ if ((acl_types & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
1335
+ ret = add_pax_acl(a, entry_original, pax,
1336
+ ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID |
1337
+ ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA |
1338
+ ARCHIVE_ENTRY_ACL_STYLE_COMPACT);
1339
+ if (ret == ARCHIVE_FATAL) {
1340
+ archive_entry_free(entry_main);
1341
+ archive_string_free(&entry_name);
1342
+ return (ARCHIVE_FATAL);
1343
+ }
1344
+ }
1345
+ if (acl_types & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
1346
+ ret = add_pax_acl(a, entry_original, pax,
1347
+ ARCHIVE_ENTRY_ACL_TYPE_ACCESS |
1348
+ ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID |
1349
+ ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA);
1350
+ if (ret == ARCHIVE_FATAL) {
1351
+ archive_entry_free(entry_main);
1352
+ archive_string_free(&entry_name);
1353
+ return (ARCHIVE_FATAL);
1354
+ }
1355
+ }
1356
+ if (acl_types & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) {
1357
+ ret = add_pax_acl(a, entry_original, pax,
1358
+ ARCHIVE_ENTRY_ACL_TYPE_DEFAULT |
1359
+ ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID |
1360
+ ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA);
1361
+ if (ret == ARCHIVE_FATAL) {
1362
+ archive_entry_free(entry_main);
1363
+ archive_string_free(&entry_name);
1364
+ return (ARCHIVE_FATAL);
1365
+ }
1366
+ }
1367
+
1368
+ /* We use GNU-tar-compatible sparse attributes. */
1369
+ if (sparse_count > 0) {
1370
+ int64_t soffset, slength;
1371
+
1372
+ add_pax_attr_int(&(pax->pax_header),
1373
+ "GNU.sparse.major", 1);
1374
+ add_pax_attr_int(&(pax->pax_header),
1375
+ "GNU.sparse.minor", 0);
1376
+ /*
1377
+ * Make sure to store the original path, since
1378
+ * truncation to ustar limit happened already.
1379
+ */
1380
+ add_pax_attr(&(pax->pax_header),
1381
+ "GNU.sparse.name", path);
1382
+ add_pax_attr_int(&(pax->pax_header),
1383
+ "GNU.sparse.realsize",
1384
+ archive_entry_size(entry_main));
1385
+
1386
+ /* Rename the file name which will be used for
1387
+ * ustar header to a special name, which GNU
1388
+ * PAX Format 1.0 requires */
1389
+ archive_entry_set_pathname(entry_main,
1390
+ build_gnu_sparse_name(gnu_sparse_name,
1391
+ entry_name.s));
1392
+
1393
+ /*
1394
+ * - Make a sparse map, which will precede a file data.
1395
+ * - Get the total size of available data of sparse.
1396
+ */
1397
+ archive_string_sprintf(&(pax->sparse_map), "%d\n",
1398
+ sparse_count);
1399
+ while (archive_entry_sparse_next(entry_main,
1400
+ &soffset, &slength) == ARCHIVE_OK) {
1401
+ archive_string_sprintf(&(pax->sparse_map),
1402
+ "%jd\n%jd\n",
1403
+ (intmax_t)soffset,
1404
+ (intmax_t)slength);
1405
+ sparse_total += slength;
1406
+ if (sparse_list_add(pax, soffset, slength)
1407
+ != ARCHIVE_OK) {
1408
+ archive_set_error(&a->archive,
1409
+ ENOMEM,
1410
+ "Can't allocate memory");
1411
+ archive_entry_free(entry_main);
1412
+ archive_string_free(&entry_name);
1413
+ return (ARCHIVE_FATAL);
1414
+ }
1415
+ }
1416
+ }
1417
+
1418
+ /* Store extended attributes */
1419
+ if (archive_write_pax_header_xattrs(a, pax, entry_original)
1420
+ == ARCHIVE_FATAL) {
1421
+ archive_entry_free(entry_main);
1422
+ archive_string_free(&entry_name);
1423
+ return (ARCHIVE_FATAL);
1424
+ }
1425
+
1426
+ /* Store extended symlink information */
1427
+ if (archive_entry_symlink_type(entry_main) ==
1428
+ AE_SYMLINK_TYPE_FILE) {
1429
+ add_pax_attr(&(pax->pax_header),
1430
+ "LIBARCHIVE.symlinktype", "file");
1431
+ } else if (archive_entry_symlink_type(entry_main) ==
1432
+ AE_SYMLINK_TYPE_DIRECTORY) {
1433
+ add_pax_attr(&(pax->pax_header),
1434
+ "LIBARCHIVE.symlinktype", "dir");
1435
+ }
1436
+ }
1437
+
1438
+ /* Only regular files have data. */
1439
+ if (archive_entry_filetype(entry_main) != AE_IFREG)
1440
+ archive_entry_set_size(entry_main, 0);
1441
+
1442
+ /*
1443
+ * XXX Full pax interchange format does permit a hardlink
1444
+ * entry to have data associated with it. I'm not supporting
1445
+ * that here because the client expects me to tell them whether
1446
+ * or not this format expects data for hardlinks. If I
1447
+ * don't check here, then every pax archive will end up with
1448
+ * duplicated data for hardlinks. Someday, there may be
1449
+ * need to select this behavior, in which case the following
1450
+ * will need to be revisited. XXX
1451
+ */
1452
+ if (hardlink != NULL)
1453
+ archive_entry_set_size(entry_main, 0);
1454
+
1455
+ /* Save a real file size. */
1456
+ real_size = archive_entry_size(entry_main);
1457
+ /*
1458
+ * Overwrite a file size by the total size of sparse blocks and
1459
+ * the size of sparse map info. That file size is the length of
1460
+ * the data, which we will exactly store into an archive file.
1461
+ */
1462
+ if (archive_strlen(&(pax->sparse_map))) {
1463
+ size_t mapsize = archive_strlen(&(pax->sparse_map));
1464
+ pax->sparse_map_padding = 0x1ff & (-(ssize_t)mapsize);
1465
+ archive_entry_set_size(entry_main,
1466
+ mapsize + pax->sparse_map_padding + sparse_total);
1467
+ }
1468
+
1469
+ /* If file size is too large, add 'size' to pax extended attrs. */
1470
+ if (archive_entry_size(entry_main) >= (((int64_t)1) << 33)) {
1471
+ add_pax_attr_int(&(pax->pax_header), "size",
1472
+ archive_entry_size(entry_main));
1473
+ }
1474
+
1475
+ /* Format 'ustar' header for main entry.
1476
+ *
1477
+ * The trouble with file size: If the reader can't understand
1478
+ * the file size, they may not be able to locate the next
1479
+ * entry and the rest of the archive is toast. Pax-compliant
1480
+ * readers are supposed to ignore the file size in the main
1481
+ * header, so the question becomes how to maximize portability
1482
+ * for readers that don't support pax attribute extensions.
1483
+ * For maximum compatibility, I permit numeric extensions in
1484
+ * the main header so that the file size stored will always be
1485
+ * correct, even if it's in a format that only some
1486
+ * implementations understand. The technique used here is:
1487
+ *
1488
+ * a) If possible, follow the standard exactly. This handles
1489
+ * files up to 8 gigabytes minus 1.
1490
+ *
1491
+ * b) If that fails, try octal but omit the field terminator.
1492
+ * That handles files up to 64 gigabytes minus 1.
1493
+ *
1494
+ * c) Otherwise, use base-256 extensions. That handles files
1495
+ * up to 2^63 in this implementation, with the potential to
1496
+ * go up to 2^94. That should hold us for a while. ;-)
1497
+ *
1498
+ * The non-strict formatter uses similar logic for other
1499
+ * numeric fields, though they're less critical.
1500
+ */
1501
+ if (__archive_write_format_header_ustar(a, ustarbuff, entry_main, -1, 0,
1502
+ NULL) == ARCHIVE_FATAL) {
1503
+ archive_entry_free(entry_main);
1504
+ archive_string_free(&entry_name);
1505
+ return (ARCHIVE_FATAL);
1506
+ }
1507
+
1508
+ /* Pad the extended header so contiguous file data starts on a pax->align
1509
+ * boundary (for reflinking); short and sparse files are left alone. */
1510
+ if (pax->align > 0
1511
+ && archive_entry_filetype(entry_main) == AE_IFREG
1512
+ && hardlink == NULL
1513
+ && sparse_count == 0
1514
+ && real_size >= (uint64_t)pax->align) {
1515
+ size_t l0 = archive_strlen(&(pax->pax_header));
1516
+ size_t needed = pax_align_body_len(
1517
+ (uint64_t)a->filter_first->bytes_written, l0, pax->align);
1518
+ if (needed != l0)
1519
+ pax_header_padding = add_pax_padding_prefix(
1520
+ &(pax->pax_header), l0, needed);
1521
+ }
1522
+
1523
+ /* If we built any extended attributes, write that entry first. */
1524
+ if (archive_strlen(&(pax->pax_header)) > 0) {
1525
+ struct archive_entry *pax_attr_entry;
1526
+ time_t s;
1527
+ int64_t uid, gid;
1528
+ __LA_MODE_T mode;
1529
+
1530
+ pax_attr_entry = archive_entry_new2(&a->archive);
1531
+ if (pax_attr_entry == NULL) {
1532
+ archive_set_error(&a->archive, ENOMEM,
1533
+ "Out of memory");
1534
+ archive_entry_free(entry_main);
1535
+ archive_string_free(&entry_name);
1536
+ return (ARCHIVE_FATAL);
1537
+ }
1538
+
1539
+ p = entry_name.s;
1540
+ archive_entry_set_pathname(pax_attr_entry,
1541
+ build_pax_attribute_name(pax_entry_name, p));
1542
+ archive_entry_set_size(pax_attr_entry,
1543
+ archive_strlen(&(pax->pax_header)) +
1544
+ pax_header_padding);
1545
+ /* Copy uid/gid (but clip to ustar limits). */
1546
+ uid = archive_entry_uid(entry_main);
1547
+ if (uid >= 1 << 18)
1548
+ uid = (1 << 18) - 1;
1549
+ archive_entry_set_uid(pax_attr_entry, uid);
1550
+ gid = archive_entry_gid(entry_main);
1551
+ if (gid >= 1 << 18)
1552
+ gid = (1 << 18) - 1;
1553
+ archive_entry_set_gid(pax_attr_entry, gid);
1554
+ /* Copy mode over (but not setuid/setgid bits) */
1555
+ mode = archive_entry_mode(entry_main);
1556
+ #ifdef S_ISUID
1557
+ mode &= ~S_ISUID;
1558
+ #endif
1559
+ #ifdef S_ISGID
1560
+ mode &= ~S_ISGID;
1561
+ #endif
1562
+ #ifdef S_ISVTX
1563
+ mode &= ~S_ISVTX;
1564
+ #endif
1565
+ archive_entry_set_mode(pax_attr_entry, mode);
1566
+
1567
+ /* Copy uname/gname. */
1568
+ archive_entry_set_uname(pax_attr_entry,
1569
+ archive_entry_uname(entry_main));
1570
+ archive_entry_set_gname(pax_attr_entry,
1571
+ archive_entry_gname(entry_main));
1572
+
1573
+ /* Copy mtime, but clip to ustar limits. */
1574
+ s = archive_entry_mtime(entry_main);
1575
+ if (s < 0) { s = 0; }
1576
+ if (s > ustar_max_mtime) { s = ustar_max_mtime; }
1577
+ archive_entry_set_mtime(pax_attr_entry, s, 0);
1578
+
1579
+ /* Standard ustar doesn't support atime. */
1580
+ archive_entry_set_atime(pax_attr_entry, 0, 0);
1581
+
1582
+ /* Standard ustar doesn't support ctime. */
1583
+ archive_entry_set_ctime(pax_attr_entry, 0, 0);
1584
+
1585
+ r = __archive_write_format_header_ustar(a, paxbuff,
1586
+ pax_attr_entry, 'x', 1, NULL);
1587
+
1588
+ archive_entry_free(pax_attr_entry);
1589
+
1590
+ /* Note that the 'x' header shouldn't ever fail to format */
1591
+ if (r < ARCHIVE_WARN) {
1592
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1593
+ "archive_write_pax_header: "
1594
+ "'x' header failed?! This can't happen");
1595
+ archive_entry_free(entry_main);
1596
+ archive_string_free(&entry_name);
1597
+ return (ARCHIVE_FATAL);
1598
+ } else if (r < ret)
1599
+ ret = r;
1600
+ r = __archive_write_output(a, paxbuff, 512);
1601
+ if (r != ARCHIVE_OK) {
1602
+ sparse_list_clear(pax);
1603
+ pax->entry_bytes_remaining = 0;
1604
+ pax->entry_padding = 0;
1605
+ archive_entry_free(entry_main);
1606
+ archive_string_free(&entry_name);
1607
+ return (ARCHIVE_FATAL);
1608
+ }
1609
+
1610
+ pax->entry_bytes_remaining = archive_strlen(&(pax->pax_header)) +
1611
+ pax_header_padding;
1612
+ pax->entry_padding =
1613
+ 0x1ff & (-(int64_t)pax->entry_bytes_remaining);
1614
+
1615
+ r = __archive_write_output(a, pax->pax_header.s,
1616
+ archive_strlen(&(pax->pax_header)));
1617
+ if (r != ARCHIVE_OK) {
1618
+ /* If a write fails, we're pretty much toast. */
1619
+ archive_entry_free(entry_main);
1620
+ archive_string_free(&entry_name);
1621
+ return (ARCHIVE_FATAL);
1622
+ }
1623
+ r = write_pax_padding(a, pax_header_padding);
1624
+ if (r != ARCHIVE_OK) {
1625
+ archive_entry_free(entry_main);
1626
+ archive_string_free(&entry_name);
1627
+ return (ARCHIVE_FATAL);
1628
+ }
1629
+ /* Pad out the end of the entry. */
1630
+ r = __archive_write_nulls(a, pax->entry_padding);
1631
+ if (r != ARCHIVE_OK) {
1632
+ /* If a write fails, we're pretty much toast. */
1633
+ archive_entry_free(entry_main);
1634
+ archive_string_free(&entry_name);
1635
+ return (ARCHIVE_FATAL);
1636
+ }
1637
+ pax->entry_bytes_remaining = pax->entry_padding = 0;
1638
+ }
1639
+
1640
+ /* Write the header for main entry. */
1641
+ r = __archive_write_output(a, ustarbuff, 512);
1642
+ if (r != ARCHIVE_OK) {
1643
+ archive_entry_free(entry_main);
1644
+ archive_string_free(&entry_name);
1645
+ return (r);
1646
+ }
1647
+
1648
+ /*
1649
+ * Inform the client of the on-disk size we're using, so
1650
+ * they can avoid unnecessarily writing a body for something
1651
+ * that we're just going to ignore.
1652
+ */
1653
+ archive_entry_set_size(entry_original, real_size);
1654
+ if (pax->sparse_list == NULL && real_size > 0) {
1655
+ /* This is not a sparse file but we handle its data as
1656
+ * a sparse block. */
1657
+ sparse_list_add(pax, 0, real_size);
1658
+ sparse_total = real_size;
1659
+ }
1660
+ pax->entry_padding = 0x1ff & (-(int64_t)sparse_total);
1661
+ archive_entry_free(entry_main);
1662
+ archive_string_free(&entry_name);
1663
+
1664
+ return (ret);
1665
+ }
1666
+
1667
+ /*
1668
+ * We need a valid name for the regular 'ustar' entry. This routine
1669
+ * tries to hack something more-or-less reasonable.
1670
+ *
1671
+ * The approach here tries to preserve leading dir names. We do so by
1672
+ * working with four sections:
1673
+ * 1) "prefix" directory names,
1674
+ * 2) "suffix" directory names,
1675
+ * 3) inserted dir name (optional),
1676
+ * 4) filename.
1677
+ *
1678
+ * These sections must satisfy the following requirements:
1679
+ * * Parts 1 & 2 together form an initial portion of the dir name.
1680
+ * * Part 3 is specified by the caller. (It should not contain a leading
1681
+ * or trailing '/'.)
1682
+ * * Part 4 forms an initial portion of the base filename.
1683
+ * * The filename must be <= 99 chars to fit the ustar 'name' field.
1684
+ * * Parts 2, 3, 4 together must be <= 99 chars to fit the ustar 'name' fld.
1685
+ * * Part 1 must be <= 155 chars to fit the ustar 'prefix' field.
1686
+ * * If the original name ends in a '/', the new name must also end in a '/'
1687
+ * * Trailing '/.' sequences may be stripped.
1688
+ *
1689
+ * Note: Recall that the ustar format does not store the '/' separating
1690
+ * parts 1 & 2, but does store the '/' separating parts 2 & 3.
1691
+ */
1692
+ static char *
1693
+ build_ustar_entry_name(char *dest, const char *src, size_t src_length,
1694
+ const char *insert)
1695
+ {
1696
+ const char *prefix, *prefix_end;
1697
+ const char *suffix, *suffix_end;
1698
+ const char *filename, *filename_end;
1699
+ char *p;
1700
+ int need_slash = 0; /* Was there a trailing slash? */
1701
+ size_t suffix_length = 98; /* 99 - 1 for trailing slash */
1702
+ size_t insert_length, insert_name_length;
1703
+
1704
+ /* Length of additional dir element to be added. */
1705
+ if (insert == NULL) {
1706
+ insert_name_length = 0;
1707
+ insert_length = 0;
1708
+ } else {
1709
+ insert_name_length = strlen(insert);
1710
+ /* +2 here allows for '/' before and after the insert. */
1711
+ insert_length = insert_name_length + 2;
1712
+ }
1713
+
1714
+ /* Step 0: Quick bailout in a common case. */
1715
+ if (src_length < 100 && insert == NULL) {
1716
+ memcpy(dest, src, src_length);
1717
+ dest[src_length] = '\0';
1718
+ return (dest);
1719
+ }
1720
+
1721
+ /* Step 1: Locate filename and enforce the length restriction. */
1722
+ filename_end = src + src_length;
1723
+ /* Remove trailing '/' chars and '/.' pairs. */
1724
+ for (;;) {
1725
+ if (filename_end > src && filename_end[-1] == '/') {
1726
+ filename_end --;
1727
+ need_slash = 1; /* Remember to restore trailing '/'. */
1728
+ continue;
1729
+ }
1730
+ if (filename_end > src + 1 && filename_end[-1] == '.'
1731
+ && filename_end[-2] == '/') {
1732
+ filename_end -= 2;
1733
+ need_slash = 1; /* "foo/." will become "foo/" */
1734
+ continue;
1735
+ }
1736
+ break;
1737
+ }
1738
+
1739
+ /*
1740
+ * Pathological case: after trimming trailing '/' characters and
1741
+ * '/.' path elements, there is no filename component left. This
1742
+ * happens for pathnames made entirely of '/' characters. Do not
1743
+ * attempt to compute filename_end - 1 in that case, which would
1744
+ * move the filename pointer in front of the input buffer and read
1745
+ * one byte out of bounds. Emit a root-like ustar name instead.
1746
+ */
1747
+ if (filename_end == src) {
1748
+ p = dest;
1749
+ if (insert != NULL) {
1750
+ strcpy(p, insert);
1751
+ p += strlen(insert);
1752
+ }
1753
+ *p++ = '/';
1754
+ *p = '\0';
1755
+ return (dest);
1756
+ }
1757
+
1758
+ if (need_slash)
1759
+ suffix_length--;
1760
+ /* Find start of filename. */
1761
+ filename = filename_end - 1;
1762
+ while ((filename > src) && (*filename != '/'))
1763
+ filename --;
1764
+ if ((*filename == '/') && (filename < filename_end - 1))
1765
+ filename ++;
1766
+ /* Adjust filename_end so that filename + insert fits in 99 chars. */
1767
+ suffix_length -= insert_length;
1768
+ if (filename_end > filename + suffix_length)
1769
+ filename_end = filename + suffix_length;
1770
+ /* Calculate max size for "suffix" section (#3 above). */
1771
+ suffix_length -= filename_end - filename;
1772
+
1773
+ /* Step 2: Locate the "prefix" section of the dirname, including
1774
+ * trailing '/'. */
1775
+ prefix = src;
1776
+ prefix_end = prefix + 154 /* 155 - 1 for trailing / */;
1777
+ if (prefix_end > filename)
1778
+ prefix_end = filename;
1779
+ while (prefix_end > prefix && *prefix_end != '/')
1780
+ prefix_end--;
1781
+ if ((prefix_end < filename) && (*prefix_end == '/'))
1782
+ prefix_end++;
1783
+
1784
+ /* Step 3: Locate the "suffix" section of the dirname,
1785
+ * including trailing '/'. */
1786
+ suffix = prefix_end;
1787
+ suffix_end = suffix + suffix_length; /* Enforce limit. */
1788
+ if (suffix_end > filename)
1789
+ suffix_end = filename;
1790
+ if (suffix_end < suffix)
1791
+ suffix_end = suffix;
1792
+ while (suffix_end > suffix && *suffix_end != '/')
1793
+ suffix_end--;
1794
+ if ((suffix_end < filename) && (*suffix_end == '/'))
1795
+ suffix_end++;
1796
+
1797
+ /* Step 4: Build the new name. */
1798
+ p = dest;
1799
+ if (prefix_end > prefix) {
1800
+ memcpy(p, prefix, (size_t)(prefix_end - prefix));
1801
+ p += prefix_end - prefix;
1802
+ }
1803
+ if (suffix_end > suffix) {
1804
+ memcpy(p, suffix, (size_t)(suffix_end - suffix));
1805
+ p += suffix_end - suffix;
1806
+ }
1807
+ if (insert != NULL) {
1808
+ /* Note: assume insert does not have leading or trailing '/' */
1809
+ memcpy(p, insert, insert_name_length);
1810
+ p += insert_name_length;
1811
+ *p++ = '/';
1812
+ }
1813
+ memcpy(p, filename, (size_t)(filename_end - filename));
1814
+ p += filename_end - filename;
1815
+ if (need_slash)
1816
+ *p++ = '/';
1817
+ *p = '\0';
1818
+
1819
+ return (dest);
1820
+ }
1821
+
1822
+ /*
1823
+ * The ustar header for the pax extended attributes must have a
1824
+ * reasonable name: SUSv3 requires 'dirname'/PaxHeader.'pid'/'filename'
1825
+ * where 'pid' is the PID of the archiving process. Unfortunately,
1826
+ * that makes testing a pain since the output varies for each run,
1827
+ * so I'm sticking with the simpler 'dirname'/PaxHeader/'filename'
1828
+ * for now. (Someday, I'll make this settable. Then I can use the
1829
+ * SUS recommendation as default and test harnesses can override it
1830
+ * to get predictable results.)
1831
+ *
1832
+ * Joerg Schilling has argued that this is unnecessary because, in
1833
+ * practice, if the pax extended attributes get extracted as regular
1834
+ * files, no one is going to bother reading those attributes to
1835
+ * manually restore them. Based on this, 'star' uses
1836
+ * /tmp/PaxHeader/'basename' as the ustar header name. This is a
1837
+ * tempting argument, in part because it's simpler than the SUSv3
1838
+ * recommendation, but I'm not entirely convinced. I'm also
1839
+ * uncomfortable with the fact that "/tmp" is a Unix-ism.
1840
+ *
1841
+ * The following routine leverages build_ustar_entry_name() above and
1842
+ * so is simpler than you might think. It just needs to provide the
1843
+ * additional path element and handle a few pathological cases).
1844
+ */
1845
+ static char *
1846
+ build_pax_attribute_name(char *dest, const char *src)
1847
+ {
1848
+ char buff[64];
1849
+ const char *p;
1850
+
1851
+ /* Handle the null filename case. */
1852
+ if (src == NULL || *src == '\0') {
1853
+ strcpy(dest, "PaxHeader/blank");
1854
+ return (dest);
1855
+ }
1856
+
1857
+ /* Prune final '/' and other unwanted final elements. */
1858
+ p = src + strlen(src);
1859
+ for (;;) {
1860
+ /* Ends in "/", remove the '/' */
1861
+ if (p > src && p[-1] == '/') {
1862
+ --p;
1863
+ continue;
1864
+ }
1865
+ /* Ends in "/.", remove the '.' */
1866
+ if (p > src + 1 && p[-1] == '.'
1867
+ && p[-2] == '/') {
1868
+ --p;
1869
+ continue;
1870
+ }
1871
+ break;
1872
+ }
1873
+
1874
+ /* Pathological case: After above, there was nothing left.
1875
+ * This includes "/." "/./." "/.//./." etc. */
1876
+ if (p == src) {
1877
+ strcpy(dest, "/PaxHeader/rootdir");
1878
+ return (dest);
1879
+ }
1880
+
1881
+ /* Convert unadorned "." into a suitable filename. */
1882
+ if (*src == '.' && p == src + 1) {
1883
+ strcpy(dest, "PaxHeader/currentdir");
1884
+ return (dest);
1885
+ }
1886
+
1887
+ /*
1888
+ * TODO: Push this string into the 'pax' structure to avoid
1889
+ * recomputing it every time. That will also open the door
1890
+ * to having clients override it.
1891
+ */
1892
+ #if HAVE_GETPID && 0 /* Disable this for now; see above comment. */
1893
+ snprintf(buff, sizeof(buff), "PaxHeader.%d", getpid());
1894
+ #else
1895
+ /* If the platform can't fetch the pid, don't include it. */
1896
+ strcpy(buff, "PaxHeader");
1897
+ #endif
1898
+ /* General case: build a ustar-compatible name adding
1899
+ * "/PaxHeader/". */
1900
+ build_ustar_entry_name(dest, src, p - src, buff);
1901
+
1902
+ return (dest);
1903
+ }
1904
+
1905
+ /*
1906
+ * GNU PAX Format 1.0 requires the special name, which pattern is:
1907
+ * <dir>/GNUSparseFile.<pid>/<original file name>
1908
+ *
1909
+ * Since reproducible archives are more important, use 0 as pid.
1910
+ *
1911
+ * This function is used for only Sparse file, a file type of which
1912
+ * is regular file.
1913
+ */
1914
+ static char *
1915
+ build_gnu_sparse_name(char *dest, const char *src)
1916
+ {
1917
+ const char *p;
1918
+
1919
+ /* Handle the null filename case. */
1920
+ if (src == NULL || *src == '\0') {
1921
+ strcpy(dest, "GNUSparseFile/blank");
1922
+ return (dest);
1923
+ }
1924
+
1925
+ /* Prune final '/' and other unwanted final elements. */
1926
+ p = src + strlen(src);
1927
+ for (;;) {
1928
+ /* Ends in "/", remove the '/' */
1929
+ if (p > src && p[-1] == '/') {
1930
+ --p;
1931
+ continue;
1932
+ }
1933
+ /* Ends in "/.", remove the '.' */
1934
+ if (p > src + 1 && p[-1] == '.'
1935
+ && p[-2] == '/') {
1936
+ --p;
1937
+ continue;
1938
+ }
1939
+ break;
1940
+ }
1941
+
1942
+ /* General case: build a ustar-compatible name adding
1943
+ * "/GNUSparseFile.0/". */
1944
+
1945
+ if (p == src) {
1946
+ strcpy(dest, "/GNUSparseFile.0/rootdir");
1947
+ return (dest);
1948
+ }
1949
+ build_ustar_entry_name(dest, src, p - src, "GNUSparseFile.0");
1950
+
1951
+ return (dest);
1952
+ }
1953
+
1954
+ /* Write two null blocks for the end of archive */
1955
+ static int
1956
+ archive_write_pax_close(struct archive_write *a)
1957
+ {
1958
+ return (__archive_write_nulls(a, 512 * 2));
1959
+ }
1960
+
1961
+ static int
1962
+ archive_write_pax_free(struct archive_write *a)
1963
+ {
1964
+ struct pax *pax = a->format_data;
1965
+
1966
+ if (pax == NULL)
1967
+ return (ARCHIVE_OK);
1968
+
1969
+ archive_string_free(&pax->pax_header);
1970
+ archive_string_free(&pax->sparse_map);
1971
+ archive_string_free(&pax->l_url_encoded_name);
1972
+ sparse_list_clear(pax);
1973
+ free(pax);
1974
+ a->format_data = NULL;
1975
+ return (ARCHIVE_OK);
1976
+ }
1977
+
1978
+ static int
1979
+ archive_write_pax_finish_entry(struct archive_write *a)
1980
+ {
1981
+ struct pax *pax = a->format_data;
1982
+ uint64_t remaining;
1983
+ int ret;
1984
+
1985
+ remaining = pax->entry_bytes_remaining;
1986
+ if (remaining == 0) {
1987
+ while (pax->sparse_list) {
1988
+ struct sparse_block *sb;
1989
+ if (!pax->sparse_list->is_hole)
1990
+ remaining += pax->sparse_list->remaining;
1991
+ sb = pax->sparse_list->next;
1992
+ free(pax->sparse_list);
1993
+ pax->sparse_list = sb;
1994
+ }
1995
+ }
1996
+ ret = __archive_write_nulls(a, remaining + pax->entry_padding);
1997
+ pax->entry_bytes_remaining = pax->entry_padding = 0;
1998
+ return (ret);
1999
+ }
2000
+
2001
+ static ssize_t
2002
+ archive_write_pax_data(struct archive_write *a, const void *buff, size_t s)
2003
+ {
2004
+ struct pax *pax = a->format_data;
2005
+ size_t ws;
2006
+ size_t total;
2007
+ int ret;
2008
+
2009
+ /*
2010
+ * According to GNU PAX format 1.0, write a sparse map
2011
+ * before the body.
2012
+ */
2013
+ if (archive_strlen(&(pax->sparse_map))) {
2014
+ ret = __archive_write_output(a, pax->sparse_map.s,
2015
+ archive_strlen(&(pax->sparse_map)));
2016
+ if (ret != ARCHIVE_OK)
2017
+ return (ret);
2018
+ ret = __archive_write_nulls(a, pax->sparse_map_padding);
2019
+ if (ret != ARCHIVE_OK)
2020
+ return (ret);
2021
+ archive_string_empty(&(pax->sparse_map));
2022
+ }
2023
+
2024
+ total = 0;
2025
+ while (total < s) {
2026
+ const unsigned char *p;
2027
+
2028
+ while (pax->sparse_list != NULL &&
2029
+ pax->sparse_list->remaining == 0) {
2030
+ struct sparse_block *sb = pax->sparse_list->next;
2031
+ free(pax->sparse_list);
2032
+ pax->sparse_list = sb;
2033
+ }
2034
+
2035
+ if (pax->sparse_list == NULL)
2036
+ return (total);
2037
+
2038
+ p = ((const unsigned char *)buff) + total;
2039
+ ws = s - total;
2040
+ if (ws > pax->sparse_list->remaining)
2041
+ ws = (size_t)pax->sparse_list->remaining;
2042
+
2043
+ if (pax->sparse_list->is_hole) {
2044
+ /* Current block is hole thus we do not write
2045
+ * the body. */
2046
+ pax->sparse_list->remaining -= ws;
2047
+ total += ws;
2048
+ continue;
2049
+ }
2050
+
2051
+ ret = __archive_write_output(a, p, ws);
2052
+ pax->sparse_list->remaining -= ws;
2053
+ total += ws;
2054
+ if (ret != ARCHIVE_OK)
2055
+ return (ret);
2056
+ }
2057
+ return (total);
2058
+ }
2059
+
2060
+ static int
2061
+ has_non_ASCII(const char *_p)
2062
+ {
2063
+ const unsigned char *p = (const unsigned char *)_p;
2064
+
2065
+ if (p == NULL)
2066
+ return (1);
2067
+ while (*p != '\0' && *p < 128)
2068
+ p++;
2069
+ return (*p != '\0');
2070
+ }
2071
+
2072
+ /*
2073
+ * Used by extended attribute support; encodes the name
2074
+ * so that there will be no '=' characters in the result.
2075
+ */
2076
+ static char *
2077
+ url_encode(const char *in)
2078
+ {
2079
+ const char *s;
2080
+ char *d;
2081
+ size_t out_len = 0;
2082
+ char *out;
2083
+
2084
+ for (s = in; *s != '\0'; s++) {
2085
+ if (*s < 33 || *s > 126 || *s == '%' || *s == '=') {
2086
+ if (archive_ckd_add_size(&out_len, out_len, 3))
2087
+ return (NULL);
2088
+ } else {
2089
+ if (archive_ckd_add_size(&out_len, out_len, 1))
2090
+ return (NULL);
2091
+ }
2092
+ }
2093
+
2094
+ if (archive_ckd_add_size(&out_len, out_len, 1))
2095
+ return (NULL);
2096
+ out = malloc(out_len);
2097
+ if (out == NULL)
2098
+ return (NULL);
2099
+
2100
+ for (s = in, d = out; *s != '\0'; s++) {
2101
+ /* encode any non-printable ASCII character or '%' or '=' */
2102
+ if (*s < 33 || *s > 126 || *s == '%' || *s == '=') {
2103
+ /* URL encoding is '%' followed by two hex digits */
2104
+ *d++ = '%';
2105
+ *d++ = "0123456789ABCDEF"[0x0f & (*s >> 4)];
2106
+ *d++ = "0123456789ABCDEF"[0x0f & *s];
2107
+ } else {
2108
+ *d++ = *s;
2109
+ }
2110
+ }
2111
+ *d = '\0';
2112
+ return (out);
2113
+ }
2114
+
2115
+ /*
2116
+ * Encode a sequence of bytes into a C string using base-64 encoding.
2117
+ *
2118
+ * Returns a null-terminated C string allocated with malloc(); caller
2119
+ * is responsible for freeing the result.
2120
+ */
2121
+ static char *
2122
+ base64_encode(const char *s, size_t len)
2123
+ {
2124
+ static const char digits[64] =
2125
+ { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
2126
+ 'P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d',
2127
+ 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s',
2128
+ 't','u','v','w','x','y','z','0','1','2','3','4','5','6','7',
2129
+ '8','9','+','/' };
2130
+ uint32_t v;
2131
+ char *d, *out;
2132
+ size_t out_len;
2133
+
2134
+ /* 3 bytes becomes 4 chars, but round up and allow for trailing NUL */
2135
+ if (archive_ckd_mul_size(&out_len, len, 4) ||
2136
+ archive_ckd_add_size(&out_len, out_len, 2))
2137
+ return (NULL);
2138
+ out_len = out_len / 3;
2139
+ if (archive_ckd_add_size(&out_len, out_len, 1))
2140
+ return (NULL);
2141
+ out = malloc(out_len);
2142
+ if (out == NULL)
2143
+ return (NULL);
2144
+ d = out;
2145
+
2146
+ /* Convert each group of 3 bytes into 4 characters. */
2147
+ while (len >= 3) {
2148
+ v = ((uint32_t)(unsigned char)s[0] << 16)
2149
+ | ((uint32_t)(unsigned char)s[1] << 8)
2150
+ | (uint32_t)(unsigned char)s[2];
2151
+ s += 3;
2152
+ len -= 3;
2153
+ *d++ = digits[(v >> 18) & 0x3f];
2154
+ *d++ = digits[(v >> 12) & 0x3f];
2155
+ *d++ = digits[(v >> 6) & 0x3f];
2156
+ *d++ = digits[(v) & 0x3f];
2157
+ }
2158
+ /* Handle final group of 1 byte (2 chars) or 2 bytes (3 chars). */
2159
+ switch (len) {
2160
+ case 0: break;
2161
+ case 1:
2162
+ v = ((uint32_t)(unsigned char)s[0] << 16);
2163
+ *d++ = digits[(v >> 18) & 0x3f];
2164
+ *d++ = digits[(v >> 12) & 0x3f];
2165
+ break;
2166
+ case 2:
2167
+ v = ((uint32_t)(unsigned char)s[0] << 16)
2168
+ | ((uint32_t)(unsigned char)s[1] << 8);
2169
+ *d++ = digits[(v >> 18) & 0x3f];
2170
+ *d++ = digits[(v >> 12) & 0x3f];
2171
+ *d++ = digits[(v >> 6) & 0x3f];
2172
+ break;
2173
+ }
2174
+ /* Add trailing NUL character so output is a valid C string. */
2175
+ *d = '\0';
2176
+ return (out);
2177
+ }
2178
+
2179
+ static void
2180
+ sparse_list_clear(struct pax *pax)
2181
+ {
2182
+ while (pax->sparse_list != NULL) {
2183
+ struct sparse_block *sb = pax->sparse_list;
2184
+ pax->sparse_list = sb->next;
2185
+ free(sb);
2186
+ }
2187
+ pax->sparse_tail = NULL;
2188
+ }
2189
+
2190
+ static int
2191
+ _sparse_list_add_block(struct pax *pax, int64_t offset, int64_t length,
2192
+ int is_hole)
2193
+ {
2194
+ struct sparse_block *sb;
2195
+
2196
+ sb = malloc(sizeof(*sb));
2197
+ if (sb == NULL)
2198
+ return (ARCHIVE_FATAL);
2199
+ sb->next = NULL;
2200
+ sb->is_hole = is_hole;
2201
+ sb->offset = offset;
2202
+ sb->remaining = length;
2203
+ if (pax->sparse_list == NULL || pax->sparse_tail == NULL)
2204
+ pax->sparse_list = pax->sparse_tail = sb;
2205
+ else {
2206
+ pax->sparse_tail->next = sb;
2207
+ pax->sparse_tail = sb;
2208
+ }
2209
+ return (ARCHIVE_OK);
2210
+ }
2211
+
2212
+ static int
2213
+ sparse_list_add(struct pax *pax, int64_t offset, int64_t length)
2214
+ {
2215
+ int64_t last_offset;
2216
+ int r;
2217
+
2218
+ if (pax->sparse_tail == NULL)
2219
+ last_offset = 0;
2220
+ else {
2221
+ last_offset = pax->sparse_tail->offset +
2222
+ pax->sparse_tail->remaining;
2223
+ }
2224
+ if (last_offset < offset) {
2225
+ /* Add a hole block. */
2226
+ r = _sparse_list_add_block(pax, last_offset,
2227
+ offset - last_offset, 1);
2228
+ if (r != ARCHIVE_OK)
2229
+ return (r);
2230
+ }
2231
+ /* Add data block. */
2232
+ return (_sparse_list_add_block(pax, offset, length, 0));
2233
+ }
2234
+
2235
+ static time_t
2236
+ get_ustar_max_mtime(void)
2237
+ {
2238
+ /*
2239
+ * Technically, the mtime field in the ustar header can
2240
+ * support 33 bits. We are using all of them to keep
2241
+ * tar/test/test_option_C_mtree.c simple and passing after 2038.
2242
+ * For platforms that use signed 32-bit time values we
2243
+ * use the 32-bit maximum.
2244
+ */
2245
+ if (sizeof(time_t) > sizeof(int32_t))
2246
+ return (time_t)0x1ffffffff;
2247
+ else
2248
+ return (time_t)0x7fffffff;
2249
+ }