|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 github.com/REAndroid |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.reandroid.apkeditor.protect; |
| 17 | + |
| 18 | +import com.reandroid.apk.ApkModule; |
| 19 | +import com.reandroid.apk.DexFileInputSource; |
| 20 | +import com.reandroid.app.AndroidManifest; |
| 21 | +import com.reandroid.archive.block.CentralEntryHeader; |
| 22 | +import com.reandroid.archive.block.DataDescriptor; |
| 23 | +import com.reandroid.archive.block.LocalFileHeader; |
| 24 | +import com.reandroid.archive.writer.ApkFileWriter; |
| 25 | +import com.reandroid.archive.writer.HeaderInterceptor; |
| 26 | +import com.reandroid.arsc.chunk.TableBlock; |
| 27 | + |
| 28 | +import java.io.File; |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.HashSet; |
| 31 | +import java.util.Set; |
| 32 | + |
| 33 | +public class ProtectedFileWriter implements HeaderInterceptor { |
| 34 | + |
| 35 | + private final ApkModule apkModule; |
| 36 | + private final File file; |
| 37 | + private final Set<String> mProtectedFiles; |
| 38 | + |
| 39 | + public ProtectedFileWriter(ApkModule apkModule, File file) { |
| 40 | + this.apkModule = apkModule; |
| 41 | + this.file = file; |
| 42 | + this.mProtectedFiles = new HashSet<>(); |
| 43 | + } |
| 44 | + |
| 45 | + public void write() throws IOException { |
| 46 | + ApkFileWriter writer = apkModule.createApkFileWriter(this.file); |
| 47 | + writer.getInterceptorChain().setHeaderInterceptor(this); |
| 48 | + writer.write(); |
| 49 | + writer.close(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void onWriteLfh(LocalFileHeader lfh) { |
| 54 | + String name = lfh.getFileName(); |
| 55 | + if (needsProtection(name)) { |
| 56 | + mProtectedFiles.add(name); |
| 57 | + lfh.getGeneralPurposeFlag().setEncryption(true); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void onWriteDD(DataDescriptor dataDescriptor) { |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void onWriteCeh(CentralEntryHeader ceh) { |
| 67 | + if (mProtectedFiles.contains(ceh.getFileName())) { |
| 68 | + ceh.getGeneralPurposeFlag().setEncryption(true); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private boolean needsProtection(String name) { |
| 73 | + if (AndroidManifest.FILE_NAME.equals(name)) { |
| 74 | + return true; |
| 75 | + } |
| 76 | + if (TableBlock.FILE_NAME.equals(name)) { |
| 77 | + return true; |
| 78 | + } |
| 79 | + if (name.startsWith("lib/") && name.endsWith(".so")) { |
| 80 | + return true; |
| 81 | + } |
| 82 | + return DexFileInputSource.isDexName(name); |
| 83 | + } |
| 84 | +} |
0 commit comments