From 031c0d95dd3c75ff2470e21813650ae625194ce7 Mon Sep 17 00:00:00 2001 From: "fenghao (P)" Date: Sat, 10 Jul 2021 20:43:06 +0800 Subject: [PATCH 1/4] =?UTF-8?q?IssueNo:#I3SH1G=20Description:=E6=89=93?= =?UTF-8?q?=E5=8C=85app=E5=9C=BA=E6=99=AF=EF=BC=8C=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E6=89=93=E5=8C=85hap=E6=97=B6=EF=BC=8C=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E6=A0=B9=E6=8D=AEcompressNativeLibs=E5=88=A4=E6=96=AD=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E5=8E=8B=E7=BC=A9so=20Sig:aafwk=20Feature=20or=20Bugf?= =?UTF-8?q?ix:=20Bugfig=20Binary=20Source:=20No?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fenghao (P) --- adapter/ohos/Compressor.java | 1 - adapter/ohos/FileUtils.java | 1 - adapter/ohos/Uncompress.java | 1 - 3 files changed, 3 deletions(-) diff --git a/adapter/ohos/Compressor.java b/adapter/ohos/Compressor.java index 1e3a5d2b..e8e0ea52 100644 --- a/adapter/ohos/Compressor.java +++ b/adapter/ohos/Compressor.java @@ -105,7 +105,6 @@ public class Compressor { // set buffer size of each read private static final int BUFFER_SIZE = 10 * 1024; - private static final long TOO_MANY_SIZE = 1024; private static final long TOO_BIG_SIZE = 0x6400000; private static final Log LOG = new Log(Compressor.class.toString()); private static String versionCode = ""; diff --git a/adapter/ohos/FileUtils.java b/adapter/ohos/FileUtils.java index 21c42fb4..ca31384b 100644 --- a/adapter/ohos/FileUtils.java +++ b/adapter/ohos/FileUtils.java @@ -40,7 +40,6 @@ import java.util.zip.ZipFile; public class FileUtils { private static final int BUFFER_SIZE = 1024; private static final long TOO_BIG_SIZE = 0x6400000; - private static final long TOO_MANY_SIZE = 1024; private static final Log LOG = new Log(FileUtils.class.toString()); /** diff --git a/adapter/ohos/Uncompress.java b/adapter/ohos/Uncompress.java index 7e3dc728..cd649efc 100644 --- a/adapter/ohos/Uncompress.java +++ b/adapter/ohos/Uncompress.java @@ -57,7 +57,6 @@ public class Uncompress { private static final int READ_BUFFER_SIZE = 1024; private static final int BUFFER_SIZE = 10 * 1024; private static final long FILE_TIME = 1546272000000L; - private static final long TOO_MANY_SIZE = 65536; private static final String LIBS_DIR_NAME = "libs"; private static final String CUT_ENTRY_FILENAME = "cut_entry.apk"; private static final String SO_SUFFIX = ".so"; -- Gitee From cfe08cfc6252fcedb28bbaefc151185a0588c9d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=AF=E7=9A=93?= Date: Sat, 10 Jul 2021 12:57:26 +0000 Subject: [PATCH 2/4] update adapter/ohos/Compressor.java. --- adapter/ohos/Compressor.java | 118 ++++++++++++++++++++++++++++------- 1 file changed, 95 insertions(+), 23 deletions(-) diff --git a/adapter/ohos/Compressor.java b/adapter/ohos/Compressor.java index e8e0ea52..d8c9fc53 100644 --- a/adapter/ohos/Compressor.java +++ b/adapter/ohos/Compressor.java @@ -25,6 +25,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.nio.file.attribute.FileTime; import java.util.Locale; @@ -54,6 +55,7 @@ public class Compressor { private static final String CONFIG_JSON = "config.json"; private static final String CODE = "code"; private static final String NAME = "name"; + private static final String VERSION = "\"version\""; private static final String NULL_DIR_NAME = ""; private static final String RES_DIR_NAME = "res/"; private static final String RESOURCES_DIR_NAME = "resources/"; @@ -105,7 +107,6 @@ public class Compressor { // set buffer size of each read private static final int BUFFER_SIZE = 10 * 1024; - private static final long TOO_BIG_SIZE = 0x6400000; private static final Log LOG = new Log(Compressor.class.toString()); private static String versionCode = ""; private static String versionName = ""; @@ -340,9 +341,12 @@ public class Compressor { String[] str = temp[temp.length - 1].split("\\."); String outPathString = path[0] + str[0]; fileList.add(outPathString); - unzip(hapPathItem, outPathString); - copyFileUsingFileStreams(utility.getPackInfoPath(), outPathString); - zipFile(outPathString); + try { + compressPackinfoIntoHap(hapPathItem, outPathString, utility.getPackInfoPath()); + } catch ( IOException e) { + LOG.error("Compressor::compressAppMode compress pack.info into hap failed"); + throw new BundleException("Compressor::compressAppMode compress pack.info into hap failed"); + } } for (String hapPath : fileList) { @@ -369,6 +373,54 @@ public class Compressor { compressFile(utility, file, NULL_DIR_NAME, false); } + private void copy(InputStream input, OutputStream output) throws IOException { + int bytesRead; + byte[] data = new byte[BUFFER_SIZE]; + while ((bytesRead = input.read(data, 0, BUFFER_SIZE)) != -1) { + output.write(data, 0, bytesRead); + } + } + + private void compressPackinfoIntoHap(String hapPathItem, String outPathString, String packInfo) + throws FileNotFoundException, IOException, BundleException { + ZipFile sourceHapFile = new ZipFile(hapPathItem); + ZipOutputStream append = new ZipOutputStream(new FileOutputStream(outPathString + ".hap")); + try { + Enumeration entries = sourceHapFile.entries(); + while (entries.hasMoreElements()) { + ZipEntry zipEntry = entries.nextElement(); + if (zipEntry.getName() != null && PACKINFO_NAME.equals(zipEntry.getName())) { + continue; + } + append.putNextEntry(zipEntry); + if (!zipEntry.isDirectory()) { + copy(sourceHapFile.getInputStream(zipEntry), append); + } + append.closeEntry(); + } + File packInfoFile = new File(packInfo); + ZipEntry zipEntry = getStoredZipEntry(packInfoFile, PACKINFO_NAME); + append.putNextEntry(zipEntry); + FileInputStream in = new FileInputStream(packInfoFile); + try { + byte[] buf = new byte[BUFFER_SIZE]; + int len; + while ((len = in.read(buf)) != -1) { + append.write(buf, 0, len); + } + } finally { + in.close(); + } + append.closeEntry(); + } catch (IOException exception) { + LOG.error("Compressor::compressPackinfoIntoHap io exception"); + throw new BundleException("Compressor::compressPackinfoIntoHap io exception"); + } finally { + sourceHapFile.close(); + append.close(); + } + } + /** * zipFile * @@ -461,9 +513,6 @@ public class Compressor { int entriesNum = 0; while (entries.hasMoreElements()) { entriesNum++; - if (entriesNum > TOO_MANY_SIZE) { - throw new IOException("dataTransferByInput failed entry num is too many"); - } ZipEntry entry = entries.nextElement(); if (entry == null) { continue; @@ -514,10 +563,6 @@ public class Compressor { while ((count = bis.read(data, 0, BUFFER_SIZE)) != -1) { bos.write(data, 0, count); total += count; - if (total > TOO_BIG_SIZE) { - LOG.error("FileUtils::unzip exception: " + entry.getName() + " is too big."); - throw new IOException("dataTransferByInput failed entry num is too many"); - } } } @@ -1115,27 +1160,27 @@ public class Compressor { if ((hapPath == null) || (hapPath.isEmpty()) || (!hapPath.contains(baseDir))) { continue; } - String configJson = obtainVersion(srcFile, hapPath); - String str = obtainInnerVersionCode(configJson); - if ((str == null) || (str.isEmpty())) { + String versionStr = obtainVersion(srcFile, hapPath); + String code = obtainInnerVersionCode(versionStr); + if ((code == null) || (code.isEmpty())) { LOG.error("Compressor::checkVersionInHaps version code is null or empty"); return false; } - if (!versionCode.isEmpty() && !versionCode.equals(str)) { + if (!versionCode.isEmpty() && !versionCode.equals(code)) { LOG.error("Compressor::checkVersionInHaps some haps with different version code"); return false; } - String nameStr = obtainInnerVersionName(configJson); - if ((nameStr == null) || (nameStr.isEmpty())) { + String name = obtainInnerVersionName(versionStr); + if ((name == null) || (name.isEmpty())) { LOG.error("Compressor::checkVersionInHaps version name is null or empty"); return false; } - if (!versionName.isEmpty() && !versionName.equals(nameStr)) { + if (!versionName.isEmpty() && !versionName.equals(name)) { LOG.error("Compressor::checkVersionInHaps some haps with different version name"); return false; } - versionCode = str; - versionName = nameStr; + versionCode = code; + versionName = name; } } catch (BundleException exception) { LOG.error("Compressor::checkVersionInHaps io exception: " + exception.getMessage()); @@ -1296,7 +1341,7 @@ public class Compressor { InputStreamReader reader = null; BufferedReader br = null; ZipEntry entry = null; - String versionStr = ""; + String configJson = ""; try { zipFile = new ZipFile(srcFile); zipInput = new FileInputStream(baseDir); @@ -1308,7 +1353,7 @@ public class Compressor { reader = new InputStreamReader(inputStream); br = new BufferedReader(reader); if (br != null) { - versionStr = br.readLine(); + configJson = br.readLine(); break; } } @@ -1325,7 +1370,34 @@ public class Compressor { Utility.closeStream(reader); Utility.closeStream(br); } - return versionStr; + return obtainInnerVersion(configJson); + } + + private String obtainInnerVersion(String configJson) throws BundleException { + try { + if (configJson != null) { + int indexOfVersion = configJson.indexOf(VERSION); + if (indexOfVersion <= 0) { + LOG.error("Compressor::obtainInnerVersion obtain index of version failed"); + throw new BundleException("Compressor::obtainInnerVersion obtain index of version failed"); + } + int lastIndex = configJson.indexOf(JSON_END, indexOfVersion); + if (lastIndex <= 0) { + LOG.error("Compressor::obtainInnerVersion obtain last index failed"); + throw new BundleException("Compressor::obtainInnerVersion obtain last index failed"); + } + String version = configJson.substring(indexOfVersion, lastIndex + 1); + if (version == null || version.isEmpty()) { + LOG.error("Compressor::obtainInnerVersion version is null or empty"); + throw new BundleException("Compressor::obtainInnerVersion failed due to null or empty version!"); + } + return version.trim(); + } + } catch (BundleException exception) { + LOG.error("Compressor::obtainInnerVersion io exception: " + exception.getMessage()); + throw new BundleException("Compressor::obtainInnerVersion failed"); + } + return ""; } /** -- Gitee From b5d6d6d3424f7b064b6565c2c3ebb8edf2f9f5e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=AF=E7=9A=93?= Date: Sat, 10 Jul 2021 13:03:19 +0000 Subject: [PATCH 3/4] =?UTF-8?q?update=20adapter/ohos/Compressor.java.=20Is?= =?UTF-8?q?sueNo:#I3SH1G=20Description:=E6=89=93=E5=8C=85app=E5=9C=BA?= =?UTF-8?q?=E6=99=AF=EF=BC=8C=E9=87=8D=E6=96=B0=E6=89=93=E5=8C=85hap?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E9=9C=80=E8=A6=81=E6=A0=B9=E6=8D=AEcompressN?= =?UTF-8?q?ativeLibs=E5=88=A4=E6=96=AD=E6=98=AF=E5=90=A6=E5=8E=8B=E7=BC=A9?= =?UTF-8?q?so=20Sig:aafwk=20Feature=20or=20Bugfix:=20Bugfig=20Binary=20Sou?= =?UTF-8?q?rce:=20No?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fenghao (P) --- adapter/ohos/FileUtils.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/adapter/ohos/FileUtils.java b/adapter/ohos/FileUtils.java index ca31384b..4681a10e 100644 --- a/adapter/ohos/FileUtils.java +++ b/adapter/ohos/FileUtils.java @@ -39,7 +39,6 @@ import java.util.zip.ZipFile; */ public class FileUtils { private static final int BUFFER_SIZE = 1024; - private static final long TOO_BIG_SIZE = 0x6400000; private static final Log LOG = new Log(FileUtils.class.toString()); /** @@ -241,11 +240,6 @@ public class FileUtils { int entriesNum = 0; while (entries.hasMoreElements()) { entriesNum++; - if (entriesNum > TOO_MANY_SIZE) { - LOG.error("FileUtils::unzip exception: the entry num is too many."); - throw new IOException("dataTransferByInput failed entry num is too many"); - } - ZipEntry entry = entries.nextElement(); if (entry == null) { continue; @@ -271,11 +265,6 @@ public class FileUtils { while ((count = bis.read(data, 0, BUFFER_SIZE)) != -1) { bos.write(data, 0, count); total += count; - - if (total > TOO_BIG_SIZE) { - LOG.error("FileUtils::unzip exception: " + entry.getName() + " is too big."); - throw new IOException("dataTransferByInput failed entry num is too many"); - } } bos.flush(); -- Gitee From c0cceb12e606e1f52de4c12948694079f8a7be67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=AF=E7=9A=93?= Date: Sat, 10 Jul 2021 13:09:13 +0000 Subject: [PATCH 4/4] =?UTF-8?q?IssueNo:#I3SH1G=20Description:=E5=8F=96?= =?UTF-8?q?=E6=B6=88=E6=89=93=E5=8C=85=E9=99=90=E5=88=B6=20Sig:aafwk=20Fea?= =?UTF-8?q?ture=20or=20Bugfix:=20Bugfig=20Binary=20Source:=20No?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fenghao (P) --- adapter/ohos/Uncompress.java | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/adapter/ohos/Uncompress.java b/adapter/ohos/Uncompress.java index cd649efc..36589515 100644 --- a/adapter/ohos/Uncompress.java +++ b/adapter/ohos/Uncompress.java @@ -311,11 +311,6 @@ public class Uncompress { int entriesNum = 0; for (Enumeration entries = zipFile.entries(); entries.hasMoreElements(); ) { entriesNum++; - if (entriesNum > TOO_MANY_SIZE) { - LOG.error("Uncompress::unzipFromFile exception: the entry num is too many."); - throw new BundleException("unzipFromFile failed entry num is too many"); - } - ZipEntry entry = entries.nextElement(); String entryName = ""; if (entry == null || entry.getName().isEmpty()) { @@ -460,11 +455,6 @@ public class Uncompress { int entriesNum = 0; while ((entry = zipIn.getNextEntry()) != null) { entriesNum++; - if (entriesNum > TOO_MANY_SIZE) { - LOG.error("Uncompress::dataTransferByInput exception: the entry num is too many."); - throw new BundleException("dataTransferByInput failed entry num is too many"); - } - String entryName = entry.getName(); if (fileName.equals(entryName)) { byte[] data = new byte[BUFFER_SIZE]; @@ -506,11 +496,6 @@ public class Uncompress { int entriesNum = 0; for (Enumeration entries = zipFile.entries(); entries.hasMoreElements(); ) { entriesNum++; - if (entriesNum > TOO_MANY_SIZE) { - LOG.error("Uncompress::dataTransferAllFiles exception: the entry num is too many."); - throw new BundleException("dataTransferAllFiles failed entry num is too many"); - } - ZipEntry entry = entries.nextElement(); if (entry == null) { continue; @@ -539,11 +524,6 @@ public class Uncompress { try { for (Enumeration entries = zipFile.entries(); entries.hasMoreElements(); ) { entriesNum++; - if (entriesNum > TOO_MANY_SIZE) { - LOG.error("Uncompress::uncompress exception: the entry num is too many."); - throw new BundleException("uncompress failed entry num is too many"); - } - ZipEntry indexEntry = entries.nextElement(); if (indexEntry == null) { continue; @@ -660,11 +640,6 @@ public class Uncompress { int entriesNum = 0; while ((entry = zipIn.getNextEntry()) != null) { entriesNum++; - if (entriesNum > TOO_MANY_SIZE) { - LOG.error("Uncompress::uncompressByInput exception: the entry num is too many."); - throw new BundleException("uncompressByInput failed entry num is too many"); - } - if (entry.getName().toLowerCase().endsWith(RESOURCE_INDEX)) { hapZipInfo.setResDataBytes(getByte(zipIn)); continue; @@ -814,11 +789,6 @@ public class Uncompress { int entriesNum = 0; while ((entry = zipIn.getNextEntry()) != null) { entriesNum++; - if (entriesNum > TOO_MANY_SIZE) { - LOG.error("Uncompress::uncompressAllByInput exception: the entry num is too many."); - throw new BundleException("uncompressAllByInput failed entry num is too many"); - } - if (PACK_INFO.equals(entry.getName().toLowerCase(Locale.ENGLISH))) { bufferedReader = new BufferedReader(new InputStreamReader(zipIn)); String line; -- Gitee