Repository navigation
Expand file tree
/
Copy pathUpdatebinFile.cs
More file actions
213 lines (172 loc) · 7.59 KB
/
Copy pathUpdatebinFile.cs
File metadata and controls
213 lines (172 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using HuaweiUpdateLibrary.Core;
using SharpCompress.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
namespace FastbootFlasher
{
class UpdatebinFile
{
const int COMPINFO_LEN_OFFSET = 178;
public class PartitionInfo
{
public int Index { get; set; }
public string Name { get; set; }
public long Offset { get; set; }
public long Size { get; set; }
public string SourceFile { get; set; }
}
const int CHUNK_SIZE = 1024 * 1024; // 1MB
public static List<ListViewItem> ParseUpdatebinFile(string filePath)
{
var entries = new List<ListViewItem>();
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using (var reader = new BinaryReader(fs))
{
long inputFileSize = fs.Length;
const int COMPINFO_LEN_OFFSET = 178;
fs.Seek(COMPINFO_LEN_OFFSET, SeekOrigin.Begin);
ushort compinfoAllSize = reader.ReadUInt16();
long type2Offset = COMPINFO_LEN_OFFSET + 2L + compinfoAllSize + 16;
fs.Seek(type2Offset, SeekOrigin.Begin);
ushort type2 = reader.ReadUInt16();
long dataStartOffset;
if (type2 == 0x08)
{
uint hashdataSize = reader.ReadUInt32();
dataStartOffset = type2Offset + 2 + 4 + hashdataSize;
}
else if (type2 == 0x06)
{
reader.ReadBytes(16); // 跳过16字节
uint hashdataSize = reader.ReadUInt32();
dataStartOffset = type2Offset + 2 + 16 + 4 + hashdataSize;
}
else
{
throw new Exception($"未知的类型: 0x{type2:X4}");
}
int componentCount = compinfoAllSize / 87;
long currentOffset = COMPINFO_LEN_OFFSET + 2;
long currentDataOffset = dataStartOffset;
for (int count = 1; count <= componentCount; count++)
{
fs.Seek(currentOffset, SeekOrigin.Begin);
// 读取组件名称
var nameBytes = new List<byte>();
byte b;
while ((b = reader.ReadByte()) != 0)
{
nameBytes.Add(b);
}
string componentName = Encoding.UTF8.GetString(nameBytes.ToArray());
componentName = componentName.TrimStart('/');
// 读取组件大小
fs.Seek(currentOffset + 47, SeekOrigin.Begin);
ulong componentSize = reader.ReadUInt64();
currentOffset += 87;
entries.Add(new ListViewItem
{
Num = count,
Part = componentName,
Size = MainWindow.FormatSize((long)componentSize),
Addr = $"0x{(long)componentSize:X8}",
Source = filePath
});
// 更新数据偏移量
currentDataOffset += (long)componentSize;
}
}
return entries;
}
public static async Task ExtractPartition(string filePath, int partitionIndex)
{
List<PartitionInfo> partitions = GetAllPartitionsInfo(filePath);
PartitionInfo partition = partitions[partitionIndex - 1];
Directory.CreateDirectory(@".\images");
long inputFileSize = new FileInfo(filePath).Length;
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using (FileStream outFile = new FileStream(@$".\images\{partition.Name}.img", FileMode.Create))
{
fs.Seek(partition.Offset, SeekOrigin.Begin);
long bytesLeft = partition.Size;
byte[] buffer = new byte[CHUNK_SIZE];
while (bytesLeft > 0)
{
int readSize = (int)Math.Min(bytesLeft, CHUNK_SIZE);
int bytesRead = await fs.ReadAsync(buffer, 0, readSize);
outFile.Write(buffer, 0, bytesRead);
bytesLeft -= bytesRead;
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
MainWindow.pb.Value = (double)(partition.Size - bytesLeft) / partition.Size * 100;
});
}
}
}
public static List<PartitionInfo> GetAllPartitionsInfo(string filePath)
{
var partitions = new List<PartitionInfo>();
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using (var reader = new BinaryReader(fs))
{
long inputFileSize = fs.Length;
fs.Seek(COMPINFO_LEN_OFFSET, SeekOrigin.Begin);
ushort compinfoAllSize = reader.ReadUInt16();
long type2Offset = COMPINFO_LEN_OFFSET + 2L + compinfoAllSize + 16;
fs.Seek(type2Offset, SeekOrigin.Begin);
ushort type2 = reader.ReadUInt16();
long dataStartOffset;
if (type2 == 0x08)
{
uint hashdataSize = reader.ReadUInt32();
dataStartOffset = type2Offset + 2 + 4 + hashdataSize;
}
else if (type2 == 0x06)
{
reader.ReadBytes(16); // 跳过16字节
uint hashdataSize = reader.ReadUInt32();
dataStartOffset = type2Offset + 2 + 16 + 4 + hashdataSize;
}
else
{
throw new Exception($"未知的类型: 0x{type2:X4}");
}
int componentCount = compinfoAllSize / 87;
long currentOffset = COMPINFO_LEN_OFFSET + 2;
long currentDataOffset = dataStartOffset;
for (int count = 1; count <= componentCount; count++)
{
fs.Seek(currentOffset, SeekOrigin.Begin);
var nameBytes = new List<byte>();
byte b;
while ((b = reader.ReadByte()) != 0)
{
nameBytes.Add(b);
}
string componentName = Encoding.UTF8.GetString(nameBytes.ToArray());
componentName = componentName.TrimStart('/');
fs.Seek(currentOffset + 47, SeekOrigin.Begin);
ulong componentSize = reader.ReadUInt64();
partitions.Add(new PartitionInfo
{
Index = count,
Name = componentName,
Offset = currentDataOffset,
Size = (long)componentSize,
SourceFile = filePath
});
currentDataOffset += (long)componentSize;
currentOffset += 87;
}
}
return partitions;
}
}
}