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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (266) hide show
  1. package/android/build.gradle +1 -0
  2. package/android/src/main/cpp/CMakeLists.txt +137 -0
  3. package/android/src/main/cpp/PapyrusComicArchiveJni.cpp +68 -0
  4. package/android/src/main/cpp/PapyrusComicArchiveTest.cpp +55 -0
  5. package/android/src/main/java/com/papyrus/engine/PapyrusComicArchiveNative.java +14 -0
  6. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentView.java +353 -0
  7. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentViewManager.java +32 -0
  8. package/android/src/main/java/com/papyrus/engine/PapyrusComicPageLayout.java +17 -0
  9. package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +298 -34
  10. package/android/src/main/java/com/papyrus/engine/PapyrusNativeTextModel.java +195 -0
  11. package/android/src/main/java/com/papyrus/engine/PapyrusPackage.java +3 -1
  12. package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerView.java +24 -0
  13. package/android/src/main/java/com/papyrus/engine/PapyrusRenderMath.java +9 -0
  14. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentView.java +439 -0
  15. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentViewManager.java +43 -0
  16. package/android/src/main/java/com/papyrus/engine/PapyrusTextStore.java +43 -0
  17. package/dist/index.d.mts +439 -3
  18. package/dist/index.d.ts +439 -3
  19. package/dist/index.js +412 -4
  20. package/dist/index.js.map +1 -1
  21. package/dist/index.mjs +404 -3
  22. package/dist/index.mjs.map +1 -1
  23. package/ios/PapyrusComicDocumentView.h +21 -0
  24. package/ios/PapyrusComicDocumentView.m +374 -0
  25. package/ios/PapyrusComicDocumentViewManager.m +23 -0
  26. package/ios/PapyrusEngineStore.m +4 -0
  27. package/ios/PapyrusNativeEngine.m +664 -0
  28. package/ios/PapyrusNativeEngine.podspec +26 -1
  29. package/ios/PapyrusPageRotationRegistry.h +29 -0
  30. package/ios/PapyrusPageRotationRegistry.m +179 -0
  31. package/ios/PapyrusPageView.m +38 -11
  32. package/ios/PapyrusPdfDocumentView.h +2 -0
  33. package/ios/PapyrusPdfDocumentView.m +116 -9
  34. package/ios/PapyrusPdfDocumentViewManager.m +2 -0
  35. package/ios/PapyrusPdfPageTheme.h +15 -0
  36. package/ios/PapyrusPdfPageTheme.m +218 -0
  37. package/ios/PapyrusTextDocumentView.h +27 -0
  38. package/ios/PapyrusTextDocumentView.m +300 -0
  39. package/ios/PapyrusTextDocumentViewManager.m +29 -0
  40. package/ios/PapyrusTextStore.h +13 -0
  41. package/ios/PapyrusTextStore.m +73 -0
  42. package/package.json +5 -3
  43. package/third_party_licenses/BLAKE2-NOTICE.txt +11 -0
  44. package/third_party_licenses/libarchive-COPYING +65 -0
  45. package/vendor/libarchive/PapyrusComicArchive.cpp +376 -0
  46. package/vendor/libarchive/PapyrusComicArchive.h +30 -0
  47. package/vendor/libarchive/README.papyrus.md +15 -0
  48. package/vendor/libarchive/android.h +192 -0
  49. package/vendor/libarchive/config/PapyrusArchiveConfig.h +65 -0
  50. package/vendor/libarchive/libarchive/CMakeLists.txt +293 -0
  51. package/vendor/libarchive/libarchive/android_lf.h +36 -0
  52. package/vendor/libarchive/libarchive/archive.h +1266 -0
  53. package/vendor/libarchive/libarchive/archive_acl.c +2025 -0
  54. package/vendor/libarchive/libarchive/archive_acl_private.h +85 -0
  55. package/vendor/libarchive/libarchive/archive_blake2.h +197 -0
  56. package/vendor/libarchive/libarchive/archive_blake2_impl.h +161 -0
  57. package/vendor/libarchive/libarchive/archive_blake2s_ref.c +369 -0
  58. package/vendor/libarchive/libarchive/archive_blake2sp_ref.c +361 -0
  59. package/vendor/libarchive/libarchive/archive_check_magic.c +183 -0
  60. package/vendor/libarchive/libarchive/archive_cmdline.c +211 -0
  61. package/vendor/libarchive/libarchive/archive_cmdline_private.h +45 -0
  62. package/vendor/libarchive/libarchive/archive_crc32.h +123 -0
  63. package/vendor/libarchive/libarchive/archive_cryptor.c +534 -0
  64. package/vendor/libarchive/libarchive/archive_cryptor_private.h +199 -0
  65. package/vendor/libarchive/libarchive/archive_digest.c +1584 -0
  66. package/vendor/libarchive/libarchive/archive_digest_private.h +431 -0
  67. package/vendor/libarchive/libarchive/archive_disk_acl_darwin.c +558 -0
  68. package/vendor/libarchive/libarchive/archive_disk_acl_freebsd.c +712 -0
  69. package/vendor/libarchive/libarchive/archive_disk_acl_linux.c +760 -0
  70. package/vendor/libarchive/libarchive/archive_disk_acl_sunos.c +824 -0
  71. package/vendor/libarchive/libarchive/archive_endian.h +234 -0
  72. package/vendor/libarchive/libarchive/archive_entry.3 +147 -0
  73. package/vendor/libarchive/libarchive/archive_entry.c +2297 -0
  74. package/vendor/libarchive/libarchive/archive_entry.h +763 -0
  75. package/vendor/libarchive/libarchive/archive_entry_acl.3 +458 -0
  76. package/vendor/libarchive/libarchive/archive_entry_copy_bhfi.c +55 -0
  77. package/vendor/libarchive/libarchive/archive_entry_copy_stat.c +82 -0
  78. package/vendor/libarchive/libarchive/archive_entry_link_resolver.c +472 -0
  79. package/vendor/libarchive/libarchive/archive_entry_linkify.3 +224 -0
  80. package/vendor/libarchive/libarchive/archive_entry_locale.h +90 -0
  81. package/vendor/libarchive/libarchive/archive_entry_misc.3 +63 -0
  82. package/vendor/libarchive/libarchive/archive_entry_paths.3 +153 -0
  83. package/vendor/libarchive/libarchive/archive_entry_perms.3 +210 -0
  84. package/vendor/libarchive/libarchive/archive_entry_private.h +205 -0
  85. package/vendor/libarchive/libarchive/archive_entry_sparse.c +156 -0
  86. package/vendor/libarchive/libarchive/archive_entry_stat.3 +274 -0
  87. package/vendor/libarchive/libarchive/archive_entry_stat.c +121 -0
  88. package/vendor/libarchive/libarchive/archive_entry_strmode.c +69 -0
  89. package/vendor/libarchive/libarchive/archive_entry_time.3 +127 -0
  90. package/vendor/libarchive/libarchive/archive_entry_xattr.c +155 -0
  91. package/vendor/libarchive/libarchive/archive_hmac.c +345 -0
  92. package/vendor/libarchive/libarchive/archive_hmac_private.h +119 -0
  93. package/vendor/libarchive/libarchive/archive_integer.h +267 -0
  94. package/vendor/libarchive/libarchive/archive_match.c +1869 -0
  95. package/vendor/libarchive/libarchive/archive_openssl_evp_private.h +54 -0
  96. package/vendor/libarchive/libarchive/archive_openssl_hmac_private.h +54 -0
  97. package/vendor/libarchive/libarchive/archive_options.c +219 -0
  98. package/vendor/libarchive/libarchive/archive_options_private.h +49 -0
  99. package/vendor/libarchive/libarchive/archive_pack_dev.c +330 -0
  100. package/vendor/libarchive/libarchive/archive_pack_dev.h +49 -0
  101. package/vendor/libarchive/libarchive/archive_parse_date.c +1173 -0
  102. package/vendor/libarchive/libarchive/archive_pathmatch.c +480 -0
  103. package/vendor/libarchive/libarchive/archive_pathmatch.h +50 -0
  104. package/vendor/libarchive/libarchive/archive_platform.h +213 -0
  105. package/vendor/libarchive/libarchive/archive_platform_acl.h +53 -0
  106. package/vendor/libarchive/libarchive/archive_platform_stat.h +45 -0
  107. package/vendor/libarchive/libarchive/archive_platform_xattr.h +45 -0
  108. package/vendor/libarchive/libarchive/archive_ppmd7.c +1173 -0
  109. package/vendor/libarchive/libarchive/archive_ppmd7_private.h +119 -0
  110. package/vendor/libarchive/libarchive/archive_ppmd8.c +1293 -0
  111. package/vendor/libarchive/libarchive/archive_ppmd8_private.h +123 -0
  112. package/vendor/libarchive/libarchive/archive_ppmd_private.h +159 -0
  113. package/vendor/libarchive/libarchive/archive_private.h +209 -0
  114. package/vendor/libarchive/libarchive/archive_random.c +274 -0
  115. package/vendor/libarchive/libarchive/archive_random_private.h +36 -0
  116. package/vendor/libarchive/libarchive/archive_rb.c +709 -0
  117. package/vendor/libarchive/libarchive/archive_rb.h +113 -0
  118. package/vendor/libarchive/libarchive/archive_read.3 +250 -0
  119. package/vendor/libarchive/libarchive/archive_read.c +1814 -0
  120. package/vendor/libarchive/libarchive/archive_read_add_passphrase.3 +72 -0
  121. package/vendor/libarchive/libarchive/archive_read_add_passphrase.c +189 -0
  122. package/vendor/libarchive/libarchive/archive_read_append_filter.c +209 -0
  123. package/vendor/libarchive/libarchive/archive_read_data.3 +128 -0
  124. package/vendor/libarchive/libarchive/archive_read_data_into_fd.c +207 -0
  125. package/vendor/libarchive/libarchive/archive_read_disk.3 +422 -0
  126. package/vendor/libarchive/libarchive/archive_read_disk_entry_from_file.c +1086 -0
  127. package/vendor/libarchive/libarchive/archive_read_disk_posix.c +2639 -0
  128. package/vendor/libarchive/libarchive/archive_read_disk_private.h +97 -0
  129. package/vendor/libarchive/libarchive/archive_read_disk_set_standard_lookup.c +315 -0
  130. package/vendor/libarchive/libarchive/archive_read_disk_windows.c +2603 -0
  131. package/vendor/libarchive/libarchive/archive_read_extract.3 +135 -0
  132. package/vendor/libarchive/libarchive/archive_read_extract.c +59 -0
  133. package/vendor/libarchive/libarchive/archive_read_extract2.c +154 -0
  134. package/vendor/libarchive/libarchive/archive_read_filter.3 +163 -0
  135. package/vendor/libarchive/libarchive/archive_read_format.3 +190 -0
  136. package/vendor/libarchive/libarchive/archive_read_free.3 +91 -0
  137. package/vendor/libarchive/libarchive/archive_read_header.3 +89 -0
  138. package/vendor/libarchive/libarchive/archive_read_new.3 +57 -0
  139. package/vendor/libarchive/libarchive/archive_read_open.3 +247 -0
  140. package/vendor/libarchive/libarchive/archive_read_open_fd.c +236 -0
  141. package/vendor/libarchive/libarchive/archive_read_open_file.c +257 -0
  142. package/vendor/libarchive/libarchive/archive_read_open_filename.c +652 -0
  143. package/vendor/libarchive/libarchive/archive_read_open_memory.c +188 -0
  144. package/vendor/libarchive/libarchive/archive_read_private.h +274 -0
  145. package/vendor/libarchive/libarchive/archive_read_set_format.c +116 -0
  146. package/vendor/libarchive/libarchive/archive_read_set_options.3 +290 -0
  147. package/vendor/libarchive/libarchive/archive_read_set_options.c +132 -0
  148. package/vendor/libarchive/libarchive/archive_read_support_filter_all.c +84 -0
  149. package/vendor/libarchive/libarchive/archive_read_support_filter_by_code.c +68 -0
  150. package/vendor/libarchive/libarchive/archive_read_support_filter_bzip2.c +360 -0
  151. package/vendor/libarchive/libarchive/archive_read_support_filter_compress.c +457 -0
  152. package/vendor/libarchive/libarchive/archive_read_support_filter_grzip.c +108 -0
  153. package/vendor/libarchive/libarchive/archive_read_support_filter_gzip.c +538 -0
  154. package/vendor/libarchive/libarchive/archive_read_support_filter_lrzip.c +119 -0
  155. package/vendor/libarchive/libarchive/archive_read_support_filter_lz4.c +829 -0
  156. package/vendor/libarchive/libarchive/archive_read_support_filter_lzop.c +493 -0
  157. package/vendor/libarchive/libarchive/archive_read_support_filter_none.c +51 -0
  158. package/vendor/libarchive/libarchive/archive_read_support_filter_program.c +497 -0
  159. package/vendor/libarchive/libarchive/archive_read_support_filter_rpm.c +249 -0
  160. package/vendor/libarchive/libarchive/archive_read_support_filter_uu.c +739 -0
  161. package/vendor/libarchive/libarchive/archive_read_support_filter_xz.c +792 -0
  162. package/vendor/libarchive/libarchive/archive_read_support_filter_zstd.c +356 -0
  163. package/vendor/libarchive/libarchive/archive_read_support_format_7zip.c +4650 -0
  164. package/vendor/libarchive/libarchive/archive_read_support_format_all.c +88 -0
  165. package/vendor/libarchive/libarchive/archive_read_support_format_ar.c +597 -0
  166. package/vendor/libarchive/libarchive/archive_read_support_format_by_code.c +76 -0
  167. package/vendor/libarchive/libarchive/archive_read_support_format_cab.c +3410 -0
  168. package/vendor/libarchive/libarchive/archive_read_support_format_cpio.c +1152 -0
  169. package/vendor/libarchive/libarchive/archive_read_support_format_empty.c +95 -0
  170. package/vendor/libarchive/libarchive/archive_read_support_format_iso9660.c +3516 -0
  171. package/vendor/libarchive/libarchive/archive_read_support_format_lha.c +2931 -0
  172. package/vendor/libarchive/libarchive/archive_read_support_format_mtree.c +2113 -0
  173. package/vendor/libarchive/libarchive/archive_read_support_format_rar.c +3932 -0
  174. package/vendor/libarchive/libarchive/archive_read_support_format_rar5.c +4501 -0
  175. package/vendor/libarchive/libarchive/archive_read_support_format_raw.c +194 -0
  176. package/vendor/libarchive/libarchive/archive_read_support_format_tar.c +3785 -0
  177. package/vendor/libarchive/libarchive/archive_read_support_format_warc.c +875 -0
  178. package/vendor/libarchive/libarchive/archive_read_support_format_xar.c +3770 -0
  179. package/vendor/libarchive/libarchive/archive_read_support_format_zip.c +4710 -0
  180. package/vendor/libarchive/libarchive/archive_string.c +4391 -0
  181. package/vendor/libarchive/libarchive/archive_string.h +244 -0
  182. package/vendor/libarchive/libarchive/archive_string_composition.h +2289 -0
  183. package/vendor/libarchive/libarchive/archive_string_sprintf.c +200 -0
  184. package/vendor/libarchive/libarchive/archive_time.c +168 -0
  185. package/vendor/libarchive/libarchive/archive_time_private.h +47 -0
  186. package/vendor/libarchive/libarchive/archive_util.3 +239 -0
  187. package/vendor/libarchive/libarchive/archive_util.c +668 -0
  188. package/vendor/libarchive/libarchive/archive_version_details.c +516 -0
  189. package/vendor/libarchive/libarchive/archive_virtual.c +162 -0
  190. package/vendor/libarchive/libarchive/archive_windows.c +1035 -0
  191. package/vendor/libarchive/libarchive/archive_windows.h +332 -0
  192. package/vendor/libarchive/libarchive/archive_write.3 +266 -0
  193. package/vendor/libarchive/libarchive/archive_write.c +896 -0
  194. package/vendor/libarchive/libarchive/archive_write_add_filter.c +71 -0
  195. package/vendor/libarchive/libarchive/archive_write_add_filter_b64encode.c +329 -0
  196. package/vendor/libarchive/libarchive/archive_write_add_filter_by_name.c +76 -0
  197. package/vendor/libarchive/libarchive/archive_write_add_filter_bzip2.c +411 -0
  198. package/vendor/libarchive/libarchive/archive_write_add_filter_compress.c +459 -0
  199. package/vendor/libarchive/libarchive/archive_write_add_filter_grzip.c +143 -0
  200. package/vendor/libarchive/libarchive/archive_write_add_filter_gzip.c +493 -0
  201. package/vendor/libarchive/libarchive/archive_write_add_filter_lrzip.c +214 -0
  202. package/vendor/libarchive/libarchive/archive_write_add_filter_lz4.c +710 -0
  203. package/vendor/libarchive/libarchive/archive_write_add_filter_lzop.c +500 -0
  204. package/vendor/libarchive/libarchive/archive_write_add_filter_none.c +42 -0
  205. package/vendor/libarchive/libarchive/archive_write_add_filter_program.c +398 -0
  206. package/vendor/libarchive/libarchive/archive_write_add_filter_uuencode.c +320 -0
  207. package/vendor/libarchive/libarchive/archive_write_add_filter_xz.c +547 -0
  208. package/vendor/libarchive/libarchive/archive_write_add_filter_zstd.c +608 -0
  209. package/vendor/libarchive/libarchive/archive_write_blocksize.3 +112 -0
  210. package/vendor/libarchive/libarchive/archive_write_data.3 +88 -0
  211. package/vendor/libarchive/libarchive/archive_write_disk.3 +360 -0
  212. package/vendor/libarchive/libarchive/archive_write_disk_posix.c +4789 -0
  213. package/vendor/libarchive/libarchive/archive_write_disk_private.h +43 -0
  214. package/vendor/libarchive/libarchive/archive_write_disk_set_standard_lookup.c +265 -0
  215. package/vendor/libarchive/libarchive/archive_write_disk_windows.c +2952 -0
  216. package/vendor/libarchive/libarchive/archive_write_filter.3 +140 -0
  217. package/vendor/libarchive/libarchive/archive_write_finish_entry.3 +77 -0
  218. package/vendor/libarchive/libarchive/archive_write_format.3 +187 -0
  219. package/vendor/libarchive/libarchive/archive_write_free.3 +94 -0
  220. package/vendor/libarchive/libarchive/archive_write_header.3 +71 -0
  221. package/vendor/libarchive/libarchive/archive_write_new.3 +56 -0
  222. package/vendor/libarchive/libarchive/archive_write_open.3 +270 -0
  223. package/vendor/libarchive/libarchive/archive_write_open_fd.c +141 -0
  224. package/vendor/libarchive/libarchive/archive_write_open_file.c +102 -0
  225. package/vendor/libarchive/libarchive/archive_write_open_filename.c +272 -0
  226. package/vendor/libarchive/libarchive/archive_write_open_memory.c +110 -0
  227. package/vendor/libarchive/libarchive/archive_write_private.h +166 -0
  228. package/vendor/libarchive/libarchive/archive_write_set_format.c +124 -0
  229. package/vendor/libarchive/libarchive/archive_write_set_format_7zip.c +2571 -0
  230. package/vendor/libarchive/libarchive/archive_write_set_format_ar.c +540 -0
  231. package/vendor/libarchive/libarchive/archive_write_set_format_by_name.c +93 -0
  232. package/vendor/libarchive/libarchive/archive_write_set_format_cpio.c +11 -0
  233. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_binary.c +618 -0
  234. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_newc.c +457 -0
  235. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_odc.c +508 -0
  236. package/vendor/libarchive/libarchive/archive_write_set_format_filter_by_ext.c +141 -0
  237. package/vendor/libarchive/libarchive/archive_write_set_format_gnutar.c +790 -0
  238. package/vendor/libarchive/libarchive/archive_write_set_format_iso9660.c +8216 -0
  239. package/vendor/libarchive/libarchive/archive_write_set_format_mtree.c +2307 -0
  240. package/vendor/libarchive/libarchive/archive_write_set_format_pax.c +2249 -0
  241. package/vendor/libarchive/libarchive/archive_write_set_format_private.h +40 -0
  242. package/vendor/libarchive/libarchive/archive_write_set_format_raw.c +123 -0
  243. package/vendor/libarchive/libarchive/archive_write_set_format_shar.c +650 -0
  244. package/vendor/libarchive/libarchive/archive_write_set_format_ustar.c +768 -0
  245. package/vendor/libarchive/libarchive/archive_write_set_format_v7tar.c +648 -0
  246. package/vendor/libarchive/libarchive/archive_write_set_format_warc.c +491 -0
  247. package/vendor/libarchive/libarchive/archive_write_set_format_xar.c +3799 -0
  248. package/vendor/libarchive/libarchive/archive_write_set_format_zip.c +2548 -0
  249. package/vendor/libarchive/libarchive/archive_write_set_options.3 +779 -0
  250. package/vendor/libarchive/libarchive/archive_write_set_options.c +129 -0
  251. package/vendor/libarchive/libarchive/archive_write_set_passphrase.3 +72 -0
  252. package/vendor/libarchive/libarchive/archive_write_set_passphrase.c +95 -0
  253. package/vendor/libarchive/libarchive/archive_xxhash.h +48 -0
  254. package/vendor/libarchive/libarchive/config_freebsd.h +275 -0
  255. package/vendor/libarchive/libarchive/cpio.5 +389 -0
  256. package/vendor/libarchive/libarchive/filter_fork.h +44 -0
  257. package/vendor/libarchive/libarchive/filter_fork_posix.c +269 -0
  258. package/vendor/libarchive/libarchive/filter_fork_windows.c +270 -0
  259. package/vendor/libarchive/libarchive/libarchive-formats.5 +478 -0
  260. package/vendor/libarchive/libarchive/libarchive.3 +297 -0
  261. package/vendor/libarchive/libarchive/libarchive_changes.3 +339 -0
  262. package/vendor/libarchive/libarchive/libarchive_internals.3 +363 -0
  263. package/vendor/libarchive/libarchive/module.modulemap +34 -0
  264. package/vendor/libarchive/libarchive/mtree.5 +323 -0
  265. package/vendor/libarchive/libarchive/tar.5 +947 -0
  266. package/vendor/libarchive/libarchive/xxhash.c +531 -0
@@ -0,0 +1,790 @@
1
+ /*-
2
+ * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
3
+ * Author: Jonas Gastal <jgastal@profusion.mobi>
4
+ * Copyright (c) 2011-2012 Michihiro NAKAJIMA
5
+ *
6
+ * All rights reserved.
7
+ *
8
+ * Redistribution and use in source and binary forms, with or without
9
+ * modification, are permitted provided that the following conditions
10
+ * are met:
11
+ * 1. Redistributions of source code must retain the above copyright
12
+ * notice, this list of conditions and the following disclaimer.
13
+ * 2. Redistributions in binary form must reproduce the above copyright
14
+ * notice, this list of conditions and the following disclaimer in the
15
+ * documentation and/or other materials provided with the distribution.
16
+ *
17
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
18
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
21
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ */
28
+
29
+ #include "archive_platform.h"
30
+
31
+ #ifdef HAVE_ERRNO_H
32
+ #include <errno.h>
33
+ #endif
34
+ #include <stdio.h>
35
+ #ifdef HAVE_STDLIB_H
36
+ #include <stdlib.h>
37
+ #endif
38
+ #ifdef HAVE_STRING_H
39
+ #include <string.h>
40
+ #endif
41
+
42
+ #include "archive.h"
43
+ #include "archive_entry.h"
44
+ #include "archive_entry_locale.h"
45
+ #include "archive_private.h"
46
+ #include "archive_write_private.h"
47
+ #include "archive_write_set_format_private.h"
48
+
49
+ struct gnutar {
50
+ uint64_t entry_bytes_remaining;
51
+ uint64_t entry_padding;
52
+ const char * linkname;
53
+ size_t linkname_length;
54
+ const char * pathname;
55
+ size_t pathname_length;
56
+ const char * uname;
57
+ size_t uname_length;
58
+ const char * gname;
59
+ size_t gname_length;
60
+ struct archive_string_conv *opt_sconv;
61
+ struct archive_string_conv *sconv_default;
62
+ int init_default_conversion;
63
+ };
64
+
65
+ /*
66
+ * Define structure of GNU tar header.
67
+ */
68
+ #define GNUTAR_name_offset 0
69
+ #define GNUTAR_name_size 100
70
+ #define GNUTAR_mode_offset 100
71
+ #define GNUTAR_mode_size 7
72
+ #define GNUTAR_mode_max_size 8
73
+ #define GNUTAR_uid_offset 108
74
+ #define GNUTAR_uid_size 7
75
+ #define GNUTAR_uid_max_size 8
76
+ #define GNUTAR_gid_offset 116
77
+ #define GNUTAR_gid_size 7
78
+ #define GNUTAR_gid_max_size 8
79
+ #define GNUTAR_size_offset 124
80
+ #define GNUTAR_size_size 11
81
+ #define GNUTAR_size_max_size 12
82
+ #define GNUTAR_mtime_offset 136
83
+ #define GNUTAR_mtime_size 11
84
+ #define GNUTAR_mtime_max_size 11
85
+ #define GNUTAR_checksum_offset 148
86
+ #define GNUTAR_checksum_size 8
87
+ #define GNUTAR_typeflag_offset 156
88
+ #define GNUTAR_typeflag_size 1
89
+ #define GNUTAR_linkname_offset 157
90
+ #define GNUTAR_linkname_size 100
91
+ #define GNUTAR_magic_offset 257
92
+ #define GNUTAR_magic_size 6
93
+ #define GNUTAR_version_offset 263
94
+ #define GNUTAR_version_size 2
95
+ #define GNUTAR_uname_offset 265
96
+ #define GNUTAR_uname_size 32
97
+ #define GNUTAR_gname_offset 297
98
+ #define GNUTAR_gname_size 32
99
+ #define GNUTAR_rdevmajor_offset 329
100
+ #define GNUTAR_rdevmajor_size 6
101
+ #define GNUTAR_rdevmajor_max_size 8
102
+ #define GNUTAR_rdevminor_offset 337
103
+ #define GNUTAR_rdevminor_size 6
104
+ #define GNUTAR_rdevminor_max_size 8
105
+
106
+ /*
107
+ * A filled-in copy of the header for initialization.
108
+ */
109
+ static const char template_header[] = {
110
+ /* name: 100 bytes */
111
+ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
112
+ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
113
+ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
114
+ 0,0,0,0,
115
+ /* Mode, null termination: 8 bytes */
116
+ '0','0','0','0','0','0', '0','\0',
117
+ /* uid, null termination: 8 bytes */
118
+ '0','0','0','0','0','0', '0','\0',
119
+ /* gid, null termination: 8 bytes */
120
+ '0','0','0','0','0','0', '0','\0',
121
+ /* size, space termination: 12 bytes */
122
+ '0','0','0','0','0','0','0','0','0','0','0', '\0',
123
+ /* mtime, space termination: 12 bytes */
124
+ '0','0','0','0','0','0','0','0','0','0','0', '\0',
125
+ /* Initial checksum value: 8 spaces */
126
+ ' ',' ',' ',' ',' ',' ',' ',' ',
127
+ /* Typeflag: 1 byte */
128
+ '0', /* '0' = regular file */
129
+ /* Linkname: 100 bytes */
130
+ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
131
+ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
132
+ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
133
+ 0,0,0,0,
134
+ /* Magic: 8 bytes */
135
+ 'u','s','t','a','r',' ', ' ','\0',
136
+ /* Uname: 32 bytes */
137
+ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
138
+ /* Gname: 32 bytes */
139
+ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
140
+ /* rdevmajor + null padding: 8 bytes */
141
+ '\0','\0','\0','\0','\0','\0', '\0','\0',
142
+ /* rdevminor + null padding: 8 bytes */
143
+ '\0','\0','\0','\0','\0','\0', '\0','\0',
144
+ /* Padding: 167 bytes */
145
+ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
146
+ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
147
+ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
148
+ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
149
+ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
150
+ 0,0,0,0,0,0,0
151
+ };
152
+
153
+ static int archive_write_gnutar_options(struct archive_write *,
154
+ const char *, const char *);
155
+ static int archive_format_gnutar_header(struct archive_write *, char h[512],
156
+ struct archive_entry *, char tartype);
157
+ static int archive_write_gnutar_header(struct archive_write *,
158
+ struct archive_entry *entry);
159
+ static ssize_t archive_write_gnutar_data(struct archive_write *a, const void *buff,
160
+ size_t s);
161
+ static int archive_write_gnutar_free(struct archive_write *);
162
+ static int archive_write_gnutar_close(struct archive_write *);
163
+ static int archive_write_gnutar_finish_entry(struct archive_write *);
164
+ static int format_256(int64_t, char *, int);
165
+ static int format_number(int64_t, char *, int size, int maxsize);
166
+ static int format_octal(int64_t, char *, int);
167
+
168
+ /*
169
+ * Set output format to 'GNU tar' format.
170
+ */
171
+ int
172
+ archive_write_set_format_gnutar(struct archive *_a)
173
+ {
174
+ struct archive_write *a = (struct archive_write *)_a;
175
+ struct gnutar *gnutar;
176
+
177
+ archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
178
+ ARCHIVE_STATE_NEW, "archive_write_set_format_gnutar");
179
+
180
+ (void)__archive_write_unregister_format(a);
181
+
182
+ gnutar = calloc(1, sizeof(*gnutar));
183
+ if (gnutar == NULL) {
184
+ archive_set_error(&a->archive, ENOMEM,
185
+ "Can't allocate gnutar data");
186
+ return (ARCHIVE_FATAL);
187
+ }
188
+ a->format_data = gnutar;
189
+ a->format_name = "gnutar";
190
+ a->format_options = archive_write_gnutar_options;
191
+ a->format_write_header = archive_write_gnutar_header;
192
+ a->format_write_data = archive_write_gnutar_data;
193
+ a->format_close = archive_write_gnutar_close;
194
+ a->format_free = archive_write_gnutar_free;
195
+ a->format_finish_entry = archive_write_gnutar_finish_entry;
196
+ a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR;
197
+ a->archive.archive_format_name = "GNU tar";
198
+ return (ARCHIVE_OK);
199
+ }
200
+
201
+ static int
202
+ archive_write_gnutar_options(struct archive_write *a, const char *key,
203
+ const char *val)
204
+ {
205
+ struct gnutar *gnutar = a->format_data;
206
+ int ret = ARCHIVE_FAILED;
207
+
208
+ if (strcmp(key, "hdrcharset") == 0) {
209
+ if (val == NULL || val[0] == 0)
210
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
211
+ "%s: hdrcharset option needs a character-set name",
212
+ a->format_name);
213
+ else {
214
+ gnutar->opt_sconv = archive_string_conversion_to_charset(
215
+ &a->archive, val, 0);
216
+ if (gnutar->opt_sconv != NULL)
217
+ ret = ARCHIVE_OK;
218
+ else
219
+ ret = ARCHIVE_FATAL;
220
+ }
221
+ return (ret);
222
+ }
223
+
224
+ /* Note: The "warn" return is just to inform the options
225
+ * supervisor that we didn't handle it. It will generate
226
+ * a suitable error if no one used this option. */
227
+ return (ARCHIVE_WARN);
228
+ }
229
+
230
+ static int
231
+ archive_write_gnutar_close(struct archive_write *a)
232
+ {
233
+ return (__archive_write_nulls(a, 512*2));
234
+ }
235
+
236
+ static int
237
+ archive_write_gnutar_free(struct archive_write *a)
238
+ {
239
+ struct gnutar *gnutar = a->format_data;
240
+
241
+ free(gnutar);
242
+ a->format_data = NULL;
243
+ return (ARCHIVE_OK);
244
+ }
245
+
246
+ static int
247
+ archive_write_gnutar_finish_entry(struct archive_write *a)
248
+ {
249
+ struct gnutar *gnutar = a->format_data;
250
+ int ret;
251
+
252
+ ret = __archive_write_nulls(a,
253
+ gnutar->entry_bytes_remaining + gnutar->entry_padding);
254
+ gnutar->entry_bytes_remaining = gnutar->entry_padding = 0;
255
+ return (ret);
256
+ }
257
+
258
+ static ssize_t
259
+ archive_write_gnutar_data(struct archive_write *a, const void *buff, size_t s)
260
+ {
261
+ struct gnutar *gnutar = a->format_data;
262
+ int ret;
263
+
264
+ if (s > gnutar->entry_bytes_remaining)
265
+ s = (size_t)gnutar->entry_bytes_remaining;
266
+ ret = __archive_write_output(a, buff, s);
267
+ gnutar->entry_bytes_remaining -= s;
268
+ if (ret != ARCHIVE_OK)
269
+ return (ret);
270
+ return (s);
271
+ }
272
+
273
+ static int
274
+ archive_write_gnutar_header(struct archive_write *a,
275
+ struct archive_entry *entry)
276
+ {
277
+ struct gnutar *gnutar = a->format_data;
278
+ char buff[512];
279
+ int r, ret, ret2 = ARCHIVE_OK;
280
+ char tartype;
281
+ struct archive_string_conv *sconv;
282
+ struct archive_entry *entry_main;
283
+
284
+ /* Setup default string conversion. */
285
+ if (gnutar->opt_sconv == NULL) {
286
+ if (!gnutar->init_default_conversion) {
287
+ gnutar->sconv_default =
288
+ archive_string_default_conversion_for_write(
289
+ &(a->archive));
290
+ gnutar->init_default_conversion = 1;
291
+ }
292
+ sconv = gnutar->sconv_default;
293
+ } else
294
+ sconv = gnutar->opt_sconv;
295
+
296
+ /* Sanity check. */
297
+ if (archive_entry_pathname(entry) == NULL
298
+ #if defined(_WIN32) && !defined(__CYGWIN__)
299
+ && archive_entry_pathname_w(entry) == NULL
300
+ #endif
301
+ ) {
302
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
303
+ "Can't record entry in tar file without pathname");
304
+ return ARCHIVE_FAILED;
305
+ }
306
+
307
+ /* Only regular files (not hardlinks) have data. */
308
+ if (archive_entry_hardlink(entry) != NULL ||
309
+ archive_entry_symlink(entry) != NULL ||
310
+ archive_entry_filetype(entry) != AE_IFREG)
311
+ archive_entry_set_size(entry, 0);
312
+
313
+ if (AE_IFDIR == archive_entry_filetype(entry)) {
314
+ const char *p;
315
+ size_t path_length;
316
+ /*
317
+ * Ensure a trailing '/'. Modify the entry so
318
+ * the client sees the change.
319
+ */
320
+ #if defined(_WIN32) && !defined(__CYGWIN__)
321
+ const wchar_t *wp;
322
+
323
+ wp = archive_entry_pathname_w(entry);
324
+ if (wp != NULL && wp[0] != L'\0' &&
325
+ wp[wcslen(wp) - 1] != L'/') {
326
+ struct archive_wstring ws;
327
+
328
+ archive_string_init(&ws);
329
+ path_length = wcslen(wp);
330
+ if (archive_wstring_ensure(&ws,
331
+ path_length + 2) == NULL) {
332
+ archive_set_error(&a->archive, ENOMEM,
333
+ "Can't allocate ustar data");
334
+ archive_wstring_free(&ws);
335
+ return(ARCHIVE_FATAL);
336
+ }
337
+ /* Should we keep '\' ? */
338
+ if (wp[path_length -1] == L'\\')
339
+ path_length--;
340
+ archive_wstrncpy(&ws, wp, path_length);
341
+ archive_wstrappend_wchar(&ws, L'/');
342
+ archive_entry_copy_pathname_w(entry, ws.s);
343
+ archive_wstring_free(&ws);
344
+ p = NULL;
345
+ } else
346
+ #endif
347
+ p = archive_entry_pathname(entry);
348
+ /*
349
+ * On Windows, this is a backup operation just in
350
+ * case getting WCS failed. On POSIX, this is a
351
+ * normal operation.
352
+ */
353
+ if (p != NULL && p[0] != '\0' && p[strlen(p) - 1] != '/') {
354
+ struct archive_string as;
355
+
356
+ archive_string_init(&as);
357
+ path_length = strlen(p);
358
+ if (archive_string_ensure(&as,
359
+ path_length + 2) == NULL) {
360
+ archive_set_error(&a->archive, ENOMEM,
361
+ "Can't allocate ustar data");
362
+ archive_string_free(&as);
363
+ return(ARCHIVE_FATAL);
364
+ }
365
+ #if defined(_WIN32) && !defined(__CYGWIN__)
366
+ /* NOTE: This might break the pathname
367
+ * if the current code page is CP932 and
368
+ * the pathname includes a character '\'
369
+ * as a part of its multibyte pathname. */
370
+ if (p[strlen(p) -1] == '\\')
371
+ path_length--;
372
+ else
373
+ #endif
374
+ archive_strncpy(&as, p, path_length);
375
+ archive_strappend_char(&as, '/');
376
+ archive_entry_copy_pathname(entry, as.s);
377
+ archive_string_free(&as);
378
+ }
379
+ }
380
+
381
+ #if defined(_WIN32) && !defined(__CYGWIN__)
382
+ /* Make sure the path separators in pathname, hardlink and symlink
383
+ * are all slash '/', not the Windows path separator '\'. */
384
+ entry_main = __la_win_entry_in_posix_pathseparator(entry);
385
+ if (entry_main == NULL) {
386
+ archive_set_error(&a->archive, ENOMEM,
387
+ "Can't allocate ustar data");
388
+ return(ARCHIVE_FATAL);
389
+ }
390
+ if (entry != entry_main)
391
+ entry = entry_main;
392
+ else
393
+ entry_main = NULL;
394
+ #else
395
+ entry_main = NULL;
396
+ #endif
397
+ r = archive_entry_pathname_l(entry, &(gnutar->pathname),
398
+ &(gnutar->pathname_length), sconv);
399
+ if (r != 0) {
400
+ const char* p_mbs;
401
+ if (errno == ENOMEM) {
402
+ archive_set_error(&a->archive, ENOMEM,
403
+ "Can't allocate memory for pathname");
404
+ ret = ARCHIVE_FATAL;
405
+ goto exit_write_header;
406
+ }
407
+ p_mbs = archive_entry_pathname(entry);
408
+ if (p_mbs) {
409
+ /* We have a wrongly-encoded MBS pathname.
410
+ * Warn and use it. */
411
+ archive_set_error(&a->archive,
412
+ ARCHIVE_ERRNO_FILE_FORMAT,
413
+ "Can't translate pathname '%s' to %s", p_mbs,
414
+ archive_string_conversion_charset_name(sconv));
415
+ ret2 = ARCHIVE_WARN;
416
+ } else {
417
+ /* We have no MBS pathname. Fail. */
418
+ archive_set_error(&a->archive,
419
+ ARCHIVE_ERRNO_FILE_FORMAT,
420
+ "Can't translate pathname to %s",
421
+ archive_string_conversion_charset_name(sconv));
422
+ return ARCHIVE_FAILED;
423
+ }
424
+ }
425
+ r = archive_entry_uname_l(entry, &(gnutar->uname),
426
+ &(gnutar->uname_length), sconv);
427
+ if (r != 0) {
428
+ if (errno == ENOMEM) {
429
+ archive_set_error(&a->archive, ENOMEM,
430
+ "Can't allocate memory for Uname");
431
+ ret = ARCHIVE_FATAL;
432
+ goto exit_write_header;
433
+ }
434
+ archive_set_error(&a->archive,
435
+ ARCHIVE_ERRNO_FILE_FORMAT,
436
+ "Can't translate uname '%s' to %s",
437
+ archive_entry_uname(entry),
438
+ archive_string_conversion_charset_name(sconv));
439
+ ret2 = ARCHIVE_WARN;
440
+ }
441
+ r = archive_entry_gname_l(entry, &(gnutar->gname),
442
+ &(gnutar->gname_length), sconv);
443
+ if (r != 0) {
444
+ if (errno == ENOMEM) {
445
+ archive_set_error(&a->archive, ENOMEM,
446
+ "Can't allocate memory for Gname");
447
+ ret = ARCHIVE_FATAL;
448
+ goto exit_write_header;
449
+ }
450
+ archive_set_error(&a->archive,
451
+ ARCHIVE_ERRNO_FILE_FORMAT,
452
+ "Can't translate gname '%s' to %s",
453
+ archive_entry_gname(entry),
454
+ archive_string_conversion_charset_name(sconv));
455
+ ret2 = ARCHIVE_WARN;
456
+ }
457
+
458
+ /* If linkname is longer than 100 chars we need to add a 'K' header. */
459
+ r = archive_entry_hardlink_l(entry, &(gnutar->linkname),
460
+ &(gnutar->linkname_length), sconv);
461
+ if (r != 0) {
462
+ if (errno == ENOMEM) {
463
+ archive_set_error(&a->archive, ENOMEM,
464
+ "Can't allocate memory for Linkname");
465
+ ret = ARCHIVE_FATAL;
466
+ goto exit_write_header;
467
+ }
468
+ archive_set_error(&a->archive,
469
+ ARCHIVE_ERRNO_FILE_FORMAT,
470
+ "Can't translate linkname '%s' to %s",
471
+ archive_entry_hardlink(entry),
472
+ archive_string_conversion_charset_name(sconv));
473
+ ret2 = ARCHIVE_WARN;
474
+ }
475
+ if (gnutar->linkname_length == 0) {
476
+ r = archive_entry_symlink_l(entry, &(gnutar->linkname),
477
+ &(gnutar->linkname_length), sconv);
478
+ if (r != 0) {
479
+ if (errno == ENOMEM) {
480
+ archive_set_error(&a->archive, ENOMEM,
481
+ "Can't allocate memory for Linkname");
482
+ ret = ARCHIVE_FATAL;
483
+ goto exit_write_header;
484
+ }
485
+ archive_set_error(&a->archive,
486
+ ARCHIVE_ERRNO_FILE_FORMAT,
487
+ "Can't translate linkname '%s' to %s",
488
+ archive_entry_hardlink(entry),
489
+ archive_string_conversion_charset_name(sconv));
490
+ ret2 = ARCHIVE_WARN;
491
+ }
492
+ }
493
+ if (gnutar->linkname_length > GNUTAR_linkname_size) {
494
+ size_t length = gnutar->linkname_length + 1;
495
+ struct archive_entry *temp = archive_entry_new2(&a->archive);
496
+
497
+ if (temp == NULL) {
498
+ archive_set_error(&a->archive, ENOMEM,
499
+ "Can't allocate memory for Linkname");
500
+ ret = ARCHIVE_FATAL;
501
+ goto exit_write_header;
502
+ }
503
+
504
+ /* Uname/gname here don't really matter since no one reads them;
505
+ * these are the values that GNU tar happens to use on FreeBSD. */
506
+ archive_entry_set_uname(temp, "root");
507
+ archive_entry_set_gname(temp, "wheel");
508
+
509
+ archive_entry_set_pathname(temp, "././@LongLink");
510
+ archive_entry_set_size(temp, length);
511
+ ret = archive_format_gnutar_header(a, buff, temp, 'K');
512
+ archive_entry_free(temp);
513
+ if (ret < ARCHIVE_WARN)
514
+ goto exit_write_header;
515
+ ret = __archive_write_output(a, buff, 512);
516
+ if (ret < ARCHIVE_WARN)
517
+ goto exit_write_header;
518
+ /* Write name and trailing null byte. */
519
+ ret = __archive_write_output(a, gnutar->linkname, length);
520
+ if (ret < ARCHIVE_WARN)
521
+ goto exit_write_header;
522
+ /* Pad to 512 bytes */
523
+ ret = __archive_write_nulls(a, 0x1ff & (-(ssize_t)length));
524
+ if (ret < ARCHIVE_WARN)
525
+ goto exit_write_header;
526
+ }
527
+
528
+ /* If pathname is longer than 100 chars we need to add an 'L' header. */
529
+ if (gnutar->pathname_length > GNUTAR_name_size) {
530
+ const char *pathname = gnutar->pathname;
531
+ size_t length = gnutar->pathname_length + 1;
532
+ struct archive_entry *temp = archive_entry_new2(&a->archive);
533
+
534
+ if (temp == NULL) {
535
+ archive_set_error(&a->archive, ENOMEM,
536
+ "Can't allocate memory for Linkname");
537
+ ret = ARCHIVE_FATAL;
538
+ goto exit_write_header;
539
+ }
540
+
541
+ /* Uname/gname here don't really matter since no one reads them;
542
+ * these are the values that GNU tar happens to use on FreeBSD. */
543
+ archive_entry_set_uname(temp, "root");
544
+ archive_entry_set_gname(temp, "wheel");
545
+
546
+ archive_entry_set_pathname(temp, "././@LongName");
547
+ archive_entry_set_size(temp, length);
548
+ ret = archive_format_gnutar_header(a, buff, temp, 'L');
549
+ archive_entry_free(temp);
550
+ if (ret < ARCHIVE_WARN)
551
+ goto exit_write_header;
552
+ ret = __archive_write_output(a, buff, 512);
553
+ if(ret < ARCHIVE_WARN)
554
+ goto exit_write_header;
555
+ /* Write pathname + trailing null byte. */
556
+ ret = __archive_write_output(a, pathname, length);
557
+ if(ret < ARCHIVE_WARN)
558
+ goto exit_write_header;
559
+ /* Pad to multiple of 512 bytes. */
560
+ ret = __archive_write_nulls(a, 0x1ff & (-(ssize_t)length));
561
+ if (ret < ARCHIVE_WARN)
562
+ goto exit_write_header;
563
+ }
564
+
565
+ if (archive_entry_hardlink_is_set(entry)) {
566
+ tartype = '1';
567
+ } else
568
+ switch (archive_entry_filetype(entry)) {
569
+ case AE_IFREG: tartype = '0' ; break;
570
+ case AE_IFLNK: tartype = '2' ; break;
571
+ case AE_IFCHR: tartype = '3' ; break;
572
+ case AE_IFBLK: tartype = '4' ; break;
573
+ case AE_IFDIR: tartype = '5' ; break;
574
+ case AE_IFIFO: tartype = '6' ; break;
575
+ default: /* AE_IFSOCK and unknown */
576
+ __archive_write_entry_filetype_unsupported(
577
+ &a->archive, entry, "gnutar");
578
+ ret = ARCHIVE_FAILED;
579
+ goto exit_write_header;
580
+ }
581
+
582
+ ret = archive_format_gnutar_header(a, buff, entry, tartype);
583
+ if (ret < ARCHIVE_WARN)
584
+ goto exit_write_header;
585
+ if (ret2 < ret)
586
+ ret = ret2;
587
+ ret2 = __archive_write_output(a, buff, 512);
588
+ if (ret2 < ARCHIVE_WARN) {
589
+ ret = ret2;
590
+ goto exit_write_header;
591
+ }
592
+ if (ret2 < ret)
593
+ ret = ret2;
594
+
595
+ gnutar->entry_bytes_remaining = archive_entry_size(entry);
596
+ gnutar->entry_padding = 0x1ff & (-(int64_t)gnutar->entry_bytes_remaining);
597
+ exit_write_header:
598
+ archive_entry_free(entry_main);
599
+ return (ret);
600
+ }
601
+
602
+ static int
603
+ archive_format_gnutar_header(struct archive_write *a, char h[512],
604
+ struct archive_entry *entry, char tartype)
605
+ {
606
+ struct gnutar *gnutar = a->format_data;
607
+ unsigned int checksum;
608
+ int i, ret;
609
+ size_t copy_length;
610
+ const char *p;
611
+
612
+ ret = 0;
613
+
614
+ /*
615
+ * The "template header" already includes the signature,
616
+ * various end-of-field markers, and other required elements.
617
+ */
618
+ memcpy(h, &template_header, 512);
619
+
620
+ /*
621
+ * Because the block is already null-filled, and strings
622
+ * are allowed to exactly fill their destination (without null),
623
+ * I use memcpy(dest, src, strlen()) here a lot to copy strings.
624
+ */
625
+
626
+ if (tartype == 'K' || tartype == 'L') {
627
+ p = archive_entry_pathname(entry);
628
+ copy_length = strlen(p);
629
+ } else {
630
+ p = gnutar->pathname;
631
+ copy_length = gnutar->pathname_length;
632
+ }
633
+ if (copy_length > GNUTAR_name_size)
634
+ copy_length = GNUTAR_name_size;
635
+ memcpy(h + GNUTAR_name_offset, p, copy_length);
636
+
637
+ if ((copy_length = gnutar->linkname_length) > 0) {
638
+ if (copy_length > GNUTAR_linkname_size)
639
+ copy_length = GNUTAR_linkname_size;
640
+ memcpy(h + GNUTAR_linkname_offset, gnutar->linkname,
641
+ copy_length);
642
+ }
643
+
644
+ /* TODO: How does GNU tar handle unames longer than GNUTAR_uname_size? */
645
+ if (tartype == 'K' || tartype == 'L') {
646
+ p = archive_entry_uname(entry);
647
+ copy_length = strlen(p);
648
+ } else {
649
+ p = gnutar->uname;
650
+ copy_length = gnutar->uname_length;
651
+ }
652
+ if (copy_length > 0) {
653
+ if (copy_length > GNUTAR_uname_size)
654
+ copy_length = GNUTAR_uname_size;
655
+ memcpy(h + GNUTAR_uname_offset, p, copy_length);
656
+ }
657
+
658
+ /* TODO: How does GNU tar handle gnames longer than GNUTAR_gname_size? */
659
+ if (tartype == 'K' || tartype == 'L') {
660
+ p = archive_entry_gname(entry);
661
+ copy_length = strlen(p);
662
+ } else {
663
+ p = gnutar->gname;
664
+ copy_length = gnutar->gname_length;
665
+ }
666
+ if (copy_length > 0) {
667
+ if (copy_length > GNUTAR_gname_size)
668
+ copy_length = GNUTAR_gname_size;
669
+ memcpy(h + GNUTAR_gname_offset, p, copy_length);
670
+ }
671
+
672
+ /* By truncating the mode here, we ensure it always fits. */
673
+ format_octal(archive_entry_mode(entry) & 07777,
674
+ h + GNUTAR_mode_offset, GNUTAR_mode_size);
675
+
676
+ /* GNU tar supports base-256 here, so should never overflow. */
677
+ if (format_number(archive_entry_uid(entry), h + GNUTAR_uid_offset,
678
+ GNUTAR_uid_size, GNUTAR_uid_max_size)) {
679
+ archive_set_error(&a->archive, ERANGE,
680
+ "Numeric user ID %jd too large for gnutar format",
681
+ (intmax_t)archive_entry_uid(entry));
682
+ ret = ARCHIVE_FAILED;
683
+ }
684
+
685
+ /* GNU tar supports base-256 here, so should never overflow. */
686
+ if (format_number(archive_entry_gid(entry), h + GNUTAR_gid_offset,
687
+ GNUTAR_gid_size, GNUTAR_gid_max_size)) {
688
+ archive_set_error(&a->archive, ERANGE,
689
+ "Numeric group ID %jd too large for gnutar format",
690
+ (intmax_t)archive_entry_gid(entry));
691
+ ret = ARCHIVE_FAILED;
692
+ }
693
+
694
+ /* GNU tar supports base-256 here, so should never overflow. */
695
+ if (format_number(archive_entry_size(entry), h + GNUTAR_size_offset,
696
+ GNUTAR_size_size, GNUTAR_size_max_size)) {
697
+ archive_set_error(&a->archive, ERANGE,
698
+ "File size out of range");
699
+ ret = ARCHIVE_FAILED;
700
+ }
701
+
702
+ /* Shouldn't overflow before 2106, since mtime field is 33 bits. */
703
+ format_octal(archive_entry_mtime(entry),
704
+ h + GNUTAR_mtime_offset, GNUTAR_mtime_size);
705
+
706
+ if (archive_entry_filetype(entry) == AE_IFBLK
707
+ || archive_entry_filetype(entry) == AE_IFCHR) {
708
+ if (format_octal(archive_entry_rdevmajor(entry),
709
+ h + GNUTAR_rdevmajor_offset,
710
+ GNUTAR_rdevmajor_size)) {
711
+ archive_set_error(&a->archive, ERANGE,
712
+ "Major device number too large for gnutar format");
713
+ ret = ARCHIVE_FAILED;
714
+ }
715
+
716
+ if (format_octal(archive_entry_rdevminor(entry),
717
+ h + GNUTAR_rdevminor_offset,
718
+ GNUTAR_rdevminor_size)) {
719
+ archive_set_error(&a->archive, ERANGE,
720
+ "Minor device number too large for gnutar format");
721
+ ret = ARCHIVE_FAILED;
722
+ }
723
+ }
724
+
725
+ h[GNUTAR_typeflag_offset] = tartype;
726
+
727
+ checksum = 0;
728
+ for (i = 0; i < 512; i++)
729
+ checksum += 255 & (unsigned int)h[i];
730
+ h[GNUTAR_checksum_offset + 6] = '\0'; /* Can't be pre-set in the template. */
731
+ /* h[GNUTAR_checksum_offset + 7] = ' '; */ /* This is pre-set in the template. */
732
+ format_octal(checksum, h + GNUTAR_checksum_offset, 6);
733
+ return (ret);
734
+ }
735
+
736
+ /*
737
+ * Format a number into a field, falling back to base-256 if necessary.
738
+ */
739
+ static int
740
+ format_number(int64_t v, char *p, int s, int maxsize)
741
+ {
742
+ int64_t limit = ((int64_t)1 << (s*3));
743
+
744
+ if (v < limit)
745
+ return (format_octal(v, p, s));
746
+ return (format_256(v, p, maxsize));
747
+ }
748
+
749
+ /*
750
+ * Format a number into the specified field using base-256.
751
+ */
752
+ static int
753
+ format_256(int64_t v, char *p, int s)
754
+ {
755
+ p += s;
756
+ while (s-- > 0) {
757
+ *--p = (char)(v & 0xff);
758
+ v >>= 8;
759
+ }
760
+ *p |= 0x80; /* Set the base-256 marker bit. */
761
+ return (0);
762
+ }
763
+
764
+ /*
765
+ * Format a number into the specified field using octal.
766
+ */
767
+ static int
768
+ format_octal(int64_t v, char *p, int s)
769
+ {
770
+ int len = s;
771
+
772
+ /* Octal values can't be negative, so use 0. */
773
+ if (v < 0)
774
+ v = 0;
775
+
776
+ p += s; /* Start at the end and work backwards. */
777
+ while (s-- > 0) {
778
+ *--p = (char)('0' + (v & 7));
779
+ v >>= 3;
780
+ }
781
+
782
+ if (v == 0)
783
+ return (0);
784
+
785
+ /* If it overflowed, fill field with max value. */
786
+ while (len-- > 0)
787
+ *p++ = '7';
788
+
789
+ return (-1);
790
+ }