hane 1.1.1 → 1.2.0

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 (379) hide show
  1. package/README.md +32 -32
  2. package/binding.gyp +50 -50
  3. package/deps/win-nodeapi/node_api.def +158 -158
  4. package/package.json +34 -34
  5. package/src/native/addon.cc +183 -183
  6. package/src/native/zsign_driver.cc +86 -86
  7. package/src/native/zsign_driver.h +38 -38
  8. package/vendor/zlib/.cmake-format.yaml +245 -245
  9. package/vendor/zlib/.github/workflows/c-std.yml +230 -230
  10. package/vendor/zlib/.github/workflows/cmake.yml +112 -112
  11. package/vendor/zlib/.github/workflows/configure.yml +136 -136
  12. package/vendor/zlib/.github/workflows/fuzz.yml +25 -25
  13. package/vendor/zlib/.github/workflows/msys-cygwin.yml +77 -77
  14. package/vendor/zlib/BUILD.bazel +134 -134
  15. package/vendor/zlib/CMakeLists.txt +330 -330
  16. package/vendor/zlib/ChangeLog +1621 -1621
  17. package/vendor/zlib/FAQ +367 -367
  18. package/vendor/zlib/INDEX +68 -68
  19. package/vendor/zlib/LICENSE +22 -22
  20. package/vendor/zlib/MODULE.bazel +9 -9
  21. package/vendor/zlib/Makefile.in +419 -419
  22. package/vendor/zlib/README +115 -115
  23. package/vendor/zlib/README-cmake.md +83 -83
  24. package/vendor/zlib/adler32.c +164 -164
  25. package/vendor/zlib/amiga/Makefile.pup +69 -69
  26. package/vendor/zlib/amiga/Makefile.sas +68 -68
  27. package/vendor/zlib/compress.c +75 -75
  28. package/vendor/zlib/configure +966 -966
  29. package/vendor/zlib/contrib/README.contrib +57 -57
  30. package/vendor/zlib/contrib/ada/buffer_demo.adb +106 -106
  31. package/vendor/zlib/contrib/ada/mtest.adb +156 -156
  32. package/vendor/zlib/contrib/ada/read.adb +156 -156
  33. package/vendor/zlib/contrib/ada/readme.txt +65 -65
  34. package/vendor/zlib/contrib/ada/test.adb +463 -463
  35. package/vendor/zlib/contrib/ada/zlib-streams.adb +225 -225
  36. package/vendor/zlib/contrib/ada/zlib-streams.ads +114 -114
  37. package/vendor/zlib/contrib/ada/zlib-thin.adb +142 -142
  38. package/vendor/zlib/contrib/ada/zlib-thin.ads +450 -450
  39. package/vendor/zlib/contrib/ada/zlib.adb +701 -701
  40. package/vendor/zlib/contrib/ada/zlib.ads +328 -328
  41. package/vendor/zlib/contrib/ada/zlib.gpr +20 -20
  42. package/vendor/zlib/contrib/blast/Makefile +8 -8
  43. package/vendor/zlib/contrib/blast/README +4 -4
  44. package/vendor/zlib/contrib/blast/blast.c +466 -466
  45. package/vendor/zlib/contrib/blast/blast.h +83 -83
  46. package/vendor/zlib/contrib/delphi/ZLib.pas +557 -557
  47. package/vendor/zlib/contrib/delphi/ZLibConst.pas +11 -11
  48. package/vendor/zlib/contrib/delphi/readme.txt +76 -76
  49. package/vendor/zlib/contrib/delphi/zlibd32.mak +99 -99
  50. package/vendor/zlib/contrib/gcc_gvmat64/gvmat64.S +574 -574
  51. package/vendor/zlib/contrib/infback9/README +1 -1
  52. package/vendor/zlib/contrib/infback9/infback9.c +603 -603
  53. package/vendor/zlib/contrib/infback9/infback9.h +37 -37
  54. package/vendor/zlib/contrib/infback9/inffix9.h +107 -107
  55. package/vendor/zlib/contrib/infback9/inflate9.h +47 -47
  56. package/vendor/zlib/contrib/infback9/inftree9.c +319 -319
  57. package/vendor/zlib/contrib/infback9/inftree9.h +61 -61
  58. package/vendor/zlib/contrib/iostream/test.cpp +24 -24
  59. package/vendor/zlib/contrib/iostream/zfstream.cpp +329 -329
  60. package/vendor/zlib/contrib/iostream/zfstream.h +128 -128
  61. package/vendor/zlib/contrib/iostream2/zstream.h +307 -307
  62. package/vendor/zlib/contrib/iostream2/zstream_test.cpp +25 -25
  63. package/vendor/zlib/contrib/iostream3/README +35 -35
  64. package/vendor/zlib/contrib/iostream3/TODO +17 -17
  65. package/vendor/zlib/contrib/iostream3/test.cc +50 -50
  66. package/vendor/zlib/contrib/iostream3/zfstream.cc +479 -479
  67. package/vendor/zlib/contrib/iostream3/zfstream.h +466 -466
  68. package/vendor/zlib/contrib/minizip/CMakeLists.txt +380 -380
  69. package/vendor/zlib/contrib/minizip/Makefile +37 -37
  70. package/vendor/zlib/contrib/minizip/Makefile.am +45 -45
  71. package/vendor/zlib/contrib/minizip/MiniZip64_Changes.txt +6 -6
  72. package/vendor/zlib/contrib/minizip/MiniZip64_info.txt +74 -74
  73. package/vendor/zlib/contrib/minizip/configure.ac +32 -32
  74. package/vendor/zlib/contrib/minizip/crypt.h +128 -128
  75. package/vendor/zlib/contrib/minizip/ints.h +57 -57
  76. package/vendor/zlib/contrib/minizip/ioapi.c +231 -231
  77. package/vendor/zlib/contrib/minizip/ioapi.h +183 -183
  78. package/vendor/zlib/contrib/minizip/iowin32.c +448 -448
  79. package/vendor/zlib/contrib/minizip/iowin32.h +28 -28
  80. package/vendor/zlib/contrib/minizip/make_vms.com +25 -25
  81. package/vendor/zlib/contrib/minizip/miniunz.c +647 -647
  82. package/vendor/zlib/contrib/minizip/miniunzip.1 +63 -63
  83. package/vendor/zlib/contrib/minizip/minizip.1 +46 -46
  84. package/vendor/zlib/contrib/minizip/minizip.c +512 -512
  85. package/vendor/zlib/contrib/minizip/minizip.pc.in +12 -12
  86. package/vendor/zlib/contrib/minizip/minizip.pc.txt +13 -13
  87. package/vendor/zlib/contrib/minizip/minizipConfig.cmake.in +35 -35
  88. package/vendor/zlib/contrib/minizip/mztools.c +288 -288
  89. package/vendor/zlib/contrib/minizip/mztools.h +37 -37
  90. package/vendor/zlib/contrib/minizip/skipset.h +361 -361
  91. package/vendor/zlib/contrib/minizip/test/CMakeLists.txt +121 -121
  92. package/vendor/zlib/contrib/minizip/test/add_subdirectory_exclude_test.cmake.in +29 -29
  93. package/vendor/zlib/contrib/minizip/test/add_subdirectory_test.cmake.in +28 -28
  94. package/vendor/zlib/contrib/minizip/test/find_package_test.cmake.in +25 -25
  95. package/vendor/zlib/contrib/minizip/test/test_helper.cm +32 -32
  96. package/vendor/zlib/contrib/minizip/unzip.c +1981 -1981
  97. package/vendor/zlib/contrib/minizip/unzip.h +441 -441
  98. package/vendor/zlib/contrib/minizip/zip.c +2199 -2199
  99. package/vendor/zlib/contrib/minizip/zip.h +370 -370
  100. package/vendor/zlib/contrib/nuget/nuget.csproj +43 -43
  101. package/vendor/zlib/contrib/nuget/nuget.sln +22 -22
  102. package/vendor/zlib/contrib/pascal/example.pas +599 -599
  103. package/vendor/zlib/contrib/pascal/readme.txt +76 -76
  104. package/vendor/zlib/contrib/pascal/zlibd32.mak +99 -99
  105. package/vendor/zlib/contrib/pascal/zlibpas.pas +276 -276
  106. package/vendor/zlib/contrib/puff/Makefile +42 -42
  107. package/vendor/zlib/contrib/puff/README +63 -63
  108. package/vendor/zlib/contrib/puff/puff.c +840 -840
  109. package/vendor/zlib/contrib/puff/puff.h +35 -35
  110. package/vendor/zlib/contrib/puff/pufftest.c +169 -169
  111. package/vendor/zlib/contrib/untgz/Makefile +14 -14
  112. package/vendor/zlib/contrib/untgz/Makefile.msc +17 -17
  113. package/vendor/zlib/contrib/untgz/untgz.c +667 -667
  114. package/vendor/zlib/contrib/vstudio/readme.txt +81 -81
  115. package/vendor/zlib/contrib/vstudio/vc12/miniunz.vcxproj +315 -315
  116. package/vendor/zlib/contrib/vstudio/vc12/minizip.vcxproj +312 -312
  117. package/vendor/zlib/contrib/vstudio/vc12/testzlib.vcxproj +421 -421
  118. package/vendor/zlib/contrib/vstudio/vc12/testzlibdll.vcxproj +315 -315
  119. package/vendor/zlib/contrib/vstudio/vc12/zlib.rc +32 -32
  120. package/vendor/zlib/contrib/vstudio/vc12/zlibstat.vcxproj +458 -458
  121. package/vendor/zlib/contrib/vstudio/vc12/zlibvc.sln +119 -119
  122. package/vendor/zlib/contrib/vstudio/vc12/zlibvc.vcxproj +667 -667
  123. package/vendor/zlib/contrib/vstudio/vc14/miniunz.vcxproj +315 -315
  124. package/vendor/zlib/contrib/vstudio/vc14/minizip.vcxproj +312 -312
  125. package/vendor/zlib/contrib/vstudio/vc14/testzlib.vcxproj +421 -421
  126. package/vendor/zlib/contrib/vstudio/vc14/testzlibdll.vcxproj +315 -315
  127. package/vendor/zlib/contrib/vstudio/vc14/zlib.rc +32 -32
  128. package/vendor/zlib/contrib/vstudio/vc14/zlibstat.vcxproj +458 -458
  129. package/vendor/zlib/contrib/vstudio/vc14/zlibvc.sln +119 -119
  130. package/vendor/zlib/contrib/vstudio/vc14/zlibvc.vcxproj +667 -667
  131. package/vendor/zlib/contrib/vstudio/vc17/miniunz.vcxproj +408 -408
  132. package/vendor/zlib/contrib/vstudio/vc17/minizip.vcxproj +404 -404
  133. package/vendor/zlib/contrib/vstudio/vc17/testzlib.vcxproj +472 -472
  134. package/vendor/zlib/contrib/vstudio/vc17/testzlibdll.vcxproj +408 -408
  135. package/vendor/zlib/contrib/vstudio/vc17/zlib.rc +32 -32
  136. package/vendor/zlib/contrib/vstudio/vc17/zlibstat.vcxproj +601 -601
  137. package/vendor/zlib/contrib/vstudio/vc17/zlibvc.sln +179 -179
  138. package/vendor/zlib/contrib/vstudio/vc17/zlibvc.vcxproj +874 -874
  139. package/vendor/zlib/crc32.c +1049 -1049
  140. package/vendor/zlib/crc32.h +9446 -9446
  141. package/vendor/zlib/deflate.c +2152 -2152
  142. package/vendor/zlib/deflate.h +380 -380
  143. package/vendor/zlib/doc/algorithm.txt +209 -209
  144. package/vendor/zlib/doc/rfc1950.txt +619 -619
  145. package/vendor/zlib/doc/rfc1951.txt +955 -955
  146. package/vendor/zlib/doc/rfc1952.txt +675 -675
  147. package/vendor/zlib/doc/txtvsbin.txt +107 -107
  148. package/vendor/zlib/examples/README.examples +54 -54
  149. package/vendor/zlib/examples/enough.c +597 -597
  150. package/vendor/zlib/examples/fitblk.c +233 -233
  151. package/vendor/zlib/examples/gun.c +702 -702
  152. package/vendor/zlib/examples/gzappend.c +504 -504
  153. package/vendor/zlib/examples/gzjoin.c +449 -449
  154. package/vendor/zlib/examples/gzlog.c +1061 -1061
  155. package/vendor/zlib/examples/gzlog.h +91 -91
  156. package/vendor/zlib/examples/gznorm.c +474 -474
  157. package/vendor/zlib/examples/zlib_how.html +549 -549
  158. package/vendor/zlib/examples/zpipe.c +209 -209
  159. package/vendor/zlib/examples/zran.c +550 -550
  160. package/vendor/zlib/examples/zran.h +53 -53
  161. package/vendor/zlib/gzclose.c +23 -23
  162. package/vendor/zlib/gzguts.h +215 -215
  163. package/vendor/zlib/gzlib.c +585 -585
  164. package/vendor/zlib/gzread.c +603 -603
  165. package/vendor/zlib/gzwrite.c +631 -631
  166. package/vendor/zlib/infback.c +628 -628
  167. package/vendor/zlib/inffast.c +320 -320
  168. package/vendor/zlib/inffast.h +11 -11
  169. package/vendor/zlib/inffixed.h +94 -94
  170. package/vendor/zlib/inflate.c +1526 -1526
  171. package/vendor/zlib/inflate.h +126 -126
  172. package/vendor/zlib/inftrees.c +299 -299
  173. package/vendor/zlib/inftrees.h +62 -62
  174. package/vendor/zlib/make_vms.com +867 -867
  175. package/vendor/zlib/msdos/Makefile.bor +115 -115
  176. package/vendor/zlib/msdos/Makefile.dj2 +104 -104
  177. package/vendor/zlib/msdos/Makefile.emx +69 -69
  178. package/vendor/zlib/msdos/Makefile.msc +112 -112
  179. package/vendor/zlib/msdos/Makefile.tc +100 -100
  180. package/vendor/zlib/nintendods/Makefile +126 -126
  181. package/vendor/zlib/nintendods/README +5 -5
  182. package/vendor/zlib/old/Makefile.emx +69 -69
  183. package/vendor/zlib/old/Makefile.riscos +151 -151
  184. package/vendor/zlib/old/README +3 -3
  185. package/vendor/zlib/old/descrip.mms +48 -48
  186. package/vendor/zlib/old/os2/Makefile.os2 +136 -136
  187. package/vendor/zlib/old/os2/zlib.def +51 -51
  188. package/vendor/zlib/old/visual-basic.txt +160 -160
  189. package/vendor/zlib/os400/README400 +48 -48
  190. package/vendor/zlib/os400/bndsrc +133 -133
  191. package/vendor/zlib/os400/make.sh +366 -366
  192. package/vendor/zlib/os400/zlib.inc +531 -531
  193. package/vendor/zlib/qnx/package.qpg +141 -141
  194. package/vendor/zlib/test/CMakeLists.txt +265 -265
  195. package/vendor/zlib/test/add_subdirectory_exclude_test.cmake.in +29 -29
  196. package/vendor/zlib/test/add_subdirectory_test.cmake.in +28 -28
  197. package/vendor/zlib/test/example.c +552 -552
  198. package/vendor/zlib/test/find_package_test.cmake.in +26 -26
  199. package/vendor/zlib/test/infcover.c +672 -672
  200. package/vendor/zlib/test/minigzip.c +590 -590
  201. package/vendor/zlib/treebuild.xml +116 -116
  202. package/vendor/zlib/trees.c +1119 -1119
  203. package/vendor/zlib/trees.h +128 -128
  204. package/vendor/zlib/uncompr.c +85 -85
  205. package/vendor/zlib/watcom/watcom_f.mak +43 -43
  206. package/vendor/zlib/watcom/watcom_l.mak +43 -43
  207. package/vendor/zlib/win32/DLL_FAQ.txt +381 -381
  208. package/vendor/zlib/win32/Makefile.bor +109 -109
  209. package/vendor/zlib/win32/Makefile.gcc +177 -177
  210. package/vendor/zlib/win32/Makefile.msc +159 -159
  211. package/vendor/zlib/win32/README-WIN32.txt +103 -103
  212. package/vendor/zlib/win32/VisualC.txt +3 -3
  213. package/vendor/zlib/win32/zlib1.rc +37 -37
  214. package/vendor/zlib/zconf.h.in +544 -544
  215. package/vendor/zlib/zlib.3 +149 -149
  216. package/vendor/zlib/zlib.h +1957 -1957
  217. package/vendor/zlib/zlib.map +103 -103
  218. package/vendor/zlib/zlib.pc.cmakein +13 -13
  219. package/vendor/zlib/zlib.pc.in +13 -13
  220. package/vendor/zlib/zlibConfig.cmake.in +26 -26
  221. package/vendor/zlib/zutil.c +299 -299
  222. package/vendor/zlib/zutil.h +257 -257
  223. package/vendor/zsign/.gitattributes +3 -3
  224. package/vendor/zsign/LICENSE +20 -20
  225. package/vendor/zsign/README.md +142 -142
  226. package/vendor/zsign/build/linux/Makefile +43 -43
  227. package/vendor/zsign/build/macos/Makefile +43 -43
  228. package/vendor/zsign/build/windows/vs2022/include/minizip/crypt.h +128 -128
  229. package/vendor/zsign/build/windows/vs2022/include/minizip/ioapi.h +216 -216
  230. package/vendor/zsign/build/windows/vs2022/include/minizip/iowin32.h +28 -28
  231. package/vendor/zsign/build/windows/vs2022/include/minizip/mztools.h +37 -37
  232. package/vendor/zsign/build/windows/vs2022/include/minizip/unzip.h +437 -437
  233. package/vendor/zsign/build/windows/vs2022/include/minizip/zip.h +364 -364
  234. package/vendor/zsign/build/windows/vs2022/include/openssl/__DECC_INCLUDE_EPILOGUE.H +22 -22
  235. package/vendor/zsign/build/windows/vs2022/include/openssl/__DECC_INCLUDE_PROLOGUE.H +26 -26
  236. package/vendor/zsign/build/windows/vs2022/include/openssl/aes.h +111 -111
  237. package/vendor/zsign/build/windows/vs2022/include/openssl/applink.c +153 -153
  238. package/vendor/zsign/build/windows/vs2022/include/openssl/asn1_mac.h +10 -10
  239. package/vendor/zsign/build/windows/vs2022/include/openssl/asn1err.h +142 -142
  240. package/vendor/zsign/build/windows/vs2022/include/openssl/async.h +104 -104
  241. package/vendor/zsign/build/windows/vs2022/include/openssl/asyncerr.h +29 -29
  242. package/vendor/zsign/build/windows/vs2022/include/openssl/bioerr.h +72 -72
  243. package/vendor/zsign/build/windows/vs2022/include/openssl/blowfish.h +78 -78
  244. package/vendor/zsign/build/windows/vs2022/include/openssl/bn.h +590 -590
  245. package/vendor/zsign/build/windows/vs2022/include/openssl/bnerr.h +47 -47
  246. package/vendor/zsign/build/windows/vs2022/include/openssl/buffer.h +62 -62
  247. package/vendor/zsign/build/windows/vs2022/include/openssl/buffererr.h +25 -25
  248. package/vendor/zsign/build/windows/vs2022/include/openssl/camellia.h +117 -117
  249. package/vendor/zsign/build/windows/vs2022/include/openssl/cast.h +71 -71
  250. package/vendor/zsign/build/windows/vs2022/include/openssl/cmac.h +52 -52
  251. package/vendor/zsign/build/windows/vs2022/include/openssl/cmp_util.h +56 -56
  252. package/vendor/zsign/build/windows/vs2022/include/openssl/cmperr.h +131 -131
  253. package/vendor/zsign/build/windows/vs2022/include/openssl/cmserr.h +125 -125
  254. package/vendor/zsign/build/windows/vs2022/include/openssl/comperr.h +38 -38
  255. package/vendor/zsign/build/windows/vs2022/include/openssl/conf_api.h +46 -46
  256. package/vendor/zsign/build/windows/vs2022/include/openssl/conferr.h +52 -52
  257. package/vendor/zsign/build/windows/vs2022/include/openssl/conftypes.h +44 -44
  258. package/vendor/zsign/build/windows/vs2022/include/openssl/core.h +236 -236
  259. package/vendor/zsign/build/windows/vs2022/include/openssl/core_dispatch.h +1022 -1022
  260. package/vendor/zsign/build/windows/vs2022/include/openssl/core_object.h +41 -41
  261. package/vendor/zsign/build/windows/vs2022/include/openssl/crmferr.h +50 -50
  262. package/vendor/zsign/build/windows/vs2022/include/openssl/cryptoerr.h +56 -56
  263. package/vendor/zsign/build/windows/vs2022/include/openssl/cryptoerr_legacy.h +1466 -1466
  264. package/vendor/zsign/build/windows/vs2022/include/openssl/cterr.h +45 -45
  265. package/vendor/zsign/build/windows/vs2022/include/openssl/decoder.h +133 -133
  266. package/vendor/zsign/build/windows/vs2022/include/openssl/decodererr.h +28 -28
  267. package/vendor/zsign/build/windows/vs2022/include/openssl/des.h +211 -211
  268. package/vendor/zsign/build/windows/vs2022/include/openssl/dh.h +339 -339
  269. package/vendor/zsign/build/windows/vs2022/include/openssl/dherr.h +59 -59
  270. package/vendor/zsign/build/windows/vs2022/include/openssl/dsa.h +280 -280
  271. package/vendor/zsign/build/windows/vs2022/include/openssl/dsaerr.h +44 -44
  272. package/vendor/zsign/build/windows/vs2022/include/openssl/dtls1.h +57 -57
  273. package/vendor/zsign/build/windows/vs2022/include/openssl/e_os2.h +310 -310
  274. package/vendor/zsign/build/windows/vs2022/include/openssl/e_ostime.h +38 -38
  275. package/vendor/zsign/build/windows/vs2022/include/openssl/ebcdic.h +39 -39
  276. package/vendor/zsign/build/windows/vs2022/include/openssl/ec.h +1588 -1588
  277. package/vendor/zsign/build/windows/vs2022/include/openssl/ecdh.h +10 -10
  278. package/vendor/zsign/build/windows/vs2022/include/openssl/ecdsa.h +10 -10
  279. package/vendor/zsign/build/windows/vs2022/include/openssl/ecerr.h +104 -104
  280. package/vendor/zsign/build/windows/vs2022/include/openssl/encoder.h +124 -124
  281. package/vendor/zsign/build/windows/vs2022/include/openssl/encodererr.h +28 -28
  282. package/vendor/zsign/build/windows/vs2022/include/openssl/engine.h +833 -833
  283. package/vendor/zsign/build/windows/vs2022/include/openssl/engineerr.h +63 -63
  284. package/vendor/zsign/build/windows/vs2022/include/openssl/esserr.h +32 -32
  285. package/vendor/zsign/build/windows/vs2022/include/openssl/evp.h +2231 -2231
  286. package/vendor/zsign/build/windows/vs2022/include/openssl/evperr.h +140 -140
  287. package/vendor/zsign/build/windows/vs2022/include/openssl/fips_names.h +50 -50
  288. package/vendor/zsign/build/windows/vs2022/include/openssl/hmac.h +62 -62
  289. package/vendor/zsign/build/windows/vs2022/include/openssl/hpke.h +169 -169
  290. package/vendor/zsign/build/windows/vs2022/include/openssl/http.h +118 -118
  291. package/vendor/zsign/build/windows/vs2022/include/openssl/httperr.h +56 -56
  292. package/vendor/zsign/build/windows/vs2022/include/openssl/idea.h +82 -82
  293. package/vendor/zsign/build/windows/vs2022/include/openssl/indicator.h +31 -31
  294. package/vendor/zsign/build/windows/vs2022/include/openssl/kdf.h +138 -138
  295. package/vendor/zsign/build/windows/vs2022/include/openssl/kdferr.h +16 -16
  296. package/vendor/zsign/build/windows/vs2022/include/openssl/macros.h +338 -338
  297. package/vendor/zsign/build/windows/vs2022/include/openssl/md2.h +56 -56
  298. package/vendor/zsign/build/windows/vs2022/include/openssl/md4.h +63 -63
  299. package/vendor/zsign/build/windows/vs2022/include/openssl/md5.h +62 -62
  300. package/vendor/zsign/build/windows/vs2022/include/openssl/mdc2.h +55 -55
  301. package/vendor/zsign/build/windows/vs2022/include/openssl/modes.h +219 -219
  302. package/vendor/zsign/build/windows/vs2022/include/openssl/obj_mac.h +5820 -5820
  303. package/vendor/zsign/build/windows/vs2022/include/openssl/objects.h +184 -184
  304. package/vendor/zsign/build/windows/vs2022/include/openssl/objectserr.h +28 -28
  305. package/vendor/zsign/build/windows/vs2022/include/openssl/ocsperr.h +53 -53
  306. package/vendor/zsign/build/windows/vs2022/include/openssl/opensslconf.h +17 -17
  307. package/vendor/zsign/build/windows/vs2022/include/openssl/ossl_typ.h +16 -16
  308. package/vendor/zsign/build/windows/vs2022/include/openssl/param_build.h +63 -63
  309. package/vendor/zsign/build/windows/vs2022/include/openssl/params.h +160 -160
  310. package/vendor/zsign/build/windows/vs2022/include/openssl/pem.h +543 -543
  311. package/vendor/zsign/build/windows/vs2022/include/openssl/pem2.h +19 -19
  312. package/vendor/zsign/build/windows/vs2022/include/openssl/pemerr.h +58 -58
  313. package/vendor/zsign/build/windows/vs2022/include/openssl/pkcs12err.h +46 -46
  314. package/vendor/zsign/build/windows/vs2022/include/openssl/pkcs7err.h +63 -63
  315. package/vendor/zsign/build/windows/vs2022/include/openssl/prov_ssl.h +38 -38
  316. package/vendor/zsign/build/windows/vs2022/include/openssl/proverr.h +162 -162
  317. package/vendor/zsign/build/windows/vs2022/include/openssl/provider.h +66 -66
  318. package/vendor/zsign/build/windows/vs2022/include/openssl/quic.h +70 -70
  319. package/vendor/zsign/build/windows/vs2022/include/openssl/rand.h +125 -125
  320. package/vendor/zsign/build/windows/vs2022/include/openssl/randerr.h +69 -69
  321. package/vendor/zsign/build/windows/vs2022/include/openssl/rc2.h +68 -68
  322. package/vendor/zsign/build/windows/vs2022/include/openssl/rc4.h +47 -47
  323. package/vendor/zsign/build/windows/vs2022/include/openssl/rc5.h +79 -79
  324. package/vendor/zsign/build/windows/vs2022/include/openssl/ripemd.h +59 -59
  325. package/vendor/zsign/build/windows/vs2022/include/openssl/rsa.h +615 -615
  326. package/vendor/zsign/build/windows/vs2022/include/openssl/rsaerr.h +107 -107
  327. package/vendor/zsign/build/windows/vs2022/include/openssl/seed.h +113 -113
  328. package/vendor/zsign/build/windows/vs2022/include/openssl/self_test.h +98 -98
  329. package/vendor/zsign/build/windows/vs2022/include/openssl/sha.h +139 -139
  330. package/vendor/zsign/build/windows/vs2022/include/openssl/srtp.h +68 -68
  331. package/vendor/zsign/build/windows/vs2022/include/openssl/ssl2.h +30 -30
  332. package/vendor/zsign/build/windows/vs2022/include/openssl/ssl3.h +357 -357
  333. package/vendor/zsign/build/windows/vs2022/include/openssl/sslerr.h +379 -379
  334. package/vendor/zsign/build/windows/vs2022/include/openssl/sslerr_legacy.h +467 -467
  335. package/vendor/zsign/build/windows/vs2022/include/openssl/stack.h +90 -90
  336. package/vendor/zsign/build/windows/vs2022/include/openssl/store.h +377 -377
  337. package/vendor/zsign/build/windows/vs2022/include/openssl/storeerr.h +49 -49
  338. package/vendor/zsign/build/windows/vs2022/include/openssl/symhacks.h +39 -39
  339. package/vendor/zsign/build/windows/vs2022/include/openssl/thread.h +31 -31
  340. package/vendor/zsign/build/windows/vs2022/include/openssl/tls1.h +1220 -1220
  341. package/vendor/zsign/build/windows/vs2022/include/openssl/trace.h +320 -320
  342. package/vendor/zsign/build/windows/vs2022/include/openssl/ts.h +522 -522
  343. package/vendor/zsign/build/windows/vs2022/include/openssl/tserr.h +67 -67
  344. package/vendor/zsign/build/windows/vs2022/include/openssl/txt_db.h +63 -63
  345. package/vendor/zsign/build/windows/vs2022/include/openssl/types.h +245 -245
  346. package/vendor/zsign/build/windows/vs2022/include/openssl/uierr.h +38 -38
  347. package/vendor/zsign/build/windows/vs2022/include/openssl/whrlpool.h +62 -62
  348. package/vendor/zsign/build/windows/vs2022/include/openssl/x509err.h +70 -70
  349. package/vendor/zsign/build/windows/vs2022/include/openssl/x509v3err.h +96 -96
  350. package/vendor/zsign/src/archo.cpp +742 -742
  351. package/vendor/zsign/src/archo.h +61 -61
  352. package/vendor/zsign/src/bundle.cpp +589 -589
  353. package/vendor/zsign/src/bundle.h +46 -46
  354. package/vendor/zsign/src/common/archive.cpp +246 -246
  355. package/vendor/zsign/src/common/archive.h +22 -22
  356. package/vendor/zsign/src/common/common.h +56 -56
  357. package/vendor/zsign/src/common/fs.cpp +573 -573
  358. package/vendor/zsign/src/common/fs.h +50 -50
  359. package/vendor/zsign/src/common/log.cpp +145 -145
  360. package/vendor/zsign/src/common/log.h +37 -37
  361. package/vendor/zsign/src/common/mach-o.h +585 -585
  362. package/vendor/zsign/src/common/sha.cpp +133 -133
  363. package/vendor/zsign/src/common/sha.h +24 -24
  364. package/vendor/zsign/src/common/timer.cpp +28 -28
  365. package/vendor/zsign/src/common/timer.h +17 -17
  366. package/vendor/zsign/src/common/util.cpp +185 -185
  367. package/vendor/zsign/src/common/util.h +25 -25
  368. package/vendor/zsign/src/macho.cpp +273 -273
  369. package/vendor/zsign/src/macho.h +38 -38
  370. package/vendor/zsign/src/openssl.cpp +698 -698
  371. package/vendor/zsign/src/openssl.h +71 -71
  372. package/vendor/zsign/src/signing.cpp +745 -745
  373. package/vendor/zsign/src/signing.h +59 -59
  374. package/vendor/zsign/src/zsign.cpp +317 -317
  375. package/vendor/zsign/test/dylib/demo/Makefile +12 -12
  376. package/vendor/zsign/test/dylib/demo/control +9 -9
  377. package/vendor/zsign/test/dylib/demo/demo.m +21 -21
  378. package/vendor/zsign/test/linux/test.sh +19 -19
  379. package/vendor/zsign/test/macos/test.sh +19 -19
@@ -1,550 +1,550 @@
1
- /* zran.c -- example of deflate stream indexing and random access
2
- * Copyright (C) 2005, 2012, 2018, 2023, 2024 Mark Adler
3
- * For conditions of distribution and use, see copyright notice in zlib.h
4
- * Version 1.6 2 Aug 2024 Mark Adler */
5
-
6
- /* Version History:
7
- 1.0 29 May 2005 First version
8
- 1.1 29 Sep 2012 Fix memory reallocation error
9
- 1.2 14 Oct 2018 Handle gzip streams with multiple members
10
- Add a header file to facilitate usage in applications
11
- 1.3 18 Feb 2023 Permit raw deflate streams as well as zlib and gzip
12
- Permit crossing gzip member boundaries when extracting
13
- Support a size_t size when extracting (was an int)
14
- Do a binary search over the index for an access point
15
- Expose the access point type to enable save and load
16
- 1.4 13 Apr 2023 Add a NOPRIME define to not use inflatePrime()
17
- 1.5 4 Feb 2024 Set returned index to NULL on an index build error
18
- Stop decoding once request is satisfied
19
- Provide a reusable inflate engine in the index
20
- Allocate the dictionaries to reduce memory usage
21
- 1.6 2 Aug 2024 Remove unneeded dependency on limits.h
22
- */
23
-
24
- // Illustrate the use of Z_BLOCK, inflatePrime(), and inflateSetDictionary()
25
- // for random access of a compressed file. A file containing a raw deflate
26
- // stream is provided on the command line. The compressed stream is decoded in
27
- // its entirety, and an index built with access points about every SPAN bytes
28
- // in the uncompressed output. The compressed file is left open, and can then
29
- // be read randomly, having to decompress on the average SPAN/2 uncompressed
30
- // bytes before getting to the desired block of data.
31
- //
32
- // An access point can be created at the start of any deflate block, by saving
33
- // the starting file offset and bit of that block, and the 32K bytes of
34
- // uncompressed data that precede that block. Also the uncompressed offset of
35
- // that block is saved to provide a reference for locating a desired starting
36
- // point in the uncompressed stream. deflate_index_build() decompresses the
37
- // input raw deflate stream a block at a time, and at the end of each block
38
- // decides if enough uncompressed data has gone by to justify the creation of a
39
- // new access point. If so, that point is saved in a data structure that grows
40
- // as needed to accommodate the points.
41
- //
42
- // To use the index, an offset in the uncompressed data is provided, for which
43
- // the latest access point at or preceding that offset is located in the index.
44
- // The input file is positioned to the specified location in the index, and if
45
- // necessary the first few bits of the compressed data is read from the file.
46
- // inflate is initialized with those bits and the 32K of uncompressed data, and
47
- // decompression then proceeds until the desired offset in the file is reached.
48
- // Then decompression continues to read the requested uncompressed data from
49
- // the file.
50
- //
51
- // There is some fair bit of overhead to starting inflation for the random
52
- // access, mainly copying the 32K byte dictionary. If small pieces of the file
53
- // are being accessed, it would make sense to implement a cache to hold some
54
- // lookahead to avoid many calls to deflate_index_extract() for small lengths.
55
- //
56
- // Another way to build an index would be to use inflateCopy(). That would not
57
- // be constrained to have access points at block boundaries, but would require
58
- // more memory per access point, and could not be saved to a file due to the
59
- // use of pointers in the state. The approach here allows for storage of the
60
- // index in a file.
61
-
62
- #include <stdio.h>
63
- #include <stdlib.h>
64
- #include <string.h>
65
- #include "zlib.h"
66
- #include "zran.h"
67
-
68
- #define WINSIZE 32768U // sliding window size
69
- #define CHUNK 16384 // file input buffer size
70
-
71
- // See comments in zran.h.
72
- void deflate_index_free(struct deflate_index *index) {
73
- if (index != NULL) {
74
- size_t i = index->have;
75
- while (i)
76
- free(index->list[--i].window);
77
- free(index->list);
78
- inflateEnd(&index->strm);
79
- free(index);
80
- }
81
- }
82
-
83
- // Add an access point to the list. If out of memory, deallocate the existing
84
- // list and return NULL. index->mode is temporarily the allocated number of
85
- // access points, until it is time for deflate_index_build() to return. Then
86
- // index->mode is set to the mode of inflation.
87
- static struct deflate_index *add_point(struct deflate_index *index, off_t in,
88
- off_t out, off_t beg,
89
- unsigned char *window) {
90
- if (index->have == index->mode) {
91
- // The list is full. Make it bigger.
92
- index->mode = index->mode ? index->mode << 1 : 8;
93
- point_t *next = realloc(index->list, sizeof(point_t) * index->mode);
94
- if (next == NULL) {
95
- deflate_index_free(index);
96
- return NULL;
97
- }
98
- index->list = next;
99
- }
100
-
101
- // Fill in the access point and increment how many we have.
102
- point_t *next = (point_t *)(index->list) + index->have++;
103
- if (index->have < 0) {
104
- // Overflowed the int!
105
- deflate_index_free(index);
106
- return NULL;
107
- }
108
- next->out = out;
109
- next->in = in;
110
- next->bits = index->strm.data_type & 7;
111
- next->dict = out - beg > WINSIZE ? WINSIZE : (unsigned)(out - beg);
112
- next->window = malloc(next->dict);
113
- if (next->window == NULL) {
114
- deflate_index_free(index);
115
- return NULL;
116
- }
117
- unsigned recent = WINSIZE - index->strm.avail_out;
118
- unsigned copy = recent > next->dict ? next->dict : recent;
119
- memcpy(next->window + next->dict - copy, window + recent - copy, copy);
120
- copy = next->dict - copy;
121
- memcpy(next->window, window + WINSIZE - copy, copy);
122
-
123
- // Return the index, which may have been newly allocated or destroyed.
124
- return index;
125
- }
126
-
127
- // Decompression modes. These are the inflateInit2() windowBits parameter.
128
- #define RAW -15
129
- #define ZLIB 15
130
- #define GZIP 31
131
-
132
- // See comments in zran.h.
133
- int deflate_index_build(FILE *in, off_t span, struct deflate_index **built) {
134
- // If this returns with an error, any attempt to use the index will cleanly
135
- // return an error.
136
- *built = NULL;
137
-
138
- // Create and initialize the index list.
139
- struct deflate_index *index = malloc(sizeof(struct deflate_index));
140
- if (index == NULL)
141
- return Z_MEM_ERROR;
142
- index->have = 0;
143
- index->mode = 0; // entries in index->list allocation
144
- index->list = NULL;
145
- index->strm.state = Z_NULL; // so inflateEnd() can work
146
-
147
- // Set up the inflation state.
148
- index->strm.avail_in = 0;
149
- index->strm.avail_out = 0;
150
- unsigned char buf[CHUNK]; // input buffer
151
- unsigned char win[WINSIZE] = {0}; // output sliding window
152
- off_t totin = 0; // total bytes read from input
153
- off_t totout = 0; // total bytes uncompressed
154
- off_t beg = 0; // starting offset of last history reset
155
- int mode = 0; // mode: RAW, ZLIB, or GZIP (0 => not set yet)
156
-
157
- // Decompress from in, generating access points along the way.
158
- int ret; // the return value from zlib, or Z_ERRNO
159
- off_t last; // last access point uncompressed offset
160
- do {
161
- // Assure available input, at least until reaching EOF.
162
- if (index->strm.avail_in == 0) {
163
- index->strm.avail_in = fread(buf, 1, sizeof(buf), in);
164
- totin += index->strm.avail_in;
165
- index->strm.next_in = buf;
166
- if (index->strm.avail_in < sizeof(buf) && ferror(in)) {
167
- ret = Z_ERRNO;
168
- break;
169
- }
170
-
171
- if (mode == 0) {
172
- // At the start of the input -- determine the type. Assume raw
173
- // if it is neither zlib nor gzip. This could in theory result
174
- // in a false positive for zlib, but in practice the fill bits
175
- // after a stored block are always zeros, so a raw stream won't
176
- // start with an 8 in the low nybble.
177
- mode = index->strm.avail_in == 0 ? RAW : // will fail
178
- (index->strm.next_in[0] & 0xf) == 8 ? ZLIB :
179
- index->strm.next_in[0] == 0x1f ? GZIP :
180
- /* else */ RAW;
181
- index->strm.zalloc = Z_NULL;
182
- index->strm.zfree = Z_NULL;
183
- index->strm.opaque = Z_NULL;
184
- ret = inflateInit2(&index->strm, mode);
185
- if (ret != Z_OK)
186
- break;
187
- }
188
- }
189
-
190
- // Assure available output. This rotates the output through, for use as
191
- // a sliding window on the uncompressed data.
192
- if (index->strm.avail_out == 0) {
193
- index->strm.avail_out = sizeof(win);
194
- index->strm.next_out = win;
195
- }
196
-
197
- if (mode == RAW && index->have == 0)
198
- // We skip the inflate() call at the start of raw deflate data in
199
- // order generate an access point there. Set data_type to imitate
200
- // the end of a header.
201
- index->strm.data_type = 0x80;
202
- else {
203
- // Inflate and update the number of uncompressed bytes.
204
- unsigned before = index->strm.avail_out;
205
- ret = inflate(&index->strm, Z_BLOCK);
206
- totout += before - index->strm.avail_out;
207
- }
208
-
209
- if ((index->strm.data_type & 0xc0) == 0x80 &&
210
- (index->have == 0 || totout - last >= span)) {
211
- // We are at the end of a header or a non-last deflate block, so we
212
- // can add an access point here. Furthermore, we are either at the
213
- // very start for the first access point, or there has been span or
214
- // more uncompressed bytes since the last access point, so we want
215
- // to add an access point here.
216
- index = add_point(index, totin - index->strm.avail_in, totout, beg,
217
- win);
218
- if (index == NULL) {
219
- ret = Z_MEM_ERROR;
220
- break;
221
- }
222
- last = totout;
223
- }
224
-
225
- if (ret == Z_STREAM_END && mode == GZIP &&
226
- (index->strm.avail_in || ungetc(getc(in), in) != EOF)) {
227
- // There is more input after the end of a gzip member. Reset the
228
- // inflate state to read another gzip member. On success, this will
229
- // set ret to Z_OK to continue decompressing.
230
- ret = inflateReset2(&index->strm, GZIP);
231
- beg = totout; // reset history
232
- }
233
-
234
- // Keep going until Z_STREAM_END or error. If the compressed data ends
235
- // prematurely without a file read error, Z_BUF_ERROR is returned.
236
- } while (ret == Z_OK);
237
-
238
- if (ret != Z_STREAM_END) {
239
- // An error was encountered. Discard the index and return a negative
240
- // error code.
241
- deflate_index_free(index);
242
- return ret == Z_NEED_DICT ? Z_DATA_ERROR : ret;
243
- }
244
-
245
- // Return the index.
246
- index->mode = mode;
247
- index->length = totout;
248
- *built = index;
249
- return index->have;
250
- }
251
-
252
- #ifdef NOPRIME
253
- // Support zlib versions before 1.2.3 (July 2005), or incomplete zlib clones
254
- // that do not have inflatePrime().
255
-
256
- # define INFLATEPRIME inflatePreface
257
-
258
- // Append the low bits bits of value to in[] at bit position *have, updating
259
- // *have. value must be zero above its low bits bits. bits must be positive.
260
- // This assumes that any bits above the *have bits in the last byte are zeros.
261
- // That assumption is preserved on return, as any bits above *have + bits in
262
- // the last byte written will be set to zeros.
263
- static inline void append_bits(unsigned value, int bits,
264
- unsigned char *in, int *have) {
265
- in += *have >> 3; // where the first bits from value will go
266
- int k = *have & 7; // the number of bits already there
267
- *have += bits;
268
- if (k)
269
- *in |= value << k; // write value above the low k bits
270
- else
271
- *in = value;
272
- k = 8 - k; // the number of bits just appended
273
- while (bits > k) {
274
- value >>= k; // drop the bits appended
275
- bits -= k;
276
- k = 8; // now at a byte boundary
277
- *++in = value;
278
- }
279
- }
280
-
281
- // Insert enough bits in the form of empty deflate blocks in front of the
282
- // low bits bits of value, in order to bring the sequence to a byte boundary.
283
- // Then feed that to inflate(). This does what inflatePrime() does, except that
284
- // a negative value of bits is not supported. bits must be in 0..16. If the
285
- // arguments are invalid, Z_STREAM_ERROR is returned. Otherwise the return
286
- // value from inflate() is returned.
287
- static int inflatePreface(z_stream *strm, int bits, int value) {
288
- // Check input.
289
- if (strm == Z_NULL || bits < 0 || bits > 16)
290
- return Z_STREAM_ERROR;
291
- if (bits == 0)
292
- return Z_OK;
293
- value &= (2 << (bits - 1)) - 1;
294
-
295
- // An empty dynamic block with an odd number of bits (95). The high bit of
296
- // the last byte is unused.
297
- static const unsigned char dyn[] = {
298
- 4, 0xe0, 0x81, 8, 0, 0, 0, 0, 0x20, 0xa8, 0xab, 0x1f
299
- };
300
- const int dynlen = 95; // number of bits in the block
301
-
302
- // Build an input buffer for inflate that is a multiple of eight bits in
303
- // length, and that ends with the low bits bits of value.
304
- unsigned char in[(dynlen + 3 * 10 + 16 + 7) / 8];
305
- int have = 0;
306
- if (bits & 1) {
307
- // Insert an empty dynamic block to get to an odd number of bits, so
308
- // when bits bits from value are appended, we are at an even number of
309
- // bits.
310
- memcpy(in, dyn, sizeof(dyn));
311
- have = dynlen;
312
- }
313
- while ((have + bits) & 7)
314
- // Insert empty fixed blocks until appending bits bits would put us on
315
- // a byte boundary. This will insert at most three fixed blocks.
316
- append_bits(2, 10, in, &have);
317
-
318
- // Append the bits bits from value, which takes us to a byte boundary.
319
- append_bits(value, bits, in, &have);
320
-
321
- // Deliver the input to inflate(). There is no output space provided, but
322
- // inflate() can't get stuck waiting on output not ingesting all of the
323
- // provided input. The reason is that there will be at most 16 bits of
324
- // input from value after the empty deflate blocks (which themselves
325
- // generate no output). At least ten bits are needed to generate the first
326
- // output byte from a fixed block. The last two bytes of the buffer have to
327
- // be ingested in order to get ten bits, which is the most that value can
328
- // occupy.
329
- strm->avail_in = have >> 3;
330
- strm->next_in = in;
331
- strm->avail_out = 0;
332
- strm->next_out = in; // not used, but can't be NULL
333
- return inflate(strm, Z_NO_FLUSH);
334
- }
335
-
336
- #else
337
- # define INFLATEPRIME inflatePrime
338
- #endif
339
-
340
- // See comments in zran.h.
341
- ptrdiff_t deflate_index_extract(FILE *in, struct deflate_index *index,
342
- off_t offset, unsigned char *buf, size_t len) {
343
- // Do a quick sanity check on the index.
344
- if (index == NULL || index->have < 1 || index->list[0].out != 0 ||
345
- index->strm.state == Z_NULL)
346
- return Z_STREAM_ERROR;
347
-
348
- // If nothing to extract, return zero bytes extracted.
349
- if (len == 0 || offset < 0 || offset >= index->length)
350
- return 0;
351
-
352
- // Find the access point closest to but not after offset.
353
- int lo = -1, hi = index->have;
354
- point_t *point = index->list;
355
- while (hi - lo > 1) {
356
- int mid = (lo + hi) >> 1;
357
- if (offset < point[mid].out)
358
- hi = mid;
359
- else
360
- lo = mid;
361
- }
362
- point += lo;
363
-
364
- // Initialize the input file and prime the inflate engine to start there.
365
- int ret = fseeko(in, point->in - (point->bits ? 1 : 0), SEEK_SET);
366
- if (ret == -1)
367
- return Z_ERRNO;
368
- int ch = 0;
369
- if (point->bits && (ch = getc(in)) == EOF)
370
- return ferror(in) ? Z_ERRNO : Z_BUF_ERROR;
371
- index->strm.avail_in = 0;
372
- ret = inflateReset2(&index->strm, RAW);
373
- if (ret != Z_OK)
374
- return ret;
375
- if (point->bits)
376
- INFLATEPRIME(&index->strm, point->bits, ch >> (8 - point->bits));
377
- inflateSetDictionary(&index->strm, point->window, point->dict);
378
-
379
- // Skip uncompressed bytes until offset reached, then satisfy request.
380
- unsigned char input[CHUNK];
381
- unsigned char discard[WINSIZE];
382
- offset -= point->out; // number of bytes to skip to get to offset
383
- size_t left = len; // number of bytes left to read after offset
384
- do {
385
- if (offset) {
386
- // Discard up to offset uncompressed bytes.
387
- index->strm.avail_out = offset < WINSIZE ? (unsigned)offset :
388
- WINSIZE;
389
- index->strm.next_out = discard;
390
- }
391
- else {
392
- // Uncompress up to left bytes into buf.
393
- index->strm.avail_out = left < (unsigned)-1 ? (unsigned)left :
394
- (unsigned)-1;
395
- index->strm.next_out = buf + len - left;
396
- }
397
-
398
- // Uncompress, setting got to the number of bytes uncompressed.
399
- if (index->strm.avail_in == 0) {
400
- // Assure available input.
401
- index->strm.avail_in = fread(input, 1, CHUNK, in);
402
- if (index->strm.avail_in < CHUNK && ferror(in)) {
403
- ret = Z_ERRNO;
404
- break;
405
- }
406
- index->strm.next_in = input;
407
- }
408
- unsigned got = index->strm.avail_out;
409
- ret = inflate(&index->strm, Z_NO_FLUSH);
410
- got -= index->strm.avail_out;
411
-
412
- // Update the appropriate count.
413
- if (offset)
414
- offset -= got;
415
- else {
416
- left -= got;
417
- if (left == 0)
418
- // Request satisfied.
419
- break;
420
- }
421
-
422
- // If we're at the end of a gzip member and there's more to read,
423
- // continue to the next gzip member.
424
- if (ret == Z_STREAM_END && index->mode == GZIP) {
425
- // Discard the gzip trailer.
426
- unsigned drop = 8; // length of gzip trailer
427
- if (index->strm.avail_in >= drop) {
428
- index->strm.avail_in -= drop;
429
- index->strm.next_in += drop;
430
- }
431
- else {
432
- // Read and discard the remainder of the gzip trailer.
433
- drop -= index->strm.avail_in;
434
- index->strm.avail_in = 0;
435
- do {
436
- if (getc(in) == EOF)
437
- // The input does not have a complete trailer.
438
- return ferror(in) ? Z_ERRNO : Z_BUF_ERROR;
439
- } while (--drop);
440
- }
441
-
442
- if (index->strm.avail_in || ungetc(getc(in), in) != EOF) {
443
- // There's more after the gzip trailer. Use inflate to skip the
444
- // gzip header and resume the raw inflate there.
445
- inflateReset2(&index->strm, GZIP);
446
- do {
447
- if (index->strm.avail_in == 0) {
448
- index->strm.avail_in = fread(input, 1, CHUNK, in);
449
- if (index->strm.avail_in < CHUNK && ferror(in)) {
450
- ret = Z_ERRNO;
451
- break;
452
- }
453
- index->strm.next_in = input;
454
- }
455
- index->strm.avail_out = WINSIZE;
456
- index->strm.next_out = discard;
457
- ret = inflate(&index->strm, Z_BLOCK); // stop after header
458
- } while (ret == Z_OK && (index->strm.data_type & 0x80) == 0);
459
- if (ret != Z_OK)
460
- break;
461
- inflateReset2(&index->strm, RAW);
462
- }
463
- }
464
-
465
- // Continue until we have the requested data, the deflate data has
466
- // ended, or an error is encountered.
467
- } while (ret == Z_OK);
468
-
469
- // Return the number of uncompressed bytes read into buf, or the error.
470
- return ret == Z_OK || ret == Z_STREAM_END ? len - left : ret;
471
- }
472
-
473
- #ifdef TEST
474
-
475
- #define SPAN 1048576L // desired distance between access points
476
- #define LEN 16384 // number of bytes to extract
477
-
478
- // Demonstrate the use of deflate_index_build() and deflate_index_extract() by
479
- // processing the file provided on the command line, and extracting LEN bytes
480
- // from 2/3rds of the way through the uncompressed output, writing that to
481
- // stdout. An offset can be provided as the second argument, in which case the
482
- // data is extracted from there instead.
483
- int main(int argc, char **argv) {
484
- // Open the input file.
485
- if (argc < 2 || argc > 3) {
486
- fprintf(stderr, "usage: zran file.raw [offset]\n");
487
- return 1;
488
- }
489
- FILE *in = fopen(argv[1], "rb");
490
- if (in == NULL) {
491
- fprintf(stderr, "zran: could not open %s for reading\n", argv[1]);
492
- return 1;
493
- }
494
-
495
- // Get optional offset.
496
- off_t offset = -1;
497
- if (argc == 3) {
498
- char *end;
499
- offset = strtoll(argv[2], &end, 10);
500
- if (*end || offset < 0) {
501
- fprintf(stderr, "zran: %s is not a valid offset\n", argv[2]);
502
- return 1;
503
- }
504
- }
505
-
506
- // Build index.
507
- struct deflate_index *index = NULL;
508
- int len = deflate_index_build(in, SPAN, &index);
509
- if (len < 0) {
510
- fclose(in);
511
- switch (len) {
512
- case Z_MEM_ERROR:
513
- fprintf(stderr, "zran: out of memory\n");
514
- break;
515
- case Z_BUF_ERROR:
516
- fprintf(stderr, "zran: %s ended prematurely\n", argv[1]);
517
- break;
518
- case Z_DATA_ERROR:
519
- fprintf(stderr, "zran: compressed data error in %s\n", argv[1]);
520
- break;
521
- case Z_ERRNO:
522
- fprintf(stderr, "zran: read error on %s\n", argv[1]);
523
- break;
524
- default:
525
- fprintf(stderr, "zran: error %d while building index\n", len);
526
- }
527
- return 1;
528
- }
529
- fprintf(stderr, "zran: built index with %d access points\n", len);
530
-
531
- // Use index by reading some bytes from an arbitrary offset.
532
- unsigned char buf[LEN];
533
- if (offset == -1)
534
- offset = ((index->length + 1) << 1) / 3;
535
- ptrdiff_t got = deflate_index_extract(in, index, offset, buf, LEN);
536
- if (got < 0)
537
- fprintf(stderr, "zran: extraction failed: %s error\n",
538
- got == Z_MEM_ERROR ? "out of memory" : "input corrupted");
539
- else {
540
- fwrite(buf, 1, got, stdout);
541
- fprintf(stderr, "zran: extracted %ld bytes at %lld\n", got, offset);
542
- }
543
-
544
- // Clean up and exit.
545
- deflate_index_free(index);
546
- fclose(in);
547
- return 0;
548
- }
549
-
550
- #endif
1
+ /* zran.c -- example of deflate stream indexing and random access
2
+ * Copyright (C) 2005, 2012, 2018, 2023, 2024 Mark Adler
3
+ * For conditions of distribution and use, see copyright notice in zlib.h
4
+ * Version 1.6 2 Aug 2024 Mark Adler */
5
+
6
+ /* Version History:
7
+ 1.0 29 May 2005 First version
8
+ 1.1 29 Sep 2012 Fix memory reallocation error
9
+ 1.2 14 Oct 2018 Handle gzip streams with multiple members
10
+ Add a header file to facilitate usage in applications
11
+ 1.3 18 Feb 2023 Permit raw deflate streams as well as zlib and gzip
12
+ Permit crossing gzip member boundaries when extracting
13
+ Support a size_t size when extracting (was an int)
14
+ Do a binary search over the index for an access point
15
+ Expose the access point type to enable save and load
16
+ 1.4 13 Apr 2023 Add a NOPRIME define to not use inflatePrime()
17
+ 1.5 4 Feb 2024 Set returned index to NULL on an index build error
18
+ Stop decoding once request is satisfied
19
+ Provide a reusable inflate engine in the index
20
+ Allocate the dictionaries to reduce memory usage
21
+ 1.6 2 Aug 2024 Remove unneeded dependency on limits.h
22
+ */
23
+
24
+ // Illustrate the use of Z_BLOCK, inflatePrime(), and inflateSetDictionary()
25
+ // for random access of a compressed file. A file containing a raw deflate
26
+ // stream is provided on the command line. The compressed stream is decoded in
27
+ // its entirety, and an index built with access points about every SPAN bytes
28
+ // in the uncompressed output. The compressed file is left open, and can then
29
+ // be read randomly, having to decompress on the average SPAN/2 uncompressed
30
+ // bytes before getting to the desired block of data.
31
+ //
32
+ // An access point can be created at the start of any deflate block, by saving
33
+ // the starting file offset and bit of that block, and the 32K bytes of
34
+ // uncompressed data that precede that block. Also the uncompressed offset of
35
+ // that block is saved to provide a reference for locating a desired starting
36
+ // point in the uncompressed stream. deflate_index_build() decompresses the
37
+ // input raw deflate stream a block at a time, and at the end of each block
38
+ // decides if enough uncompressed data has gone by to justify the creation of a
39
+ // new access point. If so, that point is saved in a data structure that grows
40
+ // as needed to accommodate the points.
41
+ //
42
+ // To use the index, an offset in the uncompressed data is provided, for which
43
+ // the latest access point at or preceding that offset is located in the index.
44
+ // The input file is positioned to the specified location in the index, and if
45
+ // necessary the first few bits of the compressed data is read from the file.
46
+ // inflate is initialized with those bits and the 32K of uncompressed data, and
47
+ // decompression then proceeds until the desired offset in the file is reached.
48
+ // Then decompression continues to read the requested uncompressed data from
49
+ // the file.
50
+ //
51
+ // There is some fair bit of overhead to starting inflation for the random
52
+ // access, mainly copying the 32K byte dictionary. If small pieces of the file
53
+ // are being accessed, it would make sense to implement a cache to hold some
54
+ // lookahead to avoid many calls to deflate_index_extract() for small lengths.
55
+ //
56
+ // Another way to build an index would be to use inflateCopy(). That would not
57
+ // be constrained to have access points at block boundaries, but would require
58
+ // more memory per access point, and could not be saved to a file due to the
59
+ // use of pointers in the state. The approach here allows for storage of the
60
+ // index in a file.
61
+
62
+ #include <stdio.h>
63
+ #include <stdlib.h>
64
+ #include <string.h>
65
+ #include "zlib.h"
66
+ #include "zran.h"
67
+
68
+ #define WINSIZE 32768U // sliding window size
69
+ #define CHUNK 16384 // file input buffer size
70
+
71
+ // See comments in zran.h.
72
+ void deflate_index_free(struct deflate_index *index) {
73
+ if (index != NULL) {
74
+ size_t i = index->have;
75
+ while (i)
76
+ free(index->list[--i].window);
77
+ free(index->list);
78
+ inflateEnd(&index->strm);
79
+ free(index);
80
+ }
81
+ }
82
+
83
+ // Add an access point to the list. If out of memory, deallocate the existing
84
+ // list and return NULL. index->mode is temporarily the allocated number of
85
+ // access points, until it is time for deflate_index_build() to return. Then
86
+ // index->mode is set to the mode of inflation.
87
+ static struct deflate_index *add_point(struct deflate_index *index, off_t in,
88
+ off_t out, off_t beg,
89
+ unsigned char *window) {
90
+ if (index->have == index->mode) {
91
+ // The list is full. Make it bigger.
92
+ index->mode = index->mode ? index->mode << 1 : 8;
93
+ point_t *next = realloc(index->list, sizeof(point_t) * index->mode);
94
+ if (next == NULL) {
95
+ deflate_index_free(index);
96
+ return NULL;
97
+ }
98
+ index->list = next;
99
+ }
100
+
101
+ // Fill in the access point and increment how many we have.
102
+ point_t *next = (point_t *)(index->list) + index->have++;
103
+ if (index->have < 0) {
104
+ // Overflowed the int!
105
+ deflate_index_free(index);
106
+ return NULL;
107
+ }
108
+ next->out = out;
109
+ next->in = in;
110
+ next->bits = index->strm.data_type & 7;
111
+ next->dict = out - beg > WINSIZE ? WINSIZE : (unsigned)(out - beg);
112
+ next->window = malloc(next->dict);
113
+ if (next->window == NULL) {
114
+ deflate_index_free(index);
115
+ return NULL;
116
+ }
117
+ unsigned recent = WINSIZE - index->strm.avail_out;
118
+ unsigned copy = recent > next->dict ? next->dict : recent;
119
+ memcpy(next->window + next->dict - copy, window + recent - copy, copy);
120
+ copy = next->dict - copy;
121
+ memcpy(next->window, window + WINSIZE - copy, copy);
122
+
123
+ // Return the index, which may have been newly allocated or destroyed.
124
+ return index;
125
+ }
126
+
127
+ // Decompression modes. These are the inflateInit2() windowBits parameter.
128
+ #define RAW -15
129
+ #define ZLIB 15
130
+ #define GZIP 31
131
+
132
+ // See comments in zran.h.
133
+ int deflate_index_build(FILE *in, off_t span, struct deflate_index **built) {
134
+ // If this returns with an error, any attempt to use the index will cleanly
135
+ // return an error.
136
+ *built = NULL;
137
+
138
+ // Create and initialize the index list.
139
+ struct deflate_index *index = malloc(sizeof(struct deflate_index));
140
+ if (index == NULL)
141
+ return Z_MEM_ERROR;
142
+ index->have = 0;
143
+ index->mode = 0; // entries in index->list allocation
144
+ index->list = NULL;
145
+ index->strm.state = Z_NULL; // so inflateEnd() can work
146
+
147
+ // Set up the inflation state.
148
+ index->strm.avail_in = 0;
149
+ index->strm.avail_out = 0;
150
+ unsigned char buf[CHUNK]; // input buffer
151
+ unsigned char win[WINSIZE] = {0}; // output sliding window
152
+ off_t totin = 0; // total bytes read from input
153
+ off_t totout = 0; // total bytes uncompressed
154
+ off_t beg = 0; // starting offset of last history reset
155
+ int mode = 0; // mode: RAW, ZLIB, or GZIP (0 => not set yet)
156
+
157
+ // Decompress from in, generating access points along the way.
158
+ int ret; // the return value from zlib, or Z_ERRNO
159
+ off_t last; // last access point uncompressed offset
160
+ do {
161
+ // Assure available input, at least until reaching EOF.
162
+ if (index->strm.avail_in == 0) {
163
+ index->strm.avail_in = fread(buf, 1, sizeof(buf), in);
164
+ totin += index->strm.avail_in;
165
+ index->strm.next_in = buf;
166
+ if (index->strm.avail_in < sizeof(buf) && ferror(in)) {
167
+ ret = Z_ERRNO;
168
+ break;
169
+ }
170
+
171
+ if (mode == 0) {
172
+ // At the start of the input -- determine the type. Assume raw
173
+ // if it is neither zlib nor gzip. This could in theory result
174
+ // in a false positive for zlib, but in practice the fill bits
175
+ // after a stored block are always zeros, so a raw stream won't
176
+ // start with an 8 in the low nybble.
177
+ mode = index->strm.avail_in == 0 ? RAW : // will fail
178
+ (index->strm.next_in[0] & 0xf) == 8 ? ZLIB :
179
+ index->strm.next_in[0] == 0x1f ? GZIP :
180
+ /* else */ RAW;
181
+ index->strm.zalloc = Z_NULL;
182
+ index->strm.zfree = Z_NULL;
183
+ index->strm.opaque = Z_NULL;
184
+ ret = inflateInit2(&index->strm, mode);
185
+ if (ret != Z_OK)
186
+ break;
187
+ }
188
+ }
189
+
190
+ // Assure available output. This rotates the output through, for use as
191
+ // a sliding window on the uncompressed data.
192
+ if (index->strm.avail_out == 0) {
193
+ index->strm.avail_out = sizeof(win);
194
+ index->strm.next_out = win;
195
+ }
196
+
197
+ if (mode == RAW && index->have == 0)
198
+ // We skip the inflate() call at the start of raw deflate data in
199
+ // order generate an access point there. Set data_type to imitate
200
+ // the end of a header.
201
+ index->strm.data_type = 0x80;
202
+ else {
203
+ // Inflate and update the number of uncompressed bytes.
204
+ unsigned before = index->strm.avail_out;
205
+ ret = inflate(&index->strm, Z_BLOCK);
206
+ totout += before - index->strm.avail_out;
207
+ }
208
+
209
+ if ((index->strm.data_type & 0xc0) == 0x80 &&
210
+ (index->have == 0 || totout - last >= span)) {
211
+ // We are at the end of a header or a non-last deflate block, so we
212
+ // can add an access point here. Furthermore, we are either at the
213
+ // very start for the first access point, or there has been span or
214
+ // more uncompressed bytes since the last access point, so we want
215
+ // to add an access point here.
216
+ index = add_point(index, totin - index->strm.avail_in, totout, beg,
217
+ win);
218
+ if (index == NULL) {
219
+ ret = Z_MEM_ERROR;
220
+ break;
221
+ }
222
+ last = totout;
223
+ }
224
+
225
+ if (ret == Z_STREAM_END && mode == GZIP &&
226
+ (index->strm.avail_in || ungetc(getc(in), in) != EOF)) {
227
+ // There is more input after the end of a gzip member. Reset the
228
+ // inflate state to read another gzip member. On success, this will
229
+ // set ret to Z_OK to continue decompressing.
230
+ ret = inflateReset2(&index->strm, GZIP);
231
+ beg = totout; // reset history
232
+ }
233
+
234
+ // Keep going until Z_STREAM_END or error. If the compressed data ends
235
+ // prematurely without a file read error, Z_BUF_ERROR is returned.
236
+ } while (ret == Z_OK);
237
+
238
+ if (ret != Z_STREAM_END) {
239
+ // An error was encountered. Discard the index and return a negative
240
+ // error code.
241
+ deflate_index_free(index);
242
+ return ret == Z_NEED_DICT ? Z_DATA_ERROR : ret;
243
+ }
244
+
245
+ // Return the index.
246
+ index->mode = mode;
247
+ index->length = totout;
248
+ *built = index;
249
+ return index->have;
250
+ }
251
+
252
+ #ifdef NOPRIME
253
+ // Support zlib versions before 1.2.3 (July 2005), or incomplete zlib clones
254
+ // that do not have inflatePrime().
255
+
256
+ # define INFLATEPRIME inflatePreface
257
+
258
+ // Append the low bits bits of value to in[] at bit position *have, updating
259
+ // *have. value must be zero above its low bits bits. bits must be positive.
260
+ // This assumes that any bits above the *have bits in the last byte are zeros.
261
+ // That assumption is preserved on return, as any bits above *have + bits in
262
+ // the last byte written will be set to zeros.
263
+ static inline void append_bits(unsigned value, int bits,
264
+ unsigned char *in, int *have) {
265
+ in += *have >> 3; // where the first bits from value will go
266
+ int k = *have & 7; // the number of bits already there
267
+ *have += bits;
268
+ if (k)
269
+ *in |= value << k; // write value above the low k bits
270
+ else
271
+ *in = value;
272
+ k = 8 - k; // the number of bits just appended
273
+ while (bits > k) {
274
+ value >>= k; // drop the bits appended
275
+ bits -= k;
276
+ k = 8; // now at a byte boundary
277
+ *++in = value;
278
+ }
279
+ }
280
+
281
+ // Insert enough bits in the form of empty deflate blocks in front of the
282
+ // low bits bits of value, in order to bring the sequence to a byte boundary.
283
+ // Then feed that to inflate(). This does what inflatePrime() does, except that
284
+ // a negative value of bits is not supported. bits must be in 0..16. If the
285
+ // arguments are invalid, Z_STREAM_ERROR is returned. Otherwise the return
286
+ // value from inflate() is returned.
287
+ static int inflatePreface(z_stream *strm, int bits, int value) {
288
+ // Check input.
289
+ if (strm == Z_NULL || bits < 0 || bits > 16)
290
+ return Z_STREAM_ERROR;
291
+ if (bits == 0)
292
+ return Z_OK;
293
+ value &= (2 << (bits - 1)) - 1;
294
+
295
+ // An empty dynamic block with an odd number of bits (95). The high bit of
296
+ // the last byte is unused.
297
+ static const unsigned char dyn[] = {
298
+ 4, 0xe0, 0x81, 8, 0, 0, 0, 0, 0x20, 0xa8, 0xab, 0x1f
299
+ };
300
+ const int dynlen = 95; // number of bits in the block
301
+
302
+ // Build an input buffer for inflate that is a multiple of eight bits in
303
+ // length, and that ends with the low bits bits of value.
304
+ unsigned char in[(dynlen + 3 * 10 + 16 + 7) / 8];
305
+ int have = 0;
306
+ if (bits & 1) {
307
+ // Insert an empty dynamic block to get to an odd number of bits, so
308
+ // when bits bits from value are appended, we are at an even number of
309
+ // bits.
310
+ memcpy(in, dyn, sizeof(dyn));
311
+ have = dynlen;
312
+ }
313
+ while ((have + bits) & 7)
314
+ // Insert empty fixed blocks until appending bits bits would put us on
315
+ // a byte boundary. This will insert at most three fixed blocks.
316
+ append_bits(2, 10, in, &have);
317
+
318
+ // Append the bits bits from value, which takes us to a byte boundary.
319
+ append_bits(value, bits, in, &have);
320
+
321
+ // Deliver the input to inflate(). There is no output space provided, but
322
+ // inflate() can't get stuck waiting on output not ingesting all of the
323
+ // provided input. The reason is that there will be at most 16 bits of
324
+ // input from value after the empty deflate blocks (which themselves
325
+ // generate no output). At least ten bits are needed to generate the first
326
+ // output byte from a fixed block. The last two bytes of the buffer have to
327
+ // be ingested in order to get ten bits, which is the most that value can
328
+ // occupy.
329
+ strm->avail_in = have >> 3;
330
+ strm->next_in = in;
331
+ strm->avail_out = 0;
332
+ strm->next_out = in; // not used, but can't be NULL
333
+ return inflate(strm, Z_NO_FLUSH);
334
+ }
335
+
336
+ #else
337
+ # define INFLATEPRIME inflatePrime
338
+ #endif
339
+
340
+ // See comments in zran.h.
341
+ ptrdiff_t deflate_index_extract(FILE *in, struct deflate_index *index,
342
+ off_t offset, unsigned char *buf, size_t len) {
343
+ // Do a quick sanity check on the index.
344
+ if (index == NULL || index->have < 1 || index->list[0].out != 0 ||
345
+ index->strm.state == Z_NULL)
346
+ return Z_STREAM_ERROR;
347
+
348
+ // If nothing to extract, return zero bytes extracted.
349
+ if (len == 0 || offset < 0 || offset >= index->length)
350
+ return 0;
351
+
352
+ // Find the access point closest to but not after offset.
353
+ int lo = -1, hi = index->have;
354
+ point_t *point = index->list;
355
+ while (hi - lo > 1) {
356
+ int mid = (lo + hi) >> 1;
357
+ if (offset < point[mid].out)
358
+ hi = mid;
359
+ else
360
+ lo = mid;
361
+ }
362
+ point += lo;
363
+
364
+ // Initialize the input file and prime the inflate engine to start there.
365
+ int ret = fseeko(in, point->in - (point->bits ? 1 : 0), SEEK_SET);
366
+ if (ret == -1)
367
+ return Z_ERRNO;
368
+ int ch = 0;
369
+ if (point->bits && (ch = getc(in)) == EOF)
370
+ return ferror(in) ? Z_ERRNO : Z_BUF_ERROR;
371
+ index->strm.avail_in = 0;
372
+ ret = inflateReset2(&index->strm, RAW);
373
+ if (ret != Z_OK)
374
+ return ret;
375
+ if (point->bits)
376
+ INFLATEPRIME(&index->strm, point->bits, ch >> (8 - point->bits));
377
+ inflateSetDictionary(&index->strm, point->window, point->dict);
378
+
379
+ // Skip uncompressed bytes until offset reached, then satisfy request.
380
+ unsigned char input[CHUNK];
381
+ unsigned char discard[WINSIZE];
382
+ offset -= point->out; // number of bytes to skip to get to offset
383
+ size_t left = len; // number of bytes left to read after offset
384
+ do {
385
+ if (offset) {
386
+ // Discard up to offset uncompressed bytes.
387
+ index->strm.avail_out = offset < WINSIZE ? (unsigned)offset :
388
+ WINSIZE;
389
+ index->strm.next_out = discard;
390
+ }
391
+ else {
392
+ // Uncompress up to left bytes into buf.
393
+ index->strm.avail_out = left < (unsigned)-1 ? (unsigned)left :
394
+ (unsigned)-1;
395
+ index->strm.next_out = buf + len - left;
396
+ }
397
+
398
+ // Uncompress, setting got to the number of bytes uncompressed.
399
+ if (index->strm.avail_in == 0) {
400
+ // Assure available input.
401
+ index->strm.avail_in = fread(input, 1, CHUNK, in);
402
+ if (index->strm.avail_in < CHUNK && ferror(in)) {
403
+ ret = Z_ERRNO;
404
+ break;
405
+ }
406
+ index->strm.next_in = input;
407
+ }
408
+ unsigned got = index->strm.avail_out;
409
+ ret = inflate(&index->strm, Z_NO_FLUSH);
410
+ got -= index->strm.avail_out;
411
+
412
+ // Update the appropriate count.
413
+ if (offset)
414
+ offset -= got;
415
+ else {
416
+ left -= got;
417
+ if (left == 0)
418
+ // Request satisfied.
419
+ break;
420
+ }
421
+
422
+ // If we're at the end of a gzip member and there's more to read,
423
+ // continue to the next gzip member.
424
+ if (ret == Z_STREAM_END && index->mode == GZIP) {
425
+ // Discard the gzip trailer.
426
+ unsigned drop = 8; // length of gzip trailer
427
+ if (index->strm.avail_in >= drop) {
428
+ index->strm.avail_in -= drop;
429
+ index->strm.next_in += drop;
430
+ }
431
+ else {
432
+ // Read and discard the remainder of the gzip trailer.
433
+ drop -= index->strm.avail_in;
434
+ index->strm.avail_in = 0;
435
+ do {
436
+ if (getc(in) == EOF)
437
+ // The input does not have a complete trailer.
438
+ return ferror(in) ? Z_ERRNO : Z_BUF_ERROR;
439
+ } while (--drop);
440
+ }
441
+
442
+ if (index->strm.avail_in || ungetc(getc(in), in) != EOF) {
443
+ // There's more after the gzip trailer. Use inflate to skip the
444
+ // gzip header and resume the raw inflate there.
445
+ inflateReset2(&index->strm, GZIP);
446
+ do {
447
+ if (index->strm.avail_in == 0) {
448
+ index->strm.avail_in = fread(input, 1, CHUNK, in);
449
+ if (index->strm.avail_in < CHUNK && ferror(in)) {
450
+ ret = Z_ERRNO;
451
+ break;
452
+ }
453
+ index->strm.next_in = input;
454
+ }
455
+ index->strm.avail_out = WINSIZE;
456
+ index->strm.next_out = discard;
457
+ ret = inflate(&index->strm, Z_BLOCK); // stop after header
458
+ } while (ret == Z_OK && (index->strm.data_type & 0x80) == 0);
459
+ if (ret != Z_OK)
460
+ break;
461
+ inflateReset2(&index->strm, RAW);
462
+ }
463
+ }
464
+
465
+ // Continue until we have the requested data, the deflate data has
466
+ // ended, or an error is encountered.
467
+ } while (ret == Z_OK);
468
+
469
+ // Return the number of uncompressed bytes read into buf, or the error.
470
+ return ret == Z_OK || ret == Z_STREAM_END ? len - left : ret;
471
+ }
472
+
473
+ #ifdef TEST
474
+
475
+ #define SPAN 1048576L // desired distance between access points
476
+ #define LEN 16384 // number of bytes to extract
477
+
478
+ // Demonstrate the use of deflate_index_build() and deflate_index_extract() by
479
+ // processing the file provided on the command line, and extracting LEN bytes
480
+ // from 2/3rds of the way through the uncompressed output, writing that to
481
+ // stdout. An offset can be provided as the second argument, in which case the
482
+ // data is extracted from there instead.
483
+ int main(int argc, char **argv) {
484
+ // Open the input file.
485
+ if (argc < 2 || argc > 3) {
486
+ fprintf(stderr, "usage: zran file.raw [offset]\n");
487
+ return 1;
488
+ }
489
+ FILE *in = fopen(argv[1], "rb");
490
+ if (in == NULL) {
491
+ fprintf(stderr, "zran: could not open %s for reading\n", argv[1]);
492
+ return 1;
493
+ }
494
+
495
+ // Get optional offset.
496
+ off_t offset = -1;
497
+ if (argc == 3) {
498
+ char *end;
499
+ offset = strtoll(argv[2], &end, 10);
500
+ if (*end || offset < 0) {
501
+ fprintf(stderr, "zran: %s is not a valid offset\n", argv[2]);
502
+ return 1;
503
+ }
504
+ }
505
+
506
+ // Build index.
507
+ struct deflate_index *index = NULL;
508
+ int len = deflate_index_build(in, SPAN, &index);
509
+ if (len < 0) {
510
+ fclose(in);
511
+ switch (len) {
512
+ case Z_MEM_ERROR:
513
+ fprintf(stderr, "zran: out of memory\n");
514
+ break;
515
+ case Z_BUF_ERROR:
516
+ fprintf(stderr, "zran: %s ended prematurely\n", argv[1]);
517
+ break;
518
+ case Z_DATA_ERROR:
519
+ fprintf(stderr, "zran: compressed data error in %s\n", argv[1]);
520
+ break;
521
+ case Z_ERRNO:
522
+ fprintf(stderr, "zran: read error on %s\n", argv[1]);
523
+ break;
524
+ default:
525
+ fprintf(stderr, "zran: error %d while building index\n", len);
526
+ }
527
+ return 1;
528
+ }
529
+ fprintf(stderr, "zran: built index with %d access points\n", len);
530
+
531
+ // Use index by reading some bytes from an arbitrary offset.
532
+ unsigned char buf[LEN];
533
+ if (offset == -1)
534
+ offset = ((index->length + 1) << 1) / 3;
535
+ ptrdiff_t got = deflate_index_extract(in, index, offset, buf, LEN);
536
+ if (got < 0)
537
+ fprintf(stderr, "zran: extraction failed: %s error\n",
538
+ got == Z_MEM_ERROR ? "out of memory" : "input corrupted");
539
+ else {
540
+ fwrite(buf, 1, got, stdout);
541
+ fprintf(stderr, "zran: extracted %ld bytes at %lld\n", got, offset);
542
+ }
543
+
544
+ // Clean up and exit.
545
+ deflate_index_free(index);
546
+ fclose(in);
547
+ return 0;
548
+ }
549
+
550
+ #endif