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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (266) hide show
  1. package/android/build.gradle +1 -0
  2. package/android/src/main/cpp/CMakeLists.txt +137 -0
  3. package/android/src/main/cpp/PapyrusComicArchiveJni.cpp +68 -0
  4. package/android/src/main/cpp/PapyrusComicArchiveTest.cpp +55 -0
  5. package/android/src/main/java/com/papyrus/engine/PapyrusComicArchiveNative.java +14 -0
  6. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentView.java +353 -0
  7. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentViewManager.java +32 -0
  8. package/android/src/main/java/com/papyrus/engine/PapyrusComicPageLayout.java +17 -0
  9. package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +298 -34
  10. package/android/src/main/java/com/papyrus/engine/PapyrusNativeTextModel.java +195 -0
  11. package/android/src/main/java/com/papyrus/engine/PapyrusPackage.java +3 -1
  12. package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerView.java +24 -0
  13. package/android/src/main/java/com/papyrus/engine/PapyrusRenderMath.java +9 -0
  14. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentView.java +439 -0
  15. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentViewManager.java +43 -0
  16. package/android/src/main/java/com/papyrus/engine/PapyrusTextStore.java +43 -0
  17. package/dist/index.d.mts +439 -3
  18. package/dist/index.d.ts +439 -3
  19. package/dist/index.js +412 -4
  20. package/dist/index.js.map +1 -1
  21. package/dist/index.mjs +404 -3
  22. package/dist/index.mjs.map +1 -1
  23. package/ios/PapyrusComicDocumentView.h +21 -0
  24. package/ios/PapyrusComicDocumentView.m +374 -0
  25. package/ios/PapyrusComicDocumentViewManager.m +23 -0
  26. package/ios/PapyrusEngineStore.m +4 -0
  27. package/ios/PapyrusNativeEngine.m +664 -0
  28. package/ios/PapyrusNativeEngine.podspec +26 -1
  29. package/ios/PapyrusPageRotationRegistry.h +29 -0
  30. package/ios/PapyrusPageRotationRegistry.m +179 -0
  31. package/ios/PapyrusPageView.m +38 -11
  32. package/ios/PapyrusPdfDocumentView.h +2 -0
  33. package/ios/PapyrusPdfDocumentView.m +116 -9
  34. package/ios/PapyrusPdfDocumentViewManager.m +2 -0
  35. package/ios/PapyrusPdfPageTheme.h +15 -0
  36. package/ios/PapyrusPdfPageTheme.m +218 -0
  37. package/ios/PapyrusTextDocumentView.h +27 -0
  38. package/ios/PapyrusTextDocumentView.m +300 -0
  39. package/ios/PapyrusTextDocumentViewManager.m +29 -0
  40. package/ios/PapyrusTextStore.h +13 -0
  41. package/ios/PapyrusTextStore.m +73 -0
  42. package/package.json +5 -3
  43. package/third_party_licenses/BLAKE2-NOTICE.txt +11 -0
  44. package/third_party_licenses/libarchive-COPYING +65 -0
  45. package/vendor/libarchive/PapyrusComicArchive.cpp +376 -0
  46. package/vendor/libarchive/PapyrusComicArchive.h +30 -0
  47. package/vendor/libarchive/README.papyrus.md +15 -0
  48. package/vendor/libarchive/android.h +192 -0
  49. package/vendor/libarchive/config/PapyrusArchiveConfig.h +65 -0
  50. package/vendor/libarchive/libarchive/CMakeLists.txt +293 -0
  51. package/vendor/libarchive/libarchive/android_lf.h +36 -0
  52. package/vendor/libarchive/libarchive/archive.h +1266 -0
  53. package/vendor/libarchive/libarchive/archive_acl.c +2025 -0
  54. package/vendor/libarchive/libarchive/archive_acl_private.h +85 -0
  55. package/vendor/libarchive/libarchive/archive_blake2.h +197 -0
  56. package/vendor/libarchive/libarchive/archive_blake2_impl.h +161 -0
  57. package/vendor/libarchive/libarchive/archive_blake2s_ref.c +369 -0
  58. package/vendor/libarchive/libarchive/archive_blake2sp_ref.c +361 -0
  59. package/vendor/libarchive/libarchive/archive_check_magic.c +183 -0
  60. package/vendor/libarchive/libarchive/archive_cmdline.c +211 -0
  61. package/vendor/libarchive/libarchive/archive_cmdline_private.h +45 -0
  62. package/vendor/libarchive/libarchive/archive_crc32.h +123 -0
  63. package/vendor/libarchive/libarchive/archive_cryptor.c +534 -0
  64. package/vendor/libarchive/libarchive/archive_cryptor_private.h +199 -0
  65. package/vendor/libarchive/libarchive/archive_digest.c +1584 -0
  66. package/vendor/libarchive/libarchive/archive_digest_private.h +431 -0
  67. package/vendor/libarchive/libarchive/archive_disk_acl_darwin.c +558 -0
  68. package/vendor/libarchive/libarchive/archive_disk_acl_freebsd.c +712 -0
  69. package/vendor/libarchive/libarchive/archive_disk_acl_linux.c +760 -0
  70. package/vendor/libarchive/libarchive/archive_disk_acl_sunos.c +824 -0
  71. package/vendor/libarchive/libarchive/archive_endian.h +234 -0
  72. package/vendor/libarchive/libarchive/archive_entry.3 +147 -0
  73. package/vendor/libarchive/libarchive/archive_entry.c +2297 -0
  74. package/vendor/libarchive/libarchive/archive_entry.h +763 -0
  75. package/vendor/libarchive/libarchive/archive_entry_acl.3 +458 -0
  76. package/vendor/libarchive/libarchive/archive_entry_copy_bhfi.c +55 -0
  77. package/vendor/libarchive/libarchive/archive_entry_copy_stat.c +82 -0
  78. package/vendor/libarchive/libarchive/archive_entry_link_resolver.c +472 -0
  79. package/vendor/libarchive/libarchive/archive_entry_linkify.3 +224 -0
  80. package/vendor/libarchive/libarchive/archive_entry_locale.h +90 -0
  81. package/vendor/libarchive/libarchive/archive_entry_misc.3 +63 -0
  82. package/vendor/libarchive/libarchive/archive_entry_paths.3 +153 -0
  83. package/vendor/libarchive/libarchive/archive_entry_perms.3 +210 -0
  84. package/vendor/libarchive/libarchive/archive_entry_private.h +205 -0
  85. package/vendor/libarchive/libarchive/archive_entry_sparse.c +156 -0
  86. package/vendor/libarchive/libarchive/archive_entry_stat.3 +274 -0
  87. package/vendor/libarchive/libarchive/archive_entry_stat.c +121 -0
  88. package/vendor/libarchive/libarchive/archive_entry_strmode.c +69 -0
  89. package/vendor/libarchive/libarchive/archive_entry_time.3 +127 -0
  90. package/vendor/libarchive/libarchive/archive_entry_xattr.c +155 -0
  91. package/vendor/libarchive/libarchive/archive_hmac.c +345 -0
  92. package/vendor/libarchive/libarchive/archive_hmac_private.h +119 -0
  93. package/vendor/libarchive/libarchive/archive_integer.h +267 -0
  94. package/vendor/libarchive/libarchive/archive_match.c +1869 -0
  95. package/vendor/libarchive/libarchive/archive_openssl_evp_private.h +54 -0
  96. package/vendor/libarchive/libarchive/archive_openssl_hmac_private.h +54 -0
  97. package/vendor/libarchive/libarchive/archive_options.c +219 -0
  98. package/vendor/libarchive/libarchive/archive_options_private.h +49 -0
  99. package/vendor/libarchive/libarchive/archive_pack_dev.c +330 -0
  100. package/vendor/libarchive/libarchive/archive_pack_dev.h +49 -0
  101. package/vendor/libarchive/libarchive/archive_parse_date.c +1173 -0
  102. package/vendor/libarchive/libarchive/archive_pathmatch.c +480 -0
  103. package/vendor/libarchive/libarchive/archive_pathmatch.h +50 -0
  104. package/vendor/libarchive/libarchive/archive_platform.h +213 -0
  105. package/vendor/libarchive/libarchive/archive_platform_acl.h +53 -0
  106. package/vendor/libarchive/libarchive/archive_platform_stat.h +45 -0
  107. package/vendor/libarchive/libarchive/archive_platform_xattr.h +45 -0
  108. package/vendor/libarchive/libarchive/archive_ppmd7.c +1173 -0
  109. package/vendor/libarchive/libarchive/archive_ppmd7_private.h +119 -0
  110. package/vendor/libarchive/libarchive/archive_ppmd8.c +1293 -0
  111. package/vendor/libarchive/libarchive/archive_ppmd8_private.h +123 -0
  112. package/vendor/libarchive/libarchive/archive_ppmd_private.h +159 -0
  113. package/vendor/libarchive/libarchive/archive_private.h +209 -0
  114. package/vendor/libarchive/libarchive/archive_random.c +274 -0
  115. package/vendor/libarchive/libarchive/archive_random_private.h +36 -0
  116. package/vendor/libarchive/libarchive/archive_rb.c +709 -0
  117. package/vendor/libarchive/libarchive/archive_rb.h +113 -0
  118. package/vendor/libarchive/libarchive/archive_read.3 +250 -0
  119. package/vendor/libarchive/libarchive/archive_read.c +1814 -0
  120. package/vendor/libarchive/libarchive/archive_read_add_passphrase.3 +72 -0
  121. package/vendor/libarchive/libarchive/archive_read_add_passphrase.c +189 -0
  122. package/vendor/libarchive/libarchive/archive_read_append_filter.c +209 -0
  123. package/vendor/libarchive/libarchive/archive_read_data.3 +128 -0
  124. package/vendor/libarchive/libarchive/archive_read_data_into_fd.c +207 -0
  125. package/vendor/libarchive/libarchive/archive_read_disk.3 +422 -0
  126. package/vendor/libarchive/libarchive/archive_read_disk_entry_from_file.c +1086 -0
  127. package/vendor/libarchive/libarchive/archive_read_disk_posix.c +2639 -0
  128. package/vendor/libarchive/libarchive/archive_read_disk_private.h +97 -0
  129. package/vendor/libarchive/libarchive/archive_read_disk_set_standard_lookup.c +315 -0
  130. package/vendor/libarchive/libarchive/archive_read_disk_windows.c +2603 -0
  131. package/vendor/libarchive/libarchive/archive_read_extract.3 +135 -0
  132. package/vendor/libarchive/libarchive/archive_read_extract.c +59 -0
  133. package/vendor/libarchive/libarchive/archive_read_extract2.c +154 -0
  134. package/vendor/libarchive/libarchive/archive_read_filter.3 +163 -0
  135. package/vendor/libarchive/libarchive/archive_read_format.3 +190 -0
  136. package/vendor/libarchive/libarchive/archive_read_free.3 +91 -0
  137. package/vendor/libarchive/libarchive/archive_read_header.3 +89 -0
  138. package/vendor/libarchive/libarchive/archive_read_new.3 +57 -0
  139. package/vendor/libarchive/libarchive/archive_read_open.3 +247 -0
  140. package/vendor/libarchive/libarchive/archive_read_open_fd.c +236 -0
  141. package/vendor/libarchive/libarchive/archive_read_open_file.c +257 -0
  142. package/vendor/libarchive/libarchive/archive_read_open_filename.c +652 -0
  143. package/vendor/libarchive/libarchive/archive_read_open_memory.c +188 -0
  144. package/vendor/libarchive/libarchive/archive_read_private.h +274 -0
  145. package/vendor/libarchive/libarchive/archive_read_set_format.c +116 -0
  146. package/vendor/libarchive/libarchive/archive_read_set_options.3 +290 -0
  147. package/vendor/libarchive/libarchive/archive_read_set_options.c +132 -0
  148. package/vendor/libarchive/libarchive/archive_read_support_filter_all.c +84 -0
  149. package/vendor/libarchive/libarchive/archive_read_support_filter_by_code.c +68 -0
  150. package/vendor/libarchive/libarchive/archive_read_support_filter_bzip2.c +360 -0
  151. package/vendor/libarchive/libarchive/archive_read_support_filter_compress.c +457 -0
  152. package/vendor/libarchive/libarchive/archive_read_support_filter_grzip.c +108 -0
  153. package/vendor/libarchive/libarchive/archive_read_support_filter_gzip.c +538 -0
  154. package/vendor/libarchive/libarchive/archive_read_support_filter_lrzip.c +119 -0
  155. package/vendor/libarchive/libarchive/archive_read_support_filter_lz4.c +829 -0
  156. package/vendor/libarchive/libarchive/archive_read_support_filter_lzop.c +493 -0
  157. package/vendor/libarchive/libarchive/archive_read_support_filter_none.c +51 -0
  158. package/vendor/libarchive/libarchive/archive_read_support_filter_program.c +497 -0
  159. package/vendor/libarchive/libarchive/archive_read_support_filter_rpm.c +249 -0
  160. package/vendor/libarchive/libarchive/archive_read_support_filter_uu.c +739 -0
  161. package/vendor/libarchive/libarchive/archive_read_support_filter_xz.c +792 -0
  162. package/vendor/libarchive/libarchive/archive_read_support_filter_zstd.c +356 -0
  163. package/vendor/libarchive/libarchive/archive_read_support_format_7zip.c +4650 -0
  164. package/vendor/libarchive/libarchive/archive_read_support_format_all.c +88 -0
  165. package/vendor/libarchive/libarchive/archive_read_support_format_ar.c +597 -0
  166. package/vendor/libarchive/libarchive/archive_read_support_format_by_code.c +76 -0
  167. package/vendor/libarchive/libarchive/archive_read_support_format_cab.c +3410 -0
  168. package/vendor/libarchive/libarchive/archive_read_support_format_cpio.c +1152 -0
  169. package/vendor/libarchive/libarchive/archive_read_support_format_empty.c +95 -0
  170. package/vendor/libarchive/libarchive/archive_read_support_format_iso9660.c +3516 -0
  171. package/vendor/libarchive/libarchive/archive_read_support_format_lha.c +2931 -0
  172. package/vendor/libarchive/libarchive/archive_read_support_format_mtree.c +2113 -0
  173. package/vendor/libarchive/libarchive/archive_read_support_format_rar.c +3932 -0
  174. package/vendor/libarchive/libarchive/archive_read_support_format_rar5.c +4501 -0
  175. package/vendor/libarchive/libarchive/archive_read_support_format_raw.c +194 -0
  176. package/vendor/libarchive/libarchive/archive_read_support_format_tar.c +3785 -0
  177. package/vendor/libarchive/libarchive/archive_read_support_format_warc.c +875 -0
  178. package/vendor/libarchive/libarchive/archive_read_support_format_xar.c +3770 -0
  179. package/vendor/libarchive/libarchive/archive_read_support_format_zip.c +4710 -0
  180. package/vendor/libarchive/libarchive/archive_string.c +4391 -0
  181. package/vendor/libarchive/libarchive/archive_string.h +244 -0
  182. package/vendor/libarchive/libarchive/archive_string_composition.h +2289 -0
  183. package/vendor/libarchive/libarchive/archive_string_sprintf.c +200 -0
  184. package/vendor/libarchive/libarchive/archive_time.c +168 -0
  185. package/vendor/libarchive/libarchive/archive_time_private.h +47 -0
  186. package/vendor/libarchive/libarchive/archive_util.3 +239 -0
  187. package/vendor/libarchive/libarchive/archive_util.c +668 -0
  188. package/vendor/libarchive/libarchive/archive_version_details.c +516 -0
  189. package/vendor/libarchive/libarchive/archive_virtual.c +162 -0
  190. package/vendor/libarchive/libarchive/archive_windows.c +1035 -0
  191. package/vendor/libarchive/libarchive/archive_windows.h +332 -0
  192. package/vendor/libarchive/libarchive/archive_write.3 +266 -0
  193. package/vendor/libarchive/libarchive/archive_write.c +896 -0
  194. package/vendor/libarchive/libarchive/archive_write_add_filter.c +71 -0
  195. package/vendor/libarchive/libarchive/archive_write_add_filter_b64encode.c +329 -0
  196. package/vendor/libarchive/libarchive/archive_write_add_filter_by_name.c +76 -0
  197. package/vendor/libarchive/libarchive/archive_write_add_filter_bzip2.c +411 -0
  198. package/vendor/libarchive/libarchive/archive_write_add_filter_compress.c +459 -0
  199. package/vendor/libarchive/libarchive/archive_write_add_filter_grzip.c +143 -0
  200. package/vendor/libarchive/libarchive/archive_write_add_filter_gzip.c +493 -0
  201. package/vendor/libarchive/libarchive/archive_write_add_filter_lrzip.c +214 -0
  202. package/vendor/libarchive/libarchive/archive_write_add_filter_lz4.c +710 -0
  203. package/vendor/libarchive/libarchive/archive_write_add_filter_lzop.c +500 -0
  204. package/vendor/libarchive/libarchive/archive_write_add_filter_none.c +42 -0
  205. package/vendor/libarchive/libarchive/archive_write_add_filter_program.c +398 -0
  206. package/vendor/libarchive/libarchive/archive_write_add_filter_uuencode.c +320 -0
  207. package/vendor/libarchive/libarchive/archive_write_add_filter_xz.c +547 -0
  208. package/vendor/libarchive/libarchive/archive_write_add_filter_zstd.c +608 -0
  209. package/vendor/libarchive/libarchive/archive_write_blocksize.3 +112 -0
  210. package/vendor/libarchive/libarchive/archive_write_data.3 +88 -0
  211. package/vendor/libarchive/libarchive/archive_write_disk.3 +360 -0
  212. package/vendor/libarchive/libarchive/archive_write_disk_posix.c +4789 -0
  213. package/vendor/libarchive/libarchive/archive_write_disk_private.h +43 -0
  214. package/vendor/libarchive/libarchive/archive_write_disk_set_standard_lookup.c +265 -0
  215. package/vendor/libarchive/libarchive/archive_write_disk_windows.c +2952 -0
  216. package/vendor/libarchive/libarchive/archive_write_filter.3 +140 -0
  217. package/vendor/libarchive/libarchive/archive_write_finish_entry.3 +77 -0
  218. package/vendor/libarchive/libarchive/archive_write_format.3 +187 -0
  219. package/vendor/libarchive/libarchive/archive_write_free.3 +94 -0
  220. package/vendor/libarchive/libarchive/archive_write_header.3 +71 -0
  221. package/vendor/libarchive/libarchive/archive_write_new.3 +56 -0
  222. package/vendor/libarchive/libarchive/archive_write_open.3 +270 -0
  223. package/vendor/libarchive/libarchive/archive_write_open_fd.c +141 -0
  224. package/vendor/libarchive/libarchive/archive_write_open_file.c +102 -0
  225. package/vendor/libarchive/libarchive/archive_write_open_filename.c +272 -0
  226. package/vendor/libarchive/libarchive/archive_write_open_memory.c +110 -0
  227. package/vendor/libarchive/libarchive/archive_write_private.h +166 -0
  228. package/vendor/libarchive/libarchive/archive_write_set_format.c +124 -0
  229. package/vendor/libarchive/libarchive/archive_write_set_format_7zip.c +2571 -0
  230. package/vendor/libarchive/libarchive/archive_write_set_format_ar.c +540 -0
  231. package/vendor/libarchive/libarchive/archive_write_set_format_by_name.c +93 -0
  232. package/vendor/libarchive/libarchive/archive_write_set_format_cpio.c +11 -0
  233. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_binary.c +618 -0
  234. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_newc.c +457 -0
  235. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_odc.c +508 -0
  236. package/vendor/libarchive/libarchive/archive_write_set_format_filter_by_ext.c +141 -0
  237. package/vendor/libarchive/libarchive/archive_write_set_format_gnutar.c +790 -0
  238. package/vendor/libarchive/libarchive/archive_write_set_format_iso9660.c +8216 -0
  239. package/vendor/libarchive/libarchive/archive_write_set_format_mtree.c +2307 -0
  240. package/vendor/libarchive/libarchive/archive_write_set_format_pax.c +2249 -0
  241. package/vendor/libarchive/libarchive/archive_write_set_format_private.h +40 -0
  242. package/vendor/libarchive/libarchive/archive_write_set_format_raw.c +123 -0
  243. package/vendor/libarchive/libarchive/archive_write_set_format_shar.c +650 -0
  244. package/vendor/libarchive/libarchive/archive_write_set_format_ustar.c +768 -0
  245. package/vendor/libarchive/libarchive/archive_write_set_format_v7tar.c +648 -0
  246. package/vendor/libarchive/libarchive/archive_write_set_format_warc.c +491 -0
  247. package/vendor/libarchive/libarchive/archive_write_set_format_xar.c +3799 -0
  248. package/vendor/libarchive/libarchive/archive_write_set_format_zip.c +2548 -0
  249. package/vendor/libarchive/libarchive/archive_write_set_options.3 +779 -0
  250. package/vendor/libarchive/libarchive/archive_write_set_options.c +129 -0
  251. package/vendor/libarchive/libarchive/archive_write_set_passphrase.3 +72 -0
  252. package/vendor/libarchive/libarchive/archive_write_set_passphrase.c +95 -0
  253. package/vendor/libarchive/libarchive/archive_xxhash.h +48 -0
  254. package/vendor/libarchive/libarchive/config_freebsd.h +275 -0
  255. package/vendor/libarchive/libarchive/cpio.5 +389 -0
  256. package/vendor/libarchive/libarchive/filter_fork.h +44 -0
  257. package/vendor/libarchive/libarchive/filter_fork_posix.c +269 -0
  258. package/vendor/libarchive/libarchive/filter_fork_windows.c +270 -0
  259. package/vendor/libarchive/libarchive/libarchive-formats.5 +478 -0
  260. package/vendor/libarchive/libarchive/libarchive.3 +297 -0
  261. package/vendor/libarchive/libarchive/libarchive_changes.3 +339 -0
  262. package/vendor/libarchive/libarchive/libarchive_internals.3 +363 -0
  263. package/vendor/libarchive/libarchive/module.modulemap +34 -0
  264. package/vendor/libarchive/libarchive/mtree.5 +323 -0
  265. package/vendor/libarchive/libarchive/tar.5 +947 -0
  266. package/vendor/libarchive/libarchive/xxhash.c +531 -0
@@ -0,0 +1,1152 @@
1
+ /*-
2
+ * Copyright (c) 2003-2007 Tim Kientzle
3
+ * Copyright (c) 2010-2012 Michihiro NAKAJIMA
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions
8
+ * are met:
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ *
15
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ */
26
+
27
+ #include "archive_platform.h"
28
+
29
+ #ifdef HAVE_ERRNO_H
30
+ #include <errno.h>
31
+ #endif
32
+ /* #include <stdint.h> */ /* See archive_platform.h */
33
+ #ifdef HAVE_STDLIB_H
34
+ #include <stdlib.h>
35
+ #endif
36
+ #ifdef HAVE_STRING_H
37
+ #include <string.h>
38
+ #endif
39
+
40
+ #include "archive.h"
41
+ #include "archive_endian.h"
42
+ #include "archive_entry.h"
43
+ #include "archive_entry_locale.h"
44
+ #include "archive_private.h"
45
+ #include "archive_read_private.h"
46
+
47
+ #define bin_magic_offset 0
48
+ #define bin_magic_size 2
49
+ #define bin_dev_offset 2
50
+ #define bin_dev_size 2
51
+ #define bin_ino_offset 4
52
+ #define bin_ino_size 2
53
+ #define bin_mode_offset 6
54
+ #define bin_mode_size 2
55
+ #define bin_uid_offset 8
56
+ #define bin_uid_size 2
57
+ #define bin_gid_offset 10
58
+ #define bin_gid_size 2
59
+ #define bin_nlink_offset 12
60
+ #define bin_nlink_size 2
61
+ #define bin_rdev_offset 14
62
+ #define bin_rdev_size 2
63
+ #define bin_mtime_offset 16
64
+ #define bin_mtime_size 4
65
+ #define bin_namesize_offset 20
66
+ #define bin_namesize_size 2
67
+ #define bin_filesize_offset 22
68
+ #define bin_filesize_size 4
69
+ #define bin_header_size 26
70
+
71
+ #define odc_magic_offset 0
72
+ #define odc_magic_size 6
73
+ #define odc_dev_offset 6
74
+ #define odc_dev_size 6
75
+ #define odc_ino_offset 12
76
+ #define odc_ino_size 6
77
+ #define odc_mode_offset 18
78
+ #define odc_mode_size 6
79
+ #define odc_uid_offset 24
80
+ #define odc_uid_size 6
81
+ #define odc_gid_offset 30
82
+ #define odc_gid_size 6
83
+ #define odc_nlink_offset 36
84
+ #define odc_nlink_size 6
85
+ #define odc_rdev_offset 42
86
+ #define odc_rdev_size 6
87
+ #define odc_mtime_offset 48
88
+ #define odc_mtime_size 11
89
+ #define odc_namesize_offset 59
90
+ #define odc_namesize_size 6
91
+ #define odc_filesize_offset 65
92
+ #define odc_filesize_size 11
93
+ #define odc_header_size 76
94
+
95
+ #define newc_magic_offset 0
96
+ #define newc_magic_size 6
97
+ #define newc_ino_offset 6
98
+ #define newc_ino_size 8
99
+ #define newc_mode_offset 14
100
+ #define newc_mode_size 8
101
+ #define newc_uid_offset 22
102
+ #define newc_uid_size 8
103
+ #define newc_gid_offset 30
104
+ #define newc_gid_size 8
105
+ #define newc_nlink_offset 38
106
+ #define newc_nlink_size 8
107
+ #define newc_mtime_offset 46
108
+ #define newc_mtime_size 8
109
+ #define newc_filesize_offset 54
110
+ #define newc_filesize_size 8
111
+ #define newc_devmajor_offset 62
112
+ #define newc_devmajor_size 8
113
+ #define newc_devminor_offset 70
114
+ #define newc_devminor_size 8
115
+ #define newc_rdevmajor_offset 78
116
+ #define newc_rdevmajor_size 8
117
+ #define newc_rdevminor_offset 86
118
+ #define newc_rdevminor_size 8
119
+ #define newc_namesize_offset 94
120
+ #define newc_namesize_size 8
121
+ #define newc_checksum_offset 102
122
+ #define newc_checksum_size 8
123
+ #define newc_header_size 110
124
+
125
+ /*
126
+ * An afio large ASCII header, which they named itself.
127
+ * afio utility uses this header, if a file size is larger than 2G bytes
128
+ * or inode/uid/gid is bigger than 65535(0xFFFF) or mtime is bigger than
129
+ * 0x7fffffff, which we cannot record to odc header because of its limit.
130
+ * If not, uses odc header.
131
+ */
132
+ #define afiol_magic_offset 0
133
+ #define afiol_magic_size 6
134
+ #define afiol_dev_offset 6
135
+ #define afiol_dev_size 8 /* hex */
136
+ #define afiol_ino_offset 14
137
+ #define afiol_ino_size 16 /* hex */
138
+ #define afiol_ino_m_offset 30 /* 'm' */
139
+ #define afiol_mode_offset 31
140
+ #define afiol_mode_size 6 /* oct */
141
+ #define afiol_uid_offset 37
142
+ #define afiol_uid_size 8 /* hex */
143
+ #define afiol_gid_offset 45
144
+ #define afiol_gid_size 8 /* hex */
145
+ #define afiol_nlink_offset 53
146
+ #define afiol_nlink_size 8 /* hex */
147
+ #define afiol_rdev_offset 61
148
+ #define afiol_rdev_size 8 /* hex */
149
+ #define afiol_mtime_offset 69
150
+ #define afiol_mtime_size 16 /* hex */
151
+ #define afiol_mtime_n_offset 85 /* 'n' */
152
+ #define afiol_namesize_offset 86
153
+ #define afiol_namesize_size 4 /* hex */
154
+ #define afiol_flag_offset 90
155
+ #define afiol_flag_size 4 /* hex */
156
+ #define afiol_xsize_offset 94
157
+ #define afiol_xsize_size 4 /* hex */
158
+ #define afiol_xsize_s_offset 98 /* 's' */
159
+ #define afiol_filesize_offset 99
160
+ #define afiol_filesize_size 16 /* hex */
161
+ #define afiol_filesize_c_offset 115 /* ':' */
162
+ #define afiol_header_size 116
163
+
164
+ /* CPIO name fields store a full pathname, including the terminating NUL. */
165
+ #define CPIO_PATHNAME_MAX (1024 * 1024)
166
+
167
+ struct links_entry {
168
+ struct links_entry *next;
169
+ struct links_entry *previous;
170
+ unsigned int links;
171
+ dev_t dev;
172
+ int64_t ino;
173
+ char *name;
174
+ };
175
+
176
+ #define CPIO_MAGIC 0x13141516
177
+ struct cpio {
178
+ int magic;
179
+ int (*read_header)(struct archive_read *, struct cpio *,
180
+ struct archive_entry *, size_t *, size_t *);
181
+ struct links_entry *links_head;
182
+ int64_t entry_bytes_remaining;
183
+ int64_t entry_bytes_unconsumed;
184
+ int64_t entry_offset;
185
+ int64_t entry_padding;
186
+
187
+ struct archive_string_conv *opt_sconv;
188
+ struct archive_string_conv *sconv_default;
189
+ int init_default_conversion;
190
+
191
+ int option_pwb;
192
+ };
193
+
194
+ static int64_t atol16(const char *, unsigned);
195
+ static uint64_t atol16u(const char *, unsigned);
196
+ static int64_t atol8(const char *, unsigned);
197
+ static int archive_read_format_cpio_bid(struct archive_read *, int);
198
+ static int archive_read_format_cpio_options(struct archive_read *,
199
+ const char *, const char *);
200
+ static int archive_read_format_cpio_cleanup(struct archive_read *);
201
+ static int archive_read_format_cpio_read_data(struct archive_read *,
202
+ const void **, size_t *, int64_t *);
203
+ static int archive_read_format_cpio_read_header(struct archive_read *,
204
+ struct archive_entry *);
205
+ static int archive_read_format_cpio_skip(struct archive_read *);
206
+ static int64_t be32dec(const unsigned char *);
207
+ static int find_odc_header(struct archive_read *);
208
+ static int find_newc_header(struct archive_read *);
209
+ static int header_bin_be(struct archive_read *, struct cpio *,
210
+ struct archive_entry *, size_t *, size_t *);
211
+ static int header_bin_le(struct archive_read *, struct cpio *,
212
+ struct archive_entry *, size_t *, size_t *);
213
+ static int header_newc(struct archive_read *, struct cpio *,
214
+ struct archive_entry *, size_t *, size_t *);
215
+ static int header_odc(struct archive_read *, struct cpio *,
216
+ struct archive_entry *, size_t *, size_t *);
217
+ static int header_afiol(struct archive_read *, struct cpio *,
218
+ struct archive_entry *, size_t *, size_t *);
219
+ static int is_octal(const char *, size_t);
220
+ static int is_hex(const char *, size_t);
221
+ static int64_t le32dec(const unsigned char *);
222
+ static int record_hardlink(struct archive_read *a,
223
+ struct cpio *cpio, struct archive_entry *entry);
224
+
225
+ int
226
+ archive_read_support_format_cpio(struct archive *_a)
227
+ {
228
+ struct archive_read *a = (struct archive_read *)_a;
229
+ struct cpio *cpio;
230
+ int r;
231
+
232
+ archive_check_magic(_a, ARCHIVE_READ_MAGIC,
233
+ ARCHIVE_STATE_NEW, "archive_read_support_format_cpio");
234
+
235
+ cpio = calloc(1, sizeof(*cpio));
236
+ if (cpio == NULL) {
237
+ archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
238
+ return (ARCHIVE_FATAL);
239
+ }
240
+ cpio->magic = CPIO_MAGIC;
241
+
242
+ r = __archive_read_register_format(a,
243
+ cpio,
244
+ "cpio",
245
+ archive_read_format_cpio_bid,
246
+ archive_read_format_cpio_options,
247
+ archive_read_format_cpio_read_header,
248
+ archive_read_format_cpio_read_data,
249
+ archive_read_format_cpio_skip,
250
+ NULL,
251
+ archive_read_format_cpio_cleanup,
252
+ NULL,
253
+ NULL);
254
+
255
+ if (r != ARCHIVE_OK)
256
+ free(cpio);
257
+ return (ARCHIVE_OK);
258
+ }
259
+
260
+
261
+ static int
262
+ archive_read_format_cpio_bid(struct archive_read *a, int best_bid)
263
+ {
264
+ struct cpio *cpio = a->format->data;
265
+ const unsigned char *p;
266
+ int bid;
267
+
268
+ (void)best_bid; /* UNUSED */
269
+
270
+ if ((p = __archive_read_ahead(a, 6, NULL)) == NULL)
271
+ return (-1);
272
+
273
+ bid = 0;
274
+ if (memcmp(p, "070707", 6) == 0) {
275
+ /* ASCII cpio archive (odc, POSIX.1) */
276
+ cpio->read_header = header_odc;
277
+ bid += 48;
278
+ /*
279
+ * XXX TODO: More verification; Could check that only octal
280
+ * digits appear in appropriate header locations. XXX
281
+ */
282
+ } else if (memcmp(p, "070727", 6) == 0) {
283
+ /* afio large ASCII cpio archive */
284
+ cpio->read_header = header_odc;
285
+ bid += 48;
286
+ /*
287
+ * XXX TODO: More verification; Could check that almost hex
288
+ * digits appear in appropriate header locations. XXX
289
+ */
290
+ } else if (memcmp(p, "070701", 6) == 0) {
291
+ /* ASCII cpio archive (SVR4 without CRC) */
292
+ cpio->read_header = header_newc;
293
+ bid += 48;
294
+ /*
295
+ * XXX TODO: More verification; Could check that only hex
296
+ * digits appear in appropriate header locations. XXX
297
+ */
298
+ } else if (memcmp(p, "070702", 6) == 0) {
299
+ /* ASCII cpio archive (SVR4 with CRC) */
300
+ /* XXX TODO: Flag that we should check the CRC. XXX */
301
+ cpio->read_header = header_newc;
302
+ bid += 48;
303
+ /*
304
+ * XXX TODO: More verification; Could check that only hex
305
+ * digits appear in appropriate header locations. XXX
306
+ */
307
+ } else if (archive_be16dec(p) == 070707) {
308
+ /* big-endian binary cpio archives */
309
+ cpio->read_header = header_bin_be;
310
+ bid += 16;
311
+ /* Is more verification possible here? */
312
+ } else if (archive_le16dec(p) == 070707) {
313
+ /* little-endian binary cpio archives */
314
+ cpio->read_header = header_bin_le;
315
+ bid += 16;
316
+ /* Is more verification possible here? */
317
+ } else
318
+ return (ARCHIVE_WARN);
319
+
320
+ return (bid);
321
+ }
322
+
323
+ static int
324
+ archive_read_format_cpio_options(struct archive_read *a,
325
+ const char *key, const char *val)
326
+ {
327
+ struct cpio *cpio = a->format->data;
328
+ int ret = ARCHIVE_FAILED;
329
+
330
+ if (strcmp(key, "compat-2x") == 0) {
331
+ /* Handle filenames as libarchive 2.x */
332
+ cpio->init_default_conversion = (val != NULL)?1:0;
333
+ return (ARCHIVE_OK);
334
+ } else if (strcmp(key, "hdrcharset") == 0) {
335
+ if (val == NULL || val[0] == 0)
336
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
337
+ "cpio: hdrcharset option needs a character-set name");
338
+ else {
339
+ cpio->opt_sconv =
340
+ archive_string_conversion_from_charset(
341
+ &a->archive, val, 0);
342
+ if (cpio->opt_sconv != NULL)
343
+ ret = ARCHIVE_OK;
344
+ else
345
+ ret = ARCHIVE_FATAL;
346
+ }
347
+ return (ret);
348
+ } else if (strcmp(key, "pwb") == 0) {
349
+ if (val != NULL && val[0] != 0)
350
+ cpio->option_pwb = 1;
351
+ return (ARCHIVE_OK);
352
+ }
353
+
354
+ /* Note: The "warn" return is just to inform the options
355
+ * supervisor that we didn't handle it. It will generate
356
+ * a suitable error if no one used this option. */
357
+ return (ARCHIVE_WARN);
358
+ }
359
+
360
+ static int
361
+ archive_read_format_cpio_read_header(struct archive_read *a,
362
+ struct archive_entry *entry)
363
+ {
364
+ struct cpio *cpio = a->format->data;
365
+ const void *h, *hl;
366
+ struct archive_string_conv *sconv;
367
+ size_t namelength;
368
+ size_t name_pad;
369
+ int is_trailer;
370
+ int r;
371
+
372
+ sconv = cpio->opt_sconv;
373
+ if (sconv == NULL) {
374
+ if (!cpio->init_default_conversion) {
375
+ cpio->sconv_default =
376
+ archive_string_default_conversion_for_read(
377
+ &(a->archive));
378
+ cpio->init_default_conversion = 1;
379
+ }
380
+ sconv = cpio->sconv_default;
381
+ }
382
+
383
+ r = (cpio->read_header(a, cpio, entry, &namelength, &name_pad));
384
+
385
+ if (r < ARCHIVE_WARN)
386
+ return (r);
387
+
388
+ if (namelength > CPIO_PATHNAME_MAX) {
389
+ archive_set_error(&a->archive, ENOMEM,
390
+ "Rejecting malformed cpio archive: "
391
+ "pathname exceeds 1 megabyte");
392
+ return (ARCHIVE_FATAL);
393
+ }
394
+
395
+ /* Read name from buffer. */
396
+ h = __archive_read_ahead(a, namelength, NULL);
397
+ if (h == NULL)
398
+ return (ARCHIVE_FATAL);
399
+ if (archive_entry_copy_pathname_l(entry,
400
+ (const char *)h, namelength, sconv) != 0) {
401
+ if (errno == ENOMEM) {
402
+ archive_set_error(&a->archive, ENOMEM,
403
+ "Can't allocate memory for Pathname");
404
+ return (ARCHIVE_FATAL);
405
+ }
406
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
407
+ "Pathname can't be converted from %s to current locale",
408
+ archive_string_conversion_charset_name(sconv));
409
+ r = ARCHIVE_WARN;
410
+ }
411
+ /* Save this before consuming the name buffer below. */
412
+ is_trailer = (namelength == 11 &&
413
+ memcmp((const char *)h, "TRAILER!!!", 10) == 0);
414
+ cpio->entry_offset = 0;
415
+
416
+ __archive_read_consume(a, namelength);
417
+ __archive_read_consume(a, name_pad);
418
+
419
+ /* If this is a symlink, read the link contents. */
420
+ if (archive_entry_filetype(entry) == AE_IFLNK) {
421
+ if (cpio->entry_bytes_remaining > 1024 * 1024) {
422
+ archive_set_error(&a->archive, ENOMEM,
423
+ "Rejecting malformed cpio archive: symlink contents exceed 1 megabyte");
424
+ return (ARCHIVE_FATAL);
425
+ }
426
+ hl = __archive_read_ahead(a,
427
+ (size_t)cpio->entry_bytes_remaining, NULL);
428
+ if (hl == NULL)
429
+ return (ARCHIVE_FATAL);
430
+ if (archive_entry_copy_symlink_l(entry, (const char *)hl,
431
+ (size_t)cpio->entry_bytes_remaining, sconv) != 0) {
432
+ if (errno == ENOMEM) {
433
+ archive_set_error(&a->archive, ENOMEM,
434
+ "Can't allocate memory for Linkname");
435
+ return (ARCHIVE_FATAL);
436
+ }
437
+ archive_set_error(&a->archive,
438
+ ARCHIVE_ERRNO_FILE_FORMAT,
439
+ "Linkname can't be converted from %s to "
440
+ "current locale",
441
+ archive_string_conversion_charset_name(sconv));
442
+ r = ARCHIVE_WARN;
443
+ }
444
+ __archive_read_consume(a, cpio->entry_bytes_remaining);
445
+ cpio->entry_bytes_remaining = 0;
446
+ }
447
+
448
+ /* XXX TODO: If the full mode is 0160200, then this is a Solaris
449
+ * ACL description for the following entry. Read this body
450
+ * and parse it as a Solaris-style ACL, then read the next
451
+ * header. XXX */
452
+
453
+ /* Compare name to "TRAILER!!!" to test for end-of-archive. */
454
+ if (is_trailer) {
455
+ /* TODO: Store file location of start of block. */
456
+ archive_clear_error(&a->archive);
457
+ return (ARCHIVE_EOF);
458
+ }
459
+
460
+ /* Detect and record hardlinks to previously-extracted entries. */
461
+ if (record_hardlink(a, cpio, entry) != ARCHIVE_OK) {
462
+ return (ARCHIVE_FATAL);
463
+ }
464
+
465
+ return (r);
466
+ }
467
+
468
+ static int
469
+ archive_read_format_cpio_read_data(struct archive_read *a,
470
+ const void **buff, size_t *size, int64_t *offset)
471
+ {
472
+ struct cpio *cpio = a->format->data;
473
+ ssize_t bytes_read;
474
+
475
+ if (cpio->entry_bytes_unconsumed) {
476
+ __archive_read_consume(a, cpio->entry_bytes_unconsumed);
477
+ cpio->entry_bytes_unconsumed = 0;
478
+ }
479
+
480
+ if (cpio->entry_bytes_remaining > 0) {
481
+ *buff = __archive_read_ahead(a, 1, &bytes_read);
482
+ if (bytes_read <= 0)
483
+ return (ARCHIVE_FATAL);
484
+ if (bytes_read > cpio->entry_bytes_remaining)
485
+ bytes_read = (ssize_t)cpio->entry_bytes_remaining;
486
+ *size = bytes_read;
487
+ cpio->entry_bytes_unconsumed = bytes_read;
488
+ *offset = cpio->entry_offset;
489
+ cpio->entry_offset += bytes_read;
490
+ cpio->entry_bytes_remaining -= bytes_read;
491
+ return (ARCHIVE_OK);
492
+ } else {
493
+ if (cpio->entry_padding !=
494
+ __archive_read_consume(a, cpio->entry_padding)) {
495
+ return (ARCHIVE_FATAL);
496
+ }
497
+ cpio->entry_padding = 0;
498
+ *buff = NULL;
499
+ *size = 0;
500
+ *offset = cpio->entry_offset;
501
+ return (ARCHIVE_EOF);
502
+ }
503
+ }
504
+
505
+ static int
506
+ archive_read_format_cpio_skip(struct archive_read *a)
507
+ {
508
+ struct cpio *cpio = a->format->data;
509
+ int64_t to_skip = cpio->entry_bytes_remaining + cpio->entry_padding +
510
+ cpio->entry_bytes_unconsumed;
511
+
512
+ if (to_skip != __archive_read_consume(a, to_skip)) {
513
+ return (ARCHIVE_FATAL);
514
+ }
515
+ cpio->entry_bytes_remaining = 0;
516
+ cpio->entry_padding = 0;
517
+ cpio->entry_bytes_unconsumed = 0;
518
+ return (ARCHIVE_OK);
519
+ }
520
+
521
+ /*
522
+ * Skip forward to the next cpio newc header by searching for the
523
+ * 07070[12] string. This should be generalized and merged with
524
+ * find_odc_header below.
525
+ */
526
+ static int
527
+ is_hex(const char *p, size_t len)
528
+ {
529
+ while (len-- > 0) {
530
+ if ((*p >= '0' && *p <= '9')
531
+ || (*p >= 'a' && *p <= 'f')
532
+ || (*p >= 'A' && *p <= 'F'))
533
+ ++p;
534
+ else
535
+ return (0);
536
+ }
537
+ return (1);
538
+ }
539
+
540
+ static int
541
+ find_newc_header(struct archive_read *a)
542
+ {
543
+ const void *h;
544
+ const char *p, *q;
545
+ int64_t skip;
546
+ uintmax_t skipped = 0;
547
+ ssize_t bytes;
548
+
549
+ for (;;) {
550
+ h = __archive_read_ahead(a, newc_header_size, &bytes);
551
+ if (h == NULL)
552
+ return (ARCHIVE_FATAL);
553
+ p = h;
554
+ q = p + bytes;
555
+
556
+ /* Try the typical case first, then go into the slow search.*/
557
+ if (memcmp("07070", p, 5) == 0
558
+ && (p[5] == '1' || p[5] == '2')
559
+ && is_hex(p, newc_header_size))
560
+ return (ARCHIVE_OK);
561
+
562
+ /*
563
+ * Scan ahead until we find something that looks
564
+ * like a newc header.
565
+ */
566
+ while (p + newc_header_size <= q) {
567
+ switch (p[5]) {
568
+ case '1':
569
+ case '2':
570
+ if (memcmp("07070", p, 5) == 0
571
+ && is_hex(p, newc_header_size)) {
572
+ skip = p - (const char *)h;
573
+ __archive_read_consume(a, skip);
574
+ skipped += skip;
575
+ if (skipped > 0) {
576
+ archive_set_error(&a->archive,
577
+ 0,
578
+ "Skipped %ju bytes before "
579
+ "finding valid header",
580
+ skipped);
581
+ return (ARCHIVE_WARN);
582
+ }
583
+ return (ARCHIVE_OK);
584
+ }
585
+ p += 2;
586
+ break;
587
+ case '0':
588
+ p++;
589
+ break;
590
+ default:
591
+ p += 6;
592
+ break;
593
+ }
594
+ }
595
+ skip = p - (const char *)h;
596
+ __archive_read_consume(a, skip);
597
+ skipped += skip;
598
+ }
599
+ }
600
+
601
+ static int
602
+ header_newc(struct archive_read *a, struct cpio *cpio,
603
+ struct archive_entry *entry, size_t *namelength, size_t *name_pad)
604
+ {
605
+ const void *h;
606
+ const char *header;
607
+ int r;
608
+
609
+ r = find_newc_header(a);
610
+ if (r < ARCHIVE_WARN)
611
+ return (r);
612
+
613
+ /* Read fixed-size portion of header. */
614
+ h = __archive_read_ahead(a, newc_header_size, NULL);
615
+ if (h == NULL)
616
+ return (ARCHIVE_FATAL);
617
+
618
+ /* Parse out hex fields. */
619
+ header = (const char *)h;
620
+
621
+ if (memcmp(header + newc_magic_offset, "070701", 6) == 0) {
622
+ a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
623
+ a->archive.archive_format_name = "ASCII cpio (SVR4 with no CRC)";
624
+ } else if (memcmp(header + newc_magic_offset, "070702", 6) == 0) {
625
+ a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_CRC;
626
+ a->archive.archive_format_name = "ASCII cpio (SVR4 with CRC)";
627
+ } else {
628
+ /* TODO: Abort here? */
629
+ }
630
+
631
+ archive_entry_set_devmajor(entry,
632
+ (dev_t)atol16(header + newc_devmajor_offset, newc_devmajor_size));
633
+ archive_entry_set_devminor(entry,
634
+ (dev_t)atol16(header + newc_devminor_offset, newc_devminor_size));
635
+ archive_entry_set_ino(entry, atol16(header + newc_ino_offset, newc_ino_size));
636
+ archive_entry_set_mode(entry,
637
+ (mode_t)atol16(header + newc_mode_offset, newc_mode_size));
638
+ archive_entry_set_uid(entry, atol16(header + newc_uid_offset, newc_uid_size));
639
+ archive_entry_set_gid(entry, atol16(header + newc_gid_offset, newc_gid_size));
640
+ archive_entry_set_nlink(entry,
641
+ (unsigned int)atol16(header + newc_nlink_offset, newc_nlink_size));
642
+ archive_entry_set_rdevmajor(entry,
643
+ (dev_t)atol16(header + newc_rdevmajor_offset, newc_rdevmajor_size));
644
+ archive_entry_set_rdevminor(entry,
645
+ (dev_t)atol16(header + newc_rdevminor_offset, newc_rdevminor_size));
646
+ archive_entry_set_mtime(entry, atol16(header + newc_mtime_offset, newc_mtime_size), 0);
647
+ *namelength = (size_t)atol16(header + newc_namesize_offset, newc_namesize_size);
648
+ /* Pad name to 2 more than a multiple of 4. */
649
+ *name_pad = (2 - *namelength) & 3;
650
+
651
+ /* Make sure that the padded name length fits into size_t. */
652
+ if (*name_pad > SIZE_MAX - *namelength) {
653
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
654
+ "cpio archive has invalid namelength");
655
+ return (ARCHIVE_FATAL);
656
+ }
657
+
658
+ /*
659
+ * Note: entry_bytes_remaining is at least 64 bits and
660
+ * therefore guaranteed to be big enough for a 33-bit file
661
+ * size.
662
+ */
663
+ cpio->entry_bytes_remaining =
664
+ atol16(header + newc_filesize_offset, newc_filesize_size);
665
+ archive_entry_set_size(entry, cpio->entry_bytes_remaining);
666
+ /* Pad file contents to a multiple of 4. */
667
+ cpio->entry_padding = 3 & -cpio->entry_bytes_remaining;
668
+ __archive_read_consume(a, newc_header_size);
669
+ return (r);
670
+ }
671
+
672
+ /*
673
+ * Skip forward to the next cpio odc header by searching for the
674
+ * 070707 string. This is a hand-optimized search that could
675
+ * probably be easily generalized to handle all character-based
676
+ * cpio variants.
677
+ */
678
+ static int
679
+ is_octal(const char *p, size_t len)
680
+ {
681
+ while (len-- > 0) {
682
+ if (*p < '0' || *p > '7')
683
+ return (0);
684
+ ++p;
685
+ }
686
+ return (1);
687
+ }
688
+
689
+ static int
690
+ is_afio_large(const char *h, size_t len)
691
+ {
692
+ if (len < afiol_header_size)
693
+ return (0);
694
+ if (h[afiol_ino_m_offset] != 'm'
695
+ || h[afiol_mtime_n_offset] != 'n'
696
+ || h[afiol_xsize_s_offset] != 's'
697
+ || h[afiol_filesize_c_offset] != ':')
698
+ return (0);
699
+ if (!is_hex(h + afiol_dev_offset, afiol_ino_m_offset - afiol_dev_offset))
700
+ return (0);
701
+ if (!is_hex(h + afiol_mode_offset, afiol_mtime_n_offset - afiol_mode_offset))
702
+ return (0);
703
+ if (!is_hex(h + afiol_namesize_offset, afiol_xsize_s_offset - afiol_namesize_offset))
704
+ return (0);
705
+ if (!is_hex(h + afiol_filesize_offset, afiol_filesize_size))
706
+ return (0);
707
+ return (1);
708
+ }
709
+
710
+ static int
711
+ find_odc_header(struct archive_read *a)
712
+ {
713
+ const void *h;
714
+ const char *p, *q;
715
+ int64_t skip;
716
+ uintmax_t skipped = 0;
717
+ ssize_t bytes;
718
+
719
+ for (;;) {
720
+ size_t header_size;
721
+
722
+ header_size = afiol_header_size;
723
+ h = __archive_read_ahead(a, afiol_header_size, &bytes);
724
+ if (h == NULL) {
725
+ if (bytes >= odc_header_size) {
726
+ header_size = odc_header_size;
727
+ h = __archive_read_ahead(a, odc_header_size,
728
+ &bytes);
729
+ }
730
+ if (h == NULL)
731
+ return (ARCHIVE_FATAL);
732
+ }
733
+ p = h;
734
+ q = p + bytes;
735
+
736
+ /* Try the typical case first, then go into the slow search.*/
737
+ if (memcmp("070707", p, 6) == 0 && is_octal(p, odc_header_size))
738
+ return (ARCHIVE_OK);
739
+ if (memcmp("070727", p, 6) == 0 && is_afio_large(p, bytes)) {
740
+ a->archive.archive_format = ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
741
+ return (ARCHIVE_OK);
742
+ }
743
+
744
+ /*
745
+ * Scan ahead until we find something that looks
746
+ * like an odc header.
747
+ */
748
+ while (p + header_size <= q) {
749
+ switch (p[5]) {
750
+ case '7':
751
+ if ((memcmp("070707", p, 6) == 0
752
+ && is_octal(p, odc_header_size))
753
+ || (memcmp("070727", p, 6) == 0
754
+ && is_afio_large(p, q - p))) {
755
+ skip = p - (const char *)h;
756
+ __archive_read_consume(a, skip);
757
+ skipped += skip;
758
+ if (p[4] == '2')
759
+ a->archive.archive_format =
760
+ ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
761
+ if (skipped > 0) {
762
+ archive_set_error(&a->archive,
763
+ 0,
764
+ "Skipped %ju bytes before "
765
+ "finding valid header",
766
+ skipped);
767
+ return (ARCHIVE_WARN);
768
+ }
769
+ return (ARCHIVE_OK);
770
+ }
771
+ p += 2;
772
+ break;
773
+ case '0':
774
+ p++;
775
+ break;
776
+ default:
777
+ p += 6;
778
+ break;
779
+ }
780
+ }
781
+ skip = p - (const char *)h;
782
+ __archive_read_consume(a, skip);
783
+ skipped += skip;
784
+ }
785
+ }
786
+
787
+ static int
788
+ header_odc(struct archive_read *a, struct cpio *cpio,
789
+ struct archive_entry *entry, size_t *namelength, size_t *name_pad)
790
+ {
791
+ const void *h;
792
+ int r;
793
+ const char *header;
794
+
795
+ a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
796
+ a->archive.archive_format_name = "POSIX octet-oriented cpio";
797
+
798
+ /* Find the start of the next header. */
799
+ r = find_odc_header(a);
800
+ if (r < ARCHIVE_WARN)
801
+ return (r);
802
+
803
+ if (a->archive.archive_format == ARCHIVE_FORMAT_CPIO_AFIO_LARGE) {
804
+ int r2 = (header_afiol(a, cpio, entry, namelength, name_pad));
805
+ if (r2 == ARCHIVE_OK)
806
+ return (r);
807
+ else
808
+ return (r2);
809
+ }
810
+
811
+ /* Read fixed-size portion of header. */
812
+ h = __archive_read_ahead(a, odc_header_size, NULL);
813
+ if (h == NULL)
814
+ return (ARCHIVE_FATAL);
815
+
816
+ /* Parse out octal fields. */
817
+ header = (const char *)h;
818
+
819
+ archive_entry_set_dev(entry,
820
+ (dev_t)atol8(header + odc_dev_offset, odc_dev_size));
821
+ archive_entry_set_ino(entry, atol8(header + odc_ino_offset, odc_ino_size));
822
+ archive_entry_set_mode(entry,
823
+ (mode_t)atol8(header + odc_mode_offset, odc_mode_size));
824
+ archive_entry_set_uid(entry, atol8(header + odc_uid_offset, odc_uid_size));
825
+ archive_entry_set_gid(entry, atol8(header + odc_gid_offset, odc_gid_size));
826
+ archive_entry_set_nlink(entry,
827
+ (unsigned int)atol8(header + odc_nlink_offset, odc_nlink_size));
828
+ archive_entry_set_rdev(entry,
829
+ (dev_t)atol8(header + odc_rdev_offset, odc_rdev_size));
830
+ archive_entry_set_mtime(entry, atol8(header + odc_mtime_offset, odc_mtime_size), 0);
831
+ *namelength = (size_t)atol8(header + odc_namesize_offset, odc_namesize_size);
832
+ *name_pad = 0; /* No padding of filename. */
833
+
834
+ /*
835
+ * Note: entry_bytes_remaining is at least 64 bits and
836
+ * therefore guaranteed to be big enough for a 33-bit file
837
+ * size.
838
+ */
839
+ cpio->entry_bytes_remaining =
840
+ atol8(header + odc_filesize_offset, odc_filesize_size);
841
+ archive_entry_set_size(entry, cpio->entry_bytes_remaining);
842
+ cpio->entry_padding = 0;
843
+ __archive_read_consume(a, odc_header_size);
844
+ return (r);
845
+ }
846
+
847
+ /*
848
+ * NOTE: if a filename suffix is ".z", it is a file gzipped by afio.
849
+ * it would be nice if we could show uncompressed file size and
850
+ * uncompress file contents automatically, unfortunately we have nothing
851
+ * to get an uncompressed file size while reading each header. It means
852
+ * we also cannot uncompress file contents under our framework.
853
+ */
854
+ static int
855
+ header_afiol(struct archive_read *a, struct cpio *cpio,
856
+ struct archive_entry *entry, size_t *namelength, size_t *name_pad)
857
+ {
858
+ int64_t t;
859
+ uint64_t u;
860
+ const void *h;
861
+ const char *header;
862
+
863
+ a->archive.archive_format = ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
864
+ a->archive.archive_format_name = "afio large ASCII";
865
+
866
+ /* Read fixed-size portion of header. */
867
+ h = __archive_read_ahead(a, afiol_header_size, NULL);
868
+ if (h == NULL)
869
+ return (ARCHIVE_FATAL);
870
+
871
+ /* Parse out octal fields. */
872
+ header = (const char *)h;
873
+
874
+ archive_entry_set_dev(entry,
875
+ (dev_t)atol16(header + afiol_dev_offset, afiol_dev_size));
876
+ u = atol16u(header + afiol_ino_offset, afiol_ino_size);
877
+ #if ARCHIVE_VERSION_NUMBER < 4000000
878
+ archive_entry_set_ino(entry, (int64_t)(u & INT64_MAX));
879
+ #else
880
+ archive_entry_set_ino(entry, u);
881
+ #endif
882
+ archive_entry_set_mode(entry,
883
+ (mode_t)atol8(header + afiol_mode_offset, afiol_mode_size));
884
+ archive_entry_set_uid(entry, atol16(header + afiol_uid_offset, afiol_uid_size));
885
+ archive_entry_set_gid(entry, atol16(header + afiol_gid_offset, afiol_gid_size));
886
+ archive_entry_set_nlink(entry,
887
+ (unsigned int)atol16(header + afiol_nlink_offset, afiol_nlink_size));
888
+ archive_entry_set_rdev(entry,
889
+ (dev_t)atol16(header + afiol_rdev_offset, afiol_rdev_size));
890
+ archive_entry_set_mtime(entry, atol16(header + afiol_mtime_offset, afiol_mtime_size), 0);
891
+ *namelength = (size_t)atol16(header + afiol_namesize_offset, afiol_namesize_size);
892
+ *name_pad = 0; /* No padding of filename. */
893
+
894
+ t = atol16(header + afiol_filesize_offset, afiol_filesize_size);
895
+ if (t < 0) {
896
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
897
+ "Nonsensical file size");
898
+ return (ARCHIVE_FATAL);
899
+ }
900
+ cpio->entry_bytes_remaining = t;
901
+ archive_entry_set_size(entry, cpio->entry_bytes_remaining);
902
+ cpio->entry_padding = 0;
903
+ __archive_read_consume(a, afiol_header_size);
904
+ return (ARCHIVE_OK);
905
+ }
906
+
907
+
908
+ static int
909
+ header_bin_le(struct archive_read *a, struct cpio *cpio,
910
+ struct archive_entry *entry, size_t *namelength, size_t *name_pad)
911
+ {
912
+ const void *h;
913
+ const unsigned char *header;
914
+
915
+ a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_LE;
916
+ a->archive.archive_format_name = "cpio (little-endian binary)";
917
+
918
+ /* Read fixed-size portion of header. */
919
+ h = __archive_read_ahead(a, bin_header_size, NULL);
920
+ if (h == NULL) {
921
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
922
+ "End of file trying to read next cpio header");
923
+ return (ARCHIVE_FATAL);
924
+ }
925
+
926
+ /* Parse out binary fields. */
927
+ header = (const unsigned char *)h;
928
+
929
+ archive_entry_set_dev(entry, archive_le16dec(header + bin_dev_offset));
930
+ archive_entry_set_ino(entry, archive_le16dec(header + bin_ino_offset));
931
+ archive_entry_set_mode(entry, archive_le16dec(header + bin_mode_offset));
932
+ if (cpio->option_pwb) {
933
+ /* turn off random bits left over from V6 inode */
934
+ archive_entry_set_mode(entry, archive_entry_mode(entry) & 067777);
935
+ if ((archive_entry_mode(entry) & AE_IFMT) == 0)
936
+ archive_entry_set_mode(entry, archive_entry_mode(entry) | AE_IFREG);
937
+ }
938
+ archive_entry_set_uid(entry, archive_le16dec(header + bin_uid_offset));
939
+ archive_entry_set_gid(entry, archive_le16dec(header + bin_gid_offset));
940
+ archive_entry_set_nlink(entry, archive_le16dec(header + bin_nlink_offset));
941
+ archive_entry_set_rdev(entry, archive_le16dec(header + bin_rdev_offset));
942
+ archive_entry_set_mtime(entry, le32dec(header + bin_mtime_offset), 0);
943
+ *namelength = archive_le16dec(header + bin_namesize_offset);
944
+ *name_pad = *namelength & 1; /* Pad to even. */
945
+
946
+ cpio->entry_bytes_remaining = le32dec(header + bin_filesize_offset);
947
+ archive_entry_set_size(entry, cpio->entry_bytes_remaining);
948
+ cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
949
+ __archive_read_consume(a, bin_header_size);
950
+ return (ARCHIVE_OK);
951
+ }
952
+
953
+ static int
954
+ header_bin_be(struct archive_read *a, struct cpio *cpio,
955
+ struct archive_entry *entry, size_t *namelength, size_t *name_pad)
956
+ {
957
+ const void *h;
958
+ const unsigned char *header;
959
+
960
+ a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_BE;
961
+ a->archive.archive_format_name = "cpio (big-endian binary)";
962
+
963
+ /* Read fixed-size portion of header. */
964
+ h = __archive_read_ahead(a, bin_header_size, NULL);
965
+ if (h == NULL) {
966
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
967
+ "End of file trying to read next cpio header");
968
+ return (ARCHIVE_FATAL);
969
+ }
970
+
971
+ /* Parse out binary fields. */
972
+ header = (const unsigned char *)h;
973
+
974
+ archive_entry_set_dev(entry, archive_be16dec(header + bin_dev_offset));
975
+ archive_entry_set_ino(entry, archive_be16dec(header + bin_ino_offset));
976
+ archive_entry_set_mode(entry, archive_be16dec(header + bin_mode_offset));
977
+ if (cpio->option_pwb) {
978
+ /* turn off random bits left over from V6 inode */
979
+ archive_entry_set_mode(entry, archive_entry_mode(entry) & 067777);
980
+ if ((archive_entry_mode(entry) & AE_IFMT) == 0)
981
+ archive_entry_set_mode(entry, archive_entry_mode(entry) | AE_IFREG);
982
+ }
983
+ archive_entry_set_uid(entry, archive_be16dec(header + bin_uid_offset));
984
+ archive_entry_set_gid(entry, archive_be16dec(header + bin_gid_offset));
985
+ archive_entry_set_nlink(entry, archive_be16dec(header + bin_nlink_offset));
986
+ archive_entry_set_rdev(entry, archive_be16dec(header + bin_rdev_offset));
987
+ archive_entry_set_mtime(entry, be32dec(header + bin_mtime_offset), 0);
988
+ *namelength = archive_be16dec(header + bin_namesize_offset);
989
+ *name_pad = *namelength & 1; /* Pad to even. */
990
+
991
+ cpio->entry_bytes_remaining = be32dec(header + bin_filesize_offset);
992
+ archive_entry_set_size(entry, cpio->entry_bytes_remaining);
993
+ cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
994
+ __archive_read_consume(a, bin_header_size);
995
+ return (ARCHIVE_OK);
996
+ }
997
+
998
+ static int
999
+ archive_read_format_cpio_cleanup(struct archive_read *a)
1000
+ {
1001
+ struct cpio *cpio = a->format->data;
1002
+
1003
+ /* Free inode->name map */
1004
+ while (cpio->links_head != NULL) {
1005
+ struct links_entry *lp = cpio->links_head->next;
1006
+
1007
+ free(cpio->links_head->name);
1008
+ free(cpio->links_head);
1009
+ cpio->links_head = lp;
1010
+ }
1011
+ free(cpio);
1012
+ a->format->data = NULL;
1013
+ return (ARCHIVE_OK);
1014
+ }
1015
+
1016
+ static int64_t
1017
+ le32dec(const unsigned char *p)
1018
+ {
1019
+ return ((int64_t)archive_le16dec(p) << 16) | archive_le16dec(p + 2);
1020
+ }
1021
+
1022
+ static int64_t
1023
+ be32dec(const unsigned char *p)
1024
+ {
1025
+ return ((int64_t)archive_be16dec(p) << 16) | archive_be16dec(p + 2);
1026
+ }
1027
+
1028
+ /*
1029
+ * Note that this implementation does not (and should not!) obey
1030
+ * locale settings; you cannot simply substitute strtol here, since
1031
+ * it does obey locale.
1032
+ */
1033
+ static int64_t
1034
+ atol8(const char *p, unsigned char_cnt)
1035
+ {
1036
+ uint64_t l;
1037
+ int digit;
1038
+
1039
+ l = 0;
1040
+ while (char_cnt-- > 0) {
1041
+ if (*p >= '0' && *p <= '7')
1042
+ digit = *p - '0';
1043
+ else
1044
+ return ((int64_t)l);
1045
+ p++;
1046
+ l <<= 3;
1047
+ l |= digit;
1048
+ }
1049
+ return ((int64_t)l);
1050
+ }
1051
+
1052
+ static int64_t
1053
+ atol16(const char *p, unsigned char_cnt)
1054
+ {
1055
+ return ((int64_t)atol16u(p, char_cnt));
1056
+ }
1057
+
1058
+ static uint64_t
1059
+ atol16u(const char *p, unsigned char_cnt)
1060
+ {
1061
+ uint64_t l;
1062
+ int digit;
1063
+
1064
+ l = 0;
1065
+ while (char_cnt-- > 0) {
1066
+ if (*p >= 'a' && *p <= 'f')
1067
+ digit = *p - 'a' + 10;
1068
+ else if (*p >= 'A' && *p <= 'F')
1069
+ digit = *p - 'A' + 10;
1070
+ else if (*p >= '0' && *p <= '9')
1071
+ digit = *p - '0';
1072
+ else
1073
+ return (l);
1074
+ p++;
1075
+ l <<= 4;
1076
+ l |= digit;
1077
+ }
1078
+ return (l);
1079
+ }
1080
+
1081
+ static int
1082
+ record_hardlink(struct archive_read *a,
1083
+ struct cpio *cpio, struct archive_entry *entry)
1084
+ {
1085
+ struct links_entry *le;
1086
+ dev_t dev;
1087
+ int64_t ino;
1088
+
1089
+ if (archive_entry_nlink(entry) <= 1)
1090
+ return (ARCHIVE_OK);
1091
+
1092
+ dev = archive_entry_dev(entry);
1093
+ ino = archive_entry_ino64(entry);
1094
+
1095
+ /*
1096
+ * First look in the list of multiply-linked files. If we've
1097
+ * already dumped it, convert this entry to a hard link entry.
1098
+ */
1099
+ for (le = cpio->links_head; le; le = le->next) {
1100
+ if (le->dev == dev && le->ino == ino) {
1101
+ archive_entry_copy_hardlink(entry, le->name);
1102
+
1103
+ if (--le->links <= 0) {
1104
+ if (le->previous != NULL)
1105
+ le->previous->next = le->next;
1106
+ if (le->next != NULL)
1107
+ le->next->previous = le->previous;
1108
+ if (cpio->links_head == le)
1109
+ cpio->links_head = le->next;
1110
+ free(le->name);
1111
+ free(le);
1112
+ }
1113
+
1114
+ return (ARCHIVE_OK);
1115
+ }
1116
+ }
1117
+
1118
+ le = malloc(sizeof(struct links_entry));
1119
+ if (le == NULL) {
1120
+ archive_set_error(&a->archive,
1121
+ ENOMEM, "Out of memory adding file to list");
1122
+ return (ARCHIVE_FATAL);
1123
+ }
1124
+
1125
+ const char *pathname = archive_entry_pathname(entry);
1126
+ if (pathname == NULL) {
1127
+ archive_set_error(&a->archive,
1128
+ ARCHIVE_ERRNO_FILE_FORMAT,
1129
+ "Invalid hardlink entry with no pathname");
1130
+ free(le);
1131
+ return (ARCHIVE_FATAL);
1132
+ }
1133
+
1134
+ le->dev = dev;
1135
+ le->ino = ino;
1136
+ le->links = archive_entry_nlink(entry) - 1;
1137
+ le->name = strdup(pathname);
1138
+ if (le->name == NULL) {
1139
+ archive_set_error(&a->archive,
1140
+ ENOMEM, "Out of memory adding file to list");
1141
+ free(le);
1142
+ return (ARCHIVE_FATAL);
1143
+ }
1144
+
1145
+ if (cpio->links_head != NULL)
1146
+ cpio->links_head->previous = le;
1147
+ le->next = cpio->links_head;
1148
+ le->previous = NULL;
1149
+ cpio->links_head = le;
1150
+
1151
+ return (ARCHIVE_OK);
1152
+ }