diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021FirmwareInfo.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021FirmwareInfo.java index d62796450..e0bc3ac75 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021FirmwareInfo.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021FirmwareInfo.java @@ -29,16 +29,12 @@ import java.util.Arrays; import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions; import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; -import nodomain.freeyourgadget.gadgetbridge.util.ZipUtils; +import nodomain.freeyourgadget.gadgetbridge.util.ZipFile; public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo { private static final Logger LOG = LoggerFactory.getLogger(Huami2021FirmwareInfo.class); - public static final byte[] ZIP_HEADER = new byte[]{ - 0x50, 0x4B, 0x03, 0x04 - }; - public Huami2021FirmwareInfo(final byte[] bytes) { super(bytes); } @@ -56,43 +52,49 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo { @Override protected HuamiFirmwareType determineFirmwareType(final byte[] bytes) { if (ArrayUtils.equals(bytes, UIHHContainer.UIHH_HEADER, 0)) { - final UIHHContainer uihh = UIHHContainer.fromRawBytes(bytes); - if (uihh == null) { - LOG.warn("Invalid UIHH file"); - return HuamiFirmwareType.INVALID; - } - - UIHHContainer.FileEntry uihhFirmwareZipFile = null; - boolean hasChangelog = false; - for (final UIHHContainer.FileEntry file : uihh.getFiles()) { - switch(file.getType()) { - case FIRMWARE_ZIP: - uihhFirmwareZipFile = file; - continue; - case FIRMWARE_CHANGELOG: - hasChangelog = true; - continue; - default: - LOG.warn("Unexpected file for {}", file.getType()); - } - } - - if (uihhFirmwareZipFile != null && hasChangelog) { - final byte[] firmwareBin = ZipUtils.getFileFromZip(uihhFirmwareZipFile.getContent(), "META/firmware.bin"); - - if (isCompatibleFirmwareBin(firmwareBin)) { - return HuamiFirmwareType.FIRMWARE_UIHH_2021_ZIP_WITH_CHANGELOG; - } - } + return handleUihhPackage(bytes); + } else if (ZipFile.isZipFile(bytes)) { + return handleZipPackage(bytes); + } else { + return HuamiFirmwareType.INVALID; + } + } + private HuamiFirmwareType handleUihhPackage(byte[] bytes) { + final UIHHContainer uihh = UIHHContainer.fromRawBytes(bytes); + if (uihh == null) { + LOG.warn("Invalid UIHH file"); return HuamiFirmwareType.INVALID; } - if (!ArrayUtils.equals(bytes, ZIP_HEADER, 0)) { - return HuamiFirmwareType.INVALID; + UIHHContainer.FileEntry uihhFirmwareZipFile = null; + boolean hasChangelog = false; + for (final UIHHContainer.FileEntry file : uihh.getFiles()) { + switch(file.getType()) { + case FIRMWARE_ZIP: + uihhFirmwareZipFile = file; + continue; + case FIRMWARE_CHANGELOG: + hasChangelog = true; + continue; + default: + LOG.warn("Unexpected file for {}", file.getType()); + } } - final byte[] firmwareBin = ZipUtils.getFileFromZip(bytes, "META/firmware.bin"); + if (uihhFirmwareZipFile != null && hasChangelog) { + byte[] firmwareBin = ZipFile.tryReadFileQuick(uihhFirmwareZipFile.getContent(), "META/firmware.bin"); + + if (isCompatibleFirmwareBin(firmwareBin)) { + return HuamiFirmwareType.FIRMWARE_UIHH_2021_ZIP_WITH_CHANGELOG; + } + } + + return HuamiFirmwareType.INVALID; + } + + private HuamiFirmwareType handleZipPackage(byte[] bytes) { + final byte[] firmwareBin = ZipFile.tryReadFileQuick(bytes, "META/firmware.bin"); if (isCompatibleFirmwareBin(firmwareBin)) { return HuamiFirmwareType.FIRMWARE; } @@ -191,7 +193,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo { } public String getFirmwareVersion(final byte[] fwbytes) { - final byte[] firmwareBin = ZipUtils.getFileFromZip(fwbytes, "META/firmware.bin"); + final byte[] firmwareBin = ZipFile.tryReadFileQuick(fwbytes, "META/firmware.bin"); if (firmwareBin == null) { LOG.warn("Failed to read firmware.bin"); @@ -224,7 +226,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo { } public String getAppName() { - final byte[] appJsonBin = ZipUtils.getFileFromZip(getBytes(), "app.json"); + final byte[] appJsonBin = ZipFile.tryReadFileQuick(getBytes(), "app.json"); if (appJsonBin == null) { LOG.warn("Failed to get app.json from zip"); return null; @@ -248,7 +250,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo { } public String getAppType() { - final byte[] appJsonBin = ZipUtils.getFileFromZip(getBytes(), "app.json"); + final byte[] appJsonBin = ZipFile.tryReadFileQuick(getBytes(), "app.json"); if (appJsonBin == null) { LOG.warn("Failed to get app.json from zip"); return null;