From 252f3f52698a85c74914d46942e868a52ce7720f Mon Sep 17 00:00:00 2001 From: z30034863 Date: Sat, 12 Oct 2024 18:03:47 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8A=A0=E5=AF=86?= =?UTF-8?q?=E6=96=87=E4=BB=B6encrypt.json=E6=89=93=E5=8C=85=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: z30034863 --- adapter/ohos/CommandParser.java | 5 ++++ adapter/ohos/Compressor.java | 18 ++++++++++-- adapter/ohos/JsonUtil.java | 11 +++++-- adapter/ohos/UncompressEntrance.java | 43 ++++++++++++++-------------- adapter/ohos/Utility.java | 11 +++++++ 5 files changed, 63 insertions(+), 25 deletions(-) diff --git a/adapter/ohos/CommandParser.java b/adapter/ohos/CommandParser.java index a12189b1..8084376f 100644 --- a/adapter/ohos/CommandParser.java +++ b/adapter/ohos/CommandParser.java @@ -62,6 +62,7 @@ public class CommandParser { private static final String CMD_FORCE = "--force"; private static final String CMD_OUT_PATH = "--out-path"; private static final String CMD_PACK_INFO_PATH = "--pack-info-path"; + private static final String CMD_ENCRYPT_PATH = "--encrypt-path"; private static final String CMD_BIN_PATH = "--bin-path"; private static final String CMD_JAR_PATH = "--jar-path"; private static final String CMD_TXT_PATH = "--txt-path"; @@ -215,6 +216,10 @@ public class CommandParser { entry.getKey().setPackInfoPath(entry.getValue()); return true; }); + commandFuncs.put(CMD_ENCRYPT_PATH, entry -> { + entry.getKey().setEncryptPath(entry.getValue()); + return true; + }); commandFuncs.put(CMD_BIN_PATH, entry -> { entry.getKey().setBinPath(entry.getValue()); return true; diff --git a/adapter/ohos/Compressor.java b/adapter/ohos/Compressor.java index 9bf2d5ca..68879722 100644 --- a/adapter/ohos/Compressor.java +++ b/adapter/ohos/Compressor.java @@ -139,6 +139,7 @@ public class Compressor { private static final String ATOMIC_SERVICE = "atomicService"; private static final String RAW_FILE_PATH = "resources/rawfile"; private static final String RES_FILE_PATH = "resources/resfile"; + private static final String ENCRYPT_JSON_FILE = "encrypt.json"; private static final String SUMMARY = "summary"; private static final String VERSION_CODE = "versionCode"; private static final String VERSION_NAME = "versionName"; @@ -1242,8 +1243,14 @@ public class Compressor { } File file = new File(utility.getPackInfoPath()); compressFile(utility, file, NULL_DIR_NAME, false); + //compressing encrypt.json file + if (!utility.getEncryptPath().isEmpty()) { + pathToFile(utility, utility.getEncryptPath(), NULL_DIR_NAME, false); + } else { + LOG.info("Compressor::compressAppMode has no encrypt.json"); + } } catch (BundleException e) { - LOG.error("Compressor::compressAppMode compress failed."); + LOG.error("Compressor::compressAppMode compress failed. msg: " + e.getMessage()); throw new BundleException("Compressor::compressAppMode compress failed."); } finally { // delete temp file @@ -1298,6 +1305,12 @@ public class Compressor { private void packFastApp(Utility utility, List fileList) throws BundleException { // pack.info pathToFile(utility, utility.getPackInfoPath(), NULL_DIR_NAME, false); + // encrypt.json + if (!utility.getEncryptPath().isEmpty()) { + pathToFile(utility, utility.getPackInfoPath(), NULL_DIR_NAME, false); + } else { + LOG.info("Compressor::packFastApp has no encrypt.json"); + } // hap/hsp for (String hapPath : fileList) { HapVerifyInfo hapVerifyInfo = hapVerifyInfoMap.get(getFileNameByPath(hapPath)); @@ -2425,7 +2438,7 @@ public class Compressor { if (!entryName.contains(RAW_FILE_PATH) && !entryName.contains(RES_FILE_PATH) && srcFile.getName().toLowerCase(Locale.ENGLISH).endsWith(JSON_SUFFIX)) { zipEntry.setMethod(ZipEntry.STORED); - if (jsonSpecialProcess(utility, srcFile, zipEntry)) { + if (!entryName.equals(ENCRYPT_JSON_FILE) && jsonSpecialProcess(utility, srcFile, zipEntry)) { return; } } @@ -2519,6 +2532,7 @@ public class Compressor { count = fileInputStream.read(buffer); } } catch (FileNotFoundException ignored) { + LOG.error("Compressor::getCrcFromFile FileNotFoundException : " + ignored.getMessage()); throw new BundleException("Get Crc from file failed: " + file.getName()); } catch (IOException exception) { LOG.error("Compressor::getCrcFromFile io exception: " + exception.getMessage()); diff --git a/adapter/ohos/JsonUtil.java b/adapter/ohos/JsonUtil.java index a45d3e6d..35a0f97a 100644 --- a/adapter/ohos/JsonUtil.java +++ b/adapter/ohos/JsonUtil.java @@ -25,6 +25,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONException; +import com.sun.xml.internal.ws.util.StringUtils; /** * Json Util. @@ -1449,7 +1450,13 @@ public class JsonUtil { String fileName = moduleMetadataInfo.resource; fileName = fileName.replace(PROFILE, ""); fileName = fileName + JSON_SUFFIX; - moduleMetadataInfo.resource = profileJson.get(fileName); + String resourceValue = profileJson.get(fileName); + if (resourceValue == null) { + LOG.warning("JsonUtil::parseModuleMetadata : metadata.resource value is null"); + moduleMetadataInfo.resource = ""; + } else { + moduleMetadataInfo.resource = profileJson.get(fileName); + } } return moduleMetadataInfo; } @@ -1568,7 +1575,7 @@ public class JsonUtil { // find shortcut and parse in metadata for (ModuleMetadataInfo moduleMetadataInfo : moduleMetadataInfos) { String jsonStr = moduleMetadataInfo.resource; - if (jsonStr.isEmpty()) { + if (jsonStr == null || jsonStr.isEmpty()) { continue; } try { diff --git a/adapter/ohos/UncompressEntrance.java b/adapter/ohos/UncompressEntrance.java index d0ee32ee..c3f02865 100644 --- a/adapter/ohos/UncompressEntrance.java +++ b/adapter/ohos/UncompressEntrance.java @@ -438,26 +438,27 @@ public class UncompressEntrance { * @param args command line */ public static void main(String[] args) { - Utility utility = new Utility(); - - if (!CommandParser.commandParser(utility, args)) { - LOG.error("UncompressEntrance::main exit, parser failed"); - ShowHelp.uncompressHelp(); - System.exit(EXIT_STATUS_EXCEPTION); - } - - if (!UncompressVerify.commandVerify(utility)) { - LOG.error("UncompressEntrance::main exit, verify failed"); - ShowHelp.uncompressHelp(); - System.exit(EXIT_STATUS_EXCEPTION); - } - - if (!Uncompress.unpackageProcess(utility)) { - LOG.error("UncompressEntrance::main exit, uncompress failed"); - ShowHelp.uncompressHelp(); - System.exit(EXIT_STATUS_EXCEPTION); - } - - System.exit(EXIT_STATUS_NORMAL); + parseHap("D:\\Project\\MyApplication0926\\entry\\build\\default\\outputs\\default\\entry-default-unsigned.hap"); +// Utility utility = new Utility(); +// +// if (!CommandParser.commandParser(utility, args)) { +// LOG.error("UncompressEntrance::main exit, parser failed"); +// ShowHelp.uncompressHelp(); +// System.exit(EXIT_STATUS_EXCEPTION); +// } +// +// if (!UncompressVerify.commandVerify(utility)) { +// LOG.error("UncompressEntrance::main exit, verify failed"); +// ShowHelp.uncompressHelp(); +// System.exit(EXIT_STATUS_EXCEPTION); +// } +// +// if (!Uncompress.unpackageProcess(utility)) { +// LOG.error("UncompressEntrance::main exit, uncompress failed"); +// ShowHelp.uncompressHelp(); +// System.exit(EXIT_STATUS_EXCEPTION); +// } +// +// System.exit(EXIT_STATUS_NORMAL); } } \ No newline at end of file diff --git a/adapter/ohos/Utility.java b/adapter/ohos/Utility.java index 5b8cd0aa..ed7e67c5 100644 --- a/adapter/ohos/Utility.java +++ b/adapter/ohos/Utility.java @@ -67,6 +67,7 @@ public class Utility { private String forceRewrite = "false"; private String outPath = ""; private String packInfoPath = ""; + private String encryptPath = ""; private String binPath = ""; private boolean isCompressNativeLibs = false; private String moduleName = ""; @@ -410,6 +411,16 @@ public class Utility { } } + public String getEncryptPath() { + return encryptPath; + } + + public void setEncryptPath(String encryptPath) { + if (!encryptPath.startsWith(CMD_PREFIX)) { + this.encryptPath = getFormattedPath(encryptPath); + } + } + public String getJarPath() { return jarPath; } -- Gitee From 2b9a72f4d5ee420d9163b67bfd2eb35964c49c35 Mon Sep 17 00:00:00 2001 From: z30034863 Date: Mon, 21 Oct 2024 17:12:55 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E5=8A=A0=E5=AF=86=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=89=93=E5=8C=85=E8=83=BD=E5=8A=9B=E9=9C=80=E6=B1=82=EF=BC=8C?= =?UTF-8?q?=E8=A1=A5=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: z30034863 --- adapter/ohos/CompressVerify.java | 26 +++++++++++++++++ adapter/ohos/Compressor.java | 29 ++++++++++--------- adapter/ohos/Constants.java | 5 ++++ adapter/ohos/PackageUtil.java | 5 ++++ adapter/ohos/ShowHelp.java | 3 +- adapter/ohos/UncompressEntrance.java | 43 ++++++++++++++-------------- 6 files changed, 74 insertions(+), 37 deletions(-) diff --git a/adapter/ohos/CompressVerify.java b/adapter/ohos/CompressVerify.java index 88d406d6..03f057a3 100644 --- a/adapter/ohos/CompressVerify.java +++ b/adapter/ohos/CompressVerify.java @@ -505,6 +505,11 @@ public class CompressVerify { return false; } + if (!isValidEncryptJsonFile(utility)) { + LOG.error("CompressVerify::isVerifyValidInAppMode encrypt-path is invalid."); + return false; + } + if (!utility.getSignaturePath().isEmpty() && !(new File(utility.getSignaturePath())).isFile()) { LOG.error("CompressVerify::isArgsValidInAppMode signature-path is invalid."); return false; @@ -609,6 +614,11 @@ public class CompressVerify { } } + if (!isValidEncryptJsonFile(utility)) { + LOG.error("CompressVerify::isVerifyValidInMultiAppMode encrypt-path is invalid."); + return false; + } + File outFile = new File(utility.getOutPath()); if (("false".equals(utility.getForceRewrite())) && outFile.exists()) { LOG.error("CompressVerify::isVerifyValidInMultiAppMode out file already existed."); @@ -1186,4 +1196,20 @@ public class CompressVerify { return false; } } + + /** + * Indicates whether the "--encrypt-path" parameter is valid. + * + * @param utility - compress parameters + * @return true if "--encrypt-path" param exists and the file name is encrypt.json, or the "--encrypt-path" + * param is empty, or has no "--encrypt-path" param + * false other situations + */ + private static boolean isValidEncryptJsonFile(Utility utility) { + if (!utility.getEncryptPath().isEmpty()) { + File fileEncryptJson = new File(utility.getEncryptPath()); + return fileEncryptJson.isFile() && Constants.FILE_ENCRYPT_JSON.equals(fileEncryptJson.getName()); + } + return true; + } } diff --git a/adapter/ohos/Compressor.java b/adapter/ohos/Compressor.java index 68879722..becc0d2a 100644 --- a/adapter/ohos/Compressor.java +++ b/adapter/ohos/Compressor.java @@ -139,7 +139,6 @@ public class Compressor { private static final String ATOMIC_SERVICE = "atomicService"; private static final String RAW_FILE_PATH = "resources/rawfile"; private static final String RES_FILE_PATH = "resources/resfile"; - private static final String ENCRYPT_JSON_FILE = "encrypt.json"; private static final String SUMMARY = "summary"; private static final String VERSION_CODE = "versionCode"; private static final String VERSION_NAME = "versionName"; @@ -1243,12 +1242,8 @@ public class Compressor { } File file = new File(utility.getPackInfoPath()); compressFile(utility, file, NULL_DIR_NAME, false); - //compressing encrypt.json file - if (!utility.getEncryptPath().isEmpty()) { - pathToFile(utility, utility.getEncryptPath(), NULL_DIR_NAME, false); - } else { - LOG.info("Compressor::compressAppMode has no encrypt.json"); - } + //pack encrypt.json file + packEncryptJsonFile(utility); } catch (BundleException e) { LOG.error("Compressor::compressAppMode compress failed. msg: " + e.getMessage()); throw new BundleException("Compressor::compressAppMode compress failed."); @@ -1305,12 +1300,8 @@ public class Compressor { private void packFastApp(Utility utility, List fileList) throws BundleException { // pack.info pathToFile(utility, utility.getPackInfoPath(), NULL_DIR_NAME, false); - // encrypt.json - if (!utility.getEncryptPath().isEmpty()) { - pathToFile(utility, utility.getPackInfoPath(), NULL_DIR_NAME, false); - } else { - LOG.info("Compressor::packFastApp has no encrypt.json"); - } + // pack encrypt.json file + packEncryptJsonFile(utility); // hap/hsp for (String hapPath : fileList) { HapVerifyInfo hapVerifyInfo = hapVerifyInfoMap.get(getFileNameByPath(hapPath)); @@ -1385,6 +1376,8 @@ public class Compressor { } File file = new File(finalPackInfoPath); compressFile(utility, file, NULL_DIR_NAME, false); + //pack encrypt.json file + packEncryptJsonFile(utility); } catch (BundleException | IOException exception) { String errMsg = "Compressor::compressAppModeForMultiProject file failed: " + exception.getMessage(); LOG.error(errMsg); @@ -2438,7 +2431,7 @@ public class Compressor { if (!entryName.contains(RAW_FILE_PATH) && !entryName.contains(RES_FILE_PATH) && srcFile.getName().toLowerCase(Locale.ENGLISH).endsWith(JSON_SUFFIX)) { zipEntry.setMethod(ZipEntry.STORED); - if (!entryName.equals(ENCRYPT_JSON_FILE) && jsonSpecialProcess(utility, srcFile, zipEntry)) { + if (!entryName.equals(Constants.FILE_ENCRYPT_JSON) && jsonSpecialProcess(utility, srcFile, zipEntry)) { return; } } @@ -3504,4 +3497,12 @@ public class Compressor { Path filePath = Paths.get(path); return filePath.getFileName().toString(); } + + private void packEncryptJsonFile(Utility utility) throws BundleException { + if (!utility.getEncryptPath().isEmpty()) { + pathToFile(utility, utility.getEncryptPath(), NULL_DIR_NAME, false); + } else { + LOG.info("Compressor::packEncryptJsonFile has no encrypt.json"); + } + } } diff --git a/adapter/ohos/Constants.java b/adapter/ohos/Constants.java index f6a31bfd..98d2cf23 100644 --- a/adapter/ohos/Constants.java +++ b/adapter/ohos/Constants.java @@ -146,6 +146,11 @@ public class Constants { */ public static final String FILE_PACK_INFO = "pack.info"; + /** + * Indicates the file name: encrypt.json + */ + public static final String FILE_ENCRYPT_JSON = "encrypt.json"; + /** * Indicates the file name: module.json */ diff --git a/adapter/ohos/PackageUtil.java b/adapter/ohos/PackageUtil.java index ca2d45cb..0e565002 100644 --- a/adapter/ohos/PackageUtil.java +++ b/adapter/ohos/PackageUtil.java @@ -798,6 +798,11 @@ public class PackageUtil { LOG.error("CompressVerify::isVerifyValidInFastAppMode pack.info is invalid."); return false; } + if (!utility.getEncryptPath().isEmpty() + && !isFileValid(utility.getEncryptPath(), Constants.FILE_ENCRYPT_JSON)) { + LOG.error("CompressVerify::isVerifyValidInFastAppMode encrypt-path is invalid."); + return false; + } Path outPath = Paths.get(utility.getOutPath()); if (utility.getForceRewrite().equals(Constants.FALSE) && Files.exists(outPath)) { LOG.error("CompressVerify::isVerifyValidInFastAppMode out file already existed."); diff --git a/adapter/ohos/ShowHelp.java b/adapter/ohos/ShowHelp.java index 0d54104d..bb0430db 100644 --- a/adapter/ohos/ShowHelp.java +++ b/adapter/ohos/ShowHelp.java @@ -86,7 +86,8 @@ public interface ShowHelp { " --force default false; if true, force delete destination\n" + " out file if exists.\n" + " --signature-path signature file path.\n" + - " --certificate-path certificate file path."); + " --certificate-path certificate file path.\n" + + " --encrypt-path encrypt.json file path."); } /** diff --git a/adapter/ohos/UncompressEntrance.java b/adapter/ohos/UncompressEntrance.java index c3f02865..d0ee32ee 100644 --- a/adapter/ohos/UncompressEntrance.java +++ b/adapter/ohos/UncompressEntrance.java @@ -438,27 +438,26 @@ public class UncompressEntrance { * @param args command line */ public static void main(String[] args) { - parseHap("D:\\Project\\MyApplication0926\\entry\\build\\default\\outputs\\default\\entry-default-unsigned.hap"); -// Utility utility = new Utility(); -// -// if (!CommandParser.commandParser(utility, args)) { -// LOG.error("UncompressEntrance::main exit, parser failed"); -// ShowHelp.uncompressHelp(); -// System.exit(EXIT_STATUS_EXCEPTION); -// } -// -// if (!UncompressVerify.commandVerify(utility)) { -// LOG.error("UncompressEntrance::main exit, verify failed"); -// ShowHelp.uncompressHelp(); -// System.exit(EXIT_STATUS_EXCEPTION); -// } -// -// if (!Uncompress.unpackageProcess(utility)) { -// LOG.error("UncompressEntrance::main exit, uncompress failed"); -// ShowHelp.uncompressHelp(); -// System.exit(EXIT_STATUS_EXCEPTION); -// } -// -// System.exit(EXIT_STATUS_NORMAL); + Utility utility = new Utility(); + + if (!CommandParser.commandParser(utility, args)) { + LOG.error("UncompressEntrance::main exit, parser failed"); + ShowHelp.uncompressHelp(); + System.exit(EXIT_STATUS_EXCEPTION); + } + + if (!UncompressVerify.commandVerify(utility)) { + LOG.error("UncompressEntrance::main exit, verify failed"); + ShowHelp.uncompressHelp(); + System.exit(EXIT_STATUS_EXCEPTION); + } + + if (!Uncompress.unpackageProcess(utility)) { + LOG.error("UncompressEntrance::main exit, uncompress failed"); + ShowHelp.uncompressHelp(); + System.exit(EXIT_STATUS_EXCEPTION); + } + + System.exit(EXIT_STATUS_NORMAL); } } \ No newline at end of file -- Gitee From d529276950a21d5ffa8cdb64b0ac81c6db17bc91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=B8=B0=E5=A3=AE?= Date: Tue, 22 Oct 2024 02:01:58 +0000 Subject: [PATCH 3/7] update adapter/ohos/JsonUtil.java. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张丰壮 --- adapter/ohos/JsonUtil.java | 1 - 1 file changed, 1 deletion(-) diff --git a/adapter/ohos/JsonUtil.java b/adapter/ohos/JsonUtil.java index 35a0f97a..b0a8f510 100644 --- a/adapter/ohos/JsonUtil.java +++ b/adapter/ohos/JsonUtil.java @@ -25,7 +25,6 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONException; -import com.sun.xml.internal.ws.util.StringUtils; /** * Json Util. -- Gitee From 62ff29471777f55f21a7ffe1b157738d4e5dda0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=B8=B0=E5=A3=AE?= Date: Tue, 22 Oct 2024 09:22:30 +0000 Subject: [PATCH 4/7] update adapter/ohos/Compressor.java. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张丰壮 --- adapter/ohos/Compressor.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/adapter/ohos/Compressor.java b/adapter/ohos/Compressor.java index becc0d2a..86f92563 100644 --- a/adapter/ohos/Compressor.java +++ b/adapter/ohos/Compressor.java @@ -2428,10 +2428,12 @@ public class Compressor { String entryName = (baseDir + srcFile.getName()).replace(File.separator, LINUX_FILE_SEPARATOR); ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName); isEntryOpen = true; - if (!entryName.contains(RAW_FILE_PATH) && !entryName.contains(RES_FILE_PATH) && - srcFile.getName().toLowerCase(Locale.ENGLISH).endsWith(JSON_SUFFIX)) { + if (!entryName.contains(RAW_FILE_PATH) + && !entryName.contains(RES_FILE_PATH) + && srcFile.getName().toLowerCase(Locale.ENGLISH).endsWith(JSON_SUFFIX) + && !entryName.equals(Constants.FILE_ENCRYPT_JSON)) { zipEntry.setMethod(ZipEntry.STORED); - if (!entryName.equals(Constants.FILE_ENCRYPT_JSON) && jsonSpecialProcess(utility, srcFile, zipEntry)) { + if (jsonSpecialProcess(utility, srcFile, zipEntry)) { return; } } -- Gitee From f0624e793472bb2b28d00dc16ea6e614265a48d5 Mon Sep 17 00:00:00 2001 From: z30034863 Date: Tue, 22 Oct 2024 17:35:12 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E5=8A=A0=E5=AF=86=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=89=93=E5=8C=85=E9=9C=80=E6=B1=82=EF=BC=8C=E8=B5=84=E6=96=99?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: z30034863 --- README_zh.md | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/README_zh.md b/README_zh.md index 6a1cded1..c2fa4fa1 100644 --- a/README_zh.md +++ b/README_zh.md @@ -93,6 +93,7 @@ java -jar app_packing_tool.jar --mode app --hap-path