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

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/android/build.gradle +0 -1
  2. package/android/src/main/cpp/CMakeLists.txt +0 -137
  3. package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +35 -305
  4. package/android/src/main/java/com/papyrus/engine/PapyrusPackage.java +1 -3
  5. package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerView.java +5 -195
  6. package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerViewManager.java +1 -20
  7. package/dist/index.d.mts +4 -614
  8. package/dist/index.d.ts +4 -614
  9. package/dist/index.js +8 -484
  10. package/dist/index.js.map +1 -1
  11. package/dist/index.mjs +7 -477
  12. package/dist/index.mjs.map +1 -1
  13. package/ios/PapyrusEngineStore.h +0 -3
  14. package/ios/PapyrusEngineStore.m +6 -32
  15. package/ios/PapyrusNativeEngine.m +5 -684
  16. package/ios/PapyrusNativeEngine.podspec +15 -0
  17. package/ios/PapyrusPageView.m +11 -38
  18. package/package.json +8 -10
  19. package/react-native.config.js +1 -1
  20. package/PapyrusNativeEngine.podspec +0 -45
  21. package/android/src/main/cpp/PapyrusComicArchiveJni.cpp +0 -68
  22. package/android/src/main/cpp/PapyrusComicArchiveTest.cpp +0 -55
  23. package/android/src/main/cpp/papyrus_outline_loader_test.cpp +0 -240
  24. package/android/src/main/cpp/papyrus_text_word_test.cpp +0 -72
  25. package/android/src/main/java/com/papyrus/engine/PapyrusComicArchiveNative.java +0 -14
  26. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentView.java +0 -491
  27. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentViewManager.java +0 -32
  28. package/android/src/main/java/com/papyrus/engine/PapyrusComicPageLayout.java +0 -38
  29. package/android/src/main/java/com/papyrus/engine/PapyrusNativeTextModel.java +0 -195
  30. package/android/src/main/java/com/papyrus/engine/PapyrusSelectionMenuIcon.java +0 -48
  31. package/android/src/main/java/com/papyrus/engine/PapyrusTextAnnotationModel.java +0 -22
  32. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentView.java +0 -661
  33. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentViewManager.java +0 -49
  34. package/android/src/main/java/com/papyrus/engine/PapyrusTextStore.java +0 -43
  35. package/ios/PapyrusComicDocumentView.h +0 -21
  36. package/ios/PapyrusComicDocumentView.m +0 -554
  37. package/ios/PapyrusComicDocumentViewManager.m +0 -23
  38. package/ios/PapyrusPageRotationRegistry.h +0 -29
  39. package/ios/PapyrusPageRotationRegistry.m +0 -179
  40. package/ios/PapyrusPdfDocumentView.h +0 -55
  41. package/ios/PapyrusPdfDocumentView.m +0 -2339
  42. package/ios/PapyrusPdfDocumentViewManager.m +0 -61
  43. package/ios/PapyrusPdfPageTheme.h +0 -15
  44. package/ios/PapyrusPdfPageTheme.m +0 -218
  45. package/ios/PapyrusSelectionMenu.h +0 -27
  46. package/ios/PapyrusTextAnnotationRange.h +0 -2
  47. package/ios/PapyrusTextAnnotationRange.m +0 -29
  48. package/ios/PapyrusTextDocumentView.h +0 -32
  49. package/ios/PapyrusTextDocumentView.m +0 -518
  50. package/ios/PapyrusTextDocumentViewManager.m +0 -34
  51. package/ios/PapyrusTextStore.h +0 -13
  52. package/ios/PapyrusTextStore.m +0 -73
  53. package/third_party_licenses/BLAKE2-NOTICE.txt +0 -11
  54. package/third_party_licenses/libarchive-COPYING +0 -65
  55. package/vendor/libarchive/PapyrusComicArchive.cpp +0 -376
  56. package/vendor/libarchive/PapyrusComicArchive.h +0 -30
  57. package/vendor/libarchive/README.papyrus.md +0 -15
  58. package/vendor/libarchive/android.h +0 -192
  59. package/vendor/libarchive/config/PapyrusArchiveConfig.h +0 -71
  60. package/vendor/libarchive/libarchive/CMakeLists.txt +0 -293
  61. package/vendor/libarchive/libarchive/android_lf.h +0 -36
  62. package/vendor/libarchive/libarchive/archive.h +0 -1266
  63. package/vendor/libarchive/libarchive/archive_acl.c +0 -2025
  64. package/vendor/libarchive/libarchive/archive_acl_private.h +0 -85
  65. package/vendor/libarchive/libarchive/archive_blake2.h +0 -197
  66. package/vendor/libarchive/libarchive/archive_blake2_impl.h +0 -161
  67. package/vendor/libarchive/libarchive/archive_blake2s_ref.c +0 -369
  68. package/vendor/libarchive/libarchive/archive_blake2sp_ref.c +0 -361
  69. package/vendor/libarchive/libarchive/archive_check_magic.c +0 -183
  70. package/vendor/libarchive/libarchive/archive_cmdline.c +0 -211
  71. package/vendor/libarchive/libarchive/archive_cmdline_private.h +0 -45
  72. package/vendor/libarchive/libarchive/archive_crc32.h +0 -123
  73. package/vendor/libarchive/libarchive/archive_cryptor.c +0 -534
  74. package/vendor/libarchive/libarchive/archive_cryptor_private.h +0 -199
  75. package/vendor/libarchive/libarchive/archive_digest.c +0 -1584
  76. package/vendor/libarchive/libarchive/archive_digest_private.h +0 -431
  77. package/vendor/libarchive/libarchive/archive_disk_acl_darwin.c +0 -558
  78. package/vendor/libarchive/libarchive/archive_disk_acl_freebsd.c +0 -712
  79. package/vendor/libarchive/libarchive/archive_disk_acl_linux.c +0 -760
  80. package/vendor/libarchive/libarchive/archive_disk_acl_sunos.c +0 -824
  81. package/vendor/libarchive/libarchive/archive_endian.h +0 -234
  82. package/vendor/libarchive/libarchive/archive_entry.3 +0 -147
  83. package/vendor/libarchive/libarchive/archive_entry.c +0 -2297
  84. package/vendor/libarchive/libarchive/archive_entry.h +0 -763
  85. package/vendor/libarchive/libarchive/archive_entry_acl.3 +0 -458
  86. package/vendor/libarchive/libarchive/archive_entry_copy_bhfi.c +0 -55
  87. package/vendor/libarchive/libarchive/archive_entry_copy_stat.c +0 -82
  88. package/vendor/libarchive/libarchive/archive_entry_link_resolver.c +0 -472
  89. package/vendor/libarchive/libarchive/archive_entry_linkify.3 +0 -224
  90. package/vendor/libarchive/libarchive/archive_entry_locale.h +0 -90
  91. package/vendor/libarchive/libarchive/archive_entry_misc.3 +0 -63
  92. package/vendor/libarchive/libarchive/archive_entry_paths.3 +0 -153
  93. package/vendor/libarchive/libarchive/archive_entry_perms.3 +0 -210
  94. package/vendor/libarchive/libarchive/archive_entry_private.h +0 -205
  95. package/vendor/libarchive/libarchive/archive_entry_sparse.c +0 -156
  96. package/vendor/libarchive/libarchive/archive_entry_stat.3 +0 -274
  97. package/vendor/libarchive/libarchive/archive_entry_stat.c +0 -121
  98. package/vendor/libarchive/libarchive/archive_entry_strmode.c +0 -69
  99. package/vendor/libarchive/libarchive/archive_entry_time.3 +0 -127
  100. package/vendor/libarchive/libarchive/archive_entry_xattr.c +0 -155
  101. package/vendor/libarchive/libarchive/archive_hmac.c +0 -345
  102. package/vendor/libarchive/libarchive/archive_hmac_private.h +0 -119
  103. package/vendor/libarchive/libarchive/archive_integer.h +0 -267
  104. package/vendor/libarchive/libarchive/archive_match.c +0 -1869
  105. package/vendor/libarchive/libarchive/archive_openssl_evp_private.h +0 -54
  106. package/vendor/libarchive/libarchive/archive_openssl_hmac_private.h +0 -54
  107. package/vendor/libarchive/libarchive/archive_options.c +0 -219
  108. package/vendor/libarchive/libarchive/archive_options_private.h +0 -49
  109. package/vendor/libarchive/libarchive/archive_pack_dev.c +0 -330
  110. package/vendor/libarchive/libarchive/archive_pack_dev.h +0 -49
  111. package/vendor/libarchive/libarchive/archive_parse_date.c +0 -1173
  112. package/vendor/libarchive/libarchive/archive_pathmatch.c +0 -480
  113. package/vendor/libarchive/libarchive/archive_pathmatch.h +0 -50
  114. package/vendor/libarchive/libarchive/archive_platform.h +0 -213
  115. package/vendor/libarchive/libarchive/archive_platform_acl.h +0 -53
  116. package/vendor/libarchive/libarchive/archive_platform_stat.h +0 -45
  117. package/vendor/libarchive/libarchive/archive_platform_xattr.h +0 -45
  118. package/vendor/libarchive/libarchive/archive_ppmd7.c +0 -1173
  119. package/vendor/libarchive/libarchive/archive_ppmd7_private.h +0 -119
  120. package/vendor/libarchive/libarchive/archive_ppmd8.c +0 -1293
  121. package/vendor/libarchive/libarchive/archive_ppmd8_private.h +0 -123
  122. package/vendor/libarchive/libarchive/archive_ppmd_private.h +0 -159
  123. package/vendor/libarchive/libarchive/archive_private.h +0 -209
  124. package/vendor/libarchive/libarchive/archive_random.c +0 -274
  125. package/vendor/libarchive/libarchive/archive_random_private.h +0 -36
  126. package/vendor/libarchive/libarchive/archive_rb.c +0 -709
  127. package/vendor/libarchive/libarchive/archive_rb.h +0 -113
  128. package/vendor/libarchive/libarchive/archive_read.3 +0 -250
  129. package/vendor/libarchive/libarchive/archive_read.c +0 -1814
  130. package/vendor/libarchive/libarchive/archive_read_add_passphrase.3 +0 -72
  131. package/vendor/libarchive/libarchive/archive_read_add_passphrase.c +0 -189
  132. package/vendor/libarchive/libarchive/archive_read_append_filter.c +0 -209
  133. package/vendor/libarchive/libarchive/archive_read_data.3 +0 -128
  134. package/vendor/libarchive/libarchive/archive_read_data_into_fd.c +0 -207
  135. package/vendor/libarchive/libarchive/archive_read_disk.3 +0 -422
  136. package/vendor/libarchive/libarchive/archive_read_disk_entry_from_file.c +0 -1086
  137. package/vendor/libarchive/libarchive/archive_read_disk_posix.c +0 -2639
  138. package/vendor/libarchive/libarchive/archive_read_disk_private.h +0 -97
  139. package/vendor/libarchive/libarchive/archive_read_disk_set_standard_lookup.c +0 -315
  140. package/vendor/libarchive/libarchive/archive_read_disk_windows.c +0 -2603
  141. package/vendor/libarchive/libarchive/archive_read_extract.3 +0 -135
  142. package/vendor/libarchive/libarchive/archive_read_extract.c +0 -59
  143. package/vendor/libarchive/libarchive/archive_read_extract2.c +0 -154
  144. package/vendor/libarchive/libarchive/archive_read_filter.3 +0 -163
  145. package/vendor/libarchive/libarchive/archive_read_format.3 +0 -190
  146. package/vendor/libarchive/libarchive/archive_read_free.3 +0 -91
  147. package/vendor/libarchive/libarchive/archive_read_header.3 +0 -89
  148. package/vendor/libarchive/libarchive/archive_read_new.3 +0 -57
  149. package/vendor/libarchive/libarchive/archive_read_open.3 +0 -247
  150. package/vendor/libarchive/libarchive/archive_read_open_fd.c +0 -236
  151. package/vendor/libarchive/libarchive/archive_read_open_file.c +0 -257
  152. package/vendor/libarchive/libarchive/archive_read_open_filename.c +0 -652
  153. package/vendor/libarchive/libarchive/archive_read_open_memory.c +0 -188
  154. package/vendor/libarchive/libarchive/archive_read_private.h +0 -274
  155. package/vendor/libarchive/libarchive/archive_read_set_format.c +0 -116
  156. package/vendor/libarchive/libarchive/archive_read_set_options.3 +0 -290
  157. package/vendor/libarchive/libarchive/archive_read_set_options.c +0 -132
  158. package/vendor/libarchive/libarchive/archive_read_support_filter_all.c +0 -84
  159. package/vendor/libarchive/libarchive/archive_read_support_filter_by_code.c +0 -68
  160. package/vendor/libarchive/libarchive/archive_read_support_filter_bzip2.c +0 -360
  161. package/vendor/libarchive/libarchive/archive_read_support_filter_compress.c +0 -457
  162. package/vendor/libarchive/libarchive/archive_read_support_filter_grzip.c +0 -108
  163. package/vendor/libarchive/libarchive/archive_read_support_filter_gzip.c +0 -538
  164. package/vendor/libarchive/libarchive/archive_read_support_filter_lrzip.c +0 -119
  165. package/vendor/libarchive/libarchive/archive_read_support_filter_lz4.c +0 -829
  166. package/vendor/libarchive/libarchive/archive_read_support_filter_lzop.c +0 -493
  167. package/vendor/libarchive/libarchive/archive_read_support_filter_none.c +0 -51
  168. package/vendor/libarchive/libarchive/archive_read_support_filter_program.c +0 -497
  169. package/vendor/libarchive/libarchive/archive_read_support_filter_rpm.c +0 -249
  170. package/vendor/libarchive/libarchive/archive_read_support_filter_uu.c +0 -739
  171. package/vendor/libarchive/libarchive/archive_read_support_filter_xz.c +0 -792
  172. package/vendor/libarchive/libarchive/archive_read_support_filter_zstd.c +0 -356
  173. package/vendor/libarchive/libarchive/archive_read_support_format_7zip.c +0 -4650
  174. package/vendor/libarchive/libarchive/archive_read_support_format_all.c +0 -88
  175. package/vendor/libarchive/libarchive/archive_read_support_format_ar.c +0 -597
  176. package/vendor/libarchive/libarchive/archive_read_support_format_by_code.c +0 -76
  177. package/vendor/libarchive/libarchive/archive_read_support_format_cab.c +0 -3410
  178. package/vendor/libarchive/libarchive/archive_read_support_format_cpio.c +0 -1152
  179. package/vendor/libarchive/libarchive/archive_read_support_format_empty.c +0 -95
  180. package/vendor/libarchive/libarchive/archive_read_support_format_iso9660.c +0 -3516
  181. package/vendor/libarchive/libarchive/archive_read_support_format_lha.c +0 -2931
  182. package/vendor/libarchive/libarchive/archive_read_support_format_mtree.c +0 -2113
  183. package/vendor/libarchive/libarchive/archive_read_support_format_rar.c +0 -3932
  184. package/vendor/libarchive/libarchive/archive_read_support_format_rar5.c +0 -4501
  185. package/vendor/libarchive/libarchive/archive_read_support_format_raw.c +0 -194
  186. package/vendor/libarchive/libarchive/archive_read_support_format_tar.c +0 -3785
  187. package/vendor/libarchive/libarchive/archive_read_support_format_warc.c +0 -875
  188. package/vendor/libarchive/libarchive/archive_read_support_format_xar.c +0 -3770
  189. package/vendor/libarchive/libarchive/archive_read_support_format_zip.c +0 -4710
  190. package/vendor/libarchive/libarchive/archive_string.c +0 -4391
  191. package/vendor/libarchive/libarchive/archive_string.h +0 -244
  192. package/vendor/libarchive/libarchive/archive_string_composition.h +0 -2289
  193. package/vendor/libarchive/libarchive/archive_string_sprintf.c +0 -200
  194. package/vendor/libarchive/libarchive/archive_time.c +0 -168
  195. package/vendor/libarchive/libarchive/archive_time_private.h +0 -47
  196. package/vendor/libarchive/libarchive/archive_util.3 +0 -239
  197. package/vendor/libarchive/libarchive/archive_util.c +0 -668
  198. package/vendor/libarchive/libarchive/archive_version_details.c +0 -516
  199. package/vendor/libarchive/libarchive/archive_virtual.c +0 -162
  200. package/vendor/libarchive/libarchive/archive_windows.c +0 -1035
  201. package/vendor/libarchive/libarchive/archive_windows.h +0 -332
  202. package/vendor/libarchive/libarchive/archive_write.3 +0 -266
  203. package/vendor/libarchive/libarchive/archive_write.c +0 -896
  204. package/vendor/libarchive/libarchive/archive_write_add_filter.c +0 -71
  205. package/vendor/libarchive/libarchive/archive_write_add_filter_b64encode.c +0 -329
  206. package/vendor/libarchive/libarchive/archive_write_add_filter_by_name.c +0 -76
  207. package/vendor/libarchive/libarchive/archive_write_add_filter_bzip2.c +0 -411
  208. package/vendor/libarchive/libarchive/archive_write_add_filter_compress.c +0 -459
  209. package/vendor/libarchive/libarchive/archive_write_add_filter_grzip.c +0 -143
  210. package/vendor/libarchive/libarchive/archive_write_add_filter_gzip.c +0 -493
  211. package/vendor/libarchive/libarchive/archive_write_add_filter_lrzip.c +0 -214
  212. package/vendor/libarchive/libarchive/archive_write_add_filter_lz4.c +0 -710
  213. package/vendor/libarchive/libarchive/archive_write_add_filter_lzop.c +0 -500
  214. package/vendor/libarchive/libarchive/archive_write_add_filter_none.c +0 -42
  215. package/vendor/libarchive/libarchive/archive_write_add_filter_program.c +0 -398
  216. package/vendor/libarchive/libarchive/archive_write_add_filter_uuencode.c +0 -320
  217. package/vendor/libarchive/libarchive/archive_write_add_filter_xz.c +0 -547
  218. package/vendor/libarchive/libarchive/archive_write_add_filter_zstd.c +0 -608
  219. package/vendor/libarchive/libarchive/archive_write_blocksize.3 +0 -112
  220. package/vendor/libarchive/libarchive/archive_write_data.3 +0 -88
  221. package/vendor/libarchive/libarchive/archive_write_disk.3 +0 -360
  222. package/vendor/libarchive/libarchive/archive_write_disk_posix.c +0 -4789
  223. package/vendor/libarchive/libarchive/archive_write_disk_private.h +0 -43
  224. package/vendor/libarchive/libarchive/archive_write_disk_set_standard_lookup.c +0 -265
  225. package/vendor/libarchive/libarchive/archive_write_disk_windows.c +0 -2952
  226. package/vendor/libarchive/libarchive/archive_write_filter.3 +0 -140
  227. package/vendor/libarchive/libarchive/archive_write_finish_entry.3 +0 -77
  228. package/vendor/libarchive/libarchive/archive_write_format.3 +0 -187
  229. package/vendor/libarchive/libarchive/archive_write_free.3 +0 -94
  230. package/vendor/libarchive/libarchive/archive_write_header.3 +0 -71
  231. package/vendor/libarchive/libarchive/archive_write_new.3 +0 -56
  232. package/vendor/libarchive/libarchive/archive_write_open.3 +0 -270
  233. package/vendor/libarchive/libarchive/archive_write_open_fd.c +0 -141
  234. package/vendor/libarchive/libarchive/archive_write_open_file.c +0 -102
  235. package/vendor/libarchive/libarchive/archive_write_open_filename.c +0 -272
  236. package/vendor/libarchive/libarchive/archive_write_open_memory.c +0 -110
  237. package/vendor/libarchive/libarchive/archive_write_private.h +0 -166
  238. package/vendor/libarchive/libarchive/archive_write_set_format.c +0 -124
  239. package/vendor/libarchive/libarchive/archive_write_set_format_7zip.c +0 -2571
  240. package/vendor/libarchive/libarchive/archive_write_set_format_ar.c +0 -540
  241. package/vendor/libarchive/libarchive/archive_write_set_format_by_name.c +0 -93
  242. package/vendor/libarchive/libarchive/archive_write_set_format_cpio.c +0 -11
  243. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_binary.c +0 -618
  244. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_newc.c +0 -457
  245. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_odc.c +0 -508
  246. package/vendor/libarchive/libarchive/archive_write_set_format_filter_by_ext.c +0 -141
  247. package/vendor/libarchive/libarchive/archive_write_set_format_gnutar.c +0 -790
  248. package/vendor/libarchive/libarchive/archive_write_set_format_iso9660.c +0 -8216
  249. package/vendor/libarchive/libarchive/archive_write_set_format_mtree.c +0 -2307
  250. package/vendor/libarchive/libarchive/archive_write_set_format_pax.c +0 -2249
  251. package/vendor/libarchive/libarchive/archive_write_set_format_private.h +0 -40
  252. package/vendor/libarchive/libarchive/archive_write_set_format_raw.c +0 -123
  253. package/vendor/libarchive/libarchive/archive_write_set_format_shar.c +0 -650
  254. package/vendor/libarchive/libarchive/archive_write_set_format_ustar.c +0 -768
  255. package/vendor/libarchive/libarchive/archive_write_set_format_v7tar.c +0 -648
  256. package/vendor/libarchive/libarchive/archive_write_set_format_warc.c +0 -491
  257. package/vendor/libarchive/libarchive/archive_write_set_format_xar.c +0 -3799
  258. package/vendor/libarchive/libarchive/archive_write_set_format_zip.c +0 -2548
  259. package/vendor/libarchive/libarchive/archive_write_set_options.3 +0 -779
  260. package/vendor/libarchive/libarchive/archive_write_set_options.c +0 -129
  261. package/vendor/libarchive/libarchive/archive_write_set_passphrase.3 +0 -72
  262. package/vendor/libarchive/libarchive/archive_write_set_passphrase.c +0 -95
  263. package/vendor/libarchive/libarchive/archive_xxhash.h +0 -48
  264. package/vendor/libarchive/libarchive/config_freebsd.h +0 -275
  265. package/vendor/libarchive/libarchive/cpio.5 +0 -389
  266. package/vendor/libarchive/libarchive/filter_fork.h +0 -44
  267. package/vendor/libarchive/libarchive/filter_fork_posix.c +0 -269
  268. package/vendor/libarchive/libarchive/filter_fork_windows.c +0 -270
  269. package/vendor/libarchive/libarchive/libarchive-formats.5 +0 -478
  270. package/vendor/libarchive/libarchive/libarchive.3 +0 -297
  271. package/vendor/libarchive/libarchive/libarchive_changes.3 +0 -339
  272. package/vendor/libarchive/libarchive/libarchive_internals.3 +0 -363
  273. package/vendor/libarchive/libarchive/module.modulemap +0 -34
  274. package/vendor/libarchive/libarchive/mtree.5 +0 -323
  275. package/vendor/libarchive/libarchive/tar.5 +0 -947
  276. package/vendor/libarchive/libarchive/xxhash.c +0 -531
@@ -1,3799 +0,0 @@
1
- /*-
2
- * Copyright (c) 2010-2012 Michihiro NAKAJIMA
3
- * All rights reserved.
4
- *
5
- * Redistribution and use in source and binary forms, with or without
6
- * modification, are permitted provided that the following conditions
7
- * are met:
8
- * 1. Redistributions of source code must retain the above copyright
9
- * notice, this list of conditions and the following disclaimer.
10
- * 2. Redistributions in binary form must reproduce the above copyright
11
- * notice, this list of conditions and the following disclaimer in the
12
- * documentation and/or other materials provided with the distribution.
13
- *
14
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17
- * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
- */
25
-
26
- #include "archive_platform.h"
27
-
28
- #ifdef HAVE_ERRNO_H
29
- #include <errno.h>
30
- #endif
31
- #ifdef HAVE_LIMITS_H
32
- #include <limits.h>
33
- #endif
34
- #include <stdlib.h>
35
- #if HAVE_LIBXML_XMLWRITER_H
36
- #include <libxml/xmlwriter.h>
37
- #if defined(LIBXML_VERSION) && LIBXML_VERSION >= 20703
38
- #define XAR_WRITER_HAS_XML
39
- #endif /* LIBXML_VERSION */
40
- #elif HAVE_XMLLITE_H
41
- #include <objidl.h>
42
- #include <initguid.h>
43
- #include <xmllite.h>
44
- #define XAR_WRITER_HAS_XML
45
- #endif
46
- #ifdef HAVE_BZLIB_H
47
- #include <bzlib.h>
48
- #endif
49
- #if HAVE_LZMA_H
50
- #include <lzma.h>
51
- #endif
52
- #ifdef HAVE_ZLIB_H
53
- #include <zlib.h>
54
- #endif
55
-
56
- #include "archive.h"
57
- #include "archive_digest_private.h"
58
- #include "archive_endian.h"
59
- #include "archive_entry.h"
60
- #include "archive_entry_locale.h"
61
- #include "archive_private.h"
62
- #include "archive_rb.h"
63
- #include "archive_string.h"
64
- #include "archive_write_private.h"
65
-
66
- /*
67
- * Differences to xar utility.
68
- * - Subdocument is not supported yet.
69
- * - ACL is not supported yet.
70
- * - When writing an XML element <link type="<file-type>">, <file-type>
71
- * which is a file type a symbolic link is referencing is always marked
72
- * as "broken". Xar utility uses stat(2) to get the file type, but, in
73
- * libarchive format writer, we should not use it; if it is needed, we
74
- * should get about it at archive_read_disk.c.
75
- * - It is possible to appear both <flags> and <ext2> elements.
76
- * Xar utility generates <flags> on BSD platform and <ext2> on Linux
77
- * platform.
78
- *
79
- */
80
-
81
- #if !defined(XAR_WRITER_HAS_XML) ||\
82
- !defined(HAVE_ZLIB_H) || \
83
- !defined(ARCHIVE_HAS_MD5) || !defined(ARCHIVE_HAS_SHA1)
84
- /*
85
- * xar needs several external libraries.
86
- * o libxml2 or xmllite (on Windows)
87
- * o openssl or MD5/SHA1 hash function
88
- * o zlib
89
- * o bzlib2 (option)
90
- * o liblzma (option)
91
- */
92
- int
93
- archive_write_set_format_xar(struct archive *_a)
94
- {
95
- struct archive_write *a = (struct archive_write *)_a;
96
-
97
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
98
- "Xar not supported on this platform");
99
- return (ARCHIVE_WARN);
100
- }
101
-
102
- #else /* Support xar format */
103
-
104
- struct xml_writer;
105
- static int xml_writer_create(struct xml_writer **pctx);
106
- static int xml_writer_start_document(struct xml_writer *ctx);
107
- static int xml_writer_end_document(struct xml_writer *ctx);
108
- static int xml_writer_set_indent(struct xml_writer *ctx, unsigned int indent);
109
- static int xml_writer_start_element(struct xml_writer *ctx,
110
- const char *localName);
111
- static int xml_writer_write_attribute(struct xml_writer *ctx, const char *key,
112
- const char *value);
113
- static int xml_writer_write_attributef(struct xml_writer *ctx, const char *key,
114
- const char *format, ...) __LA_PRINTF(3, 4);
115
- static int xml_writer_write_string(struct xml_writer *ctx, const char *string);
116
- static int xml_writer_write_base64(struct xml_writer* ctx,
117
- const char *data, size_t start, size_t len);
118
- static int xml_writer_end_element(struct xml_writer *ctx);
119
- static int xml_writer_get_final_content_and_length(struct xml_writer *ctx,
120
- const char **out, size_t *size);
121
- static int xml_writer_destroy(struct xml_writer *ctx);
122
-
123
- /*#define DEBUG_PRINT_TOC 1 */
124
-
125
- #define HEADER_MAGIC 0x78617221
126
- #define HEADER_SIZE 28
127
- #define HEADER_VERSION 1
128
-
129
- enum sumalg {
130
- CKSUM_NONE = 0,
131
- CKSUM_SHA1 = 1,
132
- CKSUM_MD5 = 2,
133
- CKSUM_SHA256 = 3,
134
- CKSUM_SHA512 = 4
135
- };
136
-
137
- #define MD5_SIZE 16
138
- #define SHA1_SIZE 20
139
- #define SHA256_SIZE 32
140
- #define SHA512_SIZE 64
141
- #define MAX_SUM_SIZE 64
142
- #define MD5_NAME "md5"
143
- #define SHA1_NAME "sha1"
144
- #define SHA256_NAME "sha256"
145
- #define SHA512_NAME "sha512"
146
-
147
- enum enctype {
148
- NONE,
149
- GZIP,
150
- BZIP2,
151
- LZMA,
152
- XZ,
153
- };
154
-
155
- struct chksumwork {
156
- enum sumalg alg;
157
- #ifdef ARCHIVE_HAS_MD5
158
- archive_md5_ctx md5ctx;
159
- #endif
160
- #ifdef ARCHIVE_HAS_SHA1
161
- archive_sha1_ctx sha1ctx;
162
- #endif
163
- #ifdef ARCHIVE_HAS_SHA256
164
- archive_sha256_ctx sha256ctx;
165
- #endif
166
- #ifdef ARCHIVE_HAS_SHA512
167
- archive_sha512_ctx sha512ctx;
168
- #endif
169
- };
170
-
171
- enum la_zaction {
172
- ARCHIVE_Z_FINISH,
173
- ARCHIVE_Z_RUN
174
- };
175
-
176
- /*
177
- * Universal zstream.
178
- */
179
- struct la_zstream {
180
- const unsigned char *next_in;
181
- size_t avail_in;
182
- uint64_t total_in;
183
-
184
- unsigned char *next_out;
185
- size_t avail_out;
186
- uint64_t total_out;
187
-
188
- int valid;
189
- void *real_stream;
190
- int (*code) (struct archive *a,
191
- struct la_zstream *lastrm,
192
- enum la_zaction action);
193
- int (*end)(struct archive *a,
194
- struct la_zstream *lastrm);
195
- };
196
-
197
- struct chksumval {
198
- enum sumalg alg;
199
- size_t len;
200
- unsigned char val[MAX_SUM_SIZE];
201
- };
202
-
203
- struct heap_data {
204
- int id;
205
- struct heap_data *next;
206
- uint64_t temp_offset;
207
- uint64_t length; /* archived size. */
208
- uint64_t size; /* extracted size. */
209
- enum enctype compression;
210
- struct chksumval a_sum; /* archived checksum. */
211
- struct chksumval e_sum; /* extracted checksum. */
212
- };
213
-
214
- struct file {
215
- struct archive_rb_node rbnode;
216
-
217
- int id;
218
- struct archive_entry *entry;
219
-
220
- struct archive_rb_tree rbtree;
221
- struct file *next;
222
- struct file *chnext;
223
- struct file *hlnext;
224
- /* For hardlinked files.
225
- * Use only when archive_entry_nlink() > 1 */
226
- struct file *hardlink_target;
227
- struct file *parent; /* parent directory entry */
228
- /*
229
- * To manage sub directory files.
230
- * We use 'chnext' (a member of struct file) to chain.
231
- */
232
- struct {
233
- struct file *first;
234
- struct file **last;
235
- } children;
236
-
237
- /* For making a directory tree. */
238
- struct archive_string parentdir;
239
- struct archive_string basename;
240
- struct archive_string symlink;
241
-
242
- int ea_idx;
243
- struct {
244
- struct heap_data *first;
245
- struct heap_data **last;
246
- } xattr;
247
- struct heap_data data;
248
- struct archive_string script;
249
-
250
- unsigned int virtual:1;
251
- unsigned int dir:1;
252
- };
253
-
254
- struct hardlink {
255
- struct archive_rb_node rbnode;
256
- int nlink;
257
- struct {
258
- struct file *first;
259
- struct file **last;
260
- } file_list;
261
- };
262
-
263
- struct xar {
264
- int temp_fd;
265
- uint64_t temp_offset;
266
-
267
- int file_idx;
268
- struct file *root;
269
- struct file *cur_dirent;
270
- struct archive_string cur_dirstr;
271
- struct file *cur_file;
272
- uint64_t bytes_remaining;
273
- struct archive_string tstr;
274
- struct archive_string vstr;
275
-
276
- enum sumalg opt_toc_sumalg;
277
- enum sumalg opt_sumalg;
278
- enum enctype opt_compression;
279
- int opt_compression_level;
280
- uint32_t opt_threads;
281
-
282
- struct chksumwork a_sumwrk; /* archived checksum. */
283
- struct chksumwork e_sumwrk; /* extracted checksum. */
284
- struct la_zstream stream;
285
- struct archive_string_conv *sconv;
286
- /*
287
- * Compressed data buffer.
288
- */
289
- unsigned char wbuff[1024 * 64];
290
- size_t wbuff_remaining;
291
-
292
- struct heap_data toc;
293
- /*
294
- * The list of all file entries is used to manage struct file
295
- * objects.
296
- * We use 'next' (a member of struct file) to chain.
297
- */
298
- struct {
299
- struct file *first;
300
- struct file **last;
301
- } file_list;
302
- /*
303
- * The list of hard-linked file entries.
304
- * We use 'hlnext' (a member of struct file) to chain.
305
- */
306
- struct archive_rb_tree hardlink_rbtree;
307
- };
308
-
309
- static int xar_options(struct archive_write *,
310
- const char *, const char *);
311
- static int xar_write_header(struct archive_write *,
312
- struct archive_entry *);
313
- static ssize_t xar_write_data(struct archive_write *,
314
- const void *, size_t);
315
- static int xar_finish_entry(struct archive_write *);
316
- static int xar_close(struct archive_write *);
317
- static int xar_free(struct archive_write *);
318
-
319
- static struct file *file_new(struct archive_write *a, struct archive_entry *);
320
- static void file_free(struct file *);
321
- static struct file *file_create_virtual_dir(struct archive_write *a, struct xar *,
322
- const char *);
323
- static int file_add_child_tail(struct file *, struct file *);
324
- static struct file *file_find_child(struct file *, const char *);
325
- static int file_gen_utility_names(struct archive_write *,
326
- struct file *);
327
- static int get_path_component(char *, int, const char *);
328
- static int file_tree(struct archive_write *, struct file **);
329
- static void file_register(struct xar *, struct file *);
330
- static void file_init_register(struct xar *);
331
- static void file_free_register(struct xar *);
332
- static int file_register_hardlink(struct archive_write *,
333
- struct file *);
334
- static void file_connect_hardlink_files(struct xar *);
335
- static void file_init_hardlinks(struct xar *);
336
- static void file_free_hardlinks(struct xar *);
337
-
338
- static void checksum_init(struct chksumwork *, enum sumalg);
339
- static void checksum_update(struct chksumwork *, const void *, size_t);
340
- static void checksum_final(struct chksumwork *, struct chksumval *);
341
- static int compression_init_encoder_gzip(struct archive *,
342
- struct la_zstream *, int, int);
343
- static int compression_code_gzip(struct archive *,
344
- struct la_zstream *, enum la_zaction);
345
- static int compression_end_gzip(struct archive *, struct la_zstream *);
346
- static int compression_init_encoder_bzip2(struct archive *,
347
- struct la_zstream *, int);
348
- #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
349
- static int compression_code_bzip2(struct archive *,
350
- struct la_zstream *, enum la_zaction);
351
- static int compression_end_bzip2(struct archive *, struct la_zstream *);
352
- #endif
353
- static int compression_init_encoder_lzma(struct archive *,
354
- struct la_zstream *, int);
355
- static int compression_init_encoder_xz(struct archive *,
356
- struct la_zstream *, int, int);
357
- #if defined(HAVE_LZMA_H)
358
- static int compression_code_lzma(struct archive *,
359
- struct la_zstream *, enum la_zaction);
360
- static int compression_end_lzma(struct archive *, struct la_zstream *);
361
- #endif
362
- static int xar_compression_init_encoder(struct archive_write *);
363
- static int compression_code(struct archive *,
364
- struct la_zstream *, enum la_zaction);
365
- static int compression_end(struct archive *,
366
- struct la_zstream *);
367
- static int save_xattrs(struct archive_write *, struct file *);
368
- static int getalgsize(enum sumalg);
369
- static const char *getalgname(enum sumalg);
370
-
371
- int
372
- archive_write_set_format_xar(struct archive *_a)
373
- {
374
- struct archive_write *a = (struct archive_write *)_a;
375
- struct xar *xar;
376
-
377
- archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
378
- ARCHIVE_STATE_NEW, "archive_write_set_format_xar");
379
-
380
- /* If another format was already registered, unregister it. */
381
- (void)__archive_write_unregister_format(a);
382
-
383
- xar = calloc(1, sizeof(*xar));
384
- if (xar == NULL) {
385
- archive_set_error(&a->archive, ENOMEM,
386
- "Can't allocate xar data");
387
- return (ARCHIVE_FATAL);
388
- }
389
- xar->temp_fd = -1;
390
- file_init_register(xar);
391
- file_init_hardlinks(xar);
392
- archive_string_init(&(xar->tstr));
393
- archive_string_init(&(xar->vstr));
394
- archive_string_init(&(xar->cur_dirstr));
395
-
396
- /*
397
- * Create the root directory.
398
- */
399
- xar->root = file_create_virtual_dir(a, xar, "");
400
- if (xar->root == NULL)
401
- goto memerr;
402
- xar->root->parent = xar->root;
403
- file_register(xar, xar->root);
404
- xar->cur_dirent = xar->root;
405
- if (archive_string_ensure(&(xar->cur_dirstr), 1) == NULL)
406
- goto memerr;
407
- xar->cur_dirstr.s[0] = 0;
408
-
409
- /*
410
- * Initialize option.
411
- */
412
- /* Set default checksum type. */
413
- xar->opt_toc_sumalg = CKSUM_SHA1;
414
- xar->opt_sumalg = CKSUM_SHA1;
415
- /* Set default compression type, level, and number of threads. */
416
- xar->opt_compression = GZIP;
417
- xar->opt_compression_level = 6;
418
- xar->opt_threads = 1;
419
-
420
- a->format_data = xar;
421
-
422
- a->format_name = "xar";
423
- a->format_options = xar_options;
424
- a->format_write_header = xar_write_header;
425
- a->format_write_data = xar_write_data;
426
- a->format_finish_entry = xar_finish_entry;
427
- a->format_close = xar_close;
428
- a->format_free = xar_free;
429
- a->archive.archive_format = ARCHIVE_FORMAT_XAR;
430
- a->archive.archive_format_name = "xar";
431
-
432
- return (ARCHIVE_OK);
433
-
434
- memerr:
435
- archive_string_free(&(xar->cur_dirstr));
436
- archive_string_free(&(xar->tstr));
437
- archive_string_free(&(xar->vstr));
438
- file_free_hardlinks(xar);
439
- file_free_register(xar);
440
- free(xar);
441
- archive_set_error(&a->archive, ENOMEM,
442
- "Can't allocate xar data");
443
- return (ARCHIVE_FATAL);
444
- }
445
-
446
- static int
447
- xar_options(struct archive_write *a, const char *key, const char *value)
448
- {
449
- struct xar *xar = a->format_data;
450
-
451
- if (strcmp(key, "checksum") == 0) {
452
- if (value == NULL)
453
- xar->opt_sumalg = CKSUM_NONE;
454
- else if (strcmp(value, "none") == 0)
455
- xar->opt_sumalg = CKSUM_NONE;
456
- else if (strcmp(value, "sha1") == 0)
457
- xar->opt_sumalg = CKSUM_SHA1;
458
- else if (strcmp(value, "md5") == 0)
459
- xar->opt_sumalg = CKSUM_MD5;
460
- #ifdef ARCHIVE_HAS_SHA256
461
- else if (strcmp(value, "sha256") == 0)
462
- xar->opt_sumalg = CKSUM_SHA256;
463
- #endif
464
- #ifdef ARCHIVE_HAS_SHA512
465
- else if (strcmp(value, "sha512") == 0)
466
- xar->opt_sumalg = CKSUM_SHA512;
467
- #endif
468
- else {
469
- archive_set_error(&(a->archive),
470
- ARCHIVE_ERRNO_MISC,
471
- "Unknown checksum name: `%s'",
472
- value);
473
- return (ARCHIVE_FAILED);
474
- }
475
- return (ARCHIVE_OK);
476
- }
477
- if (strcmp(key, "compression") == 0) {
478
- const char *name = NULL;
479
-
480
- if (value == NULL)
481
- xar->opt_compression = NONE;
482
- else if (strcmp(value, "none") == 0)
483
- xar->opt_compression = NONE;
484
- else if (strcmp(value, "gzip") == 0)
485
- xar->opt_compression = GZIP;
486
- else if (strcmp(value, "bzip2") == 0)
487
- #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
488
- xar->opt_compression = BZIP2;
489
- #else
490
- name = "bzip2";
491
- #endif
492
- else if (strcmp(value, "lzma") == 0)
493
- #if HAVE_LZMA_H
494
- xar->opt_compression = LZMA;
495
- #else
496
- name = "lzma";
497
- #endif
498
- else if (strcmp(value, "xz") == 0)
499
- #if HAVE_LZMA_H
500
- xar->opt_compression = XZ;
501
- #else
502
- name = "xz";
503
- #endif
504
- else {
505
- archive_set_error(&(a->archive),
506
- ARCHIVE_ERRNO_MISC,
507
- "Unknown compression name: `%s'",
508
- value);
509
- return (ARCHIVE_FAILED);
510
- }
511
- if (name != NULL) {
512
- archive_set_error(&(a->archive),
513
- ARCHIVE_ERRNO_MISC,
514
- "`%s' compression not supported "
515
- "on this platform",
516
- name);
517
- return (ARCHIVE_FAILED);
518
- }
519
- return (ARCHIVE_OK);
520
- }
521
- if (strcmp(key, "compression-level") == 0) {
522
- if (value == NULL ||
523
- !(value[0] >= '0' && value[0] <= '9') ||
524
- value[1] != '\0') {
525
- archive_set_error(&(a->archive),
526
- ARCHIVE_ERRNO_MISC,
527
- "Illegal value `%s'",
528
- value);
529
- return (ARCHIVE_FAILED);
530
- }
531
- xar->opt_compression_level = value[0] - '0';
532
- return (ARCHIVE_OK);
533
- }
534
- if (strcmp(key, "toc-checksum") == 0) {
535
- if (value == NULL)
536
- xar->opt_toc_sumalg = CKSUM_NONE;
537
- else if (strcmp(value, "none") == 0)
538
- xar->opt_toc_sumalg = CKSUM_NONE;
539
- else if (strcmp(value, "sha1") == 0)
540
- xar->opt_toc_sumalg = CKSUM_SHA1;
541
- else if (strcmp(value, "md5") == 0)
542
- xar->opt_toc_sumalg = CKSUM_MD5;
543
- #ifdef ARCHIVE_HAS_SHA256
544
- else if (strcmp(value, "sha256") == 0)
545
- xar->opt_toc_sumalg = CKSUM_SHA256;
546
- #endif
547
- #ifdef ARCHIVE_HAS_SHA512
548
- else if (strcmp(value, "sha512") == 0)
549
- xar->opt_toc_sumalg = CKSUM_SHA512;
550
- #endif
551
- else {
552
- archive_set_error(&(a->archive),
553
- ARCHIVE_ERRNO_MISC,
554
- "Unknown checksum name: `%s'",
555
- value);
556
- return (ARCHIVE_FAILED);
557
- }
558
- return (ARCHIVE_OK);
559
- }
560
- if (strcmp(key, "threads") == 0) {
561
- char *endptr;
562
- unsigned long val;
563
-
564
- if (value == NULL)
565
- return (ARCHIVE_FAILED);
566
- errno = 0;
567
- val = strtoul(value, &endptr, 10);
568
- if (errno != 0 || *endptr != '\0' || val > (unsigned)INT_MAX) {
569
- xar->opt_threads = 1;
570
- archive_set_error(&(a->archive),
571
- ARCHIVE_ERRNO_MISC,
572
- "Illegal value `%s'",
573
- value);
574
- return (ARCHIVE_FAILED);
575
- }
576
- xar->opt_threads = (int)val;
577
- if (xar->opt_threads == 0) {
578
- #ifdef HAVE_LZMA_STREAM_ENCODER_MT
579
- xar->opt_threads = lzma_cputhreads();
580
- #else
581
- xar->opt_threads = 1;
582
- #endif
583
- }
584
- }
585
-
586
- /* Note: The "warn" return is just to inform the options
587
- * supervisor that we didn't handle it. It will generate
588
- * a suitable error if no one used this option. */
589
- return (ARCHIVE_WARN);
590
- }
591
-
592
- static int
593
- xar_write_header(struct archive_write *a, struct archive_entry *entry)
594
- {
595
- struct xar *xar = a->format_data;
596
- struct file *file;
597
- struct archive_entry *file_entry;
598
- int r, r2;
599
-
600
- xar->cur_file = NULL;
601
- xar->bytes_remaining = 0;
602
-
603
- if (xar->sconv == NULL) {
604
- xar->sconv = archive_string_conversion_to_charset(
605
- &a->archive, "UTF-8", 1);
606
- if (xar->sconv == NULL)
607
- return (ARCHIVE_FATAL);
608
- }
609
-
610
- file = file_new(a, entry);
611
- if (file == NULL) {
612
- archive_set_error(&a->archive, ENOMEM,
613
- "Can't allocate data");
614
- return (ARCHIVE_FATAL);
615
- }
616
- r2 = file_gen_utility_names(a, file);
617
- if (r2 < ARCHIVE_WARN) {
618
- file_free(file);
619
- return (r2);
620
- }
621
-
622
- /*
623
- * Ignore a path which looks like the top of directory name
624
- * since we have already made the root directory of an Xar archive.
625
- */
626
- if (archive_strlen(&(file->parentdir)) == 0 &&
627
- archive_strlen(&(file->basename)) == 0) {
628
- file_free(file);
629
- return (r2);
630
- }
631
-
632
- /* Add entry into tree */
633
- file_entry = file->entry;
634
- r = file_tree(a, &file);
635
- if (r != ARCHIVE_OK)
636
- return (r);
637
- /* There is the same file in tree and
638
- * the current file is older than the file in tree.
639
- * So we don't need the current file data anymore. */
640
- if (file->entry != file_entry)
641
- return (r2);
642
- if (file->id == 0)
643
- file_register(xar, file);
644
-
645
- /* A virtual file, which is a directory, does not have
646
- * any contents and we won't store it into a archive
647
- * file other than its name. */
648
- if (file->virtual)
649
- return (r2);
650
-
651
- /*
652
- * Prepare to save the contents of the file.
653
- */
654
- if (xar->temp_fd == -1) {
655
- int algsize;
656
- xar->temp_offset = 0;
657
- xar->temp_fd = __archive_mktemp(NULL);
658
- if (xar->temp_fd < 0) {
659
- archive_set_error(&a->archive, errno,
660
- "Couldn't create temporary file");
661
- return (ARCHIVE_FATAL);
662
- }
663
- algsize = getalgsize(xar->opt_toc_sumalg);
664
- if (algsize > 0) {
665
- if (lseek(xar->temp_fd, algsize, SEEK_SET) < 0) {
666
- archive_set_error(&(a->archive), errno,
667
- "lseek failed");
668
- return (ARCHIVE_FATAL);
669
- }
670
- xar->temp_offset = algsize;
671
- }
672
- }
673
-
674
- if (archive_entry_hardlink(file->entry) == NULL) {
675
- r = save_xattrs(a, file);
676
- if (r != ARCHIVE_OK)
677
- return (ARCHIVE_FATAL);
678
- }
679
-
680
- /* Non regular files contents are unneeded to be saved to
681
- * a temporary file. */
682
- if (archive_entry_filetype(file->entry) != AE_IFREG)
683
- return (r2);
684
-
685
- /*
686
- * Set the current file to cur_file to read its contents.
687
- */
688
- xar->cur_file = file;
689
-
690
- if (archive_entry_nlink(file->entry) > 1) {
691
- r = file_register_hardlink(a, file);
692
- if (r != ARCHIVE_OK)
693
- return (r);
694
- if (archive_entry_hardlink(file->entry) != NULL) {
695
- archive_entry_unset_size(file->entry);
696
- return (r2);
697
- }
698
- }
699
-
700
- /* Save a offset of current file in temporary file. */
701
- file->data.temp_offset = xar->temp_offset;
702
- file->data.size = archive_entry_size(file->entry);
703
- file->data.compression = xar->opt_compression;
704
- xar->bytes_remaining = archive_entry_size(file->entry);
705
- checksum_init(&(xar->a_sumwrk), xar->opt_sumalg);
706
- checksum_init(&(xar->e_sumwrk), xar->opt_sumalg);
707
- r = xar_compression_init_encoder(a);
708
-
709
- if (r != ARCHIVE_OK) {
710
- struct chksumval sumval;
711
-
712
- checksum_final(&(xar->a_sumwrk), &sumval);
713
- checksum_final(&(xar->e_sumwrk), &sumval);
714
- xar->a_sumwrk.alg = CKSUM_NONE;
715
- xar->e_sumwrk.alg = CKSUM_NONE;
716
- return (r);
717
- } else
718
- return (r2);
719
- }
720
-
721
- static int
722
- write_to_temp(struct archive_write *a, const void *buff, size_t s)
723
- {
724
- struct xar *xar = a->format_data;
725
- const unsigned char *p;
726
- ssize_t ws;
727
-
728
- p = (const unsigned char *)buff;
729
- while (s) {
730
- ws = write(xar->temp_fd, p, s);
731
- if (ws < 0) {
732
- archive_set_error(&(a->archive), errno,
733
- "write function failed");
734
- return (ARCHIVE_FATAL);
735
- }
736
- s -= ws;
737
- p += ws;
738
- xar->temp_offset += ws;
739
- }
740
- return (ARCHIVE_OK);
741
- }
742
-
743
- static ssize_t
744
- xar_write_data(struct archive_write *a, const void *buff, size_t s)
745
- {
746
- struct xar *xar = a->format_data;
747
- enum la_zaction run;
748
- size_t size = 0;
749
- size_t rsize;
750
- int r;
751
-
752
- if (s > xar->bytes_remaining)
753
- s = (size_t)xar->bytes_remaining;
754
- if (s == 0 || xar->cur_file == NULL)
755
- return (0);
756
- if (xar->cur_file->data.compression == NONE) {
757
- checksum_update(&(xar->e_sumwrk), buff, s);
758
- checksum_update(&(xar->a_sumwrk), buff, s);
759
- size = rsize = s;
760
- } else {
761
- xar->stream.next_in = (const unsigned char *)buff;
762
- xar->stream.avail_in = s;
763
- if (xar->bytes_remaining > s)
764
- run = ARCHIVE_Z_RUN;
765
- else
766
- run = ARCHIVE_Z_FINISH;
767
- /* Compress file data. */
768
- for (;;) {
769
- r = compression_code(&(a->archive), &(xar->stream),
770
- run);
771
- if (r != ARCHIVE_OK && r != ARCHIVE_EOF)
772
- return (ARCHIVE_FATAL);
773
- if (xar->stream.avail_out == 0 ||
774
- run == ARCHIVE_Z_FINISH) {
775
- size = sizeof(xar->wbuff) -
776
- xar->stream.avail_out;
777
- checksum_update(&(xar->a_sumwrk), xar->wbuff,
778
- size);
779
- xar->cur_file->data.length += size;
780
- if (write_to_temp(a, xar->wbuff,
781
- size) != ARCHIVE_OK)
782
- return (ARCHIVE_FATAL);
783
- if (r == ARCHIVE_OK) {
784
- /* Output buffer was full */
785
- xar->stream.next_out = xar->wbuff;
786
- xar->stream.avail_out =
787
- sizeof(xar->wbuff);
788
- } else {
789
- /* ARCHIVE_EOF - We are done */
790
- break;
791
- }
792
- } else {
793
- /* Compressor wants more input */
794
- break;
795
- }
796
- }
797
- rsize = s - xar->stream.avail_in;
798
- checksum_update(&(xar->e_sumwrk), buff, rsize);
799
- }
800
- #if !defined(_WIN32) || defined(__CYGWIN__)
801
- if (xar->bytes_remaining ==
802
- (uint64_t)archive_entry_size(xar->cur_file->entry)) {
803
- /*
804
- * Get the path of a shell script if so.
805
- */
806
- const unsigned char *b = (const unsigned char *)buff;
807
-
808
- archive_string_empty(&(xar->cur_file->script));
809
- if (rsize > 2 && b[0] == '#' && b[1] == '!') {
810
- size_t i, end, off;
811
-
812
- off = 2;
813
- if (b[off] == ' ')
814
- off++;
815
- #ifdef PATH_MAX
816
- if ((rsize - off) > PATH_MAX)
817
- end = off + PATH_MAX;
818
- else
819
- #endif
820
- end = rsize;
821
- /* Find the end of a script path. */
822
- for (i = off; i < end && b[i] != '\0' &&
823
- b[i] != '\n' && b[i] != '\r' &&
824
- b[i] != ' ' && b[i] != '\t'; i++)
825
- ;
826
- archive_strncpy(&(xar->cur_file->script), b + off,
827
- i - off);
828
- }
829
- }
830
- #endif
831
-
832
- if (xar->cur_file->data.compression == NONE) {
833
- if (write_to_temp(a, buff, size) != ARCHIVE_OK)
834
- return (ARCHIVE_FATAL);
835
- xar->cur_file->data.length += size;
836
- }
837
- xar->bytes_remaining -= rsize;
838
-
839
- return (rsize);
840
- }
841
-
842
- static int
843
- xar_finish_entry(struct archive_write *a)
844
- {
845
- struct xar *xar = a->format_data;
846
- struct file *file;
847
- size_t s;
848
- ssize_t w;
849
-
850
- if (xar->cur_file == NULL)
851
- return (ARCHIVE_OK);
852
-
853
- while (xar->bytes_remaining > 0) {
854
- s = (size_t)xar->bytes_remaining;
855
- if (s > a->null_length)
856
- s = a->null_length;
857
- w = xar_write_data(a, a->nulls, s);
858
- if (w <= 0)
859
- return ((int)w);
860
- }
861
- file = xar->cur_file;
862
- checksum_final(&(xar->e_sumwrk), &(file->data.e_sum));
863
- checksum_final(&(xar->a_sumwrk), &(file->data.a_sum));
864
- xar->cur_file = NULL;
865
-
866
- return (ARCHIVE_OK);
867
- }
868
-
869
- static int
870
- xmlwrite_string_attr(struct archive_write *a, struct xml_writer *writer,
871
- const char *key, const char *value,
872
- const char *attrkey, const char *attrvalue)
873
- {
874
- int r;
875
-
876
- r = xml_writer_start_element(writer, key);
877
- if (r < 0) {
878
- archive_set_error(&a->archive,
879
- ARCHIVE_ERRNO_MISC,
880
- "xml_writer_start_element() failed: %d", r);
881
- return (ARCHIVE_FATAL);
882
- }
883
- if (attrkey != NULL && attrvalue != NULL) {
884
- r = xml_writer_write_attribute(writer, attrkey, attrvalue);
885
- if (r < 0) {
886
- archive_set_error(&a->archive,
887
- ARCHIVE_ERRNO_MISC,
888
- "xml_writer_write_attribute() failed: %d", r);
889
- return (ARCHIVE_FATAL);
890
- }
891
- }
892
- if (value != NULL) {
893
- r = xml_writer_write_string(writer, value);
894
- if (r < 0) {
895
- archive_set_error(&a->archive,
896
- ARCHIVE_ERRNO_MISC,
897
- "xml_writer_write_string() failed: %d", r);
898
- return (ARCHIVE_FATAL);
899
- }
900
- }
901
- r = xml_writer_end_element(writer);
902
- if (r < 0) {
903
- archive_set_error(&a->archive,
904
- ARCHIVE_ERRNO_MISC,
905
- "xml_writer_end_element() failed: %d", r);
906
- return (ARCHIVE_FATAL);
907
- }
908
- return (ARCHIVE_OK);
909
- }
910
-
911
- static int
912
- xmlwrite_string(struct archive_write *a, struct xml_writer *writer,
913
- const char *key, const char *value)
914
- {
915
- int r;
916
-
917
- if (value == NULL)
918
- return (ARCHIVE_OK);
919
-
920
- r = xml_writer_start_element(writer, key);
921
- if (r < 0) {
922
- archive_set_error(&a->archive,
923
- ARCHIVE_ERRNO_MISC,
924
- "xml_writer_start_element() failed: %d", r);
925
- return (ARCHIVE_FATAL);
926
- }
927
- if (value != NULL) {
928
- r = xml_writer_write_string(writer, value);
929
- if (r < 0) {
930
- archive_set_error(&a->archive,
931
- ARCHIVE_ERRNO_MISC,
932
- "xml_writer_write_string() failed: %d", r);
933
- return (ARCHIVE_FATAL);
934
- }
935
- }
936
- r = xml_writer_end_element(writer);
937
- if (r < 0) {
938
- archive_set_error(&a->archive,
939
- ARCHIVE_ERRNO_MISC,
940
- "xml_writer_end_element() failed: %d", r);
941
- return (ARCHIVE_FATAL);
942
- }
943
- return (ARCHIVE_OK);
944
- }
945
-
946
- static int __LA_PRINTF(4, 5)
947
- xmlwrite_fstring(struct archive_write *a, struct xml_writer *writer,
948
- const char *key, const char *fmt, ...)
949
- {
950
- struct xar *xar = a->format_data;
951
- va_list ap;
952
-
953
- va_start(ap, fmt);
954
- archive_string_empty(&xar->vstr);
955
- archive_string_vsprintf(&xar->vstr, fmt, ap);
956
- va_end(ap);
957
- return (xmlwrite_string(a, writer, key, xar->vstr.s));
958
- }
959
-
960
- static int
961
- xmlwrite_time(struct archive_write *a, struct xml_writer *writer,
962
- const char *key, time_t t, int z)
963
- {
964
- char timestr[100];
965
- struct tm tm;
966
-
967
- #if defined(HAVE_GMTIME_S)
968
- gmtime_s(&tm, &t);
969
- #elif defined(HAVE_GMTIME_R)
970
- gmtime_r(&t, &tm);
971
- #else
972
- memcpy(&tm, gmtime(&t), sizeof(tm));
973
- #endif
974
- memset(&timestr, 0, sizeof(timestr));
975
- /* Do not use %F and %T for portability. */
976
- strftime(timestr, sizeof(timestr), "%Y-%m-%dT%H:%M:%S", &tm);
977
- if (z)
978
- strcat(timestr, "Z");
979
- return (xmlwrite_string(a, writer, key, timestr));
980
- }
981
-
982
- static int
983
- xmlwrite_mode(struct archive_write *a, struct xml_writer *writer,
984
- const char *key, mode_t mode)
985
- {
986
- char ms[5];
987
-
988
- ms[0] = '0';
989
- ms[1] = '0' + ((mode >> 6) & 07);
990
- ms[2] = '0' + ((mode >> 3) & 07);
991
- ms[3] = '0' + (mode & 07);
992
- ms[4] = '\0';
993
-
994
- return (xmlwrite_string(a, writer, key, ms));
995
- }
996
-
997
- static int
998
- xmlwrite_sum(struct archive_write *a, struct xml_writer *writer,
999
- const char *key, struct chksumval *sum)
1000
- {
1001
- const char *algname;
1002
- int algsize;
1003
- char buff[MAX_SUM_SIZE*2 + 1];
1004
- char *p;
1005
- unsigned char *s;
1006
- int i, r;
1007
-
1008
- if (sum->len > 0) {
1009
- algname = getalgname(sum->alg);
1010
- algsize = getalgsize(sum->alg);
1011
- if (algname != NULL) {
1012
- const char *hex = "0123456789abcdef";
1013
- p = buff;
1014
- s = sum->val;
1015
- for (i = 0; i < algsize; i++) {
1016
- *p++ = hex[(*s >> 4)];
1017
- *p++ = hex[(*s & 0x0f)];
1018
- s++;
1019
- }
1020
- *p = '\0';
1021
- r = xmlwrite_string_attr(a, writer,
1022
- key, buff,
1023
- "style", algname);
1024
- if (r < 0)
1025
- return (ARCHIVE_FATAL);
1026
- }
1027
- }
1028
- return (ARCHIVE_OK);
1029
- }
1030
-
1031
- static int
1032
- xmlwrite_heap(struct archive_write *a, struct xml_writer *writer,
1033
- struct heap_data *heap)
1034
- {
1035
- const char *encname;
1036
- int r;
1037
-
1038
- r = xmlwrite_fstring(a, writer, "length", "%ju", (uintmax_t)heap->length);
1039
- if (r < 0)
1040
- return (ARCHIVE_FATAL);
1041
- r = xmlwrite_fstring(a, writer, "offset", "%ju", (uintmax_t)heap->temp_offset);
1042
- if (r < 0)
1043
- return (ARCHIVE_FATAL);
1044
- r = xmlwrite_fstring(a, writer, "size", "%ju", (uintmax_t)heap->size);
1045
- if (r < 0)
1046
- return (ARCHIVE_FATAL);
1047
- switch (heap->compression) {
1048
- case GZIP:
1049
- encname = "application/x-gzip"; break;
1050
- case BZIP2:
1051
- encname = "application/x-bzip2"; break;
1052
- case LZMA:
1053
- encname = "application/x-lzma"; break;
1054
- case XZ:
1055
- encname = "application/x-xz"; break;
1056
- default:
1057
- encname = "application/octet-stream"; break;
1058
- }
1059
- r = xmlwrite_string_attr(a, writer, "encoding", NULL,
1060
- "style", encname);
1061
- if (r < 0)
1062
- return (ARCHIVE_FATAL);
1063
- r = xmlwrite_sum(a, writer, "archived-checksum", &(heap->a_sum));
1064
- if (r < 0)
1065
- return (ARCHIVE_FATAL);
1066
- r = xmlwrite_sum(a, writer, "extracted-checksum", &(heap->e_sum));
1067
- if (r < 0)
1068
- return (ARCHIVE_FATAL);
1069
- return (ARCHIVE_OK);
1070
- }
1071
-
1072
- /*
1073
- * xar utility records fflags as following xml elements:
1074
- * <flags>
1075
- * <UserNoDump/>
1076
- * .....
1077
- * </flags>
1078
- * or
1079
- * <ext2>
1080
- * <NoDump/>
1081
- * .....
1082
- * </ext2>
1083
- * If xar is running on BSD platform, records <flags>..</flags>;
1084
- * if xar is running on linux platform, records <ext2>..</ext2>;
1085
- * otherwise does not record.
1086
- *
1087
- * Our implements records both <flags> and <ext2> if it's necessary.
1088
- */
1089
- static int
1090
- make_fflags_entry(struct archive_write *a, struct xml_writer *writer,
1091
- const char *element, const char *fflags_text)
1092
- {
1093
- static const struct flagentry {
1094
- const char *name;
1095
- const char *xarname;
1096
- }
1097
- flagbsd[] = {
1098
- { "sappnd", "SystemAppend"},
1099
- { "sappend", "SystemAppend"},
1100
- { "arch", "SystemArchived"},
1101
- { "archived", "SystemArchived"},
1102
- { "schg", "SystemImmutable"},
1103
- { "schange", "SystemImmutable"},
1104
- { "simmutable", "SystemImmutable"},
1105
- { "nosunlnk", "SystemNoUnlink"},
1106
- { "nosunlink", "SystemNoUnlink"},
1107
- { "snapshot", "SystemSnapshot"},
1108
- { "uappnd", "UserAppend"},
1109
- { "uappend", "UserAppend"},
1110
- { "uchg", "UserImmutable"},
1111
- { "uchange", "UserImmutable"},
1112
- { "uimmutable", "UserImmutable"},
1113
- { "nodump", "UserNoDump"},
1114
- { "noopaque", "UserOpaque"},
1115
- { "nouunlnk", "UserNoUnlink"},
1116
- { "nouunlink", "UserNoUnlink"},
1117
- { NULL, NULL}
1118
- },
1119
- flagext2[] = {
1120
- { "sappnd", "AppendOnly"},
1121
- { "sappend", "AppendOnly"},
1122
- { "schg", "Immutable"},
1123
- { "schange", "Immutable"},
1124
- { "simmutable", "Immutable"},
1125
- { "nodump", "NoDump"},
1126
- { "nouunlnk", "Undelete"},
1127
- { "nouunlink", "Undelete"},
1128
- { "btree", "BTree"},
1129
- { "comperr", "CompError"},
1130
- { "compress", "Compress"},
1131
- { "noatime", "NoAtime"},
1132
- { "compdirty", "CompDirty"},
1133
- { "comprblk", "CompBlock"},
1134
- { "dirsync", "DirSync"},
1135
- { "hashidx", "HashIndexed"},
1136
- { "imagic", "iMagic"},
1137
- { "journal", "Journaled"},
1138
- { "securedeletion", "SecureDeletion"},
1139
- { "sync", "Synchronous"},
1140
- { "notail", "NoTail"},
1141
- { "topdir", "TopDir"},
1142
- { "reserved", "Reserved"},
1143
- { NULL, NULL}
1144
- };
1145
- const struct flagentry *fe, *flagentry;
1146
- const char *p;
1147
- int r, started;
1148
-
1149
- if (strcmp(element, "ext2") == 0)
1150
- flagentry = flagext2;
1151
- else
1152
- flagentry = flagbsd;
1153
- p = fflags_text;
1154
- started = 0;
1155
- do {
1156
- const char *cp, *name = NULL;
1157
-
1158
- cp = strchr(p, ',');
1159
- if (cp == NULL)
1160
- cp = p + strlen(p);
1161
-
1162
- for (fe = flagentry; fe->name != NULL; fe++) {
1163
- if (strncmp(p, fe->name, cp - p) == 0) {
1164
- if (fe->name[cp - p] != '\0')
1165
- continue;
1166
- name = fe->xarname;
1167
- break;
1168
- }
1169
- }
1170
- if (*cp == ',')
1171
- p = cp + 1;
1172
- else
1173
- p = NULL;
1174
-
1175
- if (name != NULL) {
1176
- if (!started) {
1177
- r = xml_writer_start_element(writer, element);
1178
- if (r < 0) {
1179
- archive_set_error(&a->archive,
1180
- ARCHIVE_ERRNO_MISC,
1181
- "xml_writer_start_element()"
1182
- " failed: %d", r);
1183
- return (ARCHIVE_FATAL);
1184
- }
1185
- started = 1;
1186
- }
1187
- r = xmlwrite_string(a, writer, name, NULL);
1188
- if (r != ARCHIVE_OK)
1189
- return (r);
1190
- }
1191
- } while (p != NULL);
1192
-
1193
- if (started) {
1194
- r = xml_writer_end_element(writer);
1195
- if (r < 0) {
1196
- archive_set_error(&a->archive,
1197
- ARCHIVE_ERRNO_MISC,
1198
- "xml_writer_end_element() failed: %d", r);
1199
- return (ARCHIVE_FATAL);
1200
- }
1201
- }
1202
- return (ARCHIVE_OK);
1203
- }
1204
-
1205
- /*
1206
- * This function determines whether a UTF-8 string contains
1207
- * only codepoints that are convertible to Latin-1. Strings
1208
- * beyond Latin-1 are stored base64-encoded in the XAR TOC.
1209
- */
1210
- static int
1211
- is_u8_zstring_latin1(const char *in)
1212
- {
1213
- unsigned int c;
1214
- while (*in) {
1215
- c = *in++;
1216
- if (c < 0x80) continue;
1217
- /*
1218
- * Filter out non-continuation, any continuation of 2-3
1219
- * bytes, and any continuation of 1 byte whose high 3 bits
1220
- * are non-zero. Recall, 1-byte continuations can store 11
1221
- * bits whereas Latin-1 codepoints are only 8 bits wide.
1222
- */
1223
- if ((c & 0xFC) != 0xC0)
1224
- return (0);
1225
- c = *in++;
1226
- /*
1227
- * If we get any non-continuation byte (including 0x00!),
1228
- * the string is not valid UTF-8.
1229
- */
1230
- if ((c & 0xC0) != 0x80)
1231
- return (0); /* invalid unicode */
1232
- }
1233
- return (1);
1234
- }
1235
-
1236
- static int
1237
- make_file_entry(struct archive_write *a, struct xml_writer *writer,
1238
- struct file *file)
1239
- {
1240
- struct xar *xar = a->format_data;
1241
- const char *filetype, *filelink, *fflags;
1242
- struct archive_string linkto;
1243
- struct heap_data *heap;
1244
- const char *p;
1245
- size_t len;
1246
- int r, r2;
1247
-
1248
- r2 = ARCHIVE_OK;
1249
-
1250
- /*
1251
- * Make a file name entry, "<name>".
1252
- */
1253
- if (!is_u8_zstring_latin1(file->basename.s)) {
1254
- r = xml_writer_start_element(writer, "name");
1255
- if (r < 0) {
1256
- archive_set_error(&a->archive,
1257
- ARCHIVE_ERRNO_MISC,
1258
- "xml_writer_start_element() failed: %d", r);
1259
- return (ARCHIVE_FATAL);
1260
- }
1261
- r = xml_writer_write_attribute(writer,
1262
- "enctype", "base64");
1263
- if (r < 0) {
1264
- archive_set_error(&a->archive,
1265
- ARCHIVE_ERRNO_MISC,
1266
- "xml_writer_write_attribute() failed: %d", r);
1267
- return (ARCHIVE_FATAL);
1268
- }
1269
- r = xml_writer_write_base64(writer, file->basename.s,
1270
- 0, (int)archive_strlen(&(file->basename)));
1271
- if (r < 0) {
1272
- archive_set_error(&a->archive,
1273
- ARCHIVE_ERRNO_MISC,
1274
- "xml_writer_write_base64() failed: %d", r);
1275
- return (ARCHIVE_FATAL);
1276
- }
1277
- r = xml_writer_end_element(writer);
1278
- if (r < 0) {
1279
- archive_set_error(&a->archive,
1280
- ARCHIVE_ERRNO_MISC,
1281
- "xml_writer_end_element() failed: %d", r);
1282
- return (ARCHIVE_FATAL);
1283
- }
1284
- } else {
1285
- r = xmlwrite_string(a, writer, "name", file->basename.s);
1286
- if (r < 0)
1287
- return (ARCHIVE_FATAL);
1288
- }
1289
-
1290
- /*
1291
- * Make a file type entry, "<type>".
1292
- */
1293
- filelink = NULL;
1294
- archive_string_init(&linkto);
1295
- switch (archive_entry_filetype(file->entry)) {
1296
- case AE_IFDIR:
1297
- filetype = "directory"; break;
1298
- case AE_IFLNK:
1299
- filetype = "symlink"; break;
1300
- case AE_IFCHR:
1301
- filetype = "character special"; break;
1302
- case AE_IFBLK:
1303
- filetype = "block special"; break;
1304
- case AE_IFSOCK:
1305
- filetype = "socket"; break;
1306
- case AE_IFIFO:
1307
- filetype = "fifo"; break;
1308
- case AE_IFREG:
1309
- default:
1310
- if (file->hardlink_target != NULL) {
1311
- filetype = "hardlink";
1312
- filelink = "link";
1313
- if (file->hardlink_target == file)
1314
- archive_strcpy(&linkto, "original");
1315
- else
1316
- archive_string_sprintf(&linkto, "%d",
1317
- file->hardlink_target->id);
1318
- } else
1319
- filetype = "file";
1320
- break;
1321
- }
1322
- r = xmlwrite_string_attr(a, writer, "type", filetype,
1323
- filelink, linkto.s);
1324
- archive_string_free(&linkto);
1325
- if (r < 0)
1326
- return (ARCHIVE_FATAL);
1327
-
1328
- /*
1329
- * On a virtual directory, we record "name" and "type" only.
1330
- */
1331
- if (file->virtual)
1332
- return (ARCHIVE_OK);
1333
-
1334
- switch (archive_entry_filetype(file->entry)) {
1335
- case AE_IFLNK:
1336
- /*
1337
- * xar utility has checked a file type, which
1338
- * a symbolic-link file has referenced.
1339
- * For example:
1340
- * <link type="directory">../ref/</link>
1341
- * The symlink target file is "../ref/" and its
1342
- * file type is a directory.
1343
- *
1344
- * <link type="file">../f</link>
1345
- * The symlink target file is "../f" and its
1346
- * file type is a regular file.
1347
- *
1348
- * But our implementation cannot do it, and then we
1349
- * always record that a attribute "type" is "broken",
1350
- * for example:
1351
- * <link type="broken">foo/bar</link>
1352
- * It means "foo/bar" is not reachable.
1353
- */
1354
- r = xmlwrite_string_attr(a, writer, "link",
1355
- file->symlink.s,
1356
- "type", "broken");
1357
- if (r < 0)
1358
- return (ARCHIVE_FATAL);
1359
- break;
1360
- case AE_IFCHR:
1361
- case AE_IFBLK:
1362
- r = xml_writer_start_element(writer, "device");
1363
- if (r < 0) {
1364
- archive_set_error(&a->archive,
1365
- ARCHIVE_ERRNO_MISC,
1366
- "xml_writer_start_element() failed: %d", r);
1367
- return (ARCHIVE_FATAL);
1368
- }
1369
- r = xmlwrite_fstring(a, writer, "major",
1370
- "%ld", (long)archive_entry_rdevmajor(file->entry));
1371
- if (r < 0)
1372
- return (ARCHIVE_FATAL);
1373
- r = xmlwrite_fstring(a, writer, "minor",
1374
- "%ld", (long)archive_entry_rdevminor(file->entry));
1375
- if (r < 0)
1376
- return (ARCHIVE_FATAL);
1377
- r = xml_writer_end_element(writer);
1378
- if (r < 0) {
1379
- archive_set_error(&a->archive,
1380
- ARCHIVE_ERRNO_MISC,
1381
- "xml_writer_end_element() failed: %d", r);
1382
- return (ARCHIVE_FATAL);
1383
- }
1384
- break;
1385
- default:
1386
- break;
1387
- }
1388
-
1389
- /*
1390
- * Make a inode entry, "<inode>".
1391
- */
1392
- r = xmlwrite_fstring(a, writer, "inode",
1393
- "%jd", (intmax_t)archive_entry_ino64(file->entry));
1394
- if (r < 0)
1395
- return (ARCHIVE_FATAL);
1396
- if (archive_entry_dev(file->entry) != 0) {
1397
- r = xmlwrite_fstring(a, writer, "deviceno",
1398
- "%ld", (long)archive_entry_dev(file->entry));
1399
- if (r < 0)
1400
- return (ARCHIVE_FATAL);
1401
- }
1402
-
1403
- /*
1404
- * Make a file mode entry, "<mode>".
1405
- */
1406
- r = xmlwrite_mode(a, writer, "mode",
1407
- archive_entry_mode(file->entry));
1408
- if (r < 0)
1409
- return (ARCHIVE_FATAL);
1410
-
1411
- /*
1412
- * Make a user entry, "<uid>" and "<user>.
1413
- */
1414
- r = xmlwrite_fstring(a, writer, "uid",
1415
- "%jd", (intmax_t)archive_entry_uid(file->entry));
1416
- if (r < 0)
1417
- return (ARCHIVE_FATAL);
1418
- r = archive_entry_uname_l(file->entry, &p, &len, xar->sconv);
1419
- if (r != 0) {
1420
- if (errno == ENOMEM) {
1421
- archive_set_error(&a->archive, ENOMEM,
1422
- "Can't allocate memory for Uname");
1423
- return (ARCHIVE_FATAL);
1424
- }
1425
- archive_set_error(&a->archive,
1426
- ARCHIVE_ERRNO_FILE_FORMAT,
1427
- "Can't translate uname '%s' to UTF-8",
1428
- archive_entry_uname(file->entry));
1429
- r2 = ARCHIVE_WARN;
1430
- }
1431
- if (len > 0) {
1432
- r = xmlwrite_string(a, writer, "user", p);
1433
- if (r < 0)
1434
- return (ARCHIVE_FATAL);
1435
- }
1436
-
1437
- /*
1438
- * Make a group entry, "<gid>" and "<group>.
1439
- */
1440
- r = xmlwrite_fstring(a, writer, "gid",
1441
- "%jd", (intmax_t)archive_entry_gid(file->entry));
1442
- if (r < 0)
1443
- return (ARCHIVE_FATAL);
1444
- r = archive_entry_gname_l(file->entry, &p, &len, xar->sconv);
1445
- if (r != 0) {
1446
- if (errno == ENOMEM) {
1447
- archive_set_error(&a->archive, ENOMEM,
1448
- "Can't allocate memory for Gname");
1449
- return (ARCHIVE_FATAL);
1450
- }
1451
- archive_set_error(&a->archive,
1452
- ARCHIVE_ERRNO_FILE_FORMAT,
1453
- "Can't translate gname '%s' to UTF-8",
1454
- archive_entry_gname(file->entry));
1455
- r2 = ARCHIVE_WARN;
1456
- }
1457
- if (len > 0) {
1458
- r = xmlwrite_string(a, writer, "group", p);
1459
- if (r < 0)
1460
- return (ARCHIVE_FATAL);
1461
- }
1462
-
1463
- /*
1464
- * Make a ctime entry, "<ctime>".
1465
- */
1466
- if (archive_entry_ctime_is_set(file->entry)) {
1467
- r = xmlwrite_time(a, writer, "ctime",
1468
- archive_entry_ctime(file->entry), 1);
1469
- if (r < 0)
1470
- return (ARCHIVE_FATAL);
1471
- }
1472
-
1473
- /*
1474
- * Make an mtime entry, "<mtime>".
1475
- */
1476
- if (archive_entry_mtime_is_set(file->entry)) {
1477
- r = xmlwrite_time(a, writer, "mtime",
1478
- archive_entry_mtime(file->entry), 1);
1479
- if (r < 0)
1480
- return (ARCHIVE_FATAL);
1481
- }
1482
-
1483
- /*
1484
- * Make a atime entry, "<atime>".
1485
- */
1486
- if (archive_entry_atime_is_set(file->entry)) {
1487
- r = xmlwrite_time(a, writer, "atime",
1488
- archive_entry_atime(file->entry), 1);
1489
- if (r < 0)
1490
- return (ARCHIVE_FATAL);
1491
- }
1492
-
1493
- /*
1494
- * Make fflags entries, "<flags>" and "<ext2>".
1495
- */
1496
- fflags = archive_entry_fflags_text(file->entry);
1497
- if (fflags != NULL) {
1498
- r = make_fflags_entry(a, writer, "flags", fflags);
1499
- if (r < 0)
1500
- return (r);
1501
- r = make_fflags_entry(a, writer, "ext2", fflags);
1502
- if (r < 0)
1503
- return (r);
1504
- }
1505
-
1506
- /*
1507
- * Make extended attribute entries, "<ea>".
1508
- */
1509
- archive_entry_xattr_reset(file->entry);
1510
- for (heap = file->xattr.first; heap != NULL; heap = heap->next) {
1511
- const char *name;
1512
- const void *value;
1513
- size_t size;
1514
-
1515
- archive_entry_xattr_next(file->entry,
1516
- &name, &value, &size);
1517
- r = xml_writer_start_element(writer, "ea");
1518
- if (r < 0) {
1519
- archive_set_error(&a->archive,
1520
- ARCHIVE_ERRNO_MISC,
1521
- "xml_writer_start_element() failed: %d", r);
1522
- return (ARCHIVE_FATAL);
1523
- }
1524
- r = xml_writer_write_attributef(writer,
1525
- "id", "%d", heap->id);
1526
- if (r < 0) {
1527
- archive_set_error(&a->archive,
1528
- ARCHIVE_ERRNO_MISC,
1529
- "xml_writer_write_attributef() failed: %d", r);
1530
- return (ARCHIVE_FATAL);
1531
- }
1532
- r = xmlwrite_heap(a, writer, heap);
1533
- if (r < 0)
1534
- return (ARCHIVE_FATAL);
1535
- r = xmlwrite_string(a, writer, "name", name);
1536
- if (r < 0)
1537
- return (ARCHIVE_FATAL);
1538
-
1539
- r = xml_writer_end_element(writer);
1540
- if (r < 0) {
1541
- archive_set_error(&a->archive,
1542
- ARCHIVE_ERRNO_MISC,
1543
- "xml_writer_end_element() failed: %d", r);
1544
- return (ARCHIVE_FATAL);
1545
- }
1546
- }
1547
-
1548
- /*
1549
- * Make a file data entry, "<data>".
1550
- */
1551
- if (file->data.length > 0) {
1552
- r = xml_writer_start_element(writer, "data");
1553
- if (r < 0) {
1554
- archive_set_error(&a->archive,
1555
- ARCHIVE_ERRNO_MISC,
1556
- "xml_writer_start_element() failed: %d", r);
1557
- return (ARCHIVE_FATAL);
1558
- }
1559
-
1560
- r = xmlwrite_heap(a, writer, &(file->data));
1561
- if (r < 0)
1562
- return (ARCHIVE_FATAL);
1563
-
1564
- r = xml_writer_end_element(writer);
1565
- if (r < 0) {
1566
- archive_set_error(&a->archive,
1567
- ARCHIVE_ERRNO_MISC,
1568
- "xml_writer_end_element() failed: %d", r);
1569
- return (ARCHIVE_FATAL);
1570
- }
1571
- }
1572
-
1573
- if (archive_strlen(&file->script) > 0) {
1574
- r = xml_writer_start_element(writer, "content");
1575
- if (r < 0) {
1576
- archive_set_error(&a->archive,
1577
- ARCHIVE_ERRNO_MISC,
1578
- "xml_writer_start_element() failed: %d", r);
1579
- return (ARCHIVE_FATAL);
1580
- }
1581
-
1582
- r = xmlwrite_string(a, writer,
1583
- "interpreter", file->script.s);
1584
- if (r < 0)
1585
- return (ARCHIVE_FATAL);
1586
-
1587
- r = xmlwrite_string(a, writer, "type", "script");
1588
- if (r < 0)
1589
- return (ARCHIVE_FATAL);
1590
-
1591
- r = xml_writer_end_element(writer);
1592
- if (r < 0) {
1593
- archive_set_error(&a->archive,
1594
- ARCHIVE_ERRNO_MISC,
1595
- "xml_writer_end_element() failed: %d", r);
1596
- return (ARCHIVE_FATAL);
1597
- }
1598
- }
1599
-
1600
- return (r2);
1601
- }
1602
-
1603
- /*
1604
- * Make the TOC
1605
- */
1606
- static int
1607
- make_toc(struct archive_write *a)
1608
- {
1609
- struct xar *xar = a->format_data;
1610
- struct file *np;
1611
- struct xml_writer *writer;
1612
- const char* content;
1613
- size_t use;
1614
- int algsize;
1615
- int r, ret;
1616
-
1617
- ret = ARCHIVE_FATAL;
1618
-
1619
- /*
1620
- * Initialize xml writer.
1621
- */
1622
- writer = NULL;
1623
- r = xml_writer_create(&writer);
1624
- if (r < 0) {
1625
- archive_set_error(&a->archive,
1626
- ARCHIVE_ERRNO_MISC,
1627
- "xml_writer_create() failed: %d", r);
1628
- goto exit_toc;
1629
- }
1630
- r = xml_writer_set_indent(writer, 4);
1631
- if (r < 0) {
1632
- archive_set_error(&a->archive,
1633
- ARCHIVE_ERRNO_MISC,
1634
- "xml_writer_set_indent() failed: %d", r);
1635
- goto exit_toc;
1636
- }
1637
- r = xml_writer_start_document(writer);
1638
- if (r < 0) {
1639
- archive_set_error(&a->archive,
1640
- ARCHIVE_ERRNO_MISC,
1641
- "xml_writer_start_document() failed: %d", r);
1642
- goto exit_toc;
1643
- }
1644
-
1645
- /*
1646
- * Start recording TOC
1647
- */
1648
- r = xml_writer_start_element(writer, "xar");
1649
- if (r < 0) {
1650
- archive_set_error(&a->archive,
1651
- ARCHIVE_ERRNO_MISC,
1652
- "xml_writer_start_element() failed: %d", r);
1653
- goto exit_toc;
1654
- }
1655
- r = xml_writer_start_element(writer, "toc");
1656
- if (r < 0) {
1657
- archive_set_error(&a->archive,
1658
- ARCHIVE_ERRNO_MISC,
1659
- "xml_writer_start_element() failed: %d", r);
1660
- goto exit_toc;
1661
- }
1662
-
1663
- /*
1664
- * Record the creation time of the archive file.
1665
- */
1666
- r = xmlwrite_time(a, writer, "creation-time", time(NULL), 0);
1667
- if (r < 0)
1668
- goto exit_toc;
1669
-
1670
- /*
1671
- * Record the checksum value of TOC
1672
- */
1673
- algsize = getalgsize(xar->opt_toc_sumalg);
1674
- if (algsize) {
1675
- /*
1676
- * Record TOC checksum
1677
- */
1678
- r = xml_writer_start_element(writer, "checksum");
1679
- if (r < 0) {
1680
- archive_set_error(&a->archive,
1681
- ARCHIVE_ERRNO_MISC,
1682
- "xml_writer_start_element() failed: %d", r);
1683
- goto exit_toc;
1684
- }
1685
- r = xml_writer_write_attribute(writer, "style",
1686
- getalgname(xar->opt_toc_sumalg));
1687
- if (r < 0) {
1688
- archive_set_error(&a->archive,
1689
- ARCHIVE_ERRNO_MISC,
1690
- "xml_writer_write_attribute() failed: %d", r);
1691
- goto exit_toc;
1692
- }
1693
-
1694
- /*
1695
- * Record the offset of the value of checksum of TOC
1696
- */
1697
- r = xmlwrite_string(a, writer, "offset", "0");
1698
- if (r < 0)
1699
- goto exit_toc;
1700
-
1701
- /*
1702
- * Record the size of the value of checksum of TOC
1703
- */
1704
- r = xmlwrite_fstring(a, writer, "size", "%d", algsize);
1705
- if (r < 0)
1706
- goto exit_toc;
1707
-
1708
- r = xml_writer_end_element(writer);
1709
- if (r < 0) {
1710
- archive_set_error(&a->archive,
1711
- ARCHIVE_ERRNO_MISC,
1712
- "xml_writer_end_element() failed: %d", r);
1713
- goto exit_toc;
1714
- }
1715
- }
1716
-
1717
- np = xar->root;
1718
- do {
1719
- if (np != np->parent) {
1720
- r = make_file_entry(a, writer, np);
1721
- if (r != ARCHIVE_OK)
1722
- goto exit_toc;
1723
- }
1724
-
1725
- if (np->dir && np->children.first != NULL) {
1726
- /* Enter to sub directories. */
1727
- np = np->children.first;
1728
- r = xml_writer_start_element(writer,
1729
- "file");
1730
- if (r < 0) {
1731
- archive_set_error(&a->archive,
1732
- ARCHIVE_ERRNO_MISC,
1733
- "xml_writer_start_element() "
1734
- "failed: %d", r);
1735
- goto exit_toc;
1736
- }
1737
- r = xml_writer_write_attributef(
1738
- writer, "id", "%d", np->id);
1739
- if (r < 0) {
1740
- archive_set_error(&a->archive,
1741
- ARCHIVE_ERRNO_MISC,
1742
- "xml_writer_write_attributef() "
1743
- "failed: %d", r);
1744
- goto exit_toc;
1745
- }
1746
- continue;
1747
- }
1748
- while (np != np->parent) {
1749
- r = xml_writer_end_element(writer);
1750
- if (r < 0) {
1751
- archive_set_error(&a->archive,
1752
- ARCHIVE_ERRNO_MISC,
1753
- "xml_writer_end_element() "
1754
- "failed: %d", r);
1755
- goto exit_toc;
1756
- }
1757
- if (np->chnext == NULL) {
1758
- /* Return to the parent directory. */
1759
- np = np->parent;
1760
- } else {
1761
- np = np->chnext;
1762
- r = xml_writer_start_element(writer,
1763
- "file");
1764
- if (r < 0) {
1765
- archive_set_error(&a->archive,
1766
- ARCHIVE_ERRNO_MISC,
1767
- "xml_writer_start_element() "
1768
- "failed: %d", r);
1769
- goto exit_toc;
1770
- }
1771
- r = xml_writer_write_attributef(
1772
- writer, "id", "%d", np->id);
1773
- if (r < 0) {
1774
- archive_set_error(&a->archive,
1775
- ARCHIVE_ERRNO_MISC,
1776
- "xml_writer_write_attributef() "
1777
- "failed: %d", r);
1778
- goto exit_toc;
1779
- }
1780
- break;
1781
- }
1782
- }
1783
- } while (np != np->parent);
1784
-
1785
- r = xml_writer_end_document(writer);
1786
- if (r < 0) {
1787
- archive_set_error(&a->archive,
1788
- ARCHIVE_ERRNO_MISC,
1789
- "xml_writer_end_document() failed: %d", r);
1790
- goto exit_toc;
1791
- }
1792
-
1793
- r = xml_writer_get_final_content_and_length(writer, &content, &use);
1794
- if (r < 0) {
1795
- archive_set_error(&a->archive,
1796
- ARCHIVE_ERRNO_MISC,
1797
- "xml_writer_get_final_content_and_length() failed: %d", r);
1798
- goto exit_toc;
1799
- }
1800
-
1801
- #if DEBUG_PRINT_TOC
1802
- fprintf(stderr, "\n---TOC-- %d bytes --\n%s\n",
1803
- (int)strlen(content), content);
1804
- #endif
1805
-
1806
- /*
1807
- * Compress the TOC and calculate the sum of the TOC.
1808
- */
1809
- xar->toc.temp_offset = xar->temp_offset;
1810
- xar->toc.size = (uint64_t)use;
1811
- checksum_init(&(xar->a_sumwrk), xar->opt_toc_sumalg);
1812
-
1813
- r = compression_init_encoder_gzip(&(a->archive),
1814
- &(xar->stream), 6, 1);
1815
- if (r != ARCHIVE_OK)
1816
- goto exit_toc;
1817
- xar->stream.next_in = (const unsigned char *)content;
1818
- xar->stream.avail_in = use;
1819
- xar->stream.total_in = 0;
1820
- xar->stream.next_out = xar->wbuff;
1821
- xar->stream.avail_out = sizeof(xar->wbuff);
1822
- xar->stream.total_out = 0;
1823
- for (;;) {
1824
- size_t size;
1825
-
1826
- r = compression_code(&(a->archive),
1827
- &(xar->stream), ARCHIVE_Z_FINISH);
1828
- if (r != ARCHIVE_OK && r != ARCHIVE_EOF)
1829
- goto exit_toc;
1830
- size = sizeof(xar->wbuff) - xar->stream.avail_out;
1831
- checksum_update(&(xar->a_sumwrk), xar->wbuff, size);
1832
- if (write_to_temp(a, xar->wbuff, size) != ARCHIVE_OK)
1833
- goto exit_toc;
1834
- if (r == ARCHIVE_EOF)
1835
- break;
1836
- xar->stream.next_out = xar->wbuff;
1837
- xar->stream.avail_out = sizeof(xar->wbuff);
1838
- }
1839
- r = compression_end(&(a->archive), &(xar->stream));
1840
- if (r != ARCHIVE_OK)
1841
- goto exit_toc;
1842
- xar->toc.length = xar->stream.total_out;
1843
- xar->toc.compression = GZIP;
1844
- checksum_final(&(xar->a_sumwrk), &(xar->toc.a_sum));
1845
-
1846
- ret = ARCHIVE_OK;
1847
- exit_toc:
1848
- if (writer)
1849
- xml_writer_destroy(writer);
1850
-
1851
- return (ret);
1852
- }
1853
-
1854
- static int
1855
- flush_wbuff(struct archive_write *a)
1856
- {
1857
- struct xar *xar = a->format_data;
1858
- int r;
1859
- size_t s;
1860
-
1861
- s = sizeof(xar->wbuff) - xar->wbuff_remaining;
1862
- r = __archive_write_output(a, xar->wbuff, s);
1863
- if (r != ARCHIVE_OK)
1864
- return (r);
1865
- xar->wbuff_remaining = sizeof(xar->wbuff);
1866
- return (r);
1867
- }
1868
-
1869
- static int
1870
- copy_out(struct archive_write *a, uint64_t offset, uint64_t length)
1871
- {
1872
- struct xar *xar = a->format_data;
1873
- int r;
1874
-
1875
- if (lseek(xar->temp_fd, offset, SEEK_SET) < 0) {
1876
- archive_set_error(&(a->archive), errno, "lseek failed");
1877
- return (ARCHIVE_FATAL);
1878
- }
1879
- while (length) {
1880
- size_t rsize;
1881
- ssize_t rs;
1882
- unsigned char *wb;
1883
-
1884
- if (length > xar->wbuff_remaining)
1885
- rsize = xar->wbuff_remaining;
1886
- else
1887
- rsize = (size_t)length;
1888
- wb = xar->wbuff + (sizeof(xar->wbuff) - xar->wbuff_remaining);
1889
- rs = read(xar->temp_fd, wb, rsize);
1890
- if (rs < 0) {
1891
- archive_set_error(&(a->archive), errno,
1892
- "Can't read temporary file(%jd)",
1893
- (intmax_t)rs);
1894
- return (ARCHIVE_FATAL);
1895
- }
1896
- if (rs == 0) {
1897
- archive_set_error(&(a->archive),
1898
- ARCHIVE_ERRNO_FILE_FORMAT,
1899
- "Truncated xar archive");
1900
- return (ARCHIVE_FATAL);
1901
- }
1902
- xar->wbuff_remaining -= rs;
1903
- length -= rs;
1904
- if (xar->wbuff_remaining == 0) {
1905
- r = flush_wbuff(a);
1906
- if (r != ARCHIVE_OK)
1907
- return (r);
1908
- }
1909
- }
1910
- return (ARCHIVE_OK);
1911
- }
1912
-
1913
- static int
1914
- xar_close(struct archive_write *a)
1915
- {
1916
- struct xar *xar = a->format_data;
1917
- unsigned char *wb;
1918
- uint64_t length;
1919
- int r;
1920
-
1921
- /* Empty! */
1922
- if (xar->root->children.first == NULL)
1923
- return (ARCHIVE_OK);
1924
-
1925
- /* Save the length of all file extended attributes and contents. */
1926
- length = xar->temp_offset;
1927
-
1928
- /* Connect hardlinked files */
1929
- file_connect_hardlink_files(xar);
1930
-
1931
- /* Make the TOC */
1932
- r = make_toc(a);
1933
- if (r != ARCHIVE_OK)
1934
- return (r);
1935
- /*
1936
- * Make the xar header on wbuff(write buffer).
1937
- */
1938
- wb = xar->wbuff;
1939
- xar->wbuff_remaining = sizeof(xar->wbuff);
1940
- archive_be32enc(&wb[0], HEADER_MAGIC);
1941
- archive_be16enc(&wb[4], HEADER_SIZE);
1942
- archive_be16enc(&wb[6], HEADER_VERSION);
1943
- archive_be64enc(&wb[8], xar->toc.length);
1944
- archive_be64enc(&wb[16], xar->toc.size);
1945
- archive_be32enc(&wb[24], xar->toc.a_sum.alg);
1946
- xar->wbuff_remaining -= HEADER_SIZE;
1947
-
1948
- /*
1949
- * Write the TOC
1950
- */
1951
- r = copy_out(a, xar->toc.temp_offset, xar->toc.length);
1952
- if (r != ARCHIVE_OK)
1953
- return (r);
1954
-
1955
- /* Write the checksum value of the TOC. */
1956
- if (xar->toc.a_sum.len) {
1957
- if (xar->wbuff_remaining < xar->toc.a_sum.len) {
1958
- r = flush_wbuff(a);
1959
- if (r != ARCHIVE_OK)
1960
- return (r);
1961
- }
1962
- wb = xar->wbuff + (sizeof(xar->wbuff) - xar->wbuff_remaining);
1963
- memcpy(wb, xar->toc.a_sum.val, xar->toc.a_sum.len);
1964
- xar->wbuff_remaining -= xar->toc.a_sum.len;
1965
- }
1966
-
1967
- /*
1968
- * Write all file extended attributes and contents.
1969
- */
1970
- r = copy_out(a, xar->toc.a_sum.len, length);
1971
- if (r != ARCHIVE_OK)
1972
- return (r);
1973
- r = flush_wbuff(a);
1974
- return (r);
1975
- }
1976
-
1977
- static int
1978
- xar_free(struct archive_write *a)
1979
- {
1980
- struct xar *xar = a->format_data;
1981
-
1982
- /* Close the temporary file. */
1983
- if (xar->temp_fd >= 0)
1984
- close(xar->temp_fd);
1985
-
1986
- archive_string_free(&(xar->cur_dirstr));
1987
- archive_string_free(&(xar->tstr));
1988
- archive_string_free(&(xar->vstr));
1989
- file_free_hardlinks(xar);
1990
- file_free_register(xar);
1991
- compression_end(&(a->archive), &(xar->stream));
1992
- free(xar);
1993
-
1994
- return (ARCHIVE_OK);
1995
- }
1996
-
1997
- static int
1998
- file_cmp_node(const struct archive_rb_node *n1,
1999
- const struct archive_rb_node *n2)
2000
- {
2001
- const struct file *f1 = (const struct file *)n1;
2002
- const struct file *f2 = (const struct file *)n2;
2003
-
2004
- return (strcmp(f1->basename.s, f2->basename.s));
2005
- }
2006
-
2007
- static int
2008
- file_cmp_key(const struct archive_rb_node *n, const void *key)
2009
- {
2010
- const struct file *f = (const struct file *)n;
2011
-
2012
- return (strcmp(f->basename.s, (const char *)key));
2013
- }
2014
-
2015
- static struct file *
2016
- file_new(struct archive_write *a, struct archive_entry *entry)
2017
- {
2018
- struct file *file;
2019
- static const struct archive_rb_tree_ops rb_ops = {
2020
- file_cmp_node, file_cmp_key
2021
- };
2022
-
2023
- file = calloc(1, sizeof(*file));
2024
- if (file == NULL)
2025
- return (NULL);
2026
-
2027
- if (entry != NULL)
2028
- file->entry = archive_entry_clone(entry);
2029
- else
2030
- file->entry = archive_entry_new2(&a->archive);
2031
- if (file->entry == NULL) {
2032
- free(file);
2033
- return (NULL);
2034
- }
2035
- __archive_rb_tree_init(&(file->rbtree), &rb_ops);
2036
- file->children.first = NULL;
2037
- file->children.last = &(file->children.first);
2038
- file->xattr.first = NULL;
2039
- file->xattr.last = &(file->xattr.first);
2040
- archive_string_init(&(file->parentdir));
2041
- archive_string_init(&(file->basename));
2042
- archive_string_init(&(file->symlink));
2043
- archive_string_init(&(file->script));
2044
- if (entry != NULL && archive_entry_filetype(entry) == AE_IFDIR)
2045
- file->dir = 1;
2046
-
2047
- return (file);
2048
- }
2049
-
2050
- static void
2051
- file_free(struct file *file)
2052
- {
2053
- struct heap_data *heap, *next_heap;
2054
-
2055
- heap = file->xattr.first;
2056
- while (heap != NULL) {
2057
- next_heap = heap->next;
2058
- free(heap);
2059
- heap = next_heap;
2060
- }
2061
- archive_string_free(&(file->parentdir));
2062
- archive_string_free(&(file->basename));
2063
- archive_string_free(&(file->symlink));
2064
- archive_string_free(&(file->script));
2065
- archive_entry_free(file->entry);
2066
- free(file);
2067
- }
2068
-
2069
- static struct file *
2070
- file_create_virtual_dir(struct archive_write *a, struct xar *xar,
2071
- const char *pathname)
2072
- {
2073
- struct file *file;
2074
-
2075
- (void)xar; /* UNUSED */
2076
-
2077
- file = file_new(a, NULL);
2078
- if (file == NULL)
2079
- return (NULL);
2080
- archive_entry_set_pathname(file->entry, pathname);
2081
- archive_entry_set_mode(file->entry, 0555 | AE_IFDIR);
2082
-
2083
- file->dir = 1;
2084
- file->virtual = 1;
2085
-
2086
- return (file);
2087
- }
2088
-
2089
- static int
2090
- file_add_child_tail(struct file *parent, struct file *child)
2091
- {
2092
- if (!__archive_rb_tree_insert_node(
2093
- &(parent->rbtree), (struct archive_rb_node *)child))
2094
- return (0);
2095
- child->chnext = NULL;
2096
- *parent->children.last = child;
2097
- parent->children.last = &(child->chnext);
2098
- child->parent = parent;
2099
- return (1);
2100
- }
2101
-
2102
- /*
2103
- * Find a entry from `parent'
2104
- */
2105
- static struct file *
2106
- file_find_child(struct file *parent, const char *child_name)
2107
- {
2108
- struct file *np;
2109
-
2110
- np = (struct file *)__archive_rb_tree_find_node(
2111
- &(parent->rbtree), child_name);
2112
- return (np);
2113
- }
2114
-
2115
- #if defined(_WIN32) || defined(__CYGWIN__)
2116
- static void
2117
- cleanup_backslash(char *utf8, size_t len)
2118
- {
2119
-
2120
- /* Convert a path-separator from '\' to '/' */
2121
- while (*utf8 != '\0' && len) {
2122
- if (*utf8 == '\\')
2123
- *utf8 = '/';
2124
- ++utf8;
2125
- --len;
2126
- }
2127
- }
2128
- #else
2129
- #define cleanup_backslash(p, len) /* nop */
2130
- #endif
2131
-
2132
- /*
2133
- * Generate a parent directory name and a base name from a pathname.
2134
- */
2135
- static int
2136
- file_gen_utility_names(struct archive_write *a, struct file *file)
2137
- {
2138
- struct xar *xar = a->format_data;
2139
- const char *pp;
2140
- char *p, *dirname, *slash;
2141
- size_t len;
2142
- int r = ARCHIVE_OK;
2143
-
2144
- archive_string_empty(&(file->parentdir));
2145
- archive_string_empty(&(file->basename));
2146
- archive_string_empty(&(file->symlink));
2147
-
2148
- if (file->parent == file)/* virtual root */
2149
- return (ARCHIVE_OK);
2150
-
2151
- if (archive_entry_pathname_l(file->entry, &pp, &len, xar->sconv)
2152
- != 0) {
2153
- if (errno == ENOMEM) {
2154
- archive_set_error(&a->archive, ENOMEM,
2155
- "Can't allocate memory for Pathname");
2156
- return (ARCHIVE_FATAL);
2157
- }
2158
- archive_set_error(&a->archive,
2159
- ARCHIVE_ERRNO_FILE_FORMAT,
2160
- "Can't translate pathname '%s' to UTF-8",
2161
- archive_entry_pathname(file->entry));
2162
- r = ARCHIVE_WARN;
2163
- }
2164
- archive_strncpy(&(file->parentdir), pp, len);
2165
- len = file->parentdir.length;
2166
- p = dirname = file->parentdir.s;
2167
- /*
2168
- * Convert a path-separator from '\' to '/'
2169
- */
2170
- cleanup_backslash(p, len);
2171
-
2172
- /*
2173
- * Remove leading '/', '../' and './' elements
2174
- */
2175
- while (*p) {
2176
- if (p[0] == '/') {
2177
- p++;
2178
- len--;
2179
- } else if (p[0] != '.')
2180
- break;
2181
- else if (p[1] == '.' && p[2] == '/') {
2182
- p += 3;
2183
- len -= 3;
2184
- } else if (p[1] == '/' || (p[1] == '.' && p[2] == '\0')) {
2185
- p += 2;
2186
- len -= 2;
2187
- } else if (p[1] == '\0') {
2188
- p++;
2189
- len--;
2190
- } else
2191
- break;
2192
- }
2193
- if (p != dirname) {
2194
- memmove(dirname, p, len+1);
2195
- p = dirname;
2196
- }
2197
- /*
2198
- * Remove "/","/." and "/.." elements from tail.
2199
- */
2200
- while (len > 0) {
2201
- size_t ll = len;
2202
-
2203
- if (p[len-1] == '/') {
2204
- p[len-1] = '\0';
2205
- len--;
2206
- }
2207
- if (len > 1 && p[len-2] == '/' && p[len-1] == '.') {
2208
- p[len-2] = '\0';
2209
- len -= 2;
2210
- }
2211
- if (len > 2 && p[len-3] == '/' && p[len-2] == '.' &&
2212
- p[len-1] == '.') {
2213
- p[len-3] = '\0';
2214
- len -= 3;
2215
- }
2216
- if (ll == len)
2217
- break;
2218
- }
2219
- while (*p) {
2220
- if (p[0] == '/') {
2221
- if (p[1] == '/')
2222
- /* Convert '//' --> '/' */
2223
- memmove(p, p+1, strlen(p+1) + 1);
2224
- else if (p[1] == '.' && p[2] == '/')
2225
- /* Convert '/./' --> '/' */
2226
- memmove(p, p+2, strlen(p+2) + 1);
2227
- else if (p[1] == '.' && p[2] == '.' && p[3] == '/') {
2228
- /* Convert 'dir/dir1/../dir2/'
2229
- * --> 'dir/dir2/'
2230
- */
2231
- char *rp = p -1;
2232
- size_t off;
2233
- for (off = 4; p[off] == '/'; off++)
2234
- ;
2235
- while (rp >= dirname) {
2236
- if (*rp == '/')
2237
- break;
2238
- --rp;
2239
- }
2240
- if (rp > dirname) {
2241
- memmove(rp + 1, p + off, strlen(p + off) + 1);
2242
- p = rp;
2243
- } else {
2244
- memmove(dirname, p + off, strlen(p + off) + 1);
2245
- p = dirname;
2246
- }
2247
- } else
2248
- p++;
2249
- } else if (p == dirname && p[0] == '.' && p[1] == '.' && p[2] == '/') {
2250
- size_t off;
2251
- for (off = 3; p[off] == '/'; off++)
2252
- ;
2253
- memmove(dirname, p + off, strlen(p + off) + 1);
2254
- p = dirname;
2255
- } else
2256
- p++;
2257
- }
2258
- p = dirname;
2259
- len = strlen(p);
2260
-
2261
- if (archive_entry_filetype(file->entry) == AE_IFLNK) {
2262
- size_t len2;
2263
- /* Convert symlink name too. */
2264
- if (archive_entry_symlink_l(file->entry, &pp, &len2,
2265
- xar->sconv) != 0) {
2266
- if (errno == ENOMEM) {
2267
- archive_set_error(&a->archive, ENOMEM,
2268
- "Can't allocate memory for Linkname");
2269
- return (ARCHIVE_FATAL);
2270
- }
2271
- archive_set_error(&a->archive,
2272
- ARCHIVE_ERRNO_FILE_FORMAT,
2273
- "Can't translate symlink '%s' to UTF-8",
2274
- archive_entry_symlink(file->entry));
2275
- r = ARCHIVE_WARN;
2276
- }
2277
- archive_strncpy(&(file->symlink), pp, len2);
2278
- cleanup_backslash(file->symlink.s, file->symlink.length);
2279
- }
2280
- /*
2281
- * - Count up directory elements.
2282
- * - Find out the position which points the last position of
2283
- * path separator('/').
2284
- */
2285
- slash = NULL;
2286
- for (; *p != '\0'; p++)
2287
- if (*p == '/')
2288
- slash = p;
2289
- if (slash == NULL) {
2290
- /* The pathname doesn't have a parent directory. */
2291
- file->parentdir.length = len;
2292
- archive_string_copy(&(file->basename), &(file->parentdir));
2293
- archive_string_empty(&(file->parentdir));
2294
- *file->parentdir.s = '\0';
2295
- return (r);
2296
- }
2297
-
2298
- /* Make a basename from dirname and slash */
2299
- *slash = '\0';
2300
- file->parentdir.length = slash - dirname;
2301
- archive_strcpy(&(file->basename), slash + 1);
2302
- return (r);
2303
- }
2304
-
2305
- static int
2306
- get_path_component(char *name, int n, const char *fn)
2307
- {
2308
- const char *p;
2309
- int l;
2310
-
2311
- p = strchr(fn, '/');
2312
- if (p == NULL) {
2313
- if ((l = (int)strlen(fn)) == 0)
2314
- return (0);
2315
- } else
2316
- l = (int)(p - fn);
2317
- if (l > n -1)
2318
- return (-1);
2319
- memcpy(name, fn, l);
2320
- name[l] = '\0';
2321
-
2322
- return (l);
2323
- }
2324
-
2325
- /*
2326
- * Add a new entry into the tree.
2327
- */
2328
- static int
2329
- file_tree(struct archive_write *a, struct file **filepp)
2330
- {
2331
- struct xar *xar = a->format_data;
2332
- #if defined(_WIN32) && !defined(__CYGWIN__)
2333
- char name[_MAX_FNAME];/* Included null terminator size. */
2334
- #elif defined(NAME_MAX) && NAME_MAX >= 255
2335
- char name[NAME_MAX+1];
2336
- #else
2337
- char name[256];
2338
- #endif
2339
- struct file *dent, *file, *np;
2340
- struct archive_entry *ent;
2341
- const char *fn, *p;
2342
- int l;
2343
-
2344
- file = *filepp;
2345
- dent = xar->root;
2346
- if (file->parentdir.length > 0)
2347
- fn = p = file->parentdir.s;
2348
- else
2349
- fn = p = "";
2350
-
2351
- /*
2352
- * If the path of the parent directory of `file' entry is
2353
- * the same as the path of `cur_dirent', add isoent to
2354
- * `cur_dirent'.
2355
- */
2356
- if (archive_strlen(&(xar->cur_dirstr))
2357
- == archive_strlen(&(file->parentdir)) &&
2358
- strcmp(xar->cur_dirstr.s, fn) == 0) {
2359
- if (!file_add_child_tail(xar->cur_dirent, file)) {
2360
- np = (struct file *)__archive_rb_tree_find_node(
2361
- &(xar->cur_dirent->rbtree),
2362
- file->basename.s);
2363
- goto same_entry;
2364
- }
2365
- return (ARCHIVE_OK);
2366
- }
2367
-
2368
- for (;;) {
2369
- l = get_path_component(name, sizeof(name), fn);
2370
- if (l == 0) {
2371
- np = NULL;
2372
- break;
2373
- }
2374
- if (l < 0) {
2375
- archive_set_error(&a->archive,
2376
- ARCHIVE_ERRNO_MISC,
2377
- "A name buffer is too small");
2378
- file_free(file);
2379
- *filepp = NULL;
2380
- return (ARCHIVE_FATAL);
2381
- }
2382
-
2383
- np = file_find_child(dent, name);
2384
- if (np == NULL || fn[0] == '\0')
2385
- break;
2386
-
2387
- /* Find next subdirectory. */
2388
- if (!np->dir) {
2389
- /* NOT Directory! */
2390
- archive_set_error(&a->archive,
2391
- ARCHIVE_ERRNO_MISC,
2392
- "`%s' is not directory, we cannot insert `%s' ",
2393
- archive_entry_pathname(np->entry),
2394
- archive_entry_pathname(file->entry));
2395
- file_free(file);
2396
- *filepp = NULL;
2397
- return (ARCHIVE_FAILED);
2398
- }
2399
- fn += l;
2400
- if (fn[0] == '/')
2401
- fn++;
2402
- dent = np;
2403
- }
2404
- if (np == NULL) {
2405
- /*
2406
- * Create virtual parent directories.
2407
- */
2408
- while (fn[0] != '\0') {
2409
- struct file *vp;
2410
- struct archive_string as;
2411
-
2412
- archive_string_init(&as);
2413
- archive_strncat(&as, p, fn - p + l);
2414
- if (as.length > 0 && as.s[as.length-1] == '/') {
2415
- as.s[as.length-1] = '\0';
2416
- as.length--;
2417
- }
2418
- if (as.length == 0) {
2419
- archive_string_free(&as);
2420
- fn += strspn(fn, "/");
2421
- l = get_path_component(name, sizeof(name), fn);
2422
- if (l < 0) {
2423
- archive_set_error(&a->archive,
2424
- ARCHIVE_ERRNO_MISC,
2425
- "A name buffer is too small");
2426
- file_free(file);
2427
- *filepp = NULL;
2428
- return (ARCHIVE_FATAL);
2429
- }
2430
- continue;
2431
- }
2432
- vp = file_create_virtual_dir(a, xar, as.s);
2433
- if (vp == NULL) {
2434
- archive_string_free(&as);
2435
- archive_set_error(&a->archive, ENOMEM,
2436
- "Can't allocate memory");
2437
- file_free(file);
2438
- *filepp = NULL;
2439
- return (ARCHIVE_FATAL);
2440
- }
2441
- archive_string_free(&as);
2442
- if (file_gen_utility_names(a, vp) <= ARCHIVE_FAILED) {
2443
- file_free(vp);
2444
- file_free(file);
2445
- *filepp = NULL;
2446
- return (ARCHIVE_FATAL);
2447
- }
2448
- file_add_child_tail(dent, vp);
2449
- file_register(xar, vp);
2450
- np = vp;
2451
-
2452
- fn += l;
2453
- if (fn[0] == '/')
2454
- fn++;
2455
- l = get_path_component(name, sizeof(name), fn);
2456
- if (l < 0) {
2457
- archive_string_free(&as);
2458
- archive_set_error(&a->archive,
2459
- ARCHIVE_ERRNO_MISC,
2460
- "A name buffer is too small");
2461
- file_free(file);
2462
- *filepp = NULL;
2463
- return (ARCHIVE_FATAL);
2464
- }
2465
- dent = np;
2466
- }
2467
-
2468
- /* Found out the parent directory where isoent can be
2469
- * inserted. */
2470
- xar->cur_dirent = dent;
2471
- archive_string_empty(&(xar->cur_dirstr));
2472
- archive_string_ensure(&(xar->cur_dirstr),
2473
- archive_strlen(&(dent->parentdir)) +
2474
- archive_strlen(&(dent->basename)) + 2);
2475
- if (archive_strlen(&(dent->parentdir)) +
2476
- archive_strlen(&(dent->basename)) == 0)
2477
- xar->cur_dirstr.s[0] = 0;
2478
- else {
2479
- if (archive_strlen(&(dent->parentdir)) > 0) {
2480
- archive_string_copy(&(xar->cur_dirstr),
2481
- &(dent->parentdir));
2482
- archive_strappend_char(&(xar->cur_dirstr), '/');
2483
- }
2484
- archive_string_concat(&(xar->cur_dirstr),
2485
- &(dent->basename));
2486
- }
2487
-
2488
- if (!file_add_child_tail(dent, file)) {
2489
- np = (struct file *)__archive_rb_tree_find_node(
2490
- &(dent->rbtree), file->basename.s);
2491
- goto same_entry;
2492
- }
2493
- return (ARCHIVE_OK);
2494
- }
2495
-
2496
- same_entry:
2497
- /*
2498
- * We have already has the entry the filename of which is
2499
- * the same.
2500
- */
2501
- if (archive_entry_filetype(np->entry) !=
2502
- archive_entry_filetype(file->entry)) {
2503
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2504
- "Found duplicate entries `%s' and its file type is "
2505
- "different",
2506
- archive_entry_pathname(np->entry));
2507
- file_free(file);
2508
- *filepp = NULL;
2509
- return (ARCHIVE_FAILED);
2510
- }
2511
-
2512
- /* Swap files. */
2513
- ent = np->entry;
2514
- np->entry = file->entry;
2515
- file->entry = ent;
2516
- np->virtual = 0;
2517
-
2518
- file_free(file);
2519
- *filepp = np;
2520
- return (ARCHIVE_OK);
2521
- }
2522
-
2523
- static void
2524
- file_register(struct xar *xar, struct file *file)
2525
- {
2526
- file->id = xar->file_idx++;
2527
- file->next = NULL;
2528
- *xar->file_list.last = file;
2529
- xar->file_list.last = &(file->next);
2530
- }
2531
-
2532
- static void
2533
- file_init_register(struct xar *xar)
2534
- {
2535
- xar->file_list.first = NULL;
2536
- xar->file_list.last = &(xar->file_list.first);
2537
- }
2538
-
2539
- static void
2540
- file_free_register(struct xar *xar)
2541
- {
2542
- struct file *file, *file_next;
2543
-
2544
- file = xar->file_list.first;
2545
- while (file != NULL) {
2546
- file_next = file->next;
2547
- file_free(file);
2548
- file = file_next;
2549
- }
2550
- }
2551
-
2552
- /*
2553
- * Register entry to get a hardlink target.
2554
- */
2555
- static int
2556
- file_register_hardlink(struct archive_write *a, struct file *file)
2557
- {
2558
- struct xar *xar = a->format_data;
2559
- struct hardlink *hl;
2560
- const char *pathname;
2561
-
2562
- archive_entry_set_nlink(file->entry, 1);
2563
- pathname = archive_entry_hardlink(file->entry);
2564
- if (pathname == NULL) {
2565
- /* This `file` is a hardlink target. */
2566
- hl = malloc(sizeof(*hl));
2567
- if (hl == NULL) {
2568
- archive_set_error(&a->archive, ENOMEM,
2569
- "Can't allocate memory");
2570
- return (ARCHIVE_FATAL);
2571
- }
2572
- hl->nlink = 1;
2573
- /* A hardlink target must be the first position. */
2574
- file->hlnext = NULL;
2575
- hl->file_list.first = file;
2576
- hl->file_list.last = &(file->hlnext);
2577
- __archive_rb_tree_insert_node(&(xar->hardlink_rbtree),
2578
- (struct archive_rb_node *)hl);
2579
- } else {
2580
- hl = (struct hardlink *)__archive_rb_tree_find_node(
2581
- &(xar->hardlink_rbtree), pathname);
2582
- if (hl != NULL) {
2583
- /* Insert `file` entry into the tail. */
2584
- file->hlnext = NULL;
2585
- *hl->file_list.last = file;
2586
- hl->file_list.last = &(file->hlnext);
2587
- hl->nlink++;
2588
- }
2589
- archive_entry_unset_size(file->entry);
2590
- }
2591
-
2592
- return (ARCHIVE_OK);
2593
- }
2594
-
2595
- /*
2596
- * Hardlinked files have to have the same location of extent.
2597
- * We have to find out hardlink target entries for entries which
2598
- * have a hardlink target name.
2599
- */
2600
- static void
2601
- file_connect_hardlink_files(struct xar *xar)
2602
- {
2603
- struct archive_rb_node *n;
2604
- struct hardlink *hl;
2605
- struct file *target, *nf;
2606
-
2607
- ARCHIVE_RB_TREE_FOREACH(n, &(xar->hardlink_rbtree)) {
2608
- hl = (struct hardlink *)n;
2609
-
2610
- /* The first entry must be a hardlink target. */
2611
- target = hl->file_list.first;
2612
- archive_entry_set_nlink(target->entry, hl->nlink);
2613
- if (hl->nlink > 1)
2614
- /* It means this file is a hardlink
2615
- * target itself. */
2616
- target->hardlink_target = target;
2617
- for (nf = target->hlnext;
2618
- nf != NULL; nf = nf->hlnext) {
2619
- nf->hardlink_target = target;
2620
- archive_entry_set_nlink(nf->entry, hl->nlink);
2621
- }
2622
- }
2623
- }
2624
-
2625
- static int
2626
- file_hd_cmp_node(const struct archive_rb_node *n1,
2627
- const struct archive_rb_node *n2)
2628
- {
2629
- const struct hardlink *h1 = (const struct hardlink *)n1;
2630
- const struct hardlink *h2 = (const struct hardlink *)n2;
2631
-
2632
- return (strcmp(archive_entry_pathname(h1->file_list.first->entry),
2633
- archive_entry_pathname(h2->file_list.first->entry)));
2634
- }
2635
-
2636
- static int
2637
- file_hd_cmp_key(const struct archive_rb_node *n, const void *key)
2638
- {
2639
- const struct hardlink *h = (const struct hardlink *)n;
2640
-
2641
- return (strcmp(archive_entry_pathname(h->file_list.first->entry),
2642
- (const char *)key));
2643
- }
2644
-
2645
-
2646
- static void
2647
- file_init_hardlinks(struct xar *xar)
2648
- {
2649
- static const struct archive_rb_tree_ops rb_ops = {
2650
- file_hd_cmp_node, file_hd_cmp_key,
2651
- };
2652
-
2653
- __archive_rb_tree_init(&(xar->hardlink_rbtree), &rb_ops);
2654
- }
2655
-
2656
- static void
2657
- file_free_hardlinks(struct xar *xar)
2658
- {
2659
- struct archive_rb_node *n, *tmp;
2660
-
2661
- ARCHIVE_RB_TREE_FOREACH_SAFE(n, &(xar->hardlink_rbtree), tmp) {
2662
- __archive_rb_tree_remove_node(&(xar->hardlink_rbtree), n);
2663
- free(n);
2664
- }
2665
- }
2666
-
2667
- static void
2668
- checksum_init(struct chksumwork *sumwrk, enum sumalg sum_alg)
2669
- {
2670
- sumwrk->alg = sum_alg;
2671
- switch (sum_alg) {
2672
- case CKSUM_NONE:
2673
- break;
2674
- case CKSUM_SHA1:
2675
- archive_sha1_init(&(sumwrk->sha1ctx));
2676
- break;
2677
- case CKSUM_MD5:
2678
- archive_md5_init(&(sumwrk->md5ctx));
2679
- break;
2680
- #ifdef ARCHIVE_HAS_SHA256
2681
- case CKSUM_SHA256:
2682
- archive_sha256_init(&(sumwrk->sha256ctx));
2683
- break;
2684
- #endif
2685
- #ifdef ARCHIVE_HAS_SHA512
2686
- case CKSUM_SHA512:
2687
- archive_sha512_init(&(sumwrk->sha512ctx));
2688
- break;
2689
- #endif
2690
- }
2691
- }
2692
-
2693
- static void
2694
- checksum_update(struct chksumwork *sumwrk, const void *buff, size_t size)
2695
- {
2696
-
2697
- switch (sumwrk->alg) {
2698
- case CKSUM_NONE:
2699
- break;
2700
- case CKSUM_SHA1:
2701
- archive_sha1_update(&(sumwrk->sha1ctx), buff, size);
2702
- break;
2703
- case CKSUM_MD5:
2704
- archive_md5_update(&(sumwrk->md5ctx), buff, size);
2705
- break;
2706
- #ifdef ARCHIVE_HAS_SHA256
2707
- case CKSUM_SHA256:
2708
- archive_sha256_update(&(sumwrk->sha256ctx), buff, size);
2709
- break;
2710
- #endif
2711
- #ifdef ARCHIVE_HAS_SHA512
2712
- case CKSUM_SHA512:
2713
- archive_sha512_update(&(sumwrk->sha512ctx), buff, size);
2714
- break;
2715
- #endif
2716
- }
2717
- }
2718
-
2719
- static void
2720
- checksum_final(struct chksumwork *sumwrk, struct chksumval *sumval)
2721
- {
2722
-
2723
- switch (sumwrk->alg) {
2724
- case CKSUM_NONE:
2725
- sumval->len = 0;
2726
- break;
2727
- case CKSUM_SHA1:
2728
- archive_sha1_final(&(sumwrk->sha1ctx), sumval->val);
2729
- sumval->len = SHA1_SIZE;
2730
- break;
2731
- case CKSUM_MD5:
2732
- archive_md5_final(&(sumwrk->md5ctx), sumval->val);
2733
- sumval->len = MD5_SIZE;
2734
- break;
2735
- #ifdef ARCHIVE_HAS_SHA256
2736
- case CKSUM_SHA256:
2737
- archive_sha256_final(&(sumwrk->sha256ctx), sumval->val);
2738
- sumval->len = SHA256_SIZE;
2739
- break;
2740
- #endif
2741
- #ifdef ARCHIVE_HAS_SHA512
2742
- case CKSUM_SHA512:
2743
- archive_sha512_final(&(sumwrk->sha512ctx), sumval->val);
2744
- sumval->len = SHA512_SIZE;
2745
- break;
2746
- #endif
2747
- }
2748
- sumval->alg = sumwrk->alg;
2749
- }
2750
-
2751
- #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR) || !defined(HAVE_LZMA_H)
2752
- static int
2753
- compression_unsupported_encoder(struct archive *a,
2754
- struct la_zstream *lastrm, const char *name)
2755
- {
2756
-
2757
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
2758
- "%s compression not supported on this platform", name);
2759
- lastrm->valid = 0;
2760
- lastrm->real_stream = NULL;
2761
- return (ARCHIVE_FAILED);
2762
- }
2763
- #endif
2764
-
2765
- static int
2766
- compression_init_encoder_gzip(struct archive *a,
2767
- struct la_zstream *lastrm, int level, int withheader)
2768
- {
2769
- z_stream *strm;
2770
-
2771
- if (lastrm->valid)
2772
- compression_end(a, lastrm);
2773
- strm = calloc(1, sizeof(*strm));
2774
- if (strm == NULL) {
2775
- archive_set_error(a, ENOMEM,
2776
- "Can't allocate memory for gzip stream");
2777
- return (ARCHIVE_FATAL);
2778
- }
2779
- /* zlib.h is not const-correct, so we need this one bit
2780
- * of ugly hackery to convert a const * pointer to
2781
- * a non-const pointer. */
2782
- strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
2783
- strm->avail_in = (uInt)lastrm->avail_in;
2784
- strm->total_in = (uLong)lastrm->total_in;
2785
- strm->next_out = lastrm->next_out;
2786
- strm->avail_out = (uInt)lastrm->avail_out;
2787
- strm->total_out = (uLong)lastrm->total_out;
2788
- if (deflateInit2(strm, level, Z_DEFLATED,
2789
- (withheader)?15:-15,
2790
- 8, Z_DEFAULT_STRATEGY) != Z_OK) {
2791
- free(strm);
2792
- lastrm->real_stream = NULL;
2793
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
2794
- "Internal error initializing compression library");
2795
- return (ARCHIVE_FATAL);
2796
- }
2797
- lastrm->real_stream = strm;
2798
- lastrm->valid = 1;
2799
- lastrm->code = compression_code_gzip;
2800
- lastrm->end = compression_end_gzip;
2801
- return (ARCHIVE_OK);
2802
- }
2803
-
2804
- static int
2805
- compression_code_gzip(struct archive *a,
2806
- struct la_zstream *lastrm, enum la_zaction action)
2807
- {
2808
- z_stream *strm;
2809
- int r;
2810
-
2811
- strm = (z_stream *)lastrm->real_stream;
2812
- /* zlib.h is not const-correct, so we need this one bit
2813
- * of ugly hackery to convert a const * pointer to
2814
- * a non-const pointer. */
2815
- strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
2816
- strm->avail_in = (uInt)lastrm->avail_in;
2817
- strm->total_in = (uLong)lastrm->total_in;
2818
- strm->next_out = lastrm->next_out;
2819
- strm->avail_out = (uInt)lastrm->avail_out;
2820
- strm->total_out = (uLong)lastrm->total_out;
2821
- r = deflate(strm,
2822
- (action == ARCHIVE_Z_FINISH)? Z_FINISH: Z_NO_FLUSH);
2823
- lastrm->next_in = strm->next_in;
2824
- lastrm->avail_in = strm->avail_in;
2825
- lastrm->total_in = strm->total_in;
2826
- lastrm->next_out = strm->next_out;
2827
- lastrm->avail_out = strm->avail_out;
2828
- lastrm->total_out = strm->total_out;
2829
- switch (r) {
2830
- case Z_OK:
2831
- return (ARCHIVE_OK);
2832
- case Z_STREAM_END:
2833
- return (ARCHIVE_EOF);
2834
- default:
2835
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
2836
- "GZip compression failed:"
2837
- " deflate() call returned status %d", r);
2838
- return (ARCHIVE_FATAL);
2839
- }
2840
- }
2841
-
2842
- static int
2843
- compression_end_gzip(struct archive *a, struct la_zstream *lastrm)
2844
- {
2845
- z_stream *strm;
2846
- int r;
2847
-
2848
- strm = (z_stream *)lastrm->real_stream;
2849
- r = deflateEnd(strm);
2850
- free(strm);
2851
- lastrm->real_stream = NULL;
2852
- lastrm->valid = 0;
2853
- if (r != Z_OK) {
2854
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
2855
- "Failed to clean up compressor");
2856
- return (ARCHIVE_FATAL);
2857
- }
2858
- return (ARCHIVE_OK);
2859
- }
2860
-
2861
- #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
2862
- static int
2863
- compression_init_encoder_bzip2(struct archive *a,
2864
- struct la_zstream *lastrm, int level)
2865
- {
2866
- bz_stream *strm;
2867
-
2868
- if (lastrm->valid)
2869
- compression_end(a, lastrm);
2870
- strm = calloc(1, sizeof(*strm));
2871
- if (strm == NULL) {
2872
- archive_set_error(a, ENOMEM,
2873
- "Can't allocate memory for bzip2 stream");
2874
- return (ARCHIVE_FATAL);
2875
- }
2876
- /* bzlib.h is not const-correct, so we need this one bit
2877
- * of ugly hackery to convert a const * pointer to
2878
- * a non-const pointer. */
2879
- strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
2880
- strm->avail_in = (unsigned int)lastrm->avail_in;
2881
- strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
2882
- strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
2883
- strm->next_out = (char *)lastrm->next_out;
2884
- strm->avail_out = (unsigned int)lastrm->avail_out;
2885
- strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
2886
- strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
2887
- if (BZ2_bzCompressInit(strm, level, 0, 30) != BZ_OK) {
2888
- free(strm);
2889
- lastrm->real_stream = NULL;
2890
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
2891
- "Internal error initializing compression library");
2892
- return (ARCHIVE_FATAL);
2893
- }
2894
- lastrm->real_stream = strm;
2895
- lastrm->valid = 1;
2896
- lastrm->code = compression_code_bzip2;
2897
- lastrm->end = compression_end_bzip2;
2898
- return (ARCHIVE_OK);
2899
- }
2900
-
2901
- static int
2902
- compression_code_bzip2(struct archive *a,
2903
- struct la_zstream *lastrm, enum la_zaction action)
2904
- {
2905
- bz_stream *strm;
2906
- int r;
2907
-
2908
- strm = (bz_stream *)lastrm->real_stream;
2909
- /* bzlib.h is not const-correct, so we need this one bit
2910
- * of ugly hackery to convert a const * pointer to
2911
- * a non-const pointer. */
2912
- strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
2913
- strm->avail_in = (unsigned int)lastrm->avail_in;
2914
- strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
2915
- strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
2916
- strm->next_out = (char *)lastrm->next_out;
2917
- strm->avail_out = (unsigned int)lastrm->avail_out;
2918
- strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
2919
- strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
2920
- r = BZ2_bzCompress(strm,
2921
- (action == ARCHIVE_Z_FINISH)? BZ_FINISH: BZ_RUN);
2922
- lastrm->next_in = (const unsigned char *)strm->next_in;
2923
- lastrm->avail_in = strm->avail_in;
2924
- lastrm->total_in =
2925
- (((uint64_t)(uint32_t)strm->total_in_hi32) << 32)
2926
- + (uint64_t)(uint32_t)strm->total_in_lo32;
2927
- lastrm->next_out = (unsigned char *)strm->next_out;
2928
- lastrm->avail_out = strm->avail_out;
2929
- lastrm->total_out =
2930
- (((uint64_t)(uint32_t)strm->total_out_hi32) << 32)
2931
- + (uint64_t)(uint32_t)strm->total_out_lo32;
2932
- switch (r) {
2933
- case BZ_RUN_OK: /* Non-finishing */
2934
- case BZ_FINISH_OK: /* Finishing: There's more work to do */
2935
- return (ARCHIVE_OK);
2936
- case BZ_STREAM_END: /* Finishing: all done */
2937
- /* Only occurs in finishing case */
2938
- return (ARCHIVE_EOF);
2939
- default:
2940
- /* Any other return value indicates an error */
2941
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
2942
- "Bzip2 compression failed:"
2943
- " BZ2_bzCompress() call returned status %d", r);
2944
- return (ARCHIVE_FATAL);
2945
- }
2946
- }
2947
-
2948
- static int
2949
- compression_end_bzip2(struct archive *a, struct la_zstream *lastrm)
2950
- {
2951
- bz_stream *strm;
2952
- int r;
2953
-
2954
- strm = (bz_stream *)lastrm->real_stream;
2955
- r = BZ2_bzCompressEnd(strm);
2956
- free(strm);
2957
- lastrm->real_stream = NULL;
2958
- lastrm->valid = 0;
2959
- if (r != BZ_OK) {
2960
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
2961
- "Failed to clean up compressor");
2962
- return (ARCHIVE_FATAL);
2963
- }
2964
- return (ARCHIVE_OK);
2965
- }
2966
-
2967
- #else
2968
- static int
2969
- compression_init_encoder_bzip2(struct archive *a,
2970
- struct la_zstream *lastrm, int level)
2971
- {
2972
-
2973
- (void) level; /* UNUSED */
2974
- if (lastrm->valid)
2975
- compression_end(a, lastrm);
2976
- return (compression_unsupported_encoder(a, lastrm, "bzip2"));
2977
- }
2978
- #endif
2979
-
2980
- #if defined(HAVE_LZMA_H)
2981
- static int
2982
- compression_init_encoder_lzma(struct archive *a,
2983
- struct la_zstream *lastrm, int level)
2984
- {
2985
- static const lzma_stream lzma_init_data = LZMA_STREAM_INIT;
2986
- lzma_stream *strm;
2987
- lzma_options_lzma lzma_opt;
2988
- int r;
2989
-
2990
- if (lastrm->valid)
2991
- compression_end(a, lastrm);
2992
- if (lzma_lzma_preset(&lzma_opt, level)) {
2993
- lastrm->real_stream = NULL;
2994
- archive_set_error(a, ENOMEM,
2995
- "Internal error initializing compression library");
2996
- return (ARCHIVE_FATAL);
2997
- }
2998
- strm = calloc(1, sizeof(*strm));
2999
- if (strm == NULL) {
3000
- archive_set_error(a, ENOMEM,
3001
- "Can't allocate memory for lzma stream");
3002
- return (ARCHIVE_FATAL);
3003
- }
3004
- *strm = lzma_init_data;
3005
- r = lzma_alone_encoder(strm, &lzma_opt);
3006
- switch (r) {
3007
- case LZMA_OK:
3008
- lastrm->real_stream = strm;
3009
- lastrm->valid = 1;
3010
- lastrm->code = compression_code_lzma;
3011
- lastrm->end = compression_end_lzma;
3012
- r = ARCHIVE_OK;
3013
- break;
3014
- case LZMA_MEM_ERROR:
3015
- free(strm);
3016
- lastrm->real_stream = NULL;
3017
- archive_set_error(a, ENOMEM,
3018
- "Internal error initializing compression library: "
3019
- "Cannot allocate memory");
3020
- r = ARCHIVE_FATAL;
3021
- break;
3022
- default:
3023
- free(strm);
3024
- lastrm->real_stream = NULL;
3025
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
3026
- "Internal error initializing compression library: "
3027
- "It's a bug in liblzma");
3028
- r = ARCHIVE_FATAL;
3029
- break;
3030
- }
3031
- return (r);
3032
- }
3033
-
3034
- static int
3035
- compression_init_encoder_xz(struct archive *a,
3036
- struct la_zstream *lastrm, int level, int threads)
3037
- {
3038
- static const lzma_stream lzma_init_data = LZMA_STREAM_INIT;
3039
- lzma_stream *strm;
3040
- lzma_filter *lzmafilters;
3041
- lzma_options_lzma lzma_opt;
3042
- int r;
3043
- #ifdef HAVE_LZMA_STREAM_ENCODER_MT
3044
- lzma_mt mt_options;
3045
- #endif
3046
-
3047
- (void)threads; /* UNUSED (if multi-threaded LZMA library not avail) */
3048
-
3049
- if (lastrm->valid)
3050
- compression_end(a, lastrm);
3051
- strm = calloc(1, sizeof(*strm) + sizeof(*lzmafilters) * 2);
3052
- if (strm == NULL) {
3053
- archive_set_error(a, ENOMEM,
3054
- "Can't allocate memory for xz stream");
3055
- return (ARCHIVE_FATAL);
3056
- }
3057
- lzmafilters = (lzma_filter *)(strm+1);
3058
- if (level > 9)
3059
- level = 9;
3060
- if (lzma_lzma_preset(&lzma_opt, level)) {
3061
- free(strm);
3062
- lastrm->real_stream = NULL;
3063
- archive_set_error(a, ENOMEM,
3064
- "Internal error initializing compression library");
3065
- return (ARCHIVE_FATAL);
3066
- }
3067
- lzmafilters[0].id = LZMA_FILTER_LZMA2;
3068
- lzmafilters[0].options = &lzma_opt;
3069
- lzmafilters[1].id = LZMA_VLI_UNKNOWN;/* Terminate */
3070
-
3071
- *strm = lzma_init_data;
3072
- #ifdef HAVE_LZMA_STREAM_ENCODER_MT
3073
- if (threads > 1) {
3074
- memset(&mt_options, 0, sizeof(mt_options));
3075
- mt_options.threads = threads;
3076
- mt_options.timeout = 300;
3077
- mt_options.filters = lzmafilters;
3078
- mt_options.check = LZMA_CHECK_CRC64;
3079
- r = lzma_stream_encoder_mt(strm, &mt_options);
3080
- } else
3081
- #endif
3082
- r = lzma_stream_encoder(strm, lzmafilters, LZMA_CHECK_CRC64);
3083
- switch (r) {
3084
- case LZMA_OK:
3085
- lastrm->real_stream = strm;
3086
- lastrm->valid = 1;
3087
- lastrm->code = compression_code_lzma;
3088
- lastrm->end = compression_end_lzma;
3089
- r = ARCHIVE_OK;
3090
- break;
3091
- case LZMA_MEM_ERROR:
3092
- free(strm);
3093
- lastrm->real_stream = NULL;
3094
- archive_set_error(a, ENOMEM,
3095
- "Internal error initializing compression library: "
3096
- "Cannot allocate memory");
3097
- r = ARCHIVE_FATAL;
3098
- break;
3099
- default:
3100
- free(strm);
3101
- lastrm->real_stream = NULL;
3102
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
3103
- "Internal error initializing compression library: "
3104
- "It's a bug in liblzma");
3105
- r = ARCHIVE_FATAL;
3106
- break;
3107
- }
3108
- return (r);
3109
- }
3110
-
3111
- static int
3112
- compression_code_lzma(struct archive *a,
3113
- struct la_zstream *lastrm, enum la_zaction action)
3114
- {
3115
- lzma_stream *strm;
3116
- int r;
3117
-
3118
- strm = (lzma_stream *)lastrm->real_stream;
3119
- strm->next_in = lastrm->next_in;
3120
- strm->avail_in = lastrm->avail_in;
3121
- strm->total_in = lastrm->total_in;
3122
- strm->next_out = lastrm->next_out;
3123
- strm->avail_out = lastrm->avail_out;
3124
- strm->total_out = lastrm->total_out;
3125
- r = lzma_code(strm,
3126
- (action == ARCHIVE_Z_FINISH)? LZMA_FINISH: LZMA_RUN);
3127
- lastrm->next_in = strm->next_in;
3128
- lastrm->avail_in = strm->avail_in;
3129
- lastrm->total_in = strm->total_in;
3130
- lastrm->next_out = strm->next_out;
3131
- lastrm->avail_out = strm->avail_out;
3132
- lastrm->total_out = strm->total_out;
3133
- switch (r) {
3134
- case LZMA_OK:
3135
- /* Non-finishing case */
3136
- return (ARCHIVE_OK);
3137
- case LZMA_STREAM_END:
3138
- /* This return can only occur in finishing case. */
3139
- return (ARCHIVE_EOF);
3140
- case LZMA_MEMLIMIT_ERROR:
3141
- archive_set_error(a, ENOMEM,
3142
- "lzma compression error:"
3143
- " %ju MiB would have been needed",
3144
- (uintmax_t)((lzma_memusage(strm) + 1024 * 1024 -1)
3145
- / (1024 * 1024)));
3146
- return (ARCHIVE_FATAL);
3147
- default:
3148
- /* Any other return value indicates an error */
3149
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
3150
- "lzma compression failed:"
3151
- " lzma_code() call returned status %d", r);
3152
- return (ARCHIVE_FATAL);
3153
- }
3154
- }
3155
-
3156
- static int
3157
- compression_end_lzma(struct archive *a, struct la_zstream *lastrm)
3158
- {
3159
- lzma_stream *strm;
3160
-
3161
- (void)a; /* UNUSED */
3162
- strm = (lzma_stream *)lastrm->real_stream;
3163
- lzma_end(strm);
3164
- free(strm);
3165
- lastrm->valid = 0;
3166
- lastrm->real_stream = NULL;
3167
- return (ARCHIVE_OK);
3168
- }
3169
- #else
3170
- static int
3171
- compression_init_encoder_lzma(struct archive *a,
3172
- struct la_zstream *lastrm, int level)
3173
- {
3174
-
3175
- (void) level; /* UNUSED */
3176
- if (lastrm->valid)
3177
- compression_end(a, lastrm);
3178
- return (compression_unsupported_encoder(a, lastrm, "lzma"));
3179
- }
3180
- static int
3181
- compression_init_encoder_xz(struct archive *a,
3182
- struct la_zstream *lastrm, int level, int threads)
3183
- {
3184
-
3185
- (void) level; /* UNUSED */
3186
- (void) threads; /* UNUSED */
3187
- if (lastrm->valid)
3188
- compression_end(a, lastrm);
3189
- return (compression_unsupported_encoder(a, lastrm, "xz"));
3190
- }
3191
- #endif
3192
-
3193
- static int
3194
- xar_compression_init_encoder(struct archive_write *a)
3195
- {
3196
- struct xar *xar = a->format_data;
3197
- int r;
3198
-
3199
- switch (xar->opt_compression) {
3200
- case GZIP:
3201
- r = compression_init_encoder_gzip(
3202
- &(a->archive), &(xar->stream),
3203
- xar->opt_compression_level, 1);
3204
- break;
3205
- case BZIP2:
3206
- r = compression_init_encoder_bzip2(
3207
- &(a->archive), &(xar->stream),
3208
- xar->opt_compression_level);
3209
- break;
3210
- case LZMA:
3211
- r = compression_init_encoder_lzma(
3212
- &(a->archive), &(xar->stream),
3213
- xar->opt_compression_level);
3214
- break;
3215
- case XZ:
3216
- r = compression_init_encoder_xz(
3217
- &(a->archive), &(xar->stream),
3218
- xar->opt_compression_level, xar->opt_threads);
3219
- break;
3220
- default:
3221
- r = ARCHIVE_OK;
3222
- break;
3223
- }
3224
- if (r == ARCHIVE_OK) {
3225
- xar->stream.total_in = 0;
3226
- xar->stream.next_out = xar->wbuff;
3227
- xar->stream.avail_out = sizeof(xar->wbuff);
3228
- xar->stream.total_out = 0;
3229
- }
3230
-
3231
- return (r);
3232
- }
3233
-
3234
- static int
3235
- compression_code(struct archive *a, struct la_zstream *lastrm,
3236
- enum la_zaction action)
3237
- {
3238
- if (lastrm->valid)
3239
- return (lastrm->code(a, lastrm, action));
3240
- return (ARCHIVE_OK);
3241
- }
3242
-
3243
- static int
3244
- compression_end(struct archive *a, struct la_zstream *lastrm)
3245
- {
3246
- if (lastrm->valid)
3247
- return (lastrm->end(a, lastrm));
3248
- return (ARCHIVE_OK);
3249
- }
3250
-
3251
-
3252
- static int
3253
- save_xattrs(struct archive_write *a, struct file *file)
3254
- {
3255
- struct xar *xar = a->format_data;
3256
- const char *name;
3257
- const void *value;
3258
- struct heap_data *heap;
3259
- size_t size;
3260
- int count, r;
3261
-
3262
- count = archive_entry_xattr_reset(file->entry);
3263
- if (count == 0)
3264
- return (ARCHIVE_OK);
3265
- while (count--) {
3266
- archive_entry_xattr_next(file->entry,
3267
- &name, &value, &size);
3268
- checksum_init(&(xar->a_sumwrk), xar->opt_sumalg);
3269
- checksum_init(&(xar->e_sumwrk), xar->opt_sumalg);
3270
-
3271
- heap = calloc(1, sizeof(*heap));
3272
- if (heap == NULL) {
3273
- struct chksumval sumval;
3274
-
3275
- checksum_final(&(xar->a_sumwrk), &sumval);
3276
- checksum_final(&(xar->e_sumwrk), &sumval);
3277
- xar->a_sumwrk.alg = CKSUM_NONE;
3278
- xar->e_sumwrk.alg = CKSUM_NONE;
3279
- archive_set_error(&a->archive, ENOMEM,
3280
- "Can't allocate memory for xattr");
3281
- return (ARCHIVE_FATAL);
3282
- }
3283
- heap->id = file->ea_idx++;
3284
- heap->temp_offset = xar->temp_offset;
3285
- heap->size = size;/* save a extracted size */
3286
- heap->compression = xar->opt_compression;
3287
- /* Get a extracted sumcheck value. */
3288
- checksum_update(&(xar->e_sumwrk), value, size);
3289
- checksum_final(&(xar->e_sumwrk), &(heap->e_sum));
3290
-
3291
- /*
3292
- * Not compression to xattr is simple way.
3293
- */
3294
- if (heap->compression == NONE) {
3295
- checksum_update(&(xar->a_sumwrk), value, size);
3296
- checksum_final(&(xar->a_sumwrk), &(heap->a_sum));
3297
- if (write_to_temp(a, value, size)
3298
- != ARCHIVE_OK) {
3299
- free(heap);
3300
- return (ARCHIVE_FATAL);
3301
- }
3302
- heap->length = size;
3303
- /* Add heap to the tail of file->xattr. */
3304
- heap->next = NULL;
3305
- *file->xattr.last = heap;
3306
- file->xattr.last = &(heap->next);
3307
- /* Next xattr */
3308
- continue;
3309
- }
3310
-
3311
- /*
3312
- * Init compression library.
3313
- */
3314
- r = xar_compression_init_encoder(a);
3315
- if (r != ARCHIVE_OK) {
3316
- struct chksumval sumval;
3317
-
3318
- checksum_final(&(xar->a_sumwrk), &sumval);
3319
- xar->a_sumwrk.alg = CKSUM_NONE;
3320
- free(heap);
3321
- return (ARCHIVE_FATAL);
3322
- }
3323
-
3324
- xar->stream.next_in = (const unsigned char *)value;
3325
- xar->stream.avail_in = size;
3326
- for (;;) {
3327
- r = compression_code(&(a->archive),
3328
- &(xar->stream), ARCHIVE_Z_FINISH);
3329
- if (r != ARCHIVE_OK && r != ARCHIVE_EOF) {
3330
- free(heap);
3331
- return (ARCHIVE_FATAL);
3332
- }
3333
- size = sizeof(xar->wbuff) - xar->stream.avail_out;
3334
- checksum_update(&(xar->a_sumwrk),
3335
- xar->wbuff, size);
3336
- if (write_to_temp(a, xar->wbuff, size)
3337
- != ARCHIVE_OK) {
3338
- free(heap);
3339
- return (ARCHIVE_FATAL);
3340
- }
3341
- if (r == ARCHIVE_OK) {
3342
- xar->stream.next_out = xar->wbuff;
3343
- xar->stream.avail_out = sizeof(xar->wbuff);
3344
- } else {
3345
- checksum_final(&(xar->a_sumwrk),
3346
- &(heap->a_sum));
3347
- heap->length = xar->stream.total_out;
3348
- /* Add heap to the tail of file->xattr. */
3349
- heap->next = NULL;
3350
- *file->xattr.last = heap;
3351
- file->xattr.last = &(heap->next);
3352
- break;
3353
- }
3354
- }
3355
- /* Clean up compression library. */
3356
- r = compression_end(&(a->archive), &(xar->stream));
3357
- if (r != ARCHIVE_OK)
3358
- return (ARCHIVE_FATAL);
3359
- }
3360
- return (ARCHIVE_OK);
3361
- }
3362
-
3363
- static int
3364
- getalgsize(enum sumalg sumalg)
3365
- {
3366
- switch (sumalg) {
3367
- default:
3368
- case CKSUM_NONE:
3369
- return (0);
3370
- case CKSUM_SHA1:
3371
- return (SHA1_SIZE);
3372
- case CKSUM_MD5:
3373
- return (MD5_SIZE);
3374
- case CKSUM_SHA256:
3375
- return (SHA256_SIZE);
3376
- case CKSUM_SHA512:
3377
- return (SHA512_SIZE);
3378
- }
3379
- }
3380
-
3381
- static const char *
3382
- getalgname(enum sumalg sumalg)
3383
- {
3384
- switch (sumalg) {
3385
- default:
3386
- case CKSUM_NONE:
3387
- return (NULL);
3388
- case CKSUM_SHA1:
3389
- return (SHA1_NAME);
3390
- case CKSUM_MD5:
3391
- return (MD5_NAME);
3392
- case CKSUM_SHA256:
3393
- return (SHA256_NAME);
3394
- case CKSUM_SHA512:
3395
- return (SHA512_NAME);
3396
- }
3397
- }
3398
-
3399
- #if HAVE_LIBXML_XMLWRITER_H
3400
-
3401
- #define BAD_CAST_CONST (const xmlChar *)
3402
-
3403
- struct xml_writer {
3404
- xmlTextWriterPtr writer;
3405
- xmlBufferPtr bp;
3406
- unsigned int indent;
3407
- };
3408
-
3409
- static int
3410
- xml_writer_create(struct xml_writer **pctx)
3411
- {
3412
- struct xml_writer *ctx = calloc(1, sizeof(struct xml_writer));
3413
- if (ctx == NULL) {
3414
- return (-1);
3415
- }
3416
-
3417
- ctx->bp = xmlBufferCreate();
3418
- if (ctx->bp == NULL) {
3419
- free(ctx);
3420
- return (-1);
3421
- }
3422
-
3423
- ctx->writer = xmlNewTextWriterMemory(ctx->bp, 0);
3424
- if (ctx->writer == NULL) {
3425
- xmlBufferFree(ctx->bp);
3426
- free(ctx);
3427
- return (-1);
3428
- }
3429
-
3430
- *pctx = ctx;
3431
- return (0);
3432
- }
3433
-
3434
- static int
3435
- xml_writer_destroy(struct xml_writer *ctx)
3436
- {
3437
- xmlFreeTextWriter(ctx->writer);
3438
- xmlBufferFree(ctx->bp);
3439
- free(ctx);
3440
- return (0);
3441
- }
3442
-
3443
- static int
3444
- xml_writer_start_document(struct xml_writer *ctx)
3445
- {
3446
- int r;
3447
- r = xmlTextWriterStartDocument(ctx->writer, "1.0", "UTF-8", NULL);
3448
- if (r < 0) {
3449
- return (r);
3450
- }
3451
-
3452
- r = xmlTextWriterSetIndent(ctx->writer, (int)ctx->indent);
3453
- return (r);
3454
- }
3455
-
3456
- static int
3457
- xml_writer_end_document(struct xml_writer *ctx)
3458
- {
3459
- return (xmlTextWriterEndDocument(ctx->writer));
3460
- }
3461
-
3462
- static int
3463
- xml_writer_set_indent(struct xml_writer *ctx, unsigned int indent)
3464
- {
3465
- /* libxml2 only lets you set the indent after starting the document */
3466
- ctx->indent = indent;
3467
- return (0);
3468
- }
3469
-
3470
- static int
3471
- xml_writer_start_element(struct xml_writer *ctx, const char *localName)
3472
- {
3473
- return (xmlTextWriterStartElement(ctx->writer,
3474
- BAD_CAST_CONST(localName)));
3475
- }
3476
-
3477
- static int
3478
- xml_writer_write_attribute(struct xml_writer *ctx,
3479
- const char *key, const char *value)
3480
- {
3481
- return (xmlTextWriterWriteAttribute(ctx->writer,
3482
- BAD_CAST_CONST(key), BAD_CAST_CONST(value)));
3483
- }
3484
-
3485
- static int
3486
- xml_writer_write_attributef(struct xml_writer *ctx,
3487
- const char *key, const char *format, ...)
3488
- {
3489
- va_list ap;
3490
- int ret;
3491
- va_start(ap, format);
3492
- ret = xmlTextWriterWriteVFormatAttribute(ctx->writer,
3493
- BAD_CAST_CONST(key), format, ap);
3494
- va_end(ap);
3495
- return (ret);
3496
- }
3497
-
3498
- static int
3499
- xml_writer_write_string(struct xml_writer *ctx, const char *string)
3500
- {
3501
- return (xmlTextWriterWriteString(ctx->writer, BAD_CAST_CONST(string)));
3502
- }
3503
-
3504
- static int
3505
- xml_writer_write_base64(struct xml_writer* ctx,
3506
- const char *data, size_t start, size_t len)
3507
- {
3508
- return (xmlTextWriterWriteBase64(ctx->writer, data,
3509
- (int)start, (int)len));
3510
- }
3511
-
3512
- static int
3513
- xml_writer_end_element(struct xml_writer *ctx)
3514
- {
3515
- return (xmlTextWriterEndElement(ctx->writer));
3516
- }
3517
-
3518
- static int
3519
- xml_writer_get_final_content_and_length(struct xml_writer *ctx,
3520
- const char **out, size_t *size)
3521
- {
3522
- *out = (const char*)xmlBufferContent(ctx->bp);
3523
- *size = (size_t)xmlBufferLength(ctx->bp);
3524
- return (0);
3525
- }
3526
-
3527
- #elif HAVE_XMLLITE_H
3528
-
3529
- struct xml_writer {
3530
- IXmlWriter *writer;
3531
- IStream *stream;
3532
- HGLOBAL global;
3533
- };
3534
-
3535
- static int
3536
- xml_writer_create(struct xml_writer **pctx)
3537
- {
3538
- struct xml_writer *ctx;
3539
- HRESULT hr;
3540
-
3541
- ctx = calloc(1, sizeof(struct xml_writer));
3542
- if (ctx == NULL) {
3543
- return (E_OUTOFMEMORY);
3544
- }
3545
-
3546
- hr = CreateStreamOnHGlobal(NULL, TRUE, &ctx->stream);
3547
- if (FAILED(hr)) {
3548
- free(ctx);
3549
- return (hr);
3550
- }
3551
-
3552
- hr = CreateXmlWriter(&IID_IXmlWriter, (void **)&ctx->writer, NULL);
3553
- if (FAILED(hr)) {
3554
- ctx->stream->lpVtbl->Release(ctx->stream);
3555
- free(ctx);
3556
- return (hr);
3557
- }
3558
-
3559
- hr = ctx->writer->lpVtbl->SetOutput(ctx->writer,
3560
- (IUnknown *)ctx->stream);
3561
- if (FAILED(hr)) {
3562
- ctx->writer->lpVtbl->Release(ctx->writer);
3563
- ctx->stream->lpVtbl->Release(ctx->stream);
3564
- free(ctx);
3565
- return (hr);
3566
- }
3567
-
3568
- *pctx = ctx;
3569
- return (S_OK);
3570
- }
3571
-
3572
- static int
3573
- xml_writer_destroy(struct xml_writer *ctx)
3574
- {
3575
- if (ctx->global)
3576
- GlobalUnlock(ctx->global);
3577
- ctx->writer->lpVtbl->Release(ctx->writer); /* Destroys only writer */
3578
- ctx->stream->lpVtbl->Release(ctx->stream); /* Destroys stream, global */
3579
- free(ctx);
3580
- return (S_OK);
3581
- }
3582
-
3583
- static int
3584
- xml_writer_start_document(struct xml_writer *ctx)
3585
- {
3586
- return ctx->writer->lpVtbl->WriteStartDocument(ctx->writer,
3587
- XmlStandalone_Omit);
3588
- }
3589
-
3590
- static int
3591
- xml_writer_end_document(struct xml_writer *ctx)
3592
- {
3593
- return ctx->writer->lpVtbl->WriteEndDocument(ctx->writer);
3594
- }
3595
-
3596
- static int
3597
- xml_writer_set_indent(struct xml_writer *ctx, unsigned int indent)
3598
- {
3599
- /* Windows' xmllite does not support indent sizes; will always be 2 */
3600
- (void)indent;
3601
- return ctx->writer->lpVtbl->SetProperty(ctx->writer,
3602
- XmlWriterProperty_Indent, (LONG_PTR)TRUE);
3603
- }
3604
-
3605
- static int
3606
- xml_writer_start_element(struct xml_writer *ctx, const char *localName)
3607
- {
3608
- struct archive_wstring as;
3609
- HRESULT hr;
3610
- archive_string_init(&as);
3611
- if (archive_wstring_append_from_mbs(&as, localName,
3612
- strlen(localName))) {
3613
- hr = E_OUTOFMEMORY;
3614
- goto exit_hr;
3615
- }
3616
- hr = ctx->writer->lpVtbl->WriteStartElement(ctx->writer, NULL,
3617
- as.s, NULL);
3618
-
3619
- exit_hr:
3620
- archive_wstring_free(&as);
3621
- return hr;
3622
- }
3623
-
3624
- static int
3625
- xml_writer_write_attribute(struct xml_writer *ctx,
3626
- const char *key, const char *value)
3627
- {
3628
- struct archive_wstring ask, asv;
3629
- HRESULT hr;
3630
- archive_string_init(&ask);
3631
- archive_string_init(&asv);
3632
- if (archive_wstring_append_from_mbs(&ask, key, strlen(key))) {
3633
- hr = E_OUTOFMEMORY;
3634
- goto exit_hr;
3635
- }
3636
- if (archive_wstring_append_from_mbs(&asv, value, strlen(value))) {
3637
- hr = E_OUTOFMEMORY;
3638
- goto exit_hr;
3639
- }
3640
- hr = ctx->writer->lpVtbl->WriteAttributeString(ctx->writer, NULL,
3641
- ask.s, NULL, asv.s);
3642
-
3643
- exit_hr:
3644
- archive_wstring_free(&asv);
3645
- archive_wstring_free(&ask);
3646
- return hr;
3647
- }
3648
-
3649
- static int
3650
- xml_writer_write_attributef(struct xml_writer *ctx,
3651
- const char *key, const char *format, ...)
3652
- {
3653
- struct archive_wstring ask, asv;
3654
- struct archive_string asf;
3655
- HRESULT hr;
3656
- va_list ap;
3657
-
3658
- va_start(ap, format);
3659
- archive_string_init(&ask);
3660
- archive_string_init(&asv);
3661
- archive_string_init(&asf);
3662
-
3663
- if (archive_wstring_append_from_mbs(&ask, key, strlen(key))) {
3664
- hr = E_OUTOFMEMORY;
3665
- goto exit_hr;
3666
- }
3667
-
3668
- archive_string_vsprintf(&asf, format, ap);
3669
- if (archive_wstring_append_from_mbs(&asv, asf.s, asf.length)) {
3670
- hr = E_OUTOFMEMORY;
3671
- goto exit_hr;
3672
- }
3673
-
3674
- hr = ctx->writer->lpVtbl->WriteAttributeString(ctx->writer, NULL,
3675
- ask.s, NULL, asv.s);
3676
-
3677
- exit_hr:
3678
- archive_string_free(&asf);
3679
- archive_wstring_free(&asv);
3680
- archive_wstring_free(&ask);
3681
- va_end(ap);
3682
-
3683
- return hr;
3684
- }
3685
-
3686
- static int
3687
- xml_writer_write_string(struct xml_writer *ctx, const char *string)
3688
- {
3689
- struct archive_wstring as;
3690
- HRESULT hr;
3691
- archive_string_init(&as);
3692
- if (archive_wstring_append_from_mbs(&as, string, strlen(string))) {
3693
- hr = E_OUTOFMEMORY;
3694
- goto exit_hr;
3695
- }
3696
- hr = ctx->writer->lpVtbl->WriteString(ctx->writer, as.s);
3697
-
3698
- exit_hr:
3699
- archive_wstring_free(&as);
3700
- return hr;
3701
- }
3702
-
3703
- static const wchar_t base64[] = {
3704
- L'A', L'B', L'C', L'D', L'E', L'F', L'G', L'H',
3705
- L'I', L'J', L'K', L'L', L'M', L'N', L'O', L'P',
3706
- L'Q', L'R', L'S', L'T', L'U', L'V', L'W', L'X',
3707
- L'Y', L'Z', L'a', L'b', L'c', L'd', L'e', L'f',
3708
- L'g', L'h', L'i', L'j', L'k', L'l', L'm', L'n',
3709
- L'o', L'p', L'q', L'r', L's', L't', L'u', L'v',
3710
- L'w', L'x', L'y', L'z', L'0', L'1', L'2', L'3',
3711
- L'4', L'5', L'6', L'7', L'8', L'9', L'+', L'/'
3712
- };
3713
-
3714
- static void
3715
- la_b64_wencode(struct archive_wstring *as, const unsigned char *p, size_t len)
3716
- {
3717
- int c;
3718
-
3719
- for (; len >= 3; p += 3, len -= 3) {
3720
- c = p[0] >> 2;
3721
- archive_wstrappend_wchar(as, base64[c]);
3722
- c = ((p[0] & 0x03) << 4) | ((p[1] & 0xf0) >> 4);
3723
- archive_wstrappend_wchar(as, base64[c]);
3724
- c = ((p[1] & 0x0f) << 2) | ((p[2] & 0xc0) >> 6);
3725
- archive_wstrappend_wchar(as, base64[c]);
3726
- c = p[2] & 0x3f;
3727
- archive_wstrappend_wchar(as, base64[c]);
3728
- }
3729
- if (len > 0) {
3730
- c = p[0] >> 2;
3731
- archive_wstrappend_wchar(as, base64[c]);
3732
- c = (p[0] & 0x03) << 4;
3733
- if (len == 1) {
3734
- archive_wstrappend_wchar(as, base64[c]);
3735
- archive_wstrappend_wchar(as, '=');
3736
- archive_wstrappend_wchar(as, '=');
3737
- } else {
3738
- c |= (p[1] & 0xf0) >> 4;
3739
- archive_wstrappend_wchar(as, base64[c]);
3740
- c = (p[1] & 0x0f) << 2;
3741
- archive_wstrappend_wchar(as, base64[c]);
3742
- archive_wstrappend_wchar(as, '=');
3743
- }
3744
- }
3745
- }
3746
-
3747
- static int
3748
- xml_writer_write_base64(struct xml_writer* ctx,
3749
- const char *data, size_t start, size_t len)
3750
- {
3751
- struct archive_wstring as;
3752
- HRESULT hr;
3753
- archive_string_init(&as);
3754
- la_b64_wencode(&as, (const unsigned char *)data + start, len - start);
3755
- hr = ctx->writer->lpVtbl->WriteString(ctx->writer, as.s);
3756
- archive_wstring_free(&as);
3757
- return hr;
3758
- }
3759
-
3760
- static int
3761
- xml_writer_end_element(struct xml_writer *ctx)
3762
- {
3763
- return ctx->writer->lpVtbl->WriteEndElement(ctx->writer);
3764
- }
3765
-
3766
- static int
3767
- xml_writer_get_final_content_and_length(struct xml_writer *ctx,
3768
- const char **out, size_t *size)
3769
- {
3770
- HGLOBAL gbl;
3771
- HRESULT hr;
3772
-
3773
- hr = ctx->writer->lpVtbl->Flush(ctx->writer);
3774
- if (FAILED(hr)) {
3775
- return (hr);
3776
- }
3777
-
3778
- hr = GetHGlobalFromStream(ctx->stream, &gbl);
3779
- if (FAILED(hr)) {
3780
- return (hr);
3781
- }
3782
-
3783
- *out = (const char *)GlobalLock(gbl);
3784
- if (*out == NULL) {
3785
- hr = HRESULT_FROM_WIN32(GetLastError());
3786
- return (hr);
3787
- }
3788
-
3789
- /* GlobalUnlock is called in
3790
- * xml_writer_destroy.
3791
- */
3792
- *size = (size_t)GlobalSize(gbl);
3793
- ctx->global = gbl;
3794
- return (hr);
3795
- }
3796
-
3797
- #endif /* HAVE_LIBXML_XMLWRITER_H */
3798
-
3799
- #endif /* Support xar format */