forked from ReVanced/revanced-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: invalid header when writing a
ZipFile
(ReVanced#169)
- Loading branch information
Showing
6 changed files
with
144 additions
and
98 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,12 +1,37 @@ | ||
package app.revanced.cli.aligning | ||
|
||
import app.revanced.cli.command.MainCommand.logger | ||
import app.revanced.patcher.PatcherResult | ||
import app.revanced.utils.signing.align.ZipAligner | ||
import app.revanced.utils.signing.align.zip.ZipFile | ||
import app.revanced.utils.signing.align.zip.structures.ZipEntry | ||
import java.io.File | ||
|
||
object Aligning { | ||
fun align(inputFile: File, outputFile: File) { | ||
fun align(result: PatcherResult, inputFile: File, outputFile: File) { | ||
logger.info("Aligning ${inputFile.name} to ${outputFile.name}") | ||
ZipAligner.align(inputFile, outputFile) | ||
|
||
if (outputFile.exists()) outputFile.delete() | ||
|
||
ZipFile(outputFile).use { file -> | ||
result.dexFiles.forEach { | ||
file.addEntryCompressData( | ||
ZipEntry.createWithName(it.name), | ||
it.stream.readBytes() | ||
) | ||
} | ||
|
||
result.resourceFile?.let { | ||
file.copyEntriesFromFileAligned( | ||
ZipFile(it), | ||
ZipAligner::getEntryAlignment | ||
) | ||
} | ||
|
||
file.copyEntriesFromFileAligned( | ||
ZipFile(inputFile), | ||
ZipAligner::getEntryAlignment | ||
) | ||
} | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,53 +1,24 @@ | ||
package app.revanced.cli.patcher | ||
|
||
import app.revanced.cli.command.MainCommand.args | ||
import app.revanced.cli.command.MainCommand.logger | ||
import app.revanced.patcher.PatcherResult | ||
import app.revanced.patcher.data.Context | ||
import app.revanced.patcher.patch.Patch | ||
import app.revanced.utils.filesystem.ZipFileSystemUtils | ||
import app.revanced.utils.patcher.addPatchesFiltered | ||
import app.revanced.utils.patcher.applyPatchesVerbose | ||
import app.revanced.utils.patcher.mergeFiles | ||
import java.io.File | ||
import java.nio.file.Files | ||
|
||
internal object Patcher { | ||
internal fun start( | ||
patcher: app.revanced.patcher.Patcher, | ||
output: File, | ||
allPatches: List<Class<out Patch<Context>>> | ||
) { | ||
val inputFile = args.inputFile | ||
|
||
): PatcherResult { | ||
// merge files like necessary integrations | ||
patcher.mergeFiles() | ||
// add patches, but filter incompatible or excluded patches | ||
patcher.addPatchesFiltered(allPatches) | ||
// apply patches | ||
patcher.applyPatchesVerbose() | ||
|
||
// write output file | ||
if (output.exists()) Files.delete(output.toPath()) | ||
inputFile.copyTo(output) | ||
|
||
val result = patcher.save() | ||
ZipFileSystemUtils(output).use { outputFileSystem -> | ||
// replace all dex files | ||
result.dexFiles.forEach { | ||
logger.info("Writing dex file ${it.name}") | ||
outputFileSystem.write(it.name, it.stream.readAllBytes()) | ||
} | ||
|
||
result.resourceFile?.let { | ||
logger.info("Writing resources...") | ||
|
||
ZipFileSystemUtils(it).use { resourceFileSystem -> | ||
val resourceFiles = resourceFileSystem.getFile(File.separator) | ||
outputFileSystem.writePathRecursively(resourceFiles) | ||
} | ||
} | ||
|
||
result.doNotCompress?.let { outputFileSystem.uncompress(*it.toTypedArray()) } | ||
} | ||
return patcher.save() | ||
} | ||
} |
23 changes: 3 additions & 20 deletions
23
src/main/kotlin/app/revanced/utils/signing/align/ZipAligner.kt
This file contains 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 |
---|---|---|
@@ -1,28 +1,11 @@ | ||
package app.revanced.utils.signing.align | ||
|
||
import app.revanced.utils.signing.align.zip.ZipFile | ||
import java.io.File | ||
import app.revanced.utils.signing.align.zip.structures.ZipEntry | ||
|
||
internal object ZipAligner { | ||
private const val DEFAULT_ALIGNMENT = 4 | ||
private const val LIBRARY_ALIGNMENT = 4096 | ||
|
||
fun align(input: File, output: File) { | ||
val inputZip = ZipFile(input) | ||
val outputZip = ZipFile(output) | ||
|
||
for (entry in inputZip.entries) { | ||
val data = inputZip.getDataForEntry(entry) | ||
|
||
if (entry.compression == 0.toUShort()) { | ||
val alignment = if (entry.fileName.endsWith(".so")) LIBRARY_ALIGNMENT else DEFAULT_ALIGNMENT | ||
|
||
outputZip.addEntryAligned(entry, data, alignment) | ||
} else { | ||
outputZip.addEntry(entry, data) | ||
} | ||
} | ||
|
||
outputZip.finish() | ||
} | ||
fun getEntryAlignment(entry: ZipEntry): Int? = | ||
if (entry.compression.toUInt() != 0u) null else if (entry.fileName.endsWith(".so")) LIBRARY_ALIGNMENT else DEFAULT_ALIGNMENT | ||
} |
This file contains 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
Oops, something went wrong.