一、分析OTA升级包校验过程需要先了解下zip文件结构
参考文档:
https://ctf-wiki.org/en/misc/archive/zip/
https://www.cnblogs.com/li-sx/p/17531186.html
https://www.cnblogs.com/Zer0o/p/17174911.html
zip文件结构如下:
| 压缩源文件数据区 | 核心目录 | 目录结束 |
|---|---|---|
| local file header + file data + data descriptor | central directory | end of central directory record |
1.1 压缩源文件数据区
-
local file header:文件头用于标识该文件的开始,记录了该压缩文件的信息,文件头标识由固定值50 4B 03 04开头,也是ZIP的文件头的重要标志 -
file data:文件数据记录了相应压缩文件的数据 -
data descriptor:数据描述符用于标识该文件压缩结束,该结构只有在相应的local file header中通用标记字段的第3 bit设为1时才会出现,紧接在压缩文件源数据后
1.2 核心目录
记录了压缩文件的目录信息,在这个数据区中每一条纪录对应在压缩源文件数据区中的一条数据
| Offset | Bytes | Description | 译 |
|---|---|---|---|
| 0 | 4 | Central directory file header signature = 0x02014b50 | 核心目录文件 header 标识 =(0x02014b50) |
| 4 | 2 | Version made by | 压缩所用的 pkware 版本 |
| 6 | 2 | Version needed to extract (minimum) | 解压所需 pkware 的最低版本 |
| 8 | 2 | General purpose bit flag | 通用位标记伪加密 |
| 10 | 2 | Compression method | 压缩方法 |
| 12 | 2 | File last modification time | 文件最后修改时间 |
| 14 | 2 | File last modification date | 文件最后修改日期 |
| 16 | 4 | CRC-32 | CRC-32 校验码 |
| 20 | 4 | Compressed size | 压缩后的大小 |
| 24 | 4 | Uncompressed size | 未压缩的大小 |
| 28 | 2 | File name length (n) | 文件名长度 |
| 30 | 2 | Extra field length (m) | 扩展域长度 |
| 32 | 2 | File comment length (k) | 文件注释长度 |
| 34 | 2 | Disk number where file starts | 文件开始位置的磁盘编号 |
| 36 | 2 | Internal file attributes | 内部文件属性 |
| 38 | 4 | External file attributes | 外部文件属性 |
| 42 | 4 | relative offset of local header | 本地文件头的相对位移 |
| 46 | n | File name | 目录文件名 |
| 46+n | m | Extra field | 扩展域 |
| 46+n+m | k | File comment | 文件注释内容 |
1.3 目录结束
目录结束标识存在于整个归档包的结尾,用于标记压缩的目录数据的结束。每个压缩文件必须有且只有一个 EOCD 记录
ZIP 文件头 50 4B 03 04 0A 00 00 00
ZIP 文件尾 50 4B 05 06 00 00 00 00 + 其他字符
二、校验接口如下:
public static void verifyPackage(File packageFile,ProgressListener listener,File deviceCertsZipFile) throws IOException, GeneralSecurityException {
final long fileLen = packageFile.length();
final RandomAccessFile raf = new RandomAccessFile(packageFile, "r");
try {
final long startTimeMillis = System.currentTimeMillis();
if (listener != null) {
listener.onProgress(0);
}
raf.seek(fileLen - 6);//定位到倒数第六个Byte
byte[] footer = new byte[6];
raf.readFully(footer)

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



