-
Notifications
You must be signed in to change notification settings - Fork 869
检测到系统 zlib 实现为 zlib-ng 时跳过 Forge/NeoForge 安装时的哈希检测 #6068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+97
−4
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
45fc79e
feat: add ZlibUtils for zlib compatibility detection and adjust SHA-1…
Glavo 9e993d7
fix: update Zlib compatibility check with specific byte array comparison
Glavo caa71e5
fix: clarify comment on zlib output comparison in ZlibUtils
Glavo f03e6d0
refactor: simplify imports and improve Zlib compatibility check
Glavo 5c0d584
fix: remove unnecessary empty line in ForgeNewInstallTask.java
Glavo 2de8a1e
refactor: make IS_ZLIB_COMPATIBLE constant in ZlibUtils
Glavo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ | |
| import org.jackhuang.hmcl.task.Task; | ||
| import org.jackhuang.hmcl.util.DigestUtils; | ||
| import org.jackhuang.hmcl.util.StringUtils; | ||
| import org.jackhuang.hmcl.util.ZlibUtils; | ||
| import org.jackhuang.hmcl.util.function.ExceptionalFunction; | ||
| import org.jackhuang.hmcl.util.gson.JsonUtils; | ||
| import org.jackhuang.hmcl.util.io.ChecksumMismatchException; | ||
|
|
@@ -168,6 +169,22 @@ public void execute() throws Exception { | |
| } | ||
|
|
||
| if (!Objects.equals(code, entry.getValue())) { | ||
| if (!ZlibUtils.IS_ZLIB_COMPATIBLE && FileUtils.getExtension(artifact).equals("jar")) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| // Forge/NeoForge generates JARs dynamically during installation. | ||
| // When native compression libraries such as zlib-ng are in use, | ||
| // the resulting JAR may be compressed differently, causing its | ||
| // SHA-1 hash to differ from the expected value recorded in the | ||
| // install profile. In this case, fall back to verifying that the | ||
| // file is at least a structurally valid ZIP/JAR archive. | ||
| try { | ||
| FileDownloadTask.ZIP_INTEGRITY_CHECK_HANDLER.checkIntegrity(artifact, artifact); | ||
| LOG.info("Ignoring SHA-1 mismatch for " + artifact + " due to non-standard zlib compression output"); | ||
| continue; | ||
| } catch (Exception ignored) { | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Files.delete(artifact); | ||
| throw new ChecksumMismatchException("SHA-1", entry.getValue(), code); | ||
| } | ||
|
|
||
77 changes: 77 additions & 0 deletions
77
HMCLCore/src/main/java/org/jackhuang/hmcl/util/ZlibUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Hello Minecraft! Launcher | ||
| * Copyright (C) 2026 huangyuhui <huanghongxun2008@126.com> and contributors | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.jackhuang.hmcl.util; | ||
|
|
||
| import org.jetbrains.annotations.NotNullByDefault; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.zip.Deflater; | ||
|
|
||
| /// Utilities for detecting the compatibility of the system's zlib implementation. | ||
| /// | ||
| /// Some systems replace the JVM's default zlib with an alternative such as zlib-ng, | ||
| /// which may produce compressed output that differs byte-for-byte from the output of | ||
| /// the standard zlib. This can cause hash mismatches when verifying artifacts whose | ||
| /// expected checksums were computed with a specific zlib implementation. | ||
| /// | ||
| /// @author Glavo | ||
| @NotNullByDefault | ||
| public final class ZlibUtils { | ||
|
|
||
| /// Whether the JVM's [Deflater] produces output that is byte-for-byte identical | ||
| /// to the expected output of the standard reference zlib for a known test vector. | ||
| /// | ||
| /// This flag is `true` when the system's zlib behaves like the standard reference | ||
| /// implementation, and `false` when an alternative library such as zlib-ng is in | ||
| /// use and may produce different compressed bytes. | ||
| /// | ||
| /// When this flag is `false`, hash verification for dynamically generated JAR | ||
| /// files (e.g., those produced by the Forge/NeoForge installer processors) may | ||
| /// be relaxed to a structural integrity check rather than an exact hash match, | ||
| /// because the alternative zlib may legitimately produce files with different | ||
| /// checksums. | ||
| public static final boolean IS_ZLIB_COMPATIBLE; | ||
|
|
||
| static { | ||
| var expectedCompressed = new byte[]{120, -100, 99, 96, 0, 2, 0, 0, 5, 0, 1}; | ||
| byte[] compressed = new byte[64]; | ||
| int compressedLength = 0; | ||
|
|
||
| var deflater = new Deflater(); | ||
| try { | ||
| deflater.setInput(new byte[5]); | ||
| deflater.finish(); | ||
|
|
||
| while (!deflater.finished()) { | ||
| compressedLength += deflater.deflate(compressed, compressedLength, compressed.length - compressedLength); | ||
| } | ||
| } finally { | ||
| deflater.end(); | ||
| } | ||
|
|
||
| IS_ZLIB_COMPATIBLE = Arrays.equals( | ||
| expectedCompressed, | ||
| 0, expectedCompressed.length, | ||
| compressed, | ||
| 0, compressedLength | ||
| ); | ||
| } | ||
|
|
||
| private ZlibUtils() { | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议避免使用通配符导入(wildcard import)。根据 Google Java 编程风格指南以及本项目现有的代码风格,应当使用具体的类导入,以提高代码的可读性并避免潜在的命名冲突。