From 6f29bd8667ca84d2d4145d46de958e78ef571033 Mon Sep 17 00:00:00 2001 From: Vaidas Pilkauskas Date: Fri, 4 Dec 2020 13:51:16 +0200 Subject: [PATCH 01/11] Stamp scala_import jars --- scala/scala_import.bzl | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/scala/scala_import.bzl b/scala/scala_import.bzl index e0d889be6..95b722d2b 100644 --- a/scala/scala_import.bzl +++ b/scala/scala_import.bzl @@ -1,5 +1,15 @@ load("@io_bazel_rules_scala//scala:jars_to_labels.bzl", "JarsToLabelsInfo") +def _stamp_symlinked_jar(ctx, jar): + symlink_file = ctx.actions.declare_file(jar.basename) + ctx.actions.symlink(output = symlink_file, target_file = jar) + return java_common.stamp_jar( + actions = ctx.actions, + jar = symlink_file, + target_label = ctx.label, + java_toolchain = ctx.attr._java_toolchain[java_common.JavaToolchainInfo], + ) + #intellij part is tested manually, tread lightly when changing there #if you change make sure to manually re-import an intellij project and see imports #are resolved (not red) and clickable @@ -12,11 +22,17 @@ def _scala_import_impl(ctx): current_target_compile_jars, intellij_metadata, ) = (target_data.code_jars, target_data.intellij_metadata) - current_jars = depset(current_target_compile_jars) + + current_stamped_jars = [ + _stamp_symlinked_jar(ctx, jar) + for jar in current_target_compile_jars + ] + + current_jars = depset(current_stamped_jars) + exports = java_common.merge([export[JavaInfo] for export in ctx.attr.exports]) transitive_runtime_jars = \ - java_common.merge([dep[JavaInfo] for dep in ctx.attr.runtime_deps]) \ - .transitive_runtime_jars + java_common.merge([dep[JavaInfo] for dep in ctx.attr.runtime_deps]).transitive_runtime_jars jars2labels = {} _collect_labels(ctx.attr.deps, jars2labels) _collect_labels(ctx.attr.exports, jars2labels) #untested @@ -114,5 +130,8 @@ scala_import = rule( allow_single_file = True, default = Label("@io_bazel_rules_scala//scala:libPlaceHolderClassToCreateEmptyJarForScalaImport.jar"), ), + "_java_toolchain": attr.label( + default = Label("@bazel_tools//tools/jdk:current_java_toolchain"), + ), }, ) From 67bfdb3bd8f76feaac56c27c275bc1e88996b8b3 Mon Sep 17 00:00:00 2001 From: Vaidas Pilkauskas Date: Mon, 7 Dec 2020 15:27:43 +0200 Subject: [PATCH 02/11] Use target providers from stamped jars --- scala/scala_import.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scala/scala_import.bzl b/scala/scala_import.bzl index 95b722d2b..a8063e818 100644 --- a/scala/scala_import.bzl +++ b/scala/scala_import.bzl @@ -43,7 +43,7 @@ def _scala_import_impl(ctx): ) #last to override the label of the export compile jars to the current target if current_target_compile_jars: - current_target_providers = [_new_java_info(ctx, jar) for jar in current_target_compile_jars] + current_target_providers = [_new_java_info(ctx, jar) for jar in current_stamped_jars] else: # TODO(#8867): Migrate away from the placeholder jar hack when #8867 is fixed. current_target_providers = [_new_java_info(ctx, ctx.file._placeholder_jar)] From 75ad28d3f1613a35930dc538bbf7a14d8b8b754b Mon Sep 17 00:00:00 2001 From: Vaidas Pilkauskas Date: Mon, 7 Dec 2020 15:27:59 +0200 Subject: [PATCH 03/11] Fix analyzer tests --- third_party/dependency_analyzer/src/test/BUILD | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/third_party/dependency_analyzer/src/test/BUILD b/third_party/dependency_analyzer/src/test/BUILD index 1e34555ab..8f603fea7 100644 --- a/third_party/dependency_analyzer/src/test/BUILD +++ b/third_party/dependency_analyzer/src/test/BUILD @@ -4,8 +4,8 @@ load("//scala:scala.bzl", "scala_junit_test", "scala_test") common_jvm_flags = [ "-Dplugin.jar.location=$(location //third_party/dependency_analyzer/src/main:dependency_analyzer)", - "-Dscala.library.location=$(location //external:io_bazel_rules_scala/dependency/scala/scala_library)", - "-Dscala.reflect.location=$(location //external:io_bazel_rules_scala/dependency/scala/scala_reflect)", + "-Dscala.library.location=$(rootpath //external:io_bazel_rules_scala/dependency/scala/scala_library)", + "-Dscala.reflect.location=$(rootpath //external:io_bazel_rules_scala/dependency/scala/scala_reflect)", ] scala_test( @@ -64,7 +64,7 @@ scala_test( "io/bazel/rulesscala/dependencyanalyzer/StrictDepsTest.scala", ], jvm_flags = common_jvm_flags + [ - "-Dguava.jar.location=$(location @com_google_guava_guava_21_0_with_file//jar)", + "-Dguava.jar.location=$(rootpath @com_google_guava_guava_21_0_with_file//jar)", "-Dapache.commons.jar.location=$(location @org_apache_commons_commons_lang_3_5_without_file//:linkable_org_apache_commons_commons_lang_3_5_without_file)", ], unused_dependency_checker_mode = "off", From 85f9c8cb6a90cbe8277ac6d7173da7cb8d3322af Mon Sep 17 00:00:00 2001 From: Vaidas Pilkauskas Date: Mon, 11 Jan 2021 11:39:33 +0200 Subject: [PATCH 04/11] Use singlejar directly instead of ijar to stamp jars --- scala/scala_import.bzl | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/scala/scala_import.bzl b/scala/scala_import.bzl index a8063e818..c487a0a5d 100644 --- a/scala/scala_import.bzl +++ b/scala/scala_import.bzl @@ -3,13 +3,39 @@ load("@io_bazel_rules_scala//scala:jars_to_labels.bzl", "JarsToLabelsInfo") def _stamp_symlinked_jar(ctx, jar): symlink_file = ctx.actions.declare_file(jar.basename) ctx.actions.symlink(output = symlink_file, target_file = jar) - return java_common.stamp_jar( - actions = ctx.actions, - jar = symlink_file, - target_label = ctx.label, - java_toolchain = ctx.attr._java_toolchain[java_common.JavaToolchainInfo], + + # TODO: use java_common.stamp_jar version after ijar stamping is fixed: + # https://github.com/bazelbuild/bazel/issues/12730 + # + # return java_common.stamp_jar( + # actions = ctx.actions, + # jar = symlink_file, + # target_label = ctx.label, + # java_toolchain = ctx.attr._java_toolchain[java_common.JavaToolchainInfo], + # ) + + stamped_jar_filename = jar.basename.rstrip(".jar") + "-stamped.jar" + stamped_file = ctx.actions.declare_file(stamped_jar_filename) + + # TODO: use java_common + ctx.actions.run( + executable = ctx.executable._singlejar, + inputs = [symlink_file], + outputs = [stamped_file], + arguments = [ + "--sources", + symlink_file.path, + "--output", + stamped_file.path, + "--deploy_manifest_lines", + "Target-Label: %s" % ctx.label, + ], + mnemonic = "StampTargetLabel", + progress_message = "Stamping manifest of %s" % ctx.label, ) + return stamped_file + #intellij part is tested manually, tread lightly when changing there #if you change make sure to manually re-import an intellij project and see imports #are resolved (not red) and clickable @@ -130,8 +156,10 @@ scala_import = rule( allow_single_file = True, default = Label("@io_bazel_rules_scala//scala:libPlaceHolderClassToCreateEmptyJarForScalaImport.jar"), ), - "_java_toolchain": attr.label( - default = Label("@bazel_tools//tools/jdk:current_java_toolchain"), + "_singlejar": attr.label( + default = Label("@bazel_tools//tools/jdk:singlejar"), + executable = True, + cfg = "exec", ), }, ) From bff248ee9eba79ffb750881dd428e870de7caae1 Mon Sep 17 00:00:00 2001 From: Vaidas Pilkauskas Date: Mon, 11 Jan 2021 13:29:05 +0200 Subject: [PATCH 05/11] Add scala import stamping test --- .../ScalaImportExposesJarsTest.scala | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/test/src/main/scala/scalarules/test/scala_import/ScalaImportExposesJarsTest.scala b/test/src/main/scala/scalarules/test/scala_import/ScalaImportExposesJarsTest.scala index 24e7b12ec..d646480d3 100644 --- a/test/src/main/scala/scalarules/test/scala_import/ScalaImportExposesJarsTest.scala +++ b/test/src/main/scala/scalarules/test/scala_import/ScalaImportExposesJarsTest.scala @@ -1,10 +1,14 @@ package scalarules.test.scala_import +import java.util.jar.JarFile + import com.google.common.cache.Cache import org.apache.commons.lang3.ArrayUtils -import org.specs2.mutable.SpecificationWithJUnit +import org.specs2.mutable.SpecWithJUnit + +import scala.reflect.{ClassTag, _} -class ScalaImportExposesJarsTest extends SpecificationWithJUnit { +class ScalaImportExposesJarsTest extends SpecWithJUnit { "scala_import" should { "enable using the jars it exposes" in { @@ -12,6 +16,20 @@ class ScalaImportExposesJarsTest extends SpecificationWithJUnit { println(classOf[ArrayUtils]) success } + + "stamps jars with a target label" in { + val targetLabel = "//test/src/main/scala/scalarules/test/scala_import:guava_and_commons_lang" + findTargetLabel[Cache[String, String]] must beSome(targetLabel) + findTargetLabel[ArrayUtils] must beSome(targetLabel) + } + } + + def findTargetLabel[T: ClassTag]: Option[String] = { + val file = classTag[T].runtimeClass.getProtectionDomain.getCodeSource.getLocation.getFile + val jar = new JarFile(file) + val label = jar.getManifest.getMainAttributes.getValue("Target-Label") + jar.close() + Some(label) } } From dda596591af9fe6854922119e8726dd4ea81ee4f Mon Sep 17 00:00:00 2001 From: Vaidas Pilkauskas Date: Mon, 11 Jan 2021 14:05:44 +0200 Subject: [PATCH 06/11] Use execpath instead of location --- third_party/dependency_analyzer/src/test/BUILD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/third_party/dependency_analyzer/src/test/BUILD b/third_party/dependency_analyzer/src/test/BUILD index 8f603fea7..4df2756a4 100644 --- a/third_party/dependency_analyzer/src/test/BUILD +++ b/third_party/dependency_analyzer/src/test/BUILD @@ -3,7 +3,7 @@ licenses(["notice"]) # 3-clause BSD load("//scala:scala.bzl", "scala_junit_test", "scala_test") common_jvm_flags = [ - "-Dplugin.jar.location=$(location //third_party/dependency_analyzer/src/main:dependency_analyzer)", + "-Dplugin.jar.location=$(execpath //third_party/dependency_analyzer/src/main:dependency_analyzer)", "-Dscala.library.location=$(rootpath //external:io_bazel_rules_scala/dependency/scala/scala_library)", "-Dscala.reflect.location=$(rootpath //external:io_bazel_rules_scala/dependency/scala/scala_reflect)", ] From 93384c0a1885346a1f0b20def9ff5a74dc6e3bd2 Mon Sep 17 00:00:00 2001 From: Vaidas Pilkauskas Date: Tue, 12 Jan 2021 13:31:55 +0200 Subject: [PATCH 07/11] Use timestamp normalization for reproducible stamping --- scala/scala_import.bzl | 1 + 1 file changed, 1 insertion(+) diff --git a/scala/scala_import.bzl b/scala/scala_import.bzl index c487a0a5d..5f626496d 100644 --- a/scala/scala_import.bzl +++ b/scala/scala_import.bzl @@ -23,6 +23,7 @@ def _stamp_symlinked_jar(ctx, jar): inputs = [symlink_file], outputs = [stamped_file], arguments = [ + "--normalize", "--sources", symlink_file.path, "--output", From 58df994c95e938fbeeba761c868ac7e49f356e67 Mon Sep 17 00:00:00 2001 From: Vaidas Pilkauskas Date: Tue, 12 Jan 2021 15:26:40 +0200 Subject: [PATCH 08/11] Add test to ensure Manifest attributes are not removed --- .../ScalaImportExposesJarsTest.scala | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/test/src/main/scala/scalarules/test/scala_import/ScalaImportExposesJarsTest.scala b/test/src/main/scala/scalarules/test/scala_import/ScalaImportExposesJarsTest.scala index d646480d3..d0eba25a1 100644 --- a/test/src/main/scala/scalarules/test/scala_import/ScalaImportExposesJarsTest.scala +++ b/test/src/main/scala/scalarules/test/scala_import/ScalaImportExposesJarsTest.scala @@ -1,14 +1,17 @@ package scalarules.test.scala_import +import java.util.jar import java.util.jar.JarFile import com.google.common.cache.Cache import org.apache.commons.lang3.ArrayUtils -import org.specs2.mutable.SpecWithJUnit +import org.specs2.matcher.Matcher +import org.specs2.mutable.SpecificationWithJUnit import scala.reflect.{ClassTag, _} -class ScalaImportExposesJarsTest extends SpecWithJUnit { +class ScalaImportExposesJarsTest extends SpecificationWithJUnit { + val targetLabel = "//test/src/main/scala/scalarules/test/scala_import:guava_and_commons_lang" "scala_import" should { "enable using the jars it exposes" in { @@ -17,19 +20,30 @@ class ScalaImportExposesJarsTest extends SpecWithJUnit { success } - "stamps jars with a target label" in { - val targetLabel = "//test/src/main/scala/scalarules/test/scala_import:guava_and_commons_lang" - findTargetLabel[Cache[String, String]] must beSome(targetLabel) - findTargetLabel[ArrayUtils] must beSome(targetLabel) + "stamp jars with a target label" in { + findManifest[Cache[String, String]] must haveTargetLabel + findManifest[ArrayUtils] must haveTargetLabel + } + + "preserve existing Manifest attributes" in { + findManifest[ArrayUtils] must haveMainAttribute("Bundle-Name") } } - def findTargetLabel[T: ClassTag]: Option[String] = { + def findManifest[T: ClassTag]: jar.Manifest = { val file = classTag[T].runtimeClass.getProtectionDomain.getCodeSource.getLocation.getFile val jar = new JarFile(file) - val label = jar.getManifest.getMainAttributes.getValue("Target-Label") + val manifest = jar.getManifest jar.close() - Some(label) + manifest + } + + def haveTargetLabel: Matcher[jar.Manifest] = haveMainAttribute("Target-Label") + + def haveMainAttribute(attribute: String): Matcher[jar.Manifest] = { + not(beNull[String]) ^^ { (m: jar.Manifest) => + m.getMainAttributes.getValue(attribute) aka s"an attribute $attribute" + } } } From 51b1bc350c1c67f3001ccd07565b06a9b39b7365 Mon Sep 17 00:00:00 2001 From: Vaidas Pilkauskas Date: Wed, 6 Jan 2021 13:32:35 +0200 Subject: [PATCH 09/11] Use custom ijar for stamping --- scala/scala_import.bzl | 40 +- third_party/java_tools/ijar/BUILD | 132 + third_party/java_tools/ijar/LICENSE | 203 ++ third_party/java_tools/ijar/README.txt | 120 + third_party/java_tools/ijar/classfile.cc | 1900 +++++++++++++++ third_party/java_tools/ijar/common.h | 117 + .../java_tools/ijar/dummy_zlib_client.cc | 37 + third_party/java_tools/ijar/ijar.cc | 489 ++++ third_party/java_tools/ijar/mapped_file.h | 83 + .../java_tools/ijar/mapped_file_unix.cc | 159 ++ .../java_tools/ijar/mapped_file_windows.cc | 211 ++ third_party/java_tools/ijar/platform_utils.cc | 125 + third_party/java_tools/ijar/platform_utils.h | 77 + third_party/java_tools/ijar/zip.cc | 1140 +++++++++ third_party/java_tools/ijar/zip.h | 169 ++ third_party/java_tools/ijar/zip_main.cc | 478 ++++ third_party/java_tools/ijar/zlib_client.cc | 162 ++ third_party/java_tools/ijar/zlib_client.h | 69 + .../java_tools/src/main/cpp/util/BUILD | 141 ++ .../src/main/cpp/util/bazel_log_handler.cc | 194 ++ .../src/main/cpp/util/bazel_log_handler.h | 56 + .../java_tools/src/main/cpp/util/errors.h | 33 + .../src/main/cpp/util/errors_posix.cc | 33 + .../src/main/cpp/util/errors_windows.cc | 49 + .../java_tools/src/main/cpp/util/exit_code.h | 51 + .../java_tools/src/main/cpp/util/file.cc | 127 + .../java_tools/src/main/cpp/util/file.h | 92 + .../src/main/cpp/util/file_platform.h | 273 +++ .../src/main/cpp/util/file_posix.cc | 559 +++++ .../src/main/cpp/util/file_windows.cc | 888 +++++++ .../java_tools/src/main/cpp/util/logging.cc | 114 + .../java_tools/src/main/cpp/util/logging.h | 170 ++ .../java_tools/src/main/cpp/util/md5.cc | 345 +++ .../java_tools/src/main/cpp/util/md5.h | 70 + .../java_tools/src/main/cpp/util/numbers.cc | 214 ++ .../java_tools/src/main/cpp/util/numbers.h | 27 + .../java_tools/src/main/cpp/util/path.cc | 51 + .../java_tools/src/main/cpp/util/path.h | 36 + .../src/main/cpp/util/path_platform.h | 206 ++ .../src/main/cpp/util/path_posix.cc | 168 ++ .../src/main/cpp/util/path_windows.cc | 531 ++++ .../java_tools/src/main/cpp/util/port.cc | 38 + .../java_tools/src/main/cpp/util/port.h | 146 ++ .../java_tools/src/main/cpp/util/strings.cc | 391 +++ .../java_tools/src/main/cpp/util/strings.h | 146 ++ third_party/java_tools/zlib/BUILD.bazel | 14 + third_party/java_tools/zlib/LICENSE.txt | 28 + third_party/java_tools/zlib/adler32.c | 186 ++ third_party/java_tools/zlib/compress.c | 86 + third_party/java_tools/zlib/crc32.c | 442 ++++ third_party/java_tools/zlib/crc32.h | 441 ++++ third_party/java_tools/zlib/deflate.c | 2163 +++++++++++++++++ third_party/java_tools/zlib/deflate.h | 349 +++ third_party/java_tools/zlib/gzclose.c | 25 + third_party/java_tools/zlib/gzguts.h | 218 ++ third_party/java_tools/zlib/gzlib.c | 637 +++++ third_party/java_tools/zlib/gzread.c | 654 +++++ third_party/java_tools/zlib/gzwrite.c | 665 +++++ third_party/java_tools/zlib/infback.c | 640 +++++ third_party/java_tools/zlib/inffast.c | 323 +++ third_party/java_tools/zlib/inffast.h | 11 + third_party/java_tools/zlib/inffixed.h | 94 + third_party/java_tools/zlib/inflate.c | 1561 ++++++++++++ third_party/java_tools/zlib/inflate.h | 125 + third_party/java_tools/zlib/inftrees.c | 304 +++ third_party/java_tools/zlib/inftrees.h | 62 + third_party/java_tools/zlib/trees.c | 1203 +++++++++ third_party/java_tools/zlib/trees.h | 128 + third_party/java_tools/zlib/uncompr.c | 93 + third_party/java_tools/zlib/zconf.h | 534 ++++ third_party/java_tools/zlib/zlib.h | 1912 +++++++++++++++ third_party/java_tools/zlib/zutil.c | 325 +++ third_party/java_tools/zlib/zutil.h | 271 +++ 73 files changed, 24333 insertions(+), 21 deletions(-) create mode 100644 third_party/java_tools/ijar/BUILD create mode 100644 third_party/java_tools/ijar/LICENSE create mode 100644 third_party/java_tools/ijar/README.txt create mode 100644 third_party/java_tools/ijar/classfile.cc create mode 100644 third_party/java_tools/ijar/common.h create mode 100644 third_party/java_tools/ijar/dummy_zlib_client.cc create mode 100644 third_party/java_tools/ijar/ijar.cc create mode 100644 third_party/java_tools/ijar/mapped_file.h create mode 100644 third_party/java_tools/ijar/mapped_file_unix.cc create mode 100644 third_party/java_tools/ijar/mapped_file_windows.cc create mode 100644 third_party/java_tools/ijar/platform_utils.cc create mode 100644 third_party/java_tools/ijar/platform_utils.h create mode 100644 third_party/java_tools/ijar/zip.cc create mode 100644 third_party/java_tools/ijar/zip.h create mode 100644 third_party/java_tools/ijar/zip_main.cc create mode 100644 third_party/java_tools/ijar/zlib_client.cc create mode 100644 third_party/java_tools/ijar/zlib_client.h create mode 100644 third_party/java_tools/src/main/cpp/util/BUILD create mode 100644 third_party/java_tools/src/main/cpp/util/bazel_log_handler.cc create mode 100644 third_party/java_tools/src/main/cpp/util/bazel_log_handler.h create mode 100644 third_party/java_tools/src/main/cpp/util/errors.h create mode 100644 third_party/java_tools/src/main/cpp/util/errors_posix.cc create mode 100644 third_party/java_tools/src/main/cpp/util/errors_windows.cc create mode 100644 third_party/java_tools/src/main/cpp/util/exit_code.h create mode 100644 third_party/java_tools/src/main/cpp/util/file.cc create mode 100644 third_party/java_tools/src/main/cpp/util/file.h create mode 100644 third_party/java_tools/src/main/cpp/util/file_platform.h create mode 100644 third_party/java_tools/src/main/cpp/util/file_posix.cc create mode 100644 third_party/java_tools/src/main/cpp/util/file_windows.cc create mode 100644 third_party/java_tools/src/main/cpp/util/logging.cc create mode 100644 third_party/java_tools/src/main/cpp/util/logging.h create mode 100644 third_party/java_tools/src/main/cpp/util/md5.cc create mode 100644 third_party/java_tools/src/main/cpp/util/md5.h create mode 100644 third_party/java_tools/src/main/cpp/util/numbers.cc create mode 100644 third_party/java_tools/src/main/cpp/util/numbers.h create mode 100644 third_party/java_tools/src/main/cpp/util/path.cc create mode 100644 third_party/java_tools/src/main/cpp/util/path.h create mode 100644 third_party/java_tools/src/main/cpp/util/path_platform.h create mode 100644 third_party/java_tools/src/main/cpp/util/path_posix.cc create mode 100644 third_party/java_tools/src/main/cpp/util/path_windows.cc create mode 100644 third_party/java_tools/src/main/cpp/util/port.cc create mode 100644 third_party/java_tools/src/main/cpp/util/port.h create mode 100644 third_party/java_tools/src/main/cpp/util/strings.cc create mode 100644 third_party/java_tools/src/main/cpp/util/strings.h create mode 100644 third_party/java_tools/zlib/BUILD.bazel create mode 100644 third_party/java_tools/zlib/LICENSE.txt create mode 100644 third_party/java_tools/zlib/adler32.c create mode 100644 third_party/java_tools/zlib/compress.c create mode 100644 third_party/java_tools/zlib/crc32.c create mode 100644 third_party/java_tools/zlib/crc32.h create mode 100644 third_party/java_tools/zlib/deflate.c create mode 100644 third_party/java_tools/zlib/deflate.h create mode 100644 third_party/java_tools/zlib/gzclose.c create mode 100644 third_party/java_tools/zlib/gzguts.h create mode 100644 third_party/java_tools/zlib/gzlib.c create mode 100644 third_party/java_tools/zlib/gzread.c create mode 100644 third_party/java_tools/zlib/gzwrite.c create mode 100644 third_party/java_tools/zlib/infback.c create mode 100644 third_party/java_tools/zlib/inffast.c create mode 100644 third_party/java_tools/zlib/inffast.h create mode 100644 third_party/java_tools/zlib/inffixed.h create mode 100644 third_party/java_tools/zlib/inflate.c create mode 100644 third_party/java_tools/zlib/inflate.h create mode 100644 third_party/java_tools/zlib/inftrees.c create mode 100644 third_party/java_tools/zlib/inftrees.h create mode 100644 third_party/java_tools/zlib/trees.c create mode 100644 third_party/java_tools/zlib/trees.h create mode 100644 third_party/java_tools/zlib/uncompr.c create mode 100644 third_party/java_tools/zlib/zconf.h create mode 100644 third_party/java_tools/zlib/zlib.h create mode 100644 third_party/java_tools/zlib/zutil.c create mode 100644 third_party/java_tools/zlib/zutil.h diff --git a/scala/scala_import.bzl b/scala/scala_import.bzl index 5f626496d..0b0ad83f3 100644 --- a/scala/scala_import.bzl +++ b/scala/scala_import.bzl @@ -4,35 +4,32 @@ def _stamp_symlinked_jar(ctx, jar): symlink_file = ctx.actions.declare_file(jar.basename) ctx.actions.symlink(output = symlink_file, target_file = jar) - # TODO: use java_common.stamp_jar version after ijar stamping is fixed: - # https://github.com/bazelbuild/bazel/issues/12730 - # - # return java_common.stamp_jar( - # actions = ctx.actions, - # jar = symlink_file, - # target_label = ctx.label, - # java_toolchain = ctx.attr._java_toolchain[java_common.JavaToolchainInfo], - # ) - stamped_jar_filename = jar.basename.rstrip(".jar") + "-stamped.jar" + + # Preferred way, but currently broken: + # java toolchain's ijar incorrectly handles + # return java_common.stamp_jar( + # actions = ctx.actions, + # jar = symlink_file, + # target_label = ctx.label, + # java_toolchain = ctx.attr._java_toolchain[java_common.JavaToolchainInfo], + # ) + + # Stamp with custom built (https://github.com/bazelbuild/bazel/pull/12771) stamped_file = ctx.actions.declare_file(stamped_jar_filename) - # TODO: use java_common ctx.actions.run( - executable = ctx.executable._singlejar, + executable = ctx.executable.ijar, inputs = [symlink_file], outputs = [stamped_file], arguments = [ - "--normalize", - "--sources", + "--nostrip_jar", + "--target_label", + ctx.label.name, symlink_file.path, - "--output", stamped_file.path, - "--deploy_manifest_lines", - "Target-Label: %s" % ctx.label, ], - mnemonic = "StampTargetLabel", - progress_message = "Stamping manifest of %s" % ctx.label, + mnemonic = "StampWithIjar", ) return stamped_file @@ -157,10 +154,11 @@ scala_import = rule( allow_single_file = True, default = Label("@io_bazel_rules_scala//scala:libPlaceHolderClassToCreateEmptyJarForScalaImport.jar"), ), - "_singlejar": attr.label( - default = Label("@bazel_tools//tools/jdk:singlejar"), + "ijar": attr.label( + default = Label("//third_party/java_tools/ijar:ijar"), executable = True, cfg = "exec", + allow_files = True, ), }, ) diff --git a/third_party/java_tools/ijar/BUILD b/third_party/java_tools/ijar/BUILD new file mode 100644 index 000000000..05b654b1c --- /dev/null +++ b/third_party/java_tools/ijar/BUILD @@ -0,0 +1,132 @@ +package( + default_visibility = [ + "//visibility:public", + ], +) + +licenses(["notice"]) # Apache 2.0 + +cc_library( + name = "zip", + srcs = [ + "mapped_file_unix.cc", + "zip.cc", + ], + hdrs = [ + "common.h", + "mapped_file.h", + "zip.h", + ], + deps = [ + ":platform_utils", + ":zlib_client", + ], +) + +cc_library( + name = "zlib_client", + srcs = ["zlib_client.cc"], + hdrs = [ + "common.h", + "zlib_client.h", + ], + deps = ["//third_party/java_tools/zlib"], +) + +cc_library( + name = "platform_utils", + srcs = ["platform_utils.cc"], + hdrs = [ + "common.h", + "platform_utils.h", + ], + deps = [ + "//third_party/java_tools/src/main/cpp/util:errors", + "//third_party/java_tools/src/main/cpp/util:filesystem", + "//third_party/java_tools/src/main/cpp/util:logging", + ], +) +# +#cc_binary( +# name = "zipper", +# srcs = ["zip_main.cc"], +# visibility = ["//visibility:public"], +# deps = [":zip"], +#) + +cc_binary( + name = "ijar", + srcs = [ + "classfile.cc", + "ijar.cc", + ], + deps = [":zip"], +) + +filegroup( + name = "srcs", + srcs = glob(["**"]), +) + +#filegroup( +# name = "embedded_zipper_sources", +# srcs = [ +# "zip.cc", +# "zip.h", +# "zip_main.cc", +# "common.h", +# "mapped_file.h", +# "platform_utils.cc", +# "platform_utils.h", +# "zlib_client.cc", +# "zlib_client.h", +# "BUILD", +# ] + select({ +# "//src:windows": [ +# "mapped_file_windows.cc", +# ], +# "//conditions:default": [ +# "mapped_file_unix.cc", +# ], +# }), +# visibility = ["//visibility:public"], +#) + +#filegroup( +# name = "transitive_sources", +# srcs = [":srcs"] + ["//src/main/cpp/util:embedded_java_tools"], +#) + +#genrule( +# name = "ijar_transitive_srcs_zip", +# srcs = [ +# ":ijar_srcs_zip", +# "//src:zlib_zip", +# "//src/main/cpp/util:cpp_util_with_deps_zip", +# ], +# outs = ["ijar_srcs_with_deps.zip"], +# cmd = "$(location //src:merge_zip_files) java_tools $@ $(SRCS)", +# tools = ["//src:merge_zip_files"], +#) + +#genrule( +# name = "ijar_deploy_zip", +# srcs = [ +# ":ijar", +# ":zipper", +# ], +# outs = ["ijar_deploy.zip"], +# cmd = "$(location //src:zip_files) java_tools/ijar $@ $(SRCS)", +# tools = ["//src:zip_files"], +#) + +#genrule( +# name = "ijar_srcs_zip", +# srcs = glob( +# ["**"], +# exclude = ["BUILD"], +# ), +# outs = ["ijar_srcs.zip"], +# cmd = "$(location //src:zip_files) ijar $@ $(SRCS)", +# tools = ["//src:zip_files"], +#) diff --git a/third_party/java_tools/ijar/LICENSE b/third_party/java_tools/ijar/LICENSE new file mode 100644 index 000000000..6b0b1270f --- /dev/null +++ b/third_party/java_tools/ijar/LICENSE @@ -0,0 +1,203 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/third_party/java_tools/ijar/README.txt b/third_party/java_tools/ijar/README.txt new file mode 100644 index 000000000..2b404dfa5 --- /dev/null +++ b/third_party/java_tools/ijar/README.txt @@ -0,0 +1,120 @@ + +ijar: A tool for generating interface .jars from normal .jars +============================================================= + +Alan Donovan, 26 May 2007. + +Rationale: + + In order to improve the speed of compilation of Java programs in + Bazel, the output of build steps is cached. + + This works very nicely for C++ compilation: a compilation unit + includes a .cc source file and typically dozens of header files. + Header files change relatively infrequently, so the need for a + rebuild is usually driven by a change in the .cc file. Even after + syncing a slightly newer version of the tree and doing a rebuild, + many hits in the cache are still observed. + + In Java, by contrast, a compilation unit involves a set of .java + source files, plus a set of .jar files containing already-compiled + JVM .class files. Class files serve a dual purpose: from the JVM's + perspective, they are containers of executable code, but from the + compiler's perspective, they are interface definitions. The problem + here is that .jar files are very much more sensitive to change than + C++ header files, so even a change that is insignificant to the + compiler (such as the addition of a print statement to a method in a + prerequisite class) will cause the jar to change, and any code that + depends on this jar's interface will be recompiled unnecessarily. + + The purpose of ijar is to produce, from a .jar file, a much smaller, + simpler .jar file containing only the parts that are significant for + the purposes of compilation. In other words, an interface .jar + file. By changing ones compilation dependencies to be the interface + jar files, unnecessary recompilation is avoided when upstream + changes don't affect the interface. + +Details: + + ijar is a tool that reads a .jar file and emits a .jar file + containing only the parts that are relevant to Java compilation. + For example, it throws away: + + - Files whose name does not end in ".class". + - All executable method code. + - All private methods and fields. + - All constants and attributes except the minimal set necessary to + describe the class interface. + - All debugging information + (LineNumberTable, SourceFile, LocalVariableTables attributes). + + It also sets to zero the file modification times in the index of the + .jar file. + +Implementation: + + ijar is implemented in C++, and runs very quickly. For example + (when optimized) it takes only 530ms to process a 42MB + .jar file containing 5878 classes, resulting in an interface .jar + file of only 11.4MB in size. For more usual .jar sizes of a few + megabytes, a runtime of 50ms is typical. + + The implementation strategy is to mmap both the input jar and the + newly-created _interface.jar, and to scan through the former and + emit the latter in a single pass. There are a couple of locations + where some kind of "backpatching" is required: + + - in the .zip file format, for each file, the size field precedes + the data. We emit a zero but note its location, generate and emit + the stripped classfile, then poke the correct size into the + location. + + - for JVM .class files, the header (including the constant table) + precedes the body, but cannot be emitted before it because it's + not until we emit the body that we know which constants are + referenced and which are garbage. So we emit the body into a + temporary buffer, then emit the header to the output jar, followed + by the contents of the temp buffer. + + Also note that the zip file format has unnecessary duplication of + the index metadata: it has header+data for each file, then another + set of (similar) headers at the end. Rather than save the metadata + explicitly in some datastructure, we just record the addresses of + the already-emitted zip metadata entries in the output file, and + then read from there as necessary. + +Notes: + + This code has no dependency except on the STL and on zlib. + + Almost all of the getX/putX/ReadX/WriteX functions in the code + advance their first argument pointer, which is passed by reference. + + It's tempting to discard package-private classes and class members. + However, this would be incorrect because they are a necessary part + of the package interface, as a Java package is often compiled in + multiple stages. For example: in Bazel, both java tests and java + code inhabit the same Java package but are compiled separately. + +Assumptions: + + We assume that jar files are uncompressed v1.0 zip files (created + with 'jar c0f') with a zero general_purpose_bit_flag. + + We assume that javap/javac don't need the correct CRC checksums in + the .jar file. + + We assume that it's better simply to abort in the face of unknown + input than to risk leaving out something important from the output + (although in the case of annotations, it should be safe to ignore + ones we don't understand). + +TODO: + Maybe: ensure a canonical sort order is used for every list (jar + entries, class members, attributes, etc.) This isn't essential + because we can assume the compiler is deterministic and the order in + the source files changes little. Also, it would require two passes. :( + + Maybe: delete dynamically-allocated memory. + + Add (a lot) more tests. Include a test of idempotency. diff --git a/third_party/java_tools/ijar/classfile.cc b/third_party/java_tools/ijar/classfile.cc new file mode 100644 index 000000000..54fae3d00 --- /dev/null +++ b/third_party/java_tools/ijar/classfile.cc @@ -0,0 +1,1900 @@ +// Copyright 2015 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// classfile.cc -- classfile parsing and stripping. +// + +// TODO(adonovan) don't pass pointers by reference; this is not +// compatible with Google C++ style. + +// See README.txt for details. +// +// For definition of JVM class file format, see: +// Java SE 8 Edition: +// http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4 + +#define __STDC_FORMAT_MACROS 1 +#define __STDC_LIMIT_MACROS 1 +#include // for PRIx32 +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "common.h" + +namespace { +// Converts a value to string. +// Workaround for mingw where std::to_string is not implemented. +// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015. +template +std::string ToString(const T& value) { + std::ostringstream oss; + oss << value; + return oss.str(); +} +} // namespace + +namespace devtools_ijar { + +// See Table 4.3 in JVM Spec. +enum CONSTANT { + CONSTANT_Class = 7, + CONSTANT_FieldRef = 9, + CONSTANT_Methodref = 10, + CONSTANT_Interfacemethodref = 11, + CONSTANT_String = 8, + CONSTANT_Integer = 3, + CONSTANT_Float = 4, + CONSTANT_Long = 5, + CONSTANT_Double = 6, + CONSTANT_NameAndType = 12, + CONSTANT_Utf8 = 1, + CONSTANT_MethodHandle = 15, + CONSTANT_MethodType = 16, + CONSTANT_InvokeDynamic = 18 +}; + +// See Tables 4.1, 4.4, 4.5 in JVM Spec. +enum ACCESS { + ACC_PUBLIC = 0x0001, + ACC_PRIVATE = 0x0002, + ACC_PROTECTED = 0x0004, + ACC_STATIC = 0x0008, + ACC_FINAL = 0x0010, + ACC_SYNCHRONIZED = 0x0020, + ACC_BRIDGE = 0x0040, + ACC_VOLATILE = 0x0040, + ACC_TRANSIENT = 0x0080, + ACC_INTERFACE = 0x0200, + ACC_ABSTRACT = 0x0400, + ACC_SYNTHETIC = 0x1000 +}; + +// See Table 4.7.20-A in Java 8 JVM Spec. +enum TARGET_TYPE { + // Targets for type parameter declarations (ElementType.TYPE_PARAMETER): + CLASS_TYPE_PARAMETER = 0x00, + METHOD_TYPE_PARAMETER = 0x01, + + // Targets for type uses that may be externally visible in classes and members + // (ElementType.TYPE_USE): + CLASS_EXTENDS = 0x10, + CLASS_TYPE_PARAMETER_BOUND = 0x11, + METHOD_TYPE_PARAMETER_BOUND = 0x12, + FIELD = 0x13, + METHOD_RETURN = 0x14, + METHOD_RECEIVER = 0x15, + METHOD_FORMAL_PARAMETER = 0x16, + THROWS = 0x17, + + // TARGET_TYPE >= 0x40 is reserved for type uses that occur only within code + // blocks. Ijar doesn't need to know about these. +}; + +struct Constant; + +// TODO(adonovan) these globals are unfortunate +static std::vector const_pool_in; // input constant pool +static std::vector const_pool_out; // output constant_pool +static std::set used_class_names; +static Constant * class_name; + +// Returns the Constant object, given an index into the input constant pool. +// Note: constant(0) == NULL; this invariant is exploited by the +// InnerClassesAttribute, inter alia. +inline Constant *constant(int idx) { + if (idx < 0 || (unsigned)idx >= const_pool_in.size()) { + fprintf(stderr, "Illegal constant pool index: %d\n", idx); + abort(); + } + return const_pool_in[idx]; +} + +/********************************************************************** + * * + * Constants * + * * + **********************************************************************/ + +// See sec.4.4 of JVM spec. +struct Constant { + + Constant(u1 tag) : + slot_(0), + tag_(tag) {} + + virtual ~Constant() {} + + // For UTF-8 string constants, returns the encoded string. + // Otherwise, returns an undefined string value suitable for debugging. + virtual std::string Display() = 0; + + virtual void Write(u1 *&p) = 0; + + // Called by slot() when a constant has been identified as required + // in the output classfile's constant pool. This is a hook allowing + // constants to register their dependency on other constants, by + // calling slot() on them in turn. + virtual void Keep() {} + + bool Kept() { + return slot_ != 0; + } + + // Returns the index of this constant in the output class's constant + // pool, assigning a slot if not already done. + u2 slot() { + if (slot_ == 0) { + Keep(); + slot_ = const_pool_out.size(); // BugBot's "narrowing" warning + // is bogus. The number of + // output constants can't exceed + // the number of input constants. + if (slot_ == 0) { + fprintf(stderr, "Constant::slot() called before output phase.\n"); + abort(); + } + const_pool_out.push_back(this); + if (tag_ == CONSTANT_Long || tag_ == CONSTANT_Double) { + const_pool_out.push_back(NULL); + } + } + return slot_; + } + + u2 slot_; // zero => "this constant is unreachable garbage" + u1 tag_; +}; + +// Extracts class names from a signature and puts them into the global +// variable used_class_names. +// +// desc: the descriptor class names should be extracted from. +// p: the position where the extraction should tart. +void ExtractClassNames(const std::string& desc, size_t* p); + +// See sec.4.4.1 of JVM spec. +struct Constant_Class : Constant +{ + Constant_Class(u2 name_index) : + Constant(CONSTANT_Class), + name_index_(name_index) {} + + void Write(u1 *&p) { + put_u1(p, tag_); + put_u2be(p, constant(name_index_)->slot()); + } + + std::string Display() { + return constant(name_index_)->Display(); + } + + void Keep() { constant(name_index_)->slot(); } + + u2 name_index_; +}; + +// See sec.4.4.2 of JVM spec. +struct Constant_FMIref : Constant +{ + Constant_FMIref(u1 tag, + u2 class_index, + u2 name_type_index) : + Constant(tag), + class_index_(class_index), + name_type_index_(name_type_index) {} + + void Write(u1 *&p) { + put_u1(p, tag_); + put_u2be(p, constant(class_index_)->slot()); + put_u2be(p, constant(name_type_index_)->slot()); + } + + std::string Display() { + return constant(class_index_)->Display() + "::" + + constant(name_type_index_)->Display(); + } + + void Keep() { + constant(class_index_)->slot(); + constant(name_type_index_)->slot(); + } + + u2 class_index_; + u2 name_type_index_; +}; + +// See sec.4.4.3 of JVM spec. +struct Constant_String : Constant +{ + Constant_String(u2 string_index) : + Constant(CONSTANT_String), + string_index_(string_index) {} + + void Write(u1 *&p) { + put_u1(p, tag_); + put_u2be(p, constant(string_index_)->slot()); + } + + std::string Display() { + return "\"" + constant(string_index_)->Display() + "\""; + } + + void Keep() { constant(string_index_)->slot(); } + + u2 string_index_; +}; + +// See sec.4.4.4 of JVM spec. +struct Constant_IntegerOrFloat : Constant +{ + Constant_IntegerOrFloat(u1 tag, u4 bytes) : + Constant(tag), + bytes_(bytes) {} + + void Write(u1 *&p) { + put_u1(p, tag_); + put_u4be(p, bytes_); + } + + std::string Display() { return "int/float"; } + + u4 bytes_; +}; + +// See sec.4.4.5 of JVM spec. +struct Constant_LongOrDouble : Constant_IntegerOrFloat +{ + Constant_LongOrDouble(u1 tag, u4 high_bytes, u4 low_bytes) : + Constant_IntegerOrFloat(tag, high_bytes), + low_bytes_(low_bytes) {} + + void Write(u1 *&p) { + put_u1(p, tag_); + put_u4be(p, bytes_); + put_u4be(p, low_bytes_); + } + + std::string Display() { return "long/double"; } + + u4 low_bytes_; +}; + +// See sec.4.4.6 of JVM spec. +struct Constant_NameAndType : Constant +{ + Constant_NameAndType(u2 name_index, u2 descr_index) : + Constant(CONSTANT_NameAndType), + name_index_(name_index), + descr_index_(descr_index) {} + + void Write(u1 *&p) { + put_u1(p, tag_); + put_u2be(p, constant(name_index_)->slot()); + put_u2be(p, constant(descr_index_)->slot()); + } + + std::string Display() { + return constant(name_index_)->Display() + "::" + + constant(descr_index_)->Display(); + } + + void Keep() { + constant(name_index_)->slot(); + constant(descr_index_)->slot(); + } + + u2 name_index_; + u2 descr_index_; +}; + +// See sec.4.4.7 of JVM spec. +struct Constant_Utf8 : Constant +{ + Constant_Utf8(u4 length, const u1 *utf8) : + Constant(CONSTANT_Utf8), + length_(length), + utf8_(utf8) {} + + void Write(u1 *&p) { + put_u1(p, tag_); + put_u2be(p, length_); + put_n(p, utf8_, length_); + } + + std::string Display() { + return std::string((const char*) utf8_, length_); + } + + u4 length_; + const u1 *utf8_; +}; + +// See sec.4.4.8 of JVM spec. +struct Constant_MethodHandle : Constant +{ + Constant_MethodHandle(u1 reference_kind, u2 reference_index) : + Constant(CONSTANT_MethodHandle), + reference_kind_(reference_kind), + reference_index_(reference_index) {} + + void Write(u1 *&p) { + put_u1(p, tag_); + put_u1(p, reference_kind_); + put_u2be(p, reference_index_); + } + + std::string Display() { + return "Constant_MethodHandle::" + ToString(reference_kind_) + "::" + + constant(reference_index_)->Display(); + } + + u1 reference_kind_; + u2 reference_index_; +}; + +// See sec.4.4.9 of JVM spec. +struct Constant_MethodType : Constant +{ + Constant_MethodType(u2 descriptor_index) : + Constant(CONSTANT_MethodType), + descriptor_index_(descriptor_index) {} + + void Write(u1 *&p) { + put_u1(p, tag_); + put_u2be(p, descriptor_index_); + } + + std::string Display() { + return "Constant_MethodType::" + constant(descriptor_index_)->Display(); + } + + u2 descriptor_index_; +}; + +// See sec.4.4.10 of JVM spec. +struct Constant_InvokeDynamic : Constant +{ + Constant_InvokeDynamic(u2 bootstrap_method_attr_index, u2 name_and_type_index) : + Constant(CONSTANT_InvokeDynamic), + bootstrap_method_attr_index_(bootstrap_method_attr_index), + name_and_type_index_(name_and_type_index) {} + + void Write(u1 *&p) { + put_u1(p, tag_); + put_u2be(p, bootstrap_method_attr_index_); + put_u2be(p, name_and_type_index_); + } + + std::string Display() { + return "Constant_InvokeDynamic::" + + ToString(bootstrap_method_attr_index_) + "::" + + constant(name_and_type_index_)->Display(); + } + + u2 bootstrap_method_attr_index_; + u2 name_and_type_index_; +}; + +/********************************************************************** + * * + * Attributes * + * * + **********************************************************************/ + +// See sec.4.7 of JVM spec. +struct Attribute { + + virtual ~Attribute() {} + virtual void Write(u1 *&p) = 0; + virtual void ExtractClassNames() {} + + void WriteProlog(u1 *&p, u2 length) { + put_u2be(p, attribute_name_->slot()); + put_u4be(p, length); + } + + Constant *attribute_name_; +}; + +struct KeepForCompileAttribute : Attribute { + void Write(u1 *&p) { WriteProlog(p, 0); } +}; + +// See sec.4.7.5 of JVM spec. +struct ExceptionsAttribute : Attribute { + + static ExceptionsAttribute* Read(const u1 *&p, Constant *attribute_name) { + ExceptionsAttribute *attr = new ExceptionsAttribute; + attr->attribute_name_ = attribute_name; + u2 number_of_exceptions = get_u2be(p); + for (int ii = 0; ii < number_of_exceptions; ++ii) { + attr->exceptions_.push_back(constant(get_u2be(p))); + } + return attr; + } + + void Write(u1 *&p) { + WriteProlog(p, exceptions_.size() * 2 + 2); + put_u2be(p, exceptions_.size()); + for (size_t ii = 0; ii < exceptions_.size(); ++ii) { + put_u2be(p, exceptions_[ii]->slot()); + } + } + + std::vector exceptions_; +}; + +// See sec.4.7.6 of JVM spec. +struct InnerClassesAttribute : Attribute { + + struct Entry { + Constant *inner_class_info; + Constant *outer_class_info; + Constant *inner_name; + u2 inner_class_access_flags; + }; + + virtual ~InnerClassesAttribute() { + for (size_t i = 0; i < entries_.size(); i++) { + delete entries_[i]; + } + } + + static InnerClassesAttribute* Read(const u1 *&p, Constant *attribute_name) { + InnerClassesAttribute *attr = new InnerClassesAttribute; + attr->attribute_name_ = attribute_name; + + u2 number_of_classes = get_u2be(p); + for (int ii = 0; ii < number_of_classes; ++ii) { + Entry *entry = new Entry; + entry->inner_class_info = constant(get_u2be(p)); + entry->outer_class_info = constant(get_u2be(p)); + entry->inner_name = constant(get_u2be(p)); + entry->inner_class_access_flags = get_u2be(p); + + attr->entries_.push_back(entry); + } + return attr; + } + + void Write(u1 *&p) { + std::set kept_entries; + // We keep an entry if the constant referring to the inner class is already + // kept. Then we mark its outer class and its class name as kept, too, then + // iterate until a fixed point is reached. + int entry_count; + int iteration = 0; + + do { + entry_count = kept_entries.size(); + for (int i_entry = 0; i_entry < static_cast(entries_.size()); + ++i_entry) { + Entry* entry = entries_[i_entry]; + if (entry->inner_class_info->Kept() || + used_class_names.find(entry->inner_class_info->Display()) != + used_class_names.end() || + entry->outer_class_info == class_name) { + if (entry->inner_name == NULL) { + // JVMS 4.7.6: inner_name_index is zero iff the class is anonymous + continue; + } + + kept_entries.insert(i_entry); + + // JVMS 4.7.6: outer_class_info_index is zero for top-level classes + if (entry->outer_class_info != NULL) { + entry->outer_class_info->slot(); + } + + entry->inner_name->slot(); + } + } + iteration += 1; + } while (entry_count != static_cast(kept_entries.size())); + + if (kept_entries.empty()) { + return; + } + + WriteProlog(p, 2 + kept_entries.size() * 8); + put_u2be(p, kept_entries.size()); + + for (std::set::iterator it = kept_entries.begin(); + it != kept_entries.end(); + ++it) { + Entry *entry = entries_[*it]; + put_u2be(p, entry->inner_class_info == NULL + ? 0 + : entry->inner_class_info->slot()); + put_u2be(p, entry->outer_class_info == NULL + ? 0 + : entry->outer_class_info->slot()); + put_u2be(p, entry->inner_name == NULL + ? 0 + : entry->inner_name->slot()); + put_u2be(p, entry->inner_class_access_flags); + } + } + + std::vector entries_; +}; + +// See sec.4.7.7 of JVM spec. +// We preserve EnclosingMethod attributes to be able to identify local and +// anonymous classes. These classes will be stripped of most content, as they +// represent implementation details that shoudn't leak into the ijars. Omitting +// EnclosingMethod attributes can lead to type-checking failures in the presence +// of generics (see b/9070939). +struct EnclosingMethodAttribute : Attribute { + + static EnclosingMethodAttribute* Read(const u1 *&p, + Constant *attribute_name) { + EnclosingMethodAttribute *attr = new EnclosingMethodAttribute; + attr->attribute_name_ = attribute_name; + attr->class_ = constant(get_u2be(p)); + attr->method_ = constant(get_u2be(p)); + return attr; + } + + void Write(u1 *&p) { + WriteProlog(p, 4); + put_u2be(p, class_->slot()); + put_u2be(p, method_ == NULL ? 0 : method_->slot()); + } + + Constant *class_; + Constant *method_; +}; + +// See sec.4.7.16.1 of JVM spec. +// Used by AnnotationDefault and other attributes. +struct ElementValue { + virtual ~ElementValue() {} + virtual void Write(u1 *&p) = 0; + virtual void ExtractClassNames() {} + static ElementValue* Read(const u1 *&p); + u1 tag_; + u4 length_; +}; + +struct BaseTypeElementValue : ElementValue { + void Write(u1 *&p) { + put_u1(p, tag_); + put_u2be(p, const_value_->slot()); + } + static BaseTypeElementValue *Read(const u1 *&p) { + BaseTypeElementValue *value = new BaseTypeElementValue; + value->const_value_ = constant(get_u2be(p)); + return value; + } + Constant *const_value_; +}; + +struct EnumTypeElementValue : ElementValue { + void Write(u1 *&p) { + put_u1(p, tag_); + put_u2be(p, type_name_->slot()); + put_u2be(p, const_name_->slot()); + } + static EnumTypeElementValue *Read(const u1 *&p) { + EnumTypeElementValue *value = new EnumTypeElementValue; + value->type_name_ = constant(get_u2be(p)); + value->const_name_ = constant(get_u2be(p)); + return value; + } + Constant *type_name_; + Constant *const_name_; +}; + +struct ClassTypeElementValue : ElementValue { + void Write(u1 *&p) { + put_u1(p, tag_); + put_u2be(p, class_info_->slot()); + } + + virtual void ExtractClassNames() { + size_t idx = 0; + devtools_ijar::ExtractClassNames(class_info_->Display(), &idx); + } + + static ClassTypeElementValue *Read(const u1 *&p) { + ClassTypeElementValue *value = new ClassTypeElementValue; + value->class_info_ = constant(get_u2be(p)); + return value; + } + Constant *class_info_; +}; + +struct ArrayTypeElementValue : ElementValue { + virtual ~ArrayTypeElementValue() { + for (const auto *value : values_) { + delete value; + } + } + + virtual void ExtractClassNames() { + for (auto *value : values_) { + value->ExtractClassNames(); + } + } + + void Write(u1 *&p) { + put_u1(p, tag_); + put_u2be(p, values_.size()); + for (auto *value : values_) { + value->Write(p); + } + } + static ArrayTypeElementValue *Read(const u1 *&p) { + ArrayTypeElementValue *value = new ArrayTypeElementValue; + u2 num_values = get_u2be(p); + for (int ii = 0; ii < num_values; ++ii) { + value->values_.push_back(ElementValue::Read(p)); + } + return value; + } + std::vector values_; +}; + +// See sec.4.7.16 of JVM spec. +struct Annotation { + virtual ~Annotation() { + for (size_t i = 0; i < element_value_pairs_.size(); i++) { + delete element_value_pairs_[i]->element_value_; + delete element_value_pairs_[i]; + } + } + + void ExtractClassNames() { + for (size_t i = 0; i < element_value_pairs_.size(); i++) { + element_value_pairs_[i]->element_value_->ExtractClassNames(); + } + } + + void Write(u1 *&p) { + put_u2be(p, type_->slot()); + put_u2be(p, element_value_pairs_.size()); + for (size_t ii = 0; ii < element_value_pairs_.size(); ++ii) { + put_u2be(p, element_value_pairs_[ii]->element_name_->slot()); + element_value_pairs_[ii]->element_value_->Write(p); + } + } + static Annotation *Read(const u1 *&p) { + Annotation *value = new Annotation; + value->type_ = constant(get_u2be(p)); + u2 num_element_value_pairs = get_u2be(p); + for (int ii = 0; ii < num_element_value_pairs; ++ii) { + ElementValuePair *pair = new ElementValuePair; + pair->element_name_ = constant(get_u2be(p)); + pair->element_value_ = ElementValue::Read(p); + value->element_value_pairs_.push_back(pair); + } + return value; + } + Constant *type_; + struct ElementValuePair { + Constant *element_name_; + ElementValue *element_value_; + }; + std::vector element_value_pairs_; +}; + +// See sec 4.7.20 of Java 8 JVM Spec +// +// Each entry in the annotations table represents a single run-time visible +// annotation on a type used in a declaration or expression. The type_annotation +// structure has the following format: +// +// type_annotation { +// u1 target_type; +// union { +// type_parameter_target; +// supertype_target; +// type_parameter_bound_target; +// empty_target; +// method_formal_parameter_target; +// throws_target; +// localvar_target; +// catch_target; +// offset_target; +// type_argument_target; +// } target_info; +// type_path target_path; +// u2 type_index; +// u2 num_element_value_pairs; +// { +// u2 element_name_index; +// element_value value; +// } +// element_value_pairs[num_element_value_pairs]; +// } +// +struct TypeAnnotation { + virtual ~TypeAnnotation() { + delete target_info_; + delete type_path_; + delete annotation_; + } + + void ExtractClassNames() { + annotation_->ExtractClassNames(); + } + + void Write(u1 *&p) { + put_u1(p, target_type_); + target_info_->Write(p); + type_path_->Write(p); + annotation_->Write(p); + } + + static TypeAnnotation *Read(const u1 *&p) { + TypeAnnotation *value = new TypeAnnotation; + value->target_type_ = get_u1(p); + value->target_info_ = ReadTargetInfo(p, value->target_type_); + value->type_path_ = TypePath::Read(p); + value->annotation_ = Annotation::Read(p); + return value; + } + + struct TargetInfo { + virtual ~TargetInfo() {} + virtual void Write(u1 *&p) = 0; + }; + + struct TypeParameterTargetInfo : TargetInfo { + void Write(u1 *&p) { + put_u1(p, type_parameter_index_); + } + static TypeParameterTargetInfo *Read(const u1 *&p) { + TypeParameterTargetInfo *value = new TypeParameterTargetInfo; + value->type_parameter_index_ = get_u1(p); + return value; + } + u1 type_parameter_index_; + }; + + struct ClassExtendsInfo : TargetInfo { + void Write(u1 *&p) { + put_u2be(p, supertype_index_); + } + static ClassExtendsInfo *Read(const u1 *&p) { + ClassExtendsInfo *value = new ClassExtendsInfo; + value->supertype_index_ = get_u2be(p); + return value; + } + u2 supertype_index_; + }; + + struct TypeParameterBoundInfo : TargetInfo { + void Write(u1 *&p) { + put_u1(p, type_parameter_index_); + put_u1(p, bound_index_); + } + static TypeParameterBoundInfo *Read(const u1 *&p) { + TypeParameterBoundInfo *value = new TypeParameterBoundInfo; + value->type_parameter_index_ = get_u1(p); + value->bound_index_ = get_u1(p); + return value; + } + u1 type_parameter_index_; + u1 bound_index_; + }; + + struct EmptyInfo : TargetInfo { + void Write(u1 *& /*p*/) {} + static EmptyInfo *Read(const u1 *& /*p*/) { return new EmptyInfo; } + }; + + struct MethodFormalParameterInfo : TargetInfo { + void Write(u1 *&p) { + put_u1(p, method_formal_parameter_index_); + } + static MethodFormalParameterInfo *Read(const u1 *&p) { + MethodFormalParameterInfo *value = new MethodFormalParameterInfo; + value->method_formal_parameter_index_ = get_u1(p); + return value; + } + u1 method_formal_parameter_index_; + }; + + struct ThrowsTypeInfo : TargetInfo { + void Write(u1 *&p) { + put_u2be(p, throws_type_index_); + } + static ThrowsTypeInfo *Read(const u1 *&p) { + ThrowsTypeInfo *value = new ThrowsTypeInfo; + value->throws_type_index_ = get_u2be(p); + return value; + } + u2 throws_type_index_; + }; + + static TargetInfo *ReadTargetInfo(const u1 *&p, u1 target_type) { + switch (target_type) { + case CLASS_TYPE_PARAMETER: + case METHOD_TYPE_PARAMETER: + return TypeParameterTargetInfo::Read(p); + case CLASS_EXTENDS: + return ClassExtendsInfo::Read(p); + case CLASS_TYPE_PARAMETER_BOUND: + case METHOD_TYPE_PARAMETER_BOUND: + return TypeParameterBoundInfo::Read(p); + case FIELD: + case METHOD_RETURN: + case METHOD_RECEIVER: + return new EmptyInfo; + case METHOD_FORMAL_PARAMETER: + return MethodFormalParameterInfo::Read(p); + case THROWS: + return ThrowsTypeInfo::Read(p); + default: + fprintf(stderr, "Illegal type annotation target type: %d\n", + target_type); + abort(); + } + } + + struct TypePath { + void Write(u1 *&p) { + put_u1(p, path_.size()); + for (TypePathEntry entry : path_) { + put_u1(p, entry.type_path_kind_); + put_u1(p, entry.type_argument_index_); + } + } + static TypePath *Read(const u1 *&p) { + TypePath *value = new TypePath; + u1 path_length = get_u1(p); + for (int ii = 0; ii < path_length; ++ii) { + TypePathEntry entry; + entry.type_path_kind_ = get_u1(p); + entry.type_argument_index_ = get_u1(p); + value->path_.push_back(entry); + } + return value; + } + + struct TypePathEntry { + u1 type_path_kind_; + u1 type_argument_index_; + }; + std::vector path_; + }; + + u1 target_type_; + TargetInfo *target_info_; + TypePath *type_path_; + Annotation *annotation_; +}; + +struct AnnotationTypeElementValue : ElementValue { + virtual ~AnnotationTypeElementValue() { + delete annotation_; + } + + void Write(u1 *&p) { + put_u1(p, tag_); + annotation_->Write(p); + } + static AnnotationTypeElementValue *Read(const u1 *&p) { + AnnotationTypeElementValue *value = new AnnotationTypeElementValue; + value->annotation_ = Annotation::Read(p); + return value; + } + + Annotation *annotation_; +}; + +ElementValue* ElementValue::Read(const u1 *&p) { + const u1* start = p; + ElementValue *result; + u1 tag = get_u1(p); + if (tag != 0 && strchr("BCDFIJSZs", (char) tag) != NULL) { + result = BaseTypeElementValue::Read(p); + } else if ((char) tag == 'e') { + result = EnumTypeElementValue::Read(p); + } else if ((char) tag == 'c') { + result = ClassTypeElementValue::Read(p); + } else if ((char) tag == '[') { + result = ArrayTypeElementValue::Read(p); + } else if ((char) tag == '@') { + result = AnnotationTypeElementValue::Read(p); + } else { + fprintf(stderr, "Illegal element_value::tag: %d\n", tag); + abort(); + } + result->tag_ = tag; + result->length_ = p - start; + return result; +} + +// See sec.4.7.20 of JVM spec. +// We preserve AnnotationDefault attributes because they are required +// in order to make use of an annotation in new code. +struct AnnotationDefaultAttribute : Attribute { + virtual ~AnnotationDefaultAttribute() { + delete default_value_; + } + + static AnnotationDefaultAttribute* Read(const u1 *&p, + Constant *attribute_name) { + AnnotationDefaultAttribute *attr = new AnnotationDefaultAttribute; + attr->attribute_name_ = attribute_name; + attr->default_value_ = ElementValue::Read(p); + return attr; + } + + void Write(u1 *&p) { + WriteProlog(p, default_value_->length_); + default_value_->Write(p); + } + + virtual void ExtractClassNames() { + default_value_->ExtractClassNames(); + } + + ElementValue *default_value_; +}; + +// See sec.4.7.2 of JVM spec. +// We preserve ConstantValue attributes because they are required for +// compile-time constant propagation. +struct ConstantValueAttribute : Attribute { + + static ConstantValueAttribute* Read(const u1 *&p, Constant *attribute_name) { + ConstantValueAttribute *attr = new ConstantValueAttribute; + attr->attribute_name_ = attribute_name; + attr->constantvalue_ = constant(get_u2be(p)); + return attr; + } + + void Write(u1 *&p) { + WriteProlog(p, 2); + put_u2be(p, constantvalue_->slot()); + } + + Constant *constantvalue_; +}; + +// See sec.4.7.9 of JVM spec. +// We preserve Signature attributes because they are required by the +// compiler for type-checking of generics. +struct SignatureAttribute : Attribute { + + static SignatureAttribute* Read(const u1 *&p, Constant *attribute_name) { + SignatureAttribute *attr = new SignatureAttribute; + attr->attribute_name_ = attribute_name; + attr->signature_ = constant(get_u2be(p)); + return attr; + } + + void Write(u1 *&p) { + WriteProlog(p, 2); + put_u2be(p, signature_->slot()); + } + + virtual void ExtractClassNames() { + size_t signature_idx = 0; + devtools_ijar::ExtractClassNames(signature_->Display(), &signature_idx); + } + + Constant *signature_; +}; + +// See sec.4.7.15 of JVM spec. +// We preserve Deprecated attributes because they are required by the +// compiler to generate warning messages. +struct DeprecatedAttribute : Attribute { + static DeprecatedAttribute *Read(const u1 *& /*p*/, + Constant *attribute_name) { + DeprecatedAttribute *attr = new DeprecatedAttribute; + attr->attribute_name_ = attribute_name; + return attr; + } + + void Write(u1 *&p) { + WriteProlog(p, 0); + } +}; + +// See sec.4.7.16-17 of JVM spec v3. Includes RuntimeVisible and +// RuntimeInvisible. +// +// We preserve all annotations. +struct AnnotationsAttribute : Attribute { + virtual ~AnnotationsAttribute() { + for (size_t i = 0; i < annotations_.size(); i++) { + delete annotations_[i]; + } + } + + static AnnotationsAttribute* Read(const u1 *&p, Constant *attribute_name) { + AnnotationsAttribute *attr = new AnnotationsAttribute; + attr->attribute_name_ = attribute_name; + u2 num_annotations = get_u2be(p); + for (int ii = 0; ii < num_annotations; ++ii) { + Annotation *annotation = Annotation::Read(p); + attr->annotations_.push_back(annotation); + } + return attr; + } + + virtual void ExtractClassNames() { + for (auto *annotation : annotations_) { + annotation->ExtractClassNames(); + } + } + + void Write(u1 *&p) { + WriteProlog(p, -1); + u1 *payload_start = p - 4; + put_u2be(p, annotations_.size()); + for (auto *annotation : annotations_) { + annotation->Write(p); + } + put_u4be(payload_start, p - 4 - payload_start); // backpatch length + } + + std::vector annotations_; +}; + +// See sec.4.7.18-19 of JVM spec. Includes RuntimeVisible and +// RuntimeInvisible. +// +// We preserve all annotations. +struct ParameterAnnotationsAttribute : Attribute { + + static ParameterAnnotationsAttribute* Read(const u1 *&p, + Constant *attribute_name) { + ParameterAnnotationsAttribute *attr = new ParameterAnnotationsAttribute; + attr->attribute_name_ = attribute_name; + u1 num_parameters = get_u1(p); + for (int ii = 0; ii < num_parameters; ++ii) { + std::vector annotations; + u2 num_annotations = get_u2be(p); + for (int ii = 0; ii < num_annotations; ++ii) { + Annotation *annotation = Annotation::Read(p); + annotations.push_back(annotation); + } + attr->parameter_annotations_.push_back(annotations); + } + return attr; + } + + virtual void ExtractClassNames() { + for (size_t i = 0; i < parameter_annotations_.size(); i++) { + const std::vector& annotations = parameter_annotations_[i]; + for (size_t j = 0; j < annotations.size(); j++) { + annotations[j]->ExtractClassNames(); + } + } + } + + void Write(u1 *&p) { + WriteProlog(p, -1); + u1 *payload_start = p - 4; + put_u1(p, parameter_annotations_.size()); + for (size_t ii = 0; ii < parameter_annotations_.size(); ++ii) { + std::vector &annotations = parameter_annotations_[ii]; + put_u2be(p, annotations.size()); + for (size_t jj = 0; jj < annotations.size(); ++jj) { + annotations[jj]->Write(p); + } + } + put_u4be(payload_start, p - 4 - payload_start); // backpatch length + } + + std::vector > parameter_annotations_; +}; + +// See sec.4.7.20 of Java 8 JVM spec. Includes RuntimeVisibleTypeAnnotations +// and RuntimeInvisibleTypeAnnotations. +struct TypeAnnotationsAttribute : Attribute { + static TypeAnnotationsAttribute *Read(const u1 *&p, Constant *attribute_name, + u4 /*attribute_length*/) { + auto attr = new TypeAnnotationsAttribute; + attr->attribute_name_ = attribute_name; + u2 num_annotations = get_u2be(p); + for (int ii = 0; ii < num_annotations; ++ii) { + TypeAnnotation *annotation = TypeAnnotation::Read(p); + attr->type_annotations_.push_back(annotation); + } + return attr; + } + + virtual void ExtractClassNames() { + for (auto *type_annotation : type_annotations_) { + type_annotation->ExtractClassNames(); + } + } + + void Write(u1 *&p) { + WriteProlog(p, -1); + u1 *payload_start = p - 4; + put_u2be(p, type_annotations_.size()); + for (TypeAnnotation *annotation : type_annotations_) { + annotation->Write(p); + } + put_u4be(payload_start, p - 4 - payload_start); // backpatch length + } + + std::vector type_annotations_; +}; + +// See JVMS §4.7.24 +struct MethodParametersAttribute : Attribute { + static MethodParametersAttribute *Read(const u1 *&p, Constant *attribute_name, + u4 /*attribute_length*/) { + auto attr = new MethodParametersAttribute; + attr->attribute_name_ = attribute_name; + u1 parameters_count = get_u1(p); + for (int ii = 0; ii < parameters_count; ++ii) { + MethodParameter* parameter = new MethodParameter; + parameter->name_ = constant(get_u2be(p)); + parameter->access_flags_ = get_u2be(p); + attr->parameters_.push_back(parameter); + } + return attr; + } + + void Write(u1 *&p) { + WriteProlog(p, -1); + u1 *payload_start = p - 4; + put_u1(p, parameters_.size()); + for (MethodParameter* parameter : parameters_) { + put_u2be(p, parameter->name_->slot()); + put_u2be(p, parameter->access_flags_); + } + put_u4be(payload_start, p - 4 - payload_start); // backpatch length + } + + struct MethodParameter { + Constant *name_; + u2 access_flags_; + }; + + std::vector parameters_; +}; + +// See JVMS §4.7.28 +struct NestHostAttribute : Attribute { + static NestHostAttribute *Read(const u1 *&p, Constant *attribute_name, + u4 /*attribute_length*/) { + auto attr = new NestHostAttribute; + attr->attribute_name_ = attribute_name; + attr->host_class_index_ = constant(get_u2be(p)); + return attr; + } + + void Write(u1 *&p) { + WriteProlog(p, 2); + put_u2be(p, host_class_index_->slot()); + } + + Constant *host_class_index_; +}; + +// See JVMS §4.7.29 +struct NestMembersAttribute : Attribute { + static NestMembersAttribute *Read(const u1 *&p, Constant *attribute_name, + u4 /*attribute_length*/) { + auto attr = new NestMembersAttribute; + attr->attribute_name_ = attribute_name; + u2 number_of_classes = get_u2be(p); + for (int ii = 0; ii < number_of_classes; ++ii) { + attr->classes_.push_back(constant(get_u2be(p))); + } + return attr; + } + + void Write(u1 *&p) { + WriteProlog(p, classes_.size() * 2 + 2); + put_u2be(p, classes_.size()); + for (size_t ii = 0; ii < classes_.size(); ++ii) { + put_u2be(p, classes_[ii]->slot()); + } + } + + std::vector classes_; +}; + +struct GeneralAttribute : Attribute { + static GeneralAttribute* Read(const u1 *&p, Constant *attribute_name, + u4 attribute_length) { + auto attr = new GeneralAttribute; + attr->attribute_name_ = attribute_name; + attr->attribute_length_ = attribute_length; + attr->attribute_content_ = p; + p += attribute_length; + return attr; + } + + void Write(u1 *&p) { + WriteProlog(p, attribute_length_); + put_n(p, attribute_content_, attribute_length_); + } + + u4 attribute_length_; + const u1 *attribute_content_; +}; + +/********************************************************************** + * * + * ClassFile * + * * + **********************************************************************/ + +struct HasAttrs { + std::vector attributes; + + void WriteAttrs(u1 *&p); + void ReadAttrs(const u1 *&p); + + virtual ~HasAttrs() { + for (const auto *attribute : attributes) { + delete attribute; + } + } + + void ExtractClassNames() { + for (auto *attribute : attributes) { + attribute->ExtractClassNames(); + } + } +}; + +// A field or method. +// See sec.4.5 and 4.6 of JVM spec. +struct Member : HasAttrs { + u2 access_flags; + Constant *name; + Constant *descriptor; + + static Member* Read(const u1 *&p) { + Member *m = new Member; + m->access_flags = get_u2be(p); + m->name = constant(get_u2be(p)); + m->descriptor = constant(get_u2be(p)); + m->ReadAttrs(p); + return m; + } + + void Write(u1 *&p) { + put_u2be(p, access_flags); + put_u2be(p, name->slot()); + put_u2be(p, descriptor->slot()); + WriteAttrs(p); + } +}; + +// See sec.4.1 of JVM spec. +struct ClassFile : HasAttrs { + + size_t length; + + // Header: + u4 magic; + u2 major; + u2 minor; + + // Body: + u2 access_flags; + Constant *this_class; + Constant *super_class; + std::vector interfaces; + std::vector fields; + std::vector methods; + + virtual ~ClassFile() { + for (size_t i = 0; i < fields.size(); i++) { + delete fields[i]; + } + + for (size_t i = 0; i < methods.size(); i++) { + delete methods[i]; + } + + // Constants do not need to be deleted; they are owned by the constant pool. + } + + void WriteClass(u1 *&p); + + bool ReadConstantPool(const u1 *&p); + + bool IsExplicitlyKept(); + + bool IsLocalOrAnonymous(); + + void WriteHeader(u1 *&p) { + put_u4be(p, magic); + put_u2be(p, major); + put_u2be(p, minor); + + put_u2be(p, const_pool_out.size()); + for (u2 ii = 1; ii < const_pool_out.size(); ++ii) { + if (const_pool_out[ii] != NULL) { // NB: NULLs appear after long/double. + const_pool_out[ii]->Write(p); + } + } + } + + void WriteBody(u1 *&p) { + put_u2be(p, access_flags); + put_u2be(p, this_class->slot()); + put_u2be(p, super_class == NULL ? 0 : super_class->slot()); + put_u2be(p, interfaces.size()); + for (size_t ii = 0; ii < interfaces.size(); ++ii) { + put_u2be(p, interfaces[ii]->slot()); + } + put_u2be(p, fields.size()); + for (size_t ii = 0; ii < fields.size(); ++ii) { + fields[ii]->Write(p); + } + put_u2be(p, methods.size()); + for (size_t ii = 0; ii < methods.size(); ++ii) { + methods[ii]->Write(p); + } + + Attribute* inner_classes = NULL; + + // Make the inner classes attribute the last, so that it can know which + // constants were needed + for (size_t ii = 0; ii < attributes.size(); ii++) { + if (attributes[ii]->attribute_name_->Display() == "InnerClasses") { + inner_classes = attributes[ii]; + attributes.erase(attributes.begin() + ii); + break; + } + } + + if (inner_classes != NULL) { + attributes.push_back(inner_classes); + } + + WriteAttrs(p); + } + +}; + +void HasAttrs::ReadAttrs(const u1 *&p) { + u2 attributes_count = get_u2be(p); + for (int ii = 0; ii < attributes_count; ii++) { + Constant *attribute_name = constant(get_u2be(p)); + u4 attribute_length = get_u4be(p); + + std::string attr_name = attribute_name->Display(); + if (attr_name == "SourceFile" || + attr_name == "StackMapTable" || + attr_name == "LineNumberTable" || + attr_name == "LocalVariableTable" || + attr_name == "LocalVariableTypeTable" || + attr_name == "Code" || + attr_name == "Synthetic" || + attr_name == "BootstrapMethods" || + attr_name == "SourceDebugExtension") { + p += attribute_length; // drop these attributes + } else if (attr_name == "Exceptions") { + attributes.push_back(ExceptionsAttribute::Read(p, attribute_name)); + } else if (attr_name == "Signature") { + attributes.push_back(SignatureAttribute::Read(p, attribute_name)); + } else if (attr_name == "Deprecated") { + attributes.push_back(DeprecatedAttribute::Read(p, attribute_name)); + } else if (attr_name == "EnclosingMethod") { + attributes.push_back(EnclosingMethodAttribute::Read(p, attribute_name)); + } else if (attr_name == "InnerClasses") { + // TODO(bazel-team): omit private inner classes + attributes.push_back(InnerClassesAttribute::Read(p, attribute_name)); + } else if (attr_name == "AnnotationDefault") { + attributes.push_back(AnnotationDefaultAttribute::Read(p, attribute_name)); + } else if (attr_name == "ConstantValue") { + attributes.push_back(ConstantValueAttribute::Read(p, attribute_name)); + } else if (attr_name == "RuntimeVisibleAnnotations" || + attr_name == "RuntimeInvisibleAnnotations") { + attributes.push_back(AnnotationsAttribute::Read(p, attribute_name)); + } else if (attr_name == "RuntimeVisibleParameterAnnotations" || + attr_name == "RuntimeInvisibleParameterAnnotations") { + attributes.push_back( + ParameterAnnotationsAttribute::Read(p, attribute_name)); + } else if (attr_name == "Scala" || + attr_name == "ScalaSig" || + attr_name == "ScalaInlineInfo") { + // These are opaque blobs, so can be handled with a general + // attribute handler + attributes.push_back(GeneralAttribute::Read(p, attribute_name, + attribute_length)); + } else if (attr_name == "RuntimeVisibleTypeAnnotations" || + attr_name == "RuntimeInvisibleTypeAnnotations") { + attributes.push_back(TypeAnnotationsAttribute::Read(p, attribute_name, + attribute_length)); + } else if (attr_name == "MethodParameters") { + attributes.push_back( + MethodParametersAttribute::Read(p, attribute_name, attribute_length)); + } else if (attr_name == "NestHost") { + attributes.push_back( + NestHostAttribute::Read(p, attribute_name, attribute_length)); + } else if (attr_name == "NestMembers") { + attributes.push_back( + NestMembersAttribute::Read(p, attribute_name, attribute_length)); + } else if (attr_name == "com.google.devtools.ijar.KeepForCompile") { + auto attr = new KeepForCompileAttribute; + attr->attribute_name_ = attribute_name; + attributes.push_back(attr); + } else { + // Skip over unknown attributes with a warning. The JVM spec + // says this is ok, so long as we handle the mandatory attributes. + fprintf(stderr, "ijar: skipping unknown attribute: \"%s\".\n", + attr_name.c_str()); + p += attribute_length; + } + } +} + +void HasAttrs::WriteAttrs(u1 *&p) { + u1* p_size = p; + + put_u2be(p, 0); + int n_written_attrs = 0; + for (size_t ii = 0; ii < attributes.size(); ii++) { + u1* before = p; + attributes[ii]->Write(p); + if (p != before) { + n_written_attrs++; + } + } + + put_u2be(p_size, n_written_attrs); +} + +// See sec.4.4 of JVM spec. +bool ClassFile::ReadConstantPool(const u1 *&p) { + + const_pool_in.clear(); + const_pool_in.push_back(NULL); // dummy first item + + u2 cp_count = get_u2be(p); + for (int ii = 1; ii < cp_count; ++ii) { + u1 tag = get_u1(p); + + if (devtools_ijar::verbose) { + fprintf(stderr, "cp[%d/%d] = tag %d\n", ii, cp_count, tag); + } + + switch(tag) { + case CONSTANT_Class: { + u2 name_index = get_u2be(p); + const_pool_in.push_back(new Constant_Class(name_index)); + break; + } + case CONSTANT_FieldRef: + case CONSTANT_Methodref: + case CONSTANT_Interfacemethodref: { + u2 class_index = get_u2be(p); + u2 nti = get_u2be(p); + const_pool_in.push_back(new Constant_FMIref(tag, class_index, nti)); + break; + } + case CONSTANT_String: { + u2 string_index = get_u2be(p); + const_pool_in.push_back(new Constant_String(string_index)); + break; + } + case CONSTANT_NameAndType: { + u2 name_index = get_u2be(p); + u2 descriptor_index = get_u2be(p); + const_pool_in.push_back( + new Constant_NameAndType(name_index, descriptor_index)); + break; + } + case CONSTANT_Utf8: { + u2 length = get_u2be(p); + if (devtools_ijar::verbose) { + fprintf(stderr, "Utf8: \"%s\" (%d)\n", + std::string((const char*) p, length).c_str(), length); + } + + const_pool_in.push_back(new Constant_Utf8(length, p)); + p += length; + break; + } + case CONSTANT_Integer: + case CONSTANT_Float: { + u4 bytes = get_u4be(p); + const_pool_in.push_back(new Constant_IntegerOrFloat(tag, bytes)); + break; + } + case CONSTANT_Long: + case CONSTANT_Double: { + u4 high_bytes = get_u4be(p); + u4 low_bytes = get_u4be(p); + const_pool_in.push_back( + new Constant_LongOrDouble(tag, high_bytes, low_bytes)); + // Longs and doubles occupy two constant pool slots. + // ("In retrospect, making 8-byte constants take two "constant + // pool entries was a poor choice." --JVM Spec.) + const_pool_in.push_back(NULL); + ii++; + break; + } + case CONSTANT_MethodHandle: { + u1 reference_kind = get_u1(p); + u2 reference_index = get_u2be(p); + const_pool_in.push_back( + new Constant_MethodHandle(reference_kind, reference_index)); + break; + } + case CONSTANT_MethodType: { + u2 descriptor_index = get_u2be(p); + const_pool_in.push_back(new Constant_MethodType(descriptor_index)); + break; + } + case CONSTANT_InvokeDynamic: { + u2 bootstrap_method_attr = get_u2be(p); + u2 name_name_type_index = get_u2be(p); + const_pool_in.push_back(new Constant_InvokeDynamic( + bootstrap_method_attr, name_name_type_index)); + break; + } + default: { + fprintf(stderr, "Unknown constant: %02x. Passing class through.\n", + tag); + return false; + } + } + } + + return true; +} + +bool ClassFile::IsLocalOrAnonymous() { + for (const Attribute *attribute : attributes) { + if (attribute->attribute_name_->Display() == "EnclosingMethod") { + // JVMS 4.7.6: a class must has EnclosingMethod attribute iff it + // represents a local class or an anonymous class + return true; + } + } + return false; +} + +static bool HasKeepForCompile(const std::vector attributes) { + for (const Attribute *attribute : attributes) { + if (attribute->attribute_name_->Display() == + "com.google.devtools.ijar.KeepForCompile") { + return true; + } + } + return false; +} + +bool ClassFile::IsExplicitlyKept() { + if (HasKeepForCompile(attributes)) { + return true; + } + for (const Member *method : methods) { + if (HasKeepForCompile(method->attributes)) { + return true; + } + } + return false; +} + +static ClassFile *ReadClass(const void *classdata, size_t length) { + const u1 *p = (u1*) classdata; + + ClassFile *clazz = new ClassFile; + + clazz->length = length; + + clazz->magic = get_u4be(p); + if (clazz->magic != 0xCAFEBABE) { + fprintf(stderr, "Bad magic %" PRIx32 "\n", clazz->magic); + abort(); + } + clazz->major = get_u2be(p); + clazz->minor = get_u2be(p); + + if (!clazz->ReadConstantPool(p)) { + delete clazz; + return NULL; + } + + clazz->access_flags = get_u2be(p); + clazz->this_class = constant(get_u2be(p)); + class_name = clazz->this_class; + + u2 super_class_id = get_u2be(p); + clazz->super_class = super_class_id == 0 ? NULL : constant(super_class_id); + + u2 interfaces_count = get_u2be(p); + for (int ii = 0; ii < interfaces_count; ++ii) { + clazz->interfaces.push_back(constant(get_u2be(p))); + } + + u2 fields_count = get_u2be(p); + for (int ii = 0; ii < fields_count; ++ii) { + Member *field = Member::Read(p); + + if ((field->access_flags & ACC_PRIVATE) == ACC_PRIVATE) { + // drop private fields + continue; + } + clazz->fields.push_back(field); + } + + u2 methods_count = get_u2be(p); + for (int ii = 0; ii < methods_count; ++ii) { + Member *method = Member::Read(p); + + if (HasKeepForCompile(method->attributes)) { + // Always keep methods marked as such + clazz->methods.push_back(method); + continue; + } + + // drop class initializers + if (method->name->Display() == "") continue; + + if ((method->access_flags & ACC_PRIVATE) == ACC_PRIVATE) { + // drop private methods + continue; + } + if ((method->access_flags & (ACC_SYNTHETIC | ACC_BRIDGE | ACC_PUBLIC | + ACC_PROTECTED)) == ACC_SYNTHETIC) { + // drop package-private non-bridge synthetic methods, e.g. synthetic + // constructors used to instantiate private nested classes within their + // declaring compilation unit + continue; + } + clazz->methods.push_back(method); + } + + clazz->ReadAttrs(p); + + return clazz; +} + +// In theory, '/' is also reserved, but it's okay if we just parse package +// identifiers as part of the class name. Note that signatures are UTF-8, but +// this works just as well as in plain ASCII. +static const char *SIGNATURE_NON_IDENTIFIER_CHARS = ".;[<>:"; + +void Expect(const std::string& desc, size_t* p, char expected) { + if (desc[*p] != expected) { + fprintf(stderr, "Expected '%c' in '%s' at %zd in signature\n", + expected, desc.substr(*p).c_str(), *p); + exit(1); + } + + *p += 1; +} + +// These functions form a crude recursive descent parser for descriptors and +// signatures in class files (see JVM spec 4.3). +// +// This parser is a bit more liberal than the spec, but this should be fine, +// because it accepts all valid class files and croaks only on invalid ones. +void ParseFromClassTypeSignature(const std::string& desc, size_t* p); +void ParseSimpleClassTypeSignature(const std::string& desc, size_t* p); +void ParseClassTypeSignatureSuffix(const std::string& desc, size_t* p); +void ParseIdentifier(const std::string& desc, size_t* p); +void ParseTypeArgumentsOpt(const std::string& desc, size_t* p); +void ParseMethodDescriptor(const std::string& desc, size_t* p); + +void ParseClassTypeSignature(const std::string& desc, size_t* p) { + Expect(desc, p, 'L'); + ParseSimpleClassTypeSignature(desc, p); + ParseClassTypeSignatureSuffix(desc, p); + Expect(desc, p, ';'); +} + +void ParseSimpleClassTypeSignature(const std::string& desc, size_t* p) { + ParseIdentifier(desc, p); + ParseTypeArgumentsOpt(desc, p); +} + +void ParseClassTypeSignatureSuffix(const std::string& desc, size_t* p) { + while (desc[*p] == '.') { + *p += 1; + ParseSimpleClassTypeSignature(desc, p); + } +} + +void ParseIdentifier(const std::string& desc, size_t* p) { + size_t next = desc.find_first_of(SIGNATURE_NON_IDENTIFIER_CHARS, *p); + std::string id = desc.substr(*p, next - *p); + used_class_names.insert(id); + *p = next; +} + +void ParseTypeArgumentsOpt(const std::string& desc, size_t* p) { + if (desc[*p] != '<') { + return; + } + + *p += 1; + while (desc[*p] != '>') { + switch (desc[*p]) { + case '*': + *p += 1; + break; + + case '+': + case '-': + *p += 1; + ExtractClassNames(desc, p); + break; + + default: + ExtractClassNames(desc, p); + break; + } + } + + *p += 1; +} + +void ParseMethodDescriptor(const std::string& desc, size_t* p) { + Expect(desc, p, '('); + while (desc[*p] != ')') { + ExtractClassNames(desc, p); + } + + Expect(desc, p, ')'); + ExtractClassNames(desc, p); +} + +void ParseFormalTypeParameters(const std::string& desc, size_t* p) { + Expect(desc, p, '<'); + while (desc[*p] != '>') { + ParseIdentifier(desc, p); + Expect(desc, p, ':'); + if (desc[*p] != ':' && desc[*p] != '>') { + ExtractClassNames(desc, p); + } + + while (desc[*p] == ':') { + Expect(desc, p, ':'); + ExtractClassNames(desc, p); + } + } + + Expect(desc, p, '>'); +} + +void ExtractClassNames(const std::string& desc, size_t* p) { + switch (desc[*p]) { + case '<': + ParseFormalTypeParameters(desc, p); + ExtractClassNames(desc, p); + break; + + case 'L': + ParseClassTypeSignature(desc, p); + break; + + case '[': + *p += 1; + ExtractClassNames(desc, p); + break; + + case 'T': + *p += 1; + ParseIdentifier(desc, p); + Expect(desc, p, ';'); + break; + + case '(': + ParseMethodDescriptor(desc, p); + break; + + case 'B': + case 'C': + case 'D': + case 'F': + case 'I': + case 'J': + case 'S': + case 'Z': + case 'V': + *p += 1; + break; + + default: + fprintf(stderr, "Invalid signature %s\n", desc.substr(*p).c_str()); + } +} + +void ClassFile::WriteClass(u1 *&p) { + used_class_names.clear(); + std::vector members; + members.insert(members.end(), fields.begin(), fields.end()); + members.insert(members.end(), methods.begin(), methods.end()); + ExtractClassNames(); + for (auto *member : members) { + size_t idx = 0; + devtools_ijar::ExtractClassNames(member->descriptor->Display(), &idx); + member->ExtractClassNames(); + } + + // We have to write the body out before the header in order to reference + // the essential constants and populate the output constant pool: + u1 *body = new u1[length]; + u1 *q = body; + WriteBody(q); // advances q + u4 body_length = q - body; + + WriteHeader(p); // advances p + put_n(p, body, body_length); + delete[] body; +} + +bool StripClass(u1 *&classdata_out, const u1 *classdata_in, size_t in_length) { + ClassFile *clazz = ReadClass(classdata_in, in_length); + bool keep = true; + if (clazz == NULL || clazz->IsExplicitlyKept()) { + // Class is invalid or kept. Simply copy it to the output and call it a day. + // TODO: If kept, only emit methods marked with KeepForCompile attribute, + // as opposed to the entire type. + put_n(classdata_out, classdata_in, in_length); + } else if (clazz->IsLocalOrAnonymous()) { + keep = false; + } else { + // Constant pool item zero is a dummy entry. Setting it marks the + // beginning of the output phase; calls to Constant::slot() will + // fail if called prior to this. + const_pool_out.push_back(NULL); + clazz->WriteClass(classdata_out); + + delete clazz; + } + + // Now clean up all the mess we left behind. + + for (size_t i = 0; i < const_pool_in.size(); i++) { + delete const_pool_in[i]; + } + + const_pool_in.clear(); + const_pool_out.clear(); + return keep; +} + +} // namespace devtools_ijar diff --git a/third_party/java_tools/ijar/common.h b/third_party/java_tools/ijar/common.h new file mode 100644 index 000000000..c5b841aa7 --- /dev/null +++ b/third_party/java_tools/ijar/common.h @@ -0,0 +1,117 @@ +// Copyright 2015 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// common.h -- common definitions. +// + +#ifndef INCLUDED_DEVTOOLS_IJAR_COMMON_H +#define INCLUDED_DEVTOOLS_IJAR_COMMON_H + +#include +#include +#include + +#ifdef _WIN32 +#define PATH_MAX 4096 +typedef int mode_t; +#endif // _WIN32 + +namespace devtools_ijar { + +typedef unsigned long long u8; +typedef uint32_t u4; +typedef uint16_t u2; +typedef uint8_t u1; + +// be = big endian, le = little endian + +inline u1 get_u1(const u1 *&p) { + return *p++; +} + +inline u2 get_u2be(const u1 *&p) { + u4 x = (p[0] << 8) | p[1]; + p += 2; + return x; +} + +inline u2 get_u2le(const u1 *&p) { + u4 x = (p[1] << 8) | p[0]; + p += 2; + return x; +} + +inline u4 get_u4be(const u1 *&p) { + u4 x = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; + p += 4; + return x; +} + +inline u4 get_u4le(const u1 *&p) { + u4 x = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]; + p += 4; + return x; +} + +inline u8 get_u8le(const u1 *&p) { + u4 lo = get_u4le(p); + u4 hi = get_u4le(p); + u8 x = ((u8)hi << 32) | lo; + return x; +} + +inline void put_u1(u1 *&p, u1 x) { + *p++ = x; +} + +inline void put_u2be(u1 *&p, u2 x) { + *p++ = x >> 8; + *p++ = x & 0xff; +} + +inline void put_u2le(u1 *&p, u2 x) { + *p++ = x & 0xff; + *p++ = x >> 8;; +} + +inline void put_u4be(u1 *&p, u4 x) { + *p++ = x >> 24; + *p++ = (x >> 16) & 0xff; + *p++ = (x >> 8) & 0xff; + *p++ = x & 0xff; +} + +inline void put_u4le(u1 *&p, u4 x) { + *p++ = x & 0xff; + *p++ = (x >> 8) & 0xff; + *p++ = (x >> 16) & 0xff; + *p++ = x >> 24; +} + +inline void put_u8le(u1 *&p, u8 x) { + put_u4le(p, x & 0xffffffff); + put_u4le(p, (x >> 32) & 0xffffffff); +} + +// Copy n bytes from src to p, and advance p. +inline void put_n(u1 *&p, const u1 *src, size_t n) { + memcpy(p, src, n); + p += n; +} + +extern bool verbose; + +} // namespace devtools_ijar + +#endif // INCLUDED_DEVTOOLS_IJAR_COMMON_H diff --git a/third_party/java_tools/ijar/dummy_zlib_client.cc b/third_party/java_tools/ijar/dummy_zlib_client.cc new file mode 100644 index 000000000..fe1498770 --- /dev/null +++ b/third_party/java_tools/ijar/dummy_zlib_client.cc @@ -0,0 +1,37 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "third_party/ijar/common.h" +#include "third_party/ijar/zlib_client.h" + +namespace devtools_ijar { + +u4 ComputeCrcChecksum(u1* buf, size_t length) { return 0; } + +size_t TryDeflate(u1* buf, size_t length) { return 0; } + +Decompressor::Decompressor() {} +Decompressor::~Decompressor() {} + +DecompressedFile* Decompressor::UncompressFile(const u1* buffer, + size_t bytes_avail) { + return NULL; +} + +char* Decompressor::GetError() { return NULL; } + +int Decompressor::error(const char* fmt, ...) { return 0; } +} // namespace devtools_ijar diff --git a/third_party/java_tools/ijar/ijar.cc b/third_party/java_tools/ijar/ijar.cc new file mode 100644 index 000000000..0e90edfbe --- /dev/null +++ b/third_party/java_tools/ijar/ijar.cc @@ -0,0 +1,489 @@ +// Copyright 2015 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ijar.cpp -- .jar -> _interface.jar tool. +// + +#include +#include +#include +#include +#include +#include + +#include "zip.h" + +namespace devtools_ijar { + +bool verbose = false; + +// Reads a JVM class from classdata_in (of the specified length), and +// writes out a simplified class to classdata_out, advancing the +// pointer. Returns true if the class should be kept. +bool StripClass(u1 *&classdata_out, const u1 *classdata_in, size_t in_length); + +const char *CLASS_EXTENSION = ".class"; +const size_t CLASS_EXTENSION_LENGTH = strlen(CLASS_EXTENSION); +const char *KOTLIN_MODULE_EXTENSION = ".kotlin_module"; +const size_t KOTLIN_MODULE_EXTENSION_LENGTH = strlen(KOTLIN_MODULE_EXTENSION); +const char *SCALA_TASTY_EXTENSION = ".tasty"; +const size_t SCALA_TASTY_EXTENSION_LENGTH = strlen(SCALA_TASTY_EXTENSION); + +const char *MANIFEST_DIR_PATH = "META-INF/"; +const size_t MANIFEST_DIR_PATH_LENGTH = strlen(MANIFEST_DIR_PATH); +const char *MANIFEST_PATH = "META-INF/MANIFEST.MF"; +const size_t MANIFEST_PATH_LENGTH = strlen(MANIFEST_PATH); +const char *MANIFEST_HEADER = + "Manifest-Version: 1.0\r\n" + "Created-By: bazel\r\n"; +const size_t MANIFEST_HEADER_LENGTH = strlen(MANIFEST_HEADER); +// These attributes are used by JavaBuilder, Turbine, and ijar. +// They must all be kept in sync. +const char *TARGET_LABEL_KEY = "Target-Label: "; +const size_t TARGET_LABEL_KEY_LENGTH = strlen(TARGET_LABEL_KEY); +const char *INJECTING_RULE_KIND_KEY = "Injecting-Rule-Kind: "; +const size_t INJECTING_RULE_KIND_KEY_LENGTH = strlen(INJECTING_RULE_KIND_KEY); + +class JarExtractorProcessor : public ZipExtractorProcessor { + public: + // Set the ZipBuilder to add the ijar class to the output zip file. + // This pointer should not be deleted while this class is still in use and + // it should be set before any call to the Process() method. + void SetZipBuilder(ZipBuilder *builder) { this->builder_ = builder; } + virtual void WriteManifest(const char *target_label, + const char *injecting_rule_kind) = 0; + + protected: + // Not owned by JarStripperProcessor, see SetZipBuilder(). + ZipBuilder *builder_; +}; + +// ZipExtractorProcessor that select only .class file and use +// StripClass to generate an interface class, storing as a new file +// in the specified ZipBuilder. +class JarStripperProcessor : public JarExtractorProcessor { + public: + JarStripperProcessor() {} + virtual ~JarStripperProcessor() {} + + virtual void Process(const char *filename, const u4 attr, const u1 *data, + const size_t size); + virtual bool Accept(const char *filename, const u4 attr); + + virtual void WriteManifest(const char *target_label, + const char *injecting_rule_kind); +}; + +static bool StartsWith(const char *str, const size_t str_len, + const char *prefix, const size_t prefix_len) { + return str_len >= prefix_len && strncmp(str, prefix, prefix_len) == 0; +} + +static bool EndsWith(const char *str, const size_t str_len, const char *suffix, + const size_t suffix_len) { + return str_len >= suffix_len && + strcmp(str + str_len - suffix_len, suffix) == 0; +} + +static bool IsKotlinModule(const char *filename, const size_t filename_len) { + return StartsWith(filename, filename_len, MANIFEST_DIR_PATH, + MANIFEST_DIR_PATH_LENGTH) && + EndsWith(filename, filename_len, KOTLIN_MODULE_EXTENSION, + KOTLIN_MODULE_EXTENSION_LENGTH); +} + +static bool IsScalaTasty(const char *filename, const size_t filename_len) { + return EndsWith(filename, filename_len, SCALA_TASTY_EXTENSION, + SCALA_TASTY_EXTENSION_LENGTH); +} + +bool JarStripperProcessor::Accept(const char *filename, const u4 /*attr*/) { + const size_t filename_len = strlen(filename); + if (IsKotlinModule(filename, filename_len) || + IsScalaTasty(filename, filename_len)) { + return true; + } + if (filename_len < CLASS_EXTENSION_LENGTH || + strcmp(filename + filename_len - CLASS_EXTENSION_LENGTH, + CLASS_EXTENSION) != 0) { + return false; + } + return true; +} + +static bool IsModuleInfo(const char *filename) { + const char *slash = strrchr(filename, '/'); + if (slash == NULL) { + slash = filename; + } else { + slash++; + } + return strcmp(slash, "module-info.class") == 0; +} + +void JarStripperProcessor::Process(const char *filename, const u4 /*attr*/, + const u1 *data, const size_t size) { + if (verbose) { + fprintf(stderr, "INFO: StripClass: %s\n", filename); + } + if (IsModuleInfo(filename) || IsKotlinModule(filename, strlen(filename)) || + IsScalaTasty(filename, strlen(filename))) { + u1 *q = builder_->NewFile(filename, 0); + memcpy(q, data, size); + builder_->FinishFile(size, /* compress: */ false, /* compute_crc: */ true); + } else { + u1 *buf = reinterpret_cast(malloc(size)); + u1 *classdata_out = buf; + if (!StripClass(buf, data, size)) { + free(classdata_out); + return; + } + u1 *q = builder_->NewFile(filename, 0); + size_t out_length = buf - classdata_out; + memcpy(q, classdata_out, out_length); + builder_->FinishFile(out_length, /* compress: */ false, + /* compute_crc: */ true); + free(classdata_out); + } +} + +// Copies the string into the buffer without the null terminator, returns +// updated buffer pointer +static u1 *WriteStr(u1 *buf, const char *str) { + size_t len = strlen(str); + memcpy(buf, str, len); + return buf + len; +} + +// Writes a manifest attribute including a "\r\n" line break, returns updated +// buffer pointer. +static u1 *WriteManifestAttr(u1 *buf, const char *key, const char *val) { + buf = WriteStr(buf, key); + buf = WriteStr(buf, val); + *buf++ = '\r'; + *buf++ = '\n'; + return buf; +} + +void JarStripperProcessor::WriteManifest(const char *target_label, + const char *injecting_rule_kind) { + if (target_label == nullptr) { + return; + } + builder_->WriteEmptyFile(MANIFEST_DIR_PATH); + u1 *start = builder_->NewFile(MANIFEST_PATH, 0); + u1 *buf = start; + buf = WriteStr(buf, MANIFEST_HEADER); + buf = WriteManifestAttr(buf, TARGET_LABEL_KEY, target_label); + if (injecting_rule_kind) { + buf = WriteManifestAttr(buf, INJECTING_RULE_KIND_KEY, injecting_rule_kind); + } + size_t total_len = buf - start; + builder_->FinishFile(total_len, /* compress: */ false, + /* compute_crc: */ true); +} + +class JarCopierProcessor : public JarExtractorProcessor { + public: + JarCopierProcessor(const char *jar) : jar_(jar) {} + virtual ~JarCopierProcessor() {} + + virtual void Process(const char *filename, const u4 /*attr*/, const u1 *data, + const size_t size); + virtual bool Accept(const char *filename, const u4 /*attr*/); + + virtual void WriteManifest(const char *target_label, + const char *injecting_rule_kind); + + private: + class ManifestLocator : public ZipExtractorProcessor { + public: + ManifestLocator() : manifest_buf_(nullptr), manifest_size_(0) {} + virtual ~ManifestLocator() { free(manifest_buf_); } + + u1 *manifest_buf_; + size_t manifest_size_; + + virtual bool Accept(const char *filename, const u4 /*attr*/) { + return strcmp(filename, MANIFEST_PATH) == 0; + } + + virtual void Process(const char * /*filename*/, const u4 /*attr*/, + const u1 *data, const size_t size) { + manifest_buf_ = (u1 *)malloc(size); + memmove(manifest_buf_, data, size); + manifest_size_ = size; + } + }; + + const char *jar_; + + u1 *AppendTargetLabelToManifest(u1 *buf, const u1 *manifest_data, + const size_t size, const char *target_label, + const char *injecting_rule_kind); +}; + +void JarCopierProcessor::Process(const char *filename, const u4 /*attr*/, + const u1 *data, const size_t size) { + if (verbose) { + fprintf(stderr, "INFO: CopyFile: %s\n", filename); + } + // We already handled the manifest in WriteManifest + if (strcmp(filename, MANIFEST_DIR_PATH) == 0 || + strcmp(filename, MANIFEST_PATH) == 0) { + return; + } + u1 *q = builder_->NewFile(filename, 0); + memcpy(q, data, size); + builder_->FinishFile(size, /* compress: */ false, /* compute_crc: */ true); +} + +bool JarCopierProcessor::Accept(const char * /*filename*/, const u4 /*attr*/) { + return true; +} + +void JarCopierProcessor::WriteManifest(const char *target_label, + const char *injecting_rule_kind) { + ManifestLocator manifest_locator; + std::unique_ptr in( + ZipExtractor::Create(jar_, &manifest_locator)); + in->ProcessAll(); + + bool wants_manifest = + manifest_locator.manifest_buf_ != nullptr || target_label != nullptr; + if (wants_manifest) { + builder_->WriteEmptyFile(MANIFEST_DIR_PATH); + u1 *start = builder_->NewFile(MANIFEST_PATH, 0); + u1 *buf = start; + // Three cases: + // 1. We need to merge the target label into a pre-existing manifest + // 2. Write a manifest from scratch with a target label + // 3. Copy existing manifest without adding target label + if (manifest_locator.manifest_buf_ != nullptr && target_label != nullptr) { + buf = AppendTargetLabelToManifest(buf, manifest_locator.manifest_buf_, + manifest_locator.manifest_size_, + target_label, injecting_rule_kind); + } else if (target_label != nullptr) { + buf = WriteStr(buf, MANIFEST_HEADER); + buf = WriteManifestAttr(buf, TARGET_LABEL_KEY, target_label); + if (injecting_rule_kind) { + buf = WriteManifestAttr(buf, INJECTING_RULE_KIND_KEY, + injecting_rule_kind); + } + } else { + memcpy(buf, manifest_locator.manifest_buf_, + manifest_locator.manifest_size_); + buf += manifest_locator.manifest_size_; + } + + size_t total_len = buf - start; + builder_->FinishFile(total_len, /* compress: */ false, + /* compute_crc: */ true); + } +} + +u1 *JarCopierProcessor::AppendTargetLabelToManifest( + u1 *buf, const u1 *manifest_data, const size_t size, + const char *target_label, const char *injecting_rule_kind) { + const char *line_start = (const char *)manifest_data; + const char *data_end = (const char *)manifest_data + size; + + // Write main attributes part + while (line_start < data_end && line_start[0] != '\r' && line_start[0] != '\n') { + const char *line_end = strchr(line_start, '\n'); + // Go past return char to point to next line, or to end of data buffer + line_end = line_end != nullptr ? line_end + 1 : data_end; + + // Copy line unless it's Target-Label/Injecting-Rule-Kind and we're writing + // that ourselves + if (strncmp(line_start, TARGET_LABEL_KEY, TARGET_LABEL_KEY_LENGTH) != 0 && + strncmp(line_start, INJECTING_RULE_KIND_KEY, + INJECTING_RULE_KIND_KEY_LENGTH) != 0) { + size_t len = line_end - line_start; + memcpy(buf, line_start, len); + buf += len; + } + line_start = line_end; + } + + // Append target label and, if given, rule kind + buf = WriteManifestAttr(buf, TARGET_LABEL_KEY, target_label); + if (injecting_rule_kind != nullptr) { + buf = WriteManifestAttr(buf, INJECTING_RULE_KIND_KEY, injecting_rule_kind); + } + + // Write the rest of the manifest file + size_t sections_len = data_end - line_start; + if (sections_len > 0) { + memcpy(buf, line_start, sections_len); + buf += sections_len; + } + return buf; +} + +// WriteManifest, including zip file format overhead. +static size_t EstimateManifestOutputSize(const char *target_label, + const char *injecting_rule_kind) { + if (target_label == nullptr) { + return 0; + } + // local headers + size_t length = 30 * 2 + MANIFEST_DIR_PATH_LENGTH + MANIFEST_PATH_LENGTH; + // central directory + length += 46 * 2 + MANIFEST_DIR_PATH_LENGTH + MANIFEST_PATH_LENGTH; + // zip64 EOCD entries + length += 56 * 2; + + // manifest content + length += MANIFEST_HEADER_LENGTH; + // target label manifest entry, including newline + length += TARGET_LABEL_KEY_LENGTH + strlen(target_label) + 2; + if (injecting_rule_kind) { + // injecting rule kind manifest entry, including newline + length += INJECTING_RULE_KIND_KEY_LENGTH + strlen(injecting_rule_kind) + 2; + } + return length; +} + +// Opens "file_in" (a .jar file) for reading, and writes an interface +// .jar to "file_out". +static void OpenFilesAndProcessJar(const char *file_out, const char *file_in, + bool strip_jar, const char *target_label, + const char *injecting_rule_kind) { + std::unique_ptr processor; + if (strip_jar) { + processor = + std::unique_ptr(new JarStripperProcessor()); + } else { + processor = + std::unique_ptr(new JarCopierProcessor(file_in)); + } + std::unique_ptr in( + ZipExtractor::Create(file_in, processor.get())); + if (in == NULL) { + fprintf(stderr, "Unable to open Zip file %s: %s\n", file_in, + strerror(errno)); + abort(); + } + u8 output_length = + in->CalculateOutputLength() + + EstimateManifestOutputSize(target_label, injecting_rule_kind); + std::unique_ptr out(ZipBuilder::Create(file_out, output_length)); + if (out == NULL) { + fprintf(stderr, "Unable to open output file %s: %s\n", file_out, + strerror(errno)); + abort(); + } + processor->SetZipBuilder(out.get()); + processor->WriteManifest(target_label, injecting_rule_kind); + + // Process all files in the zip + if (in->ProcessAll() < 0) { + fprintf(stderr, "%s\n", in->GetError()); + abort(); + } + + // Add dummy file, since javac doesn't like truly empty jars. + if (out->GetNumberFiles() == 0) { + out->WriteEmptyFile("dummy"); + } + // Finish writing the output file + if (out->Finish() < 0) { + fprintf(stderr, "%s\n", out->GetError()); + abort(); + } + // Get all file size + size_t in_length = in->GetSize(); + size_t out_length = out->GetSize(); + if (verbose) { + fprintf(stderr, "INFO: produced interface jar: %s -> %s (%d%%).\n", file_in, + file_out, static_cast(100.0 * out_length / in_length)); + } +} +} // namespace devtools_ijar + +// +// main method +// +static void usage() { + fprintf(stderr, + "Usage: ijar " + "[-v] [--[no]strip_jar] " + "[--target label label] [--injecting_rule_kind kind] " + "x.jar [x_interface.jar>]\n"); + fprintf(stderr, "Creates an interface jar from the specified jar file.\n"); + exit(1); +} + +int main(int argc, char **argv) { + bool strip_jar = true; + const char *target_label = NULL; + const char *injecting_rule_kind = NULL; + const char *filename_in = NULL; + const char *filename_out = NULL; + + for (int ii = 1; ii < argc; ++ii) { + if (strcmp(argv[ii], "-v") == 0) { + devtools_ijar::verbose = true; + } else if (strcmp(argv[ii], "--strip_jar") == 0) { + strip_jar = true; + } else if (strcmp(argv[ii], "--nostrip_jar") == 0) { + strip_jar = false; + } else if (strcmp(argv[ii], "--target_label") == 0) { + if (++ii >= argc) { + usage(); + } + target_label = argv[ii]; + } else if (strcmp(argv[ii], "--injecting_rule_kind") == 0) { + if (++ii >= argc) { + usage(); + } + injecting_rule_kind = argv[ii]; + } else if (filename_in == NULL) { + filename_in = argv[ii]; + } else if (filename_out == NULL) { + filename_out = argv[ii]; + } else { + usage(); + } + } + + if (filename_in == NULL) { + usage(); + } + + // Guess output filename from input: + char filename_out_buf[PATH_MAX]; + if (filename_out == NULL) { + size_t len = strlen(filename_in); + if (len > 4 && strncmp(filename_in + len - 4, ".jar", 4) == 0) { + strcpy(filename_out_buf, filename_in); + strcpy(filename_out_buf + len - 4, "-interface.jar"); + filename_out = filename_out_buf; + } else { + fprintf(stderr, + "Can't determine output filename since input filename " + "doesn't end with '.jar'.\n"); + return 1; + } + } + + if (devtools_ijar::verbose) { + fprintf(stderr, "INFO: writing to '%s'.\n", filename_out); + } + + devtools_ijar::OpenFilesAndProcessJar(filename_out, filename_in, strip_jar, + target_label, injecting_rule_kind); + return 0; +} diff --git a/third_party/java_tools/ijar/mapped_file.h b/third_party/java_tools/ijar/mapped_file.h new file mode 100644 index 000000000..86a080e50 --- /dev/null +++ b/third_party/java_tools/ijar/mapped_file.h @@ -0,0 +1,83 @@ +// Copyright 2015 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef INCLUDED_THIRD_PARTY_IJAR_MAPPED_FILE_H +#define INCLUDED_THIRD_PARTY_IJAR_MAPPED_FILE_H + +#include "common.h" + +namespace devtools_ijar { + +struct MappedInputFileImpl; +struct MappedOutputFileImpl; + +// A memory mapped input file. +class MappedInputFile { + private: + MappedInputFileImpl *impl_; + + protected: + const char* errmsg_; + bool opened_; + u1* buffer_; + size_t length_; + + public: + MappedInputFile(const char* name); + virtual ~MappedInputFile(); + + // If opening the file succeeded or not. + bool Opened() const { return opened_; } + + // Description of the last error that happened. + const char* Error() const { return errmsg_; } + + // The mapped contents of the file. + u1* Buffer() const { return buffer_ ; } + + // The length of the file. + size_t Length() const { return length_; } + + // Unmap a given number of bytes from the beginning of the file. + void Discard(size_t bytes); + int Close(); +}; + +class MappedOutputFile { + private: + MappedOutputFileImpl *impl_; + + protected: + const char* errmsg_; + bool opened_; + u1* buffer_; + size_t estimated_size_; + + public: + MappedOutputFile(const char* name, size_t estimated_size); + virtual ~MappedOutputFile(); + + // If opening the file succeeded or not. + bool Opened() const { return opened_; } + + // Description of the last error that happened. + const char* Error() const { return errmsg_; } + + // The mapped contents of the file. + u1* Buffer() const { return buffer_; } + int Close(size_t size); +}; + +} // namespace devtools_ijar +#endif diff --git a/third_party/java_tools/ijar/mapped_file_unix.cc b/third_party/java_tools/ijar/mapped_file_unix.cc new file mode 100644 index 000000000..d158eab73 --- /dev/null +++ b/third_party/java_tools/ijar/mapped_file_unix.cc @@ -0,0 +1,159 @@ +// Copyright 2015 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include +#include + +#include + +#include "third_party/java_tools/ijar/mapped_file.h" + +#define MAX_ERROR 2048 + +namespace devtools_ijar { + +static char errmsg[MAX_ERROR]; + +struct MappedInputFileImpl { + size_t discarded_; + int fd_; +}; + +MappedInputFile::MappedInputFile(const char* name) { + impl_ = NULL; + opened_ = false; + + int fd = open(name, O_RDONLY); + if (fd < 0) { + snprintf(errmsg, MAX_ERROR, "open(): %s", strerror(errno)); + errmsg_ = errmsg; + return; + } + + off_t length = lseek(fd, 0, SEEK_END); + if (length < 0) { + snprintf(errmsg, MAX_ERROR, "lseek(): %s", strerror(errno)); + errmsg_ = errmsg; + return; + } + + void* buffer = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0); + if (buffer == MAP_FAILED) { + snprintf(errmsg, MAX_ERROR, "mmap(): %s", strerror(errno)); + errmsg_ = errmsg; + return; + } + + impl_ = new MappedInputFileImpl(); + impl_->fd_ = fd; + impl_->discarded_ = 0; + buffer_ = reinterpret_cast(buffer); + length_ = length; + opened_ = true; +} + +MappedInputFile::~MappedInputFile() { + delete impl_; +} + +void MappedInputFile::Discard(size_t bytes) { + munmap(buffer_ + impl_->discarded_, bytes); + impl_->discarded_ += bytes; +} + +int MappedInputFile::Close() { + if (close(impl_->fd_) < 0) { + snprintf(errmsg, MAX_ERROR, "close(): %s", strerror(errno)); + errmsg_ = errmsg; + return -1; + } + + return 0; +} + +struct MappedOutputFileImpl { + int fd_; + int mmap_length_; +}; + +MappedOutputFile::MappedOutputFile(const char* name, size_t estimated_size) + : estimated_size_(estimated_size) { + impl_ = NULL; + opened_ = false; + int fd = open(name, O_CREAT|O_RDWR|O_TRUNC, 0644); + if (fd < 0) { + snprintf(errmsg, MAX_ERROR, "open(): %s", strerror(errno)); + errmsg_ = errmsg; + return; + } + + // Create mmap-able sparse file + if (ftruncate(fd, estimated_size) < 0) { + snprintf(errmsg, MAX_ERROR, "ftruncate(): %s", strerror(errno)); + errmsg_ = errmsg; + return; + } + + // Ensure that any buffer overflow in JarStripper will result in + // SIGSEGV or SIGBUS by over-allocating beyond the end of the file. + size_t mmap_length = + std::min(static_cast(estimated_size + sysconf(_SC_PAGESIZE)), + std::numeric_limits::max()); + void* mapped = + mmap(NULL, mmap_length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (mapped == MAP_FAILED) { + snprintf(errmsg, MAX_ERROR, "mmap(): %s", strerror(errno)); + errmsg_ = errmsg; + return; + } + + impl_ = new MappedOutputFileImpl(); + impl_->fd_ = fd; + impl_->mmap_length_ = mmap_length; + buffer_ = reinterpret_cast(mapped); + opened_ = true; +} + + +MappedOutputFile::~MappedOutputFile() { + delete impl_; +} + +int MappedOutputFile::Close(size_t size) { + if (size > estimated_size_) { + snprintf(errmsg, MAX_ERROR, "size %zu > estimated size %zu", size, + estimated_size_); + errmsg_ = errmsg; + return -1; + } + munmap(buffer_, impl_->mmap_length_); + if (ftruncate(impl_->fd_, size) < 0) { + snprintf(errmsg, MAX_ERROR, "ftruncate(): %s", strerror(errno)); + errmsg_ = errmsg; + return -1; + } + + if (close(impl_->fd_) < 0) { + snprintf(errmsg, MAX_ERROR, "close(): %s", strerror(errno)); + errmsg_ = errmsg; + return -1; + } + + return 0; +} + +} // namespace devtools_ijar diff --git a/third_party/java_tools/ijar/mapped_file_windows.cc b/third_party/java_tools/ijar/mapped_file_windows.cc new file mode 100644 index 000000000..b8a86fe79 --- /dev/null +++ b/third_party/java_tools/ijar/mapped_file_windows.cc @@ -0,0 +1,211 @@ +// Copyright 2015 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include + +#include "src/main/cpp/util/errors.h" +#include "src/main/cpp/util/logging.h" +#include "src/main/cpp/util/path_platform.h" +#include "src/main/cpp/util/strings.h" +#include "third_party/ijar/mapped_file.h" + +#define MAX_ERROR 2048 + +namespace devtools_ijar { + +using std::string; +using std::wstring; + +static char errmsg[MAX_ERROR] = ""; + +struct MappedInputFileImpl { + HANDLE file_; + HANDLE mapping_; + + MappedInputFileImpl(HANDLE file, HANDLE mapping) { + file_ = file; + mapping_ = mapping; + } +}; + +MappedInputFile::MappedInputFile(const char* name) { + impl_ = NULL; + opened_ = false; + errmsg_ = errmsg; + + wstring wname; + string error; + if (!blaze_util::AsAbsoluteWindowsPath(name, &wname, &error)) { + BAZEL_DIE(255) << "MappedInputFile(" << name + << "): AsAbsoluteWindowsPath failed: " << error; + } + HANDLE file = CreateFileW(wname.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, + OPEN_EXISTING, 0, NULL); + if (file == INVALID_HANDLE_VALUE) { + string errormsg = blaze_util::GetLastErrorString(); + BAZEL_DIE(255) << "MappedInputFile(" << name << "): CreateFileW(" + << blaze_util::WstringToCstring(wname) + << ") failed: " << errormsg; + } + + LARGE_INTEGER size; + if (!GetFileSizeEx(file, &size)) { + string errormsg = blaze_util::GetLastErrorString(); + BAZEL_DIE(255) << "MappedInputFile(" << name + << "): GetFileSizeEx failed: " << errormsg; + } + + HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, + size.HighPart, size.LowPart, NULL); + if (mapping == NULL || mapping == INVALID_HANDLE_VALUE) { + string errormsg = blaze_util::GetLastErrorString(); + BAZEL_DIE(255) << "MappedInputFile(" << name + << "): CreateFileMapping failed: " << errormsg; + } + + void *view = MapViewOfFileEx(mapping, FILE_MAP_READ, 0, 0, 0, NULL); + if (view == NULL) { + string errormsg = blaze_util::GetLastErrorString(); + BAZEL_DIE(255) << "MappedInputFile(" << name + << "): MapViewOfFileEx failed: " << errormsg; + } + + impl_ = new MappedInputFileImpl(file, mapping); + length_ = size.QuadPart; + buffer_ = reinterpret_cast(view); + opened_ = true; +} + +MappedInputFile::~MappedInputFile() { + delete impl_; +} + +void MappedInputFile::Discard(size_t bytes) { + // This is not supported on Windows for now. I'm not sure if we can unmap + // parts of an existing view and that this is necessary for Windows at all. + // At any rate, this only matters for >2GB (or maybe >4GB?) input files. +} + +int MappedInputFile::Close() { + if (!UnmapViewOfFile(buffer_)) { + string errormsg = blaze_util::GetLastErrorString(); + BAZEL_DIE(255) << "MappedInputFile::Close: UnmapViewOfFile failed: " + << errormsg; + } + + if (!CloseHandle(impl_->mapping_)) { + string errormsg = blaze_util::GetLastErrorString(); + BAZEL_DIE(255) << "MappedInputFile::Close: CloseHandle for mapping failed: " + << errormsg; + } + + if (!CloseHandle(impl_->file_)) { + string errormsg = blaze_util::GetLastErrorString(); + BAZEL_DIE(255) << "MappedInputFile::Close: CloseHandle for file failed: " + << errormsg; + } + + return 0; +} + +struct MappedOutputFileImpl { + HANDLE file_; + HANDLE mapping_; + + MappedOutputFileImpl(HANDLE file, HANDLE mapping) { + file_ = file; + mapping_ = mapping; + } +}; + +MappedOutputFile::MappedOutputFile(const char* name, size_t estimated_size) { + impl_ = NULL; + opened_ = false; + errmsg_ = errmsg; + + wstring wname; + string error; + if (!blaze_util::AsAbsoluteWindowsPath(name, &wname, &error)) { + BAZEL_DIE(255) << "MappedOutputFile(" << name + << "): AsAbsoluteWindowsPath failed: " << error; + } + HANDLE file = CreateFileW(wname.c_str(), GENERIC_READ | GENERIC_WRITE, 0, + NULL, CREATE_ALWAYS, 0, NULL); + if (file == INVALID_HANDLE_VALUE) { + string errormsg = blaze_util::GetLastErrorString(); + BAZEL_DIE(255) << "MappedOutputFile(" << name << "): CreateFileW(" + << blaze_util::WstringToCstring(wname) + << ") failed: " << errormsg; + } + + HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READWRITE, + estimated_size >> 32, estimated_size & 0xffffffffUL, NULL); + if (mapping == NULL || mapping == INVALID_HANDLE_VALUE) { + BAZEL_DIE(255) << "MappedOutputFile(" << name + << "): CreateFileMapping failed"; + } + + void *view = MapViewOfFileEx(mapping, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL); + if (view == NULL) { + string errormsg = blaze_util::GetLastErrorString(); + BAZEL_DIE(255) << "MappedOutputFile(" << name + << "): MapViewOfFileEx failed: " << errormsg; + CloseHandle(mapping); + CloseHandle(file); + return; + } + + impl_ = new MappedOutputFileImpl(file, mapping); + buffer_ = reinterpret_cast(view); + opened_ = true; +} + +MappedOutputFile::~MappedOutputFile() { + delete impl_; +} + +int MappedOutputFile::Close(size_t size) { + if (!UnmapViewOfFile(buffer_)) { + BAZEL_DIE(255) << "MappedOutputFile::Close: UnmapViewOfFile failed: " + << blaze_util::GetLastErrorString(); + } + + if (!CloseHandle(impl_->mapping_)) { + BAZEL_DIE(255) + << "MappedOutputFile::Close: CloseHandle for mapping failed: " + << blaze_util::GetLastErrorString(); + } + + if (!SetFilePointer(impl_->file_, size, NULL, FILE_BEGIN)) { + BAZEL_DIE(255) << "MappedOutputFile::Close: SetFilePointer failed: " + << blaze_util::GetLastErrorString(); + } + + if (!SetEndOfFile(impl_->file_)) { + BAZEL_DIE(255) << "MappedOutputFile::Close: SetEndOfFile failed: " + << blaze_util::GetLastErrorString(); + } + + if (!CloseHandle(impl_->file_)) { + BAZEL_DIE(255) << "MappedOutputFile::Close: CloseHandle for file failed: " + << blaze_util::GetLastErrorString(); + } + + return 0; +} + +} // namespace devtools_ijar diff --git a/third_party/java_tools/ijar/platform_utils.cc b/third_party/java_tools/ijar/platform_utils.cc new file mode 100644 index 000000000..a5cfec16a --- /dev/null +++ b/third_party/java_tools/ijar/platform_utils.cc @@ -0,0 +1,125 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "third_party/java_tools/ijar/platform_utils.h" + +#include +#include + +#if defined(_WIN32) || defined(__CYGWIN__) +#include +#else // !(defined(_WIN32) || defined(__CYGWIN__)) +#include +#include +#include +#endif // defined(_WIN32) || defined(__CYGWIN__) + +#include + +#include "third_party/java_tools/src/main/cpp/util/errors.h" +#include "third_party/java_tools/src/main/cpp/util/file.h" +#include "third_party/java_tools/src/main/cpp/util/file_platform.h" +#include "third_party/java_tools/src/main/cpp/util/logging.h" +#include "third_party/java_tools/src/main/cpp/util/path.h" +#include "third_party/java_tools/src/main/cpp/util/path_platform.h" + +namespace devtools_ijar { + +using std::string; + +bool stat_file(const char* path, Stat* result) { +#if defined(_WIN32) || defined(__CYGWIN__) + std::wstring wpath; + std::string error; + if (!blaze_util::AsAbsoluteWindowsPath(path, &wpath, &error)) { + BAZEL_DIE(255) << "stat_file: AsAbsoluteWindowsPath(" << path + << ") failed: " << error; + } + + bool success = false; + BY_HANDLE_FILE_INFORMATION info; + HANDLE handle = ::CreateFileW( + /* lpFileName */ wpath.c_str(), + /* dwDesiredAccess */ GENERIC_READ, + /* dwShareMode */ FILE_SHARE_READ, + /* lpSecurityAttributes */ NULL, + /* dwCreationDisposition */ OPEN_EXISTING, + /* dwFlagsAndAttributes */ FILE_ATTRIBUTE_NORMAL, + /* hTemplateFile */ NULL); + + if (handle == INVALID_HANDLE_VALUE) { + // Opening it as a file failed, try opening it as a directory. + handle = ::CreateFileW( + /* lpFileName */ wpath.c_str(), + /* dwDesiredAccess */ GENERIC_READ, + /* dwShareMode */ FILE_SHARE_READ, + /* lpSecurityAttributes */ NULL, + /* dwCreationDisposition */ OPEN_EXISTING, + /* dwFlagsAndAttributes */ FILE_FLAG_BACKUP_SEMANTICS, + /* hTemplateFile */ NULL); + } + + if (handle != INVALID_HANDLE_VALUE && + ::GetFileInformationByHandle(handle, &info)) { + success = true; + bool is_dir = (info.dwFileAttributes != INVALID_FILE_ATTRIBUTES) && + (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); + // TODO(laszlocsomor): use info.nFileSizeHigh after we updated total_size to + // be u8 type. + result->total_size = is_dir ? 0 : info.nFileSizeLow; + // TODO(laszlocsomor): query the actual permissions and write in file_mode. + result->file_mode = 0777; + result->is_directory = is_dir; + } + ::CloseHandle(handle); + return success; +#else // !(defined(_WIN32) || defined(__CYGWIN__)) + struct stat statst; + if (stat(path, &statst) < 0) { + return false; + } + result->total_size = statst.st_size; + result->file_mode = statst.st_mode; + result->is_directory = (statst.st_mode & S_IFDIR) != 0; + return true; +#endif // defined(_WIN32) || defined(__CYGWIN__) +} + +bool write_file(const char* path, unsigned int perm, const void* data, + size_t size) { + return blaze_util::WriteFile(data, size, path, perm); +} + +bool read_file(const char* path, void* buffer, size_t size) { + return blaze_util::ReadFile(path, buffer, size); +} + +string get_cwd() { return blaze_util::GetCwd(); } + +bool make_dirs(const char* path, unsigned int mode) { +#ifndef _WIN32 + // TODO(laszlocsomor): respect `mode` on Windows/MSVC. + mode |= S_IWUSR | S_IXUSR; +#endif // not _WIN32 + string spath(path); + if (spath.empty()) { + return true; + } + if (spath.back() != '/' && spath.back() != '\\') { + spath = blaze_util::Dirname(spath); + } + return blaze_util::MakeDirectories(spath, mode); +} + +} // namespace devtools_ijar diff --git a/third_party/java_tools/ijar/platform_utils.h b/third_party/java_tools/ijar/platform_utils.h new file mode 100644 index 000000000..c24193f74 --- /dev/null +++ b/third_party/java_tools/ijar/platform_utils.h @@ -0,0 +1,77 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_IJAR_PLATFORM_UTILS_H_ +#define THIRD_PARTY_IJAR_PLATFORM_UTILS_H_ + +#include + +#include +#include +#include + +#include "third_party/java_tools/ijar/common.h" + +namespace devtools_ijar { + +// Platform-independent stat data. +struct Stat { + // Total size of the file in bytes. + int total_size; + // The Unix file mode from the stat.st_mode field. + mode_t file_mode; + // True if this is a directory. + bool is_directory; +}; + +// Converts a Stat object to ZIP attributes. +inline u4 stat_to_zipattr(const Stat& file_stat) { + return (((u4)file_stat.file_mode) << 16) | + (file_stat.is_directory != 0 ? 0x10 : 0); +} + +// Writes stat data into `result` about the file under `path`. +// Returns true if file is found and can be stat'ed. +// Returns false if the file is not found or cannot be stat'ed. +// Doesn't report any errors because it can also be used to simply check if a +// file exists. +bool stat_file(const char* path, Stat* result); + +// Writes `size` bytes from `data` into file under `path`. +// The file is created or overwritten and is set to have `perm` permissions. +// Returns true upon success: file is created and all data is written. +// Returns false upon failure and reports the error to stderr. +bool write_file(const char* path, unsigned int perm, const void* data, + size_t size); + +// Reads at most `size` bytes into `buffer` from the file under `path`. +// Returns true upon success: file is opened and all data is read. +// Returns false upon failure and reports the error to stderr. +bool read_file(const char* path, void* buffer, size_t size); + +// Returns the current working directory. +// Returns the empty string upon failure and reports the error to stderr. +std::string get_cwd(); + +// Do a recursive mkdir of all folders of path except the last path +// segment (if path ends with a / then the last path segment is empty). +// All folders are created using "perm" for creation mode, and are writable and +// openable by the current user. +// Returns true if all directories were created and permissions set. +// Returns false upon failure and reports the error to stderr. +bool make_dirs(const char* path, unsigned int perm); + +} // namespace devtools_ijar + +#endif // THIRD_PARTY_IJAR_PLATFORM_UTILS_H_ diff --git a/third_party/java_tools/ijar/zip.cc b/third_party/java_tools/ijar/zip.cc new file mode 100644 index 000000000..2d661afa0 --- /dev/null +++ b/third_party/java_tools/ijar/zip.cc @@ -0,0 +1,1140 @@ +// Copyright 2015 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// zip.cc -- .zip (.jar) file reading/writing routines. +// + +// See README.txt for details. +// +// See http://www.pkware.com/documents/casestudies/APPNOTE.TXT +// for definition of PKZIP file format. + +#define _FILE_OFFSET_BITS 64 // Support zip files larger than 2GB + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "third_party/java_tools/ijar/mapped_file.h" +#include "third_party/java_tools/ijar/platform_utils.h" +#include "third_party/java_tools/ijar/zip.h" +#include "third_party/java_tools/ijar/zlib_client.h" + +#define LOCAL_FILE_HEADER_SIGNATURE 0x04034b50 +#define CENTRAL_FILE_HEADER_SIGNATURE 0x02014b50 +#define UNIX_ZIP_FILE_VERSION 0x0300 +#define DIGITAL_SIGNATURE 0x05054b50 +#define ZIP64_EOCD_SIGNATURE 0x06064b50 +#define ZIP64_EOCD_LOCATOR_SIGNATURE 0x07064b50 +#define EOCD_SIGNATURE 0x06054b50 +#define DATA_DESCRIPTOR_SIGNATURE 0x08074b50 + +#define U2_MAX 0xffff +#define U4_MAX 0xffffffffUL + +#define ZIP64_EOCD_LOCATOR_SIZE 20 +// zip64 eocd is fixed size in the absence of a zip64 extensible data sector +#define ZIP64_EOCD_FIXED_SIZE 56 + +// version to extract: 1.0 - default value from APPNOTE.TXT. +// Output JAR files contain no extra ZIP features, so this is enough. +#define ZIP_VERSION_TO_EXTRACT 10 +#define COMPRESSION_METHOD_STORED 0 // no compression +#define COMPRESSION_METHOD_DEFLATED 8 + +#define GENERAL_PURPOSE_BIT_FLAG_COMPRESSED (1 << 3) +#define GENERAL_PURPOSE_BIT_FLAG_UTF8_ENCODED (1 << 11) +#define GENERAL_PURPOSE_BIT_FLAG_COMPRESSION_SPEED ((1 << 2) | (1 << 1)) +#define GENERAL_PURPOSE_BIT_FLAG_SUPPORTED \ + (GENERAL_PURPOSE_BIT_FLAG_COMPRESSED \ + | GENERAL_PURPOSE_BIT_FLAG_UTF8_ENCODED \ + | GENERAL_PURPOSE_BIT_FLAG_COMPRESSION_SPEED) + +namespace devtools_ijar { +// In the absence of ZIP64 support, zip files are limited to 4GB. +// http://www.info-zip.org/FAQ.html#limits +static const size_t kMaximumOutputSize = std::numeric_limits::max(); + +static const u4 kDefaultTimestamp = + 30 << 25 | 1 << 21 | 1 << 16; // January 1, 2010 in DOS time + +// +// A class representing a ZipFile for reading. Its public API is exposed +// using the ZipExtractor abstract class. +// +class InputZipFile : public ZipExtractor { + public: + InputZipFile(ZipExtractorProcessor *processor, const char* filename); + virtual ~InputZipFile(); + + virtual const char* GetError() { + if (errmsg[0] == 0) { + return NULL; + } + return errmsg; + } + + bool Open(); + virtual bool ProcessNext(); + virtual void Reset(); + virtual size_t GetSize() { + return input_file_->Length(); + } + + virtual u8 CalculateOutputLength(); + + virtual bool ProcessCentralDirEntry(const u1 *&p, size_t *compressed_size, + size_t *uncompressed_size, char *filename, + size_t filename_size, u4 *attr, + u4 *offset); + + private: + ZipExtractorProcessor *processor; + const char* filename_; + MappedInputFile *input_file_; + + // InputZipFile is responsible for maintaining the following + // pointers. They are allocated by the Create() method before + // the object is actually created using mmap. + const u1 * zipdata_in_; // start of input file mmap + size_t bytes_unmapped_; // bytes that have already been unmapped + const u1 * central_dir_; // central directory in input file + + size_t in_offset_; // offset the input file + + const u1 *p; // input cursor + + const u1* central_dir_current_; // central dir input cursor + + // Buffer size is initially INITIAL_BUFFER_SIZE. It doubles in size every + // time it is found too small, until it reaches MAX_BUFFER_SIZE. If that is + // not enough, we bail out. We only decompress class files, so they should + // be smaller than 64K anyway, but we give a little leeway. + // MAX_BUFFER_SIZE must be bigger than the size of the biggest file in the + // ZIP. It is set to 2GB here because no one has audited the code for 64-bit + // cleanliness. + static constexpr size_t INITIAL_BUFFER_SIZE = 256 * 1024; // 256K + static constexpr size_t MAX_BUFFER_SIZE = std::numeric_limits::max(); + static constexpr size_t MAX_MAPPED_REGION = 32 * 1024 * 1024; + + // These metadata fields are the fields of the ZIP header of the file being + // processed. + u2 extract_version_; + u2 general_purpose_bit_flag_; + u2 compression_method_; + u4 uncompressed_size_; + u4 compressed_size_; + u2 file_name_length_; + u2 extra_field_length_; + const u1 *file_name_; + const u1 *extra_field_; + + // Copy of the last filename entry - Null-terminated. + char filename[PATH_MAX]; + // The external file attribute field + u4 attr; + + // last error + char errmsg[4*PATH_MAX]; + + Decompressor *decompressor_; + + int error(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + vsnprintf(errmsg, 4*PATH_MAX, fmt, ap); + va_end(ap); + return -1; + } + + // Check that at least n bytes remain in the input file, otherwise + // abort with an error message. "state" is the name of the field + // we're about to read, for diagnostics. + int EnsureRemaining(size_t n, const char *state) { + size_t in_offset = p - zipdata_in_; + size_t remaining = input_file_->Length() - in_offset; + if (n > remaining) { + return error("Premature end of file (at offset %zd, state=%s); " + "expected %zd more bytes but found %zd.\n", + in_offset, state, n, remaining); + } + return 0; + } + + // Read one entry from input zip file + int ProcessLocalFileEntry(size_t compressed_size, size_t uncompressed_size); + + // Uncompress a file from the archive using zlib. The pointer returned + // is owned by InputZipFile, so it must not be freed. Advances the input + // cursor to the first byte after the compressed data. + u1* UncompressFile(); + + // Skip a file + int SkipFile(const bool compressed); + + // Process a file + int ProcessFile(const bool compressed); +}; + +// +// A class implementing ZipBuilder that represent an open zip file for writing. +// +class OutputZipFile : public ZipBuilder { + public: + OutputZipFile(const char *filename, size_t estimated_size) + : output_file_(NULL), + filename_(filename), + estimated_size_(estimated_size), + finished_(false) { + errmsg[0] = 0; + } + + virtual const char* GetError() { + if (errmsg[0] == 0) { + return NULL; + } + return errmsg; + } + + virtual ~OutputZipFile() { Finish(); } + virtual u1* NewFile(const char* filename, const u4 attr); + virtual int FinishFile(size_t filelength, bool compress = false, + bool compute_crc = false); + virtual int WriteEmptyFile(const char *filename); + virtual size_t GetSize() { + return Offset(q); + } + virtual int GetNumberFiles() { + return entries_.size(); + } + virtual int Finish(); + bool Open(); + + private: + struct LocalFileEntry { + // Start of the local header (in the output buffer). + size_t local_header_offset; + + // Sizes of the file entry + size_t uncompressed_length; + size_t compressed_length; + + // Compression method + u2 compression_method; + + // CRC32 + u4 crc32; + + // external attributes field + u4 external_attr; + + // Start/length of the file_name in the local header. + u1 *file_name; + u2 file_name_length; + + // Start/length of the extra_field in the local header. + const u1 *extra_field; + u2 extra_field_length; + }; + + MappedOutputFile* output_file_; + const char* filename_; + size_t estimated_size_; + bool finished_; + + // OutputZipFile is responsible for maintaining the following + // pointers. They are allocated by the Create() method before + // the object is actually created using mmap. + u1 *zipdata_out_; // start of output file mmap + u1 *q; // output cursor + + u1 *header_ptr; // Current pointer to "compression method" entry. + + // List of entries to write the central directory + std::vector entries_; + + // last error + char errmsg[4*PATH_MAX]; + + int error(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + vsnprintf(errmsg, 4*PATH_MAX, fmt, ap); + va_end(ap); + return -1; + } + + // Write the ZIP central directory structure for each local file + // entry in "entries". + void WriteCentralDirectory(); + + // Returns the offset of the pointer relative to the start of the + // output zip file. + size_t Offset(const u1 *const x) { + return x - zipdata_out_; + } + + // Write ZIP file header in the output. Since the compressed size is not + // known in advance, it must be recorded later. This method returns a pointer + // to "compressed size" in the file header that should be passed to + // WriteFileSizeInLocalFileHeader() later. + u1* WriteLocalFileHeader(const char *filename, const u4 attr); + + // Fill in the "compressed size" and "uncompressed size" fields in a local + // file header previously written by WriteLocalFileHeader(). + size_t WriteFileSizeInLocalFileHeader(u1 *header_ptr, + size_t out_length, + bool compress = false, + const u4 crc = 0); +}; + +// +// Implementation of InputZipFile +// +bool InputZipFile::ProcessNext() { + // Process the next entry in the central directory. Also make sure that the + // content pointer is in sync. + size_t compressed, uncompressed; + u4 offset; + if (!ProcessCentralDirEntry(central_dir_current_, &compressed, &uncompressed, + filename, PATH_MAX, &attr, &offset)) { + return false; + } + + // There might be an offset specified in the central directory that does + // not match the file offset, so always update our pointer. + p = zipdata_in_ + in_offset_ + offset; + + if (EnsureRemaining(4, "signature") < 0) { + return false; + } + u4 signature = get_u4le(p); + if (signature == LOCAL_FILE_HEADER_SIGNATURE) { + if (ProcessLocalFileEntry(compressed, uncompressed) < 0) { + return false; + } + } else { + error("local file header signature for file %s not found\n", filename); + return false; + } + + return true; +} + +int InputZipFile::ProcessLocalFileEntry( + size_t compressed_size, size_t uncompressed_size) { + if (EnsureRemaining(26, "extract_version") < 0) { + return -1; + } + extract_version_ = get_u2le(p); + general_purpose_bit_flag_ = get_u2le(p); + + if ((general_purpose_bit_flag_ & ~GENERAL_PURPOSE_BIT_FLAG_SUPPORTED) != 0) { + return error("Unsupported value (0x%04x) in general purpose bit flag.\n", + general_purpose_bit_flag_); + } + + compression_method_ = get_u2le(p); + + if (compression_method_ != COMPRESSION_METHOD_DEFLATED && + compression_method_ != COMPRESSION_METHOD_STORED) { + return error("Unsupported compression method (%d).\n", + compression_method_); + } + + // skip over: last_mod_file_time, last_mod_file_date, crc32 + p += 2 + 2 + 4; + compressed_size_ = get_u4le(p); + uncompressed_size_ = get_u4le(p); + file_name_length_ = get_u2le(p); + extra_field_length_ = get_u2le(p); + + if (EnsureRemaining(file_name_length_, "file_name") < 0) { + return -1; + } + file_name_ = p; + p += file_name_length_; + + if (EnsureRemaining(extra_field_length_, "extra_field") < 0) { + return -1; + } + extra_field_ = p; + p += extra_field_length_; + + bool is_compressed = compression_method_ == COMPRESSION_METHOD_DEFLATED; + + // If the zip is compressed, compressed and uncompressed size members are + // zero in the local file header. If not, check that they are the same as the + // lengths from the central directory, otherwise, just believe the central + // directory + if (compressed_size_ == 0) { + compressed_size_ = compressed_size; + } else { + if (compressed_size_ != compressed_size) { + return error("central directory and file header inconsistent\n"); + } + } + + if (uncompressed_size_ == 0) { + uncompressed_size_ = uncompressed_size; + } else { + if (uncompressed_size_ != uncompressed_size) { + return error("central directory and file header inconsistent\n"); + } + } + + if (processor->Accept(filename, attr)) { + if (ProcessFile(is_compressed) < 0) { + return -1; + } + } else { + if (SkipFile(is_compressed) < 0) { + return -1; + } + } + + if (general_purpose_bit_flag_ & GENERAL_PURPOSE_BIT_FLAG_COMPRESSED) { + // Skip the data descriptor. Some implementations do not put the signature + // here, so check if the next 4 bytes are a signature, and if so, skip the + // next 12 bytes (for CRC, compressed/uncompressed size), otherwise skip + // the next 8 bytes (because the value just read was the CRC). + u4 signature = get_u4le(p); + if (signature == DATA_DESCRIPTOR_SIGNATURE) { + p += 4 * 3; + } else { + p += 4 * 2; + } + } + + size_t bytes_processed = p - zipdata_in_; + if (bytes_processed > bytes_unmapped_ + MAX_MAPPED_REGION) { + input_file_->Discard(MAX_MAPPED_REGION); + bytes_unmapped_ += MAX_MAPPED_REGION; + } + + return 0; +} + +int InputZipFile::SkipFile(const bool compressed) { + if (!compressed) { + // In this case, compressed_size_ == uncompressed_size_ (since the file is + // uncompressed), so we can use either. + if (compressed_size_ != uncompressed_size_) { + return error("compressed size != uncompressed size, although the file " + "is uncompressed.\n"); + } + } + + if (EnsureRemaining(compressed_size_, "file_data") < 0) { + return -1; + } + p += compressed_size_; + return 0; +} + +u1* InputZipFile::UncompressFile() { + size_t in_offset = p - zipdata_in_; + size_t remaining = input_file_->Length() - in_offset; + DecompressedFile *decompressed_file = + decompressor_->UncompressFile(p, remaining); + if (decompressed_file == NULL) { + if (decompressor_->GetError() != NULL) { + error(decompressor_->GetError()); + } + return NULL; + } else { + compressed_size_ = decompressed_file->compressed_size; + uncompressed_size_ = decompressed_file->uncompressed_size; + u1 *uncompressed_data = decompressed_file->uncompressed_data; + free(decompressed_file); + p += compressed_size_; + return uncompressed_data; + } +} + +int InputZipFile::ProcessFile(const bool compressed) { + const u1 *file_data; + if (compressed) { + file_data = UncompressFile(); + if (file_data == NULL) { + return -1; + } + } else { + // In this case, compressed_size_ == uncompressed_size_ (since the file is + // uncompressed), so we can use either. + if (compressed_size_ != uncompressed_size_) { + return error("compressed size != uncompressed size, although the file " + "is uncompressed.\n"); + } + + if (EnsureRemaining(compressed_size_, "file_data") < 0) { + return -1; + } + file_data = p; + p += compressed_size_; + } + processor->Process(filename, attr, file_data, uncompressed_size_); + return 0; +} + + +// Reads and returns some metadata of the next file from the central directory: +// - compressed size +// - uncompressed size +// - whether the entry is a class file (to be included in the output). +// Precondition: p points to the beginning of an entry in the central dir +// Postcondition: p points to the beginning of the next entry in the central dir +// Returns true if the central directory contains another file and false if not. +// Of course, in the latter case, the size output variables are not changed. +// Note that the central directory is always followed by another data structure +// that has a signature, so parsing it this way is safe. +bool InputZipFile::ProcessCentralDirEntry(const u1 *&p, size_t *compressed_size, + size_t *uncompressed_size, + char *filename, size_t filename_size, + u4 *attr, u4 *offset) { + u4 signature = get_u4le(p); + + if (signature != CENTRAL_FILE_HEADER_SIGNATURE) { + if (signature != DIGITAL_SIGNATURE && signature != EOCD_SIGNATURE && + signature != ZIP64_EOCD_SIGNATURE) { + error("invalid central file header signature: 0x%x\n", signature); + } + return false; + } + + p += 16; // skip to 'compressed size' field + *compressed_size = get_u4le(p); + *uncompressed_size = get_u4le(p); + u2 file_name_length = get_u2le(p); + u2 extra_field_length = get_u2le(p); + u2 file_comment_length = get_u2le(p); + p += 4; // skip to external file attributes field + *attr = get_u4le(p); + *offset = get_u4le(p); + { + size_t len = (file_name_length < filename_size) + ? file_name_length + : (filename_size - 1); + memcpy(reinterpret_cast(filename), p, len); + filename[len] = 0; + } + p += file_name_length; + p += extra_field_length; + p += file_comment_length; + return true; +} + +// Gives a maximum bound on the size of the interface JAR. Basically, adds +// the difference between the compressed and uncompressed sizes to the size +// of the input file. +u8 InputZipFile::CalculateOutputLength() { + const u1* current = central_dir_; + + u8 compressed_size = 0; + u8 uncompressed_size = 0; + u8 skipped_compressed_size = 0; + u4 attr; + u4 offset; + char filename[PATH_MAX]; + + while (true) { + size_t file_compressed, file_uncompressed; + if (!ProcessCentralDirEntry(current, + &file_compressed, &file_uncompressed, + filename, PATH_MAX, &attr, &offset)) { + break; + } + + if (processor->Accept(filename, attr)) { + compressed_size += (u8) file_compressed; + uncompressed_size += (u8) file_uncompressed; + } else { + skipped_compressed_size += file_compressed; + } + } + + // The worst case is when the output is simply the input uncompressed. The + // metadata in the zip file will stay the same, so the file will grow by the + // difference between the compressed and uncompressed sizes. + return (u8) input_file_->Length() - skipped_compressed_size + + (uncompressed_size - compressed_size); +} + +// An end of central directory record, sized for optional zip64 contents. +struct EndOfCentralDirectoryRecord { + u4 number_of_this_disk; + u4 disk_with_central_dir; + u8 central_dir_entries_on_this_disk; + u8 central_dir_entries; + u8 central_dir_size; + u8 central_dir_offset; +}; + +// Checks for a zip64 end of central directory record. If a valid zip64 EOCD is +// found, updates the original EOCD record and returns true. +bool MaybeReadZip64CentralDirectory(const u1 *bytes, size_t /*in_length*/, + const u1 *current, + const u1 **end_of_central_dir, + EndOfCentralDirectoryRecord *cd) { + if (current < bytes) { + return false; + } + const u1 *candidate = current; + u4 zip64_directory_signature = get_u4le(current); + if (zip64_directory_signature != ZIP64_EOCD_SIGNATURE) { + return false; + } + + // size of zip64 end of central directory record + // (fixed size unless there's a zip64 extensible data sector, which + // we don't need to read) + get_u8le(current); + get_u2be(current); // version made by + get_u2be(current); // version needed to extract + + u4 number_of_this_disk = get_u4be(current); + u4 disk_with_central_dir = get_u4le(current); + u8 central_dir_entries_on_this_disk = get_u8le(current); + u8 central_dir_entries = get_u8le(current); + u8 central_dir_size = get_u8le(current); + u8 central_dir_offset = get_u8le(current); + + // check for a zip64 EOCD that matches the regular EOCD + if (number_of_this_disk != cd->number_of_this_disk && + cd->number_of_this_disk != U2_MAX) { + return false; + } + if (disk_with_central_dir != cd->disk_with_central_dir && + cd->disk_with_central_dir != U2_MAX) { + return false; + } + if (central_dir_entries_on_this_disk != + cd->central_dir_entries_on_this_disk && + cd->central_dir_entries_on_this_disk != U2_MAX) { + return false; + } + if (central_dir_entries != cd->central_dir_entries && + cd->central_dir_entries != U2_MAX) { + return false; + } + if (central_dir_size != cd->central_dir_size && + cd->central_dir_size != U4_MAX) { + return false; + } + if (central_dir_offset != cd->central_dir_offset && + cd->central_dir_offset != U4_MAX) { + return false; + } + + *end_of_central_dir = candidate; + cd->number_of_this_disk = number_of_this_disk; + cd->disk_with_central_dir = disk_with_central_dir; + cd->central_dir_entries_on_this_disk = central_dir_entries_on_this_disk; + cd->central_dir_entries = central_dir_entries; + cd->central_dir_size = central_dir_size; + cd->central_dir_offset = central_dir_offset; + return true; +} + +// Starting from the end of central directory record, attempts to locate a zip64 +// end of central directory record. If found, updates the given record and +// offset with the zip64 data. Returns false on error. +bool FindZip64CentralDirectory(const u1 *bytes, size_t in_length, + const u1 **end_of_central_dir, + EndOfCentralDirectoryRecord *cd) { + // In the absence of a zip64 extensible data sector, the zip64 EOCD is at a + // fixed offset from the regular central directory. + if (MaybeReadZip64CentralDirectory( + bytes, in_length, + *end_of_central_dir - ZIP64_EOCD_LOCATOR_SIZE - ZIP64_EOCD_FIXED_SIZE, + end_of_central_dir, cd)) { + return true; + } + + // If we couldn't find a zip64 EOCD at a fixed offset, either it doesn't exist + // or there was a zip64 extensible data sector, so try going through the + // locator. This approach doesn't work if data was prepended to the archive + // without updating the offset in the locator. + const u1 *zip64_locator = *end_of_central_dir - ZIP64_EOCD_LOCATOR_SIZE; + if (zip64_locator - ZIP64_EOCD_FIXED_SIZE < bytes) { + return true; + } + u4 zip64_locator_signature = get_u4le(zip64_locator); + if (zip64_locator_signature != ZIP64_EOCD_LOCATOR_SIGNATURE) { + return true; + } + u4 disk_with_zip64_central_directory = get_u4le(zip64_locator); + u8 zip64_end_of_central_dir_offset = get_u8le(zip64_locator); + u4 zip64_total_disks = get_u4le(zip64_locator); + if (MaybeReadZip64CentralDirectory(bytes, in_length, + bytes + zip64_end_of_central_dir_offset, + end_of_central_dir, cd)) { + if (disk_with_zip64_central_directory != 0 || zip64_total_disks != 1) { + fprintf(stderr, "multi-disk JAR files are not supported\n"); + return false; + } + return true; + } + return true; +} + +// Given the data in the zip file, returns the offset of the central directory +// and the number of files contained in it. +bool FindZipCentralDirectory(const u1 *bytes, size_t in_length, u4 *offset, + const u1 **central_dir) { + static const int MAX_COMMENT_LENGTH = 0xffff; + static const int CENTRAL_DIR_LOCATOR_SIZE = 22; + // Maximum distance of start of central dir locator from end of file + static const int MAX_DELTA = MAX_COMMENT_LENGTH + CENTRAL_DIR_LOCATOR_SIZE; + const u1* last_pos_to_check = in_length < MAX_DELTA + ? bytes + : bytes + (in_length - MAX_DELTA); + const u1* current; + bool found = false; + + for (current = bytes + in_length - CENTRAL_DIR_LOCATOR_SIZE; + current >= last_pos_to_check; + current-- ) { + const u1* p = current; + if (get_u4le(p) != EOCD_SIGNATURE) { + continue; + } + + p += 16; // skip to comment length field + u2 comment_length = get_u2le(p); + + // Does the comment go exactly till the end of the file? + if (current + comment_length + CENTRAL_DIR_LOCATOR_SIZE + != bytes + in_length) { + continue; + } + + // Hooray, we found it! + found = true; + break; + } + + if (!found) { + fprintf(stderr, "file is invalid or corrupted (missing end of central " + "directory record)\n"); + return false; + } + + EndOfCentralDirectoryRecord cd; + const u1* end_of_central_dir = current; + get_u4le(current); // central directory locator signature, already checked + cd.number_of_this_disk = get_u2le(current); + cd.disk_with_central_dir = get_u2le(current); + cd.central_dir_entries_on_this_disk = get_u2le(current); + cd.central_dir_entries = get_u2le(current); + cd.central_dir_size = get_u4le(current); + cd.central_dir_offset = get_u4le(current); + u2 file_comment_length = get_u2le(current); + current += file_comment_length; // set current to the end of the central dir + + if (!FindZip64CentralDirectory(bytes, in_length, &end_of_central_dir, &cd)) { + return false; + } + + if (cd.number_of_this_disk != 0 || cd.disk_with_central_dir != 0 || + cd.central_dir_entries_on_this_disk != cd.central_dir_entries) { + fprintf(stderr, "multi-disk JAR files are not supported\n"); + return false; + } + + // Do not change output values before determining that they are OK. + *offset = cd.central_dir_offset; + // Central directory start can then be used to determine the actual + // starts of the zip file (which can be different in case of a non-zip + // header like for auto-extractable binaries). + *central_dir = end_of_central_dir - cd.central_dir_size; + return true; +} + +void InputZipFile::Reset() { + central_dir_current_ = central_dir_; + bytes_unmapped_ = 0; + p = zipdata_in_ + in_offset_; +} + +int ZipExtractor::ProcessAll() { + while (ProcessNext()) {} + if (GetError() != NULL) { + return -1; + } + return 0; +} + +ZipExtractor* ZipExtractor::Create(const char* filename, + ZipExtractorProcessor *processor) { + InputZipFile* result = new InputZipFile(processor, filename); + if (!result->Open()) { + fprintf(stderr, "Opening zip \"%s\": %s\n", filename, result->GetError()); + delete result; + return NULL; + } + + return result; +} + +// zipdata_in_, in_offset_, p, central_dir_current_ + +InputZipFile::InputZipFile(ZipExtractorProcessor *processor, + const char* filename) + : processor(processor), filename_(filename), input_file_(NULL), + bytes_unmapped_(0) { + decompressor_ = new Decompressor(); + errmsg[0] = 0; +} + +bool InputZipFile::Open() { + MappedInputFile* input_file = new MappedInputFile(filename_); + if (!input_file->Opened()) { + snprintf(errmsg, sizeof(errmsg), "%s", input_file->Error()); + delete input_file; + return false; + } + + void *zipdata_in = input_file->Buffer(); + u4 central_dir_offset; + const u1 *central_dir = NULL; + + if (!devtools_ijar::FindZipCentralDirectory( + static_cast(zipdata_in), input_file->Length(), + ¢ral_dir_offset, ¢ral_dir)) { + errno = EIO; // we don't really have a good error number + error("Cannot find central directory"); + delete input_file; + return false; + } + const u1 *zipdata_start = static_cast(zipdata_in); + in_offset_ = - static_cast(zipdata_start + + central_dir_offset + - central_dir); + + input_file_ = input_file; + zipdata_in_ = zipdata_start; + central_dir_ = central_dir; + central_dir_current_ = central_dir; + p = zipdata_in_ + in_offset_; + errmsg[0] = 0; + return true; +} + +InputZipFile::~InputZipFile() { + delete decompressor_; + if (input_file_ != NULL) { + input_file_->Close(); + delete input_file_; + } +} + + +// +// Implementation of OutputZipFile +// +int OutputZipFile::WriteEmptyFile(const char *filename) { + const u1* file_name = (const u1*) filename; + size_t file_name_length = strlen(filename); + + LocalFileEntry *entry = new LocalFileEntry; + entry->local_header_offset = Offset(q); + entry->external_attr = 0; + entry->crc32 = 0; + + // Output the ZIP local_file_header: + put_u4le(q, LOCAL_FILE_HEADER_SIGNATURE); + put_u2le(q, 10); // extract_version + put_u2le(q, 0); // general_purpose_bit_flag + put_u2le(q, 0); // compression_method + put_u4le(q, kDefaultTimestamp); // last_mod_file date and time + put_u4le(q, entry->crc32); // crc32 + put_u4le(q, 0); // compressed_size + put_u4le(q, 0); // uncompressed_size + put_u2le(q, file_name_length); + put_u2le(q, 0); // extra_field_length + put_n(q, file_name, file_name_length); + + entry->file_name_length = file_name_length; + entry->extra_field_length = 0; + entry->compressed_length = 0; + entry->uncompressed_length = 0; + entry->compression_method = 0; + entry->extra_field = (const u1 *)""; + entry->file_name = (u1*) strdup((const char *) file_name); + entries_.push_back(entry); + + return 0; +} + +void OutputZipFile::WriteCentralDirectory() { + // central directory: + const u1 *central_directory_start = q; + for (size_t ii = 0; ii < entries_.size(); ++ii) { + LocalFileEntry *entry = entries_[ii]; + put_u4le(q, CENTRAL_FILE_HEADER_SIGNATURE); + put_u2le(q, UNIX_ZIP_FILE_VERSION); + + put_u2le(q, ZIP_VERSION_TO_EXTRACT); // version to extract + put_u2le(q, 0); // general purpose bit flag + put_u2le(q, entry->compression_method); // compression method: + put_u4le(q, kDefaultTimestamp); // last_mod_file date and time + put_u4le(q, entry->crc32); // crc32 + put_u4le(q, entry->compressed_length); // compressed_size + put_u4le(q, entry->uncompressed_length); // uncompressed_size + put_u2le(q, entry->file_name_length); + put_u2le(q, entry->extra_field_length); + + put_u2le(q, 0); // file comment length + put_u2le(q, 0); // disk number start + put_u2le(q, 0); // internal file attributes + put_u4le(q, entry->external_attr); // external file attributes + // relative offset of local header: + put_u4le(q, entry->local_header_offset); + + put_n(q, entry->file_name, entry->file_name_length); + put_n(q, entry->extra_field, entry->extra_field_length); + } + u8 central_directory_size = q - central_directory_start; + + if (entries_.size() > U2_MAX || central_directory_size > U4_MAX || + Offset(central_directory_start) > U4_MAX) { + u1 *zip64_end_of_central_directory_start = q; + + put_u4le(q, ZIP64_EOCD_SIGNATURE); + // signature and size field doesn't count towards size + put_u8le(q, ZIP64_EOCD_FIXED_SIZE - 12); + put_u2le(q, UNIX_ZIP_FILE_VERSION); // version made by + put_u2le(q, 0); // version needed to extract + put_u4le(q, 0); // number of this disk + put_u4le(q, 0); // # of the disk with the start of the central directory + put_u8le(q, entries_.size()); // # central dir entries on this disk + put_u8le(q, entries_.size()); // total # entries in the central directory + put_u8le(q, central_directory_size); // size of the central directory + // offset of start of central directory wrt starting disk + put_u8le(q, Offset(central_directory_start)); + + put_u4le(q, ZIP64_EOCD_LOCATOR_SIGNATURE); + // number of the disk with the start of the zip64 end of central directory + put_u4le(q, 0); + // relative offset of the zip64 end of central directory record + put_u8le(q, Offset(zip64_end_of_central_directory_start)); + // total number of disks + put_u4le(q, 1); + + put_u4le(q, EOCD_SIGNATURE); + put_u2le(q, 0); // number of this disk + put_u2le(q, 0); // # of disk with the start of the central directory + // # central dir entries on this disk + put_u2le(q, entries_.size() > 0xffff ? 0xffff : entries_.size()); + // total # entries in the central directory + put_u2le(q, entries_.size() > 0xffff ? 0xffff : entries_.size()); + // size of the central directory + put_u4le(q, + central_directory_size > U4_MAX ? U4_MAX : central_directory_size); + // offset of start of central + put_u4le(q, Offset(central_directory_start) > U4_MAX + ? U4_MAX + : Offset(central_directory_start)); + put_u2le(q, 0); // .ZIP file comment length + + } else { + put_u4le(q, EOCD_SIGNATURE); + put_u2le(q, 0); // number of this disk + put_u2le(q, 0); // # of the disk with the start of the central directory + put_u2le(q, entries_.size()); // # central dir entries on this disk + put_u2le(q, entries_.size()); // total # entries in the central directory + put_u4le(q, central_directory_size); // size of the central directory + // offset of start of central directory wrt starting disk + put_u4le(q, Offset(central_directory_start)); + put_u2le(q, 0); // .ZIP file comment length + } +} + +u1* OutputZipFile::WriteLocalFileHeader(const char* filename, const u4 attr) { + off_t file_name_length_ = strlen(filename); + LocalFileEntry *entry = new LocalFileEntry; + entry->local_header_offset = Offset(q); + entry->file_name_length = file_name_length_; + entry->file_name = new u1[file_name_length_]; + entry->external_attr = attr; + memcpy(entry->file_name, filename, file_name_length_); + entry->extra_field_length = 0; + entry->extra_field = (const u1 *)""; + entry->crc32 = 0; + + // Output the ZIP local_file_header: + put_u4le(q, LOCAL_FILE_HEADER_SIGNATURE); + put_u2le(q, ZIP_VERSION_TO_EXTRACT); // version to extract + put_u2le(q, 0); // general purpose bit flag + u1 *header_ptr = q; + put_u2le(q, COMPRESSION_METHOD_STORED); // compression method = placeholder + put_u4le(q, kDefaultTimestamp); // last_mod_file date and time + put_u4le(q, entry->crc32); // crc32 + put_u4le(q, 0); // compressed_size = placeholder + put_u4le(q, 0); // uncompressed_size = placeholder + put_u2le(q, entry->file_name_length); + put_u2le(q, entry->extra_field_length); + + put_n(q, entry->file_name, entry->file_name_length); + put_n(q, entry->extra_field, entry->extra_field_length); + entries_.push_back(entry); + + return header_ptr; +} + +size_t OutputZipFile::WriteFileSizeInLocalFileHeader(u1 *header_ptr, + size_t out_length, + bool compress, + const u4 crc) { + size_t compressed_size = out_length; + if (compress) { + compressed_size = TryDeflate(q, out_length); + } + // compression method + if (compressed_size < out_length) { + put_u2le(header_ptr, COMPRESSION_METHOD_DEFLATED); + } else { + put_u2le(header_ptr, COMPRESSION_METHOD_STORED); + } + header_ptr += 4; + put_u4le(header_ptr, crc); // crc32 + put_u4le(header_ptr, compressed_size); // compressed_size + put_u4le(header_ptr, out_length); // uncompressed_size + return compressed_size; +} + +int OutputZipFile::Finish() { + if (finished_) { + return 0; + } + + finished_ = true; + WriteCentralDirectory(); + if (output_file_->Close(GetSize()) < 0) { + return error("%s", output_file_->Error()); + } + delete output_file_; + output_file_ = NULL; + return 0; +} + +u1* OutputZipFile::NewFile(const char* filename, const u4 attr) { + header_ptr = WriteLocalFileHeader(filename, attr); + return q; +} + +int OutputZipFile::FinishFile(size_t filelength, bool compress, + bool compute_crc) { + u4 crc = 0; + if (compute_crc) { + crc = ComputeCrcChecksum(q, filelength); + + if (filelength > 0 && crc == 0) { + fprintf(stderr, "Error calculating CRC32 checksum.\n"); + return -1; + } + } + size_t compressed_size = + WriteFileSizeInLocalFileHeader(header_ptr, filelength, compress, crc); + + if (compressed_size == 0 && filelength > 0) { + fprintf(stderr, "Error compressing files.\n"); + return -1; + } + + entries_.back()->crc32 = crc; + entries_.back()->compressed_length = compressed_size; + entries_.back()->uncompressed_length = filelength; + if (compressed_size < filelength) { + entries_.back()->compression_method = COMPRESSION_METHOD_DEFLATED; + } else { + entries_.back()->compression_method = COMPRESSION_METHOD_STORED; + } + q += compressed_size; + return 0; +} + +bool OutputZipFile::Open() { + if (estimated_size_ > kMaximumOutputSize) { + fprintf(stderr, + "Uncompressed input jar has size %zu, " + "which exceeds the maximum supported output size %zu.\n" + "Assuming that ijar will be smaller and hoping for the best.\n", + estimated_size_, kMaximumOutputSize); + estimated_size_ = kMaximumOutputSize; + } + + MappedOutputFile* output_file = new MappedOutputFile( + filename_, estimated_size_); + if (!output_file->Opened()) { + snprintf(errmsg, sizeof(errmsg), "%s", output_file->Error()); + delete output_file; + return false; + } + + output_file_ = output_file; + q = output_file->Buffer(); + zipdata_out_ = output_file->Buffer(); + return true; +} + +ZipBuilder *ZipBuilder::Create(const char *zip_file, size_t estimated_size) { + OutputZipFile* result = new OutputZipFile(zip_file, estimated_size); + if (!result->Open()) { + fprintf(stderr, "%s\n", result->GetError()); + delete result; + return NULL; + } + + return result; +} + +u8 ZipBuilder::EstimateSize(char const* const* files, + char const* const* zip_paths, + int nb_entries) { + Stat file_stat; + // Digital signature field size = 6, End of central directory = 22, Total = 28 + u8 size = 28; + // Count the size of all the files in the input to estimate the size of the + // output. + for (int i = 0; i < nb_entries; i++) { + file_stat.total_size = 0; + if (files[i] != NULL && !stat_file(files[i], &file_stat)) { + fprintf(stderr, "File %s does not seem to exist.", files[i]); + return 0; + } + size += file_stat.total_size; + // Add sizes of Zip meta data + // local file header = 30 bytes + // data descriptor = 12 bytes + // central directory descriptor = 46 bytes + // Total: 88bytes + size += 88; + // The filename is stored twice (once in the central directory + // and once in the local file header). + size += strlen((zip_paths[i] != NULL) ? zip_paths[i] : files[i]) * 2; + } + return size; +} + +} // namespace devtools_ijar diff --git a/third_party/java_tools/ijar/zip.h b/third_party/java_tools/ijar/zip.h new file mode 100644 index 000000000..b6c765a7a --- /dev/null +++ b/third_party/java_tools/ijar/zip.h @@ -0,0 +1,169 @@ +// Copyright 2015 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// zip.h -- .zip (.jar) file reading/writing routines. +// +// This file specifies the interface to use the ZIP implementation of ijar. +// + +#ifndef INCLUDED_THIRD_PARTY_IJAR_ZIP_H +#define INCLUDED_THIRD_PARTY_IJAR_ZIP_H + +#include + +#include "common.h" + +namespace devtools_ijar { + +// Tells if this is a directory entry from the mode. This method +// is safer than zipattr_to_mode(attr) & S_IFDIR because the unix +// mode might not be set in DOS zip files. +inline bool zipattr_is_dir(u4 attr) { return (attr & 0x10) != 0; } + +// Convert a ZIP file attribute to a Unix file permission mask. +inline mode_t zipattr_to_perm(u4 attr) { + return ((mode_t)((attr >> 16) & 0777)); +} + +// +// Class interface for building ZIP files +// +class ZipBuilder { + public: + virtual ~ZipBuilder() {} + + // Returns the text for the last error, or null on no last error. + virtual const char* GetError() = 0; + + // Add a new file to the ZIP, the file will have path "filename" + // and external attributes "attr". This function returns a pointer + // to a memory buffer to write the data of the file into. This buffer + // is owned by ZipBuilder and should not be free'd by the caller. The + // file length is then specified when the files is finished written + // using the FinishFile(size_t) function. + // On failure, returns NULL and GetError() will return an non-empty message. + virtual u1* NewFile(const char* filename, const u4 attr) = 0; + + // Finish writing a file and specify its length. After calling this method + // one should not reuse the pointer given by NewFile. The file can be + // compressed using the deflate algorithm by setting `compress` to true. + // By default, CRC32 are not computed as java tooling doesn't care, but + // computing it can be activated by setting `compute_crc` to true. + // On failure, returns -1 and GetError() will return an non-empty message. + virtual int FinishFile(size_t filelength, + bool compress = false, + bool compute_crc = false) = 0; + + // Write an empty file, it is equivalent to: + // NewFile(filename, 0); + // FinishFile(0); + // On failure, returns -1 and GetError() will return an non-empty message. + virtual int WriteEmptyFile(const char* filename) = 0; + + // Finish writing the ZIP file. This method can be called only once + // (subsequent calls will do nothing) and none of + // NewFile/FinishFile/WriteEmptyFile should be called after calling Finish. If + // this method was not called when the object is destroyed, it will be called. + // It is here as a convenience to get information on the final generated ZIP + // file. + // On failure, returns -1 and GetError() will return an non-empty message. + virtual int Finish() = 0; + + // Get the current size of the ZIP file. This size will not be matching the + // final ZIP file until Finish() has been called because Finish() is actually + // writing the central directory of the ZIP File. + virtual size_t GetSize() = 0; + + // Returns the current number of files stored in the ZIP. + virtual int GetNumberFiles() = 0; + + // Create a new ZipBuilder writing the file zip_file and the size of the + // output will be at most estimated_size. Use ZipBuilder::EstimateSize() or + // ZipExtractor::CalculateOuputLength() to have an estimated_size depending on + // a list of file to store. + // On failure, returns NULL. Refer to errno for error code. + static ZipBuilder* Create(const char* zip_file, size_t estimated_size); + + // Estimate the maximum size of the ZIP files containing files in the "files" + // null-terminated array. + // Returns 0 on error. + static u8 EstimateSize(char const* const* files, char const* const* zip_paths, + int nb_entries); +}; + +// +// An abstract class to process data from a ZipExtractor. +// Derive from this class if you wish to process data from a ZipExtractor. +// +class ZipExtractorProcessor { + public: + virtual ~ZipExtractorProcessor() {} + + // Tells whether to skip or process the file "filename". "attr" is the + // external file attributes and can be converted to unix mode using the + // zipattr_to_mode() function. This method is suppoed to returns true + // if the file should be processed and false if it should be skipped. + virtual bool Accept(const char* filename, const u4 attr) = 0; + + // Process a file accepted by Accept. The file "filename" has external + // attributes "attr" and length "size". The file content is accessible + // in the buffer pointed by "data". + virtual void Process(const char* filename, const u4 attr, + const u1* data, const size_t size) = 0; +}; + +// +// Class interface for reading ZIP files +// +class ZipExtractor { + public: + virtual ~ZipExtractor() {} + + // Returns the text for the last error, or null on no last error. + virtual const char* GetError() = 0; + + // Process the next files, returns false if the end of ZIP file has been + // reached. The processor provided by the Create method will be called + // if a file is encountered. If false is returned, check the return value + // of GetError() for potential errors. + virtual bool ProcessNext() = 0; + + // Process the all files, returns -1 on error (GetError() will be populated + // on error). + virtual int ProcessAll(); + + // Reset the file pointer to the beginning. + virtual void Reset() = 0; + + // Return the size of the ZIP file. + virtual size_t GetSize() = 0; + + // Return the size of the resulting zip file by keeping only file + // accepted by the processor and storing them uncompressed. This + // method can be used to create a ZipBuilder for storing a subset + // of the input files. + // On error, 0 is returned and GetError() returns a non-empty message. + virtual u8 CalculateOutputLength() = 0; + + // Create a ZipExtractor that extract the zip file "filename" and process + // it with "processor". + // On error, a null pointer is returned and the value of errno should be + // checked. + static ZipExtractor* Create(const char* filename, + ZipExtractorProcessor *processor); +}; + +} // namespace devtools_ijar + +#endif // INCLUDED_THIRD_PARTY_IJAR_ZIP_H diff --git a/third_party/java_tools/ijar/zip_main.cc b/third_party/java_tools/ijar/zip_main.cc new file mode 100644 index 000000000..ba33a8b60 --- /dev/null +++ b/third_party/java_tools/ijar/zip_main.cc @@ -0,0 +1,478 @@ +// Copyright 2015 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +// Zip / Unzip file using ijar zip implementation. +// +// Note that this Zip implementation intentionally don't compute CRC-32 +// because it is useless computation for jar because Java doesn't care. +// CRC-32 of all files in the zip file will be set to 0. +// + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "third_party/ijar/platform_utils.h" +#include "third_party/ijar/zip.h" + +namespace devtools_ijar { + +// +// A ZipExtractorProcessor that extract files in the ZIP file. +// +class UnzipProcessor : public ZipExtractorProcessor { + public: + // Create a processor who will extract the given files (or all files if NULL) + // into output_root if "extract" is set to true and will print the list of + // files and their unix modes if "verbose" is set to true. + UnzipProcessor(const char *output_root, char **files, bool verbose, + bool extract, bool flatten) + : output_root_(output_root), + verbose_(verbose), + extract_(extract), + flatten_(flatten) { + if (files != NULL) { + for (int i = 0; files[i] != NULL; i++) { + file_names.insert(std::string(files[i])); + } + } + } + + virtual ~UnzipProcessor() {} + + virtual void Process(const char* filename, const u4 attr, + const u1* data, const size_t size); + virtual bool Accept(const char* filename, const u4 attr) { + // All entry files are accepted by default. + if (file_names.empty()) { + return true; + } else { + // If users have specified file entries, only accept those files. + return file_names.count(std::string(filename)) == 1; + } + } + + private: + const char *output_root_; + const bool verbose_; + const bool extract_; + const bool flatten_; + std::set file_names; +}; + +// Concatene 2 path, path1 and path2, using / as a directory separator and +// putting the result in "out". "size" specify the size of the output buffer. If +// the result would overflow the output buffer, print an error message and +// return false. +bool concat_path(char *out, const size_t size, const char *path1, + const char *path2) { + int len1 = strlen(path1); + size_t l = len1; + strncpy(out, path1, size - 1); + out[size - 1] = 0; + if (l < size - 1 && path1[len1] != '/' && path2[0] != '/') { + out[l] = '/'; + l++; + out[l] = 0; + } + if (l >= size - 1) { + fprintf(stderr, "paths too long to concat: %s + %s", path1, path2); + return false; + } + strncat(out, path2, size - 1 - l); + return true; +} + +void UnzipProcessor::Process(const char* filename, const u4 attr, + const u1* data, const size_t size) { + mode_t perm = zipattr_to_perm(attr); + bool isdir = zipattr_is_dir(attr); + const char *output_file_name = filename; + if (attr == 0) { + // Fallback when the external attribute is not set. + isdir = filename[strlen(filename)-1] == '/'; + perm = 0777; + } + + if (flatten_) { + if (isdir) { + return; + } + const char *p = strrchr(filename, '/'); + if (p != NULL) { + output_file_name = p + 1; + } + } + + if (verbose_) { + printf("%c %o %s\n", isdir ? 'd' : 'f', perm, output_file_name); + } + if (extract_) { + char path[PATH_MAX]; + if (!concat_path(path, sizeof(path), output_root_, output_file_name) || + !make_dirs(path, perm) || + (!isdir && !write_file(path, perm, data, size))) { + abort(); + } + } +} + +// Get the basename of path and store it in output. output_size +// is the size of the output buffer. +void basename(const char *path, char *output, size_t output_size) { + const char *pointer = strrchr(path, '/'); + if (pointer == NULL) { + pointer = path; + } else { + pointer++; // Skip the leading slash. + } + strncpy(output, pointer, output_size - 1); + output[output_size - 1] = 0; +} + +// Execute the extraction (or just listing if just v is provided) +int extract(char *zipfile, char *exdir, char **files, bool verbose, + bool extract, bool flatten) { + std::string cwd = get_cwd(); + if (cwd.empty()) { + return -1; + } + + char output_root[PATH_MAX + 1]; + if (exdir != NULL) { + if (!concat_path(output_root, sizeof(output_root), cwd.c_str(), exdir)) { + return -1; + } + } else if (cwd.length() >= sizeof(output_root)) { + fprintf(stderr, "current working directory path too long"); + return -1; + } else { + memcpy(output_root, cwd.c_str(), cwd.length() + 1); + } + + UnzipProcessor processor(output_root, files, verbose, extract, flatten); + std::unique_ptr extractor(ZipExtractor::Create(zipfile, + &processor)); + if (extractor == NULL) { + fprintf(stderr, "Unable to open zip file %s: %s.\n", zipfile, + strerror(errno)); + return -1; + } + + if (extractor->ProcessAll() < 0) { + fprintf(stderr, "%s.\n", extractor->GetError()); + return -1; + } + return 0; +} + +// add a file to the zip +int add_file(std::unique_ptr const &builder, char *file, + char *zip_path, bool flatten, bool verbose, bool compress) { + Stat file_stat = {0, 0666, false}; + if (file != NULL) { + if (!stat_file(file, &file_stat)) { + fprintf(stderr, "Cannot stat file %s: %s\n", file, strerror(errno)); + return -1; + } + } + char *final_path = zip_path != NULL ? zip_path : file; + + bool isdir = file_stat.is_directory; + + if (flatten && isdir) { + return 0; + } + + // Compute the path, flattening it if requested + char path[PATH_MAX]; + size_t len = strlen(final_path); + if (len > PATH_MAX) { + fprintf(stderr, "Path too long: %s.\n", final_path); + return -1; + } + if (flatten) { + basename(final_path, path, PATH_MAX); + } else { + strncpy(path, final_path, PATH_MAX); + path[PATH_MAX - 1] = 0; + if (isdir && len < PATH_MAX - 1) { + // Add the trailing slash for folders + path[len] = '/'; + path[len + 1] = 0; + } + } + + if (verbose) { + mode_t perm = file_stat.file_mode & 0777; + printf("%c %o %s\n", isdir ? 'd' : 'f', perm, path); + } + + u1 *buffer = builder->NewFile(path, stat_to_zipattr(file_stat)); + if (isdir || file_stat.total_size == 0) { + builder->FinishFile(0); + } else { + if (!read_file(file, buffer, file_stat.total_size)) { + return -1; + } + builder->FinishFile(file_stat.total_size, compress, true); + } + return 0; +} + +// Read a list of files separated by newlines. The resulting array can be +// freed using the free method. +char **read_filelist(char *filename) { + Stat file_stat; + if (!stat_file(filename, &file_stat)) { + fprintf(stderr, "Cannot stat file %s: %s\n", filename, strerror(errno)); + return NULL; + } + + char *data = static_cast(malloc(file_stat.total_size)); + if (!read_file(filename, data, file_stat.total_size)) { + return NULL; + } + + int nb_entries = 1; + for (int i = 0; i < file_stat.total_size; i++) { + if (data[i] == '\n') { + nb_entries++; + } + } + + size_t sizeof_array = sizeof(char *) * (nb_entries + 1); + void *result = malloc(sizeof_array + file_stat.total_size + 1); + // copy the content + char **filelist = static_cast(result); + char *content = static_cast(result) + sizeof_array; + memcpy(content, data, file_stat.total_size); + content[file_stat.total_size] = '\0'; + free(data); + // Create the corresponding array + int j = 1; + filelist[0] = content; + for (int i = 0; i < file_stat.total_size; i++) { + if (content[i] == '\n') { + content[i] = 0; + if (i + 1 < file_stat.total_size) { + filelist[j] = content + i + 1; + j++; + } + } + } + filelist[j] = NULL; + return filelist; +} + +// return real paths of the files +char **parse_filelist(char *zipfile, char **file_entries, int nb_entries, + bool flatten) { + // no need to free since the path lists should live until the end of the + // program + char **files = static_cast(malloc(sizeof(char *) * nb_entries)); + char **zip_paths = file_entries; + for (int i = 0; i < nb_entries; i++) { + char *p_eq = strchr(file_entries[i], '='); + if (p_eq != NULL) { + if (flatten) { + fprintf(stderr, "Unable to create zip file %s: %s.\n", zipfile, + "= can't be used with flatten"); + free(files); + return NULL; + } + if (p_eq == file_entries[i]) { + fprintf(stderr, "Unable to create zip file %s: %s.\n", zipfile, + "A zip path should be given before ="); + free(files); + return NULL; + } + *p_eq = '\0'; + files[i] = p_eq + 1; + if (files[i][0] == '\0') { + files[i] = NULL; + } + } else { + files[i] = file_entries[i]; + zip_paths[i] = NULL; + } + } + return files; +} + +// Execute the create operation +int create(char *zipfile, char **file_entries, bool flatten, bool verbose, + bool compress) { + int nb_entries = 0; + while (file_entries[nb_entries] != NULL) { + nb_entries++; + } + char **zip_paths = file_entries; + char **files = parse_filelist(zipfile, file_entries, nb_entries, flatten); + if (files == NULL) { + return -1; + } + + u8 size = ZipBuilder::EstimateSize(files, zip_paths, nb_entries); + if (size == 0) { + return -1; + } + std::unique_ptr builder(ZipBuilder::Create(zipfile, size)); + if (builder == NULL) { + fprintf(stderr, "Unable to create zip file %s: %s.\n", + zipfile, strerror(errno)); + return -1; + } + + for (int i = 0; i < nb_entries; i++) { + if (add_file(builder, files[i], zip_paths[i], flatten, verbose, compress) < + 0) { + return -1; + } + } + if (builder->Finish() < 0) { + fprintf(stderr, "%s\n", builder->GetError()); + return -1; + } + return 0; +} + +} // namespace devtools_ijar + +// +// main method +// +static void usage(char *progname) { + fprintf(stderr, + "Usage: %s [vxc[fC]] x.zip [-d exdir] [[zip_path1=]file1 ... " + "[zip_pathn=]filen]\n", + progname); + fprintf(stderr, " v verbose - list all file in x.zip\n"); + fprintf(stderr, + " x extract - extract files in x.zip to current directory, or " + " an optional directory relative to the current directory " + " specified through -d option\n"); + fprintf(stderr, " c create - add files to x.zip\n"); + fprintf(stderr, + " f flatten - flatten files to use with create or " + "extract operation\n"); + fprintf(stderr, + " C compress - compress files when using the create operation\n"); + fprintf(stderr, "x and c cannot be used in the same command-line.\n"); + fprintf(stderr, + "\nFor every file, a path in the zip can be specified. Examples:\n"); + fprintf(stderr, + " zipper c x.zip a/b/__init__.py= # Add an empty file at " + "a/b/__init__.py\n"); + fprintf(stderr, + " zipper c x.zip a/b/main.py=foo/bar/bin.py # Add file " + "foo/bar/bin.py at a/b/main.py\n"); + fprintf(stderr, + "\nIf the zip path is not specified, it is assumed to be the file " + "path.\n"); + exit(1); +} + +int main(int argc, char **argv) { + bool extract = false; + bool verbose = false; + bool create = false; + bool compress = false; + bool flatten = false; + + if (argc < 3) { + usage(argv[0]); + } + + for (int i = 0; argv[1][i] != 0; i++) { + switch (argv[1][i]) { + case 'x': + extract = true; + break; + case 'v': + verbose = true; + break; + case 'c': + create = true; + break; + case 'f': + flatten = true; + break; + case 'C': + compress = true; + break; + default: + usage(argv[0]); + } + } + + // x and c cannot be used in the same command-line. + if (create && extract) { + usage(argv[0]); + } + + // Calculate the argument index of the first entry file. + int filelist_start_index; + if (argc > 3 && strcmp(argv[3], "-d") == 0) { + filelist_start_index = 5; + } else { + filelist_start_index = 3; + } + + char** filelist = NULL; + + // We have one option file. Read and extract the content. + if (argc == filelist_start_index + 1 && + argv[filelist_start_index][0] == '@') { + char* filelist_name = argv[filelist_start_index]; + filelist = devtools_ijar::read_filelist(filelist_name + 1); + if (filelist == NULL) { + fprintf(stderr, "Can't read file list %s: %s.\n", filelist_name, + strerror(errno)); + return -1; + } + // We have more than one files. Assume that they are all file entries. + } else if (argc >= filelist_start_index + 1) { + filelist = argv + filelist_start_index; + } else { + // There are no entry files specified. This is forbidden if we are creating + // a zip file. + if (create) { + fprintf(stderr, "Can't create zip without input files specified."); + return -1; + } + } + + if (create) { + // Create a zip + return devtools_ijar::create(argv[2], filelist, flatten, verbose, compress); + } else { + char* exdir = NULL; + if (argc > 3 && strcmp(argv[3], "-d") == 0) { + exdir = argv[4]; + } + + // Extraction / list mode + return devtools_ijar::extract(argv[2], exdir, filelist, verbose, extract, + flatten); + } +} diff --git a/third_party/java_tools/ijar/zlib_client.cc b/third_party/java_tools/ijar/zlib_client.cc new file mode 100644 index 000000000..cddcaab7c --- /dev/null +++ b/third_party/java_tools/ijar/zlib_client.cc @@ -0,0 +1,162 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include "common.h" +#include "zlib_client.h" +#include + +namespace devtools_ijar { + +u4 ComputeCrcChecksum(u1 *buf, size_t length) { + return crc32(0, buf, length); +} + +size_t TryDeflate(u1 *buf, size_t length) { + u1 *outbuf = reinterpret_cast(malloc(length)); + z_stream stream; + + // Initialize the z_stream strcut for reading from buf and wrinting in outbuf. + stream.zalloc = Z_NULL; + stream.zfree = Z_NULL; + stream.opaque = Z_NULL; + stream.total_in = length; + stream.avail_in = length; + stream.total_out = length; + stream.avail_out = length; + stream.next_in = buf; + stream.next_out = outbuf; + + // deflateInit2 negative windows size prevent the zlib wrapper to be used. + if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 8, + Z_DEFAULT_STRATEGY) != Z_OK) { + // Failure to compress => return the buffer uncompressed + free(outbuf); + return length; + } + + if (deflate(&stream, Z_FINISH) == Z_STREAM_END) { + // Compression successful and fits in outbuf, let's copy the result in buf. + length = stream.total_out; + memcpy(buf, outbuf, length); + } + + deflateEnd(&stream); + free(outbuf); + + // Return the length of the resulting buffer + return length; +} + +Decompressor::Decompressor() { + uncompressed_data_allocated_ = INITIAL_BUFFER_SIZE; + uncompressed_data_ = + reinterpret_cast(malloc(uncompressed_data_allocated_)); +} + +Decompressor::~Decompressor() { free(uncompressed_data_); } + +DecompressedFile *Decompressor::UncompressFile(const u1 *buffer, + size_t bytes_avail) { + z_stream stream; + + stream.zalloc = Z_NULL; + stream.zfree = Z_NULL; + stream.opaque = Z_NULL; + stream.avail_in = bytes_avail; + stream.next_in = const_cast(reinterpret_cast(buffer)); + + int ret = inflateInit2(&stream, -MAX_WBITS); + if (ret != Z_OK) { + error("inflateInit: %d\n", ret); + return NULL; + } + + int uncompressed_until_now = 0; + + while (true) { + stream.avail_out = uncompressed_data_allocated_ - uncompressed_until_now; + stream.next_out = uncompressed_data_ + uncompressed_until_now; + int old_avail_out = stream.avail_out; + + ret = inflate(&stream, Z_SYNC_FLUSH); + int uncompressed_now = old_avail_out - stream.avail_out; + uncompressed_until_now += uncompressed_now; + + switch (ret) { + case Z_STREAM_END: { + struct DecompressedFile *decompressedFile = + reinterpret_cast( + malloc(sizeof(DecompressedFile))); + // zlib said that there is no more data to decompress. + u1 *new_p = reinterpret_cast(stream.next_in); + decompressedFile->compressed_size = new_p - buffer; + decompressedFile->uncompressed_size = uncompressed_until_now; + decompressedFile->uncompressed_data = uncompressed_data_; + inflateEnd(&stream); + return decompressedFile; + } + + case Z_OK: { + // zlib said that there is no more room in the buffer allocated for + // the decompressed data. Enlarge that buffer and try again. + + if (uncompressed_data_allocated_ == MAX_BUFFER_SIZE) { + error( + "ijar does not support decompressing files " + "larger than %dMB.\n", + static_cast((MAX_BUFFER_SIZE / (1024 * 1024)))); + return NULL; + } + + uncompressed_data_allocated_ *= 2; + if (uncompressed_data_allocated_ > MAX_BUFFER_SIZE) { + uncompressed_data_allocated_ = MAX_BUFFER_SIZE; + } + + uncompressed_data_ = reinterpret_cast( + realloc(uncompressed_data_, uncompressed_data_allocated_)); + break; + } + + case Z_DATA_ERROR: + case Z_BUF_ERROR: + case Z_STREAM_ERROR: + case Z_NEED_DICT: + default: { + error("zlib returned error code %d during inflate.\n", ret); + return NULL; + } + } + } +} + +char *Decompressor::GetError() { + if (errmsg[0] == 0) { + return NULL; + } + return errmsg; +} + +int Decompressor::error(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + vsnprintf(errmsg, 4 * PATH_MAX, fmt, ap); + va_end(ap); + return -1; +} +} // namespace devtools_ijar diff --git a/third_party/java_tools/ijar/zlib_client.h b/third_party/java_tools/ijar/zlib_client.h new file mode 100644 index 000000000..75db92d02 --- /dev/null +++ b/third_party/java_tools/ijar/zlib_client.h @@ -0,0 +1,69 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_IJAR_ZLIB_CLIENT_H_ +#define THIRD_PARTY_IJAR_ZLIB_CLIENT_H_ + +#include +#include + +#include "common.h" + +namespace devtools_ijar { +// Try to compress a file entry in memory using the deflate algorithm. +// It will compress buf (of size length) unless the compressed size is bigger +// than the input size. The result will overwrite the content of buf and the +// final size is returned. +size_t TryDeflate(u1* buf, size_t length); + +u4 ComputeCrcChecksum(u1* buf, size_t length); + +struct DecompressedFile { + u1* uncompressed_data; + u4 uncompressed_size; + u4 compressed_size; +}; + +class Decompressor { + public: + Decompressor(); + ~Decompressor(); + DecompressedFile* UncompressFile(const u1* buffer, size_t bytes_avail); + char* GetError(); + + private: + // Administration of memory reserved for decompressed data. We use the same + // buffer for each file to avoid some malloc()/free() calls and free the + // memory only in the dtor. C-style memory management is used so that we + // can call realloc. + u1* uncompressed_data_; + size_t uncompressed_data_allocated_; + // last error + char errmsg[4 * PATH_MAX]; + + int error(const char* fmt, ...); + + // Buffer size is initially INITIAL_BUFFER_SIZE. It doubles in size every + // time it is found too small, until it reaches MAX_BUFFER_SIZE. If that is + // not enough, we bail out. We only decompress class files, so they should + // be smaller than 64K anyway, but we give a little leeway. + // MAX_BUFFER_SIZE must be bigger than the size of the biggest file in the + // ZIP. It is set to 2GB here because no one has audited the code for 64-bit + // cleanliness. + static const size_t INITIAL_BUFFER_SIZE = 256 * 1024; // 256K + static const size_t MAX_BUFFER_SIZE = std::numeric_limits::max(); +}; +} // namespace devtools_ijar + +#endif // THIRD_PARTY_IJAR_ZLIB_CLIENT_H_ diff --git a/third_party/java_tools/src/main/cpp/util/BUILD b/third_party/java_tools/src/main/cpp/util/BUILD new file mode 100644 index 000000000..6b4a14c2c --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/BUILD @@ -0,0 +1,141 @@ +# Description: +# C++ utility source for Bazel +package_group( + name = "ijar", + packages = [ + "//third_party/ijar/...", + ], +) + +package(default_visibility = ["//visibility:public"]) + +load("@rules_cc//cc:defs.bzl", "cc_library") + +cc_library( + name = "util", + hdrs = [ + "errors.h", + "file.h", + "file_platform.h", + "md5.h", + "numbers.h", + "path.h", + "path_platform.h", + "port.h", + ], + visibility = ["//visibility:public"], + deps = [ + ":blaze_exit_code", + ":errors", + ":filesystem", + ":md5", + ":numbers", + ":port", + ":strings", + ], +) + +cc_library( + name = "filesystem", + srcs = [ + "file.cc", + "file_posix.cc", + "path.cc", + "path_posix.cc", + ], + hdrs = [ + "file.h", + "file_platform.h", + "path.h", + "path_platform.h", + ], + deps = [ + ":blaze_exit_code", + ":errors", + ":logging", + ":strings", + ], +) + +cc_library( + name = "errors", + srcs = ["errors_posix.cc"], + hdrs = ["errors.h"], + deps = [ + ":logging", + ":port", + ":strings", + ], +) + +cc_library( + name = "port", + srcs = ["port.cc"], + hdrs = ["port.h"], +) + +cc_library( + name = "numbers", + srcs = ["numbers.cc"], + hdrs = ["numbers.h"], + deps = [":strings"], +) + +cc_library( + name = "logging", + srcs = ["logging.cc"], + hdrs = ["logging.h"], + deps = [ + ":blaze_exit_code", + ":strings", + ], +) + +cc_library( + name = "bazel_log_handler", + srcs = ["bazel_log_handler.cc"], + hdrs = ["bazel_log_handler.h"], + deps = [ + ":blaze_exit_code", + ":filesystem", + ":logging", + ], +) + +cc_library( + name = "md5", + srcs = ["md5.cc"], + hdrs = ["md5.h"], +) + +cc_library( + name = "strings", + srcs = ["strings.cc"], + hdrs = ["strings.h"], + # Automatically propagate the symbol definition to rules depending on this. + defines = [ + "BLAZE_OPENSOURCE", + ], + visibility = ["//visibility:public"], + deps = [":blaze_exit_code"], +) + +cc_library( + name = "blaze_exit_code", + hdrs = ["exit_code.h"], +) + +filegroup( + name = "srcs", + srcs = glob(["**"]), +) + +filegroup( + name = "embedded_tools", + srcs = glob(["*.cc"]) + glob(["*.h"]) + ["BUILD"], +) + +filegroup( + name = "embedded_java_tools", + srcs = glob(["*.cc"]) + glob(["*.h"]), +) diff --git a/third_party/java_tools/src/main/cpp/util/bazel_log_handler.cc b/third_party/java_tools/src/main/cpp/util/bazel_log_handler.cc new file mode 100644 index 000000000..914c2bf39 --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/bazel_log_handler.cc @@ -0,0 +1,194 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "third_party/java_tools/src/main/cpp/util/bazel_log_handler.h" + +#include // NOLINT -- for windows portability +#include +#include +#include +#include +#include + +#include "third_party/java_tools/src/main/cpp/util/exit_code.h" +#include "third_party/java_tools/src/main/cpp/util/file.h" +#include "third_party/java_tools/src/main/cpp/util/logging.h" + +namespace blaze_util { + +BazelLogHandler::BazelLogHandler() + : output_stream_set_(false), + logging_deactivated_(false), + user_buffer_stream_(new std::stringstream()), + debug_buffer_stream_(new std::stringstream()), + output_stream_(), + owned_output_stream_() {} + +BazelLogHandler::~BazelLogHandler() { + if (!logging_deactivated_) { + // If SetLoggingOutputStream was never called, dump the buffer to stderr, + // otherwise, flush the stream. + if (output_stream_ != nullptr) { + output_stream_->flush(); + } else if (debug_buffer_stream_ != nullptr) { + std::cerr << debug_buffer_stream_->rdbuf(); + } else { + std::cerr << "Illegal state - neither a logfile nor a logbuffer " + << "existed at program end." << std::endl; + } + } +} + +// Messages intended for the user (level USER, along with WARNINGs an ERRORs) +// should be printed even if debug level logging was not requested. +void PrintUserLevelMessageToStream(std::ostream* stream, LogLevel level, + const std::string& message) { + if (level == LOGLEVEL_USER) { + (*stream) << message << std::endl; + } else if (level > LOGLEVEL_USER) { + (*stream) << LogLevelName(level) << ": " << message << std::endl; + } + // If level < USER, this is an INFO message. It's useful for debugging but + // should not be printed to the user unless the user has asked for debugging + // output. We ignore it here. +} + +static std::string Timestamp() { + auto now = std::chrono::system_clock::now(); + time_t s = std::chrono::system_clock::to_time_t(now); + auto ms = std::chrono::duration_cast( + now.time_since_epoch()); + struct tm tmbuf = {}; +#ifdef _WIN32 + tmbuf = *localtime(&s); // NOLINT -- threadsafe on windows +#else + localtime_r(&s, &tmbuf); +#endif + char buf[16]; + int r = strftime(buf, sizeof buf - 5, "%H:%M:%S", &tmbuf); + r += snprintf(buf + r, +5, ".%03d", static_cast(ms.count() % 1000)); + return std::string(buf, r); +} + +// For debug logs, print all logs, both debug logging and USER logs and above, +// along with information about where the log message came from. +void PrintDebugLevelMessageToStream(std::ostream* stream, + const std::string& filename, int line, + LogLevel level, + const std::string& message) { + (*stream) << "[" << LogLevelName(level) << " " << Timestamp() << " " + << filename << ":" << line << "] " << message << std::endl; +} + +void BazelLogHandler::HandleMessage(LogLevel level, const std::string& filename, + int line, const std::string& message, + int exit_code) { + if (logging_deactivated_) { + // If the output stream was explicitly deactivated, never print INFO + // messages, but messages of level USER and above should always be printed, + // as should warnings and errors. Omit the debug-level file and line number + // information, though. + PrintUserLevelMessageToStream(&std::cerr, level, message); + if (level == LOGLEVEL_FATAL) { + std::exit(exit_code); + } + return; + } + if (output_stream_ == nullptr) { + // If we haven't decided whether messages should be logged to debug levels + // or not, buffer each version. This is redundant for USER levels and above, + // but is to make sure we can provide the right output to the user once we + // know that they do or do not want debug level information. + PrintUserLevelMessageToStream(user_buffer_stream_.get(), level, message); + PrintDebugLevelMessageToStream(debug_buffer_stream_.get(), filename, line, + level, message); + } else { + // If an output stream has been specifically set, it is for the full suite + // of log messages. We don't print the user messages separately here as they + // are included. + PrintDebugLevelMessageToStream(output_stream_, filename, line, level, + message); + } + + // If we have a fatal message, exit with the provided error code. + if (level == LOGLEVEL_FATAL) { + if (owned_output_stream_ != nullptr) { + // If this is is not being printed to stderr but to a custom stream, + // also print the error message to stderr. + PrintUserLevelMessageToStream(&std::cerr, level, message); + } + std::exit(exit_code); + } +} + +void BazelLogHandler::SetOutputStreamToStderr() { + // Disallow second calls to this, we only intend to support setting the output + // once, otherwise the buffering will not work as intended and the log will be + // fragmented. + BAZEL_CHECK(!output_stream_set_) << "Tried to set log output a second time"; + output_stream_set_ = true; + + FlushBufferToNewStreamAndSet(debug_buffer_stream_.get(), &std::cerr); + debug_buffer_stream_ = nullptr; + // The user asked for debug level information, which includes the user + // messages. We can discard the separate buffer at this point. + user_buffer_stream_ = nullptr; +} + +void BazelLogHandler::SetOutputStream( + std::unique_ptr new_output_stream) { + // Disallow second calls to this, we only intend to support setting the output + // once, otherwise the buffering will not work as intended and the log will be + // fragmented. + BAZEL_CHECK(!output_stream_set_) << "Tried to set log output a second time"; + output_stream_set_ = true; + + if (new_output_stream == nullptr) { + logging_deactivated_ = true; + // Flush the buffered user-level messages to stderr - these are messages + // that are meant for the user even when debug logging is not set. + FlushBufferToNewStreamAndSet(user_buffer_stream_.get(), &std::cerr); + + user_buffer_stream_ = nullptr; + // We discard the debug level logs, the user level ones were enough to + // inform the user and debug logging was not requested. + debug_buffer_stream_ = nullptr; + return; + } + owned_output_stream_ = std::move(new_output_stream); + if (owned_output_stream_->fail()) { + // If opening the stream failed, continue buffering and have the logs + // dump to stderr at shutdown. + BAZEL_LOG(ERROR) << "Provided stream failed."; + return; + } + FlushBufferToNewStreamAndSet(debug_buffer_stream_.get(), + owned_output_stream_.get()); + debug_buffer_stream_ = nullptr; + // The user asked for debug level information, which includes the user + // messages. We can discard the separate buffer at this point. + user_buffer_stream_ = nullptr; +} + +void BazelLogHandler::FlushBufferToNewStreamAndSet( + std::stringstream* buffer, std::ostream* new_output_stream) { + // Flush the buffer to the new stream, and print new log lines to it. + output_stream_ = new_output_stream; + // Transfer the contents of the buffer to the new stream, then remove the + // buffer. + (*output_stream_) << buffer->str(); + output_stream_->flush(); +} + +} // namespace blaze_util diff --git a/third_party/java_tools/src/main/cpp/util/bazel_log_handler.h b/third_party/java_tools/src/main/cpp/util/bazel_log_handler.h new file mode 100644 index 000000000..4ea81959a --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/bazel_log_handler.h @@ -0,0 +1,56 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef BAZEL_SRC_MAIN_CPP_BAZEL_LOG_HANDLER_H_ +#define BAZEL_SRC_MAIN_CPP_BAZEL_LOG_HANDLER_H_ + +#include + +#include "third_party/java_tools/src/main/cpp/util/logging.h" + +namespace blaze_util { + +// Handles logging for the Bazel client. +// In order to control where the log output goes based on the flags received at +// startup, logs are buffered until SetOutputStream is called. At that point, +// all past log statements are dumped in the appropriate stream, and all +// following statements are logged directly. +class BazelLogHandler : public blaze_util::LogHandler { + public: + BazelLogHandler(); + ~BazelLogHandler() override; + + void HandleMessage(blaze_util::LogLevel level, const std::string& filename, + int line, const std::string& message, + int exit_code) override; + void SetOutputStream( + std::unique_ptr new_output_stream) override; + void SetOutputStreamToStderr() override; + + private: + void FlushBufferToNewStreamAndSet(std::stringstream* buffer, + std::ostream* new_output_stream); + bool output_stream_set_; + bool logging_deactivated_; + std::unique_ptr user_buffer_stream_; + std::unique_ptr debug_buffer_stream_; + // The actual output_stream to which all logs will be sent. + std::ostream* output_stream_; + // A unique pts to the output_stream, if we need to keep ownership of the + // stream. In the case of stderr logging, this is null. + std::unique_ptr owned_output_stream_; +}; +} // namespace blaze_util + +#endif // BAZEL_SRC_MAIN_CPP_BAZEL_LOG_HANDLER_H_ diff --git a/third_party/java_tools/src/main/cpp/util/errors.h b/third_party/java_tools/src/main/cpp/util/errors.h new file mode 100644 index 000000000..19b7db98c --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/errors.h @@ -0,0 +1,33 @@ +// Copyright 2015 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// TODO(b/32967056) die() and pdie() are really error statements with an exit; +// these can be removed once logging is on by default. + +#ifndef BAZEL_SRC_MAIN_CPP_UTIL_ERRORS_H_ +#define BAZEL_SRC_MAIN_CPP_UTIL_ERRORS_H_ + +#include +#include "port.h" + +namespace blaze_util { + +// Returns the last error as a platform-specific error message. +// The string will also contain the platform-specific error code itself +// (which is `errno` on Linux/Darwin, and `GetLastError()` on Windows). +std::string GetLastErrorString(); + +} // namespace blaze_util + +#endif // BAZEL_SRC_MAIN_CPP_UTIL_ERRORS_H_ diff --git a/third_party/java_tools/src/main/cpp/util/errors_posix.cc b/third_party/java_tools/src/main/cpp/util/errors_posix.cc new file mode 100644 index 000000000..1afc0132c --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/errors_posix.cc @@ -0,0 +1,33 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include // strerror +#include +#include +#include "errors.h" + +namespace blaze_util { + +using std::string; +using std::stringstream; + +string GetLastErrorString() { + int saved_errno = errno; + stringstream result; + result << "(error: " << saved_errno << "): " << strerror(saved_errno); + return result.str(); +} + +} // namespace blaze_util diff --git a/third_party/java_tools/src/main/cpp/util/errors_windows.cc b/third_party/java_tools/src/main/cpp/util/errors_windows.cc new file mode 100644 index 000000000..b7e014870 --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/errors_windows.cc @@ -0,0 +1,49 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include + +#include +#include + +#include "src/main/cpp/util/errors.h" + +namespace blaze_util { + +using std::string; +using std::stringstream; + +string GetLastErrorString() { + DWORD last_error = ::GetLastError(); + if (last_error == 0) { + return "success"; + } + + char* message_buffer; + size_t size = FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, last_error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPSTR)&message_buffer, 0, NULL); + + stringstream result; + result << "(error: " << last_error << "): " << message_buffer; + LocalFree(message_buffer); + return result.str(); +} + +} // namespace blaze_util diff --git a/third_party/java_tools/src/main/cpp/util/exit_code.h b/third_party/java_tools/src/main/cpp/util/exit_code.h new file mode 100644 index 000000000..fc095d83d --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/exit_code.h @@ -0,0 +1,51 @@ +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// exit_code.h: Exit codes for Blaze. +// Must be kept in sync with the Java counterpart under +// com/google/devtools/build/lib/util/ExitCode.java + +#ifndef BAZEL_SRC_MAIN_CPP_UTIL_EXIT_CODE_H_ +#define BAZEL_SRC_MAIN_CPP_UTIL_EXIT_CODE_H_ + +namespace blaze_exit_code { + +enum ExitCode { + // Success. + SUCCESS = 0, + + // Command Line Problem, Bad or Illegal flags or command combination, or + // Bad environment variables. The user must modify their command line. + BAD_ARGV = 2, + + // The user interrupted the build, most probably with Ctrl-C. + INTERRUPTED = 8, + + // The client or server lock is held, and --noblock_for_lock was passed, so + // this command fails fast. + LOCK_HELD_NOBLOCK_FOR_LOCK = 9, + + // Something is wrong with the host Bazel is running on and a re-run of the + // same command probably will not help. + LOCAL_ENVIRONMENTAL_ERROR = 36, + + // Unexpected server termination, due to e.g. external SIGKILL, misplaced + // System.exit(), or a JVM crash. + // This exit code should be a last resort. + INTERNAL_ERROR = 37, +}; + +} // namespace blaze_exit_code + +#endif // BAZEL_SRC_MAIN_CPP_UTIL_EXIT_CODE_H_ diff --git a/third_party/java_tools/src/main/cpp/util/file.cc b/third_party/java_tools/src/main/cpp/util/file.cc new file mode 100644 index 000000000..f823775a1 --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/file.cc @@ -0,0 +1,127 @@ +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "file.h" + +#include // PATH_MAX + +#include +#include +#include + +#include "third_party/java_tools/src/main/cpp/util/errors.h" +#include "third_party/java_tools/src/main/cpp/util/exit_code.h" +#include "third_party/java_tools/src/main/cpp/util/path.h" +#include "third_party/java_tools/src/main/cpp/util/strings.h" + +namespace blaze_util { + +using std::string; +using std::vector; + +bool ReadFrom(file_handle_type handle, string *content, int max_size) { + static const size_t kReadSize = 4096; // read 4K chunks + content->clear(); + char buf[kReadSize]; + // OPT: This loop generates one spurious read on regular files. + int error; + while (int r = ReadFromHandle( + handle, buf, + max_size > 0 ? std::min(static_cast(max_size), kReadSize) + : kReadSize, + &error)) { + if (r < 0) { + if (error == ReadFileResult::INTERRUPTED || + error == ReadFileResult::AGAIN) { + continue; + } + return false; + } + content->append(buf, r); + if (max_size > 0) { + if (max_size > r) { + max_size -= r; + } else { + break; + } + } + } + return true; +} + +bool ReadFrom(file_handle_type handle, void *data, size_t size) { + static const size_t kReadSize = 4096; // read 4K chunks + size_t offset = 0; + int error; + while (int r = ReadFromHandle(handle, reinterpret_cast(data) + offset, + std::min(kReadSize, size), &error)) { + if (r < 0) { + if (error == ReadFileResult::INTERRUPTED || + error == ReadFileResult::AGAIN) { + continue; + } + return false; + } + offset += r; + if (size > static_cast(r)) { + size -= r; + } else { + break; + } + } + return true; +} + +bool WriteFile(const std::string &content, const std::string &filename, + unsigned int perm) { + return WriteFile(content.c_str(), content.size(), filename, perm); +} + +bool WriteFile(const std::string &content, const Path &path, + unsigned int perm) { + return WriteFile(content.c_str(), content.size(), path, perm); +} + +class DirectoryTreeWalker : public DirectoryEntryConsumer { + public: + DirectoryTreeWalker(vector *files, + _ForEachDirectoryEntry walk_entries) + : _files(files), _walk_entries(walk_entries) {} + + void Consume(const string &path, bool follow_directory) override { + if (follow_directory) { + Walk(path); + } else { + _files->push_back(path); + } + } + + void Walk(const string &path) { _walk_entries(path, this); } + + private: + vector *_files; + _ForEachDirectoryEntry _walk_entries; +}; + +void GetAllFilesUnder(const string &path, vector *result) { + _GetAllFilesUnder(path, result, &ForEachDirectoryEntry); +} + +void _GetAllFilesUnder(const string &path, + vector *result, + _ForEachDirectoryEntry walk_entries) { + DirectoryTreeWalker(result, walk_entries).Walk(path); +} + +} // namespace blaze_util diff --git a/third_party/java_tools/src/main/cpp/util/file.h b/third_party/java_tools/src/main/cpp/util/file.h new file mode 100644 index 000000000..bd43f04cf --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/file.h @@ -0,0 +1,92 @@ +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef BAZEL_SRC_MAIN_CPP_UTIL_FILE_H_ +#define BAZEL_SRC_MAIN_CPP_UTIL_FILE_H_ + +#include +#include + +#include "third_party/java_tools/src/main/cpp/util/file_platform.h" + +namespace blaze_util { + +class IPipe { + public: + // Error modes of the pipe. + // + // This is a platform-independent abstraction of `errno`. If you need to + // handle an errno value, add an entry here and update the platform-specific + // pipe implementations accordingly. + enum Errors { + SUCCESS = 0, + OTHER_ERROR = 1, + INTERRUPTED = 2, // EINTR + }; + + virtual ~IPipe() {} + + // Sends `size` bytes from `buffer` through the pipe. + // Returns true if `size` is not negative and could send all the data. + virtual bool Send(const void *buffer, int size) = 0; + + // Receives at most `size` bytes into `buffer` from the pipe. + // Returns the number of bytes received. + // If `size` is negative or if there's an error, then returns -1, and if + // `error` isn't NULL then sets its value to one of the `Errors`. + virtual int Receive(void *buffer, int size, int *error) = 0; +}; + +// Replaces 'content' with data read from a source using `ReadFromHandle`. +// If `max_size` is positive, the method reads at most that many bytes; +// otherwise the method reads everything. +// Returns false on error. Can be called from a signal handler. +bool ReadFrom(file_handle_type handle, std::string *content, int max_size = 0); + +// Reads up to `size` bytes using `ReadFromHandle` into `data`. +// There must be enough memory allocated at `data`. +// Returns true on success, false on error. +bool ReadFrom(file_handle_type handle, void *data, size_t size); + +// Writes `content` into file `filename`, and chmods it to `perm`. +// Returns false on failure. +bool WriteFile(const std::string &content, const std::string &filename, + unsigned int perm = 0644); + +bool WriteFile(const std::string &content, const Path &path, + unsigned int perm = 0644); + +// Lists all files in `path` and all of its subdirectories. +// +// Does not follow symlinks / junctions. +// +// Populates `result` with the full paths of the files. Every entry will have +// `path` as its prefix. If `path` is a file, `result` contains just this +// file. +void GetAllFilesUnder(const std::string &path, + std::vector *result); + +class DirectoryEntryConsumer; + +// Visible for testing only. +typedef void (*_ForEachDirectoryEntry)(const std::string &path, + DirectoryEntryConsumer *consume); + +// Visible for testing only. +void _GetAllFilesUnder(const std::string &path, + std::vector *result, + _ForEachDirectoryEntry walk_entries); + +} // namespace blaze_util + +#endif // BAZEL_SRC_MAIN_CPP_UTIL_FILE_H_ diff --git a/third_party/java_tools/src/main/cpp/util/file_platform.h b/third_party/java_tools/src/main/cpp/util/file_platform.h new file mode 100644 index 000000000..4e8e254ed --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/file_platform.h @@ -0,0 +1,273 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef BAZEL_SRC_MAIN_CPP_UTIL_FILE_PLATFORM_H_ +#define BAZEL_SRC_MAIN_CPP_UTIL_FILE_PLATFORM_H_ + +#include + +#include +#include +#include + +namespace blaze_util { + +class Path; + +class IPipe; + +IPipe* CreatePipe(); + +// Class to query/manipulate the last modification time (mtime) of files. +class IFileMtime { + public: + virtual ~IFileMtime() {} + + // Checks if `path` is a file/directory in the embedded tools directory that + // was not tampered with. + // Returns true if `path` is a directory or directory symlink, or if `path` is + // a file with an mtime in the distant future. + // Returns false otherwise, or if querying the information failed. + // TODO(laszlocsomor): move this function, and with it the whole IFileMtime + // class into blaze_util_.cc, because it is Bazel-specific logic, + // not generic file-handling logic. + virtual bool IsUntampered(const Path &path) = 0; + + // Sets the mtime of file under `path` to the current time. + // Returns true if the mtime was changed successfully. + virtual bool SetToNow(const Path &path) = 0; + + // Sets the mtime of file under `path` to the distant future. + // "Distant future" should be on the order of some years into the future, like + // a decade. + // Returns true if the mtime was changed successfully. + virtual bool SetToDistantFuture(const Path &path) = 0; +}; + +// Creates a platform-specific implementation of `IFileMtime`. +IFileMtime *CreateFileMtime(); + +#if defined(_WIN32) || defined(__CYGWIN__) +// We cannot include because it #defines many symbols that conflict +// with our function names, e.g. GetUserName, SendMessage. +// Instead of typedef'ing HANDLE, let's use the actual type, void*. If that ever +// changes in the future and HANDLE would no longer be compatible with void* +// (very unlikely, given how fundamental this type is in Windows), then we'd get +// a compilation error. +typedef /* HANDLE */ void *file_handle_type; +#else // !(defined(_WIN32) || defined(__CYGWIN__)) +typedef int file_handle_type; +#endif // defined(_WIN32) || defined(__CYGWIN__) + +// Result of a `ReadFromHandle` operation. +// +// This is a platform-independent abstraction of `errno`. If you need to handle +// an errno value, add an entry here and update the platform-specific +// `ReadFromHandle` implementations accordingly. +struct ReadFileResult { + enum Errors { + SUCCESS = 0, + OTHER_ERROR = 1, + INTERRUPTED = 2, + AGAIN = 3, + }; +}; + +int ReadFromHandle(file_handle_type handle, void *data, size_t size, + int *error); + +// Replaces 'content' with contents of file 'filename'. +// If `max_size` is positive, the method reads at most that many bytes; +// otherwise the method reads the whole file. +// Returns false on error. Can be called from a signal handler. +bool ReadFile(const std::string &filename, std::string *content, + int max_size = 0); +bool ReadFile(const Path &path, std::string *content, int max_size = 0); + +// Reads up to `size` bytes from the file `filename` into `data`. +// There must be enough memory allocated at `data`. +// Returns true on success, false on error. +bool ReadFile(const std::string &filename, void *data, size_t size); +bool ReadFile(const Path &filename, void *data, size_t size); + +// Writes `size` bytes from `data` into file `filename` and chmods it to `perm`. +// Returns false on failure, sets errno. +bool WriteFile(const void *data, size_t size, const std::string &filename, + unsigned int perm = 0644); + +bool WriteFile(const void *data, size_t size, const Path &path, + unsigned int perm = 0644); + +// Result of a `WriteToStdOutErr` operation. +// +// This is a platform-independent abstraction of `errno`. If you need to handle +// an errno value, add an entry here and update the platform-specific +// `WriteToStdOutErr` implementations accordingly. +struct WriteResult { + enum Errors { + SUCCESS = 0, + OTHER_ERROR = 1, // some uncategorized error occurred + BROKEN_PIPE = 2, // EPIPE (reading end of the pipe is closed) + }; +}; + +// Writes `size` bytes from `data` into stdout/stderr. +// Writes to stdout if `to_stdout` is true, writes to stderr otherwise. +// Returns one of `WriteResult::Errors`. +// +// This is a platform-independent abstraction of `fwrite` with `errno` checking +// and awareness of pipes (i.e. in case stderr/stdout is connected to a pipe). +int WriteToStdOutErr(const void *data, size_t size, bool to_stdout); + +enum RenameDirectoryResult { + kRenameDirectorySuccess = 0, + kRenameDirectoryFailureNotEmpty = 1, + kRenameDirectoryFailureOtherError = 2, +}; + +// Renames the directory at `old_name` to `new_name`. +// Returns one of the RenameDirectoryResult enum values. +int RenameDirectory(const std::string &old_name, const std::string &new_name); + +// Reads which directory a symlink points to. Puts the target of the symlink +// in ``result`` and returns if the operation was successful. Will not work on +// symlinks that don't point to directories on Windows. +bool ReadDirectorySymlink(const blaze_util::Path &symlink, std::string *result); + +// Unlinks the file given by 'file_path'. +// Returns true on success. In case of failure sets errno. +bool UnlinkPath(const std::string &file_path); +bool UnlinkPath(const Path &file_path); + +// Returns true if this path exists, following symlinks. +bool PathExists(const std::string& path); +bool PathExists(const Path &path); + +// Returns the real, absolute path corresponding to `path`. +// The method resolves all symlink components of `path`. +// Returns the empty string upon error. +// +// This is a wrapper around realpath(3). +std::string MakeCanonical(const char *path); + +// Returns true if `path` exists, is a file or symlink to one, and is readable. +// Follows symlinks. +bool CanReadFile(const std::string &path); +bool CanReadFile(const Path &path); + +// Returns true if `path` exists, is a file or symlink to one, and is writable. +// Follows symlinks. +bool CanExecuteFile(const std::string &path); +bool CanExecuteFile(const Path &path); + +// Returns true if `path` exists, is a directory or symlink/junction to one, and +// is both readable and writable. +// Follows symlinks/junctions. +bool CanAccessDirectory(const std::string &path); +bool CanAccessDirectory(const Path &path); + +// Returns true if `path` refers to a directory or a symlink/junction to one. +bool IsDirectory(const std::string& path); +bool IsDirectory(const Path &path); + +// Calls fsync() on the file (or directory) specified in 'file_path'. +// pdie() if syncing fails. +void SyncFile(const std::string& path); +void SyncFile(const Path &path); + +// mkdir -p path. All newly created directories use the given mode. +// `mode` should be an octal permission mask, e.g. 0755. +// Returns false on failure, sets errno. +bool MakeDirectories(const std::string &path, unsigned int mode); +bool MakeDirectories(const Path &path, unsigned int mode); + +// Creates a directory starting with prefix for temporary usage. The directory +// name is guaranteed to be at least unique to this process. +std::string CreateTempDir(const std::string &prefix); + +// Removes the specified path or directory, and in the latter case, all of its +// contents. Returns true iff the path doesn't exists when the method completes +// (including if the path didn't exist to begin with). Does not follow symlinks. +bool RemoveRecursively(const std::string &path); + +// Returns the current working directory. +// The path is platform-specific (e.g. Windows path of Windows) and absolute. +std::string GetCwd(); + +// Changes the current working directory to `path`, returns true upon success. +bool ChangeDirectory(const std::string& path); + +// Interface to be implemented by ForEachDirectoryEntry clients. +class DirectoryEntryConsumer { + public: + virtual ~DirectoryEntryConsumer() {} + + // This method is called for each entry in a directory. + // `name` is the full path of the entry. + // `is_directory` is true if this entry is a directory (but false if this is a + // symlink pointing to a directory). + virtual void Consume(const std::string &name, bool is_directory) = 0; +}; + +// Executes a function for each entry in a directory (except "." and ".."). +// +// Returns true if the `path` referred to a directory or directory symlink, +// false otherwise. +// +// See DirectoryEntryConsumer for more details. +void ForEachDirectoryEntry(const std::string &path, + DirectoryEntryConsumer *consume); + +#if defined(_WIN32) || defined(__CYGWIN__) +std::wstring GetCwdW(); +bool MakeDirectoriesW(const std::wstring &path, unsigned int mode); + +// Check if `path` is a directory. +bool IsDirectoryW(const std::wstring &path); + +// Interface to be implemented by ForEachDirectoryEntryW clients. +class DirectoryEntryConsumerW { + public: + virtual ~DirectoryEntryConsumerW() {} + + // This method is called for each entry in a directory. + // `name` is the full path of the entry. + // `is_directory` is true if this entry is a directory (but false if this is a + // symlink pointing to a directory). + virtual void Consume(const std::wstring &name, bool is_directory) = 0; +}; + +// Lists all files in `path` and all of its subdirectories. +// +// Does not follow symlinks / junctions. +// +// Populates `result` with the full paths of the files. Every entry will have +// `path` as its prefix. If `path` is a file, `result` contains just this +// file. +void GetAllFilesUnderW(const std::wstring &path, + std::vector *result); + +// Visible for testing only. +typedef void (*_ForEachDirectoryEntryW)(const std::wstring &path, + DirectoryEntryConsumerW *consume); + +// Visible for testing only. +void _GetAllFilesUnderW(const std::wstring &path, + std::vector *result, + _ForEachDirectoryEntryW walk_entries); +#endif // defined(_WIN32) || defined(__CYGWIN__) + +} // namespace blaze_util + +#endif // BAZEL_SRC_MAIN_CPP_UTIL_FILE_PLATFORM_H_ diff --git a/third_party/java_tools/src/main/cpp/util/file_posix.cc b/third_party/java_tools/src/main/cpp/util/file_posix.cc new file mode 100644 index 000000000..ddc93ac7c --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/file_posix.cc @@ -0,0 +1,559 @@ +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "file_platform.h" + +#include // DIR, dirent, opendir, closedir +#include +#include // O_RDONLY +#include // PATH_MAX +#include // getenv +#include // strncmp +#include +#include // access, open, close, fsync +#include // utime + +#include +#include + +#include "third_party/java_tools/src/main/cpp/util/errors.h" +#include "third_party/java_tools/src/main/cpp/util/exit_code.h" +#include "third_party/java_tools/src/main/cpp/util/file.h" +#include "third_party/java_tools/src/main/cpp/util/logging.h" +#include "third_party/java_tools/src/main/cpp/util/path.h" +#include "third_party/java_tools/src/main/cpp/util/path_platform.h" +#include "third_party/java_tools/src/main/cpp/util/strings.h" + +namespace blaze_util { + +using std::string; + +// Runs "stat" on `path`. Returns -1 and sets errno if stat fails or +// `path` isn't a directory. If check_perms is true, this will also +// make sure that `path` is owned by the current user and has `mode` +// permissions (observing the umask). It attempts to run chmod to +// correct the mode if necessary. If `path` is a symlink, this will +// check ownership of the link, not the underlying directory. +static bool GetDirectoryStat(const string &path, mode_t mode, + bool check_perms) { + struct stat filestat = {}; + if (stat(path.c_str(), &filestat) == -1) { + return false; + } + + if (!S_ISDIR(filestat.st_mode)) { + errno = ENOTDIR; + return false; + } + + if (check_perms) { + // If this is a symlink, run checks on the link. (If we did lstat above + // then it would return false for ISDIR). + struct stat linkstat = {}; + if (lstat(path.c_str(), &linkstat) != 0) { + return false; + } + if (linkstat.st_uid != geteuid()) { + // The directory isn't owned by me. + errno = EACCES; + return false; + } + + mode_t mask = umask(022); + umask(mask); + mode = (mode & ~mask); + if ((filestat.st_mode & 0777) != mode && chmod(path.c_str(), mode) == -1) { + // errno set by chmod. + return false; + } + } + return true; +} + +static bool MakeDirectories(const string &path, mode_t mode, bool childmost) { + if (path.empty() || IsRootDirectory(path)) { + errno = EACCES; + return false; + } + + bool stat_succeeded = GetDirectoryStat(path, mode, childmost); + if (stat_succeeded) { + return true; + } + + if (errno == ENOENT) { + // Path does not exist, attempt to create its parents, then it. + string parent = Dirname(path); + if (!MakeDirectories(parent, mode, false)) { + // errno set by stat. + return false; + } + + if (mkdir(path.c_str(), mode) == -1) { + if (errno == EEXIST) { + if (childmost) { + // If there are multiple bazel calls at the same time then the + // directory could be created between the MakeDirectories and mkdir + // calls. This is okay, but we still have to check the permissions. + return GetDirectoryStat(path, mode, childmost); + } else { + // If this isn't the childmost directory, we don't care what the + // permissions were. If it's not even a directory then that error will + // get caught when we attempt to create the next directory down the + // chain. + return true; + } + } + // errno set by mkdir. + return false; + } + return true; + } + + return stat_succeeded; +} + + +string CreateTempDir(const std::string &prefix) { + std::string parent = Dirname(prefix); + // Need parent to exist first. + if (!blaze_util::PathExists(parent) && + !blaze_util::MakeDirectories(parent, 0777)) { + BAZEL_DIE(blaze_exit_code::INTERNAL_ERROR) + << "couldn't create '" << parent << "': " + << blaze_util::GetLastErrorString(); + } + + std::string result(prefix + "XXXXXX"); + if (mkdtemp(&result[0]) == nullptr) { + std::string err = GetLastErrorString(); + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "could not create temporary directory under " << parent + << " to extract install base into (" << err << ")"; + } + + // There's no better way to get the current umask than to set and reset it. + const mode_t um = umask(0); + umask(um); + chmod(result.c_str(), 0777 & ~um); + + return result; +} + +static bool RemoveDirRecursively(const std::string &path) { + DIR *dir; + if ((dir = opendir(path.c_str())) == NULL) { + return false; + } + + struct dirent *ent; + while ((ent = readdir(dir)) != NULL) { + if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) { + continue; + } + + if (!RemoveRecursively(blaze_util::JoinPath(path, ent->d_name))) { + closedir(dir); + return false; + } + } + + if (closedir(dir) != 0) { + return false; + } + + return rmdir(path.c_str()) == 0; +} + +bool RemoveRecursively(const std::string &path) { + struct stat stat_buf; + if (lstat(path.c_str(), &stat_buf) == -1) { + // Non-existent is good enough. + return errno == ENOENT; + } + + if (S_ISDIR(stat_buf.st_mode) && !S_ISLNK(stat_buf.st_mode)) { + return RemoveDirRecursively(path); + } else { + return UnlinkPath(path); + } +} + +class PosixPipe : public IPipe { + public: + PosixPipe(int recv_socket, int send_socket) + : _recv_socket(recv_socket), _send_socket(send_socket) {} + + PosixPipe() = delete; + + virtual ~PosixPipe() { + close(_recv_socket); + close(_send_socket); + } + + bool Send(const void *buffer, int size) override { + return size >= 0 && write(_send_socket, buffer, size) == size; + } + + int Receive(void *buffer, int size, int *error) override { + if (size < 0) { + if (error != nullptr) { + *error = IPipe::OTHER_ERROR; + } + return -1; + } + int result = read(_recv_socket, buffer, size); + if (error != nullptr) { + *error = result >= 0 ? IPipe::SUCCESS + : ((errno == EINTR) ? IPipe::INTERRUPTED + : IPipe::OTHER_ERROR); + } + return result; + } + + private: + int _recv_socket; + int _send_socket; +}; + +IPipe* CreatePipe() { + int fd[2]; + if (pipe(fd) < 0) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "pipe() failed: " << GetLastErrorString(); + } + + if (fcntl(fd[0], F_SETFD, FD_CLOEXEC) == -1) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "fcntl(F_SETFD, FD_CLOEXEC) failed: " << GetLastErrorString(); + } + + if (fcntl(fd[1], F_SETFD, FD_CLOEXEC) == -1) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "fcntl(F_SETFD, FD_CLOEXEC) failed: " << GetLastErrorString(); + } + + return new PosixPipe(fd[0], fd[1]); +} + +int ReadFromHandle(file_handle_type fd, void *data, size_t size, int *error) { + int result = read(fd, data, size); + if (error != nullptr) { + if (result >= 0) { + *error = ReadFileResult::SUCCESS; + } else { + if (errno == EINTR) { + *error = ReadFileResult::INTERRUPTED; + } else if (errno == EAGAIN) { + *error = ReadFileResult::AGAIN; + } else { + *error = ReadFileResult::OTHER_ERROR; + } + } + } + return result; +} + +bool ReadFile(const string &filename, string *content, int max_size) { + int fd = open(filename.c_str(), O_RDONLY); + if (fd == -1) return false; + bool result = ReadFrom(fd, content, max_size); + close(fd); + return result; +} + +bool ReadFile(const Path &path, std::string *content, int max_size) { + return ReadFile(path.AsNativePath(), content, max_size); +} + +bool ReadFile(const string &filename, void *data, size_t size) { + int fd = open(filename.c_str(), O_RDONLY); + if (fd == -1) return false; + bool result = ReadFrom(fd, data, size); + close(fd); + return result; +} + +bool ReadFile(const Path &filename, void *data, size_t size) { + return ReadFile(filename.AsNativePath(), data, size); +} + +bool WriteFile(const void *data, size_t size, const string &filename, + unsigned int perm) { + UnlinkPath(filename); // We don't care about the success of this. + int fd = open(filename.c_str(), O_CREAT | O_WRONLY | O_TRUNC, perm); + if (fd == -1) { + return false; + } + int result = write(fd, data, size); + if (close(fd)) { + return false; // Can fail on NFS. + } + return result == static_cast(size); +} + +bool WriteFile(const void *data, size_t size, const Path &path, + unsigned int perm) { + return WriteFile(data, size, path.AsNativePath(), perm); +} + +int WriteToStdOutErr(const void *data, size_t size, bool to_stdout) { + size_t r = fwrite(data, 1, size, to_stdout ? stdout : stderr); + return (r == size) ? WriteResult::SUCCESS + : ((errno == EPIPE) ? WriteResult::BROKEN_PIPE + : WriteResult::OTHER_ERROR); +} + +int RenameDirectory(const std::string &old_name, const std::string &new_name) { + if (rename(old_name.c_str(), new_name.c_str()) == 0) { + return kRenameDirectorySuccess; + } else { + if (errno == ENOTEMPTY || errno == EEXIST) { + return kRenameDirectoryFailureNotEmpty; + } else { + return kRenameDirectoryFailureOtherError; + } + } +} + +bool ReadDirectorySymlink(const blaze_util::Path &name, string *result) { + char buf[PATH_MAX + 1]; + int len = readlink(name.AsNativePath().c_str(), buf, PATH_MAX); + if (len < 0) { + return false; + } + + buf[len] = 0; + *result = buf; + return true; +} + +bool UnlinkPath(const string &file_path) { + return unlink(file_path.c_str()) == 0; +} + +bool UnlinkPath(const Path &file_path) { + return UnlinkPath(file_path.AsNativePath()); +} + +bool PathExists(const string& path) { + return access(path.c_str(), F_OK) == 0; +} + +bool PathExists(const Path &path) { return PathExists(path.AsNativePath()); } + +string MakeCanonical(const char *path) { + char *resolved_path = realpath(path, NULL); + if (resolved_path == NULL) { + return ""; + } else { + string ret = resolved_path; + free(resolved_path); + return ret; + } +} + +static bool CanAccess(const string &path, bool read, bool write, bool exec) { + int mode = 0; + if (read) { + mode |= R_OK; + } + if (write) { + mode |= W_OK; + } + if (exec) { + mode |= X_OK; + } + return access(path.c_str(), mode) == 0; +} + +bool CanReadFile(const std::string &path) { + return !IsDirectory(path) && CanAccess(path, true, false, false); +} + +bool CanReadFile(const Path &path) { + return CanReadFile(path.AsNativePath()); +} + +bool CanExecuteFile(const std::string &path) { + return !IsDirectory(path) && CanAccess(path, false, false, true); +} + +bool CanExecuteFile(const Path &path) { + return CanExecuteFile(path.AsNativePath()); +} + +bool CanAccessDirectory(const std::string &path) { + return IsDirectory(path) && CanAccess(path, true, true, true); +} + +bool CanAccessDirectory(const Path &path) { + return CanAccessDirectory(path.AsNativePath()); +} + +bool IsDirectory(const string& path) { + struct stat buf; + return stat(path.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode); +} + +bool IsDirectory(const Path &path) { return IsDirectory(path.AsNativePath()); } + +void SyncFile(const string& path) { + const char* file_path = path.c_str(); + int fd = open(file_path, O_RDONLY); + if (fd < 0) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "failed to open '" << file_path + << "' for syncing: " << GetLastErrorString(); + } + if (fsync(fd) < 0) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "failed to sync '" << file_path << "': " << GetLastErrorString(); + } + close(fd); +} + +void SyncFile(const Path &path) { SyncFile(path.AsNativePath()); } + +class PosixFileMtime : public IFileMtime { + public: + PosixFileMtime() + : near_future_(GetFuture(9)), + distant_future_({GetFuture(10), GetFuture(10)}) {} + + bool IsUntampered(const Path &path) override; + bool SetToNow(const Path &path) override; + bool SetToDistantFuture(const Path &path) override; + + private: + // 9 years in the future. + const time_t near_future_; + // 10 years in the future. + const struct utimbuf distant_future_; + + static bool Set(const Path &path, const struct utimbuf &mtime); + static time_t GetNow(); + static time_t GetFuture(unsigned int years); +}; + +bool PosixFileMtime::IsUntampered(const Path &path) { + struct stat buf; + if (stat(path.AsNativePath().c_str(), &buf)) { + return false; + } + + // Compare the mtime with `near_future_`, not with `GetNow()` or + // `distant_future_`. + // This way we don't need to call GetNow() every time we want to compare and + // we also don't need to worry about potentially unreliable time equality + // check (in case it uses floats or something crazy). + return S_ISDIR(buf.st_mode) || (buf.st_mtime > near_future_); +} + +bool PosixFileMtime::SetToNow(const Path &path) { + time_t now(GetNow()); + struct utimbuf times = {now, now}; + return Set(path, times); +} + +bool PosixFileMtime::SetToDistantFuture(const Path &path) { + return Set(path, distant_future_); +} + +bool PosixFileMtime::Set(const Path &path, const struct utimbuf &mtime) { + return utime(path.AsNativePath().c_str(), &mtime) == 0; +} + +time_t PosixFileMtime::GetNow() { + time_t result = time(NULL); + if (result == -1) { + BAZEL_DIE(blaze_exit_code::INTERNAL_ERROR) + << "time(NULL) failed: " << GetLastErrorString(); + } + return result; +} + +time_t PosixFileMtime::GetFuture(unsigned int years) { + return GetNow() + 3600 * 24 * 365 * years; +} + +IFileMtime *CreateFileMtime() { return new PosixFileMtime(); } + +// mkdir -p path. Returns true if the path was created or already exists and +// could +// be chmod-ed to exactly the given permissions. If final part of the path is a +// symlink, this ensures that the destination of the symlink has the desired +// permissions. It also checks that the directory or symlink is owned by us. +// On failure, this returns false and sets errno. +bool MakeDirectories(const string &path, unsigned int mode) { + return MakeDirectories(path, mode, true); +} + +bool MakeDirectories(const Path &path, unsigned int mode) { + return MakeDirectories(path.AsNativePath(), mode); +} + +string GetCwd() { + char cwdbuf[PATH_MAX]; + if (getcwd(cwdbuf, sizeof cwdbuf) == NULL) { + BAZEL_DIE(blaze_exit_code::INTERNAL_ERROR) + << "getcwd() failed: " << GetLastErrorString(); + } + return string(cwdbuf); +} + +bool ChangeDirectory(const string& path) { + return chdir(path.c_str()) == 0; +} + +void ForEachDirectoryEntry(const string &path, + DirectoryEntryConsumer *consume) { + DIR *dir; + struct dirent *ent; + + if ((dir = opendir(path.c_str())) == NULL) { + // This is not a directory or it cannot be opened. + return; + } + + while ((ent = readdir(dir)) != NULL) { + if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) { + continue; + } + + string filename(blaze_util::JoinPath(path, ent->d_name)); + bool is_directory; +// 'd_type' field isn't part of the POSIX spec. +#ifdef _DIRENT_HAVE_D_TYPE + if (ent->d_type != DT_UNKNOWN) { + is_directory = (ent->d_type == DT_DIR); + } else // NOLINT (the brace is on the next line) +#endif + { + struct stat buf; + if (lstat(filename.c_str(), &buf) == -1) { + BAZEL_DIE(blaze_exit_code::INTERNAL_ERROR) + << "stat failed for filename '" << filename + << "': " << GetLastErrorString(); + } + is_directory = S_ISDIR(buf.st_mode); + } + + consume->Consume(filename, is_directory); + } + + closedir(dir); + } + +} // namespace blaze_util diff --git a/third_party/java_tools/src/main/cpp/util/file_windows.cc b/third_party/java_tools/src/main/cpp/util/file_windows.cc new file mode 100644 index 000000000..533ccfbae --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/file_windows.cc @@ -0,0 +1,888 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include // isalpha +#include // wcslen +#include // iswalpha +#include + +#include // unique_ptr +#include +#include +#include + +#include "src/main/cpp/util/errors.h" +#include "src/main/cpp/util/exit_code.h" +#include "src/main/cpp/util/file.h" +#include "src/main/cpp/util/logging.h" +#include "src/main/cpp/util/path.h" +#include "src/main/cpp/util/path_platform.h" +#include "src/main/cpp/util/strings.h" +#include "src/main/native/windows/file.h" +#include "src/main/native/windows/util.h" + +namespace blaze_util { + +using bazel::windows::AutoHandle; +using bazel::windows::GetLongPath; +using bazel::windows::HasUncPrefix; +using std::basic_string; +using std::pair; +using std::string; +using std::unique_ptr; +using std::vector; +using std::wstring; + +static constexpr DWORD kAllShare = + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; + +// Returns true if `path` refers to a directory or (non-dangling) junction. +// `path` must be a normalized Windows path, with UNC prefix (and absolute) if +// necessary. +bool IsDirectoryW(const wstring& path); + +// Returns true the file or junction at `path` is successfully deleted. +// Returns false otherwise, or if `path` doesn't exist or is a directory. +// `path` must be a normalized Windows path, with UNC prefix (and absolute) if +// necessary. +static bool UnlinkPathW(const wstring& path); + +static bool CanReadFileW(const wstring& path); + +template +static bool IsPathSeparator(char_type ch) { + return ch == '/' || ch == '\\'; +} + +class WindowsPipe : public IPipe { + public: + WindowsPipe(const HANDLE& read_handle, const HANDLE& write_handle) + : _read_handle(read_handle), _write_handle(write_handle) {} + + WindowsPipe() = delete; + + bool Send(const void* buffer, int size) override { + DWORD actually_written = 0; + return ::WriteFile(_write_handle, buffer, size, &actually_written, + NULL) == TRUE; + } + + int Receive(void* buffer, int size, int* error) override { + DWORD actually_read = 0; + BOOL result = ::ReadFile(_read_handle, buffer, size, &actually_read, NULL); + if (error != nullptr) { + // TODO(laszlocsomor): handle the error mode that is errno=EINTR on Linux. + *error = result ? IPipe::SUCCESS : IPipe::OTHER_ERROR; + } + return result ? actually_read : -1; + } + + private: + AutoHandle _read_handle; + AutoHandle _write_handle; +}; + +IPipe* CreatePipe() { + // The pipe HANDLEs can be inherited. + SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE}; + HANDLE read_handle = INVALID_HANDLE_VALUE; + HANDLE write_handle = INVALID_HANDLE_VALUE; + if (!CreatePipe(&read_handle, &write_handle, &sa, 0)) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "CreatePipe failed: " << GetLastErrorString(); + } + return new WindowsPipe(read_handle, write_handle); +} + +class WindowsFileMtime : public IFileMtime { + public: + WindowsFileMtime() + : near_future_(GetFuture(9)), distant_future_(GetFuture(10)) {} + + bool IsUntampered(const Path& path) override; + bool SetToNow(const Path& path) override; + bool SetToDistantFuture(const Path& path) override; + + private: + // 9 years in the future. + const FILETIME near_future_; + // 10 years in the future. + const FILETIME distant_future_; + + static FILETIME GetNow(); + static FILETIME GetFuture(WORD years); + static bool Set(const Path& path, FILETIME time); +}; + +bool WindowsFileMtime::IsUntampered(const Path& path) { + if (path.IsEmpty() || path.IsNull()) { + return false; + } + + // Get attributes, to check if the file exists. (It may still be a dangling + // junction.) + DWORD attrs = GetFileAttributesW(path.AsNativePath().c_str()); + if (attrs == INVALID_FILE_ATTRIBUTES) { + return false; + } + + bool is_directory = attrs & FILE_ATTRIBUTE_DIRECTORY; + AutoHandle handle(CreateFileW( + /* lpFileName */ path.AsNativePath().c_str(), + /* dwDesiredAccess */ GENERIC_READ, + /* dwShareMode */ FILE_SHARE_READ, + /* lpSecurityAttributes */ NULL, + /* dwCreationDisposition */ OPEN_EXISTING, + /* dwFlagsAndAttributes */ + // Per CreateFile's documentation on MSDN, opening directories requires + // the FILE_FLAG_BACKUP_SEMANTICS flag. + is_directory ? FILE_FLAG_BACKUP_SEMANTICS : FILE_ATTRIBUTE_NORMAL, + /* hTemplateFile */ NULL)); + + if (!handle.IsValid()) { + return false; + } + + if (is_directory) { + return true; + } else { + BY_HANDLE_FILE_INFORMATION info; + if (!GetFileInformationByHandle(handle, &info)) { + return false; + } + + // Compare the mtime with `near_future_`, not with `GetNow()` or + // `distant_future_`. + // This way we don't need to call GetNow() every time we want to compare + // (and thus convert a SYSTEMTIME to FILETIME), and we also don't need to + // worry about potentially unreliable FILETIME equality check (in case it + // uses floats or something crazy). + return CompareFileTime(&near_future_, &info.ftLastWriteTime) == -1; + } +} + +bool WindowsFileMtime::SetToNow(const Path& path) { + return Set(path, GetNow()); +} + +bool WindowsFileMtime::SetToDistantFuture(const Path& path) { + return Set(path, distant_future_); +} + +bool WindowsFileMtime::Set(const Path& path, FILETIME time) { + AutoHandle handle(::CreateFileW( + /* lpFileName */ path.AsNativePath().c_str(), + /* dwDesiredAccess */ FILE_WRITE_ATTRIBUTES, + /* dwShareMode */ FILE_SHARE_READ, + /* lpSecurityAttributes */ NULL, + /* dwCreationDisposition */ OPEN_EXISTING, + /* dwFlagsAndAttributes */ + IsDirectoryW(path.AsNativePath()) + ? (FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS) + : FILE_ATTRIBUTE_NORMAL, + /* hTemplateFile */ NULL)); + if (!handle.IsValid()) { + return false; + } + return ::SetFileTime( + /* hFile */ handle, + /* lpCreationTime */ NULL, + /* lpLastAccessTime */ NULL, + /* lpLastWriteTime */ &time) == TRUE; +} + +FILETIME WindowsFileMtime::GetNow() { + FILETIME now; + GetSystemTimeAsFileTime(&now); + return now; +} + +FILETIME WindowsFileMtime::GetFuture(WORD years) { + FILETIME result; + GetSystemTimeAsFileTime(&result); + + // 1 year in FILETIME. + constexpr ULONGLONG kOneYear = 365ULL * 24 * 60 * 60 * 10'000'000; + + ULARGE_INTEGER result_value; + result_value.LowPart = result.dwLowDateTime; + result_value.HighPart = result.dwHighDateTime; + result_value.QuadPart += kOneYear * years; + result.dwLowDateTime = result_value.LowPart; + result.dwHighDateTime = result_value.HighPart; + return result; +} + +IFileMtime* CreateFileMtime() { return new WindowsFileMtime(); } + +static bool OpenFileForReading(const Path& path, HANDLE* result) { + *result = ::CreateFileW( + /* lpFileName */ path.AsNativePath().c_str(), + /* dwDesiredAccess */ GENERIC_READ, + /* dwShareMode */ kAllShare, + /* lpSecurityAttributes */ NULL, + /* dwCreationDisposition */ OPEN_EXISTING, + /* dwFlagsAndAttributes */ FILE_ATTRIBUTE_NORMAL, + /* hTemplateFile */ NULL); + return true; +} + +int ReadFromHandle(file_handle_type handle, void* data, size_t size, + int* error) { + DWORD actually_read = 0; + bool success = ::ReadFile(handle, data, size, &actually_read, NULL); + if (error != nullptr) { + // TODO(laszlocsomor): handle the error cases that are errno=EINTR and + // errno=EAGAIN on Linux. + *error = success ? ReadFileResult::SUCCESS : ReadFileResult::OTHER_ERROR; + } + return success ? actually_read : -1; +} + +bool ReadFile(const string& filename, string* content, int max_size) { + if (IsDevNull(filename.c_str())) { + // mimic read(2) behavior: we can always read 0 bytes from /dev/null + content->clear(); + return true; + } + return ReadFile(Path(filename), content, max_size); +} + +bool ReadFile(const Path& path, std::string* content, int max_size) { + if (path.IsEmpty()) { + return false; + } + // TODO(laszlocsomor): remove the following check; it won't allow opening NUL. + if (path.IsNull()) { + return true; + } + + HANDLE handle; + if (!OpenFileForReading(path, &handle)) { + return false; + } + + AutoHandle autohandle(handle); + if (!autohandle.IsValid()) { + return false; + } + content->clear(); + return ReadFrom(handle, content, max_size); +} + +bool ReadFile(const string& filename, void* data, size_t size) { + return ReadFile(Path(filename), data, size); +} + +bool ReadFile(const Path& path, void* data, size_t size) { + if (path.IsEmpty()) { + return false; + } + if (path.IsNull()) { + // mimic read(2) behavior: we can always read 0 bytes from /dev/null + return true; + } + HANDLE handle; + if (!OpenFileForReading(path, &handle)) { + return false; + } + + AutoHandle autohandle(handle); + if (!autohandle.IsValid()) { + return false; + } + return ReadFrom(handle, data, size); +} + +bool WriteFile(const void* data, size_t size, const string& filename, + unsigned int perm) { + if (IsDevNull(filename.c_str())) { + return true; // mimic write(2) behavior with /dev/null + } + return WriteFile(data, size, Path(filename), perm); +} + +bool WriteFile(const void* data, size_t size, const Path& path, + unsigned int perm) { + UnlinkPathW(path.AsNativePath()); // We don't care about the success of this. + AutoHandle handle(::CreateFileW( + /* lpFileName */ path.AsNativePath().c_str(), + /* dwDesiredAccess */ GENERIC_WRITE, + /* dwShareMode */ FILE_SHARE_READ, + /* lpSecurityAttributes */ NULL, + /* dwCreationDisposition */ CREATE_ALWAYS, + /* dwFlagsAndAttributes */ FILE_ATTRIBUTE_NORMAL, + /* hTemplateFile */ NULL)); + if (!handle.IsValid()) { + return false; + } + + // TODO(laszlocsomor): respect `perm` and set the file permissions accordingly + DWORD actually_written = 0; + ::WriteFile(handle, data, size, &actually_written, NULL); + return actually_written == size; +} + +int WriteToStdOutErr(const void* data, size_t size, bool to_stdout) { + DWORD written = 0; + HANDLE h = ::GetStdHandle(to_stdout ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE); + if (h == INVALID_HANDLE_VALUE) { + return WriteResult::OTHER_ERROR; + } + + if (::WriteFile(h, data, size, &written, NULL)) { + return (written == size) ? WriteResult::SUCCESS : WriteResult::OTHER_ERROR; + } else { + return (GetLastError() == ERROR_NO_DATA) ? WriteResult::BROKEN_PIPE + : WriteResult::OTHER_ERROR; + } +} + +int RenameDirectory(const std::string& old_name, const std::string& new_name) { + wstring wold_name; + string error; + if (!AsAbsoluteWindowsPath(old_name, &wold_name, &error)) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "RenameDirectory(" << old_name << ", " << new_name + << "): AsAbsoluteWindowsPath(" << old_name << ") failed: " << error; + return kRenameDirectoryFailureOtherError; + } + + wstring wnew_name; + if (!AsAbsoluteWindowsPath(new_name, &wnew_name, &error)) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "RenameDirectory(" << old_name << ", " << new_name + << "): AsAbsoluteWindowsPath(" << new_name << ") failed: " << error; + return kRenameDirectoryFailureOtherError; + } + + if (!::MoveFileExW(wold_name.c_str(), wnew_name.c_str(), + MOVEFILE_COPY_ALLOWED | MOVEFILE_FAIL_IF_NOT_TRACKABLE | + MOVEFILE_WRITE_THROUGH)) { + return GetLastError() == ERROR_ALREADY_EXISTS + ? kRenameDirectoryFailureNotEmpty + : kRenameDirectoryFailureOtherError; + } + return kRenameDirectorySuccess; +} + +static bool UnlinkPathW(const wstring& path) { + DWORD attrs = ::GetFileAttributesW(path.c_str()); + if (attrs == INVALID_FILE_ATTRIBUTES) { + // Path does not exist. + return false; + } + if (attrs & FILE_ATTRIBUTE_DIRECTORY) { + if (!(attrs & FILE_ATTRIBUTE_REPARSE_POINT)) { + // Path is a directory; unlink(2) also cannot remove directories. + return false; + } + // Otherwise it's a junction, remove using RemoveDirectoryW. + return ::RemoveDirectoryW(path.c_str()) == TRUE; + } else { + // Otherwise it's a file, remove using DeleteFileW. + return ::DeleteFileW(path.c_str()) == TRUE; + } +} + +bool UnlinkPath(const string& file_path) { + if (IsDevNull(file_path.c_str())) { + return false; + } + return UnlinkPath(Path(file_path)); +} + +bool UnlinkPath(const Path& path) { return UnlinkPathW(path.AsNativePath()); } + +static bool RealPath(const WCHAR* path, unique_ptr* result = nullptr) { + // Attempt opening the path, which may be anything -- a file, a directory, a + // symlink, even a dangling symlink is fine. + // Follow reparse points, getting us that much closer to the real path. + AutoHandle h(CreateFileW(path, 0, kAllShare, NULL, OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS, NULL)); + if (!h.IsValid()) { + // Path does not exist or it's a dangling junction/symlink. + return false; + } + + if (!result) { + // The caller is only interested in whether the file exists, they aren't + // interested in its real path. Since we just successfully opened the file + // we already know it exists. + // Also, GetFinalPathNameByHandleW is slow so avoid calling it if we can. + return true; + } + + // kMaxPath value: according to MSDN, maximum path length is 32767, and with + // an extra null terminator that's exactly 0x8000. + static constexpr size_t kMaxPath = 0x8000; + std::unique_ptr buf(new WCHAR[kMaxPath]); + DWORD res = GetFinalPathNameByHandleW(h, buf.get(), kMaxPath, 0); + if (res > 0 && res < kMaxPath) { + *result = std::move(buf); + return true; + } else { + return false; + } +} + +bool ReadDirectorySymlink(const blaze_util::Path& name, string* result) { + unique_ptr result_ptr; + if (!RealPath(name.AsNativePath().c_str(), &result_ptr)) { + return false; + } + *result = WstringToCstring(RemoveUncPrefixMaybe(result_ptr.get())); + return true; +} + +bool PathExists(const string& path) { return PathExists(Path(path)); } + +bool PathExists(const Path& path) { + if (path.IsEmpty()) { + return false; + } + if (path.IsNull()) { + return true; + } + return RealPath(path.AsNativePath().c_str(), nullptr); +} + +string MakeCanonical(const char* path) { + if (IsDevNull(path)) { + return "NUL"; + } + if (path == nullptr || path[0] == 0) { + return ""; + } + + std::wstring wpath; + string error; + if (!AsAbsoluteWindowsPath(path, &wpath, &error)) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "MakeCanonical(" << path + << "): AsAbsoluteWindowsPath failed: " << error; + } + + std::unique_ptr long_realpath; + if (!RealPath(wpath.c_str(), &long_realpath)) { + return ""; + } + + // Convert the path to lower-case. + size_t size = + wcslen(long_realpath.get()) - (HasUncPrefix(long_realpath.get()) ? 4 : 0); + unique_ptr lcase_realpath(new WCHAR[size + 1]); + const WCHAR* p_from = RemoveUncPrefixMaybe(long_realpath.get()); + WCHAR* p_to = lcase_realpath.get(); + while (size-- > 0) { + *p_to++ = towlower(*p_from++); + } + *p_to = 0; + return WstringToCstring(lcase_realpath.get()); +} + +static bool CanReadFileW(const wstring& path) { + AutoHandle handle(CreateFileW(path.c_str(), GENERIC_READ, kAllShare, NULL, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); + return handle.IsValid(); +} + +bool CanReadFile(const std::string& path) { + return CanReadFile(Path(path)); +} + +bool CanReadFile(const Path& path) { + return CanReadFileW(path.AsNativePath()); +} + +bool CanExecuteFile(const std::string& path) { + return CanExecuteFile(Path(path)); +} + +bool CanExecuteFile(const Path& path) { + std::wstring p = path.AsNativePath(); + if (p.size() < 4) { + return false; + } + std::wstring ext = p.substr(p.size() - 4); + return CanReadFileW(p) && + (ext == L".exe" || ext == L".com" || ext == L".cmd" || ext == L".bat"); +} + +bool CanAccessDirectory(const std::string& path) { + return CanAccessDirectory(Path(path)); +} + +bool CanAccessDirectory(const Path& path) { + DWORD attr = ::GetFileAttributesW(path.AsNativePath().c_str()); + if ((attr == INVALID_FILE_ATTRIBUTES) || !(attr & FILE_ATTRIBUTE_DIRECTORY)) { + // The path doesn't exist or is not a directory. + return false; + } + + // The only easy way to know if a directory is writable is by attempting to + // open a file for writing in it. + // File name with Thread ID avoids races among concurrent Bazel processes. + std::string dummy_name = "bazel_directory_access_test_"; + dummy_name += std::to_string(::GetCurrentThreadId()); + + Path dummy_path = path.GetRelative(dummy_name); + + // Attempt to open the dummy file for read/write access. + // If the file happens to exist, no big deal, we won't overwrite it thanks to + // OPEN_ALWAYS. + HANDLE handle = ::CreateFileW( + /* lpFileName */ dummy_path.AsNativePath().c_str(), + /* dwDesiredAccess */ GENERIC_WRITE | GENERIC_READ, + /* dwShareMode */ kAllShare, + /* lpSecurityAttributes */ NULL, + /* dwCreationDisposition */ OPEN_ALWAYS, + /* dwFlagsAndAttributes */ FILE_ATTRIBUTE_NORMAL, + /* hTemplateFile */ NULL); + DWORD err = GetLastError(); + if (handle == INVALID_HANDLE_VALUE) { + // We couldn't open the file, and not because the dummy file already exists. + // Consequently it is because `path` doesn't exist. + return false; + } + // The fact that we could open the file, regardless of it existing beforehand + // or not, means the directory also exists and we can read/write in it. + CloseHandle(handle); + if (err != ERROR_ALREADY_EXISTS) { + // The file didn't exist before, but due to OPEN_ALWAYS we created it just + // now, so do delete it. + ::DeleteFileW(dummy_path.AsNativePath().c_str()); + } // Otherwise the file existed before, leave it alone. + return true; +} + +bool IsDirectoryW(const wstring& path) { + // Attempt opening the path, which may be anything -- a file, a directory, a + // symlink, even a dangling symlink is fine. + // Follow reparse points in order to return false for dangling ones. + AutoHandle h(CreateFileW(path.c_str(), 0, kAllShare, NULL, OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS, NULL)); + BY_HANDLE_FILE_INFORMATION info; + return h.IsValid() && GetFileInformationByHandle(h, &info) && + info.dwFileAttributes != INVALID_FILE_ATTRIBUTES && + (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); +} + +bool IsDirectory(const string& path) { return IsDirectory(Path(path)); } + +bool IsDirectory(const Path& path) { + if (path.IsEmpty() || path.IsNull()) { + return false; + } + return IsDirectoryW(path.AsNativePath()); +} + +void SyncFile(const string& path) { + // No-op on Windows native; unsupported by Cygwin. + // fsync always fails on Cygwin with "Permission denied" for some reason. +} + +void SyncFile(const Path& path) {} + +bool MakeDirectoriesW(const wstring& path, unsigned int mode) { + if (path.empty()) { + return false; + } + std::wstring abs_path; + std::string error; + if (!AsAbsoluteWindowsPath(path, &abs_path, &error)) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "MakeDirectoriesW(" << blaze_util::WstringToCstring(path) + << "): " << error; + } + if (IsRootDirectoryW(abs_path) || IsDirectoryW(abs_path)) { + return true; + } + wstring parent = SplitPathW(abs_path).first; + if (parent.empty()) { + // Since `abs_path` is not a root directory, there should have been at least + // one directory above it. + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "MakeDirectoriesW(" << blaze_util::WstringToCstring(abs_path) + << ") could not find dirname: " << GetLastErrorString(); + } + return MakeDirectoriesW(parent, mode) && + ::CreateDirectoryW(abs_path.c_str(), NULL) == TRUE; +} + +bool MakeDirectories(const string& path, unsigned int mode) { + // TODO(laszlocsomor): respect `mode` to the extent that it's possible on + // Windows; it's currently ignored. + if (path.empty() || IsDevNull(path.c_str())) { + return false; + } + wstring wpath; + // According to MSDN, CreateDirectory's limit without the UNC prefix is + // 248 characters (so it could fit another filename before reaching MAX_PATH). + string error; + if (!AsAbsoluteWindowsPath(path, &wpath, &error)) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "MakeDirectories(" << path + << "): AsAbsoluteWindowsPath failed: " << error; + return false; + } + return MakeDirectoriesW(wpath, mode); +} + +bool MakeDirectories(const Path& path, unsigned int mode) { + return MakeDirectoriesW(path.AsNativePath(), mode); +} + +string CreateTempDir(const std::string &prefix) { + string result = prefix + blaze_util::ToString(GetCurrentProcessId()); + if (!blaze_util::MakeDirectories(result, 0777)) { + BAZEL_DIE(blaze_exit_code::INTERNAL_ERROR) + << "couldn't create '" << result + << "': " << blaze_util::GetLastErrorString(); + } + return result; +} + +static bool RemoveContents(wstring path) { + static const wstring kDot(L"."); + static const wstring kDotDot(L".."); + + if (path.find(L"\\\\?\\") != 0) { + path = wstring(L"\\\\?\\") + path; + } + if (path.back() != '\\') { + path.push_back('\\'); + } + + WIN32_FIND_DATAW metadata; + HANDLE handle = FindFirstFileW((path + L"*").c_str(), &metadata); + if (handle == INVALID_HANDLE_VALUE) { + return true; // directory doesn't exist + } + + bool result = true; + do { + wstring childname = metadata.cFileName; + if (kDot != childname && kDotDot != childname) { + wstring childpath = path + childname; + if ((metadata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { + // If this is not a junction, delete its contents recursively. + // Finally delete this directory/junction too. + if (((metadata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0 && + !RemoveContents(childpath)) || + !::RemoveDirectoryW(childpath.c_str())) { + result = false; + break; + } + } else { + if (!::DeleteFileW(childpath.c_str())) { + result = false; + break; + } + } + } + } while (FindNextFileW(handle, &metadata)); + FindClose(handle); + return result; +} + +static bool RemoveRecursivelyW(const wstring& path) { + DWORD attrs = ::GetFileAttributesW(path.c_str()); + if (attrs == INVALID_FILE_ATTRIBUTES) { + // Path does not exist. + return true; + } + if (attrs & FILE_ATTRIBUTE_DIRECTORY) { + if (!(attrs & FILE_ATTRIBUTE_REPARSE_POINT)) { + // Path is a directory; unlink(2) also cannot remove directories. + return RemoveContents(path) && ::RemoveDirectoryW(path.c_str()); + } + // Otherwise it's a junction, remove using RemoveDirectoryW. + return ::RemoveDirectoryW(path.c_str()) == TRUE; + } else { + // Otherwise it's a file, remove using DeleteFileW. + return ::DeleteFileW(path.c_str()) == TRUE; + } +} + +bool RemoveRecursively(const string& path) { + return RemoveRecursivelyW(Path(path).AsNativePath()); +} + +static inline void ToLowerW(WCHAR* p) { + while (*p) { + *p++ = towlower(*p); + } +} + +std::wstring GetCwdW() { + static constexpr size_t kBufSmall = MAX_PATH; + WCHAR buf[kBufSmall]; + DWORD len = GetCurrentDirectoryW(kBufSmall, buf); + if (len == 0) { + DWORD err = GetLastError(); + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "GetCurrentDirectoryW failed (error " << err << ")"; + } + + if (len < kBufSmall) { + ToLowerW(buf); + return std::wstring(buf); + } + + unique_ptr buf_big(new WCHAR[len]); + len = GetCurrentDirectoryW(len, buf_big.get()); + if (len == 0) { + DWORD err = GetLastError(); + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "GetCurrentDirectoryW failed (error " << err << ")"; + } + ToLowerW(buf_big.get()); + return std::wstring(buf_big.get()); +} + +string GetCwd() { + return WstringToCstring(RemoveUncPrefixMaybe(GetCwdW().c_str())); +} + +bool ChangeDirectory(const string& path) { + string spath; + string error; + if (!AsShortWindowsPath(path, &spath, &error)) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "ChangeDirectory(" << path << "): failed: " << error; + } + return ::SetCurrentDirectoryA(spath.c_str()) == TRUE; +} + +class DirectoryTreeWalkerW : public DirectoryEntryConsumerW { + public: + DirectoryTreeWalkerW(vector* files, + _ForEachDirectoryEntryW walk_entries) + : _files(files), _walk_entries(walk_entries) {} + + void Consume(const wstring& path, bool follow_directory) override { + if (follow_directory) { + Walk(path); + } else { + _files->push_back(path); + } + } + + void Walk(const wstring& path) { _walk_entries(path, this); } + + private: + vector* _files; + _ForEachDirectoryEntryW _walk_entries; +}; + +void ForEachDirectoryEntryW(const wstring& path, + DirectoryEntryConsumerW* consume) { + wstring wpath; + if (path.empty() || IsDevNull(path.c_str())) { + return; + } + string error; + if (!AsWindowsPath(path, &wpath, &error)) { + std::string err = GetLastErrorString(); + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "ForEachDirectoryEntryW(" << WstringToCstring(path) + << "): AsWindowsPath failed: " << err; + } + + static const wstring kUncPrefix(L"\\\\?\\"); + static const wstring kDot(L"."); + static const wstring kDotDot(L".."); + // Always add an UNC prefix to ensure we can work with long paths. + if (!HasUncPrefix(wpath.c_str())) { + wpath = kUncPrefix + wpath; + } + // Unconditionally add a trailing backslash. We know `wpath` has no trailing + // backslash because it comes from AsWindowsPath whose output is always + // normalized (see NormalizeWindowsPath). + wpath.append(L"\\"); + WIN32_FIND_DATAW metadata; + HANDLE handle = ::FindFirstFileW((wpath + L"*").c_str(), &metadata); + if (handle == INVALID_HANDLE_VALUE) { + return; // directory does not exist or is empty + } + + do { + if (kDot != metadata.cFileName && kDotDot != metadata.cFileName) { + wstring wname = wpath + metadata.cFileName; + wstring name(/* omit prefix */ 4 + wname.c_str()); + bool is_dir = (metadata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; + bool is_junc = + (metadata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0; + consume->Consume(name, is_dir && !is_junc); + } + } while (::FindNextFileW(handle, &metadata)); + ::FindClose(handle); +} + +void GetAllFilesUnderW(const wstring& path, vector* result) { + _GetAllFilesUnderW(path, result, &ForEachDirectoryEntryW); +} + +void _GetAllFilesUnderW(const wstring& path, vector* result, + _ForEachDirectoryEntryW walk_entries) { + DirectoryTreeWalkerW(result, walk_entries).Walk(path); +} + +void ForEachDirectoryEntry(const string &path, + DirectoryEntryConsumer *consume) { + wstring wpath; + if (path.empty() || IsDevNull(path.c_str())) { + return; + } + string error; + if (!AsWindowsPath(path, &wpath, &error)) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "ForEachDirectoryEntry(" << path + << "): AsWindowsPath failed: " << GetLastErrorString(); + } + + static const wstring kUncPrefix(L"\\\\?\\"); + static const wstring kDot(L"."); + static const wstring kDotDot(L".."); + // Always add an UNC prefix to ensure we can work with long paths. + if (!HasUncPrefix(wpath.c_str())) { + wpath = kUncPrefix + wpath; + } + // Unconditionally add a trailing backslash. We know `wpath` has no trailing + // backslash because it comes from AsWindowsPath whose output is always + // normalized (see NormalizeWindowsPath). + wpath.append(L"\\"); + WIN32_FIND_DATAW metadata; + HANDLE handle = ::FindFirstFileW((wpath + L"*").c_str(), &metadata); + if (handle == INVALID_HANDLE_VALUE) { + return; // directory does not exist or is empty + } + + do { + if (kDot != metadata.cFileName && kDotDot != metadata.cFileName) { + wstring wname = wpath + metadata.cFileName; + string name(WstringToCstring(/* omit prefix */ 4 + wname.c_str())); + bool is_dir = (metadata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; + bool is_junc = + (metadata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0; + consume->Consume(name, is_dir && !is_junc); + } + } while (::FindNextFileW(handle, &metadata)); + ::FindClose(handle); +} + +} // namespace blaze_util diff --git a/third_party/java_tools/src/main/cpp/util/logging.cc b/third_party/java_tools/src/main/cpp/util/logging.cc new file mode 100644 index 000000000..dace4cc71 --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/logging.cc @@ -0,0 +1,114 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// This file is based off the logging work by the protobuf team +#include "logging.h" + +#include +#include +#include +#include + +#include "third_party/java_tools/src/main/cpp/util/exit_code.h" +#include "third_party/java_tools/src/main/cpp/util/strings.h" + +namespace blaze_util { + +const char* LogLevelName(LogLevel level) { + static const char* level_names[] = {"INFO", "USER", "WARNING", "ERROR", + "FATAL"}; + BAZEL_CHECK(static_cast(level) < 5) + << "LogLevelName: level out of range, there are only 5 levels."; + return level_names[level]; +} + +namespace internal { + +static std::unique_ptr log_handler_(nullptr); + +LogMessage::LogMessage(LogLevel level, const std::string& filename, int line) + : level_(level), + filename_(filename), + line_(line), + exit_code_(blaze_exit_code::INTERNAL_ERROR) {} + +LogMessage::LogMessage(LogLevel level, const std::string& filename, int line, + int exit_code) + : level_(level), filename_(filename), line_(line), exit_code_(exit_code) {} + +#undef DECLARE_STREAM_OPERATOR +#define DECLARE_STREAM_OPERATOR(TYPE) \ + LogMessage& LogMessage::operator<<(TYPE value) { \ + message_ << value; \ + return *this; \ + } + +DECLARE_STREAM_OPERATOR(const std::string&) +DECLARE_STREAM_OPERATOR(const char*) +DECLARE_STREAM_OPERATOR(char) +DECLARE_STREAM_OPERATOR(bool) +DECLARE_STREAM_OPERATOR(short) +DECLARE_STREAM_OPERATOR(int) +DECLARE_STREAM_OPERATOR(unsigned int) +DECLARE_STREAM_OPERATOR(long) +DECLARE_STREAM_OPERATOR(unsigned long) +DECLARE_STREAM_OPERATOR(long long) +DECLARE_STREAM_OPERATOR(unsigned long long) +DECLARE_STREAM_OPERATOR(float) +DECLARE_STREAM_OPERATOR(double) +DECLARE_STREAM_OPERATOR(long double) +DECLARE_STREAM_OPERATOR(void*) + +#undef DECLARE_STREAM_OPERATOR + +void LogMessage::Finish() { + std::string message(message_.str()); + if (log_handler_ != nullptr) { + log_handler_->HandleMessage(level_, filename_, line_, message, exit_code_); + } else { + // If no custom handler was provided, never print INFO messages, + // but USER should always be printed, as should warnings and errors. + if (level_ == LOGLEVEL_USER) { + std::cerr << message << std::endl; + } else if (level_ > LOGLEVEL_USER) { + std::cerr << LogLevelName(level_) << ": " << message << std::endl; + } + + if (level_ == LOGLEVEL_FATAL) { + // Exit for fatal calls after handling the message. + std::exit(exit_code_); + } + } +} + +void LogFinisher::operator=(LogMessage& other) { other.Finish(); } + +} // namespace internal + +void SetLogHandler(std::unique_ptr new_handler) { + internal::log_handler_ = std::move(new_handler); +} + +void SetLoggingOutputStream(std::unique_ptr output_stream) { + if (internal::log_handler_ != nullptr) { + internal::log_handler_->SetOutputStream(std::move(output_stream)); + } +} +void SetLoggingOutputStreamToStderr() { + if (internal::log_handler_ != nullptr) { + internal::log_handler_->SetOutputStreamToStderr(); + } +} + +} // namespace blaze_util diff --git a/third_party/java_tools/src/main/cpp/util/logging.h b/third_party/java_tools/src/main/cpp/util/logging.h new file mode 100644 index 000000000..b2cd459d0 --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/logging.h @@ -0,0 +1,170 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef BAZEL_SRC_MAIN_CPP_LOGGING_H_ +#define BAZEL_SRC_MAIN_CPP_LOGGING_H_ + +#include +#include +#include + +// This file is based off the logging work by the protobuf team in +// stubs/logging.h, +// +// Users of this logging library should use BAZEL_LOG(level) << ""; format, +// and specify how they wish to handle the output of the log messages by +// creating a LogHandler to pass to SetLogHandler(). +namespace blaze_util { + +enum LogLevel { + LOGLEVEL_INFO, + LOGLEVEL_USER, + LOGLEVEL_WARNING, + LOGLEVEL_ERROR, + LOGLEVEL_FATAL, + +#ifdef NDEBUG + LOGLEVEL_DFATAL = LOGLEVEL_ERROR +#else + LOGLEVEL_DFATAL = LOGLEVEL_FATAL +#endif +}; + +// Returns a string representation of the log level. +const char* LogLevelName(LogLevel level); + +namespace internal { + +class LogFinisher; +class LogMessage { + public: + LogMessage(LogLevel level, const std::string& filename, int line); + LogMessage(LogLevel level, const std::string& filename, int line, + int exit_code); + + LogMessage& operator<<(const std::string& value); + LogMessage& operator<<(const char* value); + LogMessage& operator<<(char value); + LogMessage& operator<<(bool value); + LogMessage& operator<<(short value); + LogMessage& operator<<(int value); + LogMessage& operator<<(unsigned int value); + LogMessage& operator<<(long value); + LogMessage& operator<<(unsigned long value); + LogMessage& operator<<(long long value); + LogMessage& operator<<(unsigned long long value); + LogMessage& operator<<(float value); + LogMessage& operator<<(double value); + LogMessage& operator<<(long double value); + LogMessage& operator<<(void* value); + + private: + friend class LogFinisher; + void Finish(); + + const LogLevel level_; + const std::string& filename_; + const int line_; + // Only used for FATAL log messages. + const int exit_code_; + std::stringstream message_; +}; + +// Used to make the entire "LOG(BLAH) << etc." expression have a void return +// type and print a newline after each message. +class LogFinisher { + public: + void operator=(LogMessage& other); +}; + +template +bool IsOk(T status) { + return status.ok(); +} +template <> +inline bool IsOk(bool status) { + return status; +} + +} // namespace internal + +#define BAZEL_LOG(LEVEL) \ + ::blaze_util::internal::LogFinisher() = ::blaze_util::internal::LogMessage( \ + ::blaze_util::LOGLEVEL_##LEVEL, __FILE__, __LINE__) +#define BAZEL_LOG_IF(LEVEL, CONDITION) !(CONDITION) ? (void)0 : BAZEL_LOG(LEVEL) +#define BAZEL_DIE(EXIT_CODE) \ + ::blaze_util::internal::LogFinisher() = ::blaze_util::internal::LogMessage( \ + ::blaze_util::LOGLEVEL_FATAL, __FILE__, __LINE__, EXIT_CODE) + +#define BAZEL_CHECK(EXPRESSION) \ + BAZEL_LOG_IF(FATAL, !(EXPRESSION)) << "CHECK failed: " #EXPRESSION ": " +#define BAZEL_CHECK_OK(A) BAZEL_CHECK(::blaze_util::internal::IsOk(A)) +#define BAZEL_CHECK_EQ(A, B) BAZEL_CHECK((A) == (B)) +#define BAZEL_CHECK_NE(A, B) BAZEL_CHECK((A) != (B)) +#define BAZEL_CHECK_LT(A, B) BAZEL_CHECK((A) < (B)) +#define BAZEL_CHECK_LE(A, B) BAZEL_CHECK((A) <= (B)) +#define BAZEL_CHECK_GT(A, B) BAZEL_CHECK((A) > (B)) +#define BAZEL_CHECK_GE(A, B) BAZEL_CHECK((A) >= (B)) + +#ifdef NDEBUG + +#define BAZEL_DLOG(LEVEL) BAZEL_LOG_IF(LEVEL, false) + +#define BAZEL_DCHECK(EXPRESSION) \ + while (false) BAZEL_CHECK(EXPRESSION) +#define BAZEL_DCHECK_OK(E) BAZEL_DCHECK(::blaze::internal::IsOk(E)) +#define BAZEL_DCHECK_EQ(A, B) BAZEL_DCHECK((A) == (B)) +#define BAZEL_DCHECK_NE(A, B) BAZEL_DCHECK((A) != (B)) +#define BAZEL_DCHECK_LT(A, B) BAZEL_DCHECK((A) < (B)) +#define BAZEL_DCHECK_LE(A, B) BAZEL_DCHECK((A) <= (B)) +#define BAZEL_DCHECK_GT(A, B) BAZEL_DCHECK((A) > (B)) +#define BAZEL_DCHECK_GE(A, B) BAZEL_DCHECK((A) >= (B)) + +#else // NDEBUG + +#define BAZEL_DLOG BAZEL_LOG + +#define BAZEL_DCHECK BAZEL_CHECK +#define BAZEL_DCHECK_OK BAZEL_CHECK_OK +#define BAZEL_DCHECK_EQ BAZEL_CHECK_EQ +#define BAZEL_DCHECK_NE BAZEL_CHECK_NE +#define BAZEL_DCHECK_LT BAZEL_CHECK_LT +#define BAZEL_DCHECK_LE BAZEL_CHECK_LE +#define BAZEL_DCHECK_GT BAZEL_CHECK_GT +#define BAZEL_DCHECK_GE BAZEL_CHECK_GE + +#endif // !NDEBUG + +class LogHandler { + public: + virtual ~LogHandler() {} + virtual void HandleMessage(LogLevel level, const std::string& filename, + int line, const std::string& message, + int exit_code) = 0; + + virtual void SetOutputStream(std::unique_ptr output_stream) = 0; + virtual void SetOutputStreamToStderr() = 0; +}; + +// Sets the log handler that routes all log messages. +// SetLogHandler is not thread-safe. You should only call it +// at initialization time, and probably not from library code. +void SetLogHandler(std::unique_ptr new_handler); + +// Set the stream to which all log statements will be sent. +void SetLoggingOutputStream(std::unique_ptr output_stream); +void SetLoggingOutputStreamToStderr(); + +} // namespace blaze_util + +#endif // BAZEL_SRC_MAIN_CPP_LOGGING_H_ diff --git a/third_party/java_tools/src/main/cpp/util/md5.cc b/third_party/java_tools/src/main/cpp/util/md5.cc new file mode 100644 index 000000000..e8c7f8d13 --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/md5.cc @@ -0,0 +1,345 @@ +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm + */ + +/* + Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All + rights reserved. + + License to copy and use this software is granted provided that it + is identified as the "RSA Data Security, Inc. MD5 Message-Digest + Algorithm" in all material mentioning or referencing this software + or this function. + + License is also granted to make and use derivative works provided + that such works are identified as "derived from the RSA Data + Security, Inc. MD5 Message-Digest Algorithm" in all material + mentioning or referencing the derived work. + + RSA Data Security, Inc. makes no representations concerning either + the merchantability of this software or the suitability of this + software for any particular purpose. It is provided "as is" + without express or implied warranty of any kind. + + These notices must be retained in any copies of any part of this + documentation and/or software. +*/ + +#include "third_party/java_tools/src/main/cpp/util/md5.h" + +#include // for offsetof +#include // for memcpy + +#include + +#if !_STRING_ARCH_unaligned +#if defined(_LP64) || defined(_WIN64) +# define UNALIGNED_P(p) (reinterpret_cast(p) % \ + __alignof__(uint32_t) != 0) // NOLINT +# else +# define UNALIGNED_P(p) (reinterpret_cast(p) % \ + __alignof__(uint32_t) != 0) // NOLINT +# endif +#else +# define UNALIGNED_P(p) (0) +#endif + +namespace blaze_util { + +using std::string; + +static const unsigned int k8Bytes = 64; +static const unsigned int k8ByteMask = 63; + +static const unsigned char kPadding[64] = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +// Digit conversion. +static char hex_char[] = "0123456789abcdef"; + +// This is a templated function so that T can be either a char* +// or a string. This works because we use the [] operator to access +// individual characters at a time. +template +static void b2a_hex_t(const unsigned char* b, T a, int num) { + for (int i = 0; i < num; i++) { + a[i * 2 + 0] = hex_char[b[i] >> 4]; + a[i * 2 + 1] = hex_char[b[i] & 0xf]; + } +} + +// ---------------------------------------------------------------------- +// b2a_hex() +// Description: Binary-to-Ascii hex conversion. This converts +// 'num' bytes of binary to a 2*'num'-character hexadecimal representation +// Return value: 2*'num' characters of ascii text (via the 'to' argument) +// ---------------------------------------------------------------------- +static void b2a_hex(const unsigned char* from, string* to, int num) { + to->resize(num << 1); + b2a_hex_t(from, *to, num); +} + +Md5Digest::Md5Digest() { + Reset(); +} + +Md5Digest::Md5Digest(const Md5Digest& original) { + memcpy(state, original.state, sizeof(original.state)); + memcpy(count, original.count, sizeof(original.count)); + memcpy(ctx_buffer, original.ctx_buffer, original.ctx_buffer_len); + ctx_buffer_len = original.ctx_buffer_len; +} + +void Md5Digest::Reset() { + count[0] = count[1] = 0; + ctx_buffer_len = 0; + // Load magic initialization constants. + state[0] = 0x67452301; + state[1] = 0xefcdab89; + state[2] = 0x98badcfe; + state[3] = 0x10325476; +} + +void Md5Digest::Update(const void *buf, unsigned int length) { + const unsigned char *input = reinterpret_cast(buf); + unsigned int buffer_space_len; + + buffer_space_len = k8Bytes - ctx_buffer_len; + + // Transform as many times as possible. + if (length >= buffer_space_len) { + if (buffer_space_len != 0 && ctx_buffer_len != 0) { + // Copy more bytes to fill the complete buffer + memcpy(ctx_buffer + ctx_buffer_len, input, buffer_space_len); + Transform(ctx_buffer, k8Bytes); + input += buffer_space_len; + length -= buffer_space_len; + ctx_buffer_len = 0; + } + + if (UNALIGNED_P(input)) { + while (length >= k8Bytes) { + memcpy(ctx_buffer, input, k8Bytes); + Transform(ctx_buffer, k8Bytes); + input += k8Bytes; + length -= k8Bytes; + } + } else if (length >= k8Bytes) { + Transform(input, length & ~k8ByteMask); + input += length & ~k8ByteMask; + length &= k8ByteMask; + } + } + + // Buffer remaining input + memcpy(ctx_buffer + ctx_buffer_len, input, length); + ctx_buffer_len += length; +} + +void Md5Digest::Finish(unsigned char digest[16]) { + count[0] += ctx_buffer_len; + if (count[0] < ctx_buffer_len) { + ++count[1]; + } + + /* Put the 64-bit file length in *bits* at the end of the buffer. */ + unsigned int size = (ctx_buffer_len < 56 ? 64 : 128); + uint32_t words[2] = { count[0] << 3, (count[1] << 3) | (count[0] >> 29) }; + memcpy(ctx_buffer + size - 8, words, 8); + + memcpy(ctx_buffer + ctx_buffer_len, kPadding, size - 8 - ctx_buffer_len); + + Transform(ctx_buffer, size); + + uint32_t* r = reinterpret_cast(digest); + r[0] = state[0]; + r[1] = state[1]; + r[2] = state[2]; + r[3] = state[3]; +} + +void Md5Digest::Transform( + const unsigned char* buffer, unsigned int len) { + // Constants for transform routine. +#define S11 7 +#define S12 12 +#define S13 17 +#define S14 22 +#define S21 5 +#define S22 9 +#define S23 14 +#define S24 20 +#define S31 4 +#define S32 11 +#define S33 16 +#define S34 23 +#define S41 6 +#define S42 10 +#define S43 15 +#define S44 21 + + // F, G, H and I are basic MD5 functions. +/* These are the four functions used in the four steps of the MD5 algorithm + and defined in the RFC 1321. The first function is a little bit optimized + (as found in Colin Plumbs public domain implementation). */ +/* #define F(b, c, d) ((b & c) | (~b & d)) */ +#define F(x, y, z) (z ^ (x & (y ^ z))) +#define G(x, y, z) F (z, x, y) +#define H(x, y, z) ((x) ^ (y) ^ (z)) +#define I(x, y, z) ((y) ^ ((x) | (~z))) + + // ROTATE_LEFT rotates x left n bits. +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) + + // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. + // Rotation is separate from addition to prevent recomputation. +#define FF(a, b, c, d, s, ac) { \ + (a) += F((b), (c), (d)) + ((*x_pos++ = *cur_word++)) + \ + static_cast(ac); \ + (a) = ROTATE_LEFT((a), (s)); \ + (a) += (b); \ + } + +#define GG(a, b, c, d, x, s, ac) { \ + (a) += G((b), (c), (d)) + (x) + static_cast(ac); \ + (a) = ROTATE_LEFT((a), (s)); \ + (a) += (b); \ + } +#define HH(a, b, c, d, x, s, ac) { \ + (a) += H((b), (c), (d)) + (x) + static_cast(ac); \ + (a) = ROTATE_LEFT((a), (s)); \ + (a) += (b); \ + } +#define II(a, b, c, d, x, s, ac) { \ + (a) += I((b), (c), (d)) + (x) + static_cast(ac); \ + (a) = ROTATE_LEFT((a), (s)); \ + (a) += (b); \ + } + + count[0] += len; + if (count[0] < len) { + ++count[1]; + } + + uint32_t a = state[0]; + uint32_t b = state[1]; + uint32_t c = state[2]; + uint32_t d = state[3]; + uint32_t x[16]; + + const uint32_t *cur_word = reinterpret_cast(buffer); + const uint32_t *end_word = cur_word + (len / sizeof(uint32_t)); + + while (cur_word < end_word) { + uint32_t *x_pos = x; + uint32_t prev_a = a; + uint32_t prev_b = b; + uint32_t prev_c = c; + uint32_t prev_d = d; + + // Round 1 + FF(a, b, c, d, S11, 0xd76aa478); // 1 + FF(d, a, b, c, S12, 0xe8c7b756); // 2 + FF(c, d, a, b, S13, 0x242070db); // 3 + FF(b, c, d, a, S14, 0xc1bdceee); // 4 + FF(a, b, c, d, S11, 0xf57c0faf); // 5 + FF(d, a, b, c, S12, 0x4787c62a); // 6 + FF(c, d, a, b, S13, 0xa8304613); // 7 + FF(b, c, d, a, S14, 0xfd469501); // 8 + FF(a, b, c, d, S11, 0x698098d8); // 9 + FF(d, a, b, c, S12, 0x8b44f7af); // 10 + FF(c, d, a, b, S13, 0xffff5bb1); // 11 + FF(b, c, d, a, S14, 0x895cd7be); // 12 + FF(a, b, c, d, S11, 0x6b901122); // 13 + FF(d, a, b, c, S12, 0xfd987193); // 14 + FF(c, d, a, b, S13, 0xa679438e); // 15 + FF(b, c, d, a, S14, 0x49b40821); // 16 + + // Round 2 + GG(a, b, c, d, x[ 1], S21, 0xf61e2562); // 17 + GG(d, a, b, c, x[ 6], S22, 0xc040b340); // 18 + GG(c, d, a, b, x[11], S23, 0x265e5a51); // 19 + GG(b, c, d, a, x[ 0], S24, 0xe9b6c7aa); // 20 + GG(a, b, c, d, x[ 5], S21, 0xd62f105d); // 21 + GG(d, a, b, c, x[10], S22, 0x2441453); // 22 + GG(c, d, a, b, x[15], S23, 0xd8a1e681); // 23 + GG(b, c, d, a, x[ 4], S24, 0xe7d3fbc8); // 24 + GG(a, b, c, d, x[ 9], S21, 0x21e1cde6); // 25 + GG(d, a, b, c, x[14], S22, 0xc33707d6); // 26 + GG(c, d, a, b, x[ 3], S23, 0xf4d50d87); // 27 + GG(b, c, d, a, x[ 8], S24, 0x455a14ed); // 28 + GG(a, b, c, d, x[13], S21, 0xa9e3e905); // 29 + GG(d, a, b, c, x[ 2], S22, 0xfcefa3f8); // 30 + GG(c, d, a, b, x[ 7], S23, 0x676f02d9); // 31 + GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); // 32 + + // Round 3 + HH(a, b, c, d, x[ 5], S31, 0xfffa3942); // 33 + HH(d, a, b, c, x[ 8], S32, 0x8771f681); // 34 + HH(c, d, a, b, x[11], S33, 0x6d9d6122); // 35 + HH(b, c, d, a, x[14], S34, 0xfde5380c); // 36 + HH(a, b, c, d, x[ 1], S31, 0xa4beea44); // 37 + HH(d, a, b, c, x[ 4], S32, 0x4bdecfa9); // 38 + HH(c, d, a, b, x[ 7], S33, 0xf6bb4b60); // 39 + HH(b, c, d, a, x[10], S34, 0xbebfbc70); // 40 + HH(a, b, c, d, x[13], S31, 0x289b7ec6); // 41 + HH(d, a, b, c, x[ 0], S32, 0xeaa127fa); // 42 + HH(c, d, a, b, x[ 3], S33, 0xd4ef3085); // 43 + HH(b, c, d, a, x[ 6], S34, 0x4881d05); // 44 + HH(a, b, c, d, x[ 9], S31, 0xd9d4d039); // 45 + HH(d, a, b, c, x[12], S32, 0xe6db99e5); // 46 + HH(c, d, a, b, x[15], S33, 0x1fa27cf8); // 47 + HH(b, c, d, a, x[ 2], S34, 0xc4ac5665); // 48 + + // Round 4 + II(a, b, c, d, x[ 0], S41, 0xf4292244); // 49 + II(d, a, b, c, x[ 7], S42, 0x432aff97); // 50 + II(c, d, a, b, x[14], S43, 0xab9423a7); // 51 + II(b, c, d, a, x[ 5], S44, 0xfc93a039); // 52 + II(a, b, c, d, x[12], S41, 0x655b59c3); // 53 + II(d, a, b, c, x[ 3], S42, 0x8f0ccc92); // 54 + II(c, d, a, b, x[10], S43, 0xffeff47d); // 55 + II(b, c, d, a, x[ 1], S44, 0x85845dd1); // 56 + II(a, b, c, d, x[ 8], S41, 0x6fa87e4f); // 57 + II(d, a, b, c, x[15], S42, 0xfe2ce6e0); // 58 + II(c, d, a, b, x[ 6], S43, 0xa3014314); // 59 + II(b, c, d, a, x[13], S44, 0x4e0811a1); // 60 + II(a, b, c, d, x[ 4], S41, 0xf7537e82); // 61 + II(d, a, b, c, x[11], S42, 0xbd3af235); // 62 + II(c, d, a, b, x[ 2], S43, 0x2ad7d2bb); // 63 + II(b, c, d, a, x[ 9], S44, 0xeb86d391); // 64 + + a += prev_a; + b += prev_b; + c += prev_c; + d += prev_d; + } + + state[0] = a; + state[1] = b; + state[2] = c; + state[3] = d; +} + +string Md5Digest::String() const { + string result; + b2a_hex(reinterpret_cast(state), &result, 16); + return result; +} + +} // namespace blaze_util diff --git a/third_party/java_tools/src/main/cpp/util/md5.h b/third_party/java_tools/src/main/cpp/util/md5.h new file mode 100644 index 000000000..8f8f3aff2 --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/md5.h @@ -0,0 +1,70 @@ +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// Provides a fast MD5 implementation. +// +// This implementation saves us from linking huge OpenSSL library. + +#ifndef BAZEL_SRC_MAIN_CPP_UTIL_MD5_H_ +#define BAZEL_SRC_MAIN_CPP_UTIL_MD5_H_ + +#include + +#if defined(_MSC_VER) && !defined(__alignof__) +#define __alignof__ __alignof +#endif // _MSC_VER && !__alignof__ + +namespace blaze_util { + +// The Context class performs the actual MD5 +// computation. It works incrementally and can be fed a single byte at +// a time if desired. +class Md5Digest { + public: + Md5Digest(); + + Md5Digest(const Md5Digest& original); + + // the MD5 digest is always 128 bits = 16 bytes + static constexpr int kDigestLength = 16; + + // Resets the context so that it can be used to calculate another + // MD5 digest. The context is in the same state as if it had just + // been constructed. It is unnecessary to call Reset on + // a newly created context. + void Reset(); + + // Add length bytes of buf to the MD5 + // digest. + void Update(const void *buf, unsigned int length); + + // Retrieve the computed MD5 digest as a 16 byte array. + void Finish(unsigned char* digest); + + // Produces a hexadecimal string representation of this digest in the form: + // [0-9a-f]{32} + std::string String() const; + + private: + void Transform(const unsigned char* buffer, unsigned int len); + + private: + unsigned int state[4]; // state (ABCD) + unsigned int count[2]; // number of bits, modulo 2^64 (lsb first) + unsigned char ctx_buffer[128]; // input buffer + unsigned int ctx_buffer_len; +}; + +} // namespace blaze_util + +#endif // BAZEL_SRC_MAIN_CPP_UTIL_MD5_H_ diff --git a/third_party/java_tools/src/main/cpp/util/numbers.cc b/third_party/java_tools/src/main/cpp/util/numbers.cc new file mode 100644 index 000000000..e8fee3825 --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/numbers.cc @@ -0,0 +1,214 @@ +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "third_party/java_tools/src/main/cpp/util/numbers.h" + +#include // errno, ERANGE +#include +#include +#include +#include +#include + +#include "third_party/java_tools/src/main/cpp/util/strings.h" + +namespace blaze_util { + +using std::string; + +static const int32_t kint32min = static_cast(~0x7FFFFFFF); +static const int32_t kint32max = static_cast(0x7FFFFFFF); + +// Represents integer values of digits. +// Uses 36 to indicate an invalid character since we support +// bases up to 36. +static const int8_t kAsciiToInt[256] = { + 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, // 16 36s. + 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, + 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 36, 36, 36, 36, 36, 36, 36, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, + 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, + 36, 36, 36, 36, 36, 36, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, + 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, + 36, 36, 36, 36, 36, + 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, + 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, + 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, + 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, + 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, + 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, + 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, + 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36 }; + +// Parse the sign. +inline bool safe_parse_sign(const char** rest, /*inout*/ + bool* negative_ptr /*output*/) { + const char* start = *rest; + const char* end = start + strlen(start); + + // Consume whitespace. + while (start < end && ascii_isspace(start[0])) { + ++start; + } + while (start < end && ascii_isspace(end[-1])) { + --end; + } + if (start >= end) { + return false; + } + + // Consume sign. + *negative_ptr = (start[0] == '-'); + if (*negative_ptr || start[0] == '+') { + ++start; + if (start >= end) { + return false; + } + } + + *rest = start; + return true; +} + +// Consume digits. +// +// The classic loop: +// +// for each digit +// value = value * base + digit +// value *= sign +// +// The classic loop needs overflow checking. It also fails on the most +// negative integer, -2147483648 in 32-bit two's complement representation. +// +// My improved loop: +// +// if (!negative) +// for each digit +// value = value * base +// value = value + digit +// else +// for each digit +// value = value * base +// value = value - digit +// +// Overflow checking becomes simple. + +inline bool safe_parse_positive_int(const char *text, int* value_p) { + int value = 0; + const int vmax = std::numeric_limits::max(); + static_assert(vmax > 0, ""); + const int vmax_over_base = vmax / 10; + const char* start = text; + const char* end = start + strlen(text); + // loop over digits + for (; start < end; ++start) { + unsigned char c = static_cast(start[0]); + int digit = kAsciiToInt[c]; + if (digit >= 10) { + *value_p = value; + return false; + } + if (value > vmax_over_base) { + *value_p = vmax; + return false; + } + value *= 10; + if (value > vmax - digit) { + *value_p = vmax; + return false; + } + value += digit; + } + *value_p = value; + return true; +} + +inline bool safe_parse_negative_int(const char *text, int* value_p) { + int value = 0; + const int vmin = std::numeric_limits::min(); + static_assert(vmin < 0, ""); + int vmin_over_base = vmin / 10; + // 2003 c++ standard [expr.mul] + // "... the sign of the remainder is implementation-defined." + // Although (vmin/base)*base + vmin%base is always vmin. + // 2011 c++ standard tightens the spec but we cannot rely on it. + if (vmin % 10 > 0) { + vmin_over_base += 1; + } + const char* start = text; + const char* end = start + strlen(text); + // loop over digits + for (; start < end; ++start) { + unsigned char c = static_cast(start[0]); + int digit = kAsciiToInt[c]; + if (digit >= 10) { + *value_p = value; + return false; + } + if (value < vmin_over_base) { + *value_p = vmin; + return false; + } + value *= 10; + if (value < vmin + digit) { + *value_p = vmin; + return false; + } + value -= digit; + } + *value_p = value; + return true; +} + +bool safe_strto32(const string &text, int *value_p) { + *value_p = 0; + const char* rest = text.c_str(); + bool negative; + if (!safe_parse_sign(&rest, &negative)) { + return false; + } + if (!negative) { + return safe_parse_positive_int(rest, value_p); + } else { + return safe_parse_negative_int(rest, value_p); + } +} + +int32_t strto32(const char *str, char **endptr, int base) { + if (sizeof(int32_t) == sizeof(long)) { // NOLINT + return static_cast(strtol(str, endptr, base)); // NOLINT + } + const int saved_errno = errno; + errno = 0; + const long result = strtol(str, endptr, base); // NOLINT + if (errno == ERANGE && result == LONG_MIN) { + return kint32min; + } else if (errno == ERANGE && result == LONG_MAX) { + return kint32max; + } else if (errno == 0 && result < kint32min) { + errno = ERANGE; + return kint32min; + } else if (errno == 0 && result > kint32max) { + errno = ERANGE; + return kint32max; + } + if (errno == 0) + errno = saved_errno; + return static_cast(result); +} + +} // namespace blaze_util diff --git a/third_party/java_tools/src/main/cpp/util/numbers.h b/third_party/java_tools/src/main/cpp/util/numbers.h new file mode 100644 index 000000000..f1ef6f799 --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/numbers.h @@ -0,0 +1,27 @@ +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef BAZEL_SRC_MAIN_CPP_UTIL_NUMBERS_H_ +#define BAZEL_SRC_MAIN_CPP_UTIL_NUMBERS_H_ + +#include + +namespace blaze_util { + +bool safe_strto32(const std::string &text, int *value); + +int32_t strto32(const char *str, char **endptr, int base); + +} // namespace blaze_util + +#endif // BAZEL_SRC_MAIN_CPP_UTIL_NUMBERS_H_ diff --git a/third_party/java_tools/src/main/cpp/util/path.cc b/third_party/java_tools/src/main/cpp/util/path.cc new file mode 100644 index 000000000..fb920f4a7 --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/path.cc @@ -0,0 +1,51 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "path.h" + +#include "file_platform.h" +#include "path_platform.h" + +namespace blaze_util { + +std::string Dirname(const std::string &path) { return SplitPath(path).first; } + +std::string Basename(const std::string &path) { return SplitPath(path).second; } + +std::string JoinPath(const std::string &path1, const std::string &path2) { + if (path1.empty()) { + // "" + "/bar" + return path2; + } + + if (path1[path1.size() - 1] == '/') { + if (path2.find('/') == 0) { + // foo/ + /bar + return path1 + path2.substr(1); + } else { + // foo/ + bar + return path1 + path2; + } + } else { + if (path2.find('/') == 0) { + // foo + /bar + return path1 + path2; + } else { + // foo + bar + return path1 + "/" + path2; + } + } +} + +} // namespace blaze_util diff --git a/third_party/java_tools/src/main/cpp/util/path.h b/third_party/java_tools/src/main/cpp/util/path.h new file mode 100644 index 000000000..4a9b0e17c --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/path.h @@ -0,0 +1,36 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef BAZEL_SRC_MAIN_CPP_UTIL_PATH_H_ +#define BAZEL_SRC_MAIN_CPP_UTIL_PATH_H_ + +#include + +#include "path_platform.h" + +namespace blaze_util { + +// Returns the part of the path before the final "/". If there is a single +// leading "/" in the path, the result will be the leading "/". If there is +// no "/" in the path, the result is the empty prefix of the input (i.e., ""). +std::string Dirname(const std::string &path); + +// Returns the part of the path after the final "/". If there is no +// "/" in the path, the result is the same as the input. +std::string Basename(const std::string &path); + +std::string JoinPath(const std::string &path1, const std::string &path2); + +} // namespace blaze_util + +#endif // BAZEL_SRC_MAIN_CPP_UTIL_PATH_H_ diff --git a/third_party/java_tools/src/main/cpp/util/path_platform.h b/third_party/java_tools/src/main/cpp/util/path_platform.h new file mode 100644 index 000000000..bfa448b41 --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/path_platform.h @@ -0,0 +1,206 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef BAZEL_SRC_MAIN_CPP_UTIL_PATH_PLATFORM_H_ +#define BAZEL_SRC_MAIN_CPP_UTIL_PATH_PLATFORM_H_ + +#include + +namespace blaze_util { + +// Platform-native, absolute, normalized path. +// It can be converted to a printable path (for error messages) or to a native +// path (for API calls). +class Path { + public: + Path() {} + explicit Path(const std::string &path); + bool operator==(const Path &o) const { return path_ == o.path_; } + bool operator!=(const Path &o) const { return path_ != o.path_; } + bool operator<(const Path &o) const { return path_ < o.path_; } + bool IsEmpty() const { return path_.empty(); } + bool IsNull() const; + bool Contains(const char c) const; + bool Contains(const std::string &s) const; + Path GetRelative(const std::string &r) const; + + // Returns the canonical form (like realpath(1)) of this path. + // All symlinks in the path are resolved. + // If canonicalization fails, returns an empty Path. + Path Canonicalize() const; + + Path GetParent() const; + + // Returns a printable string representing this path. + // Only use when printing user messages, do not pass to filesystem API + // functions. + std::string AsPrintablePath() const; + + // Returns a string representation of this path that's safe to pass on the + // command line as a JVM argument. + std::string AsJvmArgument() const; + + // Returns a string representation of this path, safe to pass to the Bazel + // server. + std::string AsCommandLineArgument() const; + +#if defined(_WIN32) || defined(__CYGWIN__) + // Returns a platform-native, absolute, normalized path. + // Use this to pass paths to filesystem API functions. + const std::wstring AsNativePath() const { return path_; } +#else + // Returns a platform-native, absolute, normalized path. + // Use this to pass paths to filesystem API functions. + const std::string AsNativePath() const { return path_; } +#endif + + private: +#if defined(_WIN32) || defined(__CYGWIN__) + explicit Path(const std::wstring &wpath) : path_(wpath) {} + std::wstring path_; +#else + std::string path_; +#endif +}; + +// Convert a path from Bazel internal form to underlying OS form. +// On Unixes this is an identity operation. +// On Windows, Bazel internal form is cygwin path, and underlying OS form +// is Windows path. +std::string ConvertPath(const std::string &path); + +// Converts `path` to a string that's safe to pass as path in a JVM flag. +// See https://github.com/bazelbuild/bazel/issues/2576 +std::string PathAsJvmFlag(const std::string &path); + +// Compares two absolute paths. Necessary because the same path can have +// multiple different names under msys2: "C:\foo\bar" or "C:/foo/bar" +// (Windows-style) and "/c/foo/bar" (msys2 style). Returns if the paths are +// equal. +bool CompareAbsolutePaths(const std::string &a, const std::string &b); + +// Split a path to dirname and basename parts. +std::pair SplitPath(const std::string &path); + +bool IsDevNull(const char *path); + +// Returns true if `path` is the root directory or a Windows drive root. +bool IsRootDirectory(const std::string &path); +bool IsRootDirectory(const Path &path); + +// Returns true if `path` is absolute. +bool IsAbsolute(const std::string &path); + +// Returns the given path in absolute form. Does not change paths that are +// already absolute. +// +// If called from working directory "/bar": +// MakeAbsolute("foo") --> "/bar/foo" +// MakeAbsolute("/foo") ---> "/foo" +// MakeAbsolute("C:/foo") ---> "C:/foo" +std::string MakeAbsolute(const std::string &path); + +// Returns the given path in absolute form, taking into account a +// possible starting environment variable, so that we can accept +// standard path variables like %USERPROFILE% or ${BAZEL}. For +// simplicity, we implement only those two forms, not $BAZEL. +// +// MakeAbsolute("foo") in wd "/bar" --> "/bar/foo" +// MakeAbsoluteAndResolveEnvvars("%USERPROFILE%/foo") --> +// "C:\Users\bazel-user\foo" +// MakeAbsoluteAndResolveEnvvars("${BAZEL}/foo") --> "/opt/bazel/foo" +std::string MakeAbsoluteAndResolveEnvvars(const std::string &path); + +// TODO(bazel-team) consider changing the path(_platform) header split to be a +// path.h and path_windows.h split, which would make it clearer what functions +// are included by an import statement. The downside to this gain in clarity +// is that this would add more complexity to the implementation file(s)? of +// path.h, which would have to have the platform-specific implementations. +#if defined(_WIN32) || defined(__CYGWIN__) +bool IsDevNull(const wchar_t *path); + +bool IsAbsolute(const std::wstring &path); + +const wchar_t *RemoveUncPrefixMaybe(const wchar_t *ptr); + +void AddUncPrefixMaybe(std::wstring *path); + +std::pair SplitPathW(const std::wstring &path); + +bool IsRootDirectoryW(const std::wstring &path); + +// Converts 'path' to Windows style. +// +// 'path' is absolute or relative or current-drive-relative (e.g. +// "\foo"), possibly non-normalized, possibly using slash as separator. If it +// starts with the UNC prefix, the function won't process it further, just +// copies it to 'result'. +// +// 'result' equals 'path' if 'path' started with the UNC prefix, otherwise +// 'result' is normalized, using backslash as separator. +// +// Encoding: there is no assumption about encoding, 'path' is read as ASCII +// (Latin-1) and 'result' uses the same encoding. +bool AsWindowsPath(const std::string &path, std::string *result, + std::string *error); + +// Converts 'path' to Windows style. +// +// Same as the other AsWindowsPath methods, but 'path' is encoded as multibyte +// and 'result' is widechar. (MSDN does not clarify what multibyte means. The +// function uses blaze_util::WstringToCstring.) +bool AsWindowsPath(const std::string &path, std::wstring *result, + std::string *error); + +// Converts 'path' to Windows style. +// +// Same as the other AsWindowsPath methods, but 'path' and 'result' are +// widechar. +bool AsWindowsPath(const std::wstring &path, std::wstring *result, + std::string *error); + +// Converts 'path' to absolute, Windows-style path. +// +// Same as AsWindowsPath, but 'result' is always absolute and always has a UNC +// prefix. +bool AsAbsoluteWindowsPath(const std::string &path, std::wstring *result, + std::string *error); + +// Converts 'path' to absolute, Windows-style path. +// +// Same as AsWindowsPath, but 'result' is always absolute and always has a UNC +// prefix. +bool AsAbsoluteWindowsPath(const std::wstring &path, std::wstring *result, + std::string *error); + +// Converts 'path' to absolute, shortened, Windows-style path. +// +// Same as `AsWindowsPath`, but 'result' is always absolute, lowercase, +// 8dot3-style shortened path, without trailing backslash and without UNC +// prefix. +// +// Works even for non-existent paths (and non-existent drives), shortening the +// existing segments and leaving the rest unshortened. +bool AsShortWindowsPath(const std::string &path, std::string *result, + std::string *error); +bool AsShortWindowsPath(const std::wstring &path, std::wstring *result, + std::string *error); + +#else + +std::string TestOnly_NormalizeAbsPath(const std::string &s); + +#endif // defined(_WIN32) || defined(__CYGWIN__) +} // namespace blaze_util + +#endif // BAZEL_SRC_MAIN_CPP_UTIL_PATH_PLATFORM_H_ diff --git a/third_party/java_tools/src/main/cpp/util/path_posix.cc b/third_party/java_tools/src/main/cpp/util/path_posix.cc new file mode 100644 index 000000000..5863f98e7 --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/path_posix.cc @@ -0,0 +1,168 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "path_platform.h" + +#include // PATH_MAX + +#include // getenv +#include // strncmp +#include // access, open, close, fsync +#include "third_party/java_tools/src/main/cpp/util/errors.h" +#include "third_party/java_tools/src/main/cpp/util/exit_code.h" +#include "third_party/java_tools/src/main/cpp/util/file_platform.h" +#include "third_party/java_tools/src/main/cpp/util/logging.h" +#include "third_party/java_tools/src/main/cpp/util/path.h" + +namespace blaze_util { + +std::string ConvertPath(const std::string &path) { return path; } + +std::string PathAsJvmFlag(const std::string &path) { return path; } + +bool CompareAbsolutePaths(const std::string &a, const std::string &b) { + return a == b; +} + +std::pair SplitPath(const std::string &path) { + size_t pos = path.rfind('/'); + + // Handle the case with no '/' in 'path'. + if (pos == std::string::npos) return std::make_pair("", path); + + // Handle the case with a single leading '/' in 'path'. + if (pos == 0) + return std::make_pair(std::string(path, 0, 1), std::string(path, 1)); + + return std::make_pair(std::string(path, 0, pos), std::string(path, pos + 1)); +} + +bool IsDevNull(const char *path) { + return path != NULL && *path != 0 && strncmp("/dev/null\0", path, 10) == 0; +} + +bool IsRootDirectory(const std::string &path) { + return path.size() == 1 && path[0] == '/'; +} + +bool IsRootDirectory(const Path &path) { + return IsRootDirectory(path.AsNativePath()); +} + +bool IsAbsolute(const std::string &path) { + return !path.empty() && path[0] == '/'; +} + +std::string MakeAbsolute(const std::string &path) { + if (blaze_util::IsAbsolute(path) || path.empty()) { + return path; + } + + return JoinPath(blaze_util::GetCwd(), path); +} + +std::string ResolveEnvvars(const std::string &path) { + std::string result = path; + size_t start = 0; + while ((start = result.find("${", start)) != std::string::npos) { + // Just match to the next } + size_t end = result.find('}', start + 1); + if (end == std::string::npos) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "ResolveEnvvars(" << path << "): incomplete variable at position " + << start; + } + // Extract the variable name + const std::string name = result.substr(start + 2, end - start - 2); + // Get the value from the environment + const char *c_value = getenv(name.c_str()); + const std::string value = std::string(c_value ? c_value : ""); + result.replace(start, end - start + 1, value); + start += value.length(); + } + return result; +} + +std::string MakeAbsoluteAndResolveEnvvars(const std::string &path) { + return MakeAbsolute(ResolveEnvvars(path)); +} + +static std::string NormalizeAbsPath(const std::string &p) { + if (p.empty() || p[0] != '/') { + return ""; + } + typedef std::string::size_type index; + std::vector > segments; + for (index s = 0; s < p.size();) { + index e = p.find_first_of('/', s); + if (e == std::string::npos) { + e = p.size(); + } + if (e > s) { + if (p.compare(s, e - s, "..") == 0) { + if (!segments.empty()) { + segments.pop_back(); + } + } else if (p.compare(s, e - s, ".") != 0) { + segments.push_back(std::make_pair(s, e - s)); + } + } + s = e + 1; + } + if (segments.empty()) { + return "/"; + } else { + std::stringstream r; + for (const auto &s : segments) { + r << "/" << p.substr(s.first, s.second); + } + if (p[p.size() - 1] == '/') { + r << "/"; + } + return r.str(); + } +} + +std::string TestOnly_NormalizeAbsPath(const std::string &s) { + return NormalizeAbsPath(s); +} + +Path::Path(const std::string &path) + : path_(NormalizeAbsPath(MakeAbsolute(path))) {} + +bool Path::IsNull() const { return path_ == "/dev/null"; } + +bool Path::Contains(const char c) const { + return path_.find_first_of(c) != std::string::npos; +} + +bool Path::Contains(const std::string &s) const { + return path_.find(s) != std::string::npos; +} + +Path Path::GetRelative(const std::string &r) const { + return Path(JoinPath(path_, r)); +} + +Path Path::Canonicalize() const { return Path(MakeCanonical(path_.c_str())); } + +Path Path::GetParent() const { return Path(SplitPath(path_).first); } + +std::string Path::AsPrintablePath() const { return path_; } + +std::string Path::AsJvmArgument() const { return path_; } + +std::string Path::AsCommandLineArgument() const { return path_; } + +} // namespace blaze_util diff --git a/third_party/java_tools/src/main/cpp/util/path_windows.cc b/third_party/java_tools/src/main/cpp/util/path_windows.cc new file mode 100644 index 000000000..cc4ff1b0c --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/path_windows.cc @@ -0,0 +1,531 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif + +#include "src/main/cpp/util/path_platform.h" + +#include +#include // wcslen +#include + +#include +#include // unique_ptr +#include +#include + +#include "src/main/cpp/util/errors.h" +#include "src/main/cpp/util/exit_code.h" +#include "src/main/cpp/util/file_platform.h" +#include "src/main/cpp/util/logging.h" +#include "src/main/cpp/util/strings.h" +#include "src/main/native/windows/file.h" + +namespace blaze_util { + +using bazel::windows::HasUncPrefix; + +static char GetCurrentDrive(); + +template +struct CharTraits { + static bool IsAlpha(char_type ch); +}; + +template <> +struct CharTraits { + static bool IsAlpha(char ch) { return isalpha(ch); } +}; + +template <> +struct CharTraits { + static bool IsAlpha(wchar_t ch) { return iswalpha(ch); } +}; + +template +static bool IsPathSeparator(char_type ch) { + return ch == '/' || ch == '\\'; +} + +template +static bool HasDriveSpecifierPrefix(const char_type* ch) { + return CharTraits::IsAlpha(ch[0]) && ch[1] == ':'; +} + +std::string ConvertPath(const std::string& path) { + // The path may not be Windows-style and may not be normalized, so convert it. + std::string converted_path; + std::string error; + if (!AsWindowsPath(path, &converted_path, &error)) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "ConvertPath(" << path << "): AsWindowsPath failed: " << error; + } + std::transform(converted_path.begin(), converted_path.end(), + converted_path.begin(), ::tolower); + return converted_path; +} + +std::string MakeAbsolute(const std::string& path) { + // The path may not be Windows-style and may not be normalized, so convert it. + std::wstring wpath; + std::string error; + if (!AsAbsoluteWindowsPath(path, &wpath, &error)) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "MakeAbsolute(" << path + << "): AsAbsoluteWindowsPath failed: " << error; + } + std::transform(wpath.begin(), wpath.end(), wpath.begin(), ::towlower); + return WstringToCstring(RemoveUncPrefixMaybe(wpath.c_str())); +} + +std::string MakeAbsoluteAndResolveEnvvars(const std::string& path) { + // Get the size of the expanded string, so we know how big of a buffer to + // provide. The returned size includes the null terminator. + std::unique_ptr resolved(new CHAR[MAX_PATH]); + DWORD size = + ::ExpandEnvironmentStrings(path.c_str(), resolved.get(), MAX_PATH); + if (size == 0) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "MakeAbsoluteAndResolveWindowsEnvvars(" << path + << "): ExpandEnvironmentStrings failed: " << GetLastErrorString(); + } else if (size > MAX_PATH) { + // Try again with a buffer bigger than MAX_PATH. + resolved.reset(new CHAR[size]); + DWORD second_size = + ::ExpandEnvironmentStrings(path.c_str(), resolved.get(), size); + if (second_size == 0) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "MakeAbsoluteAndResolveWindowsEnvvars(" << path + << "): ExpandEnvironmentStrings failed with second buffer: " + << GetLastErrorString(); + } + assert(second_size <= size); + } + return MakeAbsolute(std::string(resolved.get())); +} + +bool CompareAbsolutePaths(const std::string& a, const std::string& b) { + return ConvertPath(a) == ConvertPath(b); +} + +std::string PathAsJvmFlag(const std::string& path) { + std::string cpath; + std::string error; + if (!AsWindowsPath(path, &cpath, &error)) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "PathAsJvmFlag(" << path << "): AsWindowsPath failed: " << error; + } + // Convert forward slashes and backslashes to double (escaped) backslashes, so + // they are safe to pass on the command line to the JVM and the JVM won't + // misinterpret them. + // See https://github.com/bazelbuild/bazel/issues/2576 and + // https://github.com/bazelbuild/bazel/issues/6098 + size_t separators = 0; + for (const auto& c : cpath) { + if (c == '/' || c == '\\') { + separators++; + } + } + // In the result we replace each '/' and '\' with "\\", i.e. the total size + // *increases* by `separators`. + // Create a string of that size, filled with zeroes. + std::string result(/* count */ cpath.size() + separators, '\0'); + std::string::size_type i = 0; + for (const auto& c : cpath) { + if (c == '/' || c == '\\') { + result[i++] = '\\'; + result[i++] = '\\'; + } else { + result[i++] = c; + } + } + return result; +} + +void AddUncPrefixMaybe(std::wstring* path) { + if (path->size() >= MAX_PATH && !HasUncPrefix(path->c_str())) { + *path = std::wstring(L"\\\\?\\") + *path; + } +} + +const wchar_t* RemoveUncPrefixMaybe(const wchar_t* ptr) { + return ptr + (HasUncPrefix(ptr) ? 4 : 0); +} + +// Checks if the path is absolute and/or is a root path. +// +// If `must_be_root` is true, then in addition to being absolute, the path must +// also be just the root part, no other components, e.g. "c:\" is both absolute +// and root, but "c:\foo" is just absolute. +template +static bool IsRootOrAbsolute(const std::basic_string& path, + bool must_be_root) { + // An absolute path is one that starts with "/", "\", "c:/", "c:\", + // "\\?\c:\", or rarely "\??\c:\" or "\\.\c:\". + // + // It is unclear whether the UNC prefix is just "\\?\" or is "\??\" also + // valid (in some cases it seems to be, though MSDN doesn't mention it). + return + // path is (or starts with) "/" or "\" + ((must_be_root ? path.size() == 1 : !path.empty()) && + IsPathSeparator(path[0])) || + // path is (or starts with) "c:/" or "c:\" or similar + ((must_be_root ? path.size() == 3 : path.size() >= 3) && + HasDriveSpecifierPrefix(path.c_str()) && IsPathSeparator(path[2])) || + // path is (or starts with) "\\?\c:\" or "\??\c:\" or similar + ((must_be_root ? path.size() == 7 : path.size() >= 7) && + HasUncPrefix(path.c_str()) && + HasDriveSpecifierPrefix(path.c_str() + 4) && IsPathSeparator(path[6])); +} + +template +static std::pair, std::basic_string > +SplitPathImpl(const std::basic_string& path) { + if (path.empty()) { + return std::make_pair(std::basic_string(), + std::basic_string()); + } + + size_t pos = path.size() - 1; + for (auto it = path.crbegin(); it != path.crend(); ++it, --pos) { + if (IsPathSeparator(*it)) { + if ((pos == 2 || pos == 6) && + IsRootOrAbsolute(path.substr(0, pos + 1), /* must_be_root */ true)) { + // Windows path, top-level directory, e.g. "c:\foo", + // result is ("c:\", "foo"). + // Or UNC path, top-level directory, e.g. "\\?\c:\foo" + // result is ("\\?\c:\", "foo"). + return std::make_pair( + // Include the "/" or "\" in the drive specifier. + path.substr(0, pos + 1), path.substr(pos + 1)); + } else { + // Windows path (neither top-level nor drive root), Unix path, or + // relative path. + return std::make_pair( + // If the only "/" is the leading one, then that shall be the first + // pair element, otherwise the substring up to the rightmost "/". + pos == 0 ? path.substr(0, 1) : path.substr(0, pos), + // If the rightmost "/" is the tail, then the second pair element + // should be empty. + pos == path.size() - 1 ? std::basic_string() + : path.substr(pos + 1)); + } + } + } + // Handle the case with no '/' or '\' in `path`. + return std::make_pair(std::basic_string(), path); +} + +std::pair SplitPath(const std::string& path) { + return SplitPathImpl(path); +} + +std::pair SplitPathW(const std::wstring& path) { + return SplitPathImpl(path); +} + +void assignNUL(std::string* s) { s->assign("NUL"); } + +void assignNUL(std::wstring* s) { s->assign(L"NUL"); } + +template +static bool AsWindowsPathImpl(const std::basic_string& path, + std::basic_string* result, + std::string* error) { + if (path.empty()) { + result->clear(); + return true; + } + if (IsDevNull(path.c_str())) { + assignNUL(result); + return true; + } + if (HasUncPrefix(path.c_str())) { + // Path has "\\?\" prefix --> assume it's already Windows-style. + *result = path.c_str(); + return true; + } + if (IsPathSeparator(path[0]) && path.size() > 1 && IsPathSeparator(path[1])) { + // Unsupported path: "\\" or "\\server\path", or some degenerate form of + // these, such as "//foo". + if (error) { + *error = "network paths are unsupported"; + } + return false; + } + if (HasDriveSpecifierPrefix(path.c_str()) && + (path.size() < 3 || !IsPathSeparator(path[2]))) { + // Unsupported path: "c:" or "c:foo" + if (error) { + *error = "working-directory relative paths are unsupported"; + } + return false; + } + + std::basic_string mutable_path = path; + if (path[0] == '/') { + if (error) { + *error = "Unix-style paths are unsupported"; + } + return false; + } + + if (path[0] == '\\') { + // This is an absolute Windows path on the current drive, e.g. "\foo\bar". + std::basic_string drive(1, GetCurrentDrive()); + drive.push_back(':'); + mutable_path = drive + path; + } // otherwise this is a relative path, or absolute Windows path. + + *result = bazel::windows::Normalize(mutable_path); + return true; +} + +bool AsWindowsPath(const std::string& path, std::string* result, + std::string* error) { + return AsWindowsPathImpl(path, result, error); +} + +bool AsWindowsPath(const std::string& path, std::wstring* result, + std::string* error) { + return AsWindowsPathImpl(CstringToWstring(path), result, error); +} + +bool AsWindowsPath(const std::wstring& path, std::wstring* result, + std::string* error) { + return AsWindowsPathImpl(path, result, error); +} + +static bool AsAbsoluteWindowsPathImpl(const std::wstring& path, + std::wstring* result, + std::string* error) { + if (path.empty()) { + result->clear(); + return true; + } + if (IsDevNull(path.c_str())) { + result->assign(L"NUL"); + return true; + } + if (!AsWindowsPath(path, result, error)) { + return false; + } + if (!IsRootOrAbsolute(*result, /* must_be_root */ false)) { + if (result->empty() || (result->size() == 1 && (*result)[0] == '.')) { + *result = GetCwdW(); + } else { + *result = GetCwdW() + L"\\" + *result; + } + } + + *result = std::wstring(L"\\\\?\\") + bazel::windows::Normalize(*result); + return true; +} + +bool AsAbsoluteWindowsPath(const std::string& path, std::wstring* result, + std::string* error) { + return AsAbsoluteWindowsPathImpl(CstringToWstring(path), result, error); +} + +bool AsAbsoluteWindowsPath(const std::wstring& path, std::wstring* result, + std::string* error) { + return AsAbsoluteWindowsPathImpl(path, result, error); +} + +bool AsShortWindowsPath(const std::string& path, std::string* result, + std::string* error) { + std::wstring wresult; + if (AsShortWindowsPath(CstringToWstring(path), &wresult, error)) { + *result = WstringToCstring(wresult); + return true; + } else { + return false; + } +} + +bool AsShortWindowsPath(const std::wstring& path, std::wstring* result, + std::string* error) { + if (IsDevNull(path.c_str())) { + *result = L"NUL"; + return true; + } + + result->clear(); + std::wstring wpath; + std::wstring wsuffix; + if (!AsAbsoluteWindowsPath(path, &wpath, error)) { + return false; + } + DWORD size = ::GetShortPathNameW(wpath.c_str(), nullptr, 0); + if (size == 0) { + // GetShortPathNameW can fail if `wpath` does not exist. This is expected + // when we are about to create a file at that path, so instead of failing, + // walk up in the path until we find a prefix that exists and can be + // shortened, or is a root directory. Save the non-existent tail in + // `wsuffix`, we'll add it back later. + std::vector segments; + while (size == 0 && !IsRootDirectoryW(wpath)) { + std::pair split = SplitPathW(wpath); + wpath = split.first; + segments.push_back(split.second); + size = ::GetShortPathNameW(wpath.c_str(), nullptr, 0); + } + + // Join all segments. + std::wostringstream builder; + bool first = true; + for (auto it = segments.crbegin(); it != segments.crend(); ++it) { + if (!first || !IsRootDirectoryW(wpath)) { + builder << L'\\' << *it; + } else { + builder << *it; + } + first = false; + } + wsuffix = builder.str(); + } + + std::wstring wresult; + if (IsRootDirectoryW(wpath)) { + // Strip the UNC prefix from `wpath`, and the leading "\" from `wsuffix`. + wresult = std::wstring(RemoveUncPrefixMaybe(wpath.c_str())) + wsuffix; + } else { + std::unique_ptr wshort( + new WCHAR[size]); // size includes null-terminator + if (size - 1 != ::GetShortPathNameW(wpath.c_str(), wshort.get(), size)) { + if (error) { + std::string last_error = GetLastErrorString(); + std::stringstream msg; + msg << "AsShortWindowsPath(" << WstringToCstring(path) + << "): GetShortPathNameW(" << WstringToCstring(wpath) + << ") failed: " << last_error; + *error = msg.str(); + } + return false; + } + // GetShortPathNameW may preserve the UNC prefix in the result, so strip it. + wresult = std::wstring(RemoveUncPrefixMaybe(wshort.get())) + wsuffix; + } + + std::transform(wresult.begin(), wresult.end(), wresult.begin(), towlower); + *result = wresult; + return true; +} + +bool IsDevNull(const char* path) { + return path != NULL && *path != 0 && + (strncmp("/dev/null\0", path, 10) == 0 || + ((path[0] == 'N' || path[0] == 'n') && + (path[1] == 'U' || path[1] == 'u') && + (path[2] == 'L' || path[2] == 'l') && path[3] == 0)); +} + +bool IsDevNull(const wchar_t* path) { + return path != NULL && *path != 0 && + (wcsncmp(L"/dev/null\0", path, 10) == 0 || + ((path[0] == L'N' || path[0] == L'n') && + (path[1] == L'U' || path[1] == L'u') && + (path[2] == L'L' || path[2] == L'l') && path[3] == 0)); +} + +bool IsRootDirectory(const std::string& path) { + return IsRootOrAbsolute(path, true); +} + +bool IsRootDirectory(const Path& path) { + return IsRootOrAbsolute(path.AsNativePath(), true); +} + +bool IsAbsolute(const std::string& path) { + return IsRootOrAbsolute(path, false); +} + +bool IsAbsolute(const std::wstring& path) { + return IsRootOrAbsolute(path, false); +} + +bool IsRootDirectoryW(const std::wstring& path) { + return IsRootOrAbsolute(path, true); +} + +static char GetCurrentDrive() { + std::wstring cwd = GetCwdW(); + wchar_t wdrive = RemoveUncPrefixMaybe(cwd.c_str())[0]; + wchar_t offset = wdrive >= L'A' && wdrive <= L'Z' ? L'A' : L'a'; + return 'a' + wdrive - offset; +} + +Path::Path(const std::string& path) { + if (path.empty()) { + return; + } else if (IsDevNull(path.c_str())) { + path_ = L"NUL"; + } else { + std::string error; + if (!AsAbsoluteWindowsPath(path, &path_, &error)) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "Path::Path(" << path + << "): AsAbsoluteWindowsPath failed: " << error; + } + } +} + +Path Path::GetRelative(const std::string& r) const { + if (r.empty()) { + return *this; + } else if (IsDevNull(r.c_str())) { + return Path(L"NUL"); + } else if (IsAbsolute(r)) { + return Path(r); + } else { + std::string error; + std::wstring new_path; + if (!AsAbsoluteWindowsPath(path_ + L"\\" + CstringToWstring(r), &new_path, + &error)) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "Path::GetRelative failed: " << error; + } + return Path(new_path); + } +} + +Path Path::Canonicalize() const { + return Path(MakeCanonical(WstringToCstring(path_).c_str())); +} + +Path Path::GetParent() const { return Path(SplitPathW(path_).first); } + +bool Path::IsNull() const { return path_ == L"NUL"; } + +bool Path::Contains(const char c) const { + return path_.find_first_of(c) != std::wstring::npos; +} + +bool Path::Contains(const std::string& s) const { + return path_.find(CstringToWstring(s)) != std::wstring::npos; +} + +std::string Path::AsPrintablePath() const { + return WstringToCstring(RemoveUncPrefixMaybe(path_.c_str())); +} + +std::string Path::AsJvmArgument() const { + return PathAsJvmFlag(AsPrintablePath()); +} + +std::string Path::AsCommandLineArgument() const { return AsPrintablePath(); } + +} // namespace blaze_util diff --git a/third_party/java_tools/src/main/cpp/util/port.cc b/third_party/java_tools/src/main/cpp/util/port.cc new file mode 100644 index 000000000..f59bcfce7 --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/port.cc @@ -0,0 +1,38 @@ +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "port.h" + +#ifdef __linux +#include +#include +#endif // __linux + +namespace blaze_util { + +#ifdef __linux + +int sys_ioprio_set(int which, int who, int ioprio) { + return syscall(SYS_ioprio_set, which, who, ioprio); +} + +#else // Not Linux. + +int sys_ioprio_set(int which, int who, int ioprio) { + return 0; +} + +#endif // __linux + +} // namespace blaze_util diff --git a/third_party/java_tools/src/main/cpp/util/port.h b/third_party/java_tools/src/main/cpp/util/port.h new file mode 100644 index 000000000..efd0b9511 --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/port.h @@ -0,0 +1,146 @@ +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef BAZEL_SRC_MAIN_CPP_UTIL_PORT_H_ +#define BAZEL_SRC_MAIN_CPP_UTIL_PORT_H_ + +#include // For size_t on Linux, Darwin + +#include // For size_t on Windows + +// GCC-specific features +#if (defined(COMPILER_GCC3) || defined(__APPLE__)) && !defined(SWIG) + +// +// Tell the compiler to do printf format string checking if the +// compiler supports it; see the 'format' attribute in +// . +// +// N.B.: As the GCC manual states, "[s]ince non-static C++ methods +// have an implicit 'this' argument, the arguments of such methods +// should be counted from two, not one." +// +#define PRINTF_ATTRIBUTE(string_index, first_to_check) \ + __attribute__((__format__ \ + (__printf__, string_index, first_to_check))) + +#define ATTRIBUTE_UNUSED __attribute__ ((__unused__)) + +#else // Not GCC + +#define PRINTF_ATTRIBUTE(string_index, first_to_check) +#define ATTRIBUTE_UNUSED + +#endif // GCC + +// HAVE_ATTRIBUTE +// +// A function-like feature checking macro that is a wrapper around +// `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a +// nonzero constant integer if the attribute is supported or 0 if not. +// +// It evaluates to zero if `__has_attribute` is not defined by the compiler. +// +// GCC: https://gcc.gnu.org/gcc-5/changes.html +// Clang: https://clang.llvm.org/docs/LanguageExtensions.html +#ifdef __has_attribute +#define HAVE_ATTRIBUTE(x) __has_attribute(x) +#else +#define HAVE_ATTRIBUTE(x) (0) +#endif + +// ATTRIBUTE_NORETURN +// +// Tells the compiler that a given function never returns. +#if defined(SWIG) +#define ATTRIBUTE_NORETURN +#elif HAVE_ATTRIBUTE(noreturn) || (defined(__GNUC__) && !defined(__clang__)) +#define ATTRIBUTE_NORETURN __attribute__((noreturn)) +#else +#define ATTRIBUTE_NORETURN +#endif + + +// Linux I/O priorities support is available only in later versions of glibc. +// Therefore, we include some of the needed definitions here. May need to +// be removed once we switch to a new version of glibc +// (As of 10/24/08 it is unclear when glibc support will become available.) +enum IOPriorityClass { + // No I/O priority value has yet been set. The kernel may assign I/O + // priority based on the process nice value. + IOPRIO_CLASS_NONE, + + // Real-time, highest priority. Given first access to the disk at + // every opportunity. Use with care: one such process can STARVE + // THE ENTIRE SYSTEM. Has 8 priority levels (0-7). + IOPRIO_CLASS_RT, + + // Best-effort, default for any process. Has 8 priority levels (0-7). + IOPRIO_CLASS_BE, + + // Idle, lowest priority. Processes running at this level only get + // I/O time when no one else needs the disk, and MAY BECOME + // STARVED if higher priority processes are constantly accessing + // the disk. With the "anticipatory" I/O scheduler, mapped to + // IOPRIO_CLASS_BE, level 3. + IOPRIO_CLASS_IDLE, +}; + +enum { + IOPRIO_WHO_PROCESS = 1, + IOPRIO_WHO_PGRP, + IOPRIO_WHO_USER, +}; + +#ifndef IOPRIO_CLASS_SHIFT +#define IOPRIO_CLASS_SHIFT 13 +#endif + +#ifndef IOPRIO_PRIO_VALUE +#define IOPRIO_PRIO_VALUE(class, data) (((class) << IOPRIO_CLASS_SHIFT) | data) +#endif + +namespace blaze_util { + +int sys_ioprio_set(int which, int who, int ioprio); + +} // namespace blaze_util + +// The arraysize(arr) macro returns the # of elements in an array arr. +// The expression is a compile-time constant, and therefore can be +// used in defining new arrays, for example. If you use arraysize on +// a pointer by mistake, you will get a compile-time error. + +// This template function declaration is used in defining arraysize. +// Note that the function doesn't need an implementation, as we only +// use its type. +template +char (&ArraySizeHelper(T (&array)[N]))[N]; + +// That gcc wants both of these prototypes seems mysterious. VC, for +// its part, can't decide which to use (another mystery). Matching of +// template overloads: the final frontier. +template +char (&ArraySizeHelper(const T (&array)[N]))[N]; + +#define arraysize(array) (sizeof(ArraySizeHelper(array))) + +#ifdef _WIN32 +// TODO(laszlocsomor) 2016-11-28: move pid_t usage out of global_variables.h and +// wherever else it appears. Find some way to not have to declare a pid_t here, +// either by making PID handling platform-independent or some other idea; remove +// the following typedef afterwards. +typedef int pid_t; +#endif // _WIN32 + +#endif // BAZEL_SRC_MAIN_CPP_UTIL_PORT_H_ diff --git a/third_party/java_tools/src/main/cpp/util/strings.cc b/third_party/java_tools/src/main/cpp/util/strings.cc new file mode 100644 index 000000000..445801e21 --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/strings.cc @@ -0,0 +1,391 @@ +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#if defined(_WIN32) || defined(__CYGWIN__) +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#endif // defined(_WIN32) || defined(__CYGWIN__) + +#include "strings.h" + +#if defined(_WIN32) || defined(__CYGWIN__) +#include +#endif // defined(_WIN32) || defined(__CYGWIN__) + +#include +#include +#include + +#include +#include // unique_ptr + +#include "exit_code.h" + +namespace blaze_util { + +using std::string; +using std::unique_ptr; +using std::vector; +using std::wstring; + +static const char kSeparator[] = " \n\t\r"; + +bool starts_with(const string &haystack, const string &needle) { + return (haystack.length() >= needle.length()) && + (memcmp(haystack.c_str(), needle.c_str(), needle.length()) == 0); +} + +template +static bool ends_with_impl(const std::basic_string &haystack, + const std::basic_string &needle) { + return (haystack.length() >= needle.length()) && + std::equal(haystack.cend() - needle.length(), haystack.cend(), + needle.cbegin()); +} + +bool ends_with(const string &haystack, const string &needle) { + return ends_with_impl(haystack, needle); +} + +bool ends_with(const wstring &haystack, const wstring &needle) { + return ends_with_impl(haystack, needle); +} + +void JoinStrings(const vector &pieces, const char delimeter, + string *output) { + bool first = true; + for (const auto &piece : pieces) { + if (first) { + *output = piece; + first = false; + } else { + *output += delimeter + piece; + } + } +} + +vector Split(const string &contents, const char delimeter) { + vector result; + SplitStringUsing(contents, delimeter, &result); + return result; +} + +void SplitStringUsing(const string &contents, const char delimeter, + vector *result) { + assert(result); + + size_t start = 0; + while (start < contents.length() && contents[start] == delimeter) { + ++start; + } + + size_t newline = contents.find(delimeter, start); + while (newline != string::npos) { + result->push_back(string(contents, start, newline - start)); + start = newline; + while (start < contents.length() && contents[start] == delimeter) { + ++start; + } + newline = contents.find(delimeter, start); + } + + // If there is a trailing line, add that. + if (start != newline && start != contents.size()) { + result->push_back(string(contents, start)); + } +} + +size_t SplitQuotedStringUsing(const string &contents, const char delimeter, + std::vector *output) { + size_t len = contents.length(); + size_t start = 0; + size_t quote = string::npos; // quote position + size_t num_segments = 0; + + for (size_t pos = 0; pos < len; ++pos) { + if (start == pos && contents[start] == delimeter) { + ++start; + } else if (contents[pos] == '\\') { + ++pos; + } else if (quote != string::npos && contents[pos] == contents[quote]) { + quote = string::npos; + } else if (quote == string::npos && + (contents[pos] == '"' || contents[pos] == '\'')) { + quote = pos; + } else if (quote == string::npos && contents[pos] == delimeter) { + output->push_back(string(contents, start, pos - start)); + start = pos + 1; + num_segments++; + } + } + + // A trailing element + if (start < len) { + output->push_back(string(contents, start)); + num_segments++; + } + return num_segments; +} + +void Replace(const string &oldsub, const string &newsub, string *str) { + size_t start = 0; + // This is O(n^2) (the complexity of erase() is actually unspecified, but + // usually linear). + while ((start = str->find(oldsub, start)) != string::npos) { + str->erase(start, oldsub.length()); + str->insert(start, newsub); + start += newsub.length(); + } +} + +void StripWhitespace(string *str) { + int str_length = str->length(); + + // Strip off leading whitespace. + int first = 0; + while (first < str_length && ascii_isspace(str->at(first))) { + ++first; + } + // If entire string is white space. + if (first == str_length) { + str->clear(); + return; + } + if (first > 0) { + str->erase(0, first); + str_length -= first; + } + + // Strip off trailing whitespace. + int last = str_length - 1; + while (last >= 0 && ascii_isspace(str->at(last))) { + --last; + } + if (last != (str_length - 1) && last >= 0) { + str->erase(last + 1, string::npos); + } +} + +static void GetNextToken(const string &str, const char &comment, + string::const_iterator *iter, vector *words) { + string output; + auto last = *iter; + char quote = '\0'; + // While not a delimiter. + while (last != str.end() && (quote || strchr(kSeparator, *last) == nullptr)) { + // Absorb escapes. + if (*last == '\\') { + ++last; + if (last == str.end()) { + break; + } + output += *last++; + continue; + } + + if (quote) { + if (*last == quote) { + // Absorb closing quote. + quote = '\0'; + ++last; + } else { + output += *last++; + } + } else { + if (*last == comment) { + last = str.end(); + break; + } + if (*last == '\'' || *last == '"') { + // Absorb opening quote. + quote = *last++; + } else { + output += *last++; + } + } + } + + if (!output.empty()) { + words->push_back(output); + } + + *iter = last; +} + +void Tokenize(const string &str, const char &comment, vector *words) { + assert(words); + words->clear(); + + string::const_iterator i = str.begin(); + while (i != str.end()) { + // Skip whitespace. + while (i != str.end() && strchr(kSeparator, *i) != nullptr) { + i++; + } + if (i != str.end() && *i == comment) { + break; + } + GetNextToken(str, comment, &i, words); + } +} + +// Evaluate a format string and store the result in 'str'. +void StringPrintf(string *str, const char *format, ...) { + assert(str); + + // Determine the required buffer size. vsnpritnf won't account for the + // terminating '\0'. + va_list args; + va_start(args, format); + int output_size = vsnprintf(nullptr, 0, format, args); + if (output_size < 0) { + fprintf(stderr, "Fatal error formatting string: %d", output_size); + exit(blaze_exit_code::INTERNAL_ERROR); + } + va_end(args); + + // Allocate a buffer and format the input. + int buffer_size = output_size + sizeof '\0'; + char *buf = new char[buffer_size]; + va_start(args, format); + int print_result = vsnprintf(buf, buffer_size, format, args); + if (print_result < 0) { + fprintf(stderr, "Fatal error formatting string: %d", print_result); + exit(blaze_exit_code::INTERNAL_ERROR); + } + va_end(args); + + *str = buf; + delete[] buf; +} + +void ToLower(string *str) { + assert(str); + *str = AsLower(*str); +} + +string AsLower(const string &str) { + if (str.empty()) { + return ""; + } + unique_ptr result(new char[str.size() + 1]); + char *result_ptr = result.get(); + for (const auto &ch : str) { + *result_ptr++ = tolower(ch); + } + result.get()[str.size()] = 0; + return string(result.get()); +} + +#if defined(_WIN32) || defined(__CYGWIN__) + +template +static bool UStrToVStr(const std::basic_string &input, + std::basic_string *output, const bool use_utf8, + int (*Convert)(const bool _utf8, + const std::basic_string &_in, V *_out, + const size_t _size), + uint32_t *win32_error) { + int buf_size = input.size() + 1; + std::unique_ptr buf(new V[buf_size]); + // Attempt to convert, optimistically using the estimated output buffer size. + int res = Convert(use_utf8, input, buf.get(), buf_size); + if (res > 0) { + *output = buf.get(); + return true; + } + + DWORD err = GetLastError(); + if (err != ERROR_INSUFFICIENT_BUFFER) { + if (win32_error) { + *win32_error = static_cast(err); + } + return false; + } + + // The output buffer was too small. Get required buffer size. + res = Convert(use_utf8, input, NULL, 0); + if (res > 0) { + buf_size = res; + buf.reset(new V[buf_size]); + res = Convert(use_utf8, input, buf.get(), buf_size); + if (res > 0) { + *output = buf.get(); + return true; + } + } + if (win32_error) { + *win32_error = static_cast(GetLastError()); + } + return false; +} + +static int ConvertWcsToMbs(const bool use_utf8, const std::wstring &input, + char *output, const size_t output_size) { + return WideCharToMultiByte(use_utf8 ? CP_UTF8 : CP_ACP, 0, input.c_str(), -1, + output, output_size, NULL, NULL); +} + +static int ConvertMbsToWcs(const bool /* unused */, const std::string &input, + wchar_t *output, const size_t output_size) { + return MultiByteToWideChar(CP_UTF8, 0, input.c_str(), -1, output, + output_size); +} + +bool WcsToAcp(const std::wstring &input, std::string *output, + uint32_t *win32_error) { + return UStrToVStr(input, output, false, ConvertWcsToMbs, win32_error); +} + +bool WcsToUtf8(const std::wstring &input, std::string *output, + uint32_t *win32_error) { + return UStrToVStr(input, output, true, ConvertWcsToMbs, win32_error); +} + +bool Utf8ToWcs(const std::string &input, std::wstring *output, + uint32_t *win32_error) { + return UStrToVStr(input, output, /* unused */ true, ConvertMbsToWcs, + win32_error); +} + +std::string WstringToCstring(const std::wstring &input) { + std::string result; + uint32_t err; + if (!WcsToUtf8(input, &result, &err)) { + fprintf(stderr, + "WstringToCstring: failed with error %d (0x%08x), " + "invalid input \"%ls\"\n", + err, err, input.c_str()); + exit(blaze_exit_code::INTERNAL_ERROR); + } + return result; +} + +std::wstring CstringToWstring(const std::string &input) { + std::wstring result; + uint32_t err; + if (!Utf8ToWcs(input, &result, &err)) { + fprintf(stderr, + "CstringToWstring: failed with error %d (0x%08x), " + "invalid input \"%s\"\n", + err, err, input.c_str()); + exit(blaze_exit_code::INTERNAL_ERROR); + } + return result; +} + +#endif // defined(_WIN32) || defined(__CYGWIN__) + +} // namespace blaze_util diff --git a/third_party/java_tools/src/main/cpp/util/strings.h b/third_party/java_tools/src/main/cpp/util/strings.h new file mode 100644 index 000000000..3b89016b5 --- /dev/null +++ b/third_party/java_tools/src/main/cpp/util/strings.h @@ -0,0 +1,146 @@ +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef BAZEL_SRC_MAIN_CPP_UTIL_STRINGS_H_ +#define BAZEL_SRC_MAIN_CPP_UTIL_STRINGS_H_ + +#include // unique_ptr +#include +#include + +#ifdef BLAZE_OPENSOURCE +#include +#endif + +namespace blaze_util { + +// Returns the string representation of `value`. +// Workaround for mingw where std::to_string is not implemented. +// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015. +template +std::string ToString(const T &value) { +#if defined(__CYGWIN__) || defined(__MINGW32__) + std::ostringstream oss; + oss << value; + return oss.str(); +#else + return std::to_string(value); +#endif +} + +// Space characters according to Python: chr(i).isspace() +static inline bool ascii_isspace(unsigned char c) { + return c == 9 // TAB + || c == 10 // LF + || c == 11 // VT (vertical tab) + || c == 12 // FF (form feed) + || c == 13 // CR + || c == 32; // space +} + +bool starts_with(const std::string &haystack, const std::string &needle); + +bool ends_with(const std::string &haystack, const std::string &needle); + +bool ends_with(const std::wstring &haystack, const std::wstring &needle); + +// Matches a prefix (which must be a char* literal!) against the beginning of +// str. Returns a pointer past the prefix, or NULL if the prefix wasn't matched. +// (Like the standard strcasecmp(), but for efficiency doesn't call strlen() on +// prefix, and returns a pointer rather than an int.) +// +// The ""'s catch people who don't pass in a literal for "prefix" +#ifndef strprefix +#define strprefix(str, prefix) \ + (strncmp(str, prefix, sizeof("" prefix "") - 1) == 0 \ + ? str + sizeof(prefix) - 1 \ + : NULL) +#endif + +// Matches a prefix; returns a pointer past the prefix, or NULL if not found. +// (Like strprefix() and strcaseprefix() but not restricted to searching for +// char* literals). Templated so searching a const char* returns a const char*, +// and searching a non-const char* returns a non-const char*. +// Matches a prefix; returns a pointer past the prefix, or NULL if not found. +// (Like strprefix() and strcaseprefix() but not restricted to searching for +// char* literals). Templated so searching a const char* returns a const char*, +// and searching a non-const char* returns a non-const char*. +template +inline CharStar var_strprefix(CharStar str, const char *prefix) { + const int len = strlen(prefix); + return strncmp(str, prefix, len) == 0 ? str + len : NULL; +} + +// Join the elements of pieces separated by delimeter. Returns the joined +// string in output. +void JoinStrings(const std::vector &pieces, const char delimeter, + std::string *output); + +// Splits contents by delimeter. Skips empty subsections. +std::vector Split(const std::string &contents, + const char delimeter); + +// Same as above, but adds results to output. +void SplitStringUsing(const std::string &contents, const char delimeter, + std::vector *output); + +// Same as above, but adds results to output. Returns number of elements added. +size_t SplitQuotedStringUsing(const std::string &contents, const char delimeter, + std::vector *output); + +// Global replace of oldsub with newsub. +void Replace(const std::string &oldsub, const std::string &newsub, + std::string *str); + +// Removes whitespace from both ends of a string. +void StripWhitespace(std::string *str); + +// Tokenizes str on whitespace and places the tokens in words. Splits on spaces, +// newlines, carriage returns, and tabs. Respects single and double quotes (that +// is, "a string of 'some stuff'" would be 4 tokens). If the comment character +// is found (outside of quotes), the rest of the string will be ignored. Any +// token can be escaped with \, e.g., "this\\ is\\ one\\ token". +void Tokenize(const std::string &str, const char &comment, + std::vector *words); + +// Evaluate a format string and store the result in 'str'. +void StringPrintf(std::string *str, const char *format, ...); + +// Convert str to lower case. No locale handling, this is just for ASCII. +void ToLower(std::string *str); + +std::string AsLower(const std::string &str); + +#if defined(_WIN32) || defined(__CYGWIN__) +// Convert UTF-16 string to ASCII (using the Active Code Page). +bool WcsToAcp(const std::wstring &input, std::string *output, + uint32_t *error = nullptr); + +// Convert UTF-16 string to UTF-8. +bool WcsToUtf8(const std::wstring &input, std::string *output, + uint32_t *error = nullptr); + +// Convert UTF-8 string to UTF-16. +bool Utf8ToWcs(const std::string &input, std::wstring *output, + uint32_t *error = nullptr); + +// Deprecated. Use WcsToAcp or WcsToUtf8. +std::string WstringToCstring(const std::wstring &input); + +// Deprecated. Use AcpToWcs or Utf8ToWcs. +std::wstring CstringToWstring(const std::string &input); +#endif // defined(_WIN32) || defined(__CYGWIN__) + +} // namespace blaze_util + +#endif // BAZEL_SRC_MAIN_CPP_UTIL_STRINGS_H_ diff --git a/third_party/java_tools/zlib/BUILD.bazel b/third_party/java_tools/zlib/BUILD.bazel new file mode 100644 index 000000000..3d55bc6e8 --- /dev/null +++ b/third_party/java_tools/zlib/BUILD.bazel @@ -0,0 +1,14 @@ +licenses(["notice"]) # BSD/MIT-like license + +cc_library( + name = "zlib", + srcs = glob(["*.c"]), + hdrs = glob(["*.h"]), + # Use -Dverbose=-1 to turn off zlib's trace logging. (#3280) + copts = [ + "-w", + "-Dverbose=-1", + ], + includes = ["."], + visibility = ["//visibility:public"], +) diff --git a/third_party/java_tools/zlib/LICENSE.txt b/third_party/java_tools/zlib/LICENSE.txt new file mode 100644 index 000000000..56cea1f7a --- /dev/null +++ b/third_party/java_tools/zlib/LICENSE.txt @@ -0,0 +1,28 @@ +/* zlib.h -- interface of the 'zlib' general purpose compression library + version 1.2.11, January 15th, 2017 + + Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + Jean-loup Gailly Mark Adler + jloup@gzip.org madler@alumni.caltech.edu + + + The data format used by the zlib library is described by RFCs (Request for + Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950 + (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format). +*/ diff --git a/third_party/java_tools/zlib/adler32.c b/third_party/java_tools/zlib/adler32.c new file mode 100644 index 000000000..d0be4380a --- /dev/null +++ b/third_party/java_tools/zlib/adler32.c @@ -0,0 +1,186 @@ +/* adler32.c -- compute the Adler-32 checksum of a data stream + * Copyright (C) 1995-2011, 2016 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* @(#) $Id$ */ + +#include "zutil.h" + +local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2)); + +#define BASE 65521U /* largest prime smaller than 65536 */ +#define NMAX 5552 +/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ + +#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;} +#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); +#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); +#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); +#define DO16(buf) DO8(buf,0); DO8(buf,8); + +/* use NO_DIVIDE if your processor does not do division in hardware -- + try it both ways to see which is faster */ +#ifdef NO_DIVIDE +/* note that this assumes BASE is 65521, where 65536 % 65521 == 15 + (thank you to John Reiser for pointing this out) */ +# define CHOP(a) \ + do { \ + unsigned long tmp = a >> 16; \ + a &= 0xffffUL; \ + a += (tmp << 4) - tmp; \ + } while (0) +# define MOD28(a) \ + do { \ + CHOP(a); \ + if (a >= BASE) a -= BASE; \ + } while (0) +# define MOD(a) \ + do { \ + CHOP(a); \ + MOD28(a); \ + } while (0) +# define MOD63(a) \ + do { /* this assumes a is not negative */ \ + z_off64_t tmp = a >> 32; \ + a &= 0xffffffffL; \ + a += (tmp << 8) - (tmp << 5) + tmp; \ + tmp = a >> 16; \ + a &= 0xffffL; \ + a += (tmp << 4) - tmp; \ + tmp = a >> 16; \ + a &= 0xffffL; \ + a += (tmp << 4) - tmp; \ + if (a >= BASE) a -= BASE; \ + } while (0) +#else +# define MOD(a) a %= BASE +# define MOD28(a) a %= BASE +# define MOD63(a) a %= BASE +#endif + +/* ========================================================================= */ +uLong ZEXPORT adler32_z(adler, buf, len) + uLong adler; + const Bytef *buf; + z_size_t len; +{ + unsigned long sum2; + unsigned n; + + /* split Adler-32 into component sums */ + sum2 = (adler >> 16) & 0xffff; + adler &= 0xffff; + + /* in case user likes doing a byte at a time, keep it fast */ + if (len == 1) { + adler += buf[0]; + if (adler >= BASE) + adler -= BASE; + sum2 += adler; + if (sum2 >= BASE) + sum2 -= BASE; + return adler | (sum2 << 16); + } + + /* initial Adler-32 value (deferred check for len == 1 speed) */ + if (buf == Z_NULL) + return 1L; + + /* in case short lengths are provided, keep it somewhat fast */ + if (len < 16) { + while (len--) { + adler += *buf++; + sum2 += adler; + } + if (adler >= BASE) + adler -= BASE; + MOD28(sum2); /* only added so many BASE's */ + return adler | (sum2 << 16); + } + + /* do length NMAX blocks -- requires just one modulo operation */ + while (len >= NMAX) { + len -= NMAX; + n = NMAX / 16; /* NMAX is divisible by 16 */ + do { + DO16(buf); /* 16 sums unrolled */ + buf += 16; + } while (--n); + MOD(adler); + MOD(sum2); + } + + /* do remaining bytes (less than NMAX, still just one modulo) */ + if (len) { /* avoid modulos if none remaining */ + while (len >= 16) { + len -= 16; + DO16(buf); + buf += 16; + } + while (len--) { + adler += *buf++; + sum2 += adler; + } + MOD(adler); + MOD(sum2); + } + + /* return recombined sums */ + return adler | (sum2 << 16); +} + +/* ========================================================================= */ +uLong ZEXPORT adler32(adler, buf, len) + uLong adler; + const Bytef *buf; + uInt len; +{ + return adler32_z(adler, buf, len); +} + +/* ========================================================================= */ +local uLong adler32_combine_(adler1, adler2, len2) + uLong adler1; + uLong adler2; + z_off64_t len2; +{ + unsigned long sum1; + unsigned long sum2; + unsigned rem; + + /* for negative len, return invalid adler32 as a clue for debugging */ + if (len2 < 0) + return 0xffffffffUL; + + /* the derivation of this formula is left as an exercise for the reader */ + MOD63(len2); /* assumes len2 >= 0 */ + rem = (unsigned)len2; + sum1 = adler1 & 0xffff; + sum2 = rem * sum1; + MOD(sum2); + sum1 += (adler2 & 0xffff) + BASE - 1; + sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; + if (sum1 >= BASE) sum1 -= BASE; + if (sum1 >= BASE) sum1 -= BASE; + if (sum2 >= ((unsigned long)BASE << 1)) sum2 -= ((unsigned long)BASE << 1); + if (sum2 >= BASE) sum2 -= BASE; + return sum1 | (sum2 << 16); +} + +/* ========================================================================= */ +uLong ZEXPORT adler32_combine(adler1, adler2, len2) + uLong adler1; + uLong adler2; + z_off_t len2; +{ + return adler32_combine_(adler1, adler2, len2); +} + +uLong ZEXPORT adler32_combine64(adler1, adler2, len2) + uLong adler1; + uLong adler2; + z_off64_t len2; +{ + return adler32_combine_(adler1, adler2, len2); +} diff --git a/third_party/java_tools/zlib/compress.c b/third_party/java_tools/zlib/compress.c new file mode 100644 index 000000000..e2db404ab --- /dev/null +++ b/third_party/java_tools/zlib/compress.c @@ -0,0 +1,86 @@ +/* compress.c -- compress a memory buffer + * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* @(#) $Id$ */ + +#define ZLIB_INTERNAL +#include "zlib.h" + +/* =========================================================================== + Compresses the source buffer into the destination buffer. The level + parameter has the same meaning as in deflateInit. sourceLen is the byte + length of the source buffer. Upon entry, destLen is the total size of the + destination buffer, which must be at least 0.1% larger than sourceLen plus + 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. + + compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_BUF_ERROR if there was not enough room in the output buffer, + Z_STREAM_ERROR if the level parameter is invalid. +*/ +int ZEXPORT compress2 (dest, destLen, source, sourceLen, level) + Bytef *dest; + uLongf *destLen; + const Bytef *source; + uLong sourceLen; + int level; +{ + z_stream stream; + int err; + const uInt max = (uInt)-1; + uLong left; + + left = *destLen; + *destLen = 0; + + stream.zalloc = (alloc_func)0; + stream.zfree = (free_func)0; + stream.opaque = (voidpf)0; + + err = deflateInit(&stream, level); + if (err != Z_OK) return err; + + stream.next_out = dest; + stream.avail_out = 0; + stream.next_in = (z_const Bytef *)source; + stream.avail_in = 0; + + do { + if (stream.avail_out == 0) { + stream.avail_out = left > (uLong)max ? max : (uInt)left; + left -= stream.avail_out; + } + if (stream.avail_in == 0) { + stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen; + sourceLen -= stream.avail_in; + } + err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH); + } while (err == Z_OK); + + *destLen = stream.total_out; + deflateEnd(&stream); + return err == Z_STREAM_END ? Z_OK : err; +} + +/* =========================================================================== + */ +int ZEXPORT compress (dest, destLen, source, sourceLen) + Bytef *dest; + uLongf *destLen; + const Bytef *source; + uLong sourceLen; +{ + return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); +} + +/* =========================================================================== + If the default memLevel or windowBits for deflateInit() is changed, then + this function needs to be updated. + */ +uLong ZEXPORT compressBound (sourceLen) + uLong sourceLen; +{ + return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + + (sourceLen >> 25) + 13; +} diff --git a/third_party/java_tools/zlib/crc32.c b/third_party/java_tools/zlib/crc32.c new file mode 100644 index 000000000..9580440c0 --- /dev/null +++ b/third_party/java_tools/zlib/crc32.c @@ -0,0 +1,442 @@ +/* crc32.c -- compute the CRC-32 of a data stream + * Copyright (C) 1995-2006, 2010, 2011, 2012, 2016 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + * + * Thanks to Rodney Brown for his contribution of faster + * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing + * tables for updating the shift register in one step with three exclusive-ors + * instead of four steps with four exclusive-ors. This results in about a + * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3. + */ + +/* @(#) $Id$ */ + +/* + Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore + protection on the static variables used to control the first-use generation + of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should + first call get_crc_table() to initialize the tables before allowing more than + one thread to use crc32(). + + DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h. + */ + +#ifdef MAKECRCH +# include +# ifndef DYNAMIC_CRC_TABLE +# define DYNAMIC_CRC_TABLE +# endif /* !DYNAMIC_CRC_TABLE */ +#endif /* MAKECRCH */ + +#include "zutil.h" /* for STDC and FAR definitions */ + +/* Definitions for doing the crc four data bytes at a time. */ +#if !defined(NOBYFOUR) && defined(Z_U4) +# define BYFOUR +#endif +#ifdef BYFOUR + local unsigned long crc32_little OF((unsigned long, + const unsigned char FAR *, z_size_t)); + local unsigned long crc32_big OF((unsigned long, + const unsigned char FAR *, z_size_t)); +# define TBLS 8 +#else +# define TBLS 1 +#endif /* BYFOUR */ + +/* Local functions for crc concatenation */ +local unsigned long gf2_matrix_times OF((unsigned long *mat, + unsigned long vec)); +local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); +local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2)); + + +#ifdef DYNAMIC_CRC_TABLE + +local volatile int crc_table_empty = 1; +local z_crc_t FAR crc_table[TBLS][256]; +local void make_crc_table OF((void)); +#ifdef MAKECRCH + local void write_table OF((FILE *, const z_crc_t FAR *)); +#endif /* MAKECRCH */ +/* + Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: + x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1. + + Polynomials over GF(2) are represented in binary, one bit per coefficient, + with the lowest powers in the most significant bit. Then adding polynomials + is just exclusive-or, and multiplying a polynomial by x is a right shift by + one. If we call the above polynomial p, and represent a byte as the + polynomial q, also with the lowest power in the most significant bit (so the + byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, + where a mod b means the remainder after dividing a by b. + + This calculation is done using the shift-register method of multiplying and + taking the remainder. The register is initialized to zero, and for each + incoming bit, x^32 is added mod p to the register if the bit is a one (where + x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by + x (which is shifting right by one and adding x^32 mod p if the bit shifted + out is a one). We start with the highest power (least significant bit) of + q and repeat for all eight bits of q. + + The first table is simply the CRC of all possible eight bit values. This is + all the information needed to generate CRCs on data a byte at a time for all + combinations of CRC register values and incoming bytes. The remaining tables + allow for word-at-a-time CRC calculation for both big-endian and little- + endian machines, where a word is four bytes. +*/ +local void make_crc_table() +{ + z_crc_t c; + int n, k; + z_crc_t poly; /* polynomial exclusive-or pattern */ + /* terms of polynomial defining this crc (except x^32): */ + static volatile int first = 1; /* flag to limit concurrent making */ + static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; + + /* See if another task is already doing this (not thread-safe, but better + than nothing -- significantly reduces duration of vulnerability in + case the advice about DYNAMIC_CRC_TABLE is ignored) */ + if (first) { + first = 0; + + /* make exclusive-or pattern from polynomial (0xedb88320UL) */ + poly = 0; + for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++) + poly |= (z_crc_t)1 << (31 - p[n]); + + /* generate a crc for every 8-bit value */ + for (n = 0; n < 256; n++) { + c = (z_crc_t)n; + for (k = 0; k < 8; k++) + c = c & 1 ? poly ^ (c >> 1) : c >> 1; + crc_table[0][n] = c; + } + +#ifdef BYFOUR + /* generate crc for each value followed by one, two, and three zeros, + and then the byte reversal of those as well as the first table */ + for (n = 0; n < 256; n++) { + c = crc_table[0][n]; + crc_table[4][n] = ZSWAP32(c); + for (k = 1; k < 4; k++) { + c = crc_table[0][c & 0xff] ^ (c >> 8); + crc_table[k][n] = c; + crc_table[k + 4][n] = ZSWAP32(c); + } + } +#endif /* BYFOUR */ + + crc_table_empty = 0; + } + else { /* not first */ + /* wait for the other guy to finish (not efficient, but rare) */ + while (crc_table_empty) + ; + } + +#ifdef MAKECRCH + /* write out CRC tables to crc32.h */ + { + FILE *out; + + out = fopen("crc32.h", "w"); + if (out == NULL) return; + fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); + fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); + fprintf(out, "local const z_crc_t FAR "); + fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); + write_table(out, crc_table[0]); +# ifdef BYFOUR + fprintf(out, "#ifdef BYFOUR\n"); + for (k = 1; k < 8; k++) { + fprintf(out, " },\n {\n"); + write_table(out, crc_table[k]); + } + fprintf(out, "#endif\n"); +# endif /* BYFOUR */ + fprintf(out, " }\n};\n"); + fclose(out); + } +#endif /* MAKECRCH */ +} + +#ifdef MAKECRCH +local void write_table(out, table) + FILE *out; + const z_crc_t FAR *table; +{ + int n; + + for (n = 0; n < 256; n++) + fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", + (unsigned long)(table[n]), + n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); +} +#endif /* MAKECRCH */ + +#else /* !DYNAMIC_CRC_TABLE */ +/* ======================================================================== + * Tables of CRC-32s of all single-byte values, made by make_crc_table(). + */ +#include "crc32.h" +#endif /* DYNAMIC_CRC_TABLE */ + +/* ========================================================================= + * This function can be used by asm versions of crc32() + */ +const z_crc_t FAR * ZEXPORT get_crc_table() +{ +#ifdef DYNAMIC_CRC_TABLE + if (crc_table_empty) + make_crc_table(); +#endif /* DYNAMIC_CRC_TABLE */ + return (const z_crc_t FAR *)crc_table; +} + +/* ========================================================================= */ +#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) +#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 + +/* ========================================================================= */ +unsigned long ZEXPORT crc32_z(crc, buf, len) + unsigned long crc; + const unsigned char FAR *buf; + z_size_t len; +{ + if (buf == Z_NULL) return 0UL; + +#ifdef DYNAMIC_CRC_TABLE + if (crc_table_empty) + make_crc_table(); +#endif /* DYNAMIC_CRC_TABLE */ + +#ifdef BYFOUR + if (sizeof(void *) == sizeof(ptrdiff_t)) { + z_crc_t endian; + + endian = 1; + if (*((unsigned char *)(&endian))) + return crc32_little(crc, buf, len); + else + return crc32_big(crc, buf, len); + } +#endif /* BYFOUR */ + crc = crc ^ 0xffffffffUL; + while (len >= 8) { + DO8; + len -= 8; + } + if (len) do { + DO1; + } while (--len); + return crc ^ 0xffffffffUL; +} + +/* ========================================================================= */ +unsigned long ZEXPORT crc32(crc, buf, len) + unsigned long crc; + const unsigned char FAR *buf; + uInt len; +{ + return crc32_z(crc, buf, len); +} + +#ifdef BYFOUR + +/* + This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit + integer pointer type. This violates the strict aliasing rule, where a + compiler can assume, for optimization purposes, that two pointers to + fundamentally different types won't ever point to the same memory. This can + manifest as a problem only if one of the pointers is written to. This code + only reads from those pointers. So long as this code remains isolated in + this compilation unit, there won't be a problem. For this reason, this code + should not be copied and pasted into a compilation unit in which other code + writes to the buffer that is passed to these routines. + */ + +/* ========================================================================= */ +#define DOLIT4 c ^= *buf4++; \ + c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ + crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] +#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 + +/* ========================================================================= */ +local unsigned long crc32_little(crc, buf, len) + unsigned long crc; + const unsigned char FAR *buf; + z_size_t len; +{ + register z_crc_t c; + register const z_crc_t FAR *buf4; + + c = (z_crc_t)crc; + c = ~c; + while (len && ((ptrdiff_t)buf & 3)) { + c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); + len--; + } + + buf4 = (const z_crc_t FAR *)(const void FAR *)buf; + while (len >= 32) { + DOLIT32; + len -= 32; + } + while (len >= 4) { + DOLIT4; + len -= 4; + } + buf = (const unsigned char FAR *)buf4; + + if (len) do { + c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); + } while (--len); + c = ~c; + return (unsigned long)c; +} + +/* ========================================================================= */ +#define DOBIG4 c ^= *buf4++; \ + c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ + crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] +#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 + +/* ========================================================================= */ +local unsigned long crc32_big(crc, buf, len) + unsigned long crc; + const unsigned char FAR *buf; + z_size_t len; +{ + register z_crc_t c; + register const z_crc_t FAR *buf4; + + c = ZSWAP32((z_crc_t)crc); + c = ~c; + while (len && ((ptrdiff_t)buf & 3)) { + c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); + len--; + } + + buf4 = (const z_crc_t FAR *)(const void FAR *)buf; + while (len >= 32) { + DOBIG32; + len -= 32; + } + while (len >= 4) { + DOBIG4; + len -= 4; + } + buf = (const unsigned char FAR *)buf4; + + if (len) do { + c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); + } while (--len); + c = ~c; + return (unsigned long)(ZSWAP32(c)); +} + +#endif /* BYFOUR */ + +#define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */ + +/* ========================================================================= */ +local unsigned long gf2_matrix_times(mat, vec) + unsigned long *mat; + unsigned long vec; +{ + unsigned long sum; + + sum = 0; + while (vec) { + if (vec & 1) + sum ^= *mat; + vec >>= 1; + mat++; + } + return sum; +} + +/* ========================================================================= */ +local void gf2_matrix_square(square, mat) + unsigned long *square; + unsigned long *mat; +{ + int n; + + for (n = 0; n < GF2_DIM; n++) + square[n] = gf2_matrix_times(mat, mat[n]); +} + +/* ========================================================================= */ +local uLong crc32_combine_(crc1, crc2, len2) + uLong crc1; + uLong crc2; + z_off64_t len2; +{ + int n; + unsigned long row; + unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */ + unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */ + + /* degenerate case (also disallow negative lengths) */ + if (len2 <= 0) + return crc1; + + /* put operator for one zero bit in odd */ + odd[0] = 0xedb88320UL; /* CRC-32 polynomial */ + row = 1; + for (n = 1; n < GF2_DIM; n++) { + odd[n] = row; + row <<= 1; + } + + /* put operator for two zero bits in even */ + gf2_matrix_square(even, odd); + + /* put operator for four zero bits in odd */ + gf2_matrix_square(odd, even); + + /* apply len2 zeros to crc1 (first square will put the operator for one + zero byte, eight zero bits, in even) */ + do { + /* apply zeros operator for this bit of len2 */ + gf2_matrix_square(even, odd); + if (len2 & 1) + crc1 = gf2_matrix_times(even, crc1); + len2 >>= 1; + + /* if no more bits set, then done */ + if (len2 == 0) + break; + + /* another iteration of the loop with odd and even swapped */ + gf2_matrix_square(odd, even); + if (len2 & 1) + crc1 = gf2_matrix_times(odd, crc1); + len2 >>= 1; + + /* if no more bits set, then done */ + } while (len2 != 0); + + /* return combined crc */ + crc1 ^= crc2; + return crc1; +} + +/* ========================================================================= */ +uLong ZEXPORT crc32_combine(crc1, crc2, len2) + uLong crc1; + uLong crc2; + z_off_t len2; +{ + return crc32_combine_(crc1, crc2, len2); +} + +uLong ZEXPORT crc32_combine64(crc1, crc2, len2) + uLong crc1; + uLong crc2; + z_off64_t len2; +{ + return crc32_combine_(crc1, crc2, len2); +} diff --git a/third_party/java_tools/zlib/crc32.h b/third_party/java_tools/zlib/crc32.h new file mode 100644 index 000000000..9e0c77810 --- /dev/null +++ b/third_party/java_tools/zlib/crc32.h @@ -0,0 +1,441 @@ +/* crc32.h -- tables for rapid CRC calculation + * Generated automatically by crc32.c + */ + +local const z_crc_t FAR crc_table[TBLS][256] = +{ + { + 0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL, + 0x706af48fUL, 0xe963a535UL, 0x9e6495a3UL, 0x0edb8832UL, 0x79dcb8a4UL, + 0xe0d5e91eUL, 0x97d2d988UL, 0x09b64c2bUL, 0x7eb17cbdUL, 0xe7b82d07UL, + 0x90bf1d91UL, 0x1db71064UL, 0x6ab020f2UL, 0xf3b97148UL, 0x84be41deUL, + 0x1adad47dUL, 0x6ddde4ebUL, 0xf4d4b551UL, 0x83d385c7UL, 0x136c9856UL, + 0x646ba8c0UL, 0xfd62f97aUL, 0x8a65c9ecUL, 0x14015c4fUL, 0x63066cd9UL, + 0xfa0f3d63UL, 0x8d080df5UL, 0x3b6e20c8UL, 0x4c69105eUL, 0xd56041e4UL, + 0xa2677172UL, 0x3c03e4d1UL, 0x4b04d447UL, 0xd20d85fdUL, 0xa50ab56bUL, + 0x35b5a8faUL, 0x42b2986cUL, 0xdbbbc9d6UL, 0xacbcf940UL, 0x32d86ce3UL, + 0x45df5c75UL, 0xdcd60dcfUL, 0xabd13d59UL, 0x26d930acUL, 0x51de003aUL, + 0xc8d75180UL, 0xbfd06116UL, 0x21b4f4b5UL, 0x56b3c423UL, 0xcfba9599UL, + 0xb8bda50fUL, 0x2802b89eUL, 0x5f058808UL, 0xc60cd9b2UL, 0xb10be924UL, + 0x2f6f7c87UL, 0x58684c11UL, 0xc1611dabUL, 0xb6662d3dUL, 0x76dc4190UL, + 0x01db7106UL, 0x98d220bcUL, 0xefd5102aUL, 0x71b18589UL, 0x06b6b51fUL, + 0x9fbfe4a5UL, 0xe8b8d433UL, 0x7807c9a2UL, 0x0f00f934UL, 0x9609a88eUL, + 0xe10e9818UL, 0x7f6a0dbbUL, 0x086d3d2dUL, 0x91646c97UL, 0xe6635c01UL, + 0x6b6b51f4UL, 0x1c6c6162UL, 0x856530d8UL, 0xf262004eUL, 0x6c0695edUL, + 0x1b01a57bUL, 0x8208f4c1UL, 0xf50fc457UL, 0x65b0d9c6UL, 0x12b7e950UL, + 0x8bbeb8eaUL, 0xfcb9887cUL, 0x62dd1ddfUL, 0x15da2d49UL, 0x8cd37cf3UL, + 0xfbd44c65UL, 0x4db26158UL, 0x3ab551ceUL, 0xa3bc0074UL, 0xd4bb30e2UL, + 0x4adfa541UL, 0x3dd895d7UL, 0xa4d1c46dUL, 0xd3d6f4fbUL, 0x4369e96aUL, + 0x346ed9fcUL, 0xad678846UL, 0xda60b8d0UL, 0x44042d73UL, 0x33031de5UL, + 0xaa0a4c5fUL, 0xdd0d7cc9UL, 0x5005713cUL, 0x270241aaUL, 0xbe0b1010UL, + 0xc90c2086UL, 0x5768b525UL, 0x206f85b3UL, 0xb966d409UL, 0xce61e49fUL, + 0x5edef90eUL, 0x29d9c998UL, 0xb0d09822UL, 0xc7d7a8b4UL, 0x59b33d17UL, + 0x2eb40d81UL, 0xb7bd5c3bUL, 0xc0ba6cadUL, 0xedb88320UL, 0x9abfb3b6UL, + 0x03b6e20cUL, 0x74b1d29aUL, 0xead54739UL, 0x9dd277afUL, 0x04db2615UL, + 0x73dc1683UL, 0xe3630b12UL, 0x94643b84UL, 0x0d6d6a3eUL, 0x7a6a5aa8UL, + 0xe40ecf0bUL, 0x9309ff9dUL, 0x0a00ae27UL, 0x7d079eb1UL, 0xf00f9344UL, + 0x8708a3d2UL, 0x1e01f268UL, 0x6906c2feUL, 0xf762575dUL, 0x806567cbUL, + 0x196c3671UL, 0x6e6b06e7UL, 0xfed41b76UL, 0x89d32be0UL, 0x10da7a5aUL, + 0x67dd4accUL, 0xf9b9df6fUL, 0x8ebeeff9UL, 0x17b7be43UL, 0x60b08ed5UL, + 0xd6d6a3e8UL, 0xa1d1937eUL, 0x38d8c2c4UL, 0x4fdff252UL, 0xd1bb67f1UL, + 0xa6bc5767UL, 0x3fb506ddUL, 0x48b2364bUL, 0xd80d2bdaUL, 0xaf0a1b4cUL, + 0x36034af6UL, 0x41047a60UL, 0xdf60efc3UL, 0xa867df55UL, 0x316e8eefUL, + 0x4669be79UL, 0xcb61b38cUL, 0xbc66831aUL, 0x256fd2a0UL, 0x5268e236UL, + 0xcc0c7795UL, 0xbb0b4703UL, 0x220216b9UL, 0x5505262fUL, 0xc5ba3bbeUL, + 0xb2bd0b28UL, 0x2bb45a92UL, 0x5cb36a04UL, 0xc2d7ffa7UL, 0xb5d0cf31UL, + 0x2cd99e8bUL, 0x5bdeae1dUL, 0x9b64c2b0UL, 0xec63f226UL, 0x756aa39cUL, + 0x026d930aUL, 0x9c0906a9UL, 0xeb0e363fUL, 0x72076785UL, 0x05005713UL, + 0x95bf4a82UL, 0xe2b87a14UL, 0x7bb12baeUL, 0x0cb61b38UL, 0x92d28e9bUL, + 0xe5d5be0dUL, 0x7cdcefb7UL, 0x0bdbdf21UL, 0x86d3d2d4UL, 0xf1d4e242UL, + 0x68ddb3f8UL, 0x1fda836eUL, 0x81be16cdUL, 0xf6b9265bUL, 0x6fb077e1UL, + 0x18b74777UL, 0x88085ae6UL, 0xff0f6a70UL, 0x66063bcaUL, 0x11010b5cUL, + 0x8f659effUL, 0xf862ae69UL, 0x616bffd3UL, 0x166ccf45UL, 0xa00ae278UL, + 0xd70dd2eeUL, 0x4e048354UL, 0x3903b3c2UL, 0xa7672661UL, 0xd06016f7UL, + 0x4969474dUL, 0x3e6e77dbUL, 0xaed16a4aUL, 0xd9d65adcUL, 0x40df0b66UL, + 0x37d83bf0UL, 0xa9bcae53UL, 0xdebb9ec5UL, 0x47b2cf7fUL, 0x30b5ffe9UL, + 0xbdbdf21cUL, 0xcabac28aUL, 0x53b39330UL, 0x24b4a3a6UL, 0xbad03605UL, + 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, + 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, + 0x2d02ef8dUL +#ifdef BYFOUR + }, + { + 0x00000000UL, 0x191b3141UL, 0x32366282UL, 0x2b2d53c3UL, 0x646cc504UL, + 0x7d77f445UL, 0x565aa786UL, 0x4f4196c7UL, 0xc8d98a08UL, 0xd1c2bb49UL, + 0xfaefe88aUL, 0xe3f4d9cbUL, 0xacb54f0cUL, 0xb5ae7e4dUL, 0x9e832d8eUL, + 0x87981ccfUL, 0x4ac21251UL, 0x53d92310UL, 0x78f470d3UL, 0x61ef4192UL, + 0x2eaed755UL, 0x37b5e614UL, 0x1c98b5d7UL, 0x05838496UL, 0x821b9859UL, + 0x9b00a918UL, 0xb02dfadbUL, 0xa936cb9aUL, 0xe6775d5dUL, 0xff6c6c1cUL, + 0xd4413fdfUL, 0xcd5a0e9eUL, 0x958424a2UL, 0x8c9f15e3UL, 0xa7b24620UL, + 0xbea97761UL, 0xf1e8e1a6UL, 0xe8f3d0e7UL, 0xc3de8324UL, 0xdac5b265UL, + 0x5d5daeaaUL, 0x44469febUL, 0x6f6bcc28UL, 0x7670fd69UL, 0x39316baeUL, + 0x202a5aefUL, 0x0b07092cUL, 0x121c386dUL, 0xdf4636f3UL, 0xc65d07b2UL, + 0xed705471UL, 0xf46b6530UL, 0xbb2af3f7UL, 0xa231c2b6UL, 0x891c9175UL, + 0x9007a034UL, 0x179fbcfbUL, 0x0e848dbaUL, 0x25a9de79UL, 0x3cb2ef38UL, + 0x73f379ffUL, 0x6ae848beUL, 0x41c51b7dUL, 0x58de2a3cUL, 0xf0794f05UL, + 0xe9627e44UL, 0xc24f2d87UL, 0xdb541cc6UL, 0x94158a01UL, 0x8d0ebb40UL, + 0xa623e883UL, 0xbf38d9c2UL, 0x38a0c50dUL, 0x21bbf44cUL, 0x0a96a78fUL, + 0x138d96ceUL, 0x5ccc0009UL, 0x45d73148UL, 0x6efa628bUL, 0x77e153caUL, + 0xbabb5d54UL, 0xa3a06c15UL, 0x888d3fd6UL, 0x91960e97UL, 0xded79850UL, + 0xc7cca911UL, 0xece1fad2UL, 0xf5facb93UL, 0x7262d75cUL, 0x6b79e61dUL, + 0x4054b5deUL, 0x594f849fUL, 0x160e1258UL, 0x0f152319UL, 0x243870daUL, + 0x3d23419bUL, 0x65fd6ba7UL, 0x7ce65ae6UL, 0x57cb0925UL, 0x4ed03864UL, + 0x0191aea3UL, 0x188a9fe2UL, 0x33a7cc21UL, 0x2abcfd60UL, 0xad24e1afUL, + 0xb43fd0eeUL, 0x9f12832dUL, 0x8609b26cUL, 0xc94824abUL, 0xd05315eaUL, + 0xfb7e4629UL, 0xe2657768UL, 0x2f3f79f6UL, 0x362448b7UL, 0x1d091b74UL, + 0x04122a35UL, 0x4b53bcf2UL, 0x52488db3UL, 0x7965de70UL, 0x607eef31UL, + 0xe7e6f3feUL, 0xfefdc2bfUL, 0xd5d0917cUL, 0xcccba03dUL, 0x838a36faUL, + 0x9a9107bbUL, 0xb1bc5478UL, 0xa8a76539UL, 0x3b83984bUL, 0x2298a90aUL, + 0x09b5fac9UL, 0x10aecb88UL, 0x5fef5d4fUL, 0x46f46c0eUL, 0x6dd93fcdUL, + 0x74c20e8cUL, 0xf35a1243UL, 0xea412302UL, 0xc16c70c1UL, 0xd8774180UL, + 0x9736d747UL, 0x8e2de606UL, 0xa500b5c5UL, 0xbc1b8484UL, 0x71418a1aUL, + 0x685abb5bUL, 0x4377e898UL, 0x5a6cd9d9UL, 0x152d4f1eUL, 0x0c367e5fUL, + 0x271b2d9cUL, 0x3e001cddUL, 0xb9980012UL, 0xa0833153UL, 0x8bae6290UL, + 0x92b553d1UL, 0xddf4c516UL, 0xc4eff457UL, 0xefc2a794UL, 0xf6d996d5UL, + 0xae07bce9UL, 0xb71c8da8UL, 0x9c31de6bUL, 0x852aef2aUL, 0xca6b79edUL, + 0xd37048acUL, 0xf85d1b6fUL, 0xe1462a2eUL, 0x66de36e1UL, 0x7fc507a0UL, + 0x54e85463UL, 0x4df36522UL, 0x02b2f3e5UL, 0x1ba9c2a4UL, 0x30849167UL, + 0x299fa026UL, 0xe4c5aeb8UL, 0xfdde9ff9UL, 0xd6f3cc3aUL, 0xcfe8fd7bUL, + 0x80a96bbcUL, 0x99b25afdUL, 0xb29f093eUL, 0xab84387fUL, 0x2c1c24b0UL, + 0x350715f1UL, 0x1e2a4632UL, 0x07317773UL, 0x4870e1b4UL, 0x516bd0f5UL, + 0x7a468336UL, 0x635db277UL, 0xcbfad74eUL, 0xd2e1e60fUL, 0xf9ccb5ccUL, + 0xe0d7848dUL, 0xaf96124aUL, 0xb68d230bUL, 0x9da070c8UL, 0x84bb4189UL, + 0x03235d46UL, 0x1a386c07UL, 0x31153fc4UL, 0x280e0e85UL, 0x674f9842UL, + 0x7e54a903UL, 0x5579fac0UL, 0x4c62cb81UL, 0x8138c51fUL, 0x9823f45eUL, + 0xb30ea79dUL, 0xaa1596dcUL, 0xe554001bUL, 0xfc4f315aUL, 0xd7626299UL, + 0xce7953d8UL, 0x49e14f17UL, 0x50fa7e56UL, 0x7bd72d95UL, 0x62cc1cd4UL, + 0x2d8d8a13UL, 0x3496bb52UL, 0x1fbbe891UL, 0x06a0d9d0UL, 0x5e7ef3ecUL, + 0x4765c2adUL, 0x6c48916eUL, 0x7553a02fUL, 0x3a1236e8UL, 0x230907a9UL, + 0x0824546aUL, 0x113f652bUL, 0x96a779e4UL, 0x8fbc48a5UL, 0xa4911b66UL, + 0xbd8a2a27UL, 0xf2cbbce0UL, 0xebd08da1UL, 0xc0fdde62UL, 0xd9e6ef23UL, + 0x14bce1bdUL, 0x0da7d0fcUL, 0x268a833fUL, 0x3f91b27eUL, 0x70d024b9UL, + 0x69cb15f8UL, 0x42e6463bUL, 0x5bfd777aUL, 0xdc656bb5UL, 0xc57e5af4UL, + 0xee530937UL, 0xf7483876UL, 0xb809aeb1UL, 0xa1129ff0UL, 0x8a3fcc33UL, + 0x9324fd72UL + }, + { + 0x00000000UL, 0x01c26a37UL, 0x0384d46eUL, 0x0246be59UL, 0x0709a8dcUL, + 0x06cbc2ebUL, 0x048d7cb2UL, 0x054f1685UL, 0x0e1351b8UL, 0x0fd13b8fUL, + 0x0d9785d6UL, 0x0c55efe1UL, 0x091af964UL, 0x08d89353UL, 0x0a9e2d0aUL, + 0x0b5c473dUL, 0x1c26a370UL, 0x1de4c947UL, 0x1fa2771eUL, 0x1e601d29UL, + 0x1b2f0bacUL, 0x1aed619bUL, 0x18abdfc2UL, 0x1969b5f5UL, 0x1235f2c8UL, + 0x13f798ffUL, 0x11b126a6UL, 0x10734c91UL, 0x153c5a14UL, 0x14fe3023UL, + 0x16b88e7aUL, 0x177ae44dUL, 0x384d46e0UL, 0x398f2cd7UL, 0x3bc9928eUL, + 0x3a0bf8b9UL, 0x3f44ee3cUL, 0x3e86840bUL, 0x3cc03a52UL, 0x3d025065UL, + 0x365e1758UL, 0x379c7d6fUL, 0x35dac336UL, 0x3418a901UL, 0x3157bf84UL, + 0x3095d5b3UL, 0x32d36beaUL, 0x331101ddUL, 0x246be590UL, 0x25a98fa7UL, + 0x27ef31feUL, 0x262d5bc9UL, 0x23624d4cUL, 0x22a0277bUL, 0x20e69922UL, + 0x2124f315UL, 0x2a78b428UL, 0x2bbade1fUL, 0x29fc6046UL, 0x283e0a71UL, + 0x2d711cf4UL, 0x2cb376c3UL, 0x2ef5c89aUL, 0x2f37a2adUL, 0x709a8dc0UL, + 0x7158e7f7UL, 0x731e59aeUL, 0x72dc3399UL, 0x7793251cUL, 0x76514f2bUL, + 0x7417f172UL, 0x75d59b45UL, 0x7e89dc78UL, 0x7f4bb64fUL, 0x7d0d0816UL, + 0x7ccf6221UL, 0x798074a4UL, 0x78421e93UL, 0x7a04a0caUL, 0x7bc6cafdUL, + 0x6cbc2eb0UL, 0x6d7e4487UL, 0x6f38fadeUL, 0x6efa90e9UL, 0x6bb5866cUL, + 0x6a77ec5bUL, 0x68315202UL, 0x69f33835UL, 0x62af7f08UL, 0x636d153fUL, + 0x612bab66UL, 0x60e9c151UL, 0x65a6d7d4UL, 0x6464bde3UL, 0x662203baUL, + 0x67e0698dUL, 0x48d7cb20UL, 0x4915a117UL, 0x4b531f4eUL, 0x4a917579UL, + 0x4fde63fcUL, 0x4e1c09cbUL, 0x4c5ab792UL, 0x4d98dda5UL, 0x46c49a98UL, + 0x4706f0afUL, 0x45404ef6UL, 0x448224c1UL, 0x41cd3244UL, 0x400f5873UL, + 0x4249e62aUL, 0x438b8c1dUL, 0x54f16850UL, 0x55330267UL, 0x5775bc3eUL, + 0x56b7d609UL, 0x53f8c08cUL, 0x523aaabbUL, 0x507c14e2UL, 0x51be7ed5UL, + 0x5ae239e8UL, 0x5b2053dfUL, 0x5966ed86UL, 0x58a487b1UL, 0x5deb9134UL, + 0x5c29fb03UL, 0x5e6f455aUL, 0x5fad2f6dUL, 0xe1351b80UL, 0xe0f771b7UL, + 0xe2b1cfeeUL, 0xe373a5d9UL, 0xe63cb35cUL, 0xe7fed96bUL, 0xe5b86732UL, + 0xe47a0d05UL, 0xef264a38UL, 0xeee4200fUL, 0xeca29e56UL, 0xed60f461UL, + 0xe82fe2e4UL, 0xe9ed88d3UL, 0xebab368aUL, 0xea695cbdUL, 0xfd13b8f0UL, + 0xfcd1d2c7UL, 0xfe976c9eUL, 0xff5506a9UL, 0xfa1a102cUL, 0xfbd87a1bUL, + 0xf99ec442UL, 0xf85cae75UL, 0xf300e948UL, 0xf2c2837fUL, 0xf0843d26UL, + 0xf1465711UL, 0xf4094194UL, 0xf5cb2ba3UL, 0xf78d95faUL, 0xf64fffcdUL, + 0xd9785d60UL, 0xd8ba3757UL, 0xdafc890eUL, 0xdb3ee339UL, 0xde71f5bcUL, + 0xdfb39f8bUL, 0xddf521d2UL, 0xdc374be5UL, 0xd76b0cd8UL, 0xd6a966efUL, + 0xd4efd8b6UL, 0xd52db281UL, 0xd062a404UL, 0xd1a0ce33UL, 0xd3e6706aUL, + 0xd2241a5dUL, 0xc55efe10UL, 0xc49c9427UL, 0xc6da2a7eUL, 0xc7184049UL, + 0xc25756ccUL, 0xc3953cfbUL, 0xc1d382a2UL, 0xc011e895UL, 0xcb4dafa8UL, + 0xca8fc59fUL, 0xc8c97bc6UL, 0xc90b11f1UL, 0xcc440774UL, 0xcd866d43UL, + 0xcfc0d31aUL, 0xce02b92dUL, 0x91af9640UL, 0x906dfc77UL, 0x922b422eUL, + 0x93e92819UL, 0x96a63e9cUL, 0x976454abUL, 0x9522eaf2UL, 0x94e080c5UL, + 0x9fbcc7f8UL, 0x9e7eadcfUL, 0x9c381396UL, 0x9dfa79a1UL, 0x98b56f24UL, + 0x99770513UL, 0x9b31bb4aUL, 0x9af3d17dUL, 0x8d893530UL, 0x8c4b5f07UL, + 0x8e0de15eUL, 0x8fcf8b69UL, 0x8a809decUL, 0x8b42f7dbUL, 0x89044982UL, + 0x88c623b5UL, 0x839a6488UL, 0x82580ebfUL, 0x801eb0e6UL, 0x81dcdad1UL, + 0x8493cc54UL, 0x8551a663UL, 0x8717183aUL, 0x86d5720dUL, 0xa9e2d0a0UL, + 0xa820ba97UL, 0xaa6604ceUL, 0xaba46ef9UL, 0xaeeb787cUL, 0xaf29124bUL, + 0xad6fac12UL, 0xacadc625UL, 0xa7f18118UL, 0xa633eb2fUL, 0xa4755576UL, + 0xa5b73f41UL, 0xa0f829c4UL, 0xa13a43f3UL, 0xa37cfdaaUL, 0xa2be979dUL, + 0xb5c473d0UL, 0xb40619e7UL, 0xb640a7beUL, 0xb782cd89UL, 0xb2cddb0cUL, + 0xb30fb13bUL, 0xb1490f62UL, 0xb08b6555UL, 0xbbd72268UL, 0xba15485fUL, + 0xb853f606UL, 0xb9919c31UL, 0xbcde8ab4UL, 0xbd1ce083UL, 0xbf5a5edaUL, + 0xbe9834edUL + }, + { + 0x00000000UL, 0xb8bc6765UL, 0xaa09c88bUL, 0x12b5afeeUL, 0x8f629757UL, + 0x37def032UL, 0x256b5fdcUL, 0x9dd738b9UL, 0xc5b428efUL, 0x7d084f8aUL, + 0x6fbde064UL, 0xd7018701UL, 0x4ad6bfb8UL, 0xf26ad8ddUL, 0xe0df7733UL, + 0x58631056UL, 0x5019579fUL, 0xe8a530faUL, 0xfa109f14UL, 0x42acf871UL, + 0xdf7bc0c8UL, 0x67c7a7adUL, 0x75720843UL, 0xcdce6f26UL, 0x95ad7f70UL, + 0x2d111815UL, 0x3fa4b7fbUL, 0x8718d09eUL, 0x1acfe827UL, 0xa2738f42UL, + 0xb0c620acUL, 0x087a47c9UL, 0xa032af3eUL, 0x188ec85bUL, 0x0a3b67b5UL, + 0xb28700d0UL, 0x2f503869UL, 0x97ec5f0cUL, 0x8559f0e2UL, 0x3de59787UL, + 0x658687d1UL, 0xdd3ae0b4UL, 0xcf8f4f5aUL, 0x7733283fUL, 0xeae41086UL, + 0x525877e3UL, 0x40edd80dUL, 0xf851bf68UL, 0xf02bf8a1UL, 0x48979fc4UL, + 0x5a22302aUL, 0xe29e574fUL, 0x7f496ff6UL, 0xc7f50893UL, 0xd540a77dUL, + 0x6dfcc018UL, 0x359fd04eUL, 0x8d23b72bUL, 0x9f9618c5UL, 0x272a7fa0UL, + 0xbafd4719UL, 0x0241207cUL, 0x10f48f92UL, 0xa848e8f7UL, 0x9b14583dUL, + 0x23a83f58UL, 0x311d90b6UL, 0x89a1f7d3UL, 0x1476cf6aUL, 0xaccaa80fUL, + 0xbe7f07e1UL, 0x06c36084UL, 0x5ea070d2UL, 0xe61c17b7UL, 0xf4a9b859UL, + 0x4c15df3cUL, 0xd1c2e785UL, 0x697e80e0UL, 0x7bcb2f0eUL, 0xc377486bUL, + 0xcb0d0fa2UL, 0x73b168c7UL, 0x6104c729UL, 0xd9b8a04cUL, 0x446f98f5UL, + 0xfcd3ff90UL, 0xee66507eUL, 0x56da371bUL, 0x0eb9274dUL, 0xb6054028UL, + 0xa4b0efc6UL, 0x1c0c88a3UL, 0x81dbb01aUL, 0x3967d77fUL, 0x2bd27891UL, + 0x936e1ff4UL, 0x3b26f703UL, 0x839a9066UL, 0x912f3f88UL, 0x299358edUL, + 0xb4446054UL, 0x0cf80731UL, 0x1e4da8dfUL, 0xa6f1cfbaUL, 0xfe92dfecUL, + 0x462eb889UL, 0x549b1767UL, 0xec277002UL, 0x71f048bbUL, 0xc94c2fdeUL, + 0xdbf98030UL, 0x6345e755UL, 0x6b3fa09cUL, 0xd383c7f9UL, 0xc1366817UL, + 0x798a0f72UL, 0xe45d37cbUL, 0x5ce150aeUL, 0x4e54ff40UL, 0xf6e89825UL, + 0xae8b8873UL, 0x1637ef16UL, 0x048240f8UL, 0xbc3e279dUL, 0x21e91f24UL, + 0x99557841UL, 0x8be0d7afUL, 0x335cb0caUL, 0xed59b63bUL, 0x55e5d15eUL, + 0x47507eb0UL, 0xffec19d5UL, 0x623b216cUL, 0xda874609UL, 0xc832e9e7UL, + 0x708e8e82UL, 0x28ed9ed4UL, 0x9051f9b1UL, 0x82e4565fUL, 0x3a58313aUL, + 0xa78f0983UL, 0x1f336ee6UL, 0x0d86c108UL, 0xb53aa66dUL, 0xbd40e1a4UL, + 0x05fc86c1UL, 0x1749292fUL, 0xaff54e4aUL, 0x322276f3UL, 0x8a9e1196UL, + 0x982bbe78UL, 0x2097d91dUL, 0x78f4c94bUL, 0xc048ae2eUL, 0xd2fd01c0UL, + 0x6a4166a5UL, 0xf7965e1cUL, 0x4f2a3979UL, 0x5d9f9697UL, 0xe523f1f2UL, + 0x4d6b1905UL, 0xf5d77e60UL, 0xe762d18eUL, 0x5fdeb6ebUL, 0xc2098e52UL, + 0x7ab5e937UL, 0x680046d9UL, 0xd0bc21bcUL, 0x88df31eaUL, 0x3063568fUL, + 0x22d6f961UL, 0x9a6a9e04UL, 0x07bda6bdUL, 0xbf01c1d8UL, 0xadb46e36UL, + 0x15080953UL, 0x1d724e9aUL, 0xa5ce29ffUL, 0xb77b8611UL, 0x0fc7e174UL, + 0x9210d9cdUL, 0x2aacbea8UL, 0x38191146UL, 0x80a57623UL, 0xd8c66675UL, + 0x607a0110UL, 0x72cfaefeUL, 0xca73c99bUL, 0x57a4f122UL, 0xef189647UL, + 0xfdad39a9UL, 0x45115eccUL, 0x764dee06UL, 0xcef18963UL, 0xdc44268dUL, + 0x64f841e8UL, 0xf92f7951UL, 0x41931e34UL, 0x5326b1daUL, 0xeb9ad6bfUL, + 0xb3f9c6e9UL, 0x0b45a18cUL, 0x19f00e62UL, 0xa14c6907UL, 0x3c9b51beUL, + 0x842736dbUL, 0x96929935UL, 0x2e2efe50UL, 0x2654b999UL, 0x9ee8defcUL, + 0x8c5d7112UL, 0x34e11677UL, 0xa9362eceUL, 0x118a49abUL, 0x033fe645UL, + 0xbb838120UL, 0xe3e09176UL, 0x5b5cf613UL, 0x49e959fdUL, 0xf1553e98UL, + 0x6c820621UL, 0xd43e6144UL, 0xc68bceaaUL, 0x7e37a9cfUL, 0xd67f4138UL, + 0x6ec3265dUL, 0x7c7689b3UL, 0xc4caeed6UL, 0x591dd66fUL, 0xe1a1b10aUL, + 0xf3141ee4UL, 0x4ba87981UL, 0x13cb69d7UL, 0xab770eb2UL, 0xb9c2a15cUL, + 0x017ec639UL, 0x9ca9fe80UL, 0x241599e5UL, 0x36a0360bUL, 0x8e1c516eUL, + 0x866616a7UL, 0x3eda71c2UL, 0x2c6fde2cUL, 0x94d3b949UL, 0x090481f0UL, + 0xb1b8e695UL, 0xa30d497bUL, 0x1bb12e1eUL, 0x43d23e48UL, 0xfb6e592dUL, + 0xe9dbf6c3UL, 0x516791a6UL, 0xccb0a91fUL, 0x740cce7aUL, 0x66b96194UL, + 0xde0506f1UL + }, + { + 0x00000000UL, 0x96300777UL, 0x2c610eeeUL, 0xba510999UL, 0x19c46d07UL, + 0x8ff46a70UL, 0x35a563e9UL, 0xa395649eUL, 0x3288db0eUL, 0xa4b8dc79UL, + 0x1ee9d5e0UL, 0x88d9d297UL, 0x2b4cb609UL, 0xbd7cb17eUL, 0x072db8e7UL, + 0x911dbf90UL, 0x6410b71dUL, 0xf220b06aUL, 0x4871b9f3UL, 0xde41be84UL, + 0x7dd4da1aUL, 0xebe4dd6dUL, 0x51b5d4f4UL, 0xc785d383UL, 0x56986c13UL, + 0xc0a86b64UL, 0x7af962fdUL, 0xecc9658aUL, 0x4f5c0114UL, 0xd96c0663UL, + 0x633d0ffaUL, 0xf50d088dUL, 0xc8206e3bUL, 0x5e10694cUL, 0xe44160d5UL, + 0x727167a2UL, 0xd1e4033cUL, 0x47d4044bUL, 0xfd850dd2UL, 0x6bb50aa5UL, + 0xfaa8b535UL, 0x6c98b242UL, 0xd6c9bbdbUL, 0x40f9bcacUL, 0xe36cd832UL, + 0x755cdf45UL, 0xcf0dd6dcUL, 0x593dd1abUL, 0xac30d926UL, 0x3a00de51UL, + 0x8051d7c8UL, 0x1661d0bfUL, 0xb5f4b421UL, 0x23c4b356UL, 0x9995bacfUL, + 0x0fa5bdb8UL, 0x9eb80228UL, 0x0888055fUL, 0xb2d90cc6UL, 0x24e90bb1UL, + 0x877c6f2fUL, 0x114c6858UL, 0xab1d61c1UL, 0x3d2d66b6UL, 0x9041dc76UL, + 0x0671db01UL, 0xbc20d298UL, 0x2a10d5efUL, 0x8985b171UL, 0x1fb5b606UL, + 0xa5e4bf9fUL, 0x33d4b8e8UL, 0xa2c90778UL, 0x34f9000fUL, 0x8ea80996UL, + 0x18980ee1UL, 0xbb0d6a7fUL, 0x2d3d6d08UL, 0x976c6491UL, 0x015c63e6UL, + 0xf4516b6bUL, 0x62616c1cUL, 0xd8306585UL, 0x4e0062f2UL, 0xed95066cUL, + 0x7ba5011bUL, 0xc1f40882UL, 0x57c40ff5UL, 0xc6d9b065UL, 0x50e9b712UL, + 0xeab8be8bUL, 0x7c88b9fcUL, 0xdf1ddd62UL, 0x492dda15UL, 0xf37cd38cUL, + 0x654cd4fbUL, 0x5861b24dUL, 0xce51b53aUL, 0x7400bca3UL, 0xe230bbd4UL, + 0x41a5df4aUL, 0xd795d83dUL, 0x6dc4d1a4UL, 0xfbf4d6d3UL, 0x6ae96943UL, + 0xfcd96e34UL, 0x468867adUL, 0xd0b860daUL, 0x732d0444UL, 0xe51d0333UL, + 0x5f4c0aaaUL, 0xc97c0dddUL, 0x3c710550UL, 0xaa410227UL, 0x10100bbeUL, + 0x86200cc9UL, 0x25b56857UL, 0xb3856f20UL, 0x09d466b9UL, 0x9fe461ceUL, + 0x0ef9de5eUL, 0x98c9d929UL, 0x2298d0b0UL, 0xb4a8d7c7UL, 0x173db359UL, + 0x810db42eUL, 0x3b5cbdb7UL, 0xad6cbac0UL, 0x2083b8edUL, 0xb6b3bf9aUL, + 0x0ce2b603UL, 0x9ad2b174UL, 0x3947d5eaUL, 0xaf77d29dUL, 0x1526db04UL, + 0x8316dc73UL, 0x120b63e3UL, 0x843b6494UL, 0x3e6a6d0dUL, 0xa85a6a7aUL, + 0x0bcf0ee4UL, 0x9dff0993UL, 0x27ae000aUL, 0xb19e077dUL, 0x44930ff0UL, + 0xd2a30887UL, 0x68f2011eUL, 0xfec20669UL, 0x5d5762f7UL, 0xcb676580UL, + 0x71366c19UL, 0xe7066b6eUL, 0x761bd4feUL, 0xe02bd389UL, 0x5a7ada10UL, + 0xcc4add67UL, 0x6fdfb9f9UL, 0xf9efbe8eUL, 0x43beb717UL, 0xd58eb060UL, + 0xe8a3d6d6UL, 0x7e93d1a1UL, 0xc4c2d838UL, 0x52f2df4fUL, 0xf167bbd1UL, + 0x6757bca6UL, 0xdd06b53fUL, 0x4b36b248UL, 0xda2b0dd8UL, 0x4c1b0aafUL, + 0xf64a0336UL, 0x607a0441UL, 0xc3ef60dfUL, 0x55df67a8UL, 0xef8e6e31UL, + 0x79be6946UL, 0x8cb361cbUL, 0x1a8366bcUL, 0xa0d26f25UL, 0x36e26852UL, + 0x95770cccUL, 0x03470bbbUL, 0xb9160222UL, 0x2f260555UL, 0xbe3bbac5UL, + 0x280bbdb2UL, 0x925ab42bUL, 0x046ab35cUL, 0xa7ffd7c2UL, 0x31cfd0b5UL, + 0x8b9ed92cUL, 0x1daede5bUL, 0xb0c2649bUL, 0x26f263ecUL, 0x9ca36a75UL, + 0x0a936d02UL, 0xa906099cUL, 0x3f360eebUL, 0x85670772UL, 0x13570005UL, + 0x824abf95UL, 0x147ab8e2UL, 0xae2bb17bUL, 0x381bb60cUL, 0x9b8ed292UL, + 0x0dbed5e5UL, 0xb7efdc7cUL, 0x21dfdb0bUL, 0xd4d2d386UL, 0x42e2d4f1UL, + 0xf8b3dd68UL, 0x6e83da1fUL, 0xcd16be81UL, 0x5b26b9f6UL, 0xe177b06fUL, + 0x7747b718UL, 0xe65a0888UL, 0x706a0fffUL, 0xca3b0666UL, 0x5c0b0111UL, + 0xff9e658fUL, 0x69ae62f8UL, 0xd3ff6b61UL, 0x45cf6c16UL, 0x78e20aa0UL, + 0xeed20dd7UL, 0x5483044eUL, 0xc2b30339UL, 0x612667a7UL, 0xf71660d0UL, + 0x4d476949UL, 0xdb776e3eUL, 0x4a6ad1aeUL, 0xdc5ad6d9UL, 0x660bdf40UL, + 0xf03bd837UL, 0x53aebca9UL, 0xc59ebbdeUL, 0x7fcfb247UL, 0xe9ffb530UL, + 0x1cf2bdbdUL, 0x8ac2bacaUL, 0x3093b353UL, 0xa6a3b424UL, 0x0536d0baUL, + 0x9306d7cdUL, 0x2957de54UL, 0xbf67d923UL, 0x2e7a66b3UL, 0xb84a61c4UL, + 0x021b685dUL, 0x942b6f2aUL, 0x37be0bb4UL, 0xa18e0cc3UL, 0x1bdf055aUL, + 0x8def022dUL + }, + { + 0x00000000UL, 0x41311b19UL, 0x82623632UL, 0xc3532d2bUL, 0x04c56c64UL, + 0x45f4777dUL, 0x86a75a56UL, 0xc796414fUL, 0x088ad9c8UL, 0x49bbc2d1UL, + 0x8ae8effaUL, 0xcbd9f4e3UL, 0x0c4fb5acUL, 0x4d7eaeb5UL, 0x8e2d839eUL, + 0xcf1c9887UL, 0x5112c24aUL, 0x1023d953UL, 0xd370f478UL, 0x9241ef61UL, + 0x55d7ae2eUL, 0x14e6b537UL, 0xd7b5981cUL, 0x96848305UL, 0x59981b82UL, + 0x18a9009bUL, 0xdbfa2db0UL, 0x9acb36a9UL, 0x5d5d77e6UL, 0x1c6c6cffUL, + 0xdf3f41d4UL, 0x9e0e5acdUL, 0xa2248495UL, 0xe3159f8cUL, 0x2046b2a7UL, + 0x6177a9beUL, 0xa6e1e8f1UL, 0xe7d0f3e8UL, 0x2483dec3UL, 0x65b2c5daUL, + 0xaaae5d5dUL, 0xeb9f4644UL, 0x28cc6b6fUL, 0x69fd7076UL, 0xae6b3139UL, + 0xef5a2a20UL, 0x2c09070bUL, 0x6d381c12UL, 0xf33646dfUL, 0xb2075dc6UL, + 0x715470edUL, 0x30656bf4UL, 0xf7f32abbUL, 0xb6c231a2UL, 0x75911c89UL, + 0x34a00790UL, 0xfbbc9f17UL, 0xba8d840eUL, 0x79dea925UL, 0x38efb23cUL, + 0xff79f373UL, 0xbe48e86aUL, 0x7d1bc541UL, 0x3c2ade58UL, 0x054f79f0UL, + 0x447e62e9UL, 0x872d4fc2UL, 0xc61c54dbUL, 0x018a1594UL, 0x40bb0e8dUL, + 0x83e823a6UL, 0xc2d938bfUL, 0x0dc5a038UL, 0x4cf4bb21UL, 0x8fa7960aUL, + 0xce968d13UL, 0x0900cc5cUL, 0x4831d745UL, 0x8b62fa6eUL, 0xca53e177UL, + 0x545dbbbaUL, 0x156ca0a3UL, 0xd63f8d88UL, 0x970e9691UL, 0x5098d7deUL, + 0x11a9ccc7UL, 0xd2fae1ecUL, 0x93cbfaf5UL, 0x5cd76272UL, 0x1de6796bUL, + 0xdeb55440UL, 0x9f844f59UL, 0x58120e16UL, 0x1923150fUL, 0xda703824UL, + 0x9b41233dUL, 0xa76bfd65UL, 0xe65ae67cUL, 0x2509cb57UL, 0x6438d04eUL, + 0xa3ae9101UL, 0xe29f8a18UL, 0x21cca733UL, 0x60fdbc2aUL, 0xafe124adUL, + 0xeed03fb4UL, 0x2d83129fUL, 0x6cb20986UL, 0xab2448c9UL, 0xea1553d0UL, + 0x29467efbUL, 0x687765e2UL, 0xf6793f2fUL, 0xb7482436UL, 0x741b091dUL, + 0x352a1204UL, 0xf2bc534bUL, 0xb38d4852UL, 0x70de6579UL, 0x31ef7e60UL, + 0xfef3e6e7UL, 0xbfc2fdfeUL, 0x7c91d0d5UL, 0x3da0cbccUL, 0xfa368a83UL, + 0xbb07919aUL, 0x7854bcb1UL, 0x3965a7a8UL, 0x4b98833bUL, 0x0aa99822UL, + 0xc9fab509UL, 0x88cbae10UL, 0x4f5def5fUL, 0x0e6cf446UL, 0xcd3fd96dUL, + 0x8c0ec274UL, 0x43125af3UL, 0x022341eaUL, 0xc1706cc1UL, 0x804177d8UL, + 0x47d73697UL, 0x06e62d8eUL, 0xc5b500a5UL, 0x84841bbcUL, 0x1a8a4171UL, + 0x5bbb5a68UL, 0x98e87743UL, 0xd9d96c5aUL, 0x1e4f2d15UL, 0x5f7e360cUL, + 0x9c2d1b27UL, 0xdd1c003eUL, 0x120098b9UL, 0x533183a0UL, 0x9062ae8bUL, + 0xd153b592UL, 0x16c5f4ddUL, 0x57f4efc4UL, 0x94a7c2efUL, 0xd596d9f6UL, + 0xe9bc07aeUL, 0xa88d1cb7UL, 0x6bde319cUL, 0x2aef2a85UL, 0xed796bcaUL, + 0xac4870d3UL, 0x6f1b5df8UL, 0x2e2a46e1UL, 0xe136de66UL, 0xa007c57fUL, + 0x6354e854UL, 0x2265f34dUL, 0xe5f3b202UL, 0xa4c2a91bUL, 0x67918430UL, + 0x26a09f29UL, 0xb8aec5e4UL, 0xf99fdefdUL, 0x3accf3d6UL, 0x7bfde8cfUL, + 0xbc6ba980UL, 0xfd5ab299UL, 0x3e099fb2UL, 0x7f3884abUL, 0xb0241c2cUL, + 0xf1150735UL, 0x32462a1eUL, 0x73773107UL, 0xb4e17048UL, 0xf5d06b51UL, + 0x3683467aUL, 0x77b25d63UL, 0x4ed7facbUL, 0x0fe6e1d2UL, 0xccb5ccf9UL, + 0x8d84d7e0UL, 0x4a1296afUL, 0x0b238db6UL, 0xc870a09dUL, 0x8941bb84UL, + 0x465d2303UL, 0x076c381aUL, 0xc43f1531UL, 0x850e0e28UL, 0x42984f67UL, + 0x03a9547eUL, 0xc0fa7955UL, 0x81cb624cUL, 0x1fc53881UL, 0x5ef42398UL, + 0x9da70eb3UL, 0xdc9615aaUL, 0x1b0054e5UL, 0x5a314ffcUL, 0x996262d7UL, + 0xd85379ceUL, 0x174fe149UL, 0x567efa50UL, 0x952dd77bUL, 0xd41ccc62UL, + 0x138a8d2dUL, 0x52bb9634UL, 0x91e8bb1fUL, 0xd0d9a006UL, 0xecf37e5eUL, + 0xadc26547UL, 0x6e91486cUL, 0x2fa05375UL, 0xe836123aUL, 0xa9070923UL, + 0x6a542408UL, 0x2b653f11UL, 0xe479a796UL, 0xa548bc8fUL, 0x661b91a4UL, + 0x272a8abdUL, 0xe0bccbf2UL, 0xa18dd0ebUL, 0x62defdc0UL, 0x23efe6d9UL, + 0xbde1bc14UL, 0xfcd0a70dUL, 0x3f838a26UL, 0x7eb2913fUL, 0xb924d070UL, + 0xf815cb69UL, 0x3b46e642UL, 0x7a77fd5bUL, 0xb56b65dcUL, 0xf45a7ec5UL, + 0x370953eeUL, 0x763848f7UL, 0xb1ae09b8UL, 0xf09f12a1UL, 0x33cc3f8aUL, + 0x72fd2493UL + }, + { + 0x00000000UL, 0x376ac201UL, 0x6ed48403UL, 0x59be4602UL, 0xdca80907UL, + 0xebc2cb06UL, 0xb27c8d04UL, 0x85164f05UL, 0xb851130eUL, 0x8f3bd10fUL, + 0xd685970dUL, 0xe1ef550cUL, 0x64f91a09UL, 0x5393d808UL, 0x0a2d9e0aUL, + 0x3d475c0bUL, 0x70a3261cUL, 0x47c9e41dUL, 0x1e77a21fUL, 0x291d601eUL, + 0xac0b2f1bUL, 0x9b61ed1aUL, 0xc2dfab18UL, 0xf5b56919UL, 0xc8f23512UL, + 0xff98f713UL, 0xa626b111UL, 0x914c7310UL, 0x145a3c15UL, 0x2330fe14UL, + 0x7a8eb816UL, 0x4de47a17UL, 0xe0464d38UL, 0xd72c8f39UL, 0x8e92c93bUL, + 0xb9f80b3aUL, 0x3cee443fUL, 0x0b84863eUL, 0x523ac03cUL, 0x6550023dUL, + 0x58175e36UL, 0x6f7d9c37UL, 0x36c3da35UL, 0x01a91834UL, 0x84bf5731UL, + 0xb3d59530UL, 0xea6bd332UL, 0xdd011133UL, 0x90e56b24UL, 0xa78fa925UL, + 0xfe31ef27UL, 0xc95b2d26UL, 0x4c4d6223UL, 0x7b27a022UL, 0x2299e620UL, + 0x15f32421UL, 0x28b4782aUL, 0x1fdeba2bUL, 0x4660fc29UL, 0x710a3e28UL, + 0xf41c712dUL, 0xc376b32cUL, 0x9ac8f52eUL, 0xada2372fUL, 0xc08d9a70UL, + 0xf7e75871UL, 0xae591e73UL, 0x9933dc72UL, 0x1c259377UL, 0x2b4f5176UL, + 0x72f11774UL, 0x459bd575UL, 0x78dc897eUL, 0x4fb64b7fUL, 0x16080d7dUL, + 0x2162cf7cUL, 0xa4748079UL, 0x931e4278UL, 0xcaa0047aUL, 0xfdcac67bUL, + 0xb02ebc6cUL, 0x87447e6dUL, 0xdefa386fUL, 0xe990fa6eUL, 0x6c86b56bUL, + 0x5bec776aUL, 0x02523168UL, 0x3538f369UL, 0x087faf62UL, 0x3f156d63UL, + 0x66ab2b61UL, 0x51c1e960UL, 0xd4d7a665UL, 0xe3bd6464UL, 0xba032266UL, + 0x8d69e067UL, 0x20cbd748UL, 0x17a11549UL, 0x4e1f534bUL, 0x7975914aUL, + 0xfc63de4fUL, 0xcb091c4eUL, 0x92b75a4cUL, 0xa5dd984dUL, 0x989ac446UL, + 0xaff00647UL, 0xf64e4045UL, 0xc1248244UL, 0x4432cd41UL, 0x73580f40UL, + 0x2ae64942UL, 0x1d8c8b43UL, 0x5068f154UL, 0x67023355UL, 0x3ebc7557UL, + 0x09d6b756UL, 0x8cc0f853UL, 0xbbaa3a52UL, 0xe2147c50UL, 0xd57ebe51UL, + 0xe839e25aUL, 0xdf53205bUL, 0x86ed6659UL, 0xb187a458UL, 0x3491eb5dUL, + 0x03fb295cUL, 0x5a456f5eUL, 0x6d2fad5fUL, 0x801b35e1UL, 0xb771f7e0UL, + 0xeecfb1e2UL, 0xd9a573e3UL, 0x5cb33ce6UL, 0x6bd9fee7UL, 0x3267b8e5UL, + 0x050d7ae4UL, 0x384a26efUL, 0x0f20e4eeUL, 0x569ea2ecUL, 0x61f460edUL, + 0xe4e22fe8UL, 0xd388ede9UL, 0x8a36abebUL, 0xbd5c69eaUL, 0xf0b813fdUL, + 0xc7d2d1fcUL, 0x9e6c97feUL, 0xa90655ffUL, 0x2c101afaUL, 0x1b7ad8fbUL, + 0x42c49ef9UL, 0x75ae5cf8UL, 0x48e900f3UL, 0x7f83c2f2UL, 0x263d84f0UL, + 0x115746f1UL, 0x944109f4UL, 0xa32bcbf5UL, 0xfa958df7UL, 0xcdff4ff6UL, + 0x605d78d9UL, 0x5737bad8UL, 0x0e89fcdaUL, 0x39e33edbUL, 0xbcf571deUL, + 0x8b9fb3dfUL, 0xd221f5ddUL, 0xe54b37dcUL, 0xd80c6bd7UL, 0xef66a9d6UL, + 0xb6d8efd4UL, 0x81b22dd5UL, 0x04a462d0UL, 0x33cea0d1UL, 0x6a70e6d3UL, + 0x5d1a24d2UL, 0x10fe5ec5UL, 0x27949cc4UL, 0x7e2adac6UL, 0x494018c7UL, + 0xcc5657c2UL, 0xfb3c95c3UL, 0xa282d3c1UL, 0x95e811c0UL, 0xa8af4dcbUL, + 0x9fc58fcaUL, 0xc67bc9c8UL, 0xf1110bc9UL, 0x740744ccUL, 0x436d86cdUL, + 0x1ad3c0cfUL, 0x2db902ceUL, 0x4096af91UL, 0x77fc6d90UL, 0x2e422b92UL, + 0x1928e993UL, 0x9c3ea696UL, 0xab546497UL, 0xf2ea2295UL, 0xc580e094UL, + 0xf8c7bc9fUL, 0xcfad7e9eUL, 0x9613389cUL, 0xa179fa9dUL, 0x246fb598UL, + 0x13057799UL, 0x4abb319bUL, 0x7dd1f39aUL, 0x3035898dUL, 0x075f4b8cUL, + 0x5ee10d8eUL, 0x698bcf8fUL, 0xec9d808aUL, 0xdbf7428bUL, 0x82490489UL, + 0xb523c688UL, 0x88649a83UL, 0xbf0e5882UL, 0xe6b01e80UL, 0xd1dadc81UL, + 0x54cc9384UL, 0x63a65185UL, 0x3a181787UL, 0x0d72d586UL, 0xa0d0e2a9UL, + 0x97ba20a8UL, 0xce0466aaUL, 0xf96ea4abUL, 0x7c78ebaeUL, 0x4b1229afUL, + 0x12ac6fadUL, 0x25c6adacUL, 0x1881f1a7UL, 0x2feb33a6UL, 0x765575a4UL, + 0x413fb7a5UL, 0xc429f8a0UL, 0xf3433aa1UL, 0xaafd7ca3UL, 0x9d97bea2UL, + 0xd073c4b5UL, 0xe71906b4UL, 0xbea740b6UL, 0x89cd82b7UL, 0x0cdbcdb2UL, + 0x3bb10fb3UL, 0x620f49b1UL, 0x55658bb0UL, 0x6822d7bbUL, 0x5f4815baUL, + 0x06f653b8UL, 0x319c91b9UL, 0xb48adebcUL, 0x83e01cbdUL, 0xda5e5abfUL, + 0xed3498beUL + }, + { + 0x00000000UL, 0x6567bcb8UL, 0x8bc809aaUL, 0xeeafb512UL, 0x5797628fUL, + 0x32f0de37UL, 0xdc5f6b25UL, 0xb938d79dUL, 0xef28b4c5UL, 0x8a4f087dUL, + 0x64e0bd6fUL, 0x018701d7UL, 0xb8bfd64aUL, 0xddd86af2UL, 0x3377dfe0UL, + 0x56106358UL, 0x9f571950UL, 0xfa30a5e8UL, 0x149f10faUL, 0x71f8ac42UL, + 0xc8c07bdfUL, 0xada7c767UL, 0x43087275UL, 0x266fcecdUL, 0x707fad95UL, + 0x1518112dUL, 0xfbb7a43fUL, 0x9ed01887UL, 0x27e8cf1aUL, 0x428f73a2UL, + 0xac20c6b0UL, 0xc9477a08UL, 0x3eaf32a0UL, 0x5bc88e18UL, 0xb5673b0aUL, + 0xd00087b2UL, 0x6938502fUL, 0x0c5fec97UL, 0xe2f05985UL, 0x8797e53dUL, + 0xd1878665UL, 0xb4e03addUL, 0x5a4f8fcfUL, 0x3f283377UL, 0x8610e4eaUL, + 0xe3775852UL, 0x0dd8ed40UL, 0x68bf51f8UL, 0xa1f82bf0UL, 0xc49f9748UL, + 0x2a30225aUL, 0x4f579ee2UL, 0xf66f497fUL, 0x9308f5c7UL, 0x7da740d5UL, + 0x18c0fc6dUL, 0x4ed09f35UL, 0x2bb7238dUL, 0xc518969fUL, 0xa07f2a27UL, + 0x1947fdbaUL, 0x7c204102UL, 0x928ff410UL, 0xf7e848a8UL, 0x3d58149bUL, + 0x583fa823UL, 0xb6901d31UL, 0xd3f7a189UL, 0x6acf7614UL, 0x0fa8caacUL, + 0xe1077fbeUL, 0x8460c306UL, 0xd270a05eUL, 0xb7171ce6UL, 0x59b8a9f4UL, + 0x3cdf154cUL, 0x85e7c2d1UL, 0xe0807e69UL, 0x0e2fcb7bUL, 0x6b4877c3UL, + 0xa20f0dcbUL, 0xc768b173UL, 0x29c70461UL, 0x4ca0b8d9UL, 0xf5986f44UL, + 0x90ffd3fcUL, 0x7e5066eeUL, 0x1b37da56UL, 0x4d27b90eUL, 0x284005b6UL, + 0xc6efb0a4UL, 0xa3880c1cUL, 0x1ab0db81UL, 0x7fd76739UL, 0x9178d22bUL, + 0xf41f6e93UL, 0x03f7263bUL, 0x66909a83UL, 0x883f2f91UL, 0xed589329UL, + 0x546044b4UL, 0x3107f80cUL, 0xdfa84d1eUL, 0xbacff1a6UL, 0xecdf92feUL, + 0x89b82e46UL, 0x67179b54UL, 0x027027ecUL, 0xbb48f071UL, 0xde2f4cc9UL, + 0x3080f9dbUL, 0x55e74563UL, 0x9ca03f6bUL, 0xf9c783d3UL, 0x176836c1UL, + 0x720f8a79UL, 0xcb375de4UL, 0xae50e15cUL, 0x40ff544eUL, 0x2598e8f6UL, + 0x73888baeUL, 0x16ef3716UL, 0xf8408204UL, 0x9d273ebcUL, 0x241fe921UL, + 0x41785599UL, 0xafd7e08bUL, 0xcab05c33UL, 0x3bb659edUL, 0x5ed1e555UL, + 0xb07e5047UL, 0xd519ecffUL, 0x6c213b62UL, 0x094687daUL, 0xe7e932c8UL, + 0x828e8e70UL, 0xd49eed28UL, 0xb1f95190UL, 0x5f56e482UL, 0x3a31583aUL, + 0x83098fa7UL, 0xe66e331fUL, 0x08c1860dUL, 0x6da63ab5UL, 0xa4e140bdUL, + 0xc186fc05UL, 0x2f294917UL, 0x4a4ef5afUL, 0xf3762232UL, 0x96119e8aUL, + 0x78be2b98UL, 0x1dd99720UL, 0x4bc9f478UL, 0x2eae48c0UL, 0xc001fdd2UL, + 0xa566416aUL, 0x1c5e96f7UL, 0x79392a4fUL, 0x97969f5dUL, 0xf2f123e5UL, + 0x05196b4dUL, 0x607ed7f5UL, 0x8ed162e7UL, 0xebb6de5fUL, 0x528e09c2UL, + 0x37e9b57aUL, 0xd9460068UL, 0xbc21bcd0UL, 0xea31df88UL, 0x8f566330UL, + 0x61f9d622UL, 0x049e6a9aUL, 0xbda6bd07UL, 0xd8c101bfUL, 0x366eb4adUL, + 0x53090815UL, 0x9a4e721dUL, 0xff29cea5UL, 0x11867bb7UL, 0x74e1c70fUL, + 0xcdd91092UL, 0xa8beac2aUL, 0x46111938UL, 0x2376a580UL, 0x7566c6d8UL, + 0x10017a60UL, 0xfeaecf72UL, 0x9bc973caUL, 0x22f1a457UL, 0x479618efUL, + 0xa939adfdUL, 0xcc5e1145UL, 0x06ee4d76UL, 0x6389f1ceUL, 0x8d2644dcUL, + 0xe841f864UL, 0x51792ff9UL, 0x341e9341UL, 0xdab12653UL, 0xbfd69aebUL, + 0xe9c6f9b3UL, 0x8ca1450bUL, 0x620ef019UL, 0x07694ca1UL, 0xbe519b3cUL, + 0xdb362784UL, 0x35999296UL, 0x50fe2e2eUL, 0x99b95426UL, 0xfcdee89eUL, + 0x12715d8cUL, 0x7716e134UL, 0xce2e36a9UL, 0xab498a11UL, 0x45e63f03UL, + 0x208183bbUL, 0x7691e0e3UL, 0x13f65c5bUL, 0xfd59e949UL, 0x983e55f1UL, + 0x2106826cUL, 0x44613ed4UL, 0xaace8bc6UL, 0xcfa9377eUL, 0x38417fd6UL, + 0x5d26c36eUL, 0xb389767cUL, 0xd6eecac4UL, 0x6fd61d59UL, 0x0ab1a1e1UL, + 0xe41e14f3UL, 0x8179a84bUL, 0xd769cb13UL, 0xb20e77abUL, 0x5ca1c2b9UL, + 0x39c67e01UL, 0x80fea99cUL, 0xe5991524UL, 0x0b36a036UL, 0x6e511c8eUL, + 0xa7166686UL, 0xc271da3eUL, 0x2cde6f2cUL, 0x49b9d394UL, 0xf0810409UL, + 0x95e6b8b1UL, 0x7b490da3UL, 0x1e2eb11bUL, 0x483ed243UL, 0x2d596efbUL, + 0xc3f6dbe9UL, 0xa6916751UL, 0x1fa9b0ccUL, 0x7ace0c74UL, 0x9461b966UL, + 0xf10605deUL +#endif + } +}; diff --git a/third_party/java_tools/zlib/deflate.c b/third_party/java_tools/zlib/deflate.c new file mode 100644 index 000000000..1ec761448 --- /dev/null +++ b/third_party/java_tools/zlib/deflate.c @@ -0,0 +1,2163 @@ +/* deflate.c -- compress data using the deflation algorithm + * Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* + * ALGORITHM + * + * The "deflation" process depends on being able to identify portions + * of the input text which are identical to earlier input (within a + * sliding window trailing behind the input currently being processed). + * + * The most straightforward technique turns out to be the fastest for + * most input files: try all possible matches and select the longest. + * The key feature of this algorithm is that insertions into the string + * dictionary are very simple and thus fast, and deletions are avoided + * completely. Insertions are performed at each input character, whereas + * string matches are performed only when the previous match ends. So it + * is preferable to spend more time in matches to allow very fast string + * insertions and avoid deletions. The matching algorithm for small + * strings is inspired from that of Rabin & Karp. A brute force approach + * is used to find longer strings when a small match has been found. + * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze + * (by Leonid Broukhis). + * A previous version of this file used a more sophisticated algorithm + * (by Fiala and Greene) which is guaranteed to run in linear amortized + * time, but has a larger average cost, uses more memory and is patented. + * However the F&G algorithm may be faster for some highly redundant + * files if the parameter max_chain_length (described below) is too large. + * + * ACKNOWLEDGEMENTS + * + * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and + * I found it in 'freeze' written by Leonid Broukhis. + * Thanks to many people for bug reports and testing. + * + * REFERENCES + * + * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification". + * Available in http://tools.ietf.org/html/rfc1951 + * + * A description of the Rabin and Karp algorithm is given in the book + * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. + * + * Fiala,E.R., and Greene,D.H. + * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 + * + */ + +/* @(#) $Id$ */ + +#include "deflate.h" + +const char deflate_copyright[] = + " deflate 1.2.11 Copyright 1995-2017 Jean-loup Gailly and Mark Adler "; +/* + If you use the zlib library in a product, an acknowledgment is welcome + in the documentation of your product. If for some reason you cannot + include such an acknowledgment, I would appreciate that you keep this + copyright string in the executable of your product. + */ + +/* =========================================================================== + * Function prototypes. + */ +typedef enum { + need_more, /* block not completed, need more input or more output */ + block_done, /* block flush performed */ + finish_started, /* finish started, need only more output at next deflate */ + finish_done /* finish done, accept no more input or output */ +} block_state; + +typedef block_state (*compress_func) OF((deflate_state *s, int flush)); +/* Compression function. Returns the block state after the call. */ + +local int deflateStateCheck OF((z_streamp strm)); +local void slide_hash OF((deflate_state *s)); +local void fill_window OF((deflate_state *s)); +local block_state deflate_stored OF((deflate_state *s, int flush)); +local block_state deflate_fast OF((deflate_state *s, int flush)); +#ifndef FASTEST +local block_state deflate_slow OF((deflate_state *s, int flush)); +#endif +local block_state deflate_rle OF((deflate_state *s, int flush)); +local block_state deflate_huff OF((deflate_state *s, int flush)); +local void lm_init OF((deflate_state *s)); +local void putShortMSB OF((deflate_state *s, uInt b)); +local void flush_pending OF((z_streamp strm)); +local unsigned read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); +#ifdef ASMV +# pragma message("Assembler code may have bugs -- use at your own risk") + void match_init OF((void)); /* asm code initialization */ + uInt longest_match OF((deflate_state *s, IPos cur_match)); +#else +local uInt longest_match OF((deflate_state *s, IPos cur_match)); +#endif + +#ifdef ZLIB_DEBUG +local void check_match OF((deflate_state *s, IPos start, IPos match, + int length)); +#endif + +/* =========================================================================== + * Local data + */ + +#define NIL 0 +/* Tail of hash chains */ + +#ifndef TOO_FAR +# define TOO_FAR 4096 +#endif +/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */ + +/* Values for max_lazy_match, good_match and max_chain_length, depending on + * the desired pack level (0..9). The values given below have been tuned to + * exclude worst case performance for pathological files. Better values may be + * found for specific files. + */ +typedef struct config_s { + ush good_length; /* reduce lazy search above this match length */ + ush max_lazy; /* do not perform lazy search above this match length */ + ush nice_length; /* quit search above this match length */ + ush max_chain; + compress_func func; +} config; + +#ifdef FASTEST +local const config configuration_table[2] = { +/* good lazy nice chain */ +/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ +/* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */ +#else +local const config configuration_table[10] = { +/* good lazy nice chain */ +/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ +/* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */ +/* 2 */ {4, 5, 16, 8, deflate_fast}, +/* 3 */ {4, 6, 32, 32, deflate_fast}, + +/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */ +/* 5 */ {8, 16, 32, 32, deflate_slow}, +/* 6 */ {8, 16, 128, 128, deflate_slow}, +/* 7 */ {8, 32, 128, 256, deflate_slow}, +/* 8 */ {32, 128, 258, 1024, deflate_slow}, +/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */ +#endif + +/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 + * For deflate_fast() (levels <= 3) good is ignored and lazy has a different + * meaning. + */ + +/* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */ +#define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0)) + +/* =========================================================================== + * Update a hash value with the given input byte + * IN assertion: all calls to UPDATE_HASH are made with consecutive input + * characters, so that a running hash key can be computed from the previous + * key instead of complete recalculation each time. + */ +#define UPDATE_HASH(s,h,c) (h = (((h)<hash_shift) ^ (c)) & s->hash_mask) + + +/* =========================================================================== + * Insert string str in the dictionary and set match_head to the previous head + * of the hash chain (the most recent string with same hash key). Return + * the previous length of the hash chain. + * If this file is compiled with -DFASTEST, the compression level is forced + * to 1, and no hash chains are maintained. + * IN assertion: all calls to INSERT_STRING are made with consecutive input + * characters and the first MIN_MATCH bytes of str are valid (except for + * the last MIN_MATCH-1 bytes of the input file). + */ +#ifdef FASTEST +#define INSERT_STRING(s, str, match_head) \ + (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ + match_head = s->head[s->ins_h], \ + s->head[s->ins_h] = (Pos)(str)) +#else +#define INSERT_STRING(s, str, match_head) \ + (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ + match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \ + s->head[s->ins_h] = (Pos)(str)) +#endif + +/* =========================================================================== + * Initialize the hash table (avoiding 64K overflow for 16 bit systems). + * prev[] will be initialized on the fly. + */ +#define CLEAR_HASH(s) \ + s->head[s->hash_size-1] = NIL; \ + zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); + +/* =========================================================================== + * Slide the hash table when sliding the window down (could be avoided with 32 + * bit values at the expense of memory usage). We slide even when level == 0 to + * keep the hash table consistent if we switch back to level > 0 later. + */ +local void slide_hash(s) + deflate_state *s; +{ + unsigned n, m; + Posf *p; + uInt wsize = s->w_size; + + n = s->hash_size; + p = &s->head[n]; + do { + m = *--p; + *p = (Pos)(m >= wsize ? m - wsize : NIL); + } while (--n); + n = wsize; +#ifndef FASTEST + p = &s->prev[n]; + do { + m = *--p; + *p = (Pos)(m >= wsize ? m - wsize : NIL); + /* If n is not on any hash chain, prev[n] is garbage but + * its value will never be used. + */ + } while (--n); +#endif +} + +/* ========================================================================= */ +int ZEXPORT deflateInit_(strm, level, version, stream_size) + z_streamp strm; + int level; + const char *version; + int stream_size; +{ + return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, + Z_DEFAULT_STRATEGY, version, stream_size); + /* To do: ignore strm->next_in if we use it as window */ +} + +/* ========================================================================= */ +int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, + version, stream_size) + z_streamp strm; + int level; + int method; + int windowBits; + int memLevel; + int strategy; + const char *version; + int stream_size; +{ + deflate_state *s; + int wrap = 1; + static const char my_version[] = ZLIB_VERSION; + + ushf *overlay; + /* We overlay pending_buf and d_buf+l_buf. This works since the average + * output size for (length,distance) codes is <= 24 bits. + */ + + if (version == Z_NULL || version[0] != my_version[0] || + stream_size != sizeof(z_stream)) { + return Z_VERSION_ERROR; + } + if (strm == Z_NULL) return Z_STREAM_ERROR; + + strm->msg = Z_NULL; + if (strm->zalloc == (alloc_func)0) { +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else + strm->zalloc = zcalloc; + strm->opaque = (voidpf)0; +#endif + } + if (strm->zfree == (free_func)0) +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else + strm->zfree = zcfree; +#endif + +#ifdef FASTEST + if (level != 0) level = 1; +#else + if (level == Z_DEFAULT_COMPRESSION) level = 6; +#endif + + if (windowBits < 0) { /* suppress zlib wrapper */ + wrap = 0; + windowBits = -windowBits; + } +#ifdef GZIP + else if (windowBits > 15) { + wrap = 2; /* write gzip wrapper instead */ + windowBits -= 16; + } +#endif + if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || + windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || + strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) { + return Z_STREAM_ERROR; + } + if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */ + s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); + if (s == Z_NULL) return Z_MEM_ERROR; + strm->state = (struct internal_state FAR *)s; + s->strm = strm; + s->status = INIT_STATE; /* to pass state test in deflateReset() */ + + s->wrap = wrap; + s->gzhead = Z_NULL; + s->w_bits = (uInt)windowBits; + s->w_size = 1 << s->w_bits; + s->w_mask = s->w_size - 1; + + s->hash_bits = (uInt)memLevel + 7; + s->hash_size = 1 << s->hash_bits; + s->hash_mask = s->hash_size - 1; + s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); + + s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); + s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); + s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); + + s->high_water = 0; /* nothing written to s->window yet */ + + s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ + + overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); + s->pending_buf = (uchf *) overlay; + s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); + + if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || + s->pending_buf == Z_NULL) { + s->status = FINISH_STATE; + strm->msg = ERR_MSG(Z_MEM_ERROR); + deflateEnd (strm); + return Z_MEM_ERROR; + } + s->d_buf = overlay + s->lit_bufsize/sizeof(ush); + s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; + + s->level = level; + s->strategy = strategy; + s->method = (Byte)method; + + return deflateReset(strm); +} + +/* ========================================================================= + * Check for a valid deflate stream state. Return 0 if ok, 1 if not. + */ +local int deflateStateCheck (strm) + z_streamp strm; +{ + deflate_state *s; + if (strm == Z_NULL || + strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) + return 1; + s = strm->state; + if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE && +#ifdef GZIP + s->status != GZIP_STATE && +#endif + s->status != EXTRA_STATE && + s->status != NAME_STATE && + s->status != COMMENT_STATE && + s->status != HCRC_STATE && + s->status != BUSY_STATE && + s->status != FINISH_STATE)) + return 1; + return 0; +} + +/* ========================================================================= */ +int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) + z_streamp strm; + const Bytef *dictionary; + uInt dictLength; +{ + deflate_state *s; + uInt str, n; + int wrap; + unsigned avail; + z_const unsigned char *next; + + if (deflateStateCheck(strm) || dictionary == Z_NULL) + return Z_STREAM_ERROR; + s = strm->state; + wrap = s->wrap; + if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead) + return Z_STREAM_ERROR; + + /* when using zlib wrappers, compute Adler-32 for provided dictionary */ + if (wrap == 1) + strm->adler = adler32(strm->adler, dictionary, dictLength); + s->wrap = 0; /* avoid computing Adler-32 in read_buf */ + + /* if dictionary would fill window, just replace the history */ + if (dictLength >= s->w_size) { + if (wrap == 0) { /* already empty otherwise */ + CLEAR_HASH(s); + s->strstart = 0; + s->block_start = 0L; + s->insert = 0; + } + dictionary += dictLength - s->w_size; /* use the tail */ + dictLength = s->w_size; + } + + /* insert dictionary into window and hash */ + avail = strm->avail_in; + next = strm->next_in; + strm->avail_in = dictLength; + strm->next_in = (z_const Bytef *)dictionary; + fill_window(s); + while (s->lookahead >= MIN_MATCH) { + str = s->strstart; + n = s->lookahead - (MIN_MATCH-1); + do { + UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); +#ifndef FASTEST + s->prev[str & s->w_mask] = s->head[s->ins_h]; +#endif + s->head[s->ins_h] = (Pos)str; + str++; + } while (--n); + s->strstart = str; + s->lookahead = MIN_MATCH-1; + fill_window(s); + } + s->strstart += s->lookahead; + s->block_start = (long)s->strstart; + s->insert = s->lookahead; + s->lookahead = 0; + s->match_length = s->prev_length = MIN_MATCH-1; + s->match_available = 0; + strm->next_in = next; + strm->avail_in = avail; + s->wrap = wrap; + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflateGetDictionary (strm, dictionary, dictLength) + z_streamp strm; + Bytef *dictionary; + uInt *dictLength; +{ + deflate_state *s; + uInt len; + + if (deflateStateCheck(strm)) + return Z_STREAM_ERROR; + s = strm->state; + len = s->strstart + s->lookahead; + if (len > s->w_size) + len = s->w_size; + if (dictionary != Z_NULL && len) + zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len); + if (dictLength != Z_NULL) + *dictLength = len; + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflateResetKeep (strm) + z_streamp strm; +{ + deflate_state *s; + + if (deflateStateCheck(strm)) { + return Z_STREAM_ERROR; + } + + strm->total_in = strm->total_out = 0; + strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */ + strm->data_type = Z_UNKNOWN; + + s = (deflate_state *)strm->state; + s->pending = 0; + s->pending_out = s->pending_buf; + + if (s->wrap < 0) { + s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */ + } + s->status = +#ifdef GZIP + s->wrap == 2 ? GZIP_STATE : +#endif + s->wrap ? INIT_STATE : BUSY_STATE; + strm->adler = +#ifdef GZIP + s->wrap == 2 ? crc32(0L, Z_NULL, 0) : +#endif + adler32(0L, Z_NULL, 0); + s->last_flush = Z_NO_FLUSH; + + _tr_init(s); + + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflateReset (strm) + z_streamp strm; +{ + int ret; + + ret = deflateResetKeep(strm); + if (ret == Z_OK) + lm_init(strm->state); + return ret; +} + +/* ========================================================================= */ +int ZEXPORT deflateSetHeader (strm, head) + z_streamp strm; + gz_headerp head; +{ + if (deflateStateCheck(strm) || strm->state->wrap != 2) + return Z_STREAM_ERROR; + strm->state->gzhead = head; + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflatePending (strm, pending, bits) + unsigned *pending; + int *bits; + z_streamp strm; +{ + if (deflateStateCheck(strm)) return Z_STREAM_ERROR; + if (pending != Z_NULL) + *pending = strm->state->pending; + if (bits != Z_NULL) + *bits = strm->state->bi_valid; + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflatePrime (strm, bits, value) + z_streamp strm; + int bits; + int value; +{ + deflate_state *s; + int put; + + if (deflateStateCheck(strm)) return Z_STREAM_ERROR; + s = strm->state; + if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3)) + return Z_BUF_ERROR; + do { + put = Buf_size - s->bi_valid; + if (put > bits) + put = bits; + s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid); + s->bi_valid += put; + _tr_flush_bits(s); + value >>= put; + bits -= put; + } while (bits); + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflateParams(strm, level, strategy) + z_streamp strm; + int level; + int strategy; +{ + deflate_state *s; + compress_func func; + + if (deflateStateCheck(strm)) return Z_STREAM_ERROR; + s = strm->state; + +#ifdef FASTEST + if (level != 0) level = 1; +#else + if (level == Z_DEFAULT_COMPRESSION) level = 6; +#endif + if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { + return Z_STREAM_ERROR; + } + func = configuration_table[s->level].func; + + if ((strategy != s->strategy || func != configuration_table[level].func) && + s->high_water) { + /* Flush the last buffer: */ + int err = deflate(strm, Z_BLOCK); + if (err == Z_STREAM_ERROR) + return err; + if (strm->avail_out == 0) + return Z_BUF_ERROR; + } + if (s->level != level) { + if (s->level == 0 && s->matches != 0) { + if (s->matches == 1) + slide_hash(s); + else + CLEAR_HASH(s); + s->matches = 0; + } + s->level = level; + s->max_lazy_match = configuration_table[level].max_lazy; + s->good_match = configuration_table[level].good_length; + s->nice_match = configuration_table[level].nice_length; + s->max_chain_length = configuration_table[level].max_chain; + } + s->strategy = strategy; + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain) + z_streamp strm; + int good_length; + int max_lazy; + int nice_length; + int max_chain; +{ + deflate_state *s; + + if (deflateStateCheck(strm)) return Z_STREAM_ERROR; + s = strm->state; + s->good_match = (uInt)good_length; + s->max_lazy_match = (uInt)max_lazy; + s->nice_match = nice_length; + s->max_chain_length = (uInt)max_chain; + return Z_OK; +} + +/* ========================================================================= + * For the default windowBits of 15 and memLevel of 8, this function returns + * a close to exact, as well as small, upper bound on the compressed size. + * They are coded as constants here for a reason--if the #define's are + * changed, then this function needs to be changed as well. The return + * value for 15 and 8 only works for those exact settings. + * + * For any setting other than those defaults for windowBits and memLevel, + * the value returned is a conservative worst case for the maximum expansion + * resulting from using fixed blocks instead of stored blocks, which deflate + * can emit on compressed data for some combinations of the parameters. + * + * This function could be more sophisticated to provide closer upper bounds for + * every combination of windowBits and memLevel. But even the conservative + * upper bound of about 14% expansion does not seem onerous for output buffer + * allocation. + */ +uLong ZEXPORT deflateBound(strm, sourceLen) + z_streamp strm; + uLong sourceLen; +{ + deflate_state *s; + uLong complen, wraplen; + + /* conservative upper bound for compressed data */ + complen = sourceLen + + ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5; + + /* if can't get parameters, return conservative bound plus zlib wrapper */ + if (deflateStateCheck(strm)) + return complen + 6; + + /* compute wrapper length */ + s = strm->state; + switch (s->wrap) { + case 0: /* raw deflate */ + wraplen = 0; + break; + case 1: /* zlib wrapper */ + wraplen = 6 + (s->strstart ? 4 : 0); + break; +#ifdef GZIP + case 2: /* gzip wrapper */ + wraplen = 18; + if (s->gzhead != Z_NULL) { /* user-supplied gzip header */ + Bytef *str; + if (s->gzhead->extra != Z_NULL) + wraplen += 2 + s->gzhead->extra_len; + str = s->gzhead->name; + if (str != Z_NULL) + do { + wraplen++; + } while (*str++); + str = s->gzhead->comment; + if (str != Z_NULL) + do { + wraplen++; + } while (*str++); + if (s->gzhead->hcrc) + wraplen += 2; + } + break; +#endif + default: /* for compiler happiness */ + wraplen = 6; + } + + /* if not default parameters, return conservative bound */ + if (s->w_bits != 15 || s->hash_bits != 8 + 7) + return complen + wraplen; + + /* default settings: return tight bound for that case */ + return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + + (sourceLen >> 25) + 13 - 6 + wraplen; +} + +/* ========================================================================= + * Put a short in the pending buffer. The 16-bit value is put in MSB order. + * IN assertion: the stream state is correct and there is enough room in + * pending_buf. + */ +local void putShortMSB (s, b) + deflate_state *s; + uInt b; +{ + put_byte(s, (Byte)(b >> 8)); + put_byte(s, (Byte)(b & 0xff)); +} + +/* ========================================================================= + * Flush as much pending output as possible. All deflate() output, except for + * some deflate_stored() output, goes through this function so some + * applications may wish to modify it to avoid allocating a large + * strm->next_out buffer and copying into it. (See also read_buf()). + */ +local void flush_pending(strm) + z_streamp strm; +{ + unsigned len; + deflate_state *s = strm->state; + + _tr_flush_bits(s); + len = s->pending; + if (len > strm->avail_out) len = strm->avail_out; + if (len == 0) return; + + zmemcpy(strm->next_out, s->pending_out, len); + strm->next_out += len; + s->pending_out += len; + strm->total_out += len; + strm->avail_out -= len; + s->pending -= len; + if (s->pending == 0) { + s->pending_out = s->pending_buf; + } +} + +/* =========================================================================== + * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1]. + */ +#define HCRC_UPDATE(beg) \ + do { \ + if (s->gzhead->hcrc && s->pending > (beg)) \ + strm->adler = crc32(strm->adler, s->pending_buf + (beg), \ + s->pending - (beg)); \ + } while (0) + +/* ========================================================================= */ +int ZEXPORT deflate (strm, flush) + z_streamp strm; + int flush; +{ + int old_flush; /* value of flush param for previous deflate call */ + deflate_state *s; + + if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) { + return Z_STREAM_ERROR; + } + s = strm->state; + + if (strm->next_out == Z_NULL || + (strm->avail_in != 0 && strm->next_in == Z_NULL) || + (s->status == FINISH_STATE && flush != Z_FINISH)) { + ERR_RETURN(strm, Z_STREAM_ERROR); + } + if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); + + old_flush = s->last_flush; + s->last_flush = flush; + + /* Flush as much pending output as possible */ + if (s->pending != 0) { + flush_pending(strm); + if (strm->avail_out == 0) { + /* Since avail_out is 0, deflate will be called again with + * more output space, but possibly with both pending and + * avail_in equal to zero. There won't be anything to do, + * but this is not an error situation so make sure we + * return OK instead of BUF_ERROR at next call of deflate: + */ + s->last_flush = -1; + return Z_OK; + } + + /* Make sure there is something to do and avoid duplicate consecutive + * flushes. For repeated and useless calls with Z_FINISH, we keep + * returning Z_STREAM_END instead of Z_BUF_ERROR. + */ + } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && + flush != Z_FINISH) { + ERR_RETURN(strm, Z_BUF_ERROR); + } + + /* User must not provide more input after the first FINISH: */ + if (s->status == FINISH_STATE && strm->avail_in != 0) { + ERR_RETURN(strm, Z_BUF_ERROR); + } + + /* Write the header */ + if (s->status == INIT_STATE) { + /* zlib header */ + uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8; + uInt level_flags; + + if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) + level_flags = 0; + else if (s->level < 6) + level_flags = 1; + else if (s->level == 6) + level_flags = 2; + else + level_flags = 3; + header |= (level_flags << 6); + if (s->strstart != 0) header |= PRESET_DICT; + header += 31 - (header % 31); + + putShortMSB(s, header); + + /* Save the adler32 of the preset dictionary: */ + if (s->strstart != 0) { + putShortMSB(s, (uInt)(strm->adler >> 16)); + putShortMSB(s, (uInt)(strm->adler & 0xffff)); + } + strm->adler = adler32(0L, Z_NULL, 0); + s->status = BUSY_STATE; + + /* Compression must start with an empty pending buffer */ + flush_pending(strm); + if (s->pending != 0) { + s->last_flush = -1; + return Z_OK; + } + } +#ifdef GZIP + if (s->status == GZIP_STATE) { + /* gzip header */ + strm->adler = crc32(0L, Z_NULL, 0); + put_byte(s, 31); + put_byte(s, 139); + put_byte(s, 8); + if (s->gzhead == Z_NULL) { + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, s->level == 9 ? 2 : + (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? + 4 : 0)); + put_byte(s, OS_CODE); + s->status = BUSY_STATE; + + /* Compression must start with an empty pending buffer */ + flush_pending(strm); + if (s->pending != 0) { + s->last_flush = -1; + return Z_OK; + } + } + else { + put_byte(s, (s->gzhead->text ? 1 : 0) + + (s->gzhead->hcrc ? 2 : 0) + + (s->gzhead->extra == Z_NULL ? 0 : 4) + + (s->gzhead->name == Z_NULL ? 0 : 8) + + (s->gzhead->comment == Z_NULL ? 0 : 16) + ); + put_byte(s, (Byte)(s->gzhead->time & 0xff)); + put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff)); + put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff)); + put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff)); + put_byte(s, s->level == 9 ? 2 : + (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? + 4 : 0)); + put_byte(s, s->gzhead->os & 0xff); + if (s->gzhead->extra != Z_NULL) { + put_byte(s, s->gzhead->extra_len & 0xff); + put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); + } + if (s->gzhead->hcrc) + strm->adler = crc32(strm->adler, s->pending_buf, + s->pending); + s->gzindex = 0; + s->status = EXTRA_STATE; + } + } + if (s->status == EXTRA_STATE) { + if (s->gzhead->extra != Z_NULL) { + ulg beg = s->pending; /* start of bytes to update crc */ + uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex; + while (s->pending + left > s->pending_buf_size) { + uInt copy = s->pending_buf_size - s->pending; + zmemcpy(s->pending_buf + s->pending, + s->gzhead->extra + s->gzindex, copy); + s->pending = s->pending_buf_size; + HCRC_UPDATE(beg); + s->gzindex += copy; + flush_pending(strm); + if (s->pending != 0) { + s->last_flush = -1; + return Z_OK; + } + beg = 0; + left -= copy; + } + zmemcpy(s->pending_buf + s->pending, + s->gzhead->extra + s->gzindex, left); + s->pending += left; + HCRC_UPDATE(beg); + s->gzindex = 0; + } + s->status = NAME_STATE; + } + if (s->status == NAME_STATE) { + if (s->gzhead->name != Z_NULL) { + ulg beg = s->pending; /* start of bytes to update crc */ + int val; + do { + if (s->pending == s->pending_buf_size) { + HCRC_UPDATE(beg); + flush_pending(strm); + if (s->pending != 0) { + s->last_flush = -1; + return Z_OK; + } + beg = 0; + } + val = s->gzhead->name[s->gzindex++]; + put_byte(s, val); + } while (val != 0); + HCRC_UPDATE(beg); + s->gzindex = 0; + } + s->status = COMMENT_STATE; + } + if (s->status == COMMENT_STATE) { + if (s->gzhead->comment != Z_NULL) { + ulg beg = s->pending; /* start of bytes to update crc */ + int val; + do { + if (s->pending == s->pending_buf_size) { + HCRC_UPDATE(beg); + flush_pending(strm); + if (s->pending != 0) { + s->last_flush = -1; + return Z_OK; + } + beg = 0; + } + val = s->gzhead->comment[s->gzindex++]; + put_byte(s, val); + } while (val != 0); + HCRC_UPDATE(beg); + } + s->status = HCRC_STATE; + } + if (s->status == HCRC_STATE) { + if (s->gzhead->hcrc) { + if (s->pending + 2 > s->pending_buf_size) { + flush_pending(strm); + if (s->pending != 0) { + s->last_flush = -1; + return Z_OK; + } + } + put_byte(s, (Byte)(strm->adler & 0xff)); + put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); + strm->adler = crc32(0L, Z_NULL, 0); + } + s->status = BUSY_STATE; + + /* Compression must start with an empty pending buffer */ + flush_pending(strm); + if (s->pending != 0) { + s->last_flush = -1; + return Z_OK; + } + } +#endif + + /* Start a new block or continue the current one. + */ + if (strm->avail_in != 0 || s->lookahead != 0 || + (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { + block_state bstate; + + bstate = s->level == 0 ? deflate_stored(s, flush) : + s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : + s->strategy == Z_RLE ? deflate_rle(s, flush) : + (*(configuration_table[s->level].func))(s, flush); + + if (bstate == finish_started || bstate == finish_done) { + s->status = FINISH_STATE; + } + if (bstate == need_more || bstate == finish_started) { + if (strm->avail_out == 0) { + s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ + } + return Z_OK; + /* If flush != Z_NO_FLUSH && avail_out == 0, the next call + * of deflate should use the same flush parameter to make sure + * that the flush is complete. So we don't have to output an + * empty block here, this will be done at next call. This also + * ensures that for a very small output buffer, we emit at most + * one empty block. + */ + } + if (bstate == block_done) { + if (flush == Z_PARTIAL_FLUSH) { + _tr_align(s); + } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ + _tr_stored_block(s, (char*)0, 0L, 0); + /* For a full flush, this empty block will be recognized + * as a special marker by inflate_sync(). + */ + if (flush == Z_FULL_FLUSH) { + CLEAR_HASH(s); /* forget history */ + if (s->lookahead == 0) { + s->strstart = 0; + s->block_start = 0L; + s->insert = 0; + } + } + } + flush_pending(strm); + if (strm->avail_out == 0) { + s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ + return Z_OK; + } + } + } + + if (flush != Z_FINISH) return Z_OK; + if (s->wrap <= 0) return Z_STREAM_END; + + /* Write the trailer */ +#ifdef GZIP + if (s->wrap == 2) { + put_byte(s, (Byte)(strm->adler & 0xff)); + put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); + put_byte(s, (Byte)((strm->adler >> 16) & 0xff)); + put_byte(s, (Byte)((strm->adler >> 24) & 0xff)); + put_byte(s, (Byte)(strm->total_in & 0xff)); + put_byte(s, (Byte)((strm->total_in >> 8) & 0xff)); + put_byte(s, (Byte)((strm->total_in >> 16) & 0xff)); + put_byte(s, (Byte)((strm->total_in >> 24) & 0xff)); + } + else +#endif + { + putShortMSB(s, (uInt)(strm->adler >> 16)); + putShortMSB(s, (uInt)(strm->adler & 0xffff)); + } + flush_pending(strm); + /* If avail_out is zero, the application will call deflate again + * to flush the rest. + */ + if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */ + return s->pending != 0 ? Z_OK : Z_STREAM_END; +} + +/* ========================================================================= */ +int ZEXPORT deflateEnd (strm) + z_streamp strm; +{ + int status; + + if (deflateStateCheck(strm)) return Z_STREAM_ERROR; + + status = strm->state->status; + + /* Deallocate in reverse order of allocations: */ + TRY_FREE(strm, strm->state->pending_buf); + TRY_FREE(strm, strm->state->head); + TRY_FREE(strm, strm->state->prev); + TRY_FREE(strm, strm->state->window); + + ZFREE(strm, strm->state); + strm->state = Z_NULL; + + return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK; +} + +/* ========================================================================= + * Copy the source state to the destination state. + * To simplify the source, this is not supported for 16-bit MSDOS (which + * doesn't have enough memory anyway to duplicate compression states). + */ +int ZEXPORT deflateCopy (dest, source) + z_streamp dest; + z_streamp source; +{ +#ifdef MAXSEG_64K + return Z_STREAM_ERROR; +#else + deflate_state *ds; + deflate_state *ss; + ushf *overlay; + + + if (deflateStateCheck(source) || dest == Z_NULL) { + return Z_STREAM_ERROR; + } + + ss = source->state; + + zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); + + ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); + if (ds == Z_NULL) return Z_MEM_ERROR; + dest->state = (struct internal_state FAR *) ds; + zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state)); + ds->strm = dest; + + ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); + ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); + ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); + overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2); + ds->pending_buf = (uchf *) overlay; + + if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || + ds->pending_buf == Z_NULL) { + deflateEnd (dest); + return Z_MEM_ERROR; + } + /* following zmemcpy do not work for 16-bit MSDOS */ + zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); + zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos)); + zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos)); + zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size); + + ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); + ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush); + ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize; + + ds->l_desc.dyn_tree = ds->dyn_ltree; + ds->d_desc.dyn_tree = ds->dyn_dtree; + ds->bl_desc.dyn_tree = ds->bl_tree; + + return Z_OK; +#endif /* MAXSEG_64K */ +} + +/* =========================================================================== + * Read a new buffer from the current input stream, update the adler32 + * and total number of bytes read. All deflate() input goes through + * this function so some applications may wish to modify it to avoid + * allocating a large strm->next_in buffer and copying from it. + * (See also flush_pending()). + */ +local unsigned read_buf(strm, buf, size) + z_streamp strm; + Bytef *buf; + unsigned size; +{ + unsigned len = strm->avail_in; + + if (len > size) len = size; + if (len == 0) return 0; + + strm->avail_in -= len; + + zmemcpy(buf, strm->next_in, len); + if (strm->state->wrap == 1) { + strm->adler = adler32(strm->adler, buf, len); + } +#ifdef GZIP + else if (strm->state->wrap == 2) { + strm->adler = crc32(strm->adler, buf, len); + } +#endif + strm->next_in += len; + strm->total_in += len; + + return len; +} + +/* =========================================================================== + * Initialize the "longest match" routines for a new zlib stream + */ +local void lm_init (s) + deflate_state *s; +{ + s->window_size = (ulg)2L*s->w_size; + + CLEAR_HASH(s); + + /* Set the default configuration parameters: + */ + s->max_lazy_match = configuration_table[s->level].max_lazy; + s->good_match = configuration_table[s->level].good_length; + s->nice_match = configuration_table[s->level].nice_length; + s->max_chain_length = configuration_table[s->level].max_chain; + + s->strstart = 0; + s->block_start = 0L; + s->lookahead = 0; + s->insert = 0; + s->match_length = s->prev_length = MIN_MATCH-1; + s->match_available = 0; + s->ins_h = 0; +#ifndef FASTEST +#ifdef ASMV + match_init(); /* initialize the asm code */ +#endif +#endif +} + +#ifndef FASTEST +/* =========================================================================== + * Set match_start to the longest match starting at the given string and + * return its length. Matches shorter or equal to prev_length are discarded, + * in which case the result is equal to prev_length and match_start is + * garbage. + * IN assertions: cur_match is the head of the hash chain for the current + * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 + * OUT assertion: the match length is not greater than s->lookahead. + */ +#ifndef ASMV +/* For 80x86 and 680x0, an optimized version will be provided in match.asm or + * match.S. The code will be functionally equivalent. + */ +local uInt longest_match(s, cur_match) + deflate_state *s; + IPos cur_match; /* current match */ +{ + unsigned chain_length = s->max_chain_length;/* max hash chain length */ + register Bytef *scan = s->window + s->strstart; /* current string */ + register Bytef *match; /* matched string */ + register int len; /* length of current match */ + int best_len = (int)s->prev_length; /* best match length so far */ + int nice_match = s->nice_match; /* stop if match long enough */ + IPos limit = s->strstart > (IPos)MAX_DIST(s) ? + s->strstart - (IPos)MAX_DIST(s) : NIL; + /* Stop when cur_match becomes <= limit. To simplify the code, + * we prevent matches with the string of window index 0. + */ + Posf *prev = s->prev; + uInt wmask = s->w_mask; + +#ifdef UNALIGNED_OK + /* Compare two bytes at a time. Note: this is not always beneficial. + * Try with and without -DUNALIGNED_OK to check. + */ + register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1; + register ush scan_start = *(ushf*)scan; + register ush scan_end = *(ushf*)(scan+best_len-1); +#else + register Bytef *strend = s->window + s->strstart + MAX_MATCH; + register Byte scan_end1 = scan[best_len-1]; + register Byte scan_end = scan[best_len]; +#endif + + /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. + * It is easy to get rid of this optimization if necessary. + */ + Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); + + /* Do not waste too much time if we already have a good match: */ + if (s->prev_length >= s->good_match) { + chain_length >>= 2; + } + /* Do not look for matches beyond the end of the input. This is necessary + * to make deflate deterministic. + */ + if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead; + + Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); + + do { + Assert(cur_match < s->strstart, "no future"); + match = s->window + cur_match; + + /* Skip to next match if the match length cannot increase + * or if the match length is less than 2. Note that the checks below + * for insufficient lookahead only occur occasionally for performance + * reasons. Therefore uninitialized memory will be accessed, and + * conditional jumps will be made that depend on those values. + * However the length of the match is limited to the lookahead, so + * the output of deflate is not affected by the uninitialized values. + */ +#if (defined(UNALIGNED_OK) && MAX_MATCH == 258) + /* This code assumes sizeof(unsigned short) == 2. Do not use + * UNALIGNED_OK if your compiler uses a different size. + */ + if (*(ushf*)(match+best_len-1) != scan_end || + *(ushf*)match != scan_start) continue; + + /* It is not necessary to compare scan[2] and match[2] since they are + * always equal when the other bytes match, given that the hash keys + * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at + * strstart+3, +5, ... up to strstart+257. We check for insufficient + * lookahead only every 4th comparison; the 128th check will be made + * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is + * necessary to put more guard bytes at the end of the window, or + * to check more often for insufficient lookahead. + */ + Assert(scan[2] == match[2], "scan[2]?"); + scan++, match++; + do { + } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) && + *(ushf*)(scan+=2) == *(ushf*)(match+=2) && + *(ushf*)(scan+=2) == *(ushf*)(match+=2) && + *(ushf*)(scan+=2) == *(ushf*)(match+=2) && + scan < strend); + /* The funny "do {}" generates better code on most compilers */ + + /* Here, scan <= window+strstart+257 */ + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); + if (*scan == *match) scan++; + + len = (MAX_MATCH - 1) - (int)(strend-scan); + scan = strend - (MAX_MATCH-1); + +#else /* UNALIGNED_OK */ + + if (match[best_len] != scan_end || + match[best_len-1] != scan_end1 || + *match != *scan || + *++match != scan[1]) continue; + + /* The check at best_len-1 can be removed because it will be made + * again later. (This heuristic is not always a win.) + * It is not necessary to compare scan[2] and match[2] since they + * are always equal when the other bytes match, given that + * the hash keys are equal and that HASH_BITS >= 8. + */ + scan += 2, match++; + Assert(*scan == *match, "match[2]?"); + + /* We check for insufficient lookahead only every 8th comparison; + * the 256th check will be made at strstart+258. + */ + do { + } while (*++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + scan < strend); + + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); + + len = MAX_MATCH - (int)(strend - scan); + scan = strend - MAX_MATCH; + +#endif /* UNALIGNED_OK */ + + if (len > best_len) { + s->match_start = cur_match; + best_len = len; + if (len >= nice_match) break; +#ifdef UNALIGNED_OK + scan_end = *(ushf*)(scan+best_len-1); +#else + scan_end1 = scan[best_len-1]; + scan_end = scan[best_len]; +#endif + } + } while ((cur_match = prev[cur_match & wmask]) > limit + && --chain_length != 0); + + if ((uInt)best_len <= s->lookahead) return (uInt)best_len; + return s->lookahead; +} +#endif /* ASMV */ + +#else /* FASTEST */ + +/* --------------------------------------------------------------------------- + * Optimized version for FASTEST only + */ +local uInt longest_match(s, cur_match) + deflate_state *s; + IPos cur_match; /* current match */ +{ + register Bytef *scan = s->window + s->strstart; /* current string */ + register Bytef *match; /* matched string */ + register int len; /* length of current match */ + register Bytef *strend = s->window + s->strstart + MAX_MATCH; + + /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. + * It is easy to get rid of this optimization if necessary. + */ + Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); + + Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); + + Assert(cur_match < s->strstart, "no future"); + + match = s->window + cur_match; + + /* Return failure if the match length is less than 2: + */ + if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1; + + /* The check at best_len-1 can be removed because it will be made + * again later. (This heuristic is not always a win.) + * It is not necessary to compare scan[2] and match[2] since they + * are always equal when the other bytes match, given that + * the hash keys are equal and that HASH_BITS >= 8. + */ + scan += 2, match += 2; + Assert(*scan == *match, "match[2]?"); + + /* We check for insufficient lookahead only every 8th comparison; + * the 256th check will be made at strstart+258. + */ + do { + } while (*++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + scan < strend); + + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); + + len = MAX_MATCH - (int)(strend - scan); + + if (len < MIN_MATCH) return MIN_MATCH - 1; + + s->match_start = cur_match; + return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead; +} + +#endif /* FASTEST */ + +#ifdef ZLIB_DEBUG + +#define EQUAL 0 +/* result of memcmp for equal strings */ + +/* =========================================================================== + * Check that the match at match_start is indeed a match. + */ +local void check_match(s, start, match, length) + deflate_state *s; + IPos start, match; + int length; +{ + /* check that the match is indeed a match */ + if (zmemcmp(s->window + match, + s->window + start, length) != EQUAL) { + fprintf(stderr, " start %u, match %u, length %d\n", + start, match, length); + do { + fprintf(stderr, "%c%c", s->window[match++], s->window[start++]); + } while (--length != 0); + z_error("invalid match"); + } + if (z_verbose > 1) { + fprintf(stderr,"\\[%d,%d]", start-match, length); + do { putc(s->window[start++], stderr); } while (--length != 0); + } +} +#else +# define check_match(s, start, match, length) +#endif /* ZLIB_DEBUG */ + +/* =========================================================================== + * Fill the window when the lookahead becomes insufficient. + * Updates strstart and lookahead. + * + * IN assertion: lookahead < MIN_LOOKAHEAD + * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD + * At least one byte has been read, or avail_in == 0; reads are + * performed for at least two bytes (required for the zip translate_eol + * option -- not supported here). + */ +local void fill_window(s) + deflate_state *s; +{ + unsigned n; + unsigned more; /* Amount of free space at the end of the window. */ + uInt wsize = s->w_size; + + Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); + + do { + more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); + + /* Deal with !@#$% 64K limit: */ + if (sizeof(int) <= 2) { + if (more == 0 && s->strstart == 0 && s->lookahead == 0) { + more = wsize; + + } else if (more == (unsigned)(-1)) { + /* Very unlikely, but possible on 16 bit machine if + * strstart == 0 && lookahead == 1 (input done a byte at time) + */ + more--; + } + } + + /* If the window is almost full and there is insufficient lookahead, + * move the upper half to the lower one to make room in the upper half. + */ + if (s->strstart >= wsize+MAX_DIST(s)) { + + zmemcpy(s->window, s->window+wsize, (unsigned)wsize - more); + s->match_start -= wsize; + s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ + s->block_start -= (long) wsize; + slide_hash(s); + more += wsize; + } + if (s->strm->avail_in == 0) break; + + /* If there was no sliding: + * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && + * more == window_size - lookahead - strstart + * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) + * => more >= window_size - 2*WSIZE + 2 + * In the BIG_MEM or MMAP case (not yet supported), + * window_size == input_size + MIN_LOOKAHEAD && + * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. + * Otherwise, window_size == 2*WSIZE so more >= 2. + * If there was sliding, more >= WSIZE. So in all cases, more >= 2. + */ + Assert(more >= 2, "more < 2"); + + n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); + s->lookahead += n; + + /* Initialize the hash value now that we have some input: */ + if (s->lookahead + s->insert >= MIN_MATCH) { + uInt str = s->strstart - s->insert; + s->ins_h = s->window[str]; + UPDATE_HASH(s, s->ins_h, s->window[str + 1]); +#if MIN_MATCH != 3 + Call UPDATE_HASH() MIN_MATCH-3 more times +#endif + while (s->insert) { + UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); +#ifndef FASTEST + s->prev[str & s->w_mask] = s->head[s->ins_h]; +#endif + s->head[s->ins_h] = (Pos)str; + str++; + s->insert--; + if (s->lookahead + s->insert < MIN_MATCH) + break; + } + } + /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, + * but this is not important since only literal bytes will be emitted. + */ + + } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); + + /* If the WIN_INIT bytes after the end of the current data have never been + * written, then zero those bytes in order to avoid memory check reports of + * the use of uninitialized (or uninitialised as Julian writes) bytes by + * the longest match routines. Update the high water mark for the next + * time through here. WIN_INIT is set to MAX_MATCH since the longest match + * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. + */ + if (s->high_water < s->window_size) { + ulg curr = s->strstart + (ulg)(s->lookahead); + ulg init; + + if (s->high_water < curr) { + /* Previous high water mark below current data -- zero WIN_INIT + * bytes or up to end of window, whichever is less. + */ + init = s->window_size - curr; + if (init > WIN_INIT) + init = WIN_INIT; + zmemzero(s->window + curr, (unsigned)init); + s->high_water = curr + init; + } + else if (s->high_water < (ulg)curr + WIN_INIT) { + /* High water mark at or above current data, but below current data + * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up + * to end of window, whichever is less. + */ + init = (ulg)curr + WIN_INIT - s->high_water; + if (init > s->window_size - s->high_water) + init = s->window_size - s->high_water; + zmemzero(s->window + s->high_water, (unsigned)init); + s->high_water += init; + } + } + + Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, + "not enough room for search"); +} + +/* =========================================================================== + * Flush the current block, with given end-of-file flag. + * IN assertion: strstart is set to the end of the current match. + */ +#define FLUSH_BLOCK_ONLY(s, last) { \ + _tr_flush_block(s, (s->block_start >= 0L ? \ + (charf *)&s->window[(unsigned)s->block_start] : \ + (charf *)Z_NULL), \ + (ulg)((long)s->strstart - s->block_start), \ + (last)); \ + s->block_start = s->strstart; \ + flush_pending(s->strm); \ + Tracev((stderr,"[FLUSH]")); \ +} + +/* Same but force premature exit if necessary. */ +#define FLUSH_BLOCK(s, last) { \ + FLUSH_BLOCK_ONLY(s, last); \ + if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ +} + +/* Maximum stored block length in deflate format (not including header). */ +#define MAX_STORED 65535 + +/* Minimum of a and b. */ +#define MIN(a, b) ((a) > (b) ? (b) : (a)) + +/* =========================================================================== + * Copy without compression as much as possible from the input stream, return + * the current block state. + * + * In case deflateParams() is used to later switch to a non-zero compression + * level, s->matches (otherwise unused when storing) keeps track of the number + * of hash table slides to perform. If s->matches is 1, then one hash table + * slide will be done when switching. If s->matches is 2, the maximum value + * allowed here, then the hash table will be cleared, since two or more slides + * is the same as a clear. + * + * deflate_stored() is written to minimize the number of times an input byte is + * copied. It is most efficient with large input and output buffers, which + * maximizes the opportunites to have a single copy from next_in to next_out. + */ +local block_state deflate_stored(s, flush) + deflate_state *s; + int flush; +{ + /* Smallest worthy block size when not flushing or finishing. By default + * this is 32K. This can be as small as 507 bytes for memLevel == 1. For + * large input and output buffers, the stored block size will be larger. + */ + unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size); + + /* Copy as many min_block or larger stored blocks directly to next_out as + * possible. If flushing, copy the remaining available input to next_out as + * stored blocks, if there is enough space. + */ + unsigned len, left, have, last = 0; + unsigned used = s->strm->avail_in; + do { + /* Set len to the maximum size block that we can copy directly with the + * available input data and output space. Set left to how much of that + * would be copied from what's left in the window. + */ + len = MAX_STORED; /* maximum deflate stored block length */ + have = (s->bi_valid + 42) >> 3; /* number of header bytes */ + if (s->strm->avail_out < have) /* need room for header */ + break; + /* maximum stored block length that will fit in avail_out: */ + have = s->strm->avail_out - have; + left = s->strstart - s->block_start; /* bytes left in window */ + if (len > (ulg)left + s->strm->avail_in) + len = left + s->strm->avail_in; /* limit len to the input */ + if (len > have) + len = have; /* limit len to the output */ + + /* If the stored block would be less than min_block in length, or if + * unable to copy all of the available input when flushing, then try + * copying to the window and the pending buffer instead. Also don't + * write an empty block when flushing -- deflate() does that. + */ + if (len < min_block && ((len == 0 && flush != Z_FINISH) || + flush == Z_NO_FLUSH || + len != left + s->strm->avail_in)) + break; + + /* Make a dummy stored block in pending to get the header bytes, + * including any pending bits. This also updates the debugging counts. + */ + last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0; + _tr_stored_block(s, (char *)0, 0L, last); + + /* Replace the lengths in the dummy stored block with len. */ + s->pending_buf[s->pending - 4] = len; + s->pending_buf[s->pending - 3] = len >> 8; + s->pending_buf[s->pending - 2] = ~len; + s->pending_buf[s->pending - 1] = ~len >> 8; + + /* Write the stored block header bytes. */ + flush_pending(s->strm); + +#ifdef ZLIB_DEBUG + /* Update debugging counts for the data about to be copied. */ + s->compressed_len += len << 3; + s->bits_sent += len << 3; +#endif + + /* Copy uncompressed bytes from the window to next_out. */ + if (left) { + if (left > len) + left = len; + zmemcpy(s->strm->next_out, s->window + s->block_start, left); + s->strm->next_out += left; + s->strm->avail_out -= left; + s->strm->total_out += left; + s->block_start += left; + len -= left; + } + + /* Copy uncompressed bytes directly from next_in to next_out, updating + * the check value. + */ + if (len) { + read_buf(s->strm, s->strm->next_out, len); + s->strm->next_out += len; + s->strm->avail_out -= len; + s->strm->total_out += len; + } + } while (last == 0); + + /* Update the sliding window with the last s->w_size bytes of the copied + * data, or append all of the copied data to the existing window if less + * than s->w_size bytes were copied. Also update the number of bytes to + * insert in the hash tables, in the event that deflateParams() switches to + * a non-zero compression level. + */ + used -= s->strm->avail_in; /* number of input bytes directly copied */ + if (used) { + /* If any input was used, then no unused input remains in the window, + * therefore s->block_start == s->strstart. + */ + if (used >= s->w_size) { /* supplant the previous history */ + s->matches = 2; /* clear hash */ + zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size); + s->strstart = s->w_size; + } + else { + if (s->window_size - s->strstart <= used) { + /* Slide the window down. */ + s->strstart -= s->w_size; + zmemcpy(s->window, s->window + s->w_size, s->strstart); + if (s->matches < 2) + s->matches++; /* add a pending slide_hash() */ + } + zmemcpy(s->window + s->strstart, s->strm->next_in - used, used); + s->strstart += used; + } + s->block_start = s->strstart; + s->insert += MIN(used, s->w_size - s->insert); + } + if (s->high_water < s->strstart) + s->high_water = s->strstart; + + /* If the last block was written to next_out, then done. */ + if (last) + return finish_done; + + /* If flushing and all input has been consumed, then done. */ + if (flush != Z_NO_FLUSH && flush != Z_FINISH && + s->strm->avail_in == 0 && (long)s->strstart == s->block_start) + return block_done; + + /* Fill the window with any remaining input. */ + have = s->window_size - s->strstart - 1; + if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) { + /* Slide the window down. */ + s->block_start -= s->w_size; + s->strstart -= s->w_size; + zmemcpy(s->window, s->window + s->w_size, s->strstart); + if (s->matches < 2) + s->matches++; /* add a pending slide_hash() */ + have += s->w_size; /* more space now */ + } + if (have > s->strm->avail_in) + have = s->strm->avail_in; + if (have) { + read_buf(s->strm, s->window + s->strstart, have); + s->strstart += have; + } + if (s->high_water < s->strstart) + s->high_water = s->strstart; + + /* There was not enough avail_out to write a complete worthy or flushed + * stored block to next_out. Write a stored block to pending instead, if we + * have enough input for a worthy block, or if flushing and there is enough + * room for the remaining input as a stored block in the pending buffer. + */ + have = (s->bi_valid + 42) >> 3; /* number of header bytes */ + /* maximum stored block length that will fit in pending: */ + have = MIN(s->pending_buf_size - have, MAX_STORED); + min_block = MIN(have, s->w_size); + left = s->strstart - s->block_start; + if (left >= min_block || + ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH && + s->strm->avail_in == 0 && left <= have)) { + len = MIN(left, have); + last = flush == Z_FINISH && s->strm->avail_in == 0 && + len == left ? 1 : 0; + _tr_stored_block(s, (charf *)s->window + s->block_start, len, last); + s->block_start += len; + flush_pending(s->strm); + } + + /* We've done all we can with the available input and output. */ + return last ? finish_started : need_more; +} + +/* =========================================================================== + * Compress as much as possible from the input stream, return the current + * block state. + * This function does not perform lazy evaluation of matches and inserts + * new strings in the dictionary only for unmatched strings or for short + * matches. It is used only for the fast compression options. + */ +local block_state deflate_fast(s, flush) + deflate_state *s; + int flush; +{ + IPos hash_head; /* head of the hash chain */ + int bflush; /* set if current block must be flushed */ + + for (;;) { + /* Make sure that we always have enough lookahead, except + * at the end of the input file. We need MAX_MATCH bytes + * for the next match, plus MIN_MATCH bytes to insert the + * string following the next match. + */ + if (s->lookahead < MIN_LOOKAHEAD) { + fill_window(s); + if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { + return need_more; + } + if (s->lookahead == 0) break; /* flush the current block */ + } + + /* Insert the string window[strstart .. strstart+2] in the + * dictionary, and set hash_head to the head of the hash chain: + */ + hash_head = NIL; + if (s->lookahead >= MIN_MATCH) { + INSERT_STRING(s, s->strstart, hash_head); + } + + /* Find the longest match, discarding those <= prev_length. + * At this point we have always match_length < MIN_MATCH + */ + if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { + /* To simplify the code, we prevent matches with the string + * of window index 0 (in particular we have to avoid a match + * of the string with itself at the start of the input file). + */ + s->match_length = longest_match (s, hash_head); + /* longest_match() sets match_start */ + } + if (s->match_length >= MIN_MATCH) { + check_match(s, s->strstart, s->match_start, s->match_length); + + _tr_tally_dist(s, s->strstart - s->match_start, + s->match_length - MIN_MATCH, bflush); + + s->lookahead -= s->match_length; + + /* Insert new strings in the hash table only if the match length + * is not too large. This saves time but degrades compression. + */ +#ifndef FASTEST + if (s->match_length <= s->max_insert_length && + s->lookahead >= MIN_MATCH) { + s->match_length--; /* string at strstart already in table */ + do { + s->strstart++; + INSERT_STRING(s, s->strstart, hash_head); + /* strstart never exceeds WSIZE-MAX_MATCH, so there are + * always MIN_MATCH bytes ahead. + */ + } while (--s->match_length != 0); + s->strstart++; + } else +#endif + { + s->strstart += s->match_length; + s->match_length = 0; + s->ins_h = s->window[s->strstart]; + UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); +#if MIN_MATCH != 3 + Call UPDATE_HASH() MIN_MATCH-3 more times +#endif + /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not + * matter since it will be recomputed at next deflate call. + */ + } + } else { + /* No match, output a literal byte */ + Tracevv((stderr,"%c", s->window[s->strstart])); + _tr_tally_lit (s, s->window[s->strstart], bflush); + s->lookahead--; + s->strstart++; + } + if (bflush) FLUSH_BLOCK(s, 0); + } + s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; + if (flush == Z_FINISH) { + FLUSH_BLOCK(s, 1); + return finish_done; + } + if (s->last_lit) + FLUSH_BLOCK(s, 0); + return block_done; +} + +#ifndef FASTEST +/* =========================================================================== + * Same as above, but achieves better compression. We use a lazy + * evaluation for matches: a match is finally adopted only if there is + * no better match at the next window position. + */ +local block_state deflate_slow(s, flush) + deflate_state *s; + int flush; +{ + IPos hash_head; /* head of hash chain */ + int bflush; /* set if current block must be flushed */ + + /* Process the input block. */ + for (;;) { + /* Make sure that we always have enough lookahead, except + * at the end of the input file. We need MAX_MATCH bytes + * for the next match, plus MIN_MATCH bytes to insert the + * string following the next match. + */ + if (s->lookahead < MIN_LOOKAHEAD) { + fill_window(s); + if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { + return need_more; + } + if (s->lookahead == 0) break; /* flush the current block */ + } + + /* Insert the string window[strstart .. strstart+2] in the + * dictionary, and set hash_head to the head of the hash chain: + */ + hash_head = NIL; + if (s->lookahead >= MIN_MATCH) { + INSERT_STRING(s, s->strstart, hash_head); + } + + /* Find the longest match, discarding those <= prev_length. + */ + s->prev_length = s->match_length, s->prev_match = s->match_start; + s->match_length = MIN_MATCH-1; + + if (hash_head != NIL && s->prev_length < s->max_lazy_match && + s->strstart - hash_head <= MAX_DIST(s)) { + /* To simplify the code, we prevent matches with the string + * of window index 0 (in particular we have to avoid a match + * of the string with itself at the start of the input file). + */ + s->match_length = longest_match (s, hash_head); + /* longest_match() sets match_start */ + + if (s->match_length <= 5 && (s->strategy == Z_FILTERED +#if TOO_FAR <= 32767 + || (s->match_length == MIN_MATCH && + s->strstart - s->match_start > TOO_FAR) +#endif + )) { + + /* If prev_match is also MIN_MATCH, match_start is garbage + * but we will ignore the current match anyway. + */ + s->match_length = MIN_MATCH-1; + } + } + /* If there was a match at the previous step and the current + * match is not better, output the previous match: + */ + if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) { + uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; + /* Do not insert strings in hash table beyond this. */ + + check_match(s, s->strstart-1, s->prev_match, s->prev_length); + + _tr_tally_dist(s, s->strstart -1 - s->prev_match, + s->prev_length - MIN_MATCH, bflush); + + /* Insert in hash table all strings up to the end of the match. + * strstart-1 and strstart are already inserted. If there is not + * enough lookahead, the last two strings are not inserted in + * the hash table. + */ + s->lookahead -= s->prev_length-1; + s->prev_length -= 2; + do { + if (++s->strstart <= max_insert) { + INSERT_STRING(s, s->strstart, hash_head); + } + } while (--s->prev_length != 0); + s->match_available = 0; + s->match_length = MIN_MATCH-1; + s->strstart++; + + if (bflush) FLUSH_BLOCK(s, 0); + + } else if (s->match_available) { + /* If there was no match at the previous position, output a + * single literal. If there was a match but the current match + * is longer, truncate the previous match to a single literal. + */ + Tracevv((stderr,"%c", s->window[s->strstart-1])); + _tr_tally_lit(s, s->window[s->strstart-1], bflush); + if (bflush) { + FLUSH_BLOCK_ONLY(s, 0); + } + s->strstart++; + s->lookahead--; + if (s->strm->avail_out == 0) return need_more; + } else { + /* There is no previous match to compare with, wait for + * the next step to decide. + */ + s->match_available = 1; + s->strstart++; + s->lookahead--; + } + } + Assert (flush != Z_NO_FLUSH, "no flush?"); + if (s->match_available) { + Tracevv((stderr,"%c", s->window[s->strstart-1])); + _tr_tally_lit(s, s->window[s->strstart-1], bflush); + s->match_available = 0; + } + s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; + if (flush == Z_FINISH) { + FLUSH_BLOCK(s, 1); + return finish_done; + } + if (s->last_lit) + FLUSH_BLOCK(s, 0); + return block_done; +} +#endif /* FASTEST */ + +/* =========================================================================== + * For Z_RLE, simply look for runs of bytes, generate matches only of distance + * one. Do not maintain a hash table. (It will be regenerated if this run of + * deflate switches away from Z_RLE.) + */ +local block_state deflate_rle(s, flush) + deflate_state *s; + int flush; +{ + int bflush; /* set if current block must be flushed */ + uInt prev; /* byte at distance one to match */ + Bytef *scan, *strend; /* scan goes up to strend for length of run */ + + for (;;) { + /* Make sure that we always have enough lookahead, except + * at the end of the input file. We need MAX_MATCH bytes + * for the longest run, plus one for the unrolled loop. + */ + if (s->lookahead <= MAX_MATCH) { + fill_window(s); + if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) { + return need_more; + } + if (s->lookahead == 0) break; /* flush the current block */ + } + + /* See how many times the previous byte repeats */ + s->match_length = 0; + if (s->lookahead >= MIN_MATCH && s->strstart > 0) { + scan = s->window + s->strstart - 1; + prev = *scan; + if (prev == *++scan && prev == *++scan && prev == *++scan) { + strend = s->window + s->strstart + MAX_MATCH; + do { + } while (prev == *++scan && prev == *++scan && + prev == *++scan && prev == *++scan && + prev == *++scan && prev == *++scan && + prev == *++scan && prev == *++scan && + scan < strend); + s->match_length = MAX_MATCH - (uInt)(strend - scan); + if (s->match_length > s->lookahead) + s->match_length = s->lookahead; + } + Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan"); + } + + /* Emit match if have run of MIN_MATCH or longer, else emit literal */ + if (s->match_length >= MIN_MATCH) { + check_match(s, s->strstart, s->strstart - 1, s->match_length); + + _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush); + + s->lookahead -= s->match_length; + s->strstart += s->match_length; + s->match_length = 0; + } else { + /* No match, output a literal byte */ + Tracevv((stderr,"%c", s->window[s->strstart])); + _tr_tally_lit (s, s->window[s->strstart], bflush); + s->lookahead--; + s->strstart++; + } + if (bflush) FLUSH_BLOCK(s, 0); + } + s->insert = 0; + if (flush == Z_FINISH) { + FLUSH_BLOCK(s, 1); + return finish_done; + } + if (s->last_lit) + FLUSH_BLOCK(s, 0); + return block_done; +} + +/* =========================================================================== + * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. + * (It will be regenerated if this run of deflate switches away from Huffman.) + */ +local block_state deflate_huff(s, flush) + deflate_state *s; + int flush; +{ + int bflush; /* set if current block must be flushed */ + + for (;;) { + /* Make sure that we have a literal to write. */ + if (s->lookahead == 0) { + fill_window(s); + if (s->lookahead == 0) { + if (flush == Z_NO_FLUSH) + return need_more; + break; /* flush the current block */ + } + } + + /* Output a literal byte */ + s->match_length = 0; + Tracevv((stderr,"%c", s->window[s->strstart])); + _tr_tally_lit (s, s->window[s->strstart], bflush); + s->lookahead--; + s->strstart++; + if (bflush) FLUSH_BLOCK(s, 0); + } + s->insert = 0; + if (flush == Z_FINISH) { + FLUSH_BLOCK(s, 1); + return finish_done; + } + if (s->last_lit) + FLUSH_BLOCK(s, 0); + return block_done; +} diff --git a/third_party/java_tools/zlib/deflate.h b/third_party/java_tools/zlib/deflate.h new file mode 100644 index 000000000..23ecdd312 --- /dev/null +++ b/third_party/java_tools/zlib/deflate.h @@ -0,0 +1,349 @@ +/* deflate.h -- internal compression state + * Copyright (C) 1995-2016 Jean-loup Gailly + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* WARNING: this file should *not* be used by applications. It is + part of the implementation of the compression library and is + subject to change. Applications should only use zlib.h. + */ + +/* @(#) $Id$ */ + +#ifndef DEFLATE_H +#define DEFLATE_H + +#include "zutil.h" + +/* define NO_GZIP when compiling if you want to disable gzip header and + trailer creation by deflate(). NO_GZIP would be used to avoid linking in + the crc code when it is not needed. For shared libraries, gzip encoding + should be left enabled. */ +#ifndef NO_GZIP +# define GZIP +#endif + +/* =========================================================================== + * Internal compression state. + */ + +#define LENGTH_CODES 29 +/* number of length codes, not counting the special END_BLOCK code */ + +#define LITERALS 256 +/* number of literal bytes 0..255 */ + +#define L_CODES (LITERALS+1+LENGTH_CODES) +/* number of Literal or Length codes, including the END_BLOCK code */ + +#define D_CODES 30 +/* number of distance codes */ + +#define BL_CODES 19 +/* number of codes used to transfer the bit lengths */ + +#define HEAP_SIZE (2*L_CODES+1) +/* maximum heap size */ + +#define MAX_BITS 15 +/* All codes must not exceed MAX_BITS bits */ + +#define Buf_size 16 +/* size of bit buffer in bi_buf */ + +#define INIT_STATE 42 /* zlib header -> BUSY_STATE */ +#ifdef GZIP +# define GZIP_STATE 57 /* gzip header -> BUSY_STATE | EXTRA_STATE */ +#endif +#define EXTRA_STATE 69 /* gzip extra block -> NAME_STATE */ +#define NAME_STATE 73 /* gzip file name -> COMMENT_STATE */ +#define COMMENT_STATE 91 /* gzip comment -> HCRC_STATE */ +#define HCRC_STATE 103 /* gzip header CRC -> BUSY_STATE */ +#define BUSY_STATE 113 /* deflate -> FINISH_STATE */ +#define FINISH_STATE 666 /* stream complete */ +/* Stream status */ + + +/* Data structure describing a single value and its code string. */ +typedef struct ct_data_s { + union { + ush freq; /* frequency count */ + ush code; /* bit string */ + } fc; + union { + ush dad; /* father node in Huffman tree */ + ush len; /* length of bit string */ + } dl; +} FAR ct_data; + +#define Freq fc.freq +#define Code fc.code +#define Dad dl.dad +#define Len dl.len + +typedef struct static_tree_desc_s static_tree_desc; + +typedef struct tree_desc_s { + ct_data *dyn_tree; /* the dynamic tree */ + int max_code; /* largest code with non zero frequency */ + const static_tree_desc *stat_desc; /* the corresponding static tree */ +} FAR tree_desc; + +typedef ush Pos; +typedef Pos FAR Posf; +typedef unsigned IPos; + +/* A Pos is an index in the character window. We use short instead of int to + * save space in the various tables. IPos is used only for parameter passing. + */ + +typedef struct internal_state { + z_streamp strm; /* pointer back to this zlib stream */ + int status; /* as the name implies */ + Bytef *pending_buf; /* output still pending */ + ulg pending_buf_size; /* size of pending_buf */ + Bytef *pending_out; /* next pending byte to output to the stream */ + ulg pending; /* nb of bytes in the pending buffer */ + int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ + gz_headerp gzhead; /* gzip header information to write */ + ulg gzindex; /* where in extra, name, or comment */ + Byte method; /* can only be DEFLATED */ + int last_flush; /* value of flush param for previous deflate call */ + + /* used by deflate.c: */ + + uInt w_size; /* LZ77 window size (32K by default) */ + uInt w_bits; /* log2(w_size) (8..16) */ + uInt w_mask; /* w_size - 1 */ + + Bytef *window; + /* Sliding window. Input bytes are read into the second half of the window, + * and move to the first half later to keep a dictionary of at least wSize + * bytes. With this organization, matches are limited to a distance of + * wSize-MAX_MATCH bytes, but this ensures that IO is always + * performed with a length multiple of the block size. Also, it limits + * the window size to 64K, which is quite useful on MSDOS. + * To do: use the user input buffer as sliding window. + */ + + ulg window_size; + /* Actual size of window: 2*wSize, except when the user input buffer + * is directly used as sliding window. + */ + + Posf *prev; + /* Link to older string with same hash index. To limit the size of this + * array to 64K, this link is maintained only for the last 32K strings. + * An index in this array is thus a window index modulo 32K. + */ + + Posf *head; /* Heads of the hash chains or NIL. */ + + uInt ins_h; /* hash index of string to be inserted */ + uInt hash_size; /* number of elements in hash table */ + uInt hash_bits; /* log2(hash_size) */ + uInt hash_mask; /* hash_size-1 */ + + uInt hash_shift; + /* Number of bits by which ins_h must be shifted at each input + * step. It must be such that after MIN_MATCH steps, the oldest + * byte no longer takes part in the hash key, that is: + * hash_shift * MIN_MATCH >= hash_bits + */ + + long block_start; + /* Window position at the beginning of the current output block. Gets + * negative when the window is moved backwards. + */ + + uInt match_length; /* length of best match */ + IPos prev_match; /* previous match */ + int match_available; /* set if previous match exists */ + uInt strstart; /* start of string to insert */ + uInt match_start; /* start of matching string */ + uInt lookahead; /* number of valid bytes ahead in window */ + + uInt prev_length; + /* Length of the best match at previous step. Matches not greater than this + * are discarded. This is used in the lazy match evaluation. + */ + + uInt max_chain_length; + /* To speed up deflation, hash chains are never searched beyond this + * length. A higher limit improves compression ratio but degrades the + * speed. + */ + + uInt max_lazy_match; + /* Attempt to find a better match only when the current match is strictly + * smaller than this value. This mechanism is used only for compression + * levels >= 4. + */ +# define max_insert_length max_lazy_match + /* Insert new strings in the hash table only if the match length is not + * greater than this length. This saves time but degrades compression. + * max_insert_length is used only for compression levels <= 3. + */ + + int level; /* compression level (1..9) */ + int strategy; /* favor or force Huffman coding*/ + + uInt good_match; + /* Use a faster search when the previous match is longer than this */ + + int nice_match; /* Stop searching when current match exceeds this */ + + /* used by trees.c: */ + /* Didn't use ct_data typedef below to suppress compiler warning */ + struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ + struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ + struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */ + + struct tree_desc_s l_desc; /* desc. for literal tree */ + struct tree_desc_s d_desc; /* desc. for distance tree */ + struct tree_desc_s bl_desc; /* desc. for bit length tree */ + + ush bl_count[MAX_BITS+1]; + /* number of codes at each bit length for an optimal tree */ + + int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ + int heap_len; /* number of elements in the heap */ + int heap_max; /* element of largest frequency */ + /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. + * The same heap array is used to build all trees. + */ + + uch depth[2*L_CODES+1]; + /* Depth of each subtree used as tie breaker for trees of equal frequency + */ + + uchf *l_buf; /* buffer for literals or lengths */ + + uInt lit_bufsize; + /* Size of match buffer for literals/lengths. There are 4 reasons for + * limiting lit_bufsize to 64K: + * - frequencies can be kept in 16 bit counters + * - if compression is not successful for the first block, all input + * data is still in the window so we can still emit a stored block even + * when input comes from standard input. (This can also be done for + * all blocks if lit_bufsize is not greater than 32K.) + * - if compression is not successful for a file smaller than 64K, we can + * even emit a stored file instead of a stored block (saving 5 bytes). + * This is applicable only for zip (not gzip or zlib). + * - creating new Huffman trees less frequently may not provide fast + * adaptation to changes in the input data statistics. (Take for + * example a binary file with poorly compressible code followed by + * a highly compressible string table.) Smaller buffer sizes give + * fast adaptation but have of course the overhead of transmitting + * trees more frequently. + * - I can't count above 4 + */ + + uInt last_lit; /* running index in l_buf */ + + ushf *d_buf; + /* Buffer for distances. To simplify the code, d_buf and l_buf have + * the same number of elements. To use different lengths, an extra flag + * array would be necessary. + */ + + ulg opt_len; /* bit length of current block with optimal trees */ + ulg static_len; /* bit length of current block with static trees */ + uInt matches; /* number of string matches in current block */ + uInt insert; /* bytes at end of window left to insert */ + +#ifdef ZLIB_DEBUG + ulg compressed_len; /* total bit length of compressed file mod 2^32 */ + ulg bits_sent; /* bit length of compressed data sent mod 2^32 */ +#endif + + ush bi_buf; + /* Output buffer. bits are inserted starting at the bottom (least + * significant bits). + */ + int bi_valid; + /* Number of valid bits in bi_buf. All bits above the last valid bit + * are always zero. + */ + + ulg high_water; + /* High water mark offset in window for initialized bytes -- bytes above + * this are set to zero in order to avoid memory check warnings when + * longest match routines access bytes past the input. This is then + * updated to the new high water mark. + */ + +} FAR deflate_state; + +/* Output a byte on the stream. + * IN assertion: there is enough room in pending_buf. + */ +#define put_byte(s, c) {s->pending_buf[s->pending++] = (Bytef)(c);} + + +#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) +/* Minimum amount of lookahead, except at the end of the input file. + * See deflate.c for comments about the MIN_MATCH+1. + */ + +#define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD) +/* In order to simplify the code, particularly on 16 bit machines, match + * distances are limited to MAX_DIST instead of WSIZE. + */ + +#define WIN_INIT MAX_MATCH +/* Number of bytes after end of data in window to initialize in order to avoid + memory checker errors from longest match routines */ + + /* in trees.c */ +void ZLIB_INTERNAL _tr_init OF((deflate_state *s)); +int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc)); +void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf, + ulg stored_len, int last)); +void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s)); +void ZLIB_INTERNAL _tr_align OF((deflate_state *s)); +void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf, + ulg stored_len, int last)); + +#define d_code(dist) \ + ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)]) +/* Mapping from a distance to a distance code. dist is the distance - 1 and + * must not have side effects. _dist_code[256] and _dist_code[257] are never + * used. + */ + +#ifndef ZLIB_DEBUG +/* Inline versions of _tr_tally for speed: */ + +#if defined(GEN_TREES_H) || !defined(STDC) + extern uch ZLIB_INTERNAL _length_code[]; + extern uch ZLIB_INTERNAL _dist_code[]; +#else + extern const uch ZLIB_INTERNAL _length_code[]; + extern const uch ZLIB_INTERNAL _dist_code[]; +#endif + +# define _tr_tally_lit(s, c, flush) \ + { uch cc = (c); \ + s->d_buf[s->last_lit] = 0; \ + s->l_buf[s->last_lit++] = cc; \ + s->dyn_ltree[cc].Freq++; \ + flush = (s->last_lit == s->lit_bufsize-1); \ + } +# define _tr_tally_dist(s, distance, length, flush) \ + { uch len = (uch)(length); \ + ush dist = (ush)(distance); \ + s->d_buf[s->last_lit] = dist; \ + s->l_buf[s->last_lit++] = len; \ + dist--; \ + s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ + s->dyn_dtree[d_code(dist)].Freq++; \ + flush = (s->last_lit == s->lit_bufsize-1); \ + } +#else +# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) +# define _tr_tally_dist(s, distance, length, flush) \ + flush = _tr_tally(s, distance, length) +#endif + +#endif /* DEFLATE_H */ diff --git a/third_party/java_tools/zlib/gzclose.c b/third_party/java_tools/zlib/gzclose.c new file mode 100644 index 000000000..caeb99a31 --- /dev/null +++ b/third_party/java_tools/zlib/gzclose.c @@ -0,0 +1,25 @@ +/* gzclose.c -- zlib gzclose() function + * Copyright (C) 2004, 2010 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#include "gzguts.h" + +/* gzclose() is in a separate file so that it is linked in only if it is used. + That way the other gzclose functions can be used instead to avoid linking in + unneeded compression or decompression routines. */ +int ZEXPORT gzclose(file) + gzFile file; +{ +#ifndef NO_GZCOMPRESS + gz_statep state; + + if (file == NULL) + return Z_STREAM_ERROR; + state = (gz_statep)file; + + return state->mode == GZ_READ ? gzclose_r(file) : gzclose_w(file); +#else + return gzclose_r(file); +#endif +} diff --git a/third_party/java_tools/zlib/gzguts.h b/third_party/java_tools/zlib/gzguts.h new file mode 100644 index 000000000..990a4d251 --- /dev/null +++ b/third_party/java_tools/zlib/gzguts.h @@ -0,0 +1,218 @@ +/* gzguts.h -- zlib internal header definitions for gz* operations + * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013, 2016 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#ifdef _LARGEFILE64_SOURCE +# ifndef _LARGEFILE_SOURCE +# define _LARGEFILE_SOURCE 1 +# endif +# ifdef _FILE_OFFSET_BITS +# undef _FILE_OFFSET_BITS +# endif +#endif + +#ifdef HAVE_HIDDEN +# define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) +#else +# define ZLIB_INTERNAL +#endif + +#include +#include "zlib.h" +#ifdef STDC +# include +# include +# include +#endif + +#ifndef _POSIX_SOURCE +# define _POSIX_SOURCE +#endif +#include + +#ifdef _WIN32 +# include +#endif + +#if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32) +# include +#endif + +#if defined(_WIN32) || defined(__CYGWIN__) +# define WIDECHAR +#endif + +#ifdef WINAPI_FAMILY +# define open _open +# define read _read +# define write _write +# define close _close +#endif + +#ifdef NO_DEFLATE /* for compatibility with old definition */ +# define NO_GZCOMPRESS +#endif + +#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550) +# ifndef HAVE_VSNPRINTF +# define HAVE_VSNPRINTF +# endif +#endif + +#if defined(__CYGWIN__) +# ifndef HAVE_VSNPRINTF +# define HAVE_VSNPRINTF +# endif +#endif + +#if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410) +# ifndef HAVE_VSNPRINTF +# define HAVE_VSNPRINTF +# endif +#endif + +#ifndef HAVE_VSNPRINTF +# ifdef MSDOS +/* vsnprintf may exist on some MS-DOS compilers (DJGPP?), + but for now we just assume it doesn't. */ +# define NO_vsnprintf +# endif +# ifdef __TURBOC__ +# define NO_vsnprintf +# endif +# ifdef WIN32 +/* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */ +# if !defined(vsnprintf) && !defined(NO_vsnprintf) +# if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 ) +# define vsnprintf _vsnprintf +# endif +# endif +# endif +# ifdef __SASC +# define NO_vsnprintf +# endif +# ifdef VMS +# define NO_vsnprintf +# endif +# ifdef __OS400__ +# define NO_vsnprintf +# endif +# ifdef __MVS__ +# define NO_vsnprintf +# endif +#endif + +/* unlike snprintf (which is required in C99), _snprintf does not guarantee + null termination of the result -- however this is only used in gzlib.c where + the result is assured to fit in the space provided */ +#if defined(_MSC_VER) && _MSC_VER < 1900 +# define snprintf _snprintf +#endif + +#ifndef local +# define local static +#endif +/* since "static" is used to mean two completely different things in C, we + define "local" for the non-static meaning of "static", for readability + (compile with -Dlocal if your debugger can't find static symbols) */ + +/* gz* functions always use library allocation functions */ +#ifndef STDC + extern voidp malloc OF((uInt size)); + extern void free OF((voidpf ptr)); +#endif + +/* get errno and strerror definition */ +#if defined UNDER_CE +# include +# define zstrerror() gz_strwinerror((DWORD)GetLastError()) +#else +# ifndef NO_STRERROR +# include +# define zstrerror() strerror(errno) +# else +# define zstrerror() "stdio error (consult errno)" +# endif +#endif + +/* provide prototypes for these when building zlib without LFS */ +#if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0 + ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); + ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int)); + ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile)); + ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile)); +#endif + +/* default memLevel */ +#if MAX_MEM_LEVEL >= 8 +# define DEF_MEM_LEVEL 8 +#else +# define DEF_MEM_LEVEL MAX_MEM_LEVEL +#endif + +/* default i/o buffer size -- double this for output when reading (this and + twice this must be able to fit in an unsigned type) */ +#define GZBUFSIZE 8192 + +/* gzip modes, also provide a little integrity check on the passed structure */ +#define GZ_NONE 0 +#define GZ_READ 7247 +#define GZ_WRITE 31153 +#define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */ + +/* values for gz_state how */ +#define LOOK 0 /* look for a gzip header */ +#define COPY 1 /* copy input directly */ +#define GZIP 2 /* decompress a gzip stream */ + +/* internal gzip file state data structure */ +typedef struct { + /* exposed contents for gzgetc() macro */ + struct gzFile_s x; /* "x" for exposed */ + /* x.have: number of bytes available at x.next */ + /* x.next: next output data to deliver or write */ + /* x.pos: current position in uncompressed data */ + /* used for both reading and writing */ + int mode; /* see gzip modes above */ + int fd; /* file descriptor */ + char *path; /* path or fd for error messages */ + unsigned size; /* buffer size, zero if not allocated yet */ + unsigned want; /* requested buffer size, default is GZBUFSIZE */ + unsigned char *in; /* input buffer (double-sized when writing) */ + unsigned char *out; /* output buffer (double-sized when reading) */ + int direct; /* 0 if processing gzip, 1 if transparent */ + /* just for reading */ + int how; /* 0: get header, 1: copy, 2: decompress */ + z_off64_t start; /* where the gzip data started, for rewinding */ + int eof; /* true if end of input file reached */ + int past; /* true if read requested past end */ + /* just for writing */ + int level; /* compression level */ + int strategy; /* compression strategy */ + /* seek request */ + z_off64_t skip; /* amount to skip (already rewound if backwards) */ + int seek; /* true if seek request pending */ + /* error information */ + int err; /* error code */ + char *msg; /* error message */ + /* zlib inflate or deflate stream */ + z_stream strm; /* stream structure in-place (not a pointer) */ +} gz_state; +typedef gz_state FAR *gz_statep; + +/* shared functions */ +void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *)); +#if defined UNDER_CE +char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error)); +#endif + +/* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t + value -- needed when comparing unsigned to z_off64_t, which is signed + (possible z_off64_t types off_t, off64_t, and long are all signed) */ +#ifdef INT_MAX +# define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX) +#else +unsigned ZLIB_INTERNAL gz_intmax OF((void)); +# define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax()) +#endif diff --git a/third_party/java_tools/zlib/gzlib.c b/third_party/java_tools/zlib/gzlib.c new file mode 100644 index 000000000..4105e6aff --- /dev/null +++ b/third_party/java_tools/zlib/gzlib.c @@ -0,0 +1,637 @@ +/* gzlib.c -- zlib functions common to reading and writing gzip files + * Copyright (C) 2004-2017 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#include "gzguts.h" + +#if defined(_WIN32) && !defined(__BORLANDC__) && !defined(__MINGW32__) +# define LSEEK _lseeki64 +#else +#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0 +# define LSEEK lseek64 +#else +# define LSEEK lseek +#endif +#endif + +/* Local functions */ +local void gz_reset OF((gz_statep)); +local gzFile gz_open OF((const void *, int, const char *)); + +#if defined UNDER_CE + +/* Map the Windows error number in ERROR to a locale-dependent error message + string and return a pointer to it. Typically, the values for ERROR come + from GetLastError. + + The string pointed to shall not be modified by the application, but may be + overwritten by a subsequent call to gz_strwinerror + + The gz_strwinerror function does not change the current setting of + GetLastError. */ +char ZLIB_INTERNAL *gz_strwinerror (error) + DWORD error; +{ + static char buf[1024]; + + wchar_t *msgbuf; + DWORD lasterr = GetLastError(); + DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM + | FORMAT_MESSAGE_ALLOCATE_BUFFER, + NULL, + error, + 0, /* Default language */ + (LPVOID)&msgbuf, + 0, + NULL); + if (chars != 0) { + /* If there is an \r\n appended, zap it. */ + if (chars >= 2 + && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') { + chars -= 2; + msgbuf[chars] = 0; + } + + if (chars > sizeof (buf) - 1) { + chars = sizeof (buf) - 1; + msgbuf[chars] = 0; + } + + wcstombs(buf, msgbuf, chars + 1); + LocalFree(msgbuf); + } + else { + sprintf(buf, "unknown win32 error (%ld)", error); + } + + SetLastError(lasterr); + return buf; +} + +#endif /* UNDER_CE */ + +/* Reset gzip file state */ +local void gz_reset(state) + gz_statep state; +{ + state->x.have = 0; /* no output data available */ + if (state->mode == GZ_READ) { /* for reading ... */ + state->eof = 0; /* not at end of file */ + state->past = 0; /* have not read past end yet */ + state->how = LOOK; /* look for gzip header */ + } + state->seek = 0; /* no seek request pending */ + gz_error(state, Z_OK, NULL); /* clear error */ + state->x.pos = 0; /* no uncompressed data yet */ + state->strm.avail_in = 0; /* no input data yet */ +} + +/* Open a gzip file either by name or file descriptor. */ +local gzFile gz_open(path, fd, mode) + const void *path; + int fd; + const char *mode; +{ + gz_statep state; + z_size_t len; + int oflag; +#ifdef O_CLOEXEC + int cloexec = 0; +#endif +#ifdef O_EXCL + int exclusive = 0; +#endif + + /* check input */ + if (path == NULL) + return NULL; + + /* allocate gzFile structure to return */ + state = (gz_statep)malloc(sizeof(gz_state)); + if (state == NULL) + return NULL; + state->size = 0; /* no buffers allocated yet */ + state->want = GZBUFSIZE; /* requested buffer size */ + state->msg = NULL; /* no error message yet */ + + /* interpret mode */ + state->mode = GZ_NONE; + state->level = Z_DEFAULT_COMPRESSION; + state->strategy = Z_DEFAULT_STRATEGY; + state->direct = 0; + while (*mode) { + if (*mode >= '0' && *mode <= '9') + state->level = *mode - '0'; + else + switch (*mode) { + case 'r': + state->mode = GZ_READ; + break; +#ifndef NO_GZCOMPRESS + case 'w': + state->mode = GZ_WRITE; + break; + case 'a': + state->mode = GZ_APPEND; + break; +#endif + case '+': /* can't read and write at the same time */ + free(state); + return NULL; + case 'b': /* ignore -- will request binary anyway */ + break; +#ifdef O_CLOEXEC + case 'e': + cloexec = 1; + break; +#endif +#ifdef O_EXCL + case 'x': + exclusive = 1; + break; +#endif + case 'f': + state->strategy = Z_FILTERED; + break; + case 'h': + state->strategy = Z_HUFFMAN_ONLY; + break; + case 'R': + state->strategy = Z_RLE; + break; + case 'F': + state->strategy = Z_FIXED; + break; + case 'T': + state->direct = 1; + break; + default: /* could consider as an error, but just ignore */ + ; + } + mode++; + } + + /* must provide an "r", "w", or "a" */ + if (state->mode == GZ_NONE) { + free(state); + return NULL; + } + + /* can't force transparent read */ + if (state->mode == GZ_READ) { + if (state->direct) { + free(state); + return NULL; + } + state->direct = 1; /* for empty file */ + } + + /* save the path name for error messages */ +#ifdef WIDECHAR + if (fd == -2) { + len = wcstombs(NULL, path, 0); + if (len == (z_size_t)-1) + len = 0; + } + else +#endif + len = strlen((const char *)path); + state->path = (char *)malloc(len + 1); + if (state->path == NULL) { + free(state); + return NULL; + } +#ifdef WIDECHAR + if (fd == -2) + if (len) + wcstombs(state->path, path, len + 1); + else + *(state->path) = 0; + else +#endif +#if !defined(NO_snprintf) && !defined(NO_vsnprintf) + (void)snprintf(state->path, len + 1, "%s", (const char *)path); +#else + strcpy(state->path, path); +#endif + + /* compute the flags for open() */ + oflag = +#ifdef O_LARGEFILE + O_LARGEFILE | +#endif +#ifdef O_BINARY + O_BINARY | +#endif +#ifdef O_CLOEXEC + (cloexec ? O_CLOEXEC : 0) | +#endif + (state->mode == GZ_READ ? + O_RDONLY : + (O_WRONLY | O_CREAT | +#ifdef O_EXCL + (exclusive ? O_EXCL : 0) | +#endif + (state->mode == GZ_WRITE ? + O_TRUNC : + O_APPEND))); + + /* open the file with the appropriate flags (or just use fd) */ + state->fd = fd > -1 ? fd : ( +#ifdef WIDECHAR + fd == -2 ? _wopen(path, oflag, 0666) : +#endif + open((const char *)path, oflag, 0666)); + if (state->fd == -1) { + free(state->path); + free(state); + return NULL; + } + if (state->mode == GZ_APPEND) { + LSEEK(state->fd, 0, SEEK_END); /* so gzoffset() is correct */ + state->mode = GZ_WRITE; /* simplify later checks */ + } + + /* save the current position for rewinding (only if reading) */ + if (state->mode == GZ_READ) { + state->start = LSEEK(state->fd, 0, SEEK_CUR); + if (state->start == -1) state->start = 0; + } + + /* initialize stream */ + gz_reset(state); + + /* return stream */ + return (gzFile)state; +} + +/* -- see zlib.h -- */ +gzFile ZEXPORT gzopen(path, mode) + const char *path; + const char *mode; +{ + return gz_open(path, -1, mode); +} + +/* -- see zlib.h -- */ +gzFile ZEXPORT gzopen64(path, mode) + const char *path; + const char *mode; +{ + return gz_open(path, -1, mode); +} + +/* -- see zlib.h -- */ +gzFile ZEXPORT gzdopen(fd, mode) + int fd; + const char *mode; +{ + char *path; /* identifier for error messages */ + gzFile gz; + + if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL) + return NULL; +#if !defined(NO_snprintf) && !defined(NO_vsnprintf) + (void)snprintf(path, 7 + 3 * sizeof(int), "", fd); +#else + sprintf(path, "", fd); /* for debugging */ +#endif + gz = gz_open(path, fd, mode); + free(path); + return gz; +} + +/* -- see zlib.h -- */ +#ifdef WIDECHAR +gzFile ZEXPORT gzopen_w(path, mode) + const wchar_t *path; + const char *mode; +{ + return gz_open(path, -2, mode); +} +#endif + +/* -- see zlib.h -- */ +int ZEXPORT gzbuffer(file, size) + gzFile file; + unsigned size; +{ + gz_statep state; + + /* get internal structure and check integrity */ + if (file == NULL) + return -1; + state = (gz_statep)file; + if (state->mode != GZ_READ && state->mode != GZ_WRITE) + return -1; + + /* make sure we haven't already allocated memory */ + if (state->size != 0) + return -1; + + /* check and set requested size */ + if ((size << 1) < size) + return -1; /* need to be able to double it */ + if (size < 2) + size = 2; /* need two bytes to check magic header */ + state->want = size; + return 0; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzrewind(file) + gzFile file; +{ + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return -1; + state = (gz_statep)file; + + /* check that we're reading and that there's no error */ + if (state->mode != GZ_READ || + (state->err != Z_OK && state->err != Z_BUF_ERROR)) + return -1; + + /* back up and start over */ + if (LSEEK(state->fd, state->start, SEEK_SET) == -1) + return -1; + gz_reset(state); + return 0; +} + +/* -- see zlib.h -- */ +z_off64_t ZEXPORT gzseek64(file, offset, whence) + gzFile file; + z_off64_t offset; + int whence; +{ + unsigned n; + z_off64_t ret; + gz_statep state; + + /* get internal structure and check integrity */ + if (file == NULL) + return -1; + state = (gz_statep)file; + if (state->mode != GZ_READ && state->mode != GZ_WRITE) + return -1; + + /* check that there's no error */ + if (state->err != Z_OK && state->err != Z_BUF_ERROR) + return -1; + + /* can only seek from start or relative to current position */ + if (whence != SEEK_SET && whence != SEEK_CUR) + return -1; + + /* normalize offset to a SEEK_CUR specification */ + if (whence == SEEK_SET) + offset -= state->x.pos; + else if (state->seek) + offset += state->skip; + state->seek = 0; + + /* if within raw area while reading, just go there */ + if (state->mode == GZ_READ && state->how == COPY && + state->x.pos + offset >= 0) { + ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR); + if (ret == -1) + return -1; + state->x.have = 0; + state->eof = 0; + state->past = 0; + state->seek = 0; + gz_error(state, Z_OK, NULL); + state->strm.avail_in = 0; + state->x.pos += offset; + return state->x.pos; + } + + /* calculate skip amount, rewinding if needed for back seek when reading */ + if (offset < 0) { + if (state->mode != GZ_READ) /* writing -- can't go backwards */ + return -1; + offset += state->x.pos; + if (offset < 0) /* before start of file! */ + return -1; + if (gzrewind(file) == -1) /* rewind, then skip to offset */ + return -1; + } + + /* if reading, skip what's in output buffer (one less gzgetc() check) */ + if (state->mode == GZ_READ) { + n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ? + (unsigned)offset : state->x.have; + state->x.have -= n; + state->x.next += n; + state->x.pos += n; + offset -= n; + } + + /* request skip (if not zero) */ + if (offset) { + state->seek = 1; + state->skip = offset; + } + return state->x.pos + offset; +} + +/* -- see zlib.h -- */ +z_off_t ZEXPORT gzseek(file, offset, whence) + gzFile file; + z_off_t offset; + int whence; +{ + z_off64_t ret; + + ret = gzseek64(file, (z_off64_t)offset, whence); + return ret == (z_off_t)ret ? (z_off_t)ret : -1; +} + +/* -- see zlib.h -- */ +z_off64_t ZEXPORT gztell64(file) + gzFile file; +{ + gz_statep state; + + /* get internal structure and check integrity */ + if (file == NULL) + return -1; + state = (gz_statep)file; + if (state->mode != GZ_READ && state->mode != GZ_WRITE) + return -1; + + /* return position */ + return state->x.pos + (state->seek ? state->skip : 0); +} + +/* -- see zlib.h -- */ +z_off_t ZEXPORT gztell(file) + gzFile file; +{ + z_off64_t ret; + + ret = gztell64(file); + return ret == (z_off_t)ret ? (z_off_t)ret : -1; +} + +/* -- see zlib.h -- */ +z_off64_t ZEXPORT gzoffset64(file) + gzFile file; +{ + z_off64_t offset; + gz_statep state; + + /* get internal structure and check integrity */ + if (file == NULL) + return -1; + state = (gz_statep)file; + if (state->mode != GZ_READ && state->mode != GZ_WRITE) + return -1; + + /* compute and return effective offset in file */ + offset = LSEEK(state->fd, 0, SEEK_CUR); + if (offset == -1) + return -1; + if (state->mode == GZ_READ) /* reading */ + offset -= state->strm.avail_in; /* don't count buffered input */ + return offset; +} + +/* -- see zlib.h -- */ +z_off_t ZEXPORT gzoffset(file) + gzFile file; +{ + z_off64_t ret; + + ret = gzoffset64(file); + return ret == (z_off_t)ret ? (z_off_t)ret : -1; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzeof(file) + gzFile file; +{ + gz_statep state; + + /* get internal structure and check integrity */ + if (file == NULL) + return 0; + state = (gz_statep)file; + if (state->mode != GZ_READ && state->mode != GZ_WRITE) + return 0; + + /* return end-of-file state */ + return state->mode == GZ_READ ? state->past : 0; +} + +/* -- see zlib.h -- */ +const char * ZEXPORT gzerror(file, errnum) + gzFile file; + int *errnum; +{ + gz_statep state; + + /* get internal structure and check integrity */ + if (file == NULL) + return NULL; + state = (gz_statep)file; + if (state->mode != GZ_READ && state->mode != GZ_WRITE) + return NULL; + + /* return error information */ + if (errnum != NULL) + *errnum = state->err; + return state->err == Z_MEM_ERROR ? "out of memory" : + (state->msg == NULL ? "" : state->msg); +} + +/* -- see zlib.h -- */ +void ZEXPORT gzclearerr(file) + gzFile file; +{ + gz_statep state; + + /* get internal structure and check integrity */ + if (file == NULL) + return; + state = (gz_statep)file; + if (state->mode != GZ_READ && state->mode != GZ_WRITE) + return; + + /* clear error and end-of-file */ + if (state->mode == GZ_READ) { + state->eof = 0; + state->past = 0; + } + gz_error(state, Z_OK, NULL); +} + +/* Create an error message in allocated memory and set state->err and + state->msg accordingly. Free any previous error message already there. Do + not try to free or allocate space if the error is Z_MEM_ERROR (out of + memory). Simply save the error message as a static string. If there is an + allocation failure constructing the error message, then convert the error to + out of memory. */ +void ZLIB_INTERNAL gz_error(state, err, msg) + gz_statep state; + int err; + const char *msg; +{ + /* free previously allocated message and clear */ + if (state->msg != NULL) { + if (state->err != Z_MEM_ERROR) + free(state->msg); + state->msg = NULL; + } + + /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */ + if (err != Z_OK && err != Z_BUF_ERROR) + state->x.have = 0; + + /* set error code, and if no message, then done */ + state->err = err; + if (msg == NULL) + return; + + /* for an out of memory error, return literal string when requested */ + if (err == Z_MEM_ERROR) + return; + + /* construct error message with path */ + if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) == + NULL) { + state->err = Z_MEM_ERROR; + return; + } +#if !defined(NO_snprintf) && !defined(NO_vsnprintf) + (void)snprintf(state->msg, strlen(state->path) + strlen(msg) + 3, + "%s%s%s", state->path, ": ", msg); +#else + strcpy(state->msg, state->path); + strcat(state->msg, ": "); + strcat(state->msg, msg); +#endif +} + +#ifndef INT_MAX +/* portably return maximum value for an int (when limits.h presumed not + available) -- we need to do this to cover cases where 2's complement not + used, since C standard permits 1's complement and sign-bit representations, + otherwise we could just use ((unsigned)-1) >> 1 */ +unsigned ZLIB_INTERNAL gz_intmax() +{ + unsigned p, q; + + p = 1; + do { + q = p; + p <<= 1; + p++; + } while (p > q); + return q >> 1; +} +#endif diff --git a/third_party/java_tools/zlib/gzread.c b/third_party/java_tools/zlib/gzread.c new file mode 100644 index 000000000..956b91ea7 --- /dev/null +++ b/third_party/java_tools/zlib/gzread.c @@ -0,0 +1,654 @@ +/* gzread.c -- zlib functions for reading gzip files + * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013, 2016 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#include "gzguts.h" + +/* Local functions */ +local int gz_load OF((gz_statep, unsigned char *, unsigned, unsigned *)); +local int gz_avail OF((gz_statep)); +local int gz_look OF((gz_statep)); +local int gz_decomp OF((gz_statep)); +local int gz_fetch OF((gz_statep)); +local int gz_skip OF((gz_statep, z_off64_t)); +local z_size_t gz_read OF((gz_statep, voidp, z_size_t)); + +/* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from + state->fd, and update state->eof, state->err, and state->msg as appropriate. + This function needs to loop on read(), since read() is not guaranteed to + read the number of bytes requested, depending on the type of descriptor. */ +local int gz_load(state, buf, len, have) + gz_statep state; + unsigned char *buf; + unsigned len; + unsigned *have; +{ + int ret; + unsigned get, max = ((unsigned)-1 >> 2) + 1; + + *have = 0; + do { + get = len - *have; + if (get > max) + get = max; + ret = read(state->fd, buf + *have, get); + if (ret <= 0) + break; + *have += (unsigned)ret; + } while (*have < len); + if (ret < 0) { + gz_error(state, Z_ERRNO, zstrerror()); + return -1; + } + if (ret == 0) + state->eof = 1; + return 0; +} + +/* Load up input buffer and set eof flag if last data loaded -- return -1 on + error, 0 otherwise. Note that the eof flag is set when the end of the input + file is reached, even though there may be unused data in the buffer. Once + that data has been used, no more attempts will be made to read the file. + If strm->avail_in != 0, then the current data is moved to the beginning of + the input buffer, and then the remainder of the buffer is loaded with the + available data from the input file. */ +local int gz_avail(state) + gz_statep state; +{ + unsigned got; + z_streamp strm = &(state->strm); + + if (state->err != Z_OK && state->err != Z_BUF_ERROR) + return -1; + if (state->eof == 0) { + if (strm->avail_in) { /* copy what's there to the start */ + unsigned char *p = state->in; + unsigned const char *q = strm->next_in; + unsigned n = strm->avail_in; + do { + *p++ = *q++; + } while (--n); + } + if (gz_load(state, state->in + strm->avail_in, + state->size - strm->avail_in, &got) == -1) + return -1; + strm->avail_in += got; + strm->next_in = state->in; + } + return 0; +} + +/* Look for gzip header, set up for inflate or copy. state->x.have must be 0. + If this is the first time in, allocate required memory. state->how will be + left unchanged if there is no more input data available, will be set to COPY + if there is no gzip header and direct copying will be performed, or it will + be set to GZIP for decompression. If direct copying, then leftover input + data from the input buffer will be copied to the output buffer. In that + case, all further file reads will be directly to either the output buffer or + a user buffer. If decompressing, the inflate state will be initialized. + gz_look() will return 0 on success or -1 on failure. */ +local int gz_look(state) + gz_statep state; +{ + z_streamp strm = &(state->strm); + + /* allocate read buffers and inflate memory */ + if (state->size == 0) { + /* allocate buffers */ + state->in = (unsigned char *)malloc(state->want); + state->out = (unsigned char *)malloc(state->want << 1); + if (state->in == NULL || state->out == NULL) { + free(state->out); + free(state->in); + gz_error(state, Z_MEM_ERROR, "out of memory"); + return -1; + } + state->size = state->want; + + /* allocate inflate memory */ + state->strm.zalloc = Z_NULL; + state->strm.zfree = Z_NULL; + state->strm.opaque = Z_NULL; + state->strm.avail_in = 0; + state->strm.next_in = Z_NULL; + if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */ + free(state->out); + free(state->in); + state->size = 0; + gz_error(state, Z_MEM_ERROR, "out of memory"); + return -1; + } + } + + /* get at least the magic bytes in the input buffer */ + if (strm->avail_in < 2) { + if (gz_avail(state) == -1) + return -1; + if (strm->avail_in == 0) + return 0; + } + + /* look for gzip magic bytes -- if there, do gzip decoding (note: there is + a logical dilemma here when considering the case of a partially written + gzip file, to wit, if a single 31 byte is written, then we cannot tell + whether this is a single-byte file, or just a partially written gzip + file -- for here we assume that if a gzip file is being written, then + the header will be written in a single operation, so that reading a + single byte is sufficient indication that it is not a gzip file) */ + if (strm->avail_in > 1 && + strm->next_in[0] == 31 && strm->next_in[1] == 139) { + inflateReset(strm); + state->how = GZIP; + state->direct = 0; + return 0; + } + + /* no gzip header -- if we were decoding gzip before, then this is trailing + garbage. Ignore the trailing garbage and finish. */ + if (state->direct == 0) { + strm->avail_in = 0; + state->eof = 1; + state->x.have = 0; + return 0; + } + + /* doing raw i/o, copy any leftover input to output -- this assumes that + the output buffer is larger than the input buffer, which also assures + space for gzungetc() */ + state->x.next = state->out; + if (strm->avail_in) { + memcpy(state->x.next, strm->next_in, strm->avail_in); + state->x.have = strm->avail_in; + strm->avail_in = 0; + } + state->how = COPY; + state->direct = 1; + return 0; +} + +/* Decompress from input to the provided next_out and avail_out in the state. + On return, state->x.have and state->x.next point to the just decompressed + data. If the gzip stream completes, state->how is reset to LOOK to look for + the next gzip stream or raw data, once state->x.have is depleted. Returns 0 + on success, -1 on failure. */ +local int gz_decomp(state) + gz_statep state; +{ + int ret = Z_OK; + unsigned had; + z_streamp strm = &(state->strm); + + /* fill output buffer up to end of deflate stream */ + had = strm->avail_out; + do { + /* get more input for inflate() */ + if (strm->avail_in == 0 && gz_avail(state) == -1) + return -1; + if (strm->avail_in == 0) { + gz_error(state, Z_BUF_ERROR, "unexpected end of file"); + break; + } + + /* decompress and handle errors */ + ret = inflate(strm, Z_NO_FLUSH); + if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) { + gz_error(state, Z_STREAM_ERROR, + "internal error: inflate stream corrupt"); + return -1; + } + if (ret == Z_MEM_ERROR) { + gz_error(state, Z_MEM_ERROR, "out of memory"); + return -1; + } + if (ret == Z_DATA_ERROR) { /* deflate stream invalid */ + gz_error(state, Z_DATA_ERROR, + strm->msg == NULL ? "compressed data error" : strm->msg); + return -1; + } + } while (strm->avail_out && ret != Z_STREAM_END); + + /* update available output */ + state->x.have = had - strm->avail_out; + state->x.next = strm->next_out - state->x.have; + + /* if the gzip stream completed successfully, look for another */ + if (ret == Z_STREAM_END) + state->how = LOOK; + + /* good decompression */ + return 0; +} + +/* Fetch data and put it in the output buffer. Assumes state->x.have is 0. + Data is either copied from the input file or decompressed from the input + file depending on state->how. If state->how is LOOK, then a gzip header is + looked for to determine whether to copy or decompress. Returns -1 on error, + otherwise 0. gz_fetch() will leave state->how as COPY or GZIP unless the + end of the input file has been reached and all data has been processed. */ +local int gz_fetch(state) + gz_statep state; +{ + z_streamp strm = &(state->strm); + + do { + switch(state->how) { + case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */ + if (gz_look(state) == -1) + return -1; + if (state->how == LOOK) + return 0; + break; + case COPY: /* -> COPY */ + if (gz_load(state, state->out, state->size << 1, &(state->x.have)) + == -1) + return -1; + state->x.next = state->out; + return 0; + case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */ + strm->avail_out = state->size << 1; + strm->next_out = state->out; + if (gz_decomp(state) == -1) + return -1; + } + } while (state->x.have == 0 && (!state->eof || strm->avail_in)); + return 0; +} + +/* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */ +local int gz_skip(state, len) + gz_statep state; + z_off64_t len; +{ + unsigned n; + + /* skip over len bytes or reach end-of-file, whichever comes first */ + while (len) + /* skip over whatever is in output buffer */ + if (state->x.have) { + n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ? + (unsigned)len : state->x.have; + state->x.have -= n; + state->x.next += n; + state->x.pos += n; + len -= n; + } + + /* output buffer empty -- return if we're at the end of the input */ + else if (state->eof && state->strm.avail_in == 0) + break; + + /* need more data to skip -- load up output buffer */ + else { + /* get more output, looking for header if required */ + if (gz_fetch(state) == -1) + return -1; + } + return 0; +} + +/* Read len bytes into buf from file, or less than len up to the end of the + input. Return the number of bytes read. If zero is returned, either the + end of file was reached, or there was an error. state->err must be + consulted in that case to determine which. */ +local z_size_t gz_read(state, buf, len) + gz_statep state; + voidp buf; + z_size_t len; +{ + z_size_t got; + unsigned n; + + /* if len is zero, avoid unnecessary operations */ + if (len == 0) + return 0; + + /* process a skip request */ + if (state->seek) { + state->seek = 0; + if (gz_skip(state, state->skip) == -1) + return 0; + } + + /* get len bytes to buf, or less than len if at the end */ + got = 0; + do { + /* set n to the maximum amount of len that fits in an unsigned int */ + n = -1; + if (n > len) + n = len; + + /* first just try copying data from the output buffer */ + if (state->x.have) { + if (state->x.have < n) + n = state->x.have; + memcpy(buf, state->x.next, n); + state->x.next += n; + state->x.have -= n; + } + + /* output buffer empty -- return if we're at the end of the input */ + else if (state->eof && state->strm.avail_in == 0) { + state->past = 1; /* tried to read past end */ + break; + } + + /* need output data -- for small len or new stream load up our output + buffer */ + else if (state->how == LOOK || n < (state->size << 1)) { + /* get more output, looking for header if required */ + if (gz_fetch(state) == -1) + return 0; + continue; /* no progress yet -- go back to copy above */ + /* the copy above assures that we will leave with space in the + output buffer, allowing at least one gzungetc() to succeed */ + } + + /* large len -- read directly into user buffer */ + else if (state->how == COPY) { /* read directly */ + if (gz_load(state, (unsigned char *)buf, n, &n) == -1) + return 0; + } + + /* large len -- decompress directly into user buffer */ + else { /* state->how == GZIP */ + state->strm.avail_out = n; + state->strm.next_out = (unsigned char *)buf; + if (gz_decomp(state) == -1) + return 0; + n = state->x.have; + state->x.have = 0; + } + + /* update progress */ + len -= n; + buf = (char *)buf + n; + got += n; + state->x.pos += n; + } while (len); + + /* return number of bytes read into user buffer */ + return got; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzread(file, buf, len) + gzFile file; + voidp buf; + unsigned len; +{ + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return -1; + state = (gz_statep)file; + + /* check that we're reading and that there's no (serious) error */ + if (state->mode != GZ_READ || + (state->err != Z_OK && state->err != Z_BUF_ERROR)) + return -1; + + /* since an int is returned, make sure len fits in one, otherwise return + with an error (this avoids a flaw in the interface) */ + if ((int)len < 0) { + gz_error(state, Z_STREAM_ERROR, "request does not fit in an int"); + return -1; + } + + /* read len or fewer bytes to buf */ + len = gz_read(state, buf, len); + + /* check for an error */ + if (len == 0 && state->err != Z_OK && state->err != Z_BUF_ERROR) + return -1; + + /* return the number of bytes read (this is assured to fit in an int) */ + return (int)len; +} + +/* -- see zlib.h -- */ +z_size_t ZEXPORT gzfread(buf, size, nitems, file) + voidp buf; + z_size_t size; + z_size_t nitems; + gzFile file; +{ + z_size_t len; + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return 0; + state = (gz_statep)file; + + /* check that we're reading and that there's no (serious) error */ + if (state->mode != GZ_READ || + (state->err != Z_OK && state->err != Z_BUF_ERROR)) + return 0; + + /* compute bytes to read -- error on overflow */ + len = nitems * size; + if (size && len / size != nitems) { + gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t"); + return 0; + } + + /* read len or fewer bytes to buf, return the number of full items read */ + return len ? gz_read(state, buf, len) / size : 0; +} + +/* -- see zlib.h -- */ +#ifdef Z_PREFIX_SET +# undef z_gzgetc +#else +# undef gzgetc +#endif +int ZEXPORT gzgetc(file) + gzFile file; +{ + int ret; + unsigned char buf[1]; + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return -1; + state = (gz_statep)file; + + /* check that we're reading and that there's no (serious) error */ + if (state->mode != GZ_READ || + (state->err != Z_OK && state->err != Z_BUF_ERROR)) + return -1; + + /* try output buffer (no need to check for skip request) */ + if (state->x.have) { + state->x.have--; + state->x.pos++; + return *(state->x.next)++; + } + + /* nothing there -- try gz_read() */ + ret = gz_read(state, buf, 1); + return ret < 1 ? -1 : buf[0]; +} + +int ZEXPORT gzgetc_(file) +gzFile file; +{ + return gzgetc(file); +} + +/* -- see zlib.h -- */ +int ZEXPORT gzungetc(c, file) + int c; + gzFile file; +{ + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return -1; + state = (gz_statep)file; + + /* check that we're reading and that there's no (serious) error */ + if (state->mode != GZ_READ || + (state->err != Z_OK && state->err != Z_BUF_ERROR)) + return -1; + + /* process a skip request */ + if (state->seek) { + state->seek = 0; + if (gz_skip(state, state->skip) == -1) + return -1; + } + + /* can't push EOF */ + if (c < 0) + return -1; + + /* if output buffer empty, put byte at end (allows more pushing) */ + if (state->x.have == 0) { + state->x.have = 1; + state->x.next = state->out + (state->size << 1) - 1; + state->x.next[0] = (unsigned char)c; + state->x.pos--; + state->past = 0; + return c; + } + + /* if no room, give up (must have already done a gzungetc()) */ + if (state->x.have == (state->size << 1)) { + gz_error(state, Z_DATA_ERROR, "out of room to push characters"); + return -1; + } + + /* slide output data if needed and insert byte before existing data */ + if (state->x.next == state->out) { + unsigned char *src = state->out + state->x.have; + unsigned char *dest = state->out + (state->size << 1); + while (src > state->out) + *--dest = *--src; + state->x.next = dest; + } + state->x.have++; + state->x.next--; + state->x.next[0] = (unsigned char)c; + state->x.pos--; + state->past = 0; + return c; +} + +/* -- see zlib.h -- */ +char * ZEXPORT gzgets(file, buf, len) + gzFile file; + char *buf; + int len; +{ + unsigned left, n; + char *str; + unsigned char *eol; + gz_statep state; + + /* check parameters and get internal structure */ + if (file == NULL || buf == NULL || len < 1) + return NULL; + state = (gz_statep)file; + + /* check that we're reading and that there's no (serious) error */ + if (state->mode != GZ_READ || + (state->err != Z_OK && state->err != Z_BUF_ERROR)) + return NULL; + + /* process a skip request */ + if (state->seek) { + state->seek = 0; + if (gz_skip(state, state->skip) == -1) + return NULL; + } + + /* copy output bytes up to new line or len - 1, whichever comes first -- + append a terminating zero to the string (we don't check for a zero in + the contents, let the user worry about that) */ + str = buf; + left = (unsigned)len - 1; + if (left) do { + /* assure that something is in the output buffer */ + if (state->x.have == 0 && gz_fetch(state) == -1) + return NULL; /* error */ + if (state->x.have == 0) { /* end of file */ + state->past = 1; /* read past end */ + break; /* return what we have */ + } + + /* look for end-of-line in current output buffer */ + n = state->x.have > left ? left : state->x.have; + eol = (unsigned char *)memchr(state->x.next, '\n', n); + if (eol != NULL) + n = (unsigned)(eol - state->x.next) + 1; + + /* copy through end-of-line, or remainder if not found */ + memcpy(buf, state->x.next, n); + state->x.have -= n; + state->x.next += n; + state->x.pos += n; + left -= n; + buf += n; + } while (left && eol == NULL); + + /* return terminated string, or if nothing, end of file */ + if (buf == str) + return NULL; + buf[0] = 0; + return str; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzdirect(file) + gzFile file; +{ + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return 0; + state = (gz_statep)file; + + /* if the state is not known, but we can find out, then do so (this is + mainly for right after a gzopen() or gzdopen()) */ + if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0) + (void)gz_look(state); + + /* return 1 if transparent, 0 if processing a gzip stream */ + return state->direct; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzclose_r(file) + gzFile file; +{ + int ret, err; + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return Z_STREAM_ERROR; + state = (gz_statep)file; + + /* check that we're reading */ + if (state->mode != GZ_READ) + return Z_STREAM_ERROR; + + /* free memory and close file */ + if (state->size) { + inflateEnd(&(state->strm)); + free(state->out); + free(state->in); + } + err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK; + gz_error(state, Z_OK, NULL); + free(state->path); + ret = close(state->fd); + free(state); + return ret ? Z_ERRNO : err; +} diff --git a/third_party/java_tools/zlib/gzwrite.c b/third_party/java_tools/zlib/gzwrite.c new file mode 100644 index 000000000..c7b5651d7 --- /dev/null +++ b/third_party/java_tools/zlib/gzwrite.c @@ -0,0 +1,665 @@ +/* gzwrite.c -- zlib functions for writing gzip files + * Copyright (C) 2004-2017 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#include "gzguts.h" + +/* Local functions */ +local int gz_init OF((gz_statep)); +local int gz_comp OF((gz_statep, int)); +local int gz_zero OF((gz_statep, z_off64_t)); +local z_size_t gz_write OF((gz_statep, voidpc, z_size_t)); + +/* Initialize state for writing a gzip file. Mark initialization by setting + state->size to non-zero. Return -1 on a memory allocation failure, or 0 on + success. */ +local int gz_init(state) + gz_statep state; +{ + int ret; + z_streamp strm = &(state->strm); + + /* allocate input buffer (double size for gzprintf) */ + state->in = (unsigned char *)malloc(state->want << 1); + if (state->in == NULL) { + gz_error(state, Z_MEM_ERROR, "out of memory"); + return -1; + } + + /* only need output buffer and deflate state if compressing */ + if (!state->direct) { + /* allocate output buffer */ + state->out = (unsigned char *)malloc(state->want); + if (state->out == NULL) { + free(state->in); + gz_error(state, Z_MEM_ERROR, "out of memory"); + return -1; + } + + /* allocate deflate memory, set up for gzip compression */ + strm->zalloc = Z_NULL; + strm->zfree = Z_NULL; + strm->opaque = Z_NULL; + ret = deflateInit2(strm, state->level, Z_DEFLATED, + MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy); + if (ret != Z_OK) { + free(state->out); + free(state->in); + gz_error(state, Z_MEM_ERROR, "out of memory"); + return -1; + } + strm->next_in = NULL; + } + + /* mark state as initialized */ + state->size = state->want; + + /* initialize write buffer if compressing */ + if (!state->direct) { + strm->avail_out = state->size; + strm->next_out = state->out; + state->x.next = strm->next_out; + } + return 0; +} + +/* Compress whatever is at avail_in and next_in and write to the output file. + Return -1 if there is an error writing to the output file or if gz_init() + fails to allocate memory, otherwise 0. flush is assumed to be a valid + deflate() flush value. If flush is Z_FINISH, then the deflate() state is + reset to start a new gzip stream. If gz->direct is true, then simply write + to the output file without compressing, and ignore flush. */ +local int gz_comp(state, flush) + gz_statep state; + int flush; +{ + int ret, writ; + unsigned have, put, max = ((unsigned)-1 >> 2) + 1; + z_streamp strm = &(state->strm); + + /* allocate memory if this is the first time through */ + if (state->size == 0 && gz_init(state) == -1) + return -1; + + /* write directly if requested */ + if (state->direct) { + while (strm->avail_in) { + put = strm->avail_in > max ? max : strm->avail_in; + writ = write(state->fd, strm->next_in, put); + if (writ < 0) { + gz_error(state, Z_ERRNO, zstrerror()); + return -1; + } + strm->avail_in -= (unsigned)writ; + strm->next_in += writ; + } + return 0; + } + + /* run deflate() on provided input until it produces no more output */ + ret = Z_OK; + do { + /* write out current buffer contents if full, or if flushing, but if + doing Z_FINISH then don't write until we get to Z_STREAM_END */ + if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && + (flush != Z_FINISH || ret == Z_STREAM_END))) { + while (strm->next_out > state->x.next) { + put = strm->next_out - state->x.next > (int)max ? max : + (unsigned)(strm->next_out - state->x.next); + writ = write(state->fd, state->x.next, put); + if (writ < 0) { + gz_error(state, Z_ERRNO, zstrerror()); + return -1; + } + state->x.next += writ; + } + if (strm->avail_out == 0) { + strm->avail_out = state->size; + strm->next_out = state->out; + state->x.next = state->out; + } + } + + /* compress */ + have = strm->avail_out; + ret = deflate(strm, flush); + if (ret == Z_STREAM_ERROR) { + gz_error(state, Z_STREAM_ERROR, + "internal error: deflate stream corrupt"); + return -1; + } + have -= strm->avail_out; + } while (have); + + /* if that completed a deflate stream, allow another to start */ + if (flush == Z_FINISH) + deflateReset(strm); + + /* all done, no errors */ + return 0; +} + +/* Compress len zeros to output. Return -1 on a write error or memory + allocation failure by gz_comp(), or 0 on success. */ +local int gz_zero(state, len) + gz_statep state; + z_off64_t len; +{ + int first; + unsigned n; + z_streamp strm = &(state->strm); + + /* consume whatever's left in the input buffer */ + if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) + return -1; + + /* compress len zeros (len guaranteed > 0) */ + first = 1; + while (len) { + n = GT_OFF(state->size) || (z_off64_t)state->size > len ? + (unsigned)len : state->size; + if (first) { + memset(state->in, 0, n); + first = 0; + } + strm->avail_in = n; + strm->next_in = state->in; + state->x.pos += n; + if (gz_comp(state, Z_NO_FLUSH) == -1) + return -1; + len -= n; + } + return 0; +} + +/* Write len bytes from buf to file. Return the number of bytes written. If + the returned value is less than len, then there was an error. */ +local z_size_t gz_write(state, buf, len) + gz_statep state; + voidpc buf; + z_size_t len; +{ + z_size_t put = len; + + /* if len is zero, avoid unnecessary operations */ + if (len == 0) + return 0; + + /* allocate memory if this is the first time through */ + if (state->size == 0 && gz_init(state) == -1) + return 0; + + /* check for seek request */ + if (state->seek) { + state->seek = 0; + if (gz_zero(state, state->skip) == -1) + return 0; + } + + /* for small len, copy to input buffer, otherwise compress directly */ + if (len < state->size) { + /* copy to input buffer, compress when full */ + do { + unsigned have, copy; + + if (state->strm.avail_in == 0) + state->strm.next_in = state->in; + have = (unsigned)((state->strm.next_in + state->strm.avail_in) - + state->in); + copy = state->size - have; + if (copy > len) + copy = len; + memcpy(state->in + have, buf, copy); + state->strm.avail_in += copy; + state->x.pos += copy; + buf = (const char *)buf + copy; + len -= copy; + if (len && gz_comp(state, Z_NO_FLUSH) == -1) + return 0; + } while (len); + } + else { + /* consume whatever's left in the input buffer */ + if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1) + return 0; + + /* directly compress user buffer to file */ + state->strm.next_in = (z_const Bytef *)buf; + do { + unsigned n = (unsigned)-1; + if (n > len) + n = len; + state->strm.avail_in = n; + state->x.pos += n; + if (gz_comp(state, Z_NO_FLUSH) == -1) + return 0; + len -= n; + } while (len); + } + + /* input was all buffered or compressed */ + return put; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzwrite(file, buf, len) + gzFile file; + voidpc buf; + unsigned len; +{ + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return 0; + state = (gz_statep)file; + + /* check that we're writing and that there's no error */ + if (state->mode != GZ_WRITE || state->err != Z_OK) + return 0; + + /* since an int is returned, make sure len fits in one, otherwise return + with an error (this avoids a flaw in the interface) */ + if ((int)len < 0) { + gz_error(state, Z_DATA_ERROR, "requested length does not fit in int"); + return 0; + } + + /* write len bytes from buf (the return value will fit in an int) */ + return (int)gz_write(state, buf, len); +} + +/* -- see zlib.h -- */ +z_size_t ZEXPORT gzfwrite(buf, size, nitems, file) + voidpc buf; + z_size_t size; + z_size_t nitems; + gzFile file; +{ + z_size_t len; + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return 0; + state = (gz_statep)file; + + /* check that we're writing and that there's no error */ + if (state->mode != GZ_WRITE || state->err != Z_OK) + return 0; + + /* compute bytes to read -- error on overflow */ + len = nitems * size; + if (size && len / size != nitems) { + gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t"); + return 0; + } + + /* write len bytes to buf, return the number of full items written */ + return len ? gz_write(state, buf, len) / size : 0; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzputc(file, c) + gzFile file; + int c; +{ + unsigned have; + unsigned char buf[1]; + gz_statep state; + z_streamp strm; + + /* get internal structure */ + if (file == NULL) + return -1; + state = (gz_statep)file; + strm = &(state->strm); + + /* check that we're writing and that there's no error */ + if (state->mode != GZ_WRITE || state->err != Z_OK) + return -1; + + /* check for seek request */ + if (state->seek) { + state->seek = 0; + if (gz_zero(state, state->skip) == -1) + return -1; + } + + /* try writing to input buffer for speed (state->size == 0 if buffer not + initialized) */ + if (state->size) { + if (strm->avail_in == 0) + strm->next_in = state->in; + have = (unsigned)((strm->next_in + strm->avail_in) - state->in); + if (have < state->size) { + state->in[have] = (unsigned char)c; + strm->avail_in++; + state->x.pos++; + return c & 0xff; + } + } + + /* no room in buffer or not initialized, use gz_write() */ + buf[0] = (unsigned char)c; + if (gz_write(state, buf, 1) != 1) + return -1; + return c & 0xff; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzputs(file, str) + gzFile file; + const char *str; +{ + int ret; + z_size_t len; + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return -1; + state = (gz_statep)file; + + /* check that we're writing and that there's no error */ + if (state->mode != GZ_WRITE || state->err != Z_OK) + return -1; + + /* write string */ + len = strlen(str); + ret = gz_write(state, str, len); + return ret == 0 && len != 0 ? -1 : ret; +} + +#if defined(STDC) || defined(Z_HAVE_STDARG_H) +#include + +/* -- see zlib.h -- */ +int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) +{ + int len; + unsigned left; + char *next; + gz_statep state; + z_streamp strm; + + /* get internal structure */ + if (file == NULL) + return Z_STREAM_ERROR; + state = (gz_statep)file; + strm = &(state->strm); + + /* check that we're writing and that there's no error */ + if (state->mode != GZ_WRITE || state->err != Z_OK) + return Z_STREAM_ERROR; + + /* make sure we have some buffer space */ + if (state->size == 0 && gz_init(state) == -1) + return state->err; + + /* check for seek request */ + if (state->seek) { + state->seek = 0; + if (gz_zero(state, state->skip) == -1) + return state->err; + } + + /* do the printf() into the input buffer, put length in len -- the input + buffer is double-sized just for this function, so there is guaranteed to + be state->size bytes available after the current contents */ + if (strm->avail_in == 0) + strm->next_in = state->in; + next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in); + next[state->size - 1] = 0; +#ifdef NO_vsnprintf +# ifdef HAS_vsprintf_void + (void)vsprintf(next, format, va); + for (len = 0; len < state->size; len++) + if (next[len] == 0) break; +# else + len = vsprintf(next, format, va); +# endif +#else +# ifdef HAS_vsnprintf_void + (void)vsnprintf(next, state->size, format, va); + len = strlen(next); +# else + len = vsnprintf(next, state->size, format, va); +# endif +#endif + + /* check that printf() results fit in buffer */ + if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0) + return 0; + + /* update buffer and position, compress first half if past that */ + strm->avail_in += (unsigned)len; + state->x.pos += len; + if (strm->avail_in >= state->size) { + left = strm->avail_in - state->size; + strm->avail_in = state->size; + if (gz_comp(state, Z_NO_FLUSH) == -1) + return state->err; + memcpy(state->in, state->in + state->size, left); + strm->next_in = state->in; + strm->avail_in = left; + } + return len; +} + +int ZEXPORTVA gzprintf(gzFile file, const char *format, ...) +{ + va_list va; + int ret; + + va_start(va, format); + ret = gzvprintf(file, format, va); + va_end(va); + return ret; +} + +#else /* !STDC && !Z_HAVE_STDARG_H */ + +/* -- see zlib.h -- */ +int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, + a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) + gzFile file; + const char *format; + int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, + a11, a12, a13, a14, a15, a16, a17, a18, a19, a20; +{ + unsigned len, left; + char *next; + gz_statep state; + z_streamp strm; + + /* get internal structure */ + if (file == NULL) + return Z_STREAM_ERROR; + state = (gz_statep)file; + strm = &(state->strm); + + /* check that can really pass pointer in ints */ + if (sizeof(int) != sizeof(void *)) + return Z_STREAM_ERROR; + + /* check that we're writing and that there's no error */ + if (state->mode != GZ_WRITE || state->err != Z_OK) + return Z_STREAM_ERROR; + + /* make sure we have some buffer space */ + if (state->size == 0 && gz_init(state) == -1) + return state->error; + + /* check for seek request */ + if (state->seek) { + state->seek = 0; + if (gz_zero(state, state->skip) == -1) + return state->error; + } + + /* do the printf() into the input buffer, put length in len -- the input + buffer is double-sized just for this function, so there is guaranteed to + be state->size bytes available after the current contents */ + if (strm->avail_in == 0) + strm->next_in = state->in; + next = (char *)(strm->next_in + strm->avail_in); + next[state->size - 1] = 0; +#ifdef NO_snprintf +# ifdef HAS_sprintf_void + sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, + a13, a14, a15, a16, a17, a18, a19, a20); + for (len = 0; len < size; len++) + if (next[len] == 0) + break; +# else + len = sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, + a12, a13, a14, a15, a16, a17, a18, a19, a20); +# endif +#else +# ifdef HAS_snprintf_void + snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, + a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); + len = strlen(next); +# else + len = snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, + a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); +# endif +#endif + + /* check that printf() results fit in buffer */ + if (len == 0 || len >= state->size || next[state->size - 1] != 0) + return 0; + + /* update buffer and position, compress first half if past that */ + strm->avail_in += len; + state->x.pos += len; + if (strm->avail_in >= state->size) { + left = strm->avail_in - state->size; + strm->avail_in = state->size; + if (gz_comp(state, Z_NO_FLUSH) == -1) + return state->err; + memcpy(state->in, state->in + state->size, left); + strm->next_in = state->in; + strm->avail_in = left; + } + return (int)len; +} + +#endif + +/* -- see zlib.h -- */ +int ZEXPORT gzflush(file, flush) + gzFile file; + int flush; +{ + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return Z_STREAM_ERROR; + state = (gz_statep)file; + + /* check that we're writing and that there's no error */ + if (state->mode != GZ_WRITE || state->err != Z_OK) + return Z_STREAM_ERROR; + + /* check flush parameter */ + if (flush < 0 || flush > Z_FINISH) + return Z_STREAM_ERROR; + + /* check for seek request */ + if (state->seek) { + state->seek = 0; + if (gz_zero(state, state->skip) == -1) + return state->err; + } + + /* compress remaining data with requested flush */ + (void)gz_comp(state, flush); + return state->err; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzsetparams(file, level, strategy) + gzFile file; + int level; + int strategy; +{ + gz_statep state; + z_streamp strm; + + /* get internal structure */ + if (file == NULL) + return Z_STREAM_ERROR; + state = (gz_statep)file; + strm = &(state->strm); + + /* check that we're writing and that there's no error */ + if (state->mode != GZ_WRITE || state->err != Z_OK) + return Z_STREAM_ERROR; + + /* if no change is requested, then do nothing */ + if (level == state->level && strategy == state->strategy) + return Z_OK; + + /* check for seek request */ + if (state->seek) { + state->seek = 0; + if (gz_zero(state, state->skip) == -1) + return state->err; + } + + /* change compression parameters for subsequent input */ + if (state->size) { + /* flush previous input with previous parameters before changing */ + if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1) + return state->err; + deflateParams(strm, level, strategy); + } + state->level = level; + state->strategy = strategy; + return Z_OK; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzclose_w(file) + gzFile file; +{ + int ret = Z_OK; + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return Z_STREAM_ERROR; + state = (gz_statep)file; + + /* check that we're writing */ + if (state->mode != GZ_WRITE) + return Z_STREAM_ERROR; + + /* check for seek request */ + if (state->seek) { + state->seek = 0; + if (gz_zero(state, state->skip) == -1) + ret = state->err; + } + + /* flush, free memory, and close file */ + if (gz_comp(state, Z_FINISH) == -1) + ret = state->err; + if (state->size) { + if (!state->direct) { + (void)deflateEnd(&(state->strm)); + free(state->out); + } + free(state->in); + } + gz_error(state, Z_OK, NULL); + free(state->path); + if (close(state->fd) == -1) + ret = Z_ERRNO; + free(state); + return ret; +} diff --git a/third_party/java_tools/zlib/infback.c b/third_party/java_tools/zlib/infback.c new file mode 100644 index 000000000..59679ecbf --- /dev/null +++ b/third_party/java_tools/zlib/infback.c @@ -0,0 +1,640 @@ +/* infback.c -- inflate using a call-back interface + * Copyright (C) 1995-2016 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* + This code is largely copied from inflate.c. Normally either infback.o or + inflate.o would be linked into an application--not both. The interface + with inffast.c is retained so that optimized assembler-coded versions of + inflate_fast() can be used with either inflate.c or infback.c. + */ + +#include "zutil.h" +#include "inftrees.h" +#include "inflate.h" +#include "inffast.h" + +/* function prototypes */ +local void fixedtables OF((struct inflate_state FAR *state)); + +/* + strm provides memory allocation functions in zalloc and zfree, or + Z_NULL to use the library memory allocation functions. + + windowBits is in the range 8..15, and window is a user-supplied + window and output buffer that is 2**windowBits bytes. + */ +int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size) +z_streamp strm; +int windowBits; +unsigned char FAR *window; +const char *version; +int stream_size; +{ + struct inflate_state FAR *state; + + if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || + stream_size != (int)(sizeof(z_stream))) + return Z_VERSION_ERROR; + if (strm == Z_NULL || window == Z_NULL || + windowBits < 8 || windowBits > 15) + return Z_STREAM_ERROR; + strm->msg = Z_NULL; /* in case we return an error */ + if (strm->zalloc == (alloc_func)0) { +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else + strm->zalloc = zcalloc; + strm->opaque = (voidpf)0; +#endif + } + if (strm->zfree == (free_func)0) +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else + strm->zfree = zcfree; +#endif + state = (struct inflate_state FAR *)ZALLOC(strm, 1, + sizeof(struct inflate_state)); + if (state == Z_NULL) return Z_MEM_ERROR; + Tracev((stderr, "inflate: allocated\n")); + strm->state = (struct internal_state FAR *)state; + state->dmax = 32768U; + state->wbits = (uInt)windowBits; + state->wsize = 1U << windowBits; + state->window = window; + state->wnext = 0; + state->whave = 0; + return Z_OK; +} + +/* + Return state with length and distance decoding tables and index sizes set to + fixed code decoding. Normally this returns fixed tables from inffixed.h. + If BUILDFIXED is defined, then instead this routine builds the tables the + first time it's called, and returns those tables the first time and + thereafter. This reduces the size of the code by about 2K bytes, in + exchange for a little execution time. However, BUILDFIXED should not be + used for threaded applications, since the rewriting of the tables and virgin + may not be thread-safe. + */ +local void fixedtables(state) +struct inflate_state FAR *state; +{ +#ifdef BUILDFIXED + static int virgin = 1; + static code *lenfix, *distfix; + static code fixed[544]; + + /* build fixed huffman tables if first call (may not be thread safe) */ + if (virgin) { + unsigned sym, bits; + static code *next; + + /* literal/length table */ + sym = 0; + while (sym < 144) state->lens[sym++] = 8; + while (sym < 256) state->lens[sym++] = 9; + while (sym < 280) state->lens[sym++] = 7; + while (sym < 288) state->lens[sym++] = 8; + next = fixed; + lenfix = next; + bits = 9; + inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work); + + /* distance table */ + sym = 0; + while (sym < 32) state->lens[sym++] = 5; + distfix = next; + bits = 5; + inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); + + /* do this just once */ + virgin = 0; + } +#else /* !BUILDFIXED */ +# include "inffixed.h" +#endif /* BUILDFIXED */ + state->lencode = lenfix; + state->lenbits = 9; + state->distcode = distfix; + state->distbits = 5; +} + +/* Macros for inflateBack(): */ + +/* Load returned state from inflate_fast() */ +#define LOAD() \ + do { \ + put = strm->next_out; \ + left = strm->avail_out; \ + next = strm->next_in; \ + have = strm->avail_in; \ + hold = state->hold; \ + bits = state->bits; \ + } while (0) + +/* Set state from registers for inflate_fast() */ +#define RESTORE() \ + do { \ + strm->next_out = put; \ + strm->avail_out = left; \ + strm->next_in = next; \ + strm->avail_in = have; \ + state->hold = hold; \ + state->bits = bits; \ + } while (0) + +/* Clear the input bit accumulator */ +#define INITBITS() \ + do { \ + hold = 0; \ + bits = 0; \ + } while (0) + +/* Assure that some input is available. If input is requested, but denied, + then return a Z_BUF_ERROR from inflateBack(). */ +#define PULL() \ + do { \ + if (have == 0) { \ + have = in(in_desc, &next); \ + if (have == 0) { \ + next = Z_NULL; \ + ret = Z_BUF_ERROR; \ + goto inf_leave; \ + } \ + } \ + } while (0) + +/* Get a byte of input into the bit accumulator, or return from inflateBack() + with an error if there is no input available. */ +#define PULLBYTE() \ + do { \ + PULL(); \ + have--; \ + hold += (unsigned long)(*next++) << bits; \ + bits += 8; \ + } while (0) + +/* Assure that there are at least n bits in the bit accumulator. If there is + not enough available input to do that, then return from inflateBack() with + an error. */ +#define NEEDBITS(n) \ + do { \ + while (bits < (unsigned)(n)) \ + PULLBYTE(); \ + } while (0) + +/* Return the low n bits of the bit accumulator (n < 16) */ +#define BITS(n) \ + ((unsigned)hold & ((1U << (n)) - 1)) + +/* Remove n bits from the bit accumulator */ +#define DROPBITS(n) \ + do { \ + hold >>= (n); \ + bits -= (unsigned)(n); \ + } while (0) + +/* Remove zero to seven bits as needed to go to a byte boundary */ +#define BYTEBITS() \ + do { \ + hold >>= bits & 7; \ + bits -= bits & 7; \ + } while (0) + +/* Assure that some output space is available, by writing out the window + if it's full. If the write fails, return from inflateBack() with a + Z_BUF_ERROR. */ +#define ROOM() \ + do { \ + if (left == 0) { \ + put = state->window; \ + left = state->wsize; \ + state->whave = left; \ + if (out(out_desc, put, left)) { \ + ret = Z_BUF_ERROR; \ + goto inf_leave; \ + } \ + } \ + } while (0) + +/* + strm provides the memory allocation functions and window buffer on input, + and provides information on the unused input on return. For Z_DATA_ERROR + returns, strm will also provide an error message. + + in() and out() are the call-back input and output functions. When + inflateBack() needs more input, it calls in(). When inflateBack() has + filled the window with output, or when it completes with data in the + window, it calls out() to write out the data. The application must not + change the provided input until in() is called again or inflateBack() + returns. The application must not change the window/output buffer until + inflateBack() returns. + + in() and out() are called with a descriptor parameter provided in the + inflateBack() call. This parameter can be a structure that provides the + information required to do the read or write, as well as accumulated + information on the input and output such as totals and check values. + + in() should return zero on failure. out() should return non-zero on + failure. If either in() or out() fails, than inflateBack() returns a + Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it + was in() or out() that caused in the error. Otherwise, inflateBack() + returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format + error, or Z_MEM_ERROR if it could not allocate memory for the state. + inflateBack() can also return Z_STREAM_ERROR if the input parameters + are not correct, i.e. strm is Z_NULL or the state was not initialized. + */ +int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc) +z_streamp strm; +in_func in; +void FAR *in_desc; +out_func out; +void FAR *out_desc; +{ + struct inflate_state FAR *state; + z_const unsigned char FAR *next; /* next input */ + unsigned char FAR *put; /* next output */ + unsigned have, left; /* available input and output */ + unsigned long hold; /* bit buffer */ + unsigned bits; /* bits in bit buffer */ + unsigned copy; /* number of stored or match bytes to copy */ + unsigned char FAR *from; /* where to copy match bytes from */ + code here; /* current decoding table entry */ + code last; /* parent table entry */ + unsigned len; /* length to copy for repeats, bits to drop */ + int ret; /* return code */ + static const unsigned short order[19] = /* permutation of code lengths */ + {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; + + /* Check that the strm exists and that the state was initialized */ + if (strm == Z_NULL || strm->state == Z_NULL) + return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + + /* Reset the state */ + strm->msg = Z_NULL; + state->mode = TYPE; + state->last = 0; + state->whave = 0; + next = strm->next_in; + have = next != Z_NULL ? strm->avail_in : 0; + hold = 0; + bits = 0; + put = state->window; + left = state->wsize; + + /* Inflate until end of block marked as last */ + for (;;) + switch (state->mode) { + case TYPE: + /* determine and dispatch block type */ + if (state->last) { + BYTEBITS(); + state->mode = DONE; + break; + } + NEEDBITS(3); + state->last = BITS(1); + DROPBITS(1); + switch (BITS(2)) { + case 0: /* stored block */ + Tracev((stderr, "inflate: stored block%s\n", + state->last ? " (last)" : "")); + state->mode = STORED; + break; + case 1: /* fixed block */ + fixedtables(state); + Tracev((stderr, "inflate: fixed codes block%s\n", + state->last ? " (last)" : "")); + state->mode = LEN; /* decode codes */ + break; + case 2: /* dynamic block */ + Tracev((stderr, "inflate: dynamic codes block%s\n", + state->last ? " (last)" : "")); + state->mode = TABLE; + break; + case 3: + strm->msg = (char *)"invalid block type"; + state->mode = BAD; + } + DROPBITS(2); + break; + + case STORED: + /* get and verify stored block length */ + BYTEBITS(); /* go to byte boundary */ + NEEDBITS(32); + if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { + strm->msg = (char *)"invalid stored block lengths"; + state->mode = BAD; + break; + } + state->length = (unsigned)hold & 0xffff; + Tracev((stderr, "inflate: stored length %u\n", + state->length)); + INITBITS(); + + /* copy stored block from input to output */ + while (state->length != 0) { + copy = state->length; + PULL(); + ROOM(); + if (copy > have) copy = have; + if (copy > left) copy = left; + zmemcpy(put, next, copy); + have -= copy; + next += copy; + left -= copy; + put += copy; + state->length -= copy; + } + Tracev((stderr, "inflate: stored end\n")); + state->mode = TYPE; + break; + + case TABLE: + /* get dynamic table entries descriptor */ + NEEDBITS(14); + state->nlen = BITS(5) + 257; + DROPBITS(5); + state->ndist = BITS(5) + 1; + DROPBITS(5); + state->ncode = BITS(4) + 4; + DROPBITS(4); +#ifndef PKZIP_BUG_WORKAROUND + if (state->nlen > 286 || state->ndist > 30) { + strm->msg = (char *)"too many length or distance symbols"; + state->mode = BAD; + break; + } +#endif + Tracev((stderr, "inflate: table sizes ok\n")); + + /* get code length code lengths (not a typo) */ + state->have = 0; + while (state->have < state->ncode) { + NEEDBITS(3); + state->lens[order[state->have++]] = (unsigned short)BITS(3); + DROPBITS(3); + } + while (state->have < 19) + state->lens[order[state->have++]] = 0; + state->next = state->codes; + state->lencode = (code const FAR *)(state->next); + state->lenbits = 7; + ret = inflate_table(CODES, state->lens, 19, &(state->next), + &(state->lenbits), state->work); + if (ret) { + strm->msg = (char *)"invalid code lengths set"; + state->mode = BAD; + break; + } + Tracev((stderr, "inflate: code lengths ok\n")); + + /* get length and distance code code lengths */ + state->have = 0; + while (state->have < state->nlen + state->ndist) { + for (;;) { + here = state->lencode[BITS(state->lenbits)]; + if ((unsigned)(here.bits) <= bits) break; + PULLBYTE(); + } + if (here.val < 16) { + DROPBITS(here.bits); + state->lens[state->have++] = here.val; + } + else { + if (here.val == 16) { + NEEDBITS(here.bits + 2); + DROPBITS(here.bits); + if (state->have == 0) { + strm->msg = (char *)"invalid bit length repeat"; + state->mode = BAD; + break; + } + len = (unsigned)(state->lens[state->have - 1]); + copy = 3 + BITS(2); + DROPBITS(2); + } + else if (here.val == 17) { + NEEDBITS(here.bits + 3); + DROPBITS(here.bits); + len = 0; + copy = 3 + BITS(3); + DROPBITS(3); + } + else { + NEEDBITS(here.bits + 7); + DROPBITS(here.bits); + len = 0; + copy = 11 + BITS(7); + DROPBITS(7); + } + if (state->have + copy > state->nlen + state->ndist) { + strm->msg = (char *)"invalid bit length repeat"; + state->mode = BAD; + break; + } + while (copy--) + state->lens[state->have++] = (unsigned short)len; + } + } + + /* handle error breaks in while */ + if (state->mode == BAD) break; + + /* check for end-of-block code (better have one) */ + if (state->lens[256] == 0) { + strm->msg = (char *)"invalid code -- missing end-of-block"; + state->mode = BAD; + break; + } + + /* build code tables -- note: do not change the lenbits or distbits + values here (9 and 6) without reading the comments in inftrees.h + concerning the ENOUGH constants, which depend on those values */ + state->next = state->codes; + state->lencode = (code const FAR *)(state->next); + state->lenbits = 9; + ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), + &(state->lenbits), state->work); + if (ret) { + strm->msg = (char *)"invalid literal/lengths set"; + state->mode = BAD; + break; + } + state->distcode = (code const FAR *)(state->next); + state->distbits = 6; + ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, + &(state->next), &(state->distbits), state->work); + if (ret) { + strm->msg = (char *)"invalid distances set"; + state->mode = BAD; + break; + } + Tracev((stderr, "inflate: codes ok\n")); + state->mode = LEN; + + case LEN: + /* use inflate_fast() if we have enough input and output */ + if (have >= 6 && left >= 258) { + RESTORE(); + if (state->whave < state->wsize) + state->whave = state->wsize - left; + inflate_fast(strm, state->wsize); + LOAD(); + break; + } + + /* get a literal, length, or end-of-block code */ + for (;;) { + here = state->lencode[BITS(state->lenbits)]; + if ((unsigned)(here.bits) <= bits) break; + PULLBYTE(); + } + if (here.op && (here.op & 0xf0) == 0) { + last = here; + for (;;) { + here = state->lencode[last.val + + (BITS(last.bits + last.op) >> last.bits)]; + if ((unsigned)(last.bits + here.bits) <= bits) break; + PULLBYTE(); + } + DROPBITS(last.bits); + } + DROPBITS(here.bits); + state->length = (unsigned)here.val; + + /* process literal */ + if (here.op == 0) { + Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? + "inflate: literal '%c'\n" : + "inflate: literal 0x%02x\n", here.val)); + ROOM(); + *put++ = (unsigned char)(state->length); + left--; + state->mode = LEN; + break; + } + + /* process end of block */ + if (here.op & 32) { + Tracevv((stderr, "inflate: end of block\n")); + state->mode = TYPE; + break; + } + + /* invalid code */ + if (here.op & 64) { + strm->msg = (char *)"invalid literal/length code"; + state->mode = BAD; + break; + } + + /* length code -- get extra bits, if any */ + state->extra = (unsigned)(here.op) & 15; + if (state->extra != 0) { + NEEDBITS(state->extra); + state->length += BITS(state->extra); + DROPBITS(state->extra); + } + Tracevv((stderr, "inflate: length %u\n", state->length)); + + /* get distance code */ + for (;;) { + here = state->distcode[BITS(state->distbits)]; + if ((unsigned)(here.bits) <= bits) break; + PULLBYTE(); + } + if ((here.op & 0xf0) == 0) { + last = here; + for (;;) { + here = state->distcode[last.val + + (BITS(last.bits + last.op) >> last.bits)]; + if ((unsigned)(last.bits + here.bits) <= bits) break; + PULLBYTE(); + } + DROPBITS(last.bits); + } + DROPBITS(here.bits); + if (here.op & 64) { + strm->msg = (char *)"invalid distance code"; + state->mode = BAD; + break; + } + state->offset = (unsigned)here.val; + + /* get distance extra bits, if any */ + state->extra = (unsigned)(here.op) & 15; + if (state->extra != 0) { + NEEDBITS(state->extra); + state->offset += BITS(state->extra); + DROPBITS(state->extra); + } + if (state->offset > state->wsize - (state->whave < state->wsize ? + left : 0)) { + strm->msg = (char *)"invalid distance too far back"; + state->mode = BAD; + break; + } + Tracevv((stderr, "inflate: distance %u\n", state->offset)); + + /* copy match from window to output */ + do { + ROOM(); + copy = state->wsize - state->offset; + if (copy < left) { + from = put + copy; + copy = left - copy; + } + else { + from = put - state->offset; + copy = left; + } + if (copy > state->length) copy = state->length; + state->length -= copy; + left -= copy; + do { + *put++ = *from++; + } while (--copy); + } while (state->length != 0); + break; + + case DONE: + /* inflate stream terminated properly -- write leftover output */ + ret = Z_STREAM_END; + if (left < state->wsize) { + if (out(out_desc, state->window, state->wsize - left)) + ret = Z_BUF_ERROR; + } + goto inf_leave; + + case BAD: + ret = Z_DATA_ERROR; + goto inf_leave; + + default: /* can't happen, but makes compilers happy */ + ret = Z_STREAM_ERROR; + goto inf_leave; + } + + /* Return unused input */ + inf_leave: + strm->next_in = next; + strm->avail_in = have; + return ret; +} + +int ZEXPORT inflateBackEnd(strm) +z_streamp strm; +{ + if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) + return Z_STREAM_ERROR; + ZFREE(strm, strm->state); + strm->state = Z_NULL; + Tracev((stderr, "inflate: end\n")); + return Z_OK; +} diff --git a/third_party/java_tools/zlib/inffast.c b/third_party/java_tools/zlib/inffast.c new file mode 100644 index 000000000..0dbd1dbc0 --- /dev/null +++ b/third_party/java_tools/zlib/inffast.c @@ -0,0 +1,323 @@ +/* inffast.c -- fast decoding + * Copyright (C) 1995-2017 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#include "zutil.h" +#include "inftrees.h" +#include "inflate.h" +#include "inffast.h" + +#ifdef ASMINF +# pragma message("Assembler code may have bugs -- use at your own risk") +#else + +/* + Decode literal, length, and distance codes and write out the resulting + literal and match bytes until either not enough input or output is + available, an end-of-block is encountered, or a data error is encountered. + When large enough input and output buffers are supplied to inflate(), for + example, a 16K input buffer and a 64K output buffer, more than 95% of the + inflate execution time is spent in this routine. + + Entry assumptions: + + state->mode == LEN + strm->avail_in >= 6 + strm->avail_out >= 258 + start >= strm->avail_out + state->bits < 8 + + On return, state->mode is one of: + + LEN -- ran out of enough output space or enough available input + TYPE -- reached end of block code, inflate() to interpret next block + BAD -- error in block data + + Notes: + + - The maximum input bits used by a length/distance pair is 15 bits for the + length code, 5 bits for the length extra, 15 bits for the distance code, + and 13 bits for the distance extra. This totals 48 bits, or six bytes. + Therefore if strm->avail_in >= 6, then there is enough input to avoid + checking for available input while decoding. + + - The maximum bytes that a single length/distance pair can output is 258 + bytes, which is the maximum length that can be coded. inflate_fast() + requires strm->avail_out >= 258 for each loop to avoid checking for + output space. + */ +void ZLIB_INTERNAL inflate_fast(strm, start) +z_streamp strm; +unsigned start; /* inflate()'s starting value for strm->avail_out */ +{ + struct inflate_state FAR *state; + z_const unsigned char FAR *in; /* local strm->next_in */ + z_const unsigned char FAR *last; /* have enough input while in < last */ + unsigned char FAR *out; /* local strm->next_out */ + unsigned char FAR *beg; /* inflate()'s initial strm->next_out */ + unsigned char FAR *end; /* while out < end, enough space available */ +#ifdef INFLATE_STRICT + unsigned dmax; /* maximum distance from zlib header */ +#endif + unsigned wsize; /* window size or zero if not using window */ + unsigned whave; /* valid bytes in the window */ + unsigned wnext; /* window write index */ + unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */ + unsigned long hold; /* local strm->hold */ + unsigned bits; /* local strm->bits */ + code const FAR *lcode; /* local strm->lencode */ + code const FAR *dcode; /* local strm->distcode */ + unsigned lmask; /* mask for first level of length codes */ + unsigned dmask; /* mask for first level of distance codes */ + code here; /* retrieved table entry */ + unsigned op; /* code bits, operation, extra bits, or */ + /* window position, window bytes to copy */ + unsigned len; /* match length, unused bytes */ + unsigned dist; /* match distance */ + unsigned char FAR *from; /* where to copy match from */ + + /* copy state to local variables */ + state = (struct inflate_state FAR *)strm->state; + in = strm->next_in; + last = in + (strm->avail_in - 5); + out = strm->next_out; + beg = out - (start - strm->avail_out); + end = out + (strm->avail_out - 257); +#ifdef INFLATE_STRICT + dmax = state->dmax; +#endif + wsize = state->wsize; + whave = state->whave; + wnext = state->wnext; + window = state->window; + hold = state->hold; + bits = state->bits; + lcode = state->lencode; + dcode = state->distcode; + lmask = (1U << state->lenbits) - 1; + dmask = (1U << state->distbits) - 1; + + /* decode literals and length/distances until end-of-block or not enough + input data or output space */ + do { + if (bits < 15) { + hold += (unsigned long)(*in++) << bits; + bits += 8; + hold += (unsigned long)(*in++) << bits; + bits += 8; + } + here = lcode[hold & lmask]; + dolen: + op = (unsigned)(here.bits); + hold >>= op; + bits -= op; + op = (unsigned)(here.op); + if (op == 0) { /* literal */ + Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? + "inflate: literal '%c'\n" : + "inflate: literal 0x%02x\n", here.val)); + *out++ = (unsigned char)(here.val); + } + else if (op & 16) { /* length base */ + len = (unsigned)(here.val); + op &= 15; /* number of extra bits */ + if (op) { + if (bits < op) { + hold += (unsigned long)(*in++) << bits; + bits += 8; + } + len += (unsigned)hold & ((1U << op) - 1); + hold >>= op; + bits -= op; + } + Tracevv((stderr, "inflate: length %u\n", len)); + if (bits < 15) { + hold += (unsigned long)(*in++) << bits; + bits += 8; + hold += (unsigned long)(*in++) << bits; + bits += 8; + } + here = dcode[hold & dmask]; + dodist: + op = (unsigned)(here.bits); + hold >>= op; + bits -= op; + op = (unsigned)(here.op); + if (op & 16) { /* distance base */ + dist = (unsigned)(here.val); + op &= 15; /* number of extra bits */ + if (bits < op) { + hold += (unsigned long)(*in++) << bits; + bits += 8; + if (bits < op) { + hold += (unsigned long)(*in++) << bits; + bits += 8; + } + } + dist += (unsigned)hold & ((1U << op) - 1); +#ifdef INFLATE_STRICT + if (dist > dmax) { + strm->msg = (char *)"invalid distance too far back"; + state->mode = BAD; + break; + } +#endif + hold >>= op; + bits -= op; + Tracevv((stderr, "inflate: distance %u\n", dist)); + op = (unsigned)(out - beg); /* max distance in output */ + if (dist > op) { /* see if copy from window */ + op = dist - op; /* distance back in window */ + if (op > whave) { + if (state->sane) { + strm->msg = + (char *)"invalid distance too far back"; + state->mode = BAD; + break; + } +#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR + if (len <= op - whave) { + do { + *out++ = 0; + } while (--len); + continue; + } + len -= op - whave; + do { + *out++ = 0; + } while (--op > whave); + if (op == 0) { + from = out - dist; + do { + *out++ = *from++; + } while (--len); + continue; + } +#endif + } + from = window; + if (wnext == 0) { /* very common case */ + from += wsize - op; + if (op < len) { /* some from window */ + len -= op; + do { + *out++ = *from++; + } while (--op); + from = out - dist; /* rest from output */ + } + } + else if (wnext < op) { /* wrap around window */ + from += wsize + wnext - op; + op -= wnext; + if (op < len) { /* some from end of window */ + len -= op; + do { + *out++ = *from++; + } while (--op); + from = window; + if (wnext < len) { /* some from start of window */ + op = wnext; + len -= op; + do { + *out++ = *from++; + } while (--op); + from = out - dist; /* rest from output */ + } + } + } + else { /* contiguous in window */ + from += wnext - op; + if (op < len) { /* some from window */ + len -= op; + do { + *out++ = *from++; + } while (--op); + from = out - dist; /* rest from output */ + } + } + while (len > 2) { + *out++ = *from++; + *out++ = *from++; + *out++ = *from++; + len -= 3; + } + if (len) { + *out++ = *from++; + if (len > 1) + *out++ = *from++; + } + } + else { + from = out - dist; /* copy direct from output */ + do { /* minimum length is three */ + *out++ = *from++; + *out++ = *from++; + *out++ = *from++; + len -= 3; + } while (len > 2); + if (len) { + *out++ = *from++; + if (len > 1) + *out++ = *from++; + } + } + } + else if ((op & 64) == 0) { /* 2nd level distance code */ + here = dcode[here.val + (hold & ((1U << op) - 1))]; + goto dodist; + } + else { + strm->msg = (char *)"invalid distance code"; + state->mode = BAD; + break; + } + } + else if ((op & 64) == 0) { /* 2nd level length code */ + here = lcode[here.val + (hold & ((1U << op) - 1))]; + goto dolen; + } + else if (op & 32) { /* end-of-block */ + Tracevv((stderr, "inflate: end of block\n")); + state->mode = TYPE; + break; + } + else { + strm->msg = (char *)"invalid literal/length code"; + state->mode = BAD; + break; + } + } while (in < last && out < end); + + /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ + len = bits >> 3; + in -= len; + bits -= len << 3; + hold &= (1U << bits) - 1; + + /* update state and return */ + strm->next_in = in; + strm->next_out = out; + strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last)); + strm->avail_out = (unsigned)(out < end ? + 257 + (end - out) : 257 - (out - end)); + state->hold = hold; + state->bits = bits; + return; +} + +/* + inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe): + - Using bit fields for code structure + - Different op definition to avoid & for extra bits (do & for table bits) + - Three separate decoding do-loops for direct, window, and wnext == 0 + - Special case for distance > 1 copies to do overlapped load and store copy + - Explicit branch predictions (based on measured branch probabilities) + - Deferring match copy and interspersed it with decoding subsequent codes + - Swapping literal/length else + - Swapping window/direct else + - Larger unrolled copy loops (three is about right) + - Moving len -= 3 statement into middle of loop + */ + +#endif /* !ASMINF */ diff --git a/third_party/java_tools/zlib/inffast.h b/third_party/java_tools/zlib/inffast.h new file mode 100644 index 000000000..e5c1aa4ca --- /dev/null +++ b/third_party/java_tools/zlib/inffast.h @@ -0,0 +1,11 @@ +/* inffast.h -- header to use inffast.c + * Copyright (C) 1995-2003, 2010 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* WARNING: this file should *not* be used by applications. It is + part of the implementation of the compression library and is + subject to change. Applications should only use zlib.h. + */ + +void ZLIB_INTERNAL inflate_fast OF((z_streamp strm, unsigned start)); diff --git a/third_party/java_tools/zlib/inffixed.h b/third_party/java_tools/zlib/inffixed.h new file mode 100644 index 000000000..d62832776 --- /dev/null +++ b/third_party/java_tools/zlib/inffixed.h @@ -0,0 +1,94 @@ + /* inffixed.h -- table for decoding fixed codes + * Generated automatically by makefixed(). + */ + + /* WARNING: this file should *not* be used by applications. + It is part of the implementation of this library and is + subject to change. Applications should only use zlib.h. + */ + + static const code lenfix[512] = { + {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48}, + {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128}, + {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59}, + {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176}, + {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20}, + {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100}, + {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8}, + {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216}, + {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76}, + {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114}, + {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2}, + {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148}, + {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42}, + {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86}, + {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15}, + {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236}, + {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62}, + {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142}, + {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31}, + {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162}, + {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25}, + {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105}, + {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4}, + {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202}, + {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69}, + {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125}, + {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13}, + {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195}, + {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35}, + {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91}, + {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19}, + {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246}, + {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55}, + {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135}, + {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99}, + {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190}, + {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16}, + {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96}, + {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6}, + {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209}, + {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72}, + {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116}, + {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4}, + {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153}, + {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44}, + {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82}, + {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11}, + {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229}, + {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58}, + {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138}, + {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51}, + {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173}, + {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30}, + {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110}, + {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0}, + {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195}, + {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65}, + {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121}, + {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9}, + {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258}, + {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37}, + {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93}, + {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23}, + {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251}, + {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51}, + {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131}, + {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67}, + {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183}, + {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23}, + {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103}, + {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9}, + {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223}, + {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79}, + {0,9,255} + }; + + static const code distfix[32] = { + {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025}, + {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193}, + {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385}, + {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577}, + {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073}, + {22,5,193},{64,5,0} + }; diff --git a/third_party/java_tools/zlib/inflate.c b/third_party/java_tools/zlib/inflate.c new file mode 100644 index 000000000..ac333e8c2 --- /dev/null +++ b/third_party/java_tools/zlib/inflate.c @@ -0,0 +1,1561 @@ +/* inflate.c -- zlib decompression + * Copyright (C) 1995-2016 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* + * Change history: + * + * 1.2.beta0 24 Nov 2002 + * - First version -- complete rewrite of inflate to simplify code, avoid + * creation of window when not needed, minimize use of window when it is + * needed, make inffast.c even faster, implement gzip decoding, and to + * improve code readability and style over the previous zlib inflate code + * + * 1.2.beta1 25 Nov 2002 + * - Use pointers for available input and output checking in inffast.c + * - Remove input and output counters in inffast.c + * - Change inffast.c entry and loop from avail_in >= 7 to >= 6 + * - Remove unnecessary second byte pull from length extra in inffast.c + * - Unroll direct copy to three copies per loop in inffast.c + * + * 1.2.beta2 4 Dec 2002 + * - Change external routine names to reduce potential conflicts + * - Correct filename to inffixed.h for fixed tables in inflate.c + * - Make hbuf[] unsigned char to match parameter type in inflate.c + * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset) + * to avoid negation problem on Alphas (64 bit) in inflate.c + * + * 1.2.beta3 22 Dec 2002 + * - Add comments on state->bits assertion in inffast.c + * - Add comments on op field in inftrees.h + * - Fix bug in reuse of allocated window after inflateReset() + * - Remove bit fields--back to byte structure for speed + * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths + * - Change post-increments to pre-increments in inflate_fast(), PPC biased? + * - Add compile time option, POSTINC, to use post-increments instead (Intel?) + * - Make MATCH copy in inflate() much faster for when inflate_fast() not used + * - Use local copies of stream next and avail values, as well as local bit + * buffer and bit count in inflate()--for speed when inflate_fast() not used + * + * 1.2.beta4 1 Jan 2003 + * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings + * - Move a comment on output buffer sizes from inffast.c to inflate.c + * - Add comments in inffast.c to introduce the inflate_fast() routine + * - Rearrange window copies in inflate_fast() for speed and simplification + * - Unroll last copy for window match in inflate_fast() + * - Use local copies of window variables in inflate_fast() for speed + * - Pull out common wnext == 0 case for speed in inflate_fast() + * - Make op and len in inflate_fast() unsigned for consistency + * - Add FAR to lcode and dcode declarations in inflate_fast() + * - Simplified bad distance check in inflate_fast() + * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new + * source file infback.c to provide a call-back interface to inflate for + * programs like gzip and unzip -- uses window as output buffer to avoid + * window copying + * + * 1.2.beta5 1 Jan 2003 + * - Improved inflateBack() interface to allow the caller to provide initial + * input in strm. + * - Fixed stored blocks bug in inflateBack() + * + * 1.2.beta6 4 Jan 2003 + * - Added comments in inffast.c on effectiveness of POSTINC + * - Typecasting all around to reduce compiler warnings + * - Changed loops from while (1) or do {} while (1) to for (;;), again to + * make compilers happy + * - Changed type of window in inflateBackInit() to unsigned char * + * + * 1.2.beta7 27 Jan 2003 + * - Changed many types to unsigned or unsigned short to avoid warnings + * - Added inflateCopy() function + * + * 1.2.0 9 Mar 2003 + * - Changed inflateBack() interface to provide separate opaque descriptors + * for the in() and out() functions + * - Changed inflateBack() argument and in_func typedef to swap the length + * and buffer address return values for the input function + * - Check next_in and next_out for Z_NULL on entry to inflate() + * + * The history for versions after 1.2.0 are in ChangeLog in zlib distribution. + */ + +#include "zutil.h" +#include "inftrees.h" +#include "inflate.h" +#include "inffast.h" + +#ifdef MAKEFIXED +# ifndef BUILDFIXED +# define BUILDFIXED +# endif +#endif + +/* function prototypes */ +local int inflateStateCheck OF((z_streamp strm)); +local void fixedtables OF((struct inflate_state FAR *state)); +local int updatewindow OF((z_streamp strm, const unsigned char FAR *end, + unsigned copy)); +#ifdef BUILDFIXED + void makefixed OF((void)); +#endif +local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf, + unsigned len)); + +local int inflateStateCheck(strm) +z_streamp strm; +{ + struct inflate_state FAR *state; + if (strm == Z_NULL || + strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) + return 1; + state = (struct inflate_state FAR *)strm->state; + if (state == Z_NULL || state->strm != strm || + state->mode < HEAD || state->mode > SYNC) + return 1; + return 0; +} + +int ZEXPORT inflateResetKeep(strm) +z_streamp strm; +{ + struct inflate_state FAR *state; + + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + strm->total_in = strm->total_out = state->total = 0; + strm->msg = Z_NULL; + if (state->wrap) /* to support ill-conceived Java test suite */ + strm->adler = state->wrap & 1; + state->mode = HEAD; + state->last = 0; + state->havedict = 0; + state->dmax = 32768U; + state->head = Z_NULL; + state->hold = 0; + state->bits = 0; + state->lencode = state->distcode = state->next = state->codes; + state->sane = 1; + state->back = -1; + Tracev((stderr, "inflate: reset\n")); + return Z_OK; +} + +int ZEXPORT inflateReset(strm) +z_streamp strm; +{ + struct inflate_state FAR *state; + + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + state->wsize = 0; + state->whave = 0; + state->wnext = 0; + return inflateResetKeep(strm); +} + +int ZEXPORT inflateReset2(strm, windowBits) +z_streamp strm; +int windowBits; +{ + int wrap; + struct inflate_state FAR *state; + + /* get the state */ + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + + /* extract wrap request from windowBits parameter */ + if (windowBits < 0) { + wrap = 0; + windowBits = -windowBits; + } + else { + wrap = (windowBits >> 4) + 5; +#ifdef GUNZIP + if (windowBits < 48) + windowBits &= 15; +#endif + } + + /* set number of window bits, free window if different */ + if (windowBits && (windowBits < 8 || windowBits > 15)) + return Z_STREAM_ERROR; + if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) { + ZFREE(strm, state->window); + state->window = Z_NULL; + } + + /* update state and reset the rest of it */ + state->wrap = wrap; + state->wbits = (unsigned)windowBits; + return inflateReset(strm); +} + +int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size) +z_streamp strm; +int windowBits; +const char *version; +int stream_size; +{ + int ret; + struct inflate_state FAR *state; + + if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || + stream_size != (int)(sizeof(z_stream))) + return Z_VERSION_ERROR; + if (strm == Z_NULL) return Z_STREAM_ERROR; + strm->msg = Z_NULL; /* in case we return an error */ + if (strm->zalloc == (alloc_func)0) { +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else + strm->zalloc = zcalloc; + strm->opaque = (voidpf)0; +#endif + } + if (strm->zfree == (free_func)0) +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else + strm->zfree = zcfree; +#endif + state = (struct inflate_state FAR *) + ZALLOC(strm, 1, sizeof(struct inflate_state)); + if (state == Z_NULL) return Z_MEM_ERROR; + Tracev((stderr, "inflate: allocated\n")); + strm->state = (struct internal_state FAR *)state; + state->strm = strm; + state->window = Z_NULL; + state->mode = HEAD; /* to pass state test in inflateReset2() */ + ret = inflateReset2(strm, windowBits); + if (ret != Z_OK) { + ZFREE(strm, state); + strm->state = Z_NULL; + } + return ret; +} + +int ZEXPORT inflateInit_(strm, version, stream_size) +z_streamp strm; +const char *version; +int stream_size; +{ + return inflateInit2_(strm, DEF_WBITS, version, stream_size); +} + +int ZEXPORT inflatePrime(strm, bits, value) +z_streamp strm; +int bits; +int value; +{ + struct inflate_state FAR *state; + + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + if (bits < 0) { + state->hold = 0; + state->bits = 0; + return Z_OK; + } + if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR; + value &= (1L << bits) - 1; + state->hold += (unsigned)value << state->bits; + state->bits += (uInt)bits; + return Z_OK; +} + +/* + Return state with length and distance decoding tables and index sizes set to + fixed code decoding. Normally this returns fixed tables from inffixed.h. + If BUILDFIXED is defined, then instead this routine builds the tables the + first time it's called, and returns those tables the first time and + thereafter. This reduces the size of the code by about 2K bytes, in + exchange for a little execution time. However, BUILDFIXED should not be + used for threaded applications, since the rewriting of the tables and virgin + may not be thread-safe. + */ +local void fixedtables(state) +struct inflate_state FAR *state; +{ +#ifdef BUILDFIXED + static int virgin = 1; + static code *lenfix, *distfix; + static code fixed[544]; + + /* build fixed huffman tables if first call (may not be thread safe) */ + if (virgin) { + unsigned sym, bits; + static code *next; + + /* literal/length table */ + sym = 0; + while (sym < 144) state->lens[sym++] = 8; + while (sym < 256) state->lens[sym++] = 9; + while (sym < 280) state->lens[sym++] = 7; + while (sym < 288) state->lens[sym++] = 8; + next = fixed; + lenfix = next; + bits = 9; + inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work); + + /* distance table */ + sym = 0; + while (sym < 32) state->lens[sym++] = 5; + distfix = next; + bits = 5; + inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); + + /* do this just once */ + virgin = 0; + } +#else /* !BUILDFIXED */ +# include "inffixed.h" +#endif /* BUILDFIXED */ + state->lencode = lenfix; + state->lenbits = 9; + state->distcode = distfix; + state->distbits = 5; +} + +#ifdef MAKEFIXED +#include + +/* + Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also + defines BUILDFIXED, so the tables are built on the fly. makefixed() writes + those tables to stdout, which would be piped to inffixed.h. A small program + can simply call makefixed to do this: + + void makefixed(void); + + int main(void) + { + makefixed(); + return 0; + } + + Then that can be linked with zlib built with MAKEFIXED defined and run: + + a.out > inffixed.h + */ +void makefixed() +{ + unsigned low, size; + struct inflate_state state; + + fixedtables(&state); + puts(" /* inffixed.h -- table for decoding fixed codes"); + puts(" * Generated automatically by makefixed()."); + puts(" */"); + puts(""); + puts(" /* WARNING: this file should *not* be used by applications."); + puts(" It is part of the implementation of this library and is"); + puts(" subject to change. Applications should only use zlib.h."); + puts(" */"); + puts(""); + size = 1U << 9; + printf(" static const code lenfix[%u] = {", size); + low = 0; + for (;;) { + if ((low % 7) == 0) printf("\n "); + printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op, + state.lencode[low].bits, state.lencode[low].val); + if (++low == size) break; + putchar(','); + } + puts("\n };"); + size = 1U << 5; + printf("\n static const code distfix[%u] = {", size); + low = 0; + for (;;) { + if ((low % 6) == 0) printf("\n "); + printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits, + state.distcode[low].val); + if (++low == size) break; + putchar(','); + } + puts("\n };"); +} +#endif /* MAKEFIXED */ + +/* + Update the window with the last wsize (normally 32K) bytes written before + returning. If window does not exist yet, create it. This is only called + when a window is already in use, or when output has been written during this + inflate call, but the end of the deflate stream has not been reached yet. + It is also called to create a window for dictionary data when a dictionary + is loaded. + + Providing output buffers larger than 32K to inflate() should provide a speed + advantage, since only the last 32K of output is copied to the sliding window + upon return from inflate(), and since all distances after the first 32K of + output will fall in the output data, making match copies simpler and faster. + The advantage may be dependent on the size of the processor's data caches. + */ +local int updatewindow(strm, end, copy) +z_streamp strm; +const Bytef *end; +unsigned copy; +{ + struct inflate_state FAR *state; + unsigned dist; + + state = (struct inflate_state FAR *)strm->state; + + /* if it hasn't been done already, allocate space for the window */ + if (state->window == Z_NULL) { + state->window = (unsigned char FAR *) + ZALLOC(strm, 1U << state->wbits, + sizeof(unsigned char)); + if (state->window == Z_NULL) return 1; + } + + /* if window not in use yet, initialize */ + if (state->wsize == 0) { + state->wsize = 1U << state->wbits; + state->wnext = 0; + state->whave = 0; + } + + /* copy state->wsize or less output bytes into the circular window */ + if (copy >= state->wsize) { + zmemcpy(state->window, end - state->wsize, state->wsize); + state->wnext = 0; + state->whave = state->wsize; + } + else { + dist = state->wsize - state->wnext; + if (dist > copy) dist = copy; + zmemcpy(state->window + state->wnext, end - copy, dist); + copy -= dist; + if (copy) { + zmemcpy(state->window, end - copy, copy); + state->wnext = copy; + state->whave = state->wsize; + } + else { + state->wnext += dist; + if (state->wnext == state->wsize) state->wnext = 0; + if (state->whave < state->wsize) state->whave += dist; + } + } + return 0; +} + +/* Macros for inflate(): */ + +/* check function to use adler32() for zlib or crc32() for gzip */ +#ifdef GUNZIP +# define UPDATE(check, buf, len) \ + (state->flags ? crc32(check, buf, len) : adler32(check, buf, len)) +#else +# define UPDATE(check, buf, len) adler32(check, buf, len) +#endif + +/* check macros for header crc */ +#ifdef GUNZIP +# define CRC2(check, word) \ + do { \ + hbuf[0] = (unsigned char)(word); \ + hbuf[1] = (unsigned char)((word) >> 8); \ + check = crc32(check, hbuf, 2); \ + } while (0) + +# define CRC4(check, word) \ + do { \ + hbuf[0] = (unsigned char)(word); \ + hbuf[1] = (unsigned char)((word) >> 8); \ + hbuf[2] = (unsigned char)((word) >> 16); \ + hbuf[3] = (unsigned char)((word) >> 24); \ + check = crc32(check, hbuf, 4); \ + } while (0) +#endif + +/* Load registers with state in inflate() for speed */ +#define LOAD() \ + do { \ + put = strm->next_out; \ + left = strm->avail_out; \ + next = strm->next_in; \ + have = strm->avail_in; \ + hold = state->hold; \ + bits = state->bits; \ + } while (0) + +/* Restore state from registers in inflate() */ +#define RESTORE() \ + do { \ + strm->next_out = put; \ + strm->avail_out = left; \ + strm->next_in = next; \ + strm->avail_in = have; \ + state->hold = hold; \ + state->bits = bits; \ + } while (0) + +/* Clear the input bit accumulator */ +#define INITBITS() \ + do { \ + hold = 0; \ + bits = 0; \ + } while (0) + +/* Get a byte of input into the bit accumulator, or return from inflate() + if there is no input available. */ +#define PULLBYTE() \ + do { \ + if (have == 0) goto inf_leave; \ + have--; \ + hold += (unsigned long)(*next++) << bits; \ + bits += 8; \ + } while (0) + +/* Assure that there are at least n bits in the bit accumulator. If there is + not enough available input to do that, then return from inflate(). */ +#define NEEDBITS(n) \ + do { \ + while (bits < (unsigned)(n)) \ + PULLBYTE(); \ + } while (0) + +/* Return the low n bits of the bit accumulator (n < 16) */ +#define BITS(n) \ + ((unsigned)hold & ((1U << (n)) - 1)) + +/* Remove n bits from the bit accumulator */ +#define DROPBITS(n) \ + do { \ + hold >>= (n); \ + bits -= (unsigned)(n); \ + } while (0) + +/* Remove zero to seven bits as needed to go to a byte boundary */ +#define BYTEBITS() \ + do { \ + hold >>= bits & 7; \ + bits -= bits & 7; \ + } while (0) + +/* + inflate() uses a state machine to process as much input data and generate as + much output data as possible before returning. The state machine is + structured roughly as follows: + + for (;;) switch (state) { + ... + case STATEn: + if (not enough input data or output space to make progress) + return; + ... make progress ... + state = STATEm; + break; + ... + } + + so when inflate() is called again, the same case is attempted again, and + if the appropriate resources are provided, the machine proceeds to the + next state. The NEEDBITS() macro is usually the way the state evaluates + whether it can proceed or should return. NEEDBITS() does the return if + the requested bits are not available. The typical use of the BITS macros + is: + + NEEDBITS(n); + ... do something with BITS(n) ... + DROPBITS(n); + + where NEEDBITS(n) either returns from inflate() if there isn't enough + input left to load n bits into the accumulator, or it continues. BITS(n) + gives the low n bits in the accumulator. When done, DROPBITS(n) drops + the low n bits off the accumulator. INITBITS() clears the accumulator + and sets the number of available bits to zero. BYTEBITS() discards just + enough bits to put the accumulator on a byte boundary. After BYTEBITS() + and a NEEDBITS(8), then BITS(8) would return the next byte in the stream. + + NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return + if there is no input available. The decoding of variable length codes uses + PULLBYTE() directly in order to pull just enough bytes to decode the next + code, and no more. + + Some states loop until they get enough input, making sure that enough + state information is maintained to continue the loop where it left off + if NEEDBITS() returns in the loop. For example, want, need, and keep + would all have to actually be part of the saved state in case NEEDBITS() + returns: + + case STATEw: + while (want < need) { + NEEDBITS(n); + keep[want++] = BITS(n); + DROPBITS(n); + } + state = STATEx; + case STATEx: + + As shown above, if the next state is also the next case, then the break + is omitted. + + A state may also return if there is not enough output space available to + complete that state. Those states are copying stored data, writing a + literal byte, and copying a matching string. + + When returning, a "goto inf_leave" is used to update the total counters, + update the check value, and determine whether any progress has been made + during that inflate() call in order to return the proper return code. + Progress is defined as a change in either strm->avail_in or strm->avail_out. + When there is a window, goto inf_leave will update the window with the last + output written. If a goto inf_leave occurs in the middle of decompression + and there is no window currently, goto inf_leave will create one and copy + output to the window for the next call of inflate(). + + In this implementation, the flush parameter of inflate() only affects the + return code (per zlib.h). inflate() always writes as much as possible to + strm->next_out, given the space available and the provided input--the effect + documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers + the allocation of and copying into a sliding window until necessary, which + provides the effect documented in zlib.h for Z_FINISH when the entire input + stream available. So the only thing the flush parameter actually does is: + when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it + will return Z_BUF_ERROR if it has not reached the end of the stream. + */ + +int ZEXPORT inflate(strm, flush) +z_streamp strm; +int flush; +{ + struct inflate_state FAR *state; + z_const unsigned char FAR *next; /* next input */ + unsigned char FAR *put; /* next output */ + unsigned have, left; /* available input and output */ + unsigned long hold; /* bit buffer */ + unsigned bits; /* bits in bit buffer */ + unsigned in, out; /* save starting available input and output */ + unsigned copy; /* number of stored or match bytes to copy */ + unsigned char FAR *from; /* where to copy match bytes from */ + code here; /* current decoding table entry */ + code last; /* parent table entry */ + unsigned len; /* length to copy for repeats, bits to drop */ + int ret; /* return code */ +#ifdef GUNZIP + unsigned char hbuf[4]; /* buffer for gzip header crc calculation */ +#endif + static const unsigned short order[19] = /* permutation of code lengths */ + {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; + + if (inflateStateCheck(strm) || strm->next_out == Z_NULL || + (strm->next_in == Z_NULL && strm->avail_in != 0)) + return Z_STREAM_ERROR; + + state = (struct inflate_state FAR *)strm->state; + if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */ + LOAD(); + in = have; + out = left; + ret = Z_OK; + for (;;) + switch (state->mode) { + case HEAD: + if (state->wrap == 0) { + state->mode = TYPEDO; + break; + } + NEEDBITS(16); +#ifdef GUNZIP + if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */ + if (state->wbits == 0) + state->wbits = 15; + state->check = crc32(0L, Z_NULL, 0); + CRC2(state->check, hold); + INITBITS(); + state->mode = FLAGS; + break; + } + state->flags = 0; /* expect zlib header */ + if (state->head != Z_NULL) + state->head->done = -1; + if (!(state->wrap & 1) || /* check if zlib header allowed */ +#else + if ( +#endif + ((BITS(8) << 8) + (hold >> 8)) % 31) { + strm->msg = (char *)"incorrect header check"; + state->mode = BAD; + break; + } + if (BITS(4) != Z_DEFLATED) { + strm->msg = (char *)"unknown compression method"; + state->mode = BAD; + break; + } + DROPBITS(4); + len = BITS(4) + 8; + if (state->wbits == 0) + state->wbits = len; + if (len > 15 || len > state->wbits) { + strm->msg = (char *)"invalid window size"; + state->mode = BAD; + break; + } + state->dmax = 1U << len; + Tracev((stderr, "inflate: zlib header ok\n")); + strm->adler = state->check = adler32(0L, Z_NULL, 0); + state->mode = hold & 0x200 ? DICTID : TYPE; + INITBITS(); + break; +#ifdef GUNZIP + case FLAGS: + NEEDBITS(16); + state->flags = (int)(hold); + if ((state->flags & 0xff) != Z_DEFLATED) { + strm->msg = (char *)"unknown compression method"; + state->mode = BAD; + break; + } + if (state->flags & 0xe000) { + strm->msg = (char *)"unknown header flags set"; + state->mode = BAD; + break; + } + if (state->head != Z_NULL) + state->head->text = (int)((hold >> 8) & 1); + if ((state->flags & 0x0200) && (state->wrap & 4)) + CRC2(state->check, hold); + INITBITS(); + state->mode = TIME; + case TIME: + NEEDBITS(32); + if (state->head != Z_NULL) + state->head->time = hold; + if ((state->flags & 0x0200) && (state->wrap & 4)) + CRC4(state->check, hold); + INITBITS(); + state->mode = OS; + case OS: + NEEDBITS(16); + if (state->head != Z_NULL) { + state->head->xflags = (int)(hold & 0xff); + state->head->os = (int)(hold >> 8); + } + if ((state->flags & 0x0200) && (state->wrap & 4)) + CRC2(state->check, hold); + INITBITS(); + state->mode = EXLEN; + case EXLEN: + if (state->flags & 0x0400) { + NEEDBITS(16); + state->length = (unsigned)(hold); + if (state->head != Z_NULL) + state->head->extra_len = (unsigned)hold; + if ((state->flags & 0x0200) && (state->wrap & 4)) + CRC2(state->check, hold); + INITBITS(); + } + else if (state->head != Z_NULL) + state->head->extra = Z_NULL; + state->mode = EXTRA; + case EXTRA: + if (state->flags & 0x0400) { + copy = state->length; + if (copy > have) copy = have; + if (copy) { + if (state->head != Z_NULL && + state->head->extra != Z_NULL) { + len = state->head->extra_len - state->length; + zmemcpy(state->head->extra + len, next, + len + copy > state->head->extra_max ? + state->head->extra_max - len : copy); + } + if ((state->flags & 0x0200) && (state->wrap & 4)) + state->check = crc32(state->check, next, copy); + have -= copy; + next += copy; + state->length -= copy; + } + if (state->length) goto inf_leave; + } + state->length = 0; + state->mode = NAME; + case NAME: + if (state->flags & 0x0800) { + if (have == 0) goto inf_leave; + copy = 0; + do { + len = (unsigned)(next[copy++]); + if (state->head != Z_NULL && + state->head->name != Z_NULL && + state->length < state->head->name_max) + state->head->name[state->length++] = (Bytef)len; + } while (len && copy < have); + if ((state->flags & 0x0200) && (state->wrap & 4)) + state->check = crc32(state->check, next, copy); + have -= copy; + next += copy; + if (len) goto inf_leave; + } + else if (state->head != Z_NULL) + state->head->name = Z_NULL; + state->length = 0; + state->mode = COMMENT; + case COMMENT: + if (state->flags & 0x1000) { + if (have == 0) goto inf_leave; + copy = 0; + do { + len = (unsigned)(next[copy++]); + if (state->head != Z_NULL && + state->head->comment != Z_NULL && + state->length < state->head->comm_max) + state->head->comment[state->length++] = (Bytef)len; + } while (len && copy < have); + if ((state->flags & 0x0200) && (state->wrap & 4)) + state->check = crc32(state->check, next, copy); + have -= copy; + next += copy; + if (len) goto inf_leave; + } + else if (state->head != Z_NULL) + state->head->comment = Z_NULL; + state->mode = HCRC; + case HCRC: + if (state->flags & 0x0200) { + NEEDBITS(16); + if ((state->wrap & 4) && hold != (state->check & 0xffff)) { + strm->msg = (char *)"header crc mismatch"; + state->mode = BAD; + break; + } + INITBITS(); + } + if (state->head != Z_NULL) { + state->head->hcrc = (int)((state->flags >> 9) & 1); + state->head->done = 1; + } + strm->adler = state->check = crc32(0L, Z_NULL, 0); + state->mode = TYPE; + break; +#endif + case DICTID: + NEEDBITS(32); + strm->adler = state->check = ZSWAP32(hold); + INITBITS(); + state->mode = DICT; + case DICT: + if (state->havedict == 0) { + RESTORE(); + return Z_NEED_DICT; + } + strm->adler = state->check = adler32(0L, Z_NULL, 0); + state->mode = TYPE; + case TYPE: + if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave; + case TYPEDO: + if (state->last) { + BYTEBITS(); + state->mode = CHECK; + break; + } + NEEDBITS(3); + state->last = BITS(1); + DROPBITS(1); + switch (BITS(2)) { + case 0: /* stored block */ + Tracev((stderr, "inflate: stored block%s\n", + state->last ? " (last)" : "")); + state->mode = STORED; + break; + case 1: /* fixed block */ + fixedtables(state); + Tracev((stderr, "inflate: fixed codes block%s\n", + state->last ? " (last)" : "")); + state->mode = LEN_; /* decode codes */ + if (flush == Z_TREES) { + DROPBITS(2); + goto inf_leave; + } + break; + case 2: /* dynamic block */ + Tracev((stderr, "inflate: dynamic codes block%s\n", + state->last ? " (last)" : "")); + state->mode = TABLE; + break; + case 3: + strm->msg = (char *)"invalid block type"; + state->mode = BAD; + } + DROPBITS(2); + break; + case STORED: + BYTEBITS(); /* go to byte boundary */ + NEEDBITS(32); + if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { + strm->msg = (char *)"invalid stored block lengths"; + state->mode = BAD; + break; + } + state->length = (unsigned)hold & 0xffff; + Tracev((stderr, "inflate: stored length %u\n", + state->length)); + INITBITS(); + state->mode = COPY_; + if (flush == Z_TREES) goto inf_leave; + case COPY_: + state->mode = COPY; + case COPY: + copy = state->length; + if (copy) { + if (copy > have) copy = have; + if (copy > left) copy = left; + if (copy == 0) goto inf_leave; + zmemcpy(put, next, copy); + have -= copy; + next += copy; + left -= copy; + put += copy; + state->length -= copy; + break; + } + Tracev((stderr, "inflate: stored end\n")); + state->mode = TYPE; + break; + case TABLE: + NEEDBITS(14); + state->nlen = BITS(5) + 257; + DROPBITS(5); + state->ndist = BITS(5) + 1; + DROPBITS(5); + state->ncode = BITS(4) + 4; + DROPBITS(4); +#ifndef PKZIP_BUG_WORKAROUND + if (state->nlen > 286 || state->ndist > 30) { + strm->msg = (char *)"too many length or distance symbols"; + state->mode = BAD; + break; + } +#endif + Tracev((stderr, "inflate: table sizes ok\n")); + state->have = 0; + state->mode = LENLENS; + case LENLENS: + while (state->have < state->ncode) { + NEEDBITS(3); + state->lens[order[state->have++]] = (unsigned short)BITS(3); + DROPBITS(3); + } + while (state->have < 19) + state->lens[order[state->have++]] = 0; + state->next = state->codes; + state->lencode = (const code FAR *)(state->next); + state->lenbits = 7; + ret = inflate_table(CODES, state->lens, 19, &(state->next), + &(state->lenbits), state->work); + if (ret) { + strm->msg = (char *)"invalid code lengths set"; + state->mode = BAD; + break; + } + Tracev((stderr, "inflate: code lengths ok\n")); + state->have = 0; + state->mode = CODELENS; + case CODELENS: + while (state->have < state->nlen + state->ndist) { + for (;;) { + here = state->lencode[BITS(state->lenbits)]; + if ((unsigned)(here.bits) <= bits) break; + PULLBYTE(); + } + if (here.val < 16) { + DROPBITS(here.bits); + state->lens[state->have++] = here.val; + } + else { + if (here.val == 16) { + NEEDBITS(here.bits + 2); + DROPBITS(here.bits); + if (state->have == 0) { + strm->msg = (char *)"invalid bit length repeat"; + state->mode = BAD; + break; + } + len = state->lens[state->have - 1]; + copy = 3 + BITS(2); + DROPBITS(2); + } + else if (here.val == 17) { + NEEDBITS(here.bits + 3); + DROPBITS(here.bits); + len = 0; + copy = 3 + BITS(3); + DROPBITS(3); + } + else { + NEEDBITS(here.bits + 7); + DROPBITS(here.bits); + len = 0; + copy = 11 + BITS(7); + DROPBITS(7); + } + if (state->have + copy > state->nlen + state->ndist) { + strm->msg = (char *)"invalid bit length repeat"; + state->mode = BAD; + break; + } + while (copy--) + state->lens[state->have++] = (unsigned short)len; + } + } + + /* handle error breaks in while */ + if (state->mode == BAD) break; + + /* check for end-of-block code (better have one) */ + if (state->lens[256] == 0) { + strm->msg = (char *)"invalid code -- missing end-of-block"; + state->mode = BAD; + break; + } + + /* build code tables -- note: do not change the lenbits or distbits + values here (9 and 6) without reading the comments in inftrees.h + concerning the ENOUGH constants, which depend on those values */ + state->next = state->codes; + state->lencode = (const code FAR *)(state->next); + state->lenbits = 9; + ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), + &(state->lenbits), state->work); + if (ret) { + strm->msg = (char *)"invalid literal/lengths set"; + state->mode = BAD; + break; + } + state->distcode = (const code FAR *)(state->next); + state->distbits = 6; + ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, + &(state->next), &(state->distbits), state->work); + if (ret) { + strm->msg = (char *)"invalid distances set"; + state->mode = BAD; + break; + } + Tracev((stderr, "inflate: codes ok\n")); + state->mode = LEN_; + if (flush == Z_TREES) goto inf_leave; + case LEN_: + state->mode = LEN; + case LEN: + if (have >= 6 && left >= 258) { + RESTORE(); + inflate_fast(strm, out); + LOAD(); + if (state->mode == TYPE) + state->back = -1; + break; + } + state->back = 0; + for (;;) { + here = state->lencode[BITS(state->lenbits)]; + if ((unsigned)(here.bits) <= bits) break; + PULLBYTE(); + } + if (here.op && (here.op & 0xf0) == 0) { + last = here; + for (;;) { + here = state->lencode[last.val + + (BITS(last.bits + last.op) >> last.bits)]; + if ((unsigned)(last.bits + here.bits) <= bits) break; + PULLBYTE(); + } + DROPBITS(last.bits); + state->back += last.bits; + } + DROPBITS(here.bits); + state->back += here.bits; + state->length = (unsigned)here.val; + if ((int)(here.op) == 0) { + Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? + "inflate: literal '%c'\n" : + "inflate: literal 0x%02x\n", here.val)); + state->mode = LIT; + break; + } + if (here.op & 32) { + Tracevv((stderr, "inflate: end of block\n")); + state->back = -1; + state->mode = TYPE; + break; + } + if (here.op & 64) { + strm->msg = (char *)"invalid literal/length code"; + state->mode = BAD; + break; + } + state->extra = (unsigned)(here.op) & 15; + state->mode = LENEXT; + case LENEXT: + if (state->extra) { + NEEDBITS(state->extra); + state->length += BITS(state->extra); + DROPBITS(state->extra); + state->back += state->extra; + } + Tracevv((stderr, "inflate: length %u\n", state->length)); + state->was = state->length; + state->mode = DIST; + case DIST: + for (;;) { + here = state->distcode[BITS(state->distbits)]; + if ((unsigned)(here.bits) <= bits) break; + PULLBYTE(); + } + if ((here.op & 0xf0) == 0) { + last = here; + for (;;) { + here = state->distcode[last.val + + (BITS(last.bits + last.op) >> last.bits)]; + if ((unsigned)(last.bits + here.bits) <= bits) break; + PULLBYTE(); + } + DROPBITS(last.bits); + state->back += last.bits; + } + DROPBITS(here.bits); + state->back += here.bits; + if (here.op & 64) { + strm->msg = (char *)"invalid distance code"; + state->mode = BAD; + break; + } + state->offset = (unsigned)here.val; + state->extra = (unsigned)(here.op) & 15; + state->mode = DISTEXT; + case DISTEXT: + if (state->extra) { + NEEDBITS(state->extra); + state->offset += BITS(state->extra); + DROPBITS(state->extra); + state->back += state->extra; + } +#ifdef INFLATE_STRICT + if (state->offset > state->dmax) { + strm->msg = (char *)"invalid distance too far back"; + state->mode = BAD; + break; + } +#endif + Tracevv((stderr, "inflate: distance %u\n", state->offset)); + state->mode = MATCH; + case MATCH: + if (left == 0) goto inf_leave; + copy = out - left; + if (state->offset > copy) { /* copy from window */ + copy = state->offset - copy; + if (copy > state->whave) { + if (state->sane) { + strm->msg = (char *)"invalid distance too far back"; + state->mode = BAD; + break; + } +#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR + Trace((stderr, "inflate.c too far\n")); + copy -= state->whave; + if (copy > state->length) copy = state->length; + if (copy > left) copy = left; + left -= copy; + state->length -= copy; + do { + *put++ = 0; + } while (--copy); + if (state->length == 0) state->mode = LEN; + break; +#endif + } + if (copy > state->wnext) { + copy -= state->wnext; + from = state->window + (state->wsize - copy); + } + else + from = state->window + (state->wnext - copy); + if (copy > state->length) copy = state->length; + } + else { /* copy from output */ + from = put - state->offset; + copy = state->length; + } + if (copy > left) copy = left; + left -= copy; + state->length -= copy; + do { + *put++ = *from++; + } while (--copy); + if (state->length == 0) state->mode = LEN; + break; + case LIT: + if (left == 0) goto inf_leave; + *put++ = (unsigned char)(state->length); + left--; + state->mode = LEN; + break; + case CHECK: + if (state->wrap) { + NEEDBITS(32); + out -= left; + strm->total_out += out; + state->total += out; + if ((state->wrap & 4) && out) + strm->adler = state->check = + UPDATE(state->check, put - out, out); + out = left; + if ((state->wrap & 4) && ( +#ifdef GUNZIP + state->flags ? hold : +#endif + ZSWAP32(hold)) != state->check) { + strm->msg = (char *)"incorrect data check"; + state->mode = BAD; + break; + } + INITBITS(); + Tracev((stderr, "inflate: check matches trailer\n")); + } +#ifdef GUNZIP + state->mode = LENGTH; + case LENGTH: + if (state->wrap && state->flags) { + NEEDBITS(32); + if (hold != (state->total & 0xffffffffUL)) { + strm->msg = (char *)"incorrect length check"; + state->mode = BAD; + break; + } + INITBITS(); + Tracev((stderr, "inflate: length matches trailer\n")); + } +#endif + state->mode = DONE; + case DONE: + ret = Z_STREAM_END; + goto inf_leave; + case BAD: + ret = Z_DATA_ERROR; + goto inf_leave; + case MEM: + return Z_MEM_ERROR; + case SYNC: + default: + return Z_STREAM_ERROR; + } + + /* + Return from inflate(), updating the total counts and the check value. + If there was no progress during the inflate() call, return a buffer + error. Call updatewindow() to create and/or update the window state. + Note: a memory error from inflate() is non-recoverable. + */ + inf_leave: + RESTORE(); + if (state->wsize || (out != strm->avail_out && state->mode < BAD && + (state->mode < CHECK || flush != Z_FINISH))) + if (updatewindow(strm, strm->next_out, out - strm->avail_out)) { + state->mode = MEM; + return Z_MEM_ERROR; + } + in -= strm->avail_in; + out -= strm->avail_out; + strm->total_in += in; + strm->total_out += out; + state->total += out; + if ((state->wrap & 4) && out) + strm->adler = state->check = + UPDATE(state->check, strm->next_out - out, out); + strm->data_type = (int)state->bits + (state->last ? 64 : 0) + + (state->mode == TYPE ? 128 : 0) + + (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0); + if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) + ret = Z_BUF_ERROR; + return ret; +} + +int ZEXPORT inflateEnd(strm) +z_streamp strm; +{ + struct inflate_state FAR *state; + if (inflateStateCheck(strm)) + return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + if (state->window != Z_NULL) ZFREE(strm, state->window); + ZFREE(strm, strm->state); + strm->state = Z_NULL; + Tracev((stderr, "inflate: end\n")); + return Z_OK; +} + +int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength) +z_streamp strm; +Bytef *dictionary; +uInt *dictLength; +{ + struct inflate_state FAR *state; + + /* check state */ + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + + /* copy dictionary */ + if (state->whave && dictionary != Z_NULL) { + zmemcpy(dictionary, state->window + state->wnext, + state->whave - state->wnext); + zmemcpy(dictionary + state->whave - state->wnext, + state->window, state->wnext); + } + if (dictLength != Z_NULL) + *dictLength = state->whave; + return Z_OK; +} + +int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) +z_streamp strm; +const Bytef *dictionary; +uInt dictLength; +{ + struct inflate_state FAR *state; + unsigned long dictid; + int ret; + + /* check state */ + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + if (state->wrap != 0 && state->mode != DICT) + return Z_STREAM_ERROR; + + /* check for correct dictionary identifier */ + if (state->mode == DICT) { + dictid = adler32(0L, Z_NULL, 0); + dictid = adler32(dictid, dictionary, dictLength); + if (dictid != state->check) + return Z_DATA_ERROR; + } + + /* copy dictionary to window using updatewindow(), which will amend the + existing dictionary if appropriate */ + ret = updatewindow(strm, dictionary + dictLength, dictLength); + if (ret) { + state->mode = MEM; + return Z_MEM_ERROR; + } + state->havedict = 1; + Tracev((stderr, "inflate: dictionary set\n")); + return Z_OK; +} + +int ZEXPORT inflateGetHeader(strm, head) +z_streamp strm; +gz_headerp head; +{ + struct inflate_state FAR *state; + + /* check state */ + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + if ((state->wrap & 2) == 0) return Z_STREAM_ERROR; + + /* save header structure */ + state->head = head; + head->done = 0; + return Z_OK; +} + +/* + Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found + or when out of input. When called, *have is the number of pattern bytes + found in order so far, in 0..3. On return *have is updated to the new + state. If on return *have equals four, then the pattern was found and the + return value is how many bytes were read including the last byte of the + pattern. If *have is less than four, then the pattern has not been found + yet and the return value is len. In the latter case, syncsearch() can be + called again with more data and the *have state. *have is initialized to + zero for the first call. + */ +local unsigned syncsearch(have, buf, len) +unsigned FAR *have; +const unsigned char FAR *buf; +unsigned len; +{ + unsigned got; + unsigned next; + + got = *have; + next = 0; + while (next < len && got < 4) { + if ((int)(buf[next]) == (got < 2 ? 0 : 0xff)) + got++; + else if (buf[next]) + got = 0; + else + got = 4 - got; + next++; + } + *have = got; + return next; +} + +int ZEXPORT inflateSync(strm) +z_streamp strm; +{ + unsigned len; /* number of bytes to look at or looked at */ + unsigned long in, out; /* temporary to save total_in and total_out */ + unsigned char buf[4]; /* to restore bit buffer to byte string */ + struct inflate_state FAR *state; + + /* check parameters */ + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR; + + /* if first time, start search in bit buffer */ + if (state->mode != SYNC) { + state->mode = SYNC; + state->hold <<= state->bits & 7; + state->bits -= state->bits & 7; + len = 0; + while (state->bits >= 8) { + buf[len++] = (unsigned char)(state->hold); + state->hold >>= 8; + state->bits -= 8; + } + state->have = 0; + syncsearch(&(state->have), buf, len); + } + + /* search available input */ + len = syncsearch(&(state->have), strm->next_in, strm->avail_in); + strm->avail_in -= len; + strm->next_in += len; + strm->total_in += len; + + /* return no joy or set up to restart inflate() on a new block */ + if (state->have != 4) return Z_DATA_ERROR; + in = strm->total_in; out = strm->total_out; + inflateReset(strm); + strm->total_in = in; strm->total_out = out; + state->mode = TYPE; + return Z_OK; +} + +/* + Returns true if inflate is currently at the end of a block generated by + Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP + implementation to provide an additional safety check. PPP uses + Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored + block. When decompressing, PPP checks that at the end of input packet, + inflate is waiting for these length bytes. + */ +int ZEXPORT inflateSyncPoint(strm) +z_streamp strm; +{ + struct inflate_state FAR *state; + + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + return state->mode == STORED && state->bits == 0; +} + +int ZEXPORT inflateCopy(dest, source) +z_streamp dest; +z_streamp source; +{ + struct inflate_state FAR *state; + struct inflate_state FAR *copy; + unsigned char FAR *window; + unsigned wsize; + + /* check input */ + if (inflateStateCheck(source) || dest == Z_NULL) + return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)source->state; + + /* allocate space */ + copy = (struct inflate_state FAR *) + ZALLOC(source, 1, sizeof(struct inflate_state)); + if (copy == Z_NULL) return Z_MEM_ERROR; + window = Z_NULL; + if (state->window != Z_NULL) { + window = (unsigned char FAR *) + ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); + if (window == Z_NULL) { + ZFREE(source, copy); + return Z_MEM_ERROR; + } + } + + /* copy state */ + zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); + zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state)); + copy->strm = dest; + if (state->lencode >= state->codes && + state->lencode <= state->codes + ENOUGH - 1) { + copy->lencode = copy->codes + (state->lencode - state->codes); + copy->distcode = copy->codes + (state->distcode - state->codes); + } + copy->next = copy->codes + (state->next - state->codes); + if (window != Z_NULL) { + wsize = 1U << state->wbits; + zmemcpy(window, state->window, wsize); + } + copy->window = window; + dest->state = (struct internal_state FAR *)copy; + return Z_OK; +} + +int ZEXPORT inflateUndermine(strm, subvert) +z_streamp strm; +int subvert; +{ + struct inflate_state FAR *state; + + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; +#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR + state->sane = !subvert; + return Z_OK; +#else + (void)subvert; + state->sane = 1; + return Z_DATA_ERROR; +#endif +} + +int ZEXPORT inflateValidate(strm, check) +z_streamp strm; +int check; +{ + struct inflate_state FAR *state; + + if (inflateStateCheck(strm)) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + if (check) + state->wrap |= 4; + else + state->wrap &= ~4; + return Z_OK; +} + +long ZEXPORT inflateMark(strm) +z_streamp strm; +{ + struct inflate_state FAR *state; + + if (inflateStateCheck(strm)) + return -(1L << 16); + state = (struct inflate_state FAR *)strm->state; + return (long)(((unsigned long)((long)state->back)) << 16) + + (state->mode == COPY ? state->length : + (state->mode == MATCH ? state->was - state->length : 0)); +} + +unsigned long ZEXPORT inflateCodesUsed(strm) +z_streamp strm; +{ + struct inflate_state FAR *state; + if (inflateStateCheck(strm)) return (unsigned long)-1; + state = (struct inflate_state FAR *)strm->state; + return (unsigned long)(state->next - state->codes); +} diff --git a/third_party/java_tools/zlib/inflate.h b/third_party/java_tools/zlib/inflate.h new file mode 100644 index 000000000..a46cce6b6 --- /dev/null +++ b/third_party/java_tools/zlib/inflate.h @@ -0,0 +1,125 @@ +/* inflate.h -- internal inflate state definition + * Copyright (C) 1995-2016 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* WARNING: this file should *not* be used by applications. It is + part of the implementation of the compression library and is + subject to change. Applications should only use zlib.h. + */ + +/* define NO_GZIP when compiling if you want to disable gzip header and + trailer decoding by inflate(). NO_GZIP would be used to avoid linking in + the crc code when it is not needed. For shared libraries, gzip decoding + should be left enabled. */ +#ifndef NO_GZIP +# define GUNZIP +#endif + +/* Possible inflate modes between inflate() calls */ +typedef enum { + HEAD = 16180, /* i: waiting for magic header */ + FLAGS, /* i: waiting for method and flags (gzip) */ + TIME, /* i: waiting for modification time (gzip) */ + OS, /* i: waiting for extra flags and operating system (gzip) */ + EXLEN, /* i: waiting for extra length (gzip) */ + EXTRA, /* i: waiting for extra bytes (gzip) */ + NAME, /* i: waiting for end of file name (gzip) */ + COMMENT, /* i: waiting for end of comment (gzip) */ + HCRC, /* i: waiting for header crc (gzip) */ + DICTID, /* i: waiting for dictionary check value */ + DICT, /* waiting for inflateSetDictionary() call */ + TYPE, /* i: waiting for type bits, including last-flag bit */ + TYPEDO, /* i: same, but skip check to exit inflate on new block */ + STORED, /* i: waiting for stored size (length and complement) */ + COPY_, /* i/o: same as COPY below, but only first time in */ + COPY, /* i/o: waiting for input or output to copy stored block */ + TABLE, /* i: waiting for dynamic block table lengths */ + LENLENS, /* i: waiting for code length code lengths */ + CODELENS, /* i: waiting for length/lit and distance code lengths */ + LEN_, /* i: same as LEN below, but only first time in */ + LEN, /* i: waiting for length/lit/eob code */ + LENEXT, /* i: waiting for length extra bits */ + DIST, /* i: waiting for distance code */ + DISTEXT, /* i: waiting for distance extra bits */ + MATCH, /* o: waiting for output space to copy string */ + LIT, /* o: waiting for output space to write literal */ + CHECK, /* i: waiting for 32-bit check value */ + LENGTH, /* i: waiting for 32-bit length (gzip) */ + DONE, /* finished check, done -- remain here until reset */ + BAD, /* got a data error -- remain here until reset */ + MEM, /* got an inflate() memory error -- remain here until reset */ + SYNC /* looking for synchronization bytes to restart inflate() */ +} inflate_mode; + +/* + State transitions between above modes - + + (most modes can go to BAD or MEM on error -- not shown for clarity) + + Process header: + HEAD -> (gzip) or (zlib) or (raw) + (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT -> + HCRC -> TYPE + (zlib) -> DICTID or TYPE + DICTID -> DICT -> TYPE + (raw) -> TYPEDO + Read deflate blocks: + TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK + STORED -> COPY_ -> COPY -> TYPE + TABLE -> LENLENS -> CODELENS -> LEN_ + LEN_ -> LEN + Read deflate codes in fixed or dynamic block: + LEN -> LENEXT or LIT or TYPE + LENEXT -> DIST -> DISTEXT -> MATCH -> LEN + LIT -> LEN + Process trailer: + CHECK -> LENGTH -> DONE + */ + +/* State maintained between inflate() calls -- approximately 7K bytes, not + including the allocated sliding window, which is up to 32K bytes. */ +struct inflate_state { + z_streamp strm; /* pointer back to this zlib stream */ + inflate_mode mode; /* current inflate mode */ + int last; /* true if processing last block */ + int wrap; /* bit 0 true for zlib, bit 1 true for gzip, + bit 2 true to validate check value */ + int havedict; /* true if dictionary provided */ + int flags; /* gzip header method and flags (0 if zlib) */ + unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */ + unsigned long check; /* protected copy of check value */ + unsigned long total; /* protected copy of output count */ + gz_headerp head; /* where to save gzip header information */ + /* sliding window */ + unsigned wbits; /* log base 2 of requested window size */ + unsigned wsize; /* window size or zero if not using window */ + unsigned whave; /* valid bytes in the window */ + unsigned wnext; /* window write index */ + unsigned char FAR *window; /* allocated sliding window, if needed */ + /* bit accumulator */ + unsigned long hold; /* input bit accumulator */ + unsigned bits; /* number of bits in "in" */ + /* for string and stored block copying */ + unsigned length; /* literal or length of data to copy */ + unsigned offset; /* distance back to copy string from */ + /* for table and code decoding */ + unsigned extra; /* extra bits needed */ + /* fixed and dynamic code tables */ + code const FAR *lencode; /* starting table for length/literal codes */ + code const FAR *distcode; /* starting table for distance codes */ + unsigned lenbits; /* index bits for lencode */ + unsigned distbits; /* index bits for distcode */ + /* dynamic table building */ + unsigned ncode; /* number of code length code lengths */ + unsigned nlen; /* number of length code lengths */ + unsigned ndist; /* number of distance code lengths */ + unsigned have; /* number of code lengths in lens[] */ + code FAR *next; /* next available space in codes[] */ + unsigned short lens[320]; /* temporary storage for code lengths */ + unsigned short work[288]; /* work area for code table building */ + code codes[ENOUGH]; /* space for code tables */ + int sane; /* if false, allow invalid distance too far */ + int back; /* bits back of last unprocessed length/lit */ + unsigned was; /* initial length of match */ +}; diff --git a/third_party/java_tools/zlib/inftrees.c b/third_party/java_tools/zlib/inftrees.c new file mode 100644 index 000000000..2ea08fc13 --- /dev/null +++ b/third_party/java_tools/zlib/inftrees.c @@ -0,0 +1,304 @@ +/* inftrees.c -- generate Huffman trees for efficient decoding + * Copyright (C) 1995-2017 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#include "zutil.h" +#include "inftrees.h" + +#define MAXBITS 15 + +const char inflate_copyright[] = + " inflate 1.2.11 Copyright 1995-2017 Mark Adler "; +/* + If you use the zlib library in a product, an acknowledgment is welcome + in the documentation of your product. If for some reason you cannot + include such an acknowledgment, I would appreciate that you keep this + copyright string in the executable of your product. + */ + +/* + Build a set of tables to decode the provided canonical Huffman code. + The code lengths are lens[0..codes-1]. The result starts at *table, + whose indices are 0..2^bits-1. work is a writable array of at least + lens shorts, which is used as a work area. type is the type of code + to be generated, CODES, LENS, or DISTS. On return, zero is success, + -1 is an invalid code, and +1 means that ENOUGH isn't enough. table + on return points to the next available entry's address. bits is the + requested root table index bits, and on return it is the actual root + table index bits. It will differ if the request is greater than the + longest code or if it is less than the shortest code. + */ +int ZLIB_INTERNAL inflate_table(type, lens, codes, table, bits, work) +codetype type; +unsigned short FAR *lens; +unsigned codes; +code FAR * FAR *table; +unsigned FAR *bits; +unsigned short FAR *work; +{ + unsigned len; /* a code's length in bits */ + unsigned sym; /* index of code symbols */ + unsigned min, max; /* minimum and maximum code lengths */ + unsigned root; /* number of index bits for root table */ + unsigned curr; /* number of index bits for current table */ + unsigned drop; /* code bits to drop for sub-table */ + int left; /* number of prefix codes available */ + unsigned used; /* code entries in table used */ + unsigned huff; /* Huffman code */ + unsigned incr; /* for incrementing code, index */ + unsigned fill; /* index for replicating entries */ + unsigned low; /* low bits for current root entry */ + unsigned mask; /* mask for low root bits */ + code here; /* table entry for duplication */ + code FAR *next; /* next available space in table */ + const unsigned short FAR *base; /* base value table to use */ + const unsigned short FAR *extra; /* extra bits table to use */ + unsigned match; /* use base and extra for symbol >= match */ + unsigned short count[MAXBITS+1]; /* number of codes of each length */ + unsigned short offs[MAXBITS+1]; /* offsets in table for each length */ + static const unsigned short lbase[31] = { /* Length codes 257..285 base */ + 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, + 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; + static const unsigned short lext[31] = { /* Length codes 257..285 extra */ + 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, + 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 77, 202}; + static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ + 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, + 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, + 8193, 12289, 16385, 24577, 0, 0}; + static const unsigned short dext[32] = { /* Distance codes 0..29 extra */ + 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, + 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, + 28, 28, 29, 29, 64, 64}; + + /* + Process a set of code lengths to create a canonical Huffman code. The + code lengths are lens[0..codes-1]. Each length corresponds to the + symbols 0..codes-1. The Huffman code is generated by first sorting the + symbols by length from short to long, and retaining the symbol order + for codes with equal lengths. Then the code starts with all zero bits + for the first code of the shortest length, and the codes are integer + increments for the same length, and zeros are appended as the length + increases. For the deflate format, these bits are stored backwards + from their more natural integer increment ordering, and so when the + decoding tables are built in the large loop below, the integer codes + are incremented backwards. + + This routine assumes, but does not check, that all of the entries in + lens[] are in the range 0..MAXBITS. The caller must assure this. + 1..MAXBITS is interpreted as that code length. zero means that that + symbol does not occur in this code. + + The codes are sorted by computing a count of codes for each length, + creating from that a table of starting indices for each length in the + sorted table, and then entering the symbols in order in the sorted + table. The sorted table is work[], with that space being provided by + the caller. + + The length counts are used for other purposes as well, i.e. finding + the minimum and maximum length codes, determining if there are any + codes at all, checking for a valid set of lengths, and looking ahead + at length counts to determine sub-table sizes when building the + decoding tables. + */ + + /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ + for (len = 0; len <= MAXBITS; len++) + count[len] = 0; + for (sym = 0; sym < codes; sym++) + count[lens[sym]]++; + + /* bound code lengths, force root to be within code lengths */ + root = *bits; + for (max = MAXBITS; max >= 1; max--) + if (count[max] != 0) break; + if (root > max) root = max; + if (max == 0) { /* no symbols to code at all */ + here.op = (unsigned char)64; /* invalid code marker */ + here.bits = (unsigned char)1; + here.val = (unsigned short)0; + *(*table)++ = here; /* make a table to force an error */ + *(*table)++ = here; + *bits = 1; + return 0; /* no symbols, but wait for decoding to report error */ + } + for (min = 1; min < max; min++) + if (count[min] != 0) break; + if (root < min) root = min; + + /* check for an over-subscribed or incomplete set of lengths */ + left = 1; + for (len = 1; len <= MAXBITS; len++) { + left <<= 1; + left -= count[len]; + if (left < 0) return -1; /* over-subscribed */ + } + if (left > 0 && (type == CODES || max != 1)) + return -1; /* incomplete set */ + + /* generate offsets into symbol table for each length for sorting */ + offs[1] = 0; + for (len = 1; len < MAXBITS; len++) + offs[len + 1] = offs[len] + count[len]; + + /* sort symbols by length, by symbol order within each length */ + for (sym = 0; sym < codes; sym++) + if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym; + + /* + Create and fill in decoding tables. In this loop, the table being + filled is at next and has curr index bits. The code being used is huff + with length len. That code is converted to an index by dropping drop + bits off of the bottom. For codes where len is less than drop + curr, + those top drop + curr - len bits are incremented through all values to + fill the table with replicated entries. + + root is the number of index bits for the root table. When len exceeds + root, sub-tables are created pointed to by the root entry with an index + of the low root bits of huff. This is saved in low to check for when a + new sub-table should be started. drop is zero when the root table is + being filled, and drop is root when sub-tables are being filled. + + When a new sub-table is needed, it is necessary to look ahead in the + code lengths to determine what size sub-table is needed. The length + counts are used for this, and so count[] is decremented as codes are + entered in the tables. + + used keeps track of how many table entries have been allocated from the + provided *table space. It is checked for LENS and DIST tables against + the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in + the initial root table size constants. See the comments in inftrees.h + for more information. + + sym increments through all symbols, and the loop terminates when + all codes of length max, i.e. all codes, have been processed. This + routine permits incomplete codes, so another loop after this one fills + in the rest of the decoding tables with invalid code markers. + */ + + /* set up for code type */ + switch (type) { + case CODES: + base = extra = work; /* dummy value--not used */ + match = 20; + break; + case LENS: + base = lbase; + extra = lext; + match = 257; + break; + default: /* DISTS */ + base = dbase; + extra = dext; + match = 0; + } + + /* initialize state for loop */ + huff = 0; /* starting code */ + sym = 0; /* starting code symbol */ + len = min; /* starting code length */ + next = *table; /* current table to fill in */ + curr = root; /* current table index bits */ + drop = 0; /* current bits to drop from code for index */ + low = (unsigned)(-1); /* trigger new sub-table when len > root */ + used = 1U << root; /* use root table entries */ + mask = used - 1; /* mask for comparing low */ + + /* check available table space */ + if ((type == LENS && used > ENOUGH_LENS) || + (type == DISTS && used > ENOUGH_DISTS)) + return 1; + + /* process all codes and make table entries */ + for (;;) { + /* create table entry */ + here.bits = (unsigned char)(len - drop); + if (work[sym] + 1U < match) { + here.op = (unsigned char)0; + here.val = work[sym]; + } + else if (work[sym] >= match) { + here.op = (unsigned char)(extra[work[sym] - match]); + here.val = base[work[sym] - match]; + } + else { + here.op = (unsigned char)(32 + 64); /* end of block */ + here.val = 0; + } + + /* replicate for those indices with low len bits equal to huff */ + incr = 1U << (len - drop); + fill = 1U << curr; + min = fill; /* save offset to next table */ + do { + fill -= incr; + next[(huff >> drop) + fill] = here; + } while (fill != 0); + + /* backwards increment the len-bit code huff */ + incr = 1U << (len - 1); + while (huff & incr) + incr >>= 1; + if (incr != 0) { + huff &= incr - 1; + huff += incr; + } + else + huff = 0; + + /* go to next symbol, update count, len */ + sym++; + if (--(count[len]) == 0) { + if (len == max) break; + len = lens[work[sym]]; + } + + /* create new sub-table if needed */ + if (len > root && (huff & mask) != low) { + /* if first time, transition to sub-tables */ + if (drop == 0) + drop = root; + + /* increment past last table */ + next += min; /* here min is 1 << curr */ + + /* determine length of next table */ + curr = len - drop; + left = (int)(1 << curr); + while (curr + drop < max) { + left -= count[curr + drop]; + if (left <= 0) break; + curr++; + left <<= 1; + } + + /* check for enough space */ + used += 1U << curr; + if ((type == LENS && used > ENOUGH_LENS) || + (type == DISTS && used > ENOUGH_DISTS)) + return 1; + + /* point entry in root table to sub-table */ + low = huff & mask; + (*table)[low].op = (unsigned char)curr; + (*table)[low].bits = (unsigned char)root; + (*table)[low].val = (unsigned short)(next - *table); + } + } + + /* fill in remaining table entry if code is incomplete (guaranteed to have + at most one remaining entry, since if the code is incomplete, the + maximum code length that was allowed to get this far is one bit) */ + if (huff != 0) { + here.op = (unsigned char)64; /* invalid code marker */ + here.bits = (unsigned char)(len - drop); + here.val = (unsigned short)0; + next[huff] = here; + } + + /* set return parameters */ + *table += used; + *bits = root; + return 0; +} diff --git a/third_party/java_tools/zlib/inftrees.h b/third_party/java_tools/zlib/inftrees.h new file mode 100644 index 000000000..baa53a0b1 --- /dev/null +++ b/third_party/java_tools/zlib/inftrees.h @@ -0,0 +1,62 @@ +/* inftrees.h -- header to use inftrees.c + * Copyright (C) 1995-2005, 2010 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* WARNING: this file should *not* be used by applications. It is + part of the implementation of the compression library and is + subject to change. Applications should only use zlib.h. + */ + +/* Structure for decoding tables. Each entry provides either the + information needed to do the operation requested by the code that + indexed that table entry, or it provides a pointer to another + table that indexes more bits of the code. op indicates whether + the entry is a pointer to another table, a literal, a length or + distance, an end-of-block, or an invalid code. For a table + pointer, the low four bits of op is the number of index bits of + that table. For a length or distance, the low four bits of op + is the number of extra bits to get after the code. bits is + the number of bits in this code or part of the code to drop off + of the bit buffer. val is the actual byte to output in the case + of a literal, the base length or distance, or the offset from + the current table to the next table. Each entry is four bytes. */ +typedef struct { + unsigned char op; /* operation, extra bits, table bits */ + unsigned char bits; /* bits in this part of the code */ + unsigned short val; /* offset in table or code value */ +} code; + +/* op values as set by inflate_table(): + 00000000 - literal + 0000tttt - table link, tttt != 0 is the number of table index bits + 0001eeee - length or distance, eeee is the number of extra bits + 01100000 - end of block + 01000000 - invalid code + */ + +/* Maximum size of the dynamic table. The maximum number of code structures is + 1444, which is the sum of 852 for literal/length codes and 592 for distance + codes. These values were found by exhaustive searches using the program + examples/enough.c found in the zlib distribtution. The arguments to that + program are the number of symbols, the initial root table size, and the + maximum bit length of a code. "enough 286 9 15" for literal/length codes + returns returns 852, and "enough 30 6 15" for distance codes returns 592. + The initial root table size (9 or 6) is found in the fifth argument of the + inflate_table() calls in inflate.c and infback.c. If the root table size is + changed, then these maximum sizes would be need to be recalculated and + updated. */ +#define ENOUGH_LENS 852 +#define ENOUGH_DISTS 592 +#define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS) + +/* Type of code to build for inflate_table() */ +typedef enum { + CODES, + LENS, + DISTS +} codetype; + +int ZLIB_INTERNAL inflate_table OF((codetype type, unsigned short FAR *lens, + unsigned codes, code FAR * FAR *table, + unsigned FAR *bits, unsigned short FAR *work)); diff --git a/third_party/java_tools/zlib/trees.c b/third_party/java_tools/zlib/trees.c new file mode 100644 index 000000000..50cf4b457 --- /dev/null +++ b/third_party/java_tools/zlib/trees.c @@ -0,0 +1,1203 @@ +/* trees.c -- output deflated data using Huffman coding + * Copyright (C) 1995-2017 Jean-loup Gailly + * detect_data_type() function provided freely by Cosmin Truta, 2006 + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* + * ALGORITHM + * + * The "deflation" process uses several Huffman trees. The more + * common source values are represented by shorter bit sequences. + * + * Each code tree is stored in a compressed form which is itself + * a Huffman encoding of the lengths of all the code strings (in + * ascending order by source values). The actual code strings are + * reconstructed from the lengths in the inflate process, as described + * in the deflate specification. + * + * REFERENCES + * + * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification". + * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc + * + * Storer, James A. + * Data Compression: Methods and Theory, pp. 49-50. + * Computer Science Press, 1988. ISBN 0-7167-8156-5. + * + * Sedgewick, R. + * Algorithms, p290. + * Addison-Wesley, 1983. ISBN 0-201-06672-6. + */ + +/* @(#) $Id$ */ + +/* #define GEN_TREES_H */ + +#include "deflate.h" + +#ifdef ZLIB_DEBUG +# include +#endif + +/* =========================================================================== + * Constants + */ + +#define MAX_BL_BITS 7 +/* Bit length codes must not exceed MAX_BL_BITS bits */ + +#define END_BLOCK 256 +/* end of block literal code */ + +#define REP_3_6 16 +/* repeat previous bit length 3-6 times (2 bits of repeat count) */ + +#define REPZ_3_10 17 +/* repeat a zero length 3-10 times (3 bits of repeat count) */ + +#define REPZ_11_138 18 +/* repeat a zero length 11-138 times (7 bits of repeat count) */ + +local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */ + = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0}; + +local const int extra_dbits[D_CODES] /* extra bits for each distance code */ + = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}; + +local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */ + = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7}; + +local const uch bl_order[BL_CODES] + = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15}; +/* The lengths of the bit length codes are sent in order of decreasing + * probability, to avoid transmitting the lengths for unused bit length codes. + */ + +/* =========================================================================== + * Local data. These are initialized only once. + */ + +#define DIST_CODE_LEN 512 /* see definition of array dist_code below */ + +#if defined(GEN_TREES_H) || !defined(STDC) +/* non ANSI compilers may not accept trees.h */ + +local ct_data static_ltree[L_CODES+2]; +/* The static literal tree. Since the bit lengths are imposed, there is no + * need for the L_CODES extra codes used during heap construction. However + * The codes 286 and 287 are needed to build a canonical tree (see _tr_init + * below). + */ + +local ct_data static_dtree[D_CODES]; +/* The static distance tree. (Actually a trivial tree since all codes use + * 5 bits.) + */ + +uch _dist_code[DIST_CODE_LEN]; +/* Distance codes. The first 256 values correspond to the distances + * 3 .. 258, the last 256 values correspond to the top 8 bits of + * the 15 bit distances. + */ + +uch _length_code[MAX_MATCH-MIN_MATCH+1]; +/* length code for each normalized match length (0 == MIN_MATCH) */ + +local int base_length[LENGTH_CODES]; +/* First normalized length for each code (0 = MIN_MATCH) */ + +local int base_dist[D_CODES]; +/* First normalized distance for each code (0 = distance of 1) */ + +#else +# include "trees.h" +#endif /* GEN_TREES_H */ + +struct static_tree_desc_s { + const ct_data *static_tree; /* static tree or NULL */ + const intf *extra_bits; /* extra bits for each code or NULL */ + int extra_base; /* base index for extra_bits */ + int elems; /* max number of elements in the tree */ + int max_length; /* max bit length for the codes */ +}; + +local const static_tree_desc static_l_desc = +{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS}; + +local const static_tree_desc static_d_desc = +{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS}; + +local const static_tree_desc static_bl_desc = +{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS}; + +/* =========================================================================== + * Local (static) routines in this file. + */ + +local void tr_static_init OF((void)); +local void init_block OF((deflate_state *s)); +local void pqdownheap OF((deflate_state *s, ct_data *tree, int k)); +local void gen_bitlen OF((deflate_state *s, tree_desc *desc)); +local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count)); +local void build_tree OF((deflate_state *s, tree_desc *desc)); +local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code)); +local void send_tree OF((deflate_state *s, ct_data *tree, int max_code)); +local int build_bl_tree OF((deflate_state *s)); +local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes, + int blcodes)); +local void compress_block OF((deflate_state *s, const ct_data *ltree, + const ct_data *dtree)); +local int detect_data_type OF((deflate_state *s)); +local unsigned bi_reverse OF((unsigned value, int length)); +local void bi_windup OF((deflate_state *s)); +local void bi_flush OF((deflate_state *s)); + +#ifdef GEN_TREES_H +local void gen_trees_header OF((void)); +#endif + +#ifndef ZLIB_DEBUG +# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len) + /* Send a code of the given tree. c and tree must not have side effects */ + +#else /* !ZLIB_DEBUG */ +# define send_code(s, c, tree) \ + { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \ + send_bits(s, tree[c].Code, tree[c].Len); } +#endif + +/* =========================================================================== + * Output a short LSB first on the stream. + * IN assertion: there is enough room in pendingBuf. + */ +#define put_short(s, w) { \ + put_byte(s, (uch)((w) & 0xff)); \ + put_byte(s, (uch)((ush)(w) >> 8)); \ +} + +/* =========================================================================== + * Send a value on a given number of bits. + * IN assertion: length <= 16 and value fits in length bits. + */ +#ifdef ZLIB_DEBUG +local void send_bits OF((deflate_state *s, int value, int length)); + +local void send_bits(s, value, length) + deflate_state *s; + int value; /* value to send */ + int length; /* number of bits */ +{ + Tracevv((stderr," l %2d v %4x ", length, value)); + Assert(length > 0 && length <= 15, "invalid length"); + s->bits_sent += (ulg)length; + + /* If not enough room in bi_buf, use (valid) bits from bi_buf and + * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid)) + * unused bits in value. + */ + if (s->bi_valid > (int)Buf_size - length) { + s->bi_buf |= (ush)value << s->bi_valid; + put_short(s, s->bi_buf); + s->bi_buf = (ush)value >> (Buf_size - s->bi_valid); + s->bi_valid += length - Buf_size; + } else { + s->bi_buf |= (ush)value << s->bi_valid; + s->bi_valid += length; + } +} +#else /* !ZLIB_DEBUG */ + +#define send_bits(s, value, length) \ +{ int len = length;\ + if (s->bi_valid > (int)Buf_size - len) {\ + int val = (int)value;\ + s->bi_buf |= (ush)val << s->bi_valid;\ + put_short(s, s->bi_buf);\ + s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\ + s->bi_valid += len - Buf_size;\ + } else {\ + s->bi_buf |= (ush)(value) << s->bi_valid;\ + s->bi_valid += len;\ + }\ +} +#endif /* ZLIB_DEBUG */ + + +/* the arguments must not have side effects */ + +/* =========================================================================== + * Initialize the various 'constant' tables. + */ +local void tr_static_init() +{ +#if defined(GEN_TREES_H) || !defined(STDC) + static int static_init_done = 0; + int n; /* iterates over tree elements */ + int bits; /* bit counter */ + int length; /* length value */ + int code; /* code value */ + int dist; /* distance index */ + ush bl_count[MAX_BITS+1]; + /* number of codes at each bit length for an optimal tree */ + + if (static_init_done) return; + + /* For some embedded targets, global variables are not initialized: */ +#ifdef NO_INIT_GLOBAL_POINTERS + static_l_desc.static_tree = static_ltree; + static_l_desc.extra_bits = extra_lbits; + static_d_desc.static_tree = static_dtree; + static_d_desc.extra_bits = extra_dbits; + static_bl_desc.extra_bits = extra_blbits; +#endif + + /* Initialize the mapping length (0..255) -> length code (0..28) */ + length = 0; + for (code = 0; code < LENGTH_CODES-1; code++) { + base_length[code] = length; + for (n = 0; n < (1< dist code (0..29) */ + dist = 0; + for (code = 0 ; code < 16; code++) { + base_dist[code] = dist; + for (n = 0; n < (1<>= 7; /* from now on, all distances are divided by 128 */ + for ( ; code < D_CODES; code++) { + base_dist[code] = dist << 7; + for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) { + _dist_code[256 + dist++] = (uch)code; + } + } + Assert (dist == 256, "tr_static_init: 256+dist != 512"); + + /* Construct the codes of the static literal tree */ + for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0; + n = 0; + while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++; + while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++; + while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++; + while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++; + /* Codes 286 and 287 do not exist, but we must include them in the + * tree construction to get a canonical Huffman tree (longest code + * all ones) + */ + gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count); + + /* The static distance tree is trivial: */ + for (n = 0; n < D_CODES; n++) { + static_dtree[n].Len = 5; + static_dtree[n].Code = bi_reverse((unsigned)n, 5); + } + static_init_done = 1; + +# ifdef GEN_TREES_H + gen_trees_header(); +# endif +#endif /* defined(GEN_TREES_H) || !defined(STDC) */ +} + +/* =========================================================================== + * Genererate the file trees.h describing the static trees. + */ +#ifdef GEN_TREES_H +# ifndef ZLIB_DEBUG +# include +# endif + +# define SEPARATOR(i, last, width) \ + ((i) == (last)? "\n};\n\n" : \ + ((i) % (width) == (width)-1 ? ",\n" : ", ")) + +void gen_trees_header() +{ + FILE *header = fopen("trees.h", "w"); + int i; + + Assert (header != NULL, "Can't open trees.h"); + fprintf(header, + "/* header created automatically with -DGEN_TREES_H */\n\n"); + + fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n"); + for (i = 0; i < L_CODES+2; i++) { + fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code, + static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5)); + } + + fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n"); + for (i = 0; i < D_CODES; i++) { + fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code, + static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5)); + } + + fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n"); + for (i = 0; i < DIST_CODE_LEN; i++) { + fprintf(header, "%2u%s", _dist_code[i], + SEPARATOR(i, DIST_CODE_LEN-1, 20)); + } + + fprintf(header, + "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n"); + for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) { + fprintf(header, "%2u%s", _length_code[i], + SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20)); + } + + fprintf(header, "local const int base_length[LENGTH_CODES] = {\n"); + for (i = 0; i < LENGTH_CODES; i++) { + fprintf(header, "%1u%s", base_length[i], + SEPARATOR(i, LENGTH_CODES-1, 20)); + } + + fprintf(header, "local const int base_dist[D_CODES] = {\n"); + for (i = 0; i < D_CODES; i++) { + fprintf(header, "%5u%s", base_dist[i], + SEPARATOR(i, D_CODES-1, 10)); + } + + fclose(header); +} +#endif /* GEN_TREES_H */ + +/* =========================================================================== + * Initialize the tree data structures for a new zlib stream. + */ +void ZLIB_INTERNAL _tr_init(s) + deflate_state *s; +{ + tr_static_init(); + + s->l_desc.dyn_tree = s->dyn_ltree; + s->l_desc.stat_desc = &static_l_desc; + + s->d_desc.dyn_tree = s->dyn_dtree; + s->d_desc.stat_desc = &static_d_desc; + + s->bl_desc.dyn_tree = s->bl_tree; + s->bl_desc.stat_desc = &static_bl_desc; + + s->bi_buf = 0; + s->bi_valid = 0; +#ifdef ZLIB_DEBUG + s->compressed_len = 0L; + s->bits_sent = 0L; +#endif + + /* Initialize the first block of the first file: */ + init_block(s); +} + +/* =========================================================================== + * Initialize a new block. + */ +local void init_block(s) + deflate_state *s; +{ + int n; /* iterates over tree elements */ + + /* Initialize the trees. */ + for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0; + for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0; + for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0; + + s->dyn_ltree[END_BLOCK].Freq = 1; + s->opt_len = s->static_len = 0L; + s->last_lit = s->matches = 0; +} + +#define SMALLEST 1 +/* Index within the heap array of least frequent node in the Huffman tree */ + + +/* =========================================================================== + * Remove the smallest element from the heap and recreate the heap with + * one less element. Updates heap and heap_len. + */ +#define pqremove(s, tree, top) \ +{\ + top = s->heap[SMALLEST]; \ + s->heap[SMALLEST] = s->heap[s->heap_len--]; \ + pqdownheap(s, tree, SMALLEST); \ +} + +/* =========================================================================== + * Compares to subtrees, using the tree depth as tie breaker when + * the subtrees have equal frequency. This minimizes the worst case length. + */ +#define smaller(tree, n, m, depth) \ + (tree[n].Freq < tree[m].Freq || \ + (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m])) + +/* =========================================================================== + * Restore the heap property by moving down the tree starting at node k, + * exchanging a node with the smallest of its two sons if necessary, stopping + * when the heap property is re-established (each father smaller than its + * two sons). + */ +local void pqdownheap(s, tree, k) + deflate_state *s; + ct_data *tree; /* the tree to restore */ + int k; /* node to move down */ +{ + int v = s->heap[k]; + int j = k << 1; /* left son of k */ + while (j <= s->heap_len) { + /* Set j to the smallest of the two sons: */ + if (j < s->heap_len && + smaller(tree, s->heap[j+1], s->heap[j], s->depth)) { + j++; + } + /* Exit if v is smaller than both sons */ + if (smaller(tree, v, s->heap[j], s->depth)) break; + + /* Exchange v with the smallest son */ + s->heap[k] = s->heap[j]; k = j; + + /* And continue down the tree, setting j to the left son of k */ + j <<= 1; + } + s->heap[k] = v; +} + +/* =========================================================================== + * Compute the optimal bit lengths for a tree and update the total bit length + * for the current block. + * IN assertion: the fields freq and dad are set, heap[heap_max] and + * above are the tree nodes sorted by increasing frequency. + * OUT assertions: the field len is set to the optimal bit length, the + * array bl_count contains the frequencies for each bit length. + * The length opt_len is updated; static_len is also updated if stree is + * not null. + */ +local void gen_bitlen(s, desc) + deflate_state *s; + tree_desc *desc; /* the tree descriptor */ +{ + ct_data *tree = desc->dyn_tree; + int max_code = desc->max_code; + const ct_data *stree = desc->stat_desc->static_tree; + const intf *extra = desc->stat_desc->extra_bits; + int base = desc->stat_desc->extra_base; + int max_length = desc->stat_desc->max_length; + int h; /* heap index */ + int n, m; /* iterate over the tree elements */ + int bits; /* bit length */ + int xbits; /* extra bits */ + ush f; /* frequency */ + int overflow = 0; /* number of elements with bit length too large */ + + for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0; + + /* In a first pass, compute the optimal bit lengths (which may + * overflow in the case of the bit length tree). + */ + tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */ + + for (h = s->heap_max+1; h < HEAP_SIZE; h++) { + n = s->heap[h]; + bits = tree[tree[n].Dad].Len + 1; + if (bits > max_length) bits = max_length, overflow++; + tree[n].Len = (ush)bits; + /* We overwrite tree[n].Dad which is no longer needed */ + + if (n > max_code) continue; /* not a leaf node */ + + s->bl_count[bits]++; + xbits = 0; + if (n >= base) xbits = extra[n-base]; + f = tree[n].Freq; + s->opt_len += (ulg)f * (unsigned)(bits + xbits); + if (stree) s->static_len += (ulg)f * (unsigned)(stree[n].Len + xbits); + } + if (overflow == 0) return; + + Tracev((stderr,"\nbit length overflow\n")); + /* This happens for example on obj2 and pic of the Calgary corpus */ + + /* Find the first bit length which could increase: */ + do { + bits = max_length-1; + while (s->bl_count[bits] == 0) bits--; + s->bl_count[bits]--; /* move one leaf down the tree */ + s->bl_count[bits+1] += 2; /* move one overflow item as its brother */ + s->bl_count[max_length]--; + /* The brother of the overflow item also moves one step up, + * but this does not affect bl_count[max_length] + */ + overflow -= 2; + } while (overflow > 0); + + /* Now recompute all bit lengths, scanning in increasing frequency. + * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all + * lengths instead of fixing only the wrong ones. This idea is taken + * from 'ar' written by Haruhiko Okumura.) + */ + for (bits = max_length; bits != 0; bits--) { + n = s->bl_count[bits]; + while (n != 0) { + m = s->heap[--h]; + if (m > max_code) continue; + if ((unsigned) tree[m].Len != (unsigned) bits) { + Tracev((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); + s->opt_len += ((ulg)bits - tree[m].Len) * tree[m].Freq; + tree[m].Len = (ush)bits; + } + n--; + } + } +} + +/* =========================================================================== + * Generate the codes for a given tree and bit counts (which need not be + * optimal). + * IN assertion: the array bl_count contains the bit length statistics for + * the given tree and the field len is set for all tree elements. + * OUT assertion: the field code is set for all tree elements of non + * zero code length. + */ +local void gen_codes (tree, max_code, bl_count) + ct_data *tree; /* the tree to decorate */ + int max_code; /* largest code with non zero frequency */ + ushf *bl_count; /* number of codes at each bit length */ +{ + ush next_code[MAX_BITS+1]; /* next code value for each bit length */ + unsigned code = 0; /* running code value */ + int bits; /* bit index */ + int n; /* code index */ + + /* The distribution counts are first used to generate the code values + * without bit reversal. + */ + for (bits = 1; bits <= MAX_BITS; bits++) { + code = (code + bl_count[bits-1]) << 1; + next_code[bits] = (ush)code; + } + /* Check that the bit counts in bl_count are consistent. The last code + * must be all ones. + */ + Assert (code + bl_count[MAX_BITS]-1 == (1<dyn_tree; + const ct_data *stree = desc->stat_desc->static_tree; + int elems = desc->stat_desc->elems; + int n, m; /* iterate over heap elements */ + int max_code = -1; /* largest code with non zero frequency */ + int node; /* new node being created */ + + /* Construct the initial heap, with least frequent element in + * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1]. + * heap[0] is not used. + */ + s->heap_len = 0, s->heap_max = HEAP_SIZE; + + for (n = 0; n < elems; n++) { + if (tree[n].Freq != 0) { + s->heap[++(s->heap_len)] = max_code = n; + s->depth[n] = 0; + } else { + tree[n].Len = 0; + } + } + + /* The pkzip format requires that at least one distance code exists, + * and that at least one bit should be sent even if there is only one + * possible code. So to avoid special checks later on we force at least + * two codes of non zero frequency. + */ + while (s->heap_len < 2) { + node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0); + tree[node].Freq = 1; + s->depth[node] = 0; + s->opt_len--; if (stree) s->static_len -= stree[node].Len; + /* node is 0 or 1 so it does not have extra bits */ + } + desc->max_code = max_code; + + /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, + * establish sub-heaps of increasing lengths: + */ + for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n); + + /* Construct the Huffman tree by repeatedly combining the least two + * frequent nodes. + */ + node = elems; /* next internal node of the tree */ + do { + pqremove(s, tree, n); /* n = node of least frequency */ + m = s->heap[SMALLEST]; /* m = node of next least frequency */ + + s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */ + s->heap[--(s->heap_max)] = m; + + /* Create a new node father of n and m */ + tree[node].Freq = tree[n].Freq + tree[m].Freq; + s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ? + s->depth[n] : s->depth[m]) + 1); + tree[n].Dad = tree[m].Dad = (ush)node; +#ifdef DUMP_BL_TREE + if (tree == s->bl_tree) { + fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)", + node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq); + } +#endif + /* and insert the new node in the heap */ + s->heap[SMALLEST] = node++; + pqdownheap(s, tree, SMALLEST); + + } while (s->heap_len >= 2); + + s->heap[--(s->heap_max)] = s->heap[SMALLEST]; + + /* At this point, the fields freq and dad are set. We can now + * generate the bit lengths. + */ + gen_bitlen(s, (tree_desc *)desc); + + /* The field len is now set, we can generate the bit codes */ + gen_codes ((ct_data *)tree, max_code, s->bl_count); +} + +/* =========================================================================== + * Scan a literal or distance tree to determine the frequencies of the codes + * in the bit length tree. + */ +local void scan_tree (s, tree, max_code) + deflate_state *s; + ct_data *tree; /* the tree to be scanned */ + int max_code; /* and its largest code of non zero frequency */ +{ + int n; /* iterates over all tree elements */ + int prevlen = -1; /* last emitted length */ + int curlen; /* length of current code */ + int nextlen = tree[0].Len; /* length of next code */ + int count = 0; /* repeat count of the current code */ + int max_count = 7; /* max repeat count */ + int min_count = 4; /* min repeat count */ + + if (nextlen == 0) max_count = 138, min_count = 3; + tree[max_code+1].Len = (ush)0xffff; /* guard */ + + for (n = 0; n <= max_code; n++) { + curlen = nextlen; nextlen = tree[n+1].Len; + if (++count < max_count && curlen == nextlen) { + continue; + } else if (count < min_count) { + s->bl_tree[curlen].Freq += count; + } else if (curlen != 0) { + if (curlen != prevlen) s->bl_tree[curlen].Freq++; + s->bl_tree[REP_3_6].Freq++; + } else if (count <= 10) { + s->bl_tree[REPZ_3_10].Freq++; + } else { + s->bl_tree[REPZ_11_138].Freq++; + } + count = 0; prevlen = curlen; + if (nextlen == 0) { + max_count = 138, min_count = 3; + } else if (curlen == nextlen) { + max_count = 6, min_count = 3; + } else { + max_count = 7, min_count = 4; + } + } +} + +/* =========================================================================== + * Send a literal or distance tree in compressed form, using the codes in + * bl_tree. + */ +local void send_tree (s, tree, max_code) + deflate_state *s; + ct_data *tree; /* the tree to be scanned */ + int max_code; /* and its largest code of non zero frequency */ +{ + int n; /* iterates over all tree elements */ + int prevlen = -1; /* last emitted length */ + int curlen; /* length of current code */ + int nextlen = tree[0].Len; /* length of next code */ + int count = 0; /* repeat count of the current code */ + int max_count = 7; /* max repeat count */ + int min_count = 4; /* min repeat count */ + + /* tree[max_code+1].Len = -1; */ /* guard already set */ + if (nextlen == 0) max_count = 138, min_count = 3; + + for (n = 0; n <= max_code; n++) { + curlen = nextlen; nextlen = tree[n+1].Len; + if (++count < max_count && curlen == nextlen) { + continue; + } else if (count < min_count) { + do { send_code(s, curlen, s->bl_tree); } while (--count != 0); + + } else if (curlen != 0) { + if (curlen != prevlen) { + send_code(s, curlen, s->bl_tree); count--; + } + Assert(count >= 3 && count <= 6, " 3_6?"); + send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2); + + } else if (count <= 10) { + send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3); + + } else { + send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7); + } + count = 0; prevlen = curlen; + if (nextlen == 0) { + max_count = 138, min_count = 3; + } else if (curlen == nextlen) { + max_count = 6, min_count = 3; + } else { + max_count = 7, min_count = 4; + } + } +} + +/* =========================================================================== + * Construct the Huffman tree for the bit lengths and return the index in + * bl_order of the last bit length code to send. + */ +local int build_bl_tree(s) + deflate_state *s; +{ + int max_blindex; /* index of last bit length code of non zero freq */ + + /* Determine the bit length frequencies for literal and distance trees */ + scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code); + scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code); + + /* Build the bit length tree: */ + build_tree(s, (tree_desc *)(&(s->bl_desc))); + /* opt_len now includes the length of the tree representations, except + * the lengths of the bit lengths codes and the 5+5+4 bits for the counts. + */ + + /* Determine the number of bit length codes to send. The pkzip format + * requires that at least 4 bit length codes be sent. (appnote.txt says + * 3 but the actual value used is 4.) + */ + for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) { + if (s->bl_tree[bl_order[max_blindex]].Len != 0) break; + } + /* Update opt_len to include the bit length tree and counts */ + s->opt_len += 3*((ulg)max_blindex+1) + 5+5+4; + Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", + s->opt_len, s->static_len)); + + return max_blindex; +} + +/* =========================================================================== + * Send the header for a block using dynamic Huffman trees: the counts, the + * lengths of the bit length codes, the literal tree and the distance tree. + * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. + */ +local void send_all_trees(s, lcodes, dcodes, blcodes) + deflate_state *s; + int lcodes, dcodes, blcodes; /* number of codes for each tree */ +{ + int rank; /* index in bl_order */ + + Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes"); + Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, + "too many codes"); + Tracev((stderr, "\nbl counts: ")); + send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */ + send_bits(s, dcodes-1, 5); + send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */ + for (rank = 0; rank < blcodes; rank++) { + Tracev((stderr, "\nbl code %2d ", bl_order[rank])); + send_bits(s, s->bl_tree[bl_order[rank]].Len, 3); + } + Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent)); + + send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */ + Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent)); + + send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */ + Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent)); +} + +/* =========================================================================== + * Send a stored block + */ +void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last) + deflate_state *s; + charf *buf; /* input block */ + ulg stored_len; /* length of input block */ + int last; /* one if this is the last block for a file */ +{ + send_bits(s, (STORED_BLOCK<<1)+last, 3); /* send block type */ + bi_windup(s); /* align on byte boundary */ + put_short(s, (ush)stored_len); + put_short(s, (ush)~stored_len); + zmemcpy(s->pending_buf + s->pending, (Bytef *)buf, stored_len); + s->pending += stored_len; +#ifdef ZLIB_DEBUG + s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L; + s->compressed_len += (stored_len + 4) << 3; + s->bits_sent += 2*16; + s->bits_sent += stored_len<<3; +#endif +} + +/* =========================================================================== + * Flush the bits in the bit buffer to pending output (leaves at most 7 bits) + */ +void ZLIB_INTERNAL _tr_flush_bits(s) + deflate_state *s; +{ + bi_flush(s); +} + +/* =========================================================================== + * Send one empty static block to give enough lookahead for inflate. + * This takes 10 bits, of which 7 may remain in the bit buffer. + */ +void ZLIB_INTERNAL _tr_align(s) + deflate_state *s; +{ + send_bits(s, STATIC_TREES<<1, 3); + send_code(s, END_BLOCK, static_ltree); +#ifdef ZLIB_DEBUG + s->compressed_len += 10L; /* 3 for block type, 7 for EOB */ +#endif + bi_flush(s); +} + +/* =========================================================================== + * Determine the best encoding for the current block: dynamic trees, static + * trees or store, and write out the encoded block. + */ +void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) + deflate_state *s; + charf *buf; /* input block, or NULL if too old */ + ulg stored_len; /* length of input block */ + int last; /* one if this is the last block for a file */ +{ + ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ + int max_blindex = 0; /* index of last bit length code of non zero freq */ + + /* Build the Huffman trees unless a stored block is forced */ + if (s->level > 0) { + + /* Check if the file is binary or text */ + if (s->strm->data_type == Z_UNKNOWN) + s->strm->data_type = detect_data_type(s); + + /* Construct the literal and distance trees */ + build_tree(s, (tree_desc *)(&(s->l_desc))); + Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, + s->static_len)); + + build_tree(s, (tree_desc *)(&(s->d_desc))); + Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, + s->static_len)); + /* At this point, opt_len and static_len are the total bit lengths of + * the compressed block data, excluding the tree representations. + */ + + /* Build the bit length tree for the above two trees, and get the index + * in bl_order of the last bit length code to send. + */ + max_blindex = build_bl_tree(s); + + /* Determine the best encoding. Compute the block lengths in bytes. */ + opt_lenb = (s->opt_len+3+7)>>3; + static_lenb = (s->static_len+3+7)>>3; + + Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", + opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, + s->last_lit)); + + if (static_lenb <= opt_lenb) opt_lenb = static_lenb; + + } else { + Assert(buf != (char*)0, "lost buf"); + opt_lenb = static_lenb = stored_len + 5; /* force a stored block */ + } + +#ifdef FORCE_STORED + if (buf != (char*)0) { /* force stored block */ +#else + if (stored_len+4 <= opt_lenb && buf != (char*)0) { + /* 4: two words for the lengths */ +#endif + /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. + * Otherwise we can't have processed more than WSIZE input bytes since + * the last block flush, because compression would have been + * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to + * transform a block into a stored block. + */ + _tr_stored_block(s, buf, stored_len, last); + +#ifdef FORCE_STATIC + } else if (static_lenb >= 0) { /* force static trees */ +#else + } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) { +#endif + send_bits(s, (STATIC_TREES<<1)+last, 3); + compress_block(s, (const ct_data *)static_ltree, + (const ct_data *)static_dtree); +#ifdef ZLIB_DEBUG + s->compressed_len += 3 + s->static_len; +#endif + } else { + send_bits(s, (DYN_TREES<<1)+last, 3); + send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, + max_blindex+1); + compress_block(s, (const ct_data *)s->dyn_ltree, + (const ct_data *)s->dyn_dtree); +#ifdef ZLIB_DEBUG + s->compressed_len += 3 + s->opt_len; +#endif + } + Assert (s->compressed_len == s->bits_sent, "bad compressed size"); + /* The above check is made mod 2^32, for files larger than 512 MB + * and uLong implemented on 32 bits. + */ + init_block(s); + + if (last) { + bi_windup(s); +#ifdef ZLIB_DEBUG + s->compressed_len += 7; /* align on byte boundary */ +#endif + } + Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3, + s->compressed_len-7*last)); +} + +/* =========================================================================== + * Save the match info and tally the frequency counts. Return true if + * the current block must be flushed. + */ +int ZLIB_INTERNAL _tr_tally (s, dist, lc) + deflate_state *s; + unsigned dist; /* distance of matched string */ + unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */ +{ + s->d_buf[s->last_lit] = (ush)dist; + s->l_buf[s->last_lit++] = (uch)lc; + if (dist == 0) { + /* lc is the unmatched char */ + s->dyn_ltree[lc].Freq++; + } else { + s->matches++; + /* Here, lc is the match length - MIN_MATCH */ + dist--; /* dist = match distance - 1 */ + Assert((ush)dist < (ush)MAX_DIST(s) && + (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && + (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match"); + + s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++; + s->dyn_dtree[d_code(dist)].Freq++; + } + +#ifdef TRUNCATE_BLOCK + /* Try to guess if it is profitable to stop the current block here */ + if ((s->last_lit & 0x1fff) == 0 && s->level > 2) { + /* Compute an upper bound for the compressed length */ + ulg out_length = (ulg)s->last_lit*8L; + ulg in_length = (ulg)((long)s->strstart - s->block_start); + int dcode; + for (dcode = 0; dcode < D_CODES; dcode++) { + out_length += (ulg)s->dyn_dtree[dcode].Freq * + (5L+extra_dbits[dcode]); + } + out_length >>= 3; + Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ", + s->last_lit, in_length, out_length, + 100L - out_length*100L/in_length)); + if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1; + } +#endif + return (s->last_lit == s->lit_bufsize-1); + /* We avoid equality with lit_bufsize because of wraparound at 64K + * on 16 bit machines and because stored blocks are restricted to + * 64K-1 bytes. + */ +} + +/* =========================================================================== + * Send the block data compressed using the given Huffman trees + */ +local void compress_block(s, ltree, dtree) + deflate_state *s; + const ct_data *ltree; /* literal tree */ + const ct_data *dtree; /* distance tree */ +{ + unsigned dist; /* distance of matched string */ + int lc; /* match length or unmatched char (if dist == 0) */ + unsigned lx = 0; /* running index in l_buf */ + unsigned code; /* the code to send */ + int extra; /* number of extra bits to send */ + + if (s->last_lit != 0) do { + dist = s->d_buf[lx]; + lc = s->l_buf[lx++]; + if (dist == 0) { + send_code(s, lc, ltree); /* send a literal byte */ + Tracecv(isgraph(lc), (stderr," '%c' ", lc)); + } else { + /* Here, lc is the match length - MIN_MATCH */ + code = _length_code[lc]; + send_code(s, code+LITERALS+1, ltree); /* send the length code */ + extra = extra_lbits[code]; + if (extra != 0) { + lc -= base_length[code]; + send_bits(s, lc, extra); /* send the extra length bits */ + } + dist--; /* dist is now the match distance - 1 */ + code = d_code(dist); + Assert (code < D_CODES, "bad d_code"); + + send_code(s, code, dtree); /* send the distance code */ + extra = extra_dbits[code]; + if (extra != 0) { + dist -= (unsigned)base_dist[code]; + send_bits(s, dist, extra); /* send the extra distance bits */ + } + } /* literal or match pair ? */ + + /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ + Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx, + "pendingBuf overflow"); + + } while (lx < s->last_lit); + + send_code(s, END_BLOCK, ltree); +} + +/* =========================================================================== + * Check if the data type is TEXT or BINARY, using the following algorithm: + * - TEXT if the two conditions below are satisfied: + * a) There are no non-portable control characters belonging to the + * "black list" (0..6, 14..25, 28..31). + * b) There is at least one printable character belonging to the + * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255). + * - BINARY otherwise. + * - The following partially-portable control characters form a + * "gray list" that is ignored in this detection algorithm: + * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}). + * IN assertion: the fields Freq of dyn_ltree are set. + */ +local int detect_data_type(s) + deflate_state *s; +{ + /* black_mask is the bit mask of black-listed bytes + * set bits 0..6, 14..25, and 28..31 + * 0xf3ffc07f = binary 11110011111111111100000001111111 + */ + unsigned long black_mask = 0xf3ffc07fUL; + int n; + + /* Check for non-textual ("black-listed") bytes. */ + for (n = 0; n <= 31; n++, black_mask >>= 1) + if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0)) + return Z_BINARY; + + /* Check for textual ("white-listed") bytes. */ + if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0 + || s->dyn_ltree[13].Freq != 0) + return Z_TEXT; + for (n = 32; n < LITERALS; n++) + if (s->dyn_ltree[n].Freq != 0) + return Z_TEXT; + + /* There are no "black-listed" or "white-listed" bytes: + * this stream either is empty or has tolerated ("gray-listed") bytes only. + */ + return Z_BINARY; +} + +/* =========================================================================== + * Reverse the first len bits of a code, using straightforward code (a faster + * method would use a table) + * IN assertion: 1 <= len <= 15 + */ +local unsigned bi_reverse(code, len) + unsigned code; /* the value to invert */ + int len; /* its bit length */ +{ + register unsigned res = 0; + do { + res |= code & 1; + code >>= 1, res <<= 1; + } while (--len > 0); + return res >> 1; +} + +/* =========================================================================== + * Flush the bit buffer, keeping at most 7 bits in it. + */ +local void bi_flush(s) + deflate_state *s; +{ + if (s->bi_valid == 16) { + put_short(s, s->bi_buf); + s->bi_buf = 0; + s->bi_valid = 0; + } else if (s->bi_valid >= 8) { + put_byte(s, (Byte)s->bi_buf); + s->bi_buf >>= 8; + s->bi_valid -= 8; + } +} + +/* =========================================================================== + * Flush the bit buffer and align the output on a byte boundary + */ +local void bi_windup(s) + deflate_state *s; +{ + if (s->bi_valid > 8) { + put_short(s, s->bi_buf); + } else if (s->bi_valid > 0) { + put_byte(s, (Byte)s->bi_buf); + } + s->bi_buf = 0; + s->bi_valid = 0; +#ifdef ZLIB_DEBUG + s->bits_sent = (s->bits_sent+7) & ~7; +#endif +} diff --git a/third_party/java_tools/zlib/trees.h b/third_party/java_tools/zlib/trees.h new file mode 100644 index 000000000..d35639d82 --- /dev/null +++ b/third_party/java_tools/zlib/trees.h @@ -0,0 +1,128 @@ +/* header created automatically with -DGEN_TREES_H */ + +local const ct_data static_ltree[L_CODES+2] = { +{{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}}, +{{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}}, +{{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}}, +{{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}}, +{{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}}, +{{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}}, +{{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}}, +{{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}}, +{{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}}, +{{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}}, +{{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}}, +{{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}}, +{{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}}, +{{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}}, +{{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}}, +{{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}}, +{{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}}, +{{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}}, +{{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}}, +{{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}}, +{{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}}, +{{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}}, +{{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}}, +{{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}}, +{{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}}, +{{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}}, +{{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}}, +{{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}}, +{{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}}, +{{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}}, +{{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}}, +{{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}}, +{{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}}, +{{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}}, +{{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}}, +{{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}}, +{{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}}, +{{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}}, +{{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}}, +{{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}}, +{{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}}, +{{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}}, +{{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}}, +{{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}}, +{{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}}, +{{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}}, +{{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}}, +{{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}}, +{{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}}, +{{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}}, +{{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}}, +{{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}}, +{{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}}, +{{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}}, +{{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}}, +{{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}}, +{{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}}, +{{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}} +}; + +local const ct_data static_dtree[D_CODES] = { +{{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}}, +{{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}}, +{{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}}, +{{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}}, +{{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}}, +{{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}} +}; + +const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = { + 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, + 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, +10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, +11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, +12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, +13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, +13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17, +18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, +23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, +24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, +27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, +28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, +28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, +28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29 +}; + +const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12, +13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, +17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, +19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, +21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, +22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, +23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, +24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, +25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, +25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, +26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28 +}; + +local const int base_length[LENGTH_CODES] = { +0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, +64, 80, 96, 112, 128, 160, 192, 224, 0 +}; + +local const int base_dist[D_CODES] = { + 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, + 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, + 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576 +}; + diff --git a/third_party/java_tools/zlib/uncompr.c b/third_party/java_tools/zlib/uncompr.c new file mode 100644 index 000000000..f03a1a865 --- /dev/null +++ b/third_party/java_tools/zlib/uncompr.c @@ -0,0 +1,93 @@ +/* uncompr.c -- decompress a memory buffer + * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* @(#) $Id$ */ + +#define ZLIB_INTERNAL +#include "zlib.h" + +/* =========================================================================== + Decompresses the source buffer into the destination buffer. *sourceLen is + the byte length of the source buffer. Upon entry, *destLen is the total size + of the destination buffer, which must be large enough to hold the entire + uncompressed data. (The size of the uncompressed data must have been saved + previously by the compressor and transmitted to the decompressor by some + mechanism outside the scope of this compression library.) Upon exit, + *destLen is the size of the decompressed data and *sourceLen is the number + of source bytes consumed. Upon return, source + *sourceLen points to the + first unused input byte. + + uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_BUF_ERROR if there was not enough room in the output buffer, or + Z_DATA_ERROR if the input data was corrupted, including if the input data is + an incomplete zlib stream. +*/ +int ZEXPORT uncompress2 (dest, destLen, source, sourceLen) + Bytef *dest; + uLongf *destLen; + const Bytef *source; + uLong *sourceLen; +{ + z_stream stream; + int err; + const uInt max = (uInt)-1; + uLong len, left; + Byte buf[1]; /* for detection of incomplete stream when *destLen == 0 */ + + len = *sourceLen; + if (*destLen) { + left = *destLen; + *destLen = 0; + } + else { + left = 1; + dest = buf; + } + + stream.next_in = (z_const Bytef *)source; + stream.avail_in = 0; + stream.zalloc = (alloc_func)0; + stream.zfree = (free_func)0; + stream.opaque = (voidpf)0; + + err = inflateInit(&stream); + if (err != Z_OK) return err; + + stream.next_out = dest; + stream.avail_out = 0; + + do { + if (stream.avail_out == 0) { + stream.avail_out = left > (uLong)max ? max : (uInt)left; + left -= stream.avail_out; + } + if (stream.avail_in == 0) { + stream.avail_in = len > (uLong)max ? max : (uInt)len; + len -= stream.avail_in; + } + err = inflate(&stream, Z_NO_FLUSH); + } while (err == Z_OK); + + *sourceLen -= len + stream.avail_in; + if (dest != buf) + *destLen = stream.total_out; + else if (stream.total_out && err == Z_BUF_ERROR) + left = 1; + + inflateEnd(&stream); + return err == Z_STREAM_END ? Z_OK : + err == Z_NEED_DICT ? Z_DATA_ERROR : + err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR : + err; +} + +int ZEXPORT uncompress (dest, destLen, source, sourceLen) + Bytef *dest; + uLongf *destLen; + const Bytef *source; + uLong sourceLen; +{ + return uncompress2(dest, destLen, source, &sourceLen); +} diff --git a/third_party/java_tools/zlib/zconf.h b/third_party/java_tools/zlib/zconf.h new file mode 100644 index 000000000..5e1d68a00 --- /dev/null +++ b/third_party/java_tools/zlib/zconf.h @@ -0,0 +1,534 @@ +/* zconf.h -- configuration of the zlib compression library + * Copyright (C) 1995-2016 Jean-loup Gailly, Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* @(#) $Id$ */ + +#ifndef ZCONF_H +#define ZCONF_H + +/* + * If you *really* need a unique prefix for all types and library functions, + * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it. + * Even better than compiling with -DZ_PREFIX would be to use configure to set + * this permanently in zconf.h using "./configure --zprefix". + */ +#ifdef Z_PREFIX /* may be set to #if 1 by ./configure */ +# define Z_PREFIX_SET + +/* all linked symbols and init macros */ +# define _dist_code z__dist_code +# define _length_code z__length_code +# define _tr_align z__tr_align +# define _tr_flush_bits z__tr_flush_bits +# define _tr_flush_block z__tr_flush_block +# define _tr_init z__tr_init +# define _tr_stored_block z__tr_stored_block +# define _tr_tally z__tr_tally +# define adler32 z_adler32 +# define adler32_combine z_adler32_combine +# define adler32_combine64 z_adler32_combine64 +# define adler32_z z_adler32_z +# ifndef Z_SOLO +# define compress z_compress +# define compress2 z_compress2 +# define compressBound z_compressBound +# endif +# define crc32 z_crc32 +# define crc32_combine z_crc32_combine +# define crc32_combine64 z_crc32_combine64 +# define crc32_z z_crc32_z +# define deflate z_deflate +# define deflateBound z_deflateBound +# define deflateCopy z_deflateCopy +# define deflateEnd z_deflateEnd +# define deflateGetDictionary z_deflateGetDictionary +# define deflateInit z_deflateInit +# define deflateInit2 z_deflateInit2 +# define deflateInit2_ z_deflateInit2_ +# define deflateInit_ z_deflateInit_ +# define deflateParams z_deflateParams +# define deflatePending z_deflatePending +# define deflatePrime z_deflatePrime +# define deflateReset z_deflateReset +# define deflateResetKeep z_deflateResetKeep +# define deflateSetDictionary z_deflateSetDictionary +# define deflateSetHeader z_deflateSetHeader +# define deflateTune z_deflateTune +# define deflate_copyright z_deflate_copyright +# define get_crc_table z_get_crc_table +# ifndef Z_SOLO +# define gz_error z_gz_error +# define gz_intmax z_gz_intmax +# define gz_strwinerror z_gz_strwinerror +# define gzbuffer z_gzbuffer +# define gzclearerr z_gzclearerr +# define gzclose z_gzclose +# define gzclose_r z_gzclose_r +# define gzclose_w z_gzclose_w +# define gzdirect z_gzdirect +# define gzdopen z_gzdopen +# define gzeof z_gzeof +# define gzerror z_gzerror +# define gzflush z_gzflush +# define gzfread z_gzfread +# define gzfwrite z_gzfwrite +# define gzgetc z_gzgetc +# define gzgetc_ z_gzgetc_ +# define gzgets z_gzgets +# define gzoffset z_gzoffset +# define gzoffset64 z_gzoffset64 +# define gzopen z_gzopen +# define gzopen64 z_gzopen64 +# ifdef _WIN32 +# define gzopen_w z_gzopen_w +# endif +# define gzprintf z_gzprintf +# define gzputc z_gzputc +# define gzputs z_gzputs +# define gzread z_gzread +# define gzrewind z_gzrewind +# define gzseek z_gzseek +# define gzseek64 z_gzseek64 +# define gzsetparams z_gzsetparams +# define gztell z_gztell +# define gztell64 z_gztell64 +# define gzungetc z_gzungetc +# define gzvprintf z_gzvprintf +# define gzwrite z_gzwrite +# endif +# define inflate z_inflate +# define inflateBack z_inflateBack +# define inflateBackEnd z_inflateBackEnd +# define inflateBackInit z_inflateBackInit +# define inflateBackInit_ z_inflateBackInit_ +# define inflateCodesUsed z_inflateCodesUsed +# define inflateCopy z_inflateCopy +# define inflateEnd z_inflateEnd +# define inflateGetDictionary z_inflateGetDictionary +# define inflateGetHeader z_inflateGetHeader +# define inflateInit z_inflateInit +# define inflateInit2 z_inflateInit2 +# define inflateInit2_ z_inflateInit2_ +# define inflateInit_ z_inflateInit_ +# define inflateMark z_inflateMark +# define inflatePrime z_inflatePrime +# define inflateReset z_inflateReset +# define inflateReset2 z_inflateReset2 +# define inflateResetKeep z_inflateResetKeep +# define inflateSetDictionary z_inflateSetDictionary +# define inflateSync z_inflateSync +# define inflateSyncPoint z_inflateSyncPoint +# define inflateUndermine z_inflateUndermine +# define inflateValidate z_inflateValidate +# define inflate_copyright z_inflate_copyright +# define inflate_fast z_inflate_fast +# define inflate_table z_inflate_table +# ifndef Z_SOLO +# define uncompress z_uncompress +# define uncompress2 z_uncompress2 +# endif +# define zError z_zError +# ifndef Z_SOLO +# define zcalloc z_zcalloc +# define zcfree z_zcfree +# endif +# define zlibCompileFlags z_zlibCompileFlags +# define zlibVersion z_zlibVersion + +/* all zlib typedefs in zlib.h and zconf.h */ +# define Byte z_Byte +# define Bytef z_Bytef +# define alloc_func z_alloc_func +# define charf z_charf +# define free_func z_free_func +# ifndef Z_SOLO +# define gzFile z_gzFile +# endif +# define gz_header z_gz_header +# define gz_headerp z_gz_headerp +# define in_func z_in_func +# define intf z_intf +# define out_func z_out_func +# define uInt z_uInt +# define uIntf z_uIntf +# define uLong z_uLong +# define uLongf z_uLongf +# define voidp z_voidp +# define voidpc z_voidpc +# define voidpf z_voidpf + +/* all zlib structs in zlib.h and zconf.h */ +# define gz_header_s z_gz_header_s +# define internal_state z_internal_state + +#endif + +#if defined(__MSDOS__) && !defined(MSDOS) +# define MSDOS +#endif +#if (defined(OS_2) || defined(__OS2__)) && !defined(OS2) +# define OS2 +#endif +#if defined(_WINDOWS) && !defined(WINDOWS) +# define WINDOWS +#endif +#if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__) +# ifndef WIN32 +# define WIN32 +# endif +#endif +#if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32) +# if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__) +# ifndef SYS16BIT +# define SYS16BIT +# endif +# endif +#endif + +/* + * Compile with -DMAXSEG_64K if the alloc function cannot allocate more + * than 64k bytes at a time (needed on systems with 16-bit int). + */ +#ifdef SYS16BIT +# define MAXSEG_64K +#endif +#ifdef MSDOS +# define UNALIGNED_OK +#endif + +#ifdef __STDC_VERSION__ +# ifndef STDC +# define STDC +# endif +# if __STDC_VERSION__ >= 199901L +# ifndef STDC99 +# define STDC99 +# endif +# endif +#endif +#if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus)) +# define STDC +#endif +#if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__)) +# define STDC +#endif +#if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32)) +# define STDC +#endif +#if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__)) +# define STDC +#endif + +#if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */ +# define STDC +#endif + +#ifndef STDC +# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */ +# define const /* note: need a more gentle solution here */ +# endif +#endif + +#if defined(ZLIB_CONST) && !defined(z_const) +# define z_const const +#else +# define z_const +#endif + +#ifdef Z_SOLO + typedef unsigned long z_size_t; +#else +# define z_longlong long long +# if defined(NO_SIZE_T) + typedef unsigned NO_SIZE_T z_size_t; +# elif defined(STDC) +# include + typedef size_t z_size_t; +# else + typedef unsigned long z_size_t; +# endif +# undef z_longlong +#endif + +/* Maximum value for memLevel in deflateInit2 */ +#ifndef MAX_MEM_LEVEL +# ifdef MAXSEG_64K +# define MAX_MEM_LEVEL 8 +# else +# define MAX_MEM_LEVEL 9 +# endif +#endif + +/* Maximum value for windowBits in deflateInit2 and inflateInit2. + * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files + * created by gzip. (Files created by minigzip can still be extracted by + * gzip.) + */ +#ifndef MAX_WBITS +# define MAX_WBITS 15 /* 32K LZ77 window */ +#endif + +/* The memory requirements for deflate are (in bytes): + (1 << (windowBits+2)) + (1 << (memLevel+9)) + that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) + plus a few kilobytes for small objects. For example, if you want to reduce + the default memory requirements from 256K to 128K, compile with + make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" + Of course this will generally degrade compression (there's no free lunch). + + The memory requirements for inflate are (in bytes) 1 << windowBits + that is, 32K for windowBits=15 (default value) plus about 7 kilobytes + for small objects. +*/ + + /* Type declarations */ + +#ifndef OF /* function prototypes */ +# ifdef STDC +# define OF(args) args +# else +# define OF(args) () +# endif +#endif + +#ifndef Z_ARG /* function prototypes for stdarg */ +# if defined(STDC) || defined(Z_HAVE_STDARG_H) +# define Z_ARG(args) args +# else +# define Z_ARG(args) () +# endif +#endif + +/* The following definitions for FAR are needed only for MSDOS mixed + * model programming (small or medium model with some far allocations). + * This was tested only with MSC; for other MSDOS compilers you may have + * to define NO_MEMCPY in zutil.h. If you don't need the mixed model, + * just define FAR to be empty. + */ +#ifdef SYS16BIT +# if defined(M_I86SM) || defined(M_I86MM) + /* MSC small or medium model */ +# define SMALL_MEDIUM +# ifdef _MSC_VER +# define FAR _far +# else +# define FAR far +# endif +# endif +# if (defined(__SMALL__) || defined(__MEDIUM__)) + /* Turbo C small or medium model */ +# define SMALL_MEDIUM +# ifdef __BORLANDC__ +# define FAR _far +# else +# define FAR far +# endif +# endif +#endif + +#if defined(WINDOWS) || defined(WIN32) + /* If building or using zlib as a DLL, define ZLIB_DLL. + * This is not mandatory, but it offers a little performance increase. + */ +# ifdef ZLIB_DLL +# if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500)) +# ifdef ZLIB_INTERNAL +# define ZEXTERN extern __declspec(dllexport) +# else +# define ZEXTERN extern __declspec(dllimport) +# endif +# endif +# endif /* ZLIB_DLL */ + /* If building or using zlib with the WINAPI/WINAPIV calling convention, + * define ZLIB_WINAPI. + * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI. + */ +# ifdef ZLIB_WINAPI +# ifdef FAR +# undef FAR +# endif +# include + /* No need for _export, use ZLIB.DEF instead. */ + /* For complete Windows compatibility, use WINAPI, not __stdcall. */ +# define ZEXPORT WINAPI +# ifdef WIN32 +# define ZEXPORTVA WINAPIV +# else +# define ZEXPORTVA FAR CDECL +# endif +# endif +#endif + +#if defined (__BEOS__) +# ifdef ZLIB_DLL +# ifdef ZLIB_INTERNAL +# define ZEXPORT __declspec(dllexport) +# define ZEXPORTVA __declspec(dllexport) +# else +# define ZEXPORT __declspec(dllimport) +# define ZEXPORTVA __declspec(dllimport) +# endif +# endif +#endif + +#ifndef ZEXTERN +# define ZEXTERN extern +#endif +#ifndef ZEXPORT +# define ZEXPORT +#endif +#ifndef ZEXPORTVA +# define ZEXPORTVA +#endif + +#ifndef FAR +# define FAR +#endif + +#if !defined(__MACTYPES__) +typedef unsigned char Byte; /* 8 bits */ +#endif +typedef unsigned int uInt; /* 16 bits or more */ +typedef unsigned long uLong; /* 32 bits or more */ + +#ifdef SMALL_MEDIUM + /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */ +# define Bytef Byte FAR +#else + typedef Byte FAR Bytef; +#endif +typedef char FAR charf; +typedef int FAR intf; +typedef uInt FAR uIntf; +typedef uLong FAR uLongf; + +#ifdef STDC + typedef void const *voidpc; + typedef void FAR *voidpf; + typedef void *voidp; +#else + typedef Byte const *voidpc; + typedef Byte FAR *voidpf; + typedef Byte *voidp; +#endif + +#if !defined(Z_U4) && !defined(Z_SOLO) && defined(STDC) +# include +# if (UINT_MAX == 0xffffffffUL) +# define Z_U4 unsigned +# elif (ULONG_MAX == 0xffffffffUL) +# define Z_U4 unsigned long +# elif (USHRT_MAX == 0xffffffffUL) +# define Z_U4 unsigned short +# endif +#endif + +#ifdef Z_U4 + typedef Z_U4 z_crc_t; +#else + typedef unsigned long z_crc_t; +#endif + +#ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */ +# define Z_HAVE_UNISTD_H +#endif + +#ifdef HAVE_STDARG_H /* may be set to #if 1 by ./configure */ +# define Z_HAVE_STDARG_H +#endif + +#ifdef STDC +# ifndef Z_SOLO +# include /* for off_t */ +# endif +#endif + +#if defined(STDC) || defined(Z_HAVE_STDARG_H) +# ifndef Z_SOLO +# include /* for va_list */ +# endif +#endif + +#ifdef _WIN32 +# ifndef Z_SOLO +# include /* for wchar_t */ +# endif +#endif + +/* a little trick to accommodate both "#define _LARGEFILE64_SOURCE" and + * "#define _LARGEFILE64_SOURCE 1" as requesting 64-bit operations, (even + * though the former does not conform to the LFS document), but considering + * both "#undef _LARGEFILE64_SOURCE" and "#define _LARGEFILE64_SOURCE 0" as + * equivalently requesting no 64-bit operations + */ +#if defined(_LARGEFILE64_SOURCE) && -_LARGEFILE64_SOURCE - -1 == 1 +# undef _LARGEFILE64_SOURCE +#endif + +#if defined(__WATCOMC__) && !defined(Z_HAVE_UNISTD_H) +# define Z_HAVE_UNISTD_H +#endif +#ifndef Z_SOLO +# if defined(Z_HAVE_UNISTD_H) || defined(_LARGEFILE64_SOURCE) +# include /* for SEEK_*, off_t, and _LFS64_LARGEFILE */ +# ifdef VMS +# include /* for off_t */ +# endif +# ifndef z_off_t +# define z_off_t off_t +# endif +# endif +#endif + +#if defined(_LFS64_LARGEFILE) && _LFS64_LARGEFILE-0 +# define Z_LFS64 +#endif + +#if defined(_LARGEFILE64_SOURCE) && defined(Z_LFS64) +# define Z_LARGE64 +#endif + +#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS-0 == 64 && defined(Z_LFS64) +# define Z_WANT64 +#endif + +#if !defined(SEEK_SET) && !defined(Z_SOLO) +# define SEEK_SET 0 /* Seek from beginning of file. */ +# define SEEK_CUR 1 /* Seek from current position. */ +# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */ +#endif + +#ifndef z_off_t +# define z_off_t long +#endif + +#if !defined(_WIN32) && defined(Z_LARGE64) +# define z_off64_t off64_t +#else +# if defined(_WIN32) && !defined(__GNUC__) && !defined(Z_SOLO) +# define z_off64_t __int64 +# else +# define z_off64_t z_off_t +# endif +#endif + +/* MVS linker does not support external names larger than 8 bytes */ +#if defined(__MVS__) + #pragma map(deflateInit_,"DEIN") + #pragma map(deflateInit2_,"DEIN2") + #pragma map(deflateEnd,"DEEND") + #pragma map(deflateBound,"DEBND") + #pragma map(inflateInit_,"ININ") + #pragma map(inflateInit2_,"ININ2") + #pragma map(inflateEnd,"INEND") + #pragma map(inflateSync,"INSY") + #pragma map(inflateSetDictionary,"INSEDI") + #pragma map(compressBound,"CMBND") + #pragma map(inflate_table,"INTABL") + #pragma map(inflate_fast,"INFA") + #pragma map(inflate_copyright,"INCOPY") +#endif + +#endif /* ZCONF_H */ diff --git a/third_party/java_tools/zlib/zlib.h b/third_party/java_tools/zlib/zlib.h new file mode 100644 index 000000000..f09cdaf1e --- /dev/null +++ b/third_party/java_tools/zlib/zlib.h @@ -0,0 +1,1912 @@ +/* zlib.h -- interface of the 'zlib' general purpose compression library + version 1.2.11, January 15th, 2017 + + Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + Jean-loup Gailly Mark Adler + jloup@gzip.org madler@alumni.caltech.edu + + + The data format used by the zlib library is described by RFCs (Request for + Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950 + (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format). +*/ + +#ifndef ZLIB_H +#define ZLIB_H + +#include "zconf.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define ZLIB_VERSION "1.2.11" +#define ZLIB_VERNUM 0x12b0 +#define ZLIB_VER_MAJOR 1 +#define ZLIB_VER_MINOR 2 +#define ZLIB_VER_REVISION 11 +#define ZLIB_VER_SUBREVISION 0 + +/* + The 'zlib' compression library provides in-memory compression and + decompression functions, including integrity checks of the uncompressed data. + This version of the library supports only one compression method (deflation) + but other algorithms will be added later and will have the same stream + interface. + + Compression can be done in a single step if the buffers are large enough, + or can be done by repeated calls of the compression function. In the latter + case, the application must provide more input and/or consume the output + (providing more output space) before each call. + + The compressed data format used by default by the in-memory functions is + the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped + around a deflate stream, which is itself documented in RFC 1951. + + The library also supports reading and writing files in gzip (.gz) format + with an interface similar to that of stdio using the functions that start + with "gz". The gzip format is different from the zlib format. gzip is a + gzip wrapper, documented in RFC 1952, wrapped around a deflate stream. + + This library can optionally read and write gzip and raw deflate streams in + memory as well. + + The zlib format was designed to be compact and fast for use in memory + and on communications channels. The gzip format was designed for single- + file compression on file systems, has a larger header than zlib to maintain + directory information, and uses a different, slower check method than zlib. + + The library does not install any signal handler. The decoder checks + the consistency of the compressed data, so the library should never crash + even in the case of corrupted input. +*/ + +typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); +typedef void (*free_func) OF((voidpf opaque, voidpf address)); + +struct internal_state; + +typedef struct z_stream_s { + z_const Bytef *next_in; /* next input byte */ + uInt avail_in; /* number of bytes available at next_in */ + uLong total_in; /* total number of input bytes read so far */ + + Bytef *next_out; /* next output byte will go here */ + uInt avail_out; /* remaining free space at next_out */ + uLong total_out; /* total number of bytes output so far */ + + z_const char *msg; /* last error message, NULL if no error */ + struct internal_state FAR *state; /* not visible by applications */ + + alloc_func zalloc; /* used to allocate the internal state */ + free_func zfree; /* used to free the internal state */ + voidpf opaque; /* private data object passed to zalloc and zfree */ + + int data_type; /* best guess about the data type: binary or text + for deflate, or the decoding state for inflate */ + uLong adler; /* Adler-32 or CRC-32 value of the uncompressed data */ + uLong reserved; /* reserved for future use */ +} z_stream; + +typedef z_stream FAR *z_streamp; + +/* + gzip header information passed to and from zlib routines. See RFC 1952 + for more details on the meanings of these fields. +*/ +typedef struct gz_header_s { + int text; /* true if compressed data believed to be text */ + uLong time; /* modification time */ + int xflags; /* extra flags (not used when writing a gzip file) */ + int os; /* operating system */ + Bytef *extra; /* pointer to extra field or Z_NULL if none */ + uInt extra_len; /* extra field length (valid if extra != Z_NULL) */ + uInt extra_max; /* space at extra (only when reading header) */ + Bytef *name; /* pointer to zero-terminated file name or Z_NULL */ + uInt name_max; /* space at name (only when reading header) */ + Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */ + uInt comm_max; /* space at comment (only when reading header) */ + int hcrc; /* true if there was or will be a header crc */ + int done; /* true when done reading gzip header (not used + when writing a gzip file) */ +} gz_header; + +typedef gz_header FAR *gz_headerp; + +/* + The application must update next_in and avail_in when avail_in has dropped + to zero. It must update next_out and avail_out when avail_out has dropped + to zero. The application must initialize zalloc, zfree and opaque before + calling the init function. All other fields are set by the compression + library and must not be updated by the application. + + The opaque value provided by the application will be passed as the first + parameter for calls of zalloc and zfree. This can be useful for custom + memory management. The compression library attaches no meaning to the + opaque value. + + zalloc must return Z_NULL if there is not enough memory for the object. + If zlib is used in a multi-threaded application, zalloc and zfree must be + thread safe. In that case, zlib is thread-safe. When zalloc and zfree are + Z_NULL on entry to the initialization function, they are set to internal + routines that use the standard library functions malloc() and free(). + + On 16-bit systems, the functions zalloc and zfree must be able to allocate + exactly 65536 bytes, but will not be required to allocate more than this if + the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, pointers + returned by zalloc for objects of exactly 65536 bytes *must* have their + offset normalized to zero. The default allocation function provided by this + library ensures this (see zutil.c). To reduce memory requirements and avoid + any allocation of 64K objects, at the expense of compression ratio, compile + the library with -DMAX_WBITS=14 (see zconf.h). + + The fields total_in and total_out can be used for statistics or progress + reports. After compression, total_in holds the total size of the + uncompressed data and may be saved for use by the decompressor (particularly + if the decompressor wants to decompress everything in a single step). +*/ + + /* constants */ + +#define Z_NO_FLUSH 0 +#define Z_PARTIAL_FLUSH 1 +#define Z_SYNC_FLUSH 2 +#define Z_FULL_FLUSH 3 +#define Z_FINISH 4 +#define Z_BLOCK 5 +#define Z_TREES 6 +/* Allowed flush values; see deflate() and inflate() below for details */ + +#define Z_OK 0 +#define Z_STREAM_END 1 +#define Z_NEED_DICT 2 +#define Z_ERRNO (-1) +#define Z_STREAM_ERROR (-2) +#define Z_DATA_ERROR (-3) +#define Z_MEM_ERROR (-4) +#define Z_BUF_ERROR (-5) +#define Z_VERSION_ERROR (-6) +/* Return codes for the compression/decompression functions. Negative values + * are errors, positive values are used for special but normal events. + */ + +#define Z_NO_COMPRESSION 0 +#define Z_BEST_SPEED 1 +#define Z_BEST_COMPRESSION 9 +#define Z_DEFAULT_COMPRESSION (-1) +/* compression levels */ + +#define Z_FILTERED 1 +#define Z_HUFFMAN_ONLY 2 +#define Z_RLE 3 +#define Z_FIXED 4 +#define Z_DEFAULT_STRATEGY 0 +/* compression strategy; see deflateInit2() below for details */ + +#define Z_BINARY 0 +#define Z_TEXT 1 +#define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */ +#define Z_UNKNOWN 2 +/* Possible values of the data_type field for deflate() */ + +#define Z_DEFLATED 8 +/* The deflate compression method (the only one supported in this version) */ + +#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ + +#define zlib_version zlibVersion() +/* for compatibility with versions < 1.0.2 */ + + + /* basic functions */ + +ZEXTERN const char * ZEXPORT zlibVersion OF((void)); +/* The application can compare zlibVersion and ZLIB_VERSION for consistency. + If the first character differs, the library code actually used is not + compatible with the zlib.h header file used by the application. This check + is automatically made by deflateInit and inflateInit. + */ + +/* +ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level)); + + Initializes the internal stream state for compression. The fields + zalloc, zfree and opaque must be initialized before by the caller. If + zalloc and zfree are set to Z_NULL, deflateInit updates them to use default + allocation functions. + + The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: + 1 gives best speed, 9 gives best compression, 0 gives no compression at all + (the input data is simply copied a block at a time). Z_DEFAULT_COMPRESSION + requests a default compromise between speed and compression (currently + equivalent to level 6). + + deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_STREAM_ERROR if level is not a valid compression level, or + Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible + with the version assumed by the caller (ZLIB_VERSION). msg is set to null + if there is no error message. deflateInit does not perform any compression: + this will be done by deflate(). +*/ + + +ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush)); +/* + deflate compresses as much data as possible, and stops when the input + buffer becomes empty or the output buffer becomes full. It may introduce + some output latency (reading input without producing any output) except when + forced to flush. + + The detailed semantics are as follows. deflate performs one or both of the + following actions: + + - Compress more input starting at next_in and update next_in and avail_in + accordingly. If not all input can be processed (because there is not + enough room in the output buffer), next_in and avail_in are updated and + processing will resume at this point for the next call of deflate(). + + - Generate more output starting at next_out and update next_out and avail_out + accordingly. This action is forced if the parameter flush is non zero. + Forcing flush frequently degrades the compression ratio, so this parameter + should be set only when necessary. Some output may be provided even if + flush is zero. + + Before the call of deflate(), the application should ensure that at least + one of the actions is possible, by providing more input and/or consuming more + output, and updating avail_in or avail_out accordingly; avail_out should + never be zero before the call. The application can consume the compressed + output when it wants, for example when the output buffer is full (avail_out + == 0), or after each call of deflate(). If deflate returns Z_OK and with + zero avail_out, it must be called again after making room in the output + buffer because there might be more output pending. See deflatePending(), + which can be used if desired to determine whether or not there is more ouput + in that case. + + Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to + decide how much data to accumulate before producing output, in order to + maximize compression. + + If the parameter flush is set to Z_SYNC_FLUSH, all pending output is + flushed to the output buffer and the output is aligned on a byte boundary, so + that the decompressor can get all input data available so far. (In + particular avail_in is zero after the call if enough output space has been + provided before the call.) Flushing may degrade compression for some + compression algorithms and so it should be used only when necessary. This + completes the current deflate block and follows it with an empty stored block + that is three bits plus filler bits to the next byte, followed by four bytes + (00 00 ff ff). + + If flush is set to Z_PARTIAL_FLUSH, all pending output is flushed to the + output buffer, but the output is not aligned to a byte boundary. All of the + input data so far will be available to the decompressor, as for Z_SYNC_FLUSH. + This completes the current deflate block and follows it with an empty fixed + codes block that is 10 bits long. This assures that enough bytes are output + in order for the decompressor to finish the block before the empty fixed + codes block. + + If flush is set to Z_BLOCK, a deflate block is completed and emitted, as + for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to + seven bits of the current block are held to be written as the next byte after + the next deflate block is completed. In this case, the decompressor may not + be provided enough bits at this point in order to complete decompression of + the data provided so far to the compressor. It may need to wait for the next + block to be emitted. This is for advanced applications that need to control + the emission of deflate blocks. + + If flush is set to Z_FULL_FLUSH, all output is flushed as with + Z_SYNC_FLUSH, and the compression state is reset so that decompression can + restart from this point if previous compressed data has been damaged or if + random access is desired. Using Z_FULL_FLUSH too often can seriously degrade + compression. + + If deflate returns with avail_out == 0, this function must be called again + with the same value of the flush parameter and more output space (updated + avail_out), until the flush is complete (deflate returns with non-zero + avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that + avail_out is greater than six to avoid repeated flush markers due to + avail_out == 0 on return. + + If the parameter flush is set to Z_FINISH, pending input is processed, + pending output is flushed and deflate returns with Z_STREAM_END if there was + enough output space. If deflate returns with Z_OK or Z_BUF_ERROR, this + function must be called again with Z_FINISH and more output space (updated + avail_out) but no more input data, until it returns with Z_STREAM_END or an + error. After deflate has returned Z_STREAM_END, the only possible operations + on the stream are deflateReset or deflateEnd. + + Z_FINISH can be used in the first deflate call after deflateInit if all the + compression is to be done in a single step. In order to complete in one + call, avail_out must be at least the value returned by deflateBound (see + below). Then deflate is guaranteed to return Z_STREAM_END. If not enough + output space is provided, deflate will not return Z_STREAM_END, and it must + be called again as described above. + + deflate() sets strm->adler to the Adler-32 checksum of all input read + so far (that is, total_in bytes). If a gzip stream is being generated, then + strm->adler will be the CRC-32 checksum of the input read so far. (See + deflateInit2 below.) + + deflate() may update strm->data_type if it can make a good guess about + the input data type (Z_BINARY or Z_TEXT). If in doubt, the data is + considered binary. This field is only for information purposes and does not + affect the compression algorithm in any manner. + + deflate() returns Z_OK if some progress has been made (more input + processed or more output produced), Z_STREAM_END if all input has been + consumed and all output has been produced (only when flush is set to + Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example + if next_in or next_out was Z_NULL or the state was inadvertently written over + by the application), or Z_BUF_ERROR if no progress is possible (for example + avail_in or avail_out was zero). Note that Z_BUF_ERROR is not fatal, and + deflate() can be called again with more input and more output space to + continue compressing. +*/ + + +ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm)); +/* + All dynamically allocated data structures for this stream are freed. + This function discards any unprocessed input and does not flush any pending + output. + + deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the + stream state was inconsistent, Z_DATA_ERROR if the stream was freed + prematurely (some input or output was discarded). In the error case, msg + may be set but then points to a static string (which must not be + deallocated). +*/ + + +/* +ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm)); + + Initializes the internal stream state for decompression. The fields + next_in, avail_in, zalloc, zfree and opaque must be initialized before by + the caller. In the current version of inflate, the provided input is not + read or consumed. The allocation of a sliding window will be deferred to + the first call of inflate (if the decompression does not complete on the + first call). If zalloc and zfree are set to Z_NULL, inflateInit updates + them to use default allocation functions. + + inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_VERSION_ERROR if the zlib library version is incompatible with the + version assumed by the caller, or Z_STREAM_ERROR if the parameters are + invalid, such as a null pointer to the structure. msg is set to null if + there is no error message. inflateInit does not perform any decompression. + Actual decompression will be done by inflate(). So next_in, and avail_in, + next_out, and avail_out are unused and unchanged. The current + implementation of inflateInit() does not process any header information -- + that is deferred until inflate() is called. +*/ + + +ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush)); +/* + inflate decompresses as much data as possible, and stops when the input + buffer becomes empty or the output buffer becomes full. It may introduce + some output latency (reading input without producing any output) except when + forced to flush. + + The detailed semantics are as follows. inflate performs one or both of the + following actions: + + - Decompress more input starting at next_in and update next_in and avail_in + accordingly. If not all input can be processed (because there is not + enough room in the output buffer), then next_in and avail_in are updated + accordingly, and processing will resume at this point for the next call of + inflate(). + + - Generate more output starting at next_out and update next_out and avail_out + accordingly. inflate() provides as much output as possible, until there is + no more input data or no more space in the output buffer (see below about + the flush parameter). + + Before the call of inflate(), the application should ensure that at least + one of the actions is possible, by providing more input and/or consuming more + output, and updating the next_* and avail_* values accordingly. If the + caller of inflate() does not provide both available input and available + output space, it is possible that there will be no progress made. The + application can consume the uncompressed output when it wants, for example + when the output buffer is full (avail_out == 0), or after each call of + inflate(). If inflate returns Z_OK and with zero avail_out, it must be + called again after making room in the output buffer because there might be + more output pending. + + The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FINISH, + Z_BLOCK, or Z_TREES. Z_SYNC_FLUSH requests that inflate() flush as much + output as possible to the output buffer. Z_BLOCK requests that inflate() + stop if and when it gets to the next deflate block boundary. When decoding + the zlib or gzip format, this will cause inflate() to return immediately + after the header and before the first block. When doing a raw inflate, + inflate() will go ahead and process the first block, and will return when it + gets to the end of that block, or when it runs out of data. + + The Z_BLOCK option assists in appending to or combining deflate streams. + To assist in this, on return inflate() always sets strm->data_type to the + number of unused bits in the last byte taken from strm->next_in, plus 64 if + inflate() is currently decoding the last block in the deflate stream, plus + 128 if inflate() returned immediately after decoding an end-of-block code or + decoding the complete header up to just before the first byte of the deflate + stream. The end-of-block will not be indicated until all of the uncompressed + data from that block has been written to strm->next_out. The number of + unused bits may in general be greater than seven, except when bit 7 of + data_type is set, in which case the number of unused bits will be less than + eight. data_type is set as noted here every time inflate() returns for all + flush options, and so can be used to determine the amount of currently + consumed input in bits. + + The Z_TREES option behaves as Z_BLOCK does, but it also returns when the + end of each deflate block header is reached, before any actual data in that + block is decoded. This allows the caller to determine the length of the + deflate block header for later use in random access within a deflate block. + 256 is added to the value of strm->data_type when inflate() returns + immediately after reaching the end of the deflate block header. + + inflate() should normally be called until it returns Z_STREAM_END or an + error. However if all decompression is to be performed in a single step (a + single call of inflate), the parameter flush should be set to Z_FINISH. In + this case all pending input is processed and all pending output is flushed; + avail_out must be large enough to hold all of the uncompressed data for the + operation to complete. (The size of the uncompressed data may have been + saved by the compressor for this purpose.) The use of Z_FINISH is not + required to perform an inflation in one step. However it may be used to + inform inflate that a faster approach can be used for the single inflate() + call. Z_FINISH also informs inflate to not maintain a sliding window if the + stream completes, which reduces inflate's memory footprint. If the stream + does not complete, either because not all of the stream is provided or not + enough output space is provided, then a sliding window will be allocated and + inflate() can be called again to continue the operation as if Z_NO_FLUSH had + been used. + + In this implementation, inflate() always flushes as much output as + possible to the output buffer, and always uses the faster approach on the + first call. So the effects of the flush parameter in this implementation are + on the return value of inflate() as noted below, when inflate() returns early + when Z_BLOCK or Z_TREES is used, and when inflate() avoids the allocation of + memory for a sliding window when Z_FINISH is used. + + If a preset dictionary is needed after this call (see inflateSetDictionary + below), inflate sets strm->adler to the Adler-32 checksum of the dictionary + chosen by the compressor and returns Z_NEED_DICT; otherwise it sets + strm->adler to the Adler-32 checksum of all output produced so far (that is, + total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described + below. At the end of the stream, inflate() checks that its computed Adler-32 + checksum is equal to that saved by the compressor and returns Z_STREAM_END + only if the checksum is correct. + + inflate() can decompress and check either zlib-wrapped or gzip-wrapped + deflate data. The header type is detected automatically, if requested when + initializing with inflateInit2(). Any information contained in the gzip + header is not retained unless inflateGetHeader() is used. When processing + gzip-wrapped deflate data, strm->adler32 is set to the CRC-32 of the output + produced so far. The CRC-32 is checked against the gzip trailer, as is the + uncompressed length, modulo 2^32. + + inflate() returns Z_OK if some progress has been made (more input processed + or more output produced), Z_STREAM_END if the end of the compressed data has + been reached and all uncompressed output has been produced, Z_NEED_DICT if a + preset dictionary is needed at this point, Z_DATA_ERROR if the input data was + corrupted (input stream not conforming to the zlib format or incorrect check + value, in which case strm->msg points to a string with a more specific + error), Z_STREAM_ERROR if the stream structure was inconsistent (for example + next_in or next_out was Z_NULL, or the state was inadvertently written over + by the application), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR + if no progress was possible or if there was not enough room in the output + buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and + inflate() can be called again with more input and more output space to + continue decompressing. If Z_DATA_ERROR is returned, the application may + then call inflateSync() to look for a good compression block if a partial + recovery of the data is to be attempted. +*/ + + +ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm)); +/* + All dynamically allocated data structures for this stream are freed. + This function discards any unprocessed input and does not flush any pending + output. + + inflateEnd returns Z_OK if success, or Z_STREAM_ERROR if the stream state + was inconsistent. +*/ + + + /* Advanced functions */ + +/* + The following functions are needed only in some special applications. +*/ + +/* +ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm, + int level, + int method, + int windowBits, + int memLevel, + int strategy)); + + This is another version of deflateInit with more compression options. The + fields next_in, zalloc, zfree and opaque must be initialized before by the + caller. + + The method parameter is the compression method. It must be Z_DEFLATED in + this version of the library. + + The windowBits parameter is the base two logarithm of the window size + (the size of the history buffer). It should be in the range 8..15 for this + version of the library. Larger values of this parameter result in better + compression at the expense of memory usage. The default value is 15 if + deflateInit is used instead. + + For the current implementation of deflate(), a windowBits value of 8 (a + window size of 256 bytes) is not supported. As a result, a request for 8 + will result in 9 (a 512-byte window). In that case, providing 8 to + inflateInit2() will result in an error when the zlib header with 9 is + checked against the initialization of inflate(). The remedy is to not use 8 + with deflateInit2() with this initialization, or at least in that case use 9 + with inflateInit2(). + + windowBits can also be -8..-15 for raw deflate. In this case, -windowBits + determines the window size. deflate() will then generate raw deflate data + with no zlib header or trailer, and will not compute a check value. + + windowBits can also be greater than 15 for optional gzip encoding. Add + 16 to windowBits to write a simple gzip header and trailer around the + compressed data instead of a zlib wrapper. The gzip header will have no + file name, no extra data, no comment, no modification time (set to zero), no + header crc, and the operating system will be set to the appropriate value, + if the operating system was determined at compile time. If a gzip stream is + being written, strm->adler is a CRC-32 instead of an Adler-32. + + For raw deflate or gzip encoding, a request for a 256-byte window is + rejected as invalid, since only the zlib header provides a means of + transmitting the window size to the decompressor. + + The memLevel parameter specifies how much memory should be allocated + for the internal compression state. memLevel=1 uses minimum memory but is + slow and reduces compression ratio; memLevel=9 uses maximum memory for + optimal speed. The default value is 8. See zconf.h for total memory usage + as a function of windowBits and memLevel. + + The strategy parameter is used to tune the compression algorithm. Use the + value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a + filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no + string match), or Z_RLE to limit match distances to one (run-length + encoding). Filtered data consists mostly of small values with a somewhat + random distribution. In this case, the compression algorithm is tuned to + compress them better. The effect of Z_FILTERED is to force more Huffman + coding and less string matching; it is somewhat intermediate between + Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as + fast as Z_HUFFMAN_ONLY, but give better compression for PNG image data. The + strategy parameter only affects the compression ratio but not the + correctness of the compressed output even if it is not set appropriately. + Z_FIXED prevents the use of dynamic Huffman codes, allowing for a simpler + decoder for special applications. + + deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_STREAM_ERROR if any parameter is invalid (such as an invalid + method), or Z_VERSION_ERROR if the zlib library version (zlib_version) is + incompatible with the version assumed by the caller (ZLIB_VERSION). msg is + set to null if there is no error message. deflateInit2 does not perform any + compression: this will be done by deflate(). +*/ + +ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm, + const Bytef *dictionary, + uInt dictLength)); +/* + Initializes the compression dictionary from the given byte sequence + without producing any compressed output. When using the zlib format, this + function must be called immediately after deflateInit, deflateInit2 or + deflateReset, and before any call of deflate. When doing raw deflate, this + function must be called either before any call of deflate, or immediately + after the completion of a deflate block, i.e. after all input has been + consumed and all output has been delivered when using any of the flush + options Z_BLOCK, Z_PARTIAL_FLUSH, Z_SYNC_FLUSH, or Z_FULL_FLUSH. The + compressor and decompressor must use exactly the same dictionary (see + inflateSetDictionary). + + The dictionary should consist of strings (byte sequences) that are likely + to be encountered later in the data to be compressed, with the most commonly + used strings preferably put towards the end of the dictionary. Using a + dictionary is most useful when the data to be compressed is short and can be + predicted with good accuracy; the data can then be compressed better than + with the default empty dictionary. + + Depending on the size of the compression data structures selected by + deflateInit or deflateInit2, a part of the dictionary may in effect be + discarded, for example if the dictionary is larger than the window size + provided in deflateInit or deflateInit2. Thus the strings most likely to be + useful should be put at the end of the dictionary, not at the front. In + addition, the current implementation of deflate will use at most the window + size minus 262 bytes of the provided dictionary. + + Upon return of this function, strm->adler is set to the Adler-32 value + of the dictionary; the decompressor may later use this value to determine + which dictionary has been used by the compressor. (The Adler-32 value + applies to the whole dictionary even if only a subset of the dictionary is + actually used by the compressor.) If a raw deflate was requested, then the + Adler-32 value is not computed and strm->adler is not set. + + deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a + parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is + inconsistent (for example if deflate has already been called for this stream + or if not at a block boundary for raw deflate). deflateSetDictionary does + not perform any compression: this will be done by deflate(). +*/ + +ZEXTERN int ZEXPORT deflateGetDictionary OF((z_streamp strm, + Bytef *dictionary, + uInt *dictLength)); +/* + Returns the sliding dictionary being maintained by deflate. dictLength is + set to the number of bytes in the dictionary, and that many bytes are copied + to dictionary. dictionary must have enough space, where 32768 bytes is + always enough. If deflateGetDictionary() is called with dictionary equal to + Z_NULL, then only the dictionary length is returned, and nothing is copied. + Similary, if dictLength is Z_NULL, then it is not set. + + deflateGetDictionary() may return a length less than the window size, even + when more than the window size in input has been provided. It may return up + to 258 bytes less in that case, due to how zlib's implementation of deflate + manages the sliding window and lookahead for matches, where matches can be + up to 258 bytes long. If the application needs the last window-size bytes of + input, then that would need to be saved by the application outside of zlib. + + deflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the + stream state is inconsistent. +*/ + +ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest, + z_streamp source)); +/* + Sets the destination stream as a complete copy of the source stream. + + This function can be useful when several compression strategies will be + tried, for example when there are several ways of pre-processing the input + data with a filter. The streams that will be discarded should then be freed + by calling deflateEnd. Note that deflateCopy duplicates the internal + compression state which can be quite large, so this strategy is slow and can + consume lots of memory. + + deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_STREAM_ERROR if the source stream state was inconsistent + (such as zalloc being Z_NULL). msg is left unchanged in both source and + destination. +*/ + +ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm)); +/* + This function is equivalent to deflateEnd followed by deflateInit, but + does not free and reallocate the internal compression state. The stream + will leave the compression level and any other attributes that may have been + set unchanged. + + deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being Z_NULL). +*/ + +ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm, + int level, + int strategy)); +/* + Dynamically update the compression level and compression strategy. The + interpretation of level and strategy is as in deflateInit2(). This can be + used to switch between compression and straight copy of the input data, or + to switch to a different kind of input data requiring a different strategy. + If the compression approach (which is a function of the level) or the + strategy is changed, and if any input has been consumed in a previous + deflate() call, then the input available so far is compressed with the old + level and strategy using deflate(strm, Z_BLOCK). There are three approaches + for the compression levels 0, 1..3, and 4..9 respectively. The new level + and strategy will take effect at the next call of deflate(). + + If a deflate(strm, Z_BLOCK) is performed by deflateParams(), and it does + not have enough output space to complete, then the parameter change will not + take effect. In this case, deflateParams() can be called again with the + same parameters and more output space to try again. + + In order to assure a change in the parameters on the first try, the + deflate stream should be flushed using deflate() with Z_BLOCK or other flush + request until strm.avail_out is not zero, before calling deflateParams(). + Then no more input data should be provided before the deflateParams() call. + If this is done, the old level and strategy will be applied to the data + compressed before deflateParams(), and the new level and strategy will be + applied to the the data compressed after deflateParams(). + + deflateParams returns Z_OK on success, Z_STREAM_ERROR if the source stream + state was inconsistent or if a parameter was invalid, or Z_BUF_ERROR if + there was not enough output space to complete the compression of the + available input data before a change in the strategy or approach. Note that + in the case of a Z_BUF_ERROR, the parameters are not changed. A return + value of Z_BUF_ERROR is not fatal, in which case deflateParams() can be + retried with more output space. +*/ + +ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm, + int good_length, + int max_lazy, + int nice_length, + int max_chain)); +/* + Fine tune deflate's internal compression parameters. This should only be + used by someone who understands the algorithm used by zlib's deflate for + searching for the best matching string, and even then only by the most + fanatic optimizer trying to squeeze out the last compressed bit for their + specific input data. Read the deflate.c source code for the meaning of the + max_lazy, good_length, nice_length, and max_chain parameters. + + deflateTune() can be called after deflateInit() or deflateInit2(), and + returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream. + */ + +ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm, + uLong sourceLen)); +/* + deflateBound() returns an upper bound on the compressed size after + deflation of sourceLen bytes. It must be called after deflateInit() or + deflateInit2(), and after deflateSetHeader(), if used. This would be used + to allocate an output buffer for deflation in a single pass, and so would be + called before deflate(). If that first deflate() call is provided the + sourceLen input bytes, an output buffer allocated to the size returned by + deflateBound(), and the flush value Z_FINISH, then deflate() is guaranteed + to return Z_STREAM_END. Note that it is possible for the compressed size to + be larger than the value returned by deflateBound() if flush options other + than Z_FINISH or Z_NO_FLUSH are used. +*/ + +ZEXTERN int ZEXPORT deflatePending OF((z_streamp strm, + unsigned *pending, + int *bits)); +/* + deflatePending() returns the number of bytes and bits of output that have + been generated, but not yet provided in the available output. The bytes not + provided would be due to the available output space having being consumed. + The number of bits of output not provided are between 0 and 7, where they + await more bits to join them in order to fill out a full byte. If pending + or bits are Z_NULL, then those values are not set. + + deflatePending returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. + */ + +ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm, + int bits, + int value)); +/* + deflatePrime() inserts bits in the deflate output stream. The intent + is that this function is used to start off the deflate output with the bits + leftover from a previous deflate stream when appending to it. As such, this + function can only be used for raw deflate, and must be used before the first + deflate() call after a deflateInit2() or deflateReset(). bits must be less + than or equal to 16, and that many of the least significant bits of value + will be inserted in the output. + + deflatePrime returns Z_OK if success, Z_BUF_ERROR if there was not enough + room in the internal buffer to insert the bits, or Z_STREAM_ERROR if the + source stream state was inconsistent. +*/ + +ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm, + gz_headerp head)); +/* + deflateSetHeader() provides gzip header information for when a gzip + stream is requested by deflateInit2(). deflateSetHeader() may be called + after deflateInit2() or deflateReset() and before the first call of + deflate(). The text, time, os, extra field, name, and comment information + in the provided gz_header structure are written to the gzip header (xflag is + ignored -- the extra flags are set according to the compression level). The + caller must assure that, if not Z_NULL, name and comment are terminated with + a zero byte, and that if extra is not Z_NULL, that extra_len bytes are + available there. If hcrc is true, a gzip header crc is included. Note that + the current versions of the command-line version of gzip (up through version + 1.3.x) do not support header crc's, and will report that it is a "multi-part + gzip file" and give up. + + If deflateSetHeader is not used, the default gzip header has text false, + the time set to zero, and os set to 255, with no extra, name, or comment + fields. The gzip header is returned to the default state by deflateReset(). + + deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +/* +ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, + int windowBits)); + + This is another version of inflateInit with an extra parameter. The + fields next_in, avail_in, zalloc, zfree and opaque must be initialized + before by the caller. + + The windowBits parameter is the base two logarithm of the maximum window + size (the size of the history buffer). It should be in the range 8..15 for + this version of the library. The default value is 15 if inflateInit is used + instead. windowBits must be greater than or equal to the windowBits value + provided to deflateInit2() while compressing, or it must be equal to 15 if + deflateInit2() was not used. If a compressed stream with a larger window + size is given as input, inflate() will return with the error code + Z_DATA_ERROR instead of trying to allocate a larger window. + + windowBits can also be zero to request that inflate use the window size in + the zlib header of the compressed stream. + + windowBits can also be -8..-15 for raw inflate. In this case, -windowBits + determines the window size. inflate() will then process raw deflate data, + not looking for a zlib or gzip header, not generating a check value, and not + looking for any check values for comparison at the end of the stream. This + is for use with other formats that use the deflate compressed data format + such as zip. Those formats provide their own check values. If a custom + format is developed using the raw deflate format for compressed data, it is + recommended that a check value such as an Adler-32 or a CRC-32 be applied to + the uncompressed data as is done in the zlib, gzip, and zip formats. For + most applications, the zlib format should be used as is. Note that comments + above on the use in deflateInit2() applies to the magnitude of windowBits. + + windowBits can also be greater than 15 for optional gzip decoding. Add + 32 to windowBits to enable zlib and gzip decoding with automatic header + detection, or add 16 to decode only the gzip format (the zlib format will + return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a + CRC-32 instead of an Adler-32. Unlike the gunzip utility and gzread() (see + below), inflate() will not automatically decode concatenated gzip streams. + inflate() will return Z_STREAM_END at the end of the gzip stream. The state + would need to be reset to continue decoding a subsequent gzip stream. + + inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_VERSION_ERROR if the zlib library version is incompatible with the + version assumed by the caller, or Z_STREAM_ERROR if the parameters are + invalid, such as a null pointer to the structure. msg is set to null if + there is no error message. inflateInit2 does not perform any decompression + apart from possibly reading the zlib header if present: actual decompression + will be done by inflate(). (So next_in and avail_in may be modified, but + next_out and avail_out are unused and unchanged.) The current implementation + of inflateInit2() does not process any header information -- that is + deferred until inflate() is called. +*/ + +ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm, + const Bytef *dictionary, + uInt dictLength)); +/* + Initializes the decompression dictionary from the given uncompressed byte + sequence. This function must be called immediately after a call of inflate, + if that call returned Z_NEED_DICT. The dictionary chosen by the compressor + can be determined from the Adler-32 value returned by that call of inflate. + The compressor and decompressor must use exactly the same dictionary (see + deflateSetDictionary). For raw inflate, this function can be called at any + time to set the dictionary. If the provided dictionary is smaller than the + window and there is already data in the window, then the provided dictionary + will amend what's there. The application must insure that the dictionary + that was used for compression is provided. + + inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a + parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is + inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the + expected one (incorrect Adler-32 value). inflateSetDictionary does not + perform any decompression: this will be done by subsequent calls of + inflate(). +*/ + +ZEXTERN int ZEXPORT inflateGetDictionary OF((z_streamp strm, + Bytef *dictionary, + uInt *dictLength)); +/* + Returns the sliding dictionary being maintained by inflate. dictLength is + set to the number of bytes in the dictionary, and that many bytes are copied + to dictionary. dictionary must have enough space, where 32768 bytes is + always enough. If inflateGetDictionary() is called with dictionary equal to + Z_NULL, then only the dictionary length is returned, and nothing is copied. + Similary, if dictLength is Z_NULL, then it is not set. + + inflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the + stream state is inconsistent. +*/ + +ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm)); +/* + Skips invalid compressed data until a possible full flush point (see above + for the description of deflate with Z_FULL_FLUSH) can be found, or until all + available input is skipped. No output is provided. + + inflateSync searches for a 00 00 FF FF pattern in the compressed data. + All full flush points have this pattern, but not all occurrences of this + pattern are full flush points. + + inflateSync returns Z_OK if a possible full flush point has been found, + Z_BUF_ERROR if no more input was provided, Z_DATA_ERROR if no flush point + has been found, or Z_STREAM_ERROR if the stream structure was inconsistent. + In the success case, the application may save the current current value of + total_in which indicates where valid compressed data was found. In the + error case, the application may repeatedly call inflateSync, providing more + input each time, until success or end of the input data. +*/ + +ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest, + z_streamp source)); +/* + Sets the destination stream as a complete copy of the source stream. + + This function can be useful when randomly accessing a large stream. The + first pass through the stream can periodically record the inflate state, + allowing restarting inflate at those points when randomly accessing the + stream. + + inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_STREAM_ERROR if the source stream state was inconsistent + (such as zalloc being Z_NULL). msg is left unchanged in both source and + destination. +*/ + +ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm)); +/* + This function is equivalent to inflateEnd followed by inflateInit, + but does not free and reallocate the internal decompression state. The + stream will keep attributes that may have been set by inflateInit2. + + inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being Z_NULL). +*/ + +ZEXTERN int ZEXPORT inflateReset2 OF((z_streamp strm, + int windowBits)); +/* + This function is the same as inflateReset, but it also permits changing + the wrap and window size requests. The windowBits parameter is interpreted + the same as it is for inflateInit2. If the window size is changed, then the + memory allocated for the window is freed, and the window will be reallocated + by inflate() if needed. + + inflateReset2 returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being Z_NULL), or if + the windowBits parameter is invalid. +*/ + +ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm, + int bits, + int value)); +/* + This function inserts bits in the inflate input stream. The intent is + that this function is used to start inflating at a bit position in the + middle of a byte. The provided bits will be used before any bytes are used + from next_in. This function should only be used with raw inflate, and + should be used before the first inflate() call after inflateInit2() or + inflateReset(). bits must be less than or equal to 16, and that many of the + least significant bits of value will be inserted in the input. + + If bits is negative, then the input stream bit buffer is emptied. Then + inflatePrime() can be called again to put bits in the buffer. This is used + to clear out bits leftover after feeding inflate a block description prior + to feeding inflate codes. + + inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +ZEXTERN long ZEXPORT inflateMark OF((z_streamp strm)); +/* + This function returns two values, one in the lower 16 bits of the return + value, and the other in the remaining upper bits, obtained by shifting the + return value down 16 bits. If the upper value is -1 and the lower value is + zero, then inflate() is currently decoding information outside of a block. + If the upper value is -1 and the lower value is non-zero, then inflate is in + the middle of a stored block, with the lower value equaling the number of + bytes from the input remaining to copy. If the upper value is not -1, then + it is the number of bits back from the current bit position in the input of + the code (literal or length/distance pair) currently being processed. In + that case the lower value is the number of bytes already emitted for that + code. + + A code is being processed if inflate is waiting for more input to complete + decoding of the code, or if it has completed decoding but is waiting for + more output space to write the literal or match data. + + inflateMark() is used to mark locations in the input data for random + access, which may be at bit positions, and to note those cases where the + output of a code may span boundaries of random access blocks. The current + location in the input stream can be determined from avail_in and data_type + as noted in the description for the Z_BLOCK flush parameter for inflate. + + inflateMark returns the value noted above, or -65536 if the provided + source stream state was inconsistent. +*/ + +ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm, + gz_headerp head)); +/* + inflateGetHeader() requests that gzip header information be stored in the + provided gz_header structure. inflateGetHeader() may be called after + inflateInit2() or inflateReset(), and before the first call of inflate(). + As inflate() processes the gzip stream, head->done is zero until the header + is completed, at which time head->done is set to one. If a zlib stream is + being decoded, then head->done is set to -1 to indicate that there will be + no gzip header information forthcoming. Note that Z_BLOCK or Z_TREES can be + used to force inflate() to return immediately after header processing is + complete and before any actual data is decompressed. + + The text, time, xflags, and os fields are filled in with the gzip header + contents. hcrc is set to true if there is a header CRC. (The header CRC + was valid if done is set to one.) If extra is not Z_NULL, then extra_max + contains the maximum number of bytes to write to extra. Once done is true, + extra_len contains the actual extra field length, and extra contains the + extra field, or that field truncated if extra_max is less than extra_len. + If name is not Z_NULL, then up to name_max characters are written there, + terminated with a zero unless the length is greater than name_max. If + comment is not Z_NULL, then up to comm_max characters are written there, + terminated with a zero unless the length is greater than comm_max. When any + of extra, name, or comment are not Z_NULL and the respective field is not + present in the header, then that field is set to Z_NULL to signal its + absence. This allows the use of deflateSetHeader() with the returned + structure to duplicate the header. However if those fields are set to + allocated memory, then the application will need to save those pointers + elsewhere so that they can be eventually freed. + + If inflateGetHeader is not used, then the header information is simply + discarded. The header is always checked for validity, including the header + CRC if present. inflateReset() will reset the process to discard the header + information. The application would need to call inflateGetHeader() again to + retrieve the header from the next gzip stream. + + inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +/* +ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits, + unsigned char FAR *window)); + + Initialize the internal stream state for decompression using inflateBack() + calls. The fields zalloc, zfree and opaque in strm must be initialized + before the call. If zalloc and zfree are Z_NULL, then the default library- + derived memory allocation routines are used. windowBits is the base two + logarithm of the window size, in the range 8..15. window is a caller + supplied buffer of that size. Except for special applications where it is + assured that deflate was used with small window sizes, windowBits must be 15 + and a 32K byte window must be supplied to be able to decompress general + deflate streams. + + See inflateBack() for the usage of these routines. + + inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of + the parameters are invalid, Z_MEM_ERROR if the internal state could not be + allocated, or Z_VERSION_ERROR if the version of the library does not match + the version of the header file. +*/ + +typedef unsigned (*in_func) OF((void FAR *, + z_const unsigned char FAR * FAR *)); +typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned)); + +ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm, + in_func in, void FAR *in_desc, + out_func out, void FAR *out_desc)); +/* + inflateBack() does a raw inflate with a single call using a call-back + interface for input and output. This is potentially more efficient than + inflate() for file i/o applications, in that it avoids copying between the + output and the sliding window by simply making the window itself the output + buffer. inflate() can be faster on modern CPUs when used with large + buffers. inflateBack() trusts the application to not change the output + buffer passed by the output function, at least until inflateBack() returns. + + inflateBackInit() must be called first to allocate the internal state + and to initialize the state with the user-provided window buffer. + inflateBack() may then be used multiple times to inflate a complete, raw + deflate stream with each call. inflateBackEnd() is then called to free the + allocated state. + + A raw deflate stream is one with no zlib or gzip header or trailer. + This routine would normally be used in a utility that reads zip or gzip + files and writes out uncompressed files. The utility would decode the + header and process the trailer on its own, hence this routine expects only + the raw deflate stream to decompress. This is different from the default + behavior of inflate(), which expects a zlib header and trailer around the + deflate stream. + + inflateBack() uses two subroutines supplied by the caller that are then + called by inflateBack() for input and output. inflateBack() calls those + routines until it reads a complete deflate stream and writes out all of the + uncompressed data, or until it encounters an error. The function's + parameters and return types are defined above in the in_func and out_func + typedefs. inflateBack() will call in(in_desc, &buf) which should return the + number of bytes of provided input, and a pointer to that input in buf. If + there is no input available, in() must return zero -- buf is ignored in that + case -- and inflateBack() will return a buffer error. inflateBack() will + call out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. + out() should return zero on success, or non-zero on failure. If out() + returns non-zero, inflateBack() will return with an error. Neither in() nor + out() are permitted to change the contents of the window provided to + inflateBackInit(), which is also the buffer that out() uses to write from. + The length written by out() will be at most the window size. Any non-zero + amount of input may be provided by in(). + + For convenience, inflateBack() can be provided input on the first call by + setting strm->next_in and strm->avail_in. If that input is exhausted, then + in() will be called. Therefore strm->next_in must be initialized before + calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called + immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in + must also be initialized, and then if strm->avail_in is not zero, input will + initially be taken from strm->next_in[0 .. strm->avail_in - 1]. + + The in_desc and out_desc parameters of inflateBack() is passed as the + first parameter of in() and out() respectively when they are called. These + descriptors can be optionally used to pass any information that the caller- + supplied in() and out() functions need to do their job. + + On return, inflateBack() will set strm->next_in and strm->avail_in to + pass back any unused input that was provided by the last in() call. The + return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR + if in() or out() returned an error, Z_DATA_ERROR if there was a format error + in the deflate stream (in which case strm->msg is set to indicate the nature + of the error), or Z_STREAM_ERROR if the stream was not properly initialized. + In the case of Z_BUF_ERROR, an input or output error can be distinguished + using strm->next_in which will be Z_NULL only if in() returned an error. If + strm->next_in is not Z_NULL, then the Z_BUF_ERROR was due to out() returning + non-zero. (in() will always be called before out(), so strm->next_in is + assured to be defined if out() returns non-zero.) Note that inflateBack() + cannot return Z_OK. +*/ + +ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm)); +/* + All memory allocated by inflateBackInit() is freed. + + inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream + state was inconsistent. +*/ + +ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void)); +/* Return flags indicating compile-time options. + + Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other: + 1.0: size of uInt + 3.2: size of uLong + 5.4: size of voidpf (pointer) + 7.6: size of z_off_t + + Compiler, assembler, and debug options: + 8: ZLIB_DEBUG + 9: ASMV or ASMINF -- use ASM code + 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention + 11: 0 (reserved) + + One-time table building (smaller code, but not thread-safe if true): + 12: BUILDFIXED -- build static block decoding tables when needed + 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed + 14,15: 0 (reserved) + + Library content (indicates missing functionality): + 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking + deflate code when not needed) + 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect + and decode gzip streams (to avoid linking crc code) + 18-19: 0 (reserved) + + Operation variations (changes in library functionality): + 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate + 21: FASTEST -- deflate algorithm with only one, lowest compression level + 22,23: 0 (reserved) + + The sprintf variant used by gzprintf (zero is best): + 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format + 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure! + 26: 0 = returns value, 1 = void -- 1 means inferred string length returned + + Remainder: + 27-31: 0 (reserved) + */ + +#ifndef Z_SOLO + + /* utility functions */ + +/* + The following utility functions are implemented on top of the basic + stream-oriented functions. To simplify the interface, some default options + are assumed (compression level and memory usage, standard memory allocation + functions). The source code of these utility functions can be modified if + you need special options. +*/ + +ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen)); +/* + Compresses the source buffer into the destination buffer. sourceLen is + the byte length of the source buffer. Upon entry, destLen is the total size + of the destination buffer, which must be at least the value returned by + compressBound(sourceLen). Upon exit, destLen is the actual size of the + compressed data. compress() is equivalent to compress2() with a level + parameter of Z_DEFAULT_COMPRESSION. + + compress returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_BUF_ERROR if there was not enough room in the output + buffer. +*/ + +ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen, + int level)); +/* + Compresses the source buffer into the destination buffer. The level + parameter has the same meaning as in deflateInit. sourceLen is the byte + length of the source buffer. Upon entry, destLen is the total size of the + destination buffer, which must be at least the value returned by + compressBound(sourceLen). Upon exit, destLen is the actual size of the + compressed data. + + compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_BUF_ERROR if there was not enough room in the output buffer, + Z_STREAM_ERROR if the level parameter is invalid. +*/ + +ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen)); +/* + compressBound() returns an upper bound on the compressed size after + compress() or compress2() on sourceLen bytes. It would be used before a + compress() or compress2() call to allocate the destination buffer. +*/ + +ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen)); +/* + Decompresses the source buffer into the destination buffer. sourceLen is + the byte length of the source buffer. Upon entry, destLen is the total size + of the destination buffer, which must be large enough to hold the entire + uncompressed data. (The size of the uncompressed data must have been saved + previously by the compressor and transmitted to the decompressor by some + mechanism outside the scope of this compression library.) Upon exit, destLen + is the actual size of the uncompressed data. + + uncompress returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_BUF_ERROR if there was not enough room in the output + buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. In + the case where there is not enough room, uncompress() will fill the output + buffer with the uncompressed data up to that point. +*/ + +ZEXTERN int ZEXPORT uncompress2 OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong *sourceLen)); +/* + Same as uncompress, except that sourceLen is a pointer, where the + length of the source is *sourceLen. On return, *sourceLen is the number of + source bytes consumed. +*/ + + /* gzip file access functions */ + +/* + This library supports reading and writing files in gzip (.gz) format with + an interface similar to that of stdio, using the functions that start with + "gz". The gzip format is different from the zlib format. gzip is a gzip + wrapper, documented in RFC 1952, wrapped around a deflate stream. +*/ + +typedef struct gzFile_s *gzFile; /* semi-opaque gzip file descriptor */ + +/* +ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode)); + + Opens a gzip (.gz) file for reading or writing. The mode parameter is as + in fopen ("rb" or "wb") but can also include a compression level ("wb9") or + a strategy: 'f' for filtered data as in "wb6f", 'h' for Huffman-only + compression as in "wb1h", 'R' for run-length encoding as in "wb1R", or 'F' + for fixed code compression as in "wb9F". (See the description of + deflateInit2 for more information about the strategy parameter.) 'T' will + request transparent writing or appending with no compression and not using + the gzip format. + + "a" can be used instead of "w" to request that the gzip stream that will + be written be appended to the file. "+" will result in an error, since + reading and writing to the same gzip file is not supported. The addition of + "x" when writing will create the file exclusively, which fails if the file + already exists. On systems that support it, the addition of "e" when + reading or writing will set the flag to close the file on an execve() call. + + These functions, as well as gzip, will read and decode a sequence of gzip + streams in a file. The append function of gzopen() can be used to create + such a file. (Also see gzflush() for another way to do this.) When + appending, gzopen does not test whether the file begins with a gzip stream, + nor does it look for the end of the gzip streams to begin appending. gzopen + will simply append a gzip stream to the existing file. + + gzopen can be used to read a file which is not in gzip format; in this + case gzread will directly read from the file without decompression. When + reading, this will be detected automatically by looking for the magic two- + byte gzip header. + + gzopen returns NULL if the file could not be opened, if there was + insufficient memory to allocate the gzFile state, or if an invalid mode was + specified (an 'r', 'w', or 'a' was not provided, or '+' was provided). + errno can be checked to determine if the reason gzopen failed was that the + file could not be opened. +*/ + +ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode)); +/* + gzdopen associates a gzFile with the file descriptor fd. File descriptors + are obtained from calls like open, dup, creat, pipe or fileno (if the file + has been previously opened with fopen). The mode parameter is as in gzopen. + + The next call of gzclose on the returned gzFile will also close the file + descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor + fd. If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd, + mode);. The duplicated descriptor should be saved to avoid a leak, since + gzdopen does not close fd if it fails. If you are using fileno() to get the + file descriptor from a FILE *, then you will have to use dup() to avoid + double-close()ing the file descriptor. Both gzclose() and fclose() will + close the associated file descriptor, so they need to have different file + descriptors. + + gzdopen returns NULL if there was insufficient memory to allocate the + gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not + provided, or '+' was provided), or if fd is -1. The file descriptor is not + used until the next gz* read, write, seek, or close operation, so gzdopen + will not detect if fd is invalid (unless fd is -1). +*/ + +ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size)); +/* + Set the internal buffer size used by this library's functions. The + default buffer size is 8192 bytes. This function must be called after + gzopen() or gzdopen(), and before any other calls that read or write the + file. The buffer memory allocation is always deferred to the first read or + write. Three times that size in buffer space is allocated. A larger buffer + size of, for example, 64K or 128K bytes will noticeably increase the speed + of decompression (reading). + + The new buffer size also affects the maximum length for gzprintf(). + + gzbuffer() returns 0 on success, or -1 on failure, such as being called + too late. +*/ + +ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy)); +/* + Dynamically update the compression level or strategy. See the description + of deflateInit2 for the meaning of these parameters. Previously provided + data is flushed before the parameter change. + + gzsetparams returns Z_OK if success, Z_STREAM_ERROR if the file was not + opened for writing, Z_ERRNO if there is an error writing the flushed data, + or Z_MEM_ERROR if there is a memory allocation error. +*/ + +ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len)); +/* + Reads the given number of uncompressed bytes from the compressed file. If + the input file is not in gzip format, gzread copies the given number of + bytes into the buffer directly from the file. + + After reaching the end of a gzip stream in the input, gzread will continue + to read, looking for another gzip stream. Any number of gzip streams may be + concatenated in the input file, and will all be decompressed by gzread(). + If something other than a gzip stream is encountered after a gzip stream, + that remaining trailing garbage is ignored (and no error is returned). + + gzread can be used to read a gzip file that is being concurrently written. + Upon reaching the end of the input, gzread will return with the available + data. If the error code returned by gzerror is Z_OK or Z_BUF_ERROR, then + gzclearerr can be used to clear the end of file indicator in order to permit + gzread to be tried again. Z_OK indicates that a gzip stream was completed + on the last gzread. Z_BUF_ERROR indicates that the input file ended in the + middle of a gzip stream. Note that gzread does not return -1 in the event + of an incomplete gzip stream. This error is deferred until gzclose(), which + will return Z_BUF_ERROR if the last gzread ended in the middle of a gzip + stream. Alternatively, gzerror can be used before gzclose to detect this + case. + + gzread returns the number of uncompressed bytes actually read, less than + len for end of file, or -1 for error. If len is too large to fit in an int, + then nothing is read, -1 is returned, and the error state is set to + Z_STREAM_ERROR. +*/ + +ZEXTERN z_size_t ZEXPORT gzfread OF((voidp buf, z_size_t size, z_size_t nitems, + gzFile file)); +/* + Read up to nitems items of size size from file to buf, otherwise operating + as gzread() does. This duplicates the interface of stdio's fread(), with + size_t request and return types. If the library defines size_t, then + z_size_t is identical to size_t. If not, then z_size_t is an unsigned + integer type that can contain a pointer. + + gzfread() returns the number of full items read of size size, or zero if + the end of the file was reached and a full item could not be read, or if + there was an error. gzerror() must be consulted if zero is returned in + order to determine if there was an error. If the multiplication of size and + nitems overflows, i.e. the product does not fit in a z_size_t, then nothing + is read, zero is returned, and the error state is set to Z_STREAM_ERROR. + + In the event that the end of file is reached and only a partial item is + available at the end, i.e. the remaining uncompressed data length is not a + multiple of size, then the final partial item is nevetheless read into buf + and the end-of-file flag is set. The length of the partial item read is not + provided, but could be inferred from the result of gztell(). This behavior + is the same as the behavior of fread() implementations in common libraries, + but it prevents the direct use of gzfread() to read a concurrently written + file, reseting and retrying on end-of-file, when size is not 1. +*/ + +ZEXTERN int ZEXPORT gzwrite OF((gzFile file, + voidpc buf, unsigned len)); +/* + Writes the given number of uncompressed bytes into the compressed file. + gzwrite returns the number of uncompressed bytes written or 0 in case of + error. +*/ + +ZEXTERN z_size_t ZEXPORT gzfwrite OF((voidpc buf, z_size_t size, + z_size_t nitems, gzFile file)); +/* + gzfwrite() writes nitems items of size size from buf to file, duplicating + the interface of stdio's fwrite(), with size_t request and return types. If + the library defines size_t, then z_size_t is identical to size_t. If not, + then z_size_t is an unsigned integer type that can contain a pointer. + + gzfwrite() returns the number of full items written of size size, or zero + if there was an error. If the multiplication of size and nitems overflows, + i.e. the product does not fit in a z_size_t, then nothing is written, zero + is returned, and the error state is set to Z_STREAM_ERROR. +*/ + +ZEXTERN int ZEXPORTVA gzprintf Z_ARG((gzFile file, const char *format, ...)); +/* + Converts, formats, and writes the arguments to the compressed file under + control of the format string, as in fprintf. gzprintf returns the number of + uncompressed bytes actually written, or a negative zlib error code in case + of error. The number of uncompressed bytes written is limited to 8191, or + one less than the buffer size given to gzbuffer(). The caller should assure + that this limit is not exceeded. If it is exceeded, then gzprintf() will + return an error (0) with nothing written. In this case, there may also be a + buffer overflow with unpredictable consequences, which is possible only if + zlib was compiled with the insecure functions sprintf() or vsprintf() + because the secure snprintf() or vsnprintf() functions were not available. + This can be determined using zlibCompileFlags(). +*/ + +ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s)); +/* + Writes the given null-terminated string to the compressed file, excluding + the terminating null character. + + gzputs returns the number of characters written, or -1 in case of error. +*/ + +ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len)); +/* + Reads bytes from the compressed file until len-1 characters are read, or a + newline character is read and transferred to buf, or an end-of-file + condition is encountered. If any characters are read or if len == 1, the + string is terminated with a null character. If no characters are read due + to an end-of-file or len < 1, then the buffer is left untouched. + + gzgets returns buf which is a null-terminated string, or it returns NULL + for end-of-file or in case of error. If there was an error, the contents at + buf are indeterminate. +*/ + +ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c)); +/* + Writes c, converted to an unsigned char, into the compressed file. gzputc + returns the value that was written, or -1 in case of error. +*/ + +ZEXTERN int ZEXPORT gzgetc OF((gzFile file)); +/* + Reads one byte from the compressed file. gzgetc returns this byte or -1 + in case of end of file or error. This is implemented as a macro for speed. + As such, it does not do all of the checking the other functions do. I.e. + it does not check to see if file is NULL, nor whether the structure file + points to has been clobbered or not. +*/ + +ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file)); +/* + Push one character back onto the stream to be read as the first character + on the next read. At least one character of push-back is allowed. + gzungetc() returns the character pushed, or -1 on failure. gzungetc() will + fail if c is -1, and may fail if a character has been pushed but not read + yet. If gzungetc is used immediately after gzopen or gzdopen, at least the + output buffer size of pushed characters is allowed. (See gzbuffer above.) + The pushed character will be discarded if the stream is repositioned with + gzseek() or gzrewind(). +*/ + +ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush)); +/* + Flushes all pending output into the compressed file. The parameter flush + is as in the deflate() function. The return value is the zlib error number + (see function gzerror below). gzflush is only permitted when writing. + + If the flush parameter is Z_FINISH, the remaining data is written and the + gzip stream is completed in the output. If gzwrite() is called again, a new + gzip stream will be started in the output. gzread() is able to read such + concatenated gzip streams. + + gzflush should be called only when strictly necessary because it will + degrade compression if called too often. +*/ + +/* +ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, + z_off_t offset, int whence)); + + Sets the starting position for the next gzread or gzwrite on the given + compressed file. The offset represents a number of bytes in the + uncompressed data stream. The whence parameter is defined as in lseek(2); + the value SEEK_END is not supported. + + If the file is opened for reading, this function is emulated but can be + extremely slow. If the file is opened for writing, only forward seeks are + supported; gzseek then compresses a sequence of zeroes up to the new + starting position. + + gzseek returns the resulting offset location as measured in bytes from + the beginning of the uncompressed stream, or -1 in case of error, in + particular if the file is opened for writing and the new starting position + would be before the current position. +*/ + +ZEXTERN int ZEXPORT gzrewind OF((gzFile file)); +/* + Rewinds the given file. This function is supported only for reading. + + gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET) +*/ + +/* +ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file)); + + Returns the starting position for the next gzread or gzwrite on the given + compressed file. This position represents a number of bytes in the + uncompressed data stream, and is zero when starting, even if appending or + reading a gzip stream from the middle of a file using gzdopen(). + + gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) +*/ + +/* +ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile file)); + + Returns the current offset in the file being read or written. This offset + includes the count of bytes that precede the gzip stream, for example when + appending or when using gzdopen() for reading. When reading, the offset + does not include as yet unused buffered input. This information can be used + for a progress indicator. On error, gzoffset() returns -1. +*/ + +ZEXTERN int ZEXPORT gzeof OF((gzFile file)); +/* + Returns true (1) if the end-of-file indicator has been set while reading, + false (0) otherwise. Note that the end-of-file indicator is set only if the + read tried to go past the end of the input, but came up short. Therefore, + just like feof(), gzeof() may return false even if there is no more data to + read, in the event that the last read request was for the exact number of + bytes remaining in the input file. This will happen if the input file size + is an exact multiple of the buffer size. + + If gzeof() returns true, then the read functions will return no more data, + unless the end-of-file indicator is reset by gzclearerr() and the input file + has grown since the previous end of file was detected. +*/ + +ZEXTERN int ZEXPORT gzdirect OF((gzFile file)); +/* + Returns true (1) if file is being copied directly while reading, or false + (0) if file is a gzip stream being decompressed. + + If the input file is empty, gzdirect() will return true, since the input + does not contain a gzip stream. + + If gzdirect() is used immediately after gzopen() or gzdopen() it will + cause buffers to be allocated to allow reading the file to determine if it + is a gzip file. Therefore if gzbuffer() is used, it should be called before + gzdirect(). + + When writing, gzdirect() returns true (1) if transparent writing was + requested ("wT" for the gzopen() mode), or false (0) otherwise. (Note: + gzdirect() is not needed when writing. Transparent writing must be + explicitly requested, so the application already knows the answer. When + linking statically, using gzdirect() will include all of the zlib code for + gzip file reading and decompression, which may not be desired.) +*/ + +ZEXTERN int ZEXPORT gzclose OF((gzFile file)); +/* + Flushes all pending output if necessary, closes the compressed file and + deallocates the (de)compression state. Note that once file is closed, you + cannot call gzerror with file, since its structures have been deallocated. + gzclose must not be called more than once on the same file, just as free + must not be called more than once on the same allocation. + + gzclose will return Z_STREAM_ERROR if file is not valid, Z_ERRNO on a + file operation error, Z_MEM_ERROR if out of memory, Z_BUF_ERROR if the + last read ended in the middle of a gzip stream, or Z_OK on success. +*/ + +ZEXTERN int ZEXPORT gzclose_r OF((gzFile file)); +ZEXTERN int ZEXPORT gzclose_w OF((gzFile file)); +/* + Same as gzclose(), but gzclose_r() is only for use when reading, and + gzclose_w() is only for use when writing or appending. The advantage to + using these instead of gzclose() is that they avoid linking in zlib + compression or decompression code that is not used when only reading or only + writing respectively. If gzclose() is used, then both compression and + decompression code will be included the application when linking to a static + zlib library. +*/ + +ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum)); +/* + Returns the error message for the last error which occurred on the given + compressed file. errnum is set to zlib error number. If an error occurred + in the file system and not in the compression library, errnum is set to + Z_ERRNO and the application may consult errno to get the exact error code. + + The application must not modify the returned string. Future calls to + this function may invalidate the previously returned string. If file is + closed, then the string previously returned by gzerror will no longer be + available. + + gzerror() should be used to distinguish errors from end-of-file for those + functions above that do not distinguish those cases in their return values. +*/ + +ZEXTERN void ZEXPORT gzclearerr OF((gzFile file)); +/* + Clears the error and end-of-file flags for file. This is analogous to the + clearerr() function in stdio. This is useful for continuing to read a gzip + file that is being written concurrently. +*/ + +#endif /* !Z_SOLO */ + + /* checksum functions */ + +/* + These functions are not related to compression but are exported + anyway because they might be useful in applications using the compression + library. +*/ + +ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len)); +/* + Update a running Adler-32 checksum with the bytes buf[0..len-1] and + return the updated checksum. If buf is Z_NULL, this function returns the + required initial value for the checksum. + + An Adler-32 checksum is almost as reliable as a CRC-32 but can be computed + much faster. + + Usage example: + + uLong adler = adler32(0L, Z_NULL, 0); + + while (read_buffer(buffer, length) != EOF) { + adler = adler32(adler, buffer, length); + } + if (adler != original_adler) error(); +*/ + +ZEXTERN uLong ZEXPORT adler32_z OF((uLong adler, const Bytef *buf, + z_size_t len)); +/* + Same as adler32(), but with a size_t length. +*/ + +/* +ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2, + z_off_t len2)); + + Combine two Adler-32 checksums into one. For two sequences of bytes, seq1 + and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for + each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of + seq1 and seq2 concatenated, requiring only adler1, adler2, and len2. Note + that the z_off_t type (like off_t) is a signed integer. If len2 is + negative, the result has no meaning or utility. +*/ + +ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len)); +/* + Update a running CRC-32 with the bytes buf[0..len-1] and return the + updated CRC-32. If buf is Z_NULL, this function returns the required + initial value for the crc. Pre- and post-conditioning (one's complement) is + performed within this function so it shouldn't be done by the application. + + Usage example: + + uLong crc = crc32(0L, Z_NULL, 0); + + while (read_buffer(buffer, length) != EOF) { + crc = crc32(crc, buffer, length); + } + if (crc != original_crc) error(); +*/ + +ZEXTERN uLong ZEXPORT crc32_z OF((uLong adler, const Bytef *buf, + z_size_t len)); +/* + Same as crc32(), but with a size_t length. +*/ + +/* +ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2)); + + Combine two CRC-32 check values into one. For two sequences of bytes, + seq1 and seq2 with lengths len1 and len2, CRC-32 check values were + calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32 + check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and + len2. +*/ + + + /* various hacks, don't look :) */ + +/* deflateInit and inflateInit are macros to allow checking the zlib version + * and the compiler's view of z_stream: + */ +ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method, + int windowBits, int memLevel, + int strategy, const char *version, + int stream_size)); +ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits, + unsigned char FAR *window, + const char *version, + int stream_size)); +#ifdef Z_PREFIX_SET +# define z_deflateInit(strm, level) \ + deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream)) +# define z_inflateInit(strm) \ + inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream)) +# define z_deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ + deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ + (strategy), ZLIB_VERSION, (int)sizeof(z_stream)) +# define z_inflateInit2(strm, windowBits) \ + inflateInit2_((strm), (windowBits), ZLIB_VERSION, \ + (int)sizeof(z_stream)) +# define z_inflateBackInit(strm, windowBits, window) \ + inflateBackInit_((strm), (windowBits), (window), \ + ZLIB_VERSION, (int)sizeof(z_stream)) +#else +# define deflateInit(strm, level) \ + deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream)) +# define inflateInit(strm) \ + inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream)) +# define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ + deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ + (strategy), ZLIB_VERSION, (int)sizeof(z_stream)) +# define inflateInit2(strm, windowBits) \ + inflateInit2_((strm), (windowBits), ZLIB_VERSION, \ + (int)sizeof(z_stream)) +# define inflateBackInit(strm, windowBits, window) \ + inflateBackInit_((strm), (windowBits), (window), \ + ZLIB_VERSION, (int)sizeof(z_stream)) +#endif + +#ifndef Z_SOLO + +/* gzgetc() macro and its supporting function and exposed data structure. Note + * that the real internal state is much larger than the exposed structure. + * This abbreviated structure exposes just enough for the gzgetc() macro. The + * user should not mess with these exposed elements, since their names or + * behavior could change in the future, perhaps even capriciously. They can + * only be used by the gzgetc() macro. You have been warned. + */ +struct gzFile_s { + unsigned have; + unsigned char *next; + z_off64_t pos; +}; +ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */ +#ifdef Z_PREFIX_SET +# undef z_gzgetc +# define z_gzgetc(g) \ + ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g)) +#else +# define gzgetc(g) \ + ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g)) +#endif + +/* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or + * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if + * both are true, the application gets the *64 functions, and the regular + * functions are changed to 64 bits) -- in case these are set on systems + * without large file support, _LFS64_LARGEFILE must also be true + */ +#ifdef Z_LARGE64 + ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); + ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int)); + ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile)); + ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile)); + ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t)); + ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t)); +#endif + +#if !defined(ZLIB_INTERNAL) && defined(Z_WANT64) +# ifdef Z_PREFIX_SET +# define z_gzopen z_gzopen64 +# define z_gzseek z_gzseek64 +# define z_gztell z_gztell64 +# define z_gzoffset z_gzoffset64 +# define z_adler32_combine z_adler32_combine64 +# define z_crc32_combine z_crc32_combine64 +# else +# define gzopen gzopen64 +# define gzseek gzseek64 +# define gztell gztell64 +# define gzoffset gzoffset64 +# define adler32_combine adler32_combine64 +# define crc32_combine crc32_combine64 +# endif +# ifndef Z_LARGE64 + ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); + ZEXTERN z_off_t ZEXPORT gzseek64 OF((gzFile, z_off_t, int)); + ZEXTERN z_off_t ZEXPORT gztell64 OF((gzFile)); + ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile)); + ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t)); + ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t)); +# endif +#else + ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *)); + ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile, z_off_t, int)); + ZEXTERN z_off_t ZEXPORT gztell OF((gzFile)); + ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile)); + ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t)); + ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t)); +#endif + +#else /* Z_SOLO */ + + ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t)); + ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t)); + +#endif /* !Z_SOLO */ + +/* undocumented functions */ +ZEXTERN const char * ZEXPORT zError OF((int)); +ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp)); +ZEXTERN const z_crc_t FAR * ZEXPORT get_crc_table OF((void)); +ZEXTERN int ZEXPORT inflateUndermine OF((z_streamp, int)); +ZEXTERN int ZEXPORT inflateValidate OF((z_streamp, int)); +ZEXTERN unsigned long ZEXPORT inflateCodesUsed OF ((z_streamp)); +ZEXTERN int ZEXPORT inflateResetKeep OF((z_streamp)); +ZEXTERN int ZEXPORT deflateResetKeep OF((z_streamp)); +#if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(Z_SOLO) +ZEXTERN gzFile ZEXPORT gzopen_w OF((const wchar_t *path, + const char *mode)); +#endif +#if defined(STDC) || defined(Z_HAVE_STDARG_H) +# ifndef Z_SOLO +ZEXTERN int ZEXPORTVA gzvprintf Z_ARG((gzFile file, + const char *format, + va_list va)); +# endif +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* ZLIB_H */ diff --git a/third_party/java_tools/zlib/zutil.c b/third_party/java_tools/zlib/zutil.c new file mode 100644 index 000000000..a76c6b0c7 --- /dev/null +++ b/third_party/java_tools/zlib/zutil.c @@ -0,0 +1,325 @@ +/* zutil.c -- target dependent utility functions for the compression library + * Copyright (C) 1995-2017 Jean-loup Gailly + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* @(#) $Id$ */ + +#include "zutil.h" +#ifndef Z_SOLO +# include "gzguts.h" +#endif + +z_const char * const z_errmsg[10] = { + (z_const char *)"need dictionary", /* Z_NEED_DICT 2 */ + (z_const char *)"stream end", /* Z_STREAM_END 1 */ + (z_const char *)"", /* Z_OK 0 */ + (z_const char *)"file error", /* Z_ERRNO (-1) */ + (z_const char *)"stream error", /* Z_STREAM_ERROR (-2) */ + (z_const char *)"data error", /* Z_DATA_ERROR (-3) */ + (z_const char *)"insufficient memory", /* Z_MEM_ERROR (-4) */ + (z_const char *)"buffer error", /* Z_BUF_ERROR (-5) */ + (z_const char *)"incompatible version",/* Z_VERSION_ERROR (-6) */ + (z_const char *)"" +}; + + +const char * ZEXPORT zlibVersion() +{ + return ZLIB_VERSION; +} + +uLong ZEXPORT zlibCompileFlags() +{ + uLong flags; + + flags = 0; + switch ((int)(sizeof(uInt))) { + case 2: break; + case 4: flags += 1; break; + case 8: flags += 2; break; + default: flags += 3; + } + switch ((int)(sizeof(uLong))) { + case 2: break; + case 4: flags += 1 << 2; break; + case 8: flags += 2 << 2; break; + default: flags += 3 << 2; + } + switch ((int)(sizeof(voidpf))) { + case 2: break; + case 4: flags += 1 << 4; break; + case 8: flags += 2 << 4; break; + default: flags += 3 << 4; + } + switch ((int)(sizeof(z_off_t))) { + case 2: break; + case 4: flags += 1 << 6; break; + case 8: flags += 2 << 6; break; + default: flags += 3 << 6; + } +#ifdef ZLIB_DEBUG + flags += 1 << 8; +#endif +#if defined(ASMV) || defined(ASMINF) + flags += 1 << 9; +#endif +#ifdef ZLIB_WINAPI + flags += 1 << 10; +#endif +#ifdef BUILDFIXED + flags += 1 << 12; +#endif +#ifdef DYNAMIC_CRC_TABLE + flags += 1 << 13; +#endif +#ifdef NO_GZCOMPRESS + flags += 1L << 16; +#endif +#ifdef NO_GZIP + flags += 1L << 17; +#endif +#ifdef PKZIP_BUG_WORKAROUND + flags += 1L << 20; +#endif +#ifdef FASTEST + flags += 1L << 21; +#endif +#if defined(STDC) || defined(Z_HAVE_STDARG_H) +# ifdef NO_vsnprintf + flags += 1L << 25; +# ifdef HAS_vsprintf_void + flags += 1L << 26; +# endif +# else +# ifdef HAS_vsnprintf_void + flags += 1L << 26; +# endif +# endif +#else + flags += 1L << 24; +# ifdef NO_snprintf + flags += 1L << 25; +# ifdef HAS_sprintf_void + flags += 1L << 26; +# endif +# else +# ifdef HAS_snprintf_void + flags += 1L << 26; +# endif +# endif +#endif + return flags; +} + +#ifdef ZLIB_DEBUG +#include +# ifndef verbose +# define verbose 0 +# endif +int ZLIB_INTERNAL z_verbose = verbose; + +void ZLIB_INTERNAL z_error (m) + char *m; +{ + fprintf(stderr, "%s\n", m); + exit(1); +} +#endif + +/* exported to allow conversion of error code to string for compress() and + * uncompress() + */ +const char * ZEXPORT zError(err) + int err; +{ + return ERR_MSG(err); +} + +#if defined(_WIN32_WCE) + /* The Microsoft C Run-Time Library for Windows CE doesn't have + * errno. We define it as a global variable to simplify porting. + * Its value is always 0 and should not be used. + */ + int errno = 0; +#endif + +#ifndef HAVE_MEMCPY + +void ZLIB_INTERNAL zmemcpy(dest, source, len) + Bytef* dest; + const Bytef* source; + uInt len; +{ + if (len == 0) return; + do { + *dest++ = *source++; /* ??? to be unrolled */ + } while (--len != 0); +} + +int ZLIB_INTERNAL zmemcmp(s1, s2, len) + const Bytef* s1; + const Bytef* s2; + uInt len; +{ + uInt j; + + for (j = 0; j < len; j++) { + if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; + } + return 0; +} + +void ZLIB_INTERNAL zmemzero(dest, len) + Bytef* dest; + uInt len; +{ + if (len == 0) return; + do { + *dest++ = 0; /* ??? to be unrolled */ + } while (--len != 0); +} +#endif + +#ifndef Z_SOLO + +#ifdef SYS16BIT + +#ifdef __TURBOC__ +/* Turbo C in 16-bit mode */ + +# define MY_ZCALLOC + +/* Turbo C malloc() does not allow dynamic allocation of 64K bytes + * and farmalloc(64K) returns a pointer with an offset of 8, so we + * must fix the pointer. Warning: the pointer must be put back to its + * original form in order to free it, use zcfree(). + */ + +#define MAX_PTR 10 +/* 10*64K = 640K */ + +local int next_ptr = 0; + +typedef struct ptr_table_s { + voidpf org_ptr; + voidpf new_ptr; +} ptr_table; + +local ptr_table table[MAX_PTR]; +/* This table is used to remember the original form of pointers + * to large buffers (64K). Such pointers are normalized with a zero offset. + * Since MSDOS is not a preemptive multitasking OS, this table is not + * protected from concurrent access. This hack doesn't work anyway on + * a protected system like OS/2. Use Microsoft C instead. + */ + +voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size) +{ + voidpf buf; + ulg bsize = (ulg)items*size; + + (void)opaque; + + /* If we allocate less than 65520 bytes, we assume that farmalloc + * will return a usable pointer which doesn't have to be normalized. + */ + if (bsize < 65520L) { + buf = farmalloc(bsize); + if (*(ush*)&buf != 0) return buf; + } else { + buf = farmalloc(bsize + 16L); + } + if (buf == NULL || next_ptr >= MAX_PTR) return NULL; + table[next_ptr].org_ptr = buf; + + /* Normalize the pointer to seg:0 */ + *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; + *(ush*)&buf = 0; + table[next_ptr++].new_ptr = buf; + return buf; +} + +void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) +{ + int n; + + (void)opaque; + + if (*(ush*)&ptr != 0) { /* object < 64K */ + farfree(ptr); + return; + } + /* Find the original pointer */ + for (n = 0; n < next_ptr; n++) { + if (ptr != table[n].new_ptr) continue; + + farfree(table[n].org_ptr); + while (++n < next_ptr) { + table[n-1] = table[n]; + } + next_ptr--; + return; + } + Assert(0, "zcfree: ptr not found"); +} + +#endif /* __TURBOC__ */ + + +#ifdef M_I86 +/* Microsoft C in 16-bit mode */ + +# define MY_ZCALLOC + +#if (!defined(_MSC_VER) || (_MSC_VER <= 600)) +# define _halloc halloc +# define _hfree hfree +#endif + +voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size) +{ + (void)opaque; + return _halloc((long)items, size); +} + +void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) +{ + (void)opaque; + _hfree(ptr); +} + +#endif /* M_I86 */ + +#endif /* SYS16BIT */ + + +#ifndef MY_ZCALLOC /* Any system without a special alloc function */ + +#ifndef STDC +extern voidp malloc OF((uInt size)); +extern voidp calloc OF((uInt items, uInt size)); +extern void free OF((voidpf ptr)); +#endif + +voidpf ZLIB_INTERNAL zcalloc (opaque, items, size) + voidpf opaque; + unsigned items; + unsigned size; +{ + (void)opaque; + return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : + (voidpf)calloc(items, size); +} + +void ZLIB_INTERNAL zcfree (opaque, ptr) + voidpf opaque; + voidpf ptr; +{ + (void)opaque; + free(ptr); +} + +#endif /* MY_ZCALLOC */ + +#endif /* !Z_SOLO */ diff --git a/third_party/java_tools/zlib/zutil.h b/third_party/java_tools/zlib/zutil.h new file mode 100644 index 000000000..b079ea6a8 --- /dev/null +++ b/third_party/java_tools/zlib/zutil.h @@ -0,0 +1,271 @@ +/* zutil.h -- internal interface and configuration of the compression library + * Copyright (C) 1995-2016 Jean-loup Gailly, Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* WARNING: this file should *not* be used by applications. It is + part of the implementation of the compression library and is + subject to change. Applications should only use zlib.h. + */ + +/* @(#) $Id$ */ + +#ifndef ZUTIL_H +#define ZUTIL_H + +#ifdef HAVE_HIDDEN +# define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) +#else +# define ZLIB_INTERNAL +#endif + +#include "zlib.h" + +#if defined(STDC) && !defined(Z_SOLO) +# if !(defined(_WIN32_WCE) && defined(_MSC_VER)) +# include +# endif +# include +# include +#endif + +#ifdef Z_SOLO + typedef long ptrdiff_t; /* guess -- will be caught if guess is wrong */ +#endif + +#ifndef local +# define local static +#endif +/* since "static" is used to mean two completely different things in C, we + define "local" for the non-static meaning of "static", for readability + (compile with -Dlocal if your debugger can't find static symbols) */ + +typedef unsigned char uch; +typedef uch FAR uchf; +typedef unsigned short ush; +typedef ush FAR ushf; +typedef unsigned long ulg; + +extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ +/* (size given to avoid silly warnings with Visual C++) */ + +#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)] + +#define ERR_RETURN(strm,err) \ + return (strm->msg = ERR_MSG(err), (err)) +/* To be used only when the state is known to be valid */ + + /* common constants */ + +#ifndef DEF_WBITS +# define DEF_WBITS MAX_WBITS +#endif +/* default windowBits for decompression. MAX_WBITS is for compression only */ + +#if MAX_MEM_LEVEL >= 8 +# define DEF_MEM_LEVEL 8 +#else +# define DEF_MEM_LEVEL MAX_MEM_LEVEL +#endif +/* default memLevel */ + +#define STORED_BLOCK 0 +#define STATIC_TREES 1 +#define DYN_TREES 2 +/* The three kinds of block type */ + +#define MIN_MATCH 3 +#define MAX_MATCH 258 +/* The minimum and maximum match lengths */ + +#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */ + + /* target dependencies */ + +#if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32)) +# define OS_CODE 0x00 +# ifndef Z_SOLO +# if defined(__TURBOC__) || defined(__BORLANDC__) +# if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__)) + /* Allow compilation with ANSI keywords only enabled */ + void _Cdecl farfree( void *block ); + void *_Cdecl farmalloc( unsigned long nbytes ); +# else +# include +# endif +# else /* MSC or DJGPP */ +# include +# endif +# endif +#endif + +#ifdef AMIGA +# define OS_CODE 1 +#endif + +#if defined(VAXC) || defined(VMS) +# define OS_CODE 2 +# define F_OPEN(name, mode) \ + fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512") +#endif + +#ifdef __370__ +# if __TARGET_LIB__ < 0x20000000 +# define OS_CODE 4 +# elif __TARGET_LIB__ < 0x40000000 +# define OS_CODE 11 +# else +# define OS_CODE 8 +# endif +#endif + +#if defined(ATARI) || defined(atarist) +# define OS_CODE 5 +#endif + +#ifdef OS2 +# define OS_CODE 6 +# if defined(M_I86) && !defined(Z_SOLO) +# include +# endif +#endif + +#if defined(MACOS) || defined(TARGET_OS_MAC) +# define OS_CODE 7 +# ifndef Z_SOLO +# if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os +# include /* for fdopen */ +# else +# ifndef fdopen +# define fdopen(fd,mode) NULL /* No fdopen() */ +# endif +# endif +# endif +#endif + +#ifdef __acorn +# define OS_CODE 13 +#endif + +#if defined(WIN32) && !defined(__CYGWIN__) +# define OS_CODE 10 +#endif + +#ifdef _BEOS_ +# define OS_CODE 16 +#endif + +#ifdef __TOS_OS400__ +# define OS_CODE 18 +#endif + +#ifdef __APPLE__ +# define OS_CODE 19 +#endif + +#if defined(_BEOS_) || defined(RISCOS) +# define fdopen(fd,mode) NULL /* No fdopen() */ +#endif + +#if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX +# if defined(_WIN32_WCE) +# define fdopen(fd,mode) NULL /* No fdopen() */ +# ifndef _PTRDIFF_T_DEFINED + typedef int ptrdiff_t; +# define _PTRDIFF_T_DEFINED +# endif +# else +# define fdopen(fd,type) _fdopen(fd,type) +# endif +#endif + +#if defined(__BORLANDC__) && !defined(MSDOS) + #pragma warn -8004 + #pragma warn -8008 + #pragma warn -8066 +#endif + +/* provide prototypes for these when building zlib without LFS */ +#if !defined(_WIN32) && \ + (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0) + ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t)); + ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t)); +#endif + + /* common defaults */ + +#ifndef OS_CODE +# define OS_CODE 3 /* assume Unix */ +#endif + +#ifndef F_OPEN +# define F_OPEN(name, mode) fopen((name), (mode)) +#endif + + /* functions */ + +#if defined(pyr) || defined(Z_SOLO) +# define NO_MEMCPY +#endif +#if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__) + /* Use our own functions for small and medium model with MSC <= 5.0. + * You may have to use the same strategy for Borland C (untested). + * The __SC__ check is for Symantec. + */ +# define NO_MEMCPY +#endif +#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY) +# define HAVE_MEMCPY +#endif +#ifdef HAVE_MEMCPY +# ifdef SMALL_MEDIUM /* MSDOS small or medium model */ +# define zmemcpy _fmemcpy +# define zmemcmp _fmemcmp +# define zmemzero(dest, len) _fmemset(dest, 0, len) +# else +# define zmemcpy memcpy +# define zmemcmp memcmp +# define zmemzero(dest, len) memset(dest, 0, len) +# endif +#else + void ZLIB_INTERNAL zmemcpy OF((Bytef* dest, const Bytef* source, uInt len)); + int ZLIB_INTERNAL zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len)); + void ZLIB_INTERNAL zmemzero OF((Bytef* dest, uInt len)); +#endif + +/* Diagnostic functions */ +#ifdef ZLIB_DEBUG +# include + extern int ZLIB_INTERNAL z_verbose; + extern void ZLIB_INTERNAL z_error OF((char *m)); +# define Assert(cond,msg) {if(!(cond)) z_error(msg);} +# define Trace(x) {if (z_verbose>=0) fprintf x ;} +# define Tracev(x) {if (z_verbose>0) fprintf x ;} +# define Tracevv(x) {if (z_verbose>1) fprintf x ;} +# define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;} +# define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;} +#else +# define Assert(cond,msg) +# define Trace(x) +# define Tracev(x) +# define Tracevv(x) +# define Tracec(c,x) +# define Tracecv(c,x) +#endif + +#ifndef Z_SOLO + voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items, + unsigned size)); + void ZLIB_INTERNAL zcfree OF((voidpf opaque, voidpf ptr)); +#endif + +#define ZALLOC(strm, items, size) \ + (*((strm)->zalloc))((strm)->opaque, (items), (size)) +#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr)) +#define TRY_FREE(s, p) {if (p) ZFREE(s, p);} + +/* Reverse the bytes in a 32-bit value */ +#define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ + (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) + +#endif /* ZUTIL_H */ From aa4a41331897a7cfeb44c272a290128a04ab2752 Mon Sep 17 00:00:00 2001 From: Vaidas Pilkauskas Date: Wed, 13 Jan 2021 09:31:48 +0200 Subject: [PATCH 10/11] Add comments regarding ijar bug --- scala/scala_import.bzl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scala/scala_import.bzl b/scala/scala_import.bzl index 0b0ad83f3..6174bbf27 100644 --- a/scala/scala_import.bzl +++ b/scala/scala_import.bzl @@ -7,7 +7,8 @@ def _stamp_symlinked_jar(ctx, jar): stamped_jar_filename = jar.basename.rstrip(".jar") + "-stamped.jar" # Preferred way, but currently broken: - # java toolchain's ijar incorrectly handles + # java toolchain's ijar incorrectly handles MANIFEST sections + # https://github.com/bazelbuild/bazel/issues/12730 # return java_common.stamp_jar( # actions = ctx.actions, # jar = symlink_file, From 427d2071d2f9d4a704925fd2d2d7bb37a13fa9b3 Mon Sep 17 00:00:00 2001 From: Vaidas Pilkauskas Date: Wed, 13 Jan 2021 09:37:28 +0200 Subject: [PATCH 11/11] Fix default label and make ijar implicit dep --- scala/scala_import.bzl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scala/scala_import.bzl b/scala/scala_import.bzl index 6174bbf27..8f9b280e2 100644 --- a/scala/scala_import.bzl +++ b/scala/scala_import.bzl @@ -20,7 +20,7 @@ def _stamp_symlinked_jar(ctx, jar): stamped_file = ctx.actions.declare_file(stamped_jar_filename) ctx.actions.run( - executable = ctx.executable.ijar, + executable = ctx.executable._ijar, inputs = [symlink_file], outputs = [stamped_file], arguments = [ @@ -155,8 +155,8 @@ scala_import = rule( allow_single_file = True, default = Label("@io_bazel_rules_scala//scala:libPlaceHolderClassToCreateEmptyJarForScalaImport.jar"), ), - "ijar": attr.label( - default = Label("//third_party/java_tools/ijar:ijar"), + "_ijar": attr.label( + default = Label("@io_bazel_rules_scala//third_party/java_tools/ijar:ijar"), executable = True, cfg = "exec", allow_files = True,