Skip to content

Commit

Permalink
Add option to toggle how JVM zip strategy checks for the start offset…
Browse files Browse the repository at this point in the history
… of ZIP files
  • Loading branch information
Col-E committed Jan 13, 2025
1 parent 14d4b4c commit 2431fc3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jlinker = "1.0.7"
jphantom = "1.4.4"
junit = "5.11.0"
jsvg = "1.6.1"
llzip = "2.6.2"
lljzip = "2.7.0"
logback-classic = { strictly = "1.4.11" } # newer releases break in jar releases
mapping-io = "0.6.1"
mockito = "5.14.2"
Expand Down Expand Up @@ -109,7 +109,7 @@ junit-params = { module = "org.junit.jupiter:junit-jupiter-params", version.ref

jsvg = { module = "com.github.weisj:jsvg", version.ref = "jsvg" }

llzip = { module = "software.coley:lljzip", version.ref = "llzip" }
lljzip = { module = "software.coley:lljzip", version.ref = "lljzip" }

logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "logback-classic" }

Expand Down
2 changes: 1 addition & 1 deletion recaf-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies {
api(libs.gson) // required by R8 (from dex-translator)
api(libs.instrument.server)
api(libs.jphantom)
api(libs.llzip)
api(libs.lljzip)
api(libs.bundles.logging)
api(libs.mapping.io)
api(libs.natural.order)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
public class ResourceImporterConfig extends BasicConfigContainer implements ServiceConfig {
private final ObservableObject<ZipStrategy> zipStrategy = new ObservableObject<>(ZipStrategy.JVM);
private final ObservableBoolean skipRevisitedCenToLocalLinks = new ObservableBoolean(true);
private final ObservableBoolean allowBasicJvmBaseOffsetZeroCheck = new ObservableBoolean(true);

@Inject
public ResourceImporterConfig() {
super(ConfigGroups.SERVICE_IO, ResourceImporter.SERVICE_ID + CONFIG_SUFFIX);

addValue(new BasicConfigValue<>("zip-strategy", ZipStrategy.class, zipStrategy));
addValue(new BasicConfigValue<>("skip-revisited-cen-to-local-links", boolean.class, skipRevisitedCenToLocalLinks));
addValue(new BasicConfigValue<>("allow-basic-base-offset-zero-check", boolean.class, allowBasicJvmBaseOffsetZeroCheck));
}

/**
Expand All @@ -55,14 +57,39 @@ public ObservableBoolean getSkipRevisitedCenToLocalLinks() {
return skipRevisitedCenToLocalLinks;
}

/**
* When the {@link #getZipStrategy() ZIP strategy} is {@link ZipStrategy#JVM} this allows toggling
* how the JVM base offset of the zip file is calculated. Normally the start of a ZIP file is calculated
* based off the logic in {@code ZipFile.Source#findEND()} which looks like:
* <pre>{@code
* // ENDSIG matched, however the size of file comment in it does
* // not match the real size. One "common" cause for this problem
* // is some "extra" bytes are padded at the end of the zipfile.
* // Let's do some extra verification, we don't care about the
* // performance in this situation.
* byte[] sbuf = new byte[4];
* long cenpos = end.endpos - end.cenlen;
* long locpos = cenpos - end.cenoff;
* }</pre>
* In some edge cases this results in {@code locpos} being {@code > 0} even when the file has no prefix/padding.
*
* @return {@code true} when defaulting to check for zero being the base JVM zip offset instead of the lookup
* based on the code in {@code ZipFile.Source#findEND()}.
*/
@Nonnull
public ObservableBoolean getAllowBasicJvmBaseOffsetZeroCheck() {
return allowBasicJvmBaseOffsetZeroCheck;
}

/**
* @return Mapping of input bytes to a ZIP archive model.
*/
@Nonnull
public UncheckedFunction<byte[], ZipArchive> mapping() {
ZipStrategy strategy = zipStrategy.getValue();
if (strategy == ZipStrategy.JVM)
return input -> ZipIO.read(input, new JvmZipReader(skipRevisitedCenToLocalLinks.getValue()));
return input -> ZipIO.read(input, new JvmZipReader(skipRevisitedCenToLocalLinks.getValue(),
allowBasicJvmBaseOffsetZeroCheck.getValue()));
if (strategy == ZipStrategy.STANDARD)
return ZipIO::readStandard;
return ZipIO::readNaive;
Expand Down
1 change: 1 addition & 0 deletions recaf-ui/src/main/resources/translations/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ service.io.recent-workspaces-config.recent-workspaces=Recent workspace
service.io.recent-workspaces-config.last-class-export-path=Last class export path
service.io.resource-importer-config=Archive importing
service.io.resource-importer-config.zip-strategy=ZIP parsing strategy
service.io.resource-importer-config.allow-basic-base-offset-zero-check=Default to check 0 as zip beginning with JVM strategy
service.io.resource-importer-config.skip-revisited-cen-to-local-links=Skip duplicate CEN-to-LOC entries with JVM strategy
service.mapping=Mapping
service.mapping.mapping-aggregator-config=Mapping aggregation
Expand Down

0 comments on commit 2431fc3

Please sign in to comment.