-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProgram.cs
More file actions
320 lines (302 loc) · 11.9 KB
/
Copy pathProgram.cs
File metadata and controls
320 lines (302 loc) · 11.9 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Utilities;
namespace SparseConverter
{
class Program
{
static void Main(string[] args)
{
args = CommandLineParser.GetCommandLineArgsIgnoreEscape();
if (args.Length < 2)
{
PrintHelp();
return;
}
string inputPath = args[1];
if (String.Equals(args[0], "/compress", StringComparison.InvariantCultureIgnoreCase))
{
if (args.Length != 4)
{
PrintHelp();
return;
}
string outputPath = args[2];
long maxSparseSize = ParseStandardSizeString(args[3]);
int minSparseSize = SparseHeader.Length + 3 * ChunkHeader.Length + SparseCompressionHelper.BlockSize;
if (maxSparseSize >= minSparseSize)
{
Compress(inputPath, outputPath, maxSparseSize);
}
else
{
PrintHelp();
return;
}
}
else if (String.Equals(args[0], "/decompress", StringComparison.InvariantCultureIgnoreCase))
{
if (args.Length != 3)
{
PrintHelp();
return;
}
string outputPath = args[2];
List<string> sparseList = GetSparseList(inputPath);
Decompress(sparseList, outputPath);
}
else if (String.Equals(args[0], "/stats", StringComparison.InvariantCultureIgnoreCase))
{
PrintSparseImageStatistics(inputPath);
}
else
{
PrintHelp();
}
}
private static void PrintHelp()
{
Console.WriteLine("SparseConverter v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
Console.WriteLine("Author: Tal Aloni (tal.aloni.il@gmail.com)");
Console.WriteLine("About:");
Console.WriteLine("This software is designed to create / decompress compressed ext4 file system");
Console.WriteLine("sparse image format, which is defined by AOSP.");
Console.WriteLine();
Console.WriteLine("Usage:");
Console.WriteLine("SparseConverter /compress <image-path> <output-folder> <max-sparse-size>");
Console.WriteLine("SparseConverter /decompress <first-sparse-path> <output-image-path>");
Console.WriteLine("SparseConverter /stats <sparse-path>");
}
private static void Compress(string inputPath, string outputPath, long maxSparseSize)
{
FileStream input;
try
{
input = File.Open(inputPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
catch (IOException)
{
Console.WriteLine("Cannot open " + inputPath);
return;
}
catch (UnauthorizedAccessException)
{
Console.WriteLine("Cannot open {0} - Access Denied", inputPath);
return;
}
if (input.Length % SparseCompressionHelper.BlockSize > 0)
{
Console.WriteLine("Image size is not a multiple of {0} bytes", SparseCompressionHelper.BlockSize);
return;
}
if (!Directory.Exists(outputPath))
{
Console.WriteLine("Output directory does not exist");
return;
}
if (!outputPath.EndsWith(":") && !outputPath.EndsWith(@"\"))
{
outputPath += @"\";
}
string imageFileName = Path.GetFileName(inputPath);
string prefix = outputPath + imageFileName + "_sparsechunk";
int sparseIndex = 1;
while(true)
{
string sparsePath = prefix + sparseIndex.ToString();
FileStream output;
try
{
output = File.Open(sparsePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
output.SetLength(0);
}
catch (IOException)
{
Console.WriteLine("Cannot open " + sparsePath);
return;
}
catch (UnauthorizedAccessException)
{
Console.WriteLine("Cannot open {0} - Access Denied", sparsePath);
return;
}
Console.WriteLine("Writing: {0}", sparsePath);
bool complete = SparseCompressionHelper.WriteCompressedSparse(input, output, maxSparseSize);
if (complete)
{
break;
}
sparseIndex++;
}
input.Close();
}
private static List<string> GetSparseList(string inputPath)
{
List<string> sparseList = new List<string>();
sparseList.Add(inputPath);
if (inputPath.EndsWith("0") || inputPath.EndsWith("1"))
{
int firstSparseIndex = Convert.ToInt32(inputPath.Substring(inputPath.Length - 1));
string prefix = inputPath.Substring(0, inputPath.Length - 1);
int sparseIndex = firstSparseIndex + 1;
string sparsePath = prefix + sparseIndex.ToString();
while (File.Exists(sparsePath))
{
sparseList.Add(sparsePath);
sparseIndex++;
sparsePath = prefix + sparseIndex.ToString();
}
}
return sparseList;
}
private static void Decompress(List<string> sparseList, string outputPath)
{
FileStream output;
try
{
output = File.Open(outputPath, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
output.SetLength(0);
}
catch (IOException)
{
Console.WriteLine("Cannot open " + outputPath);
return;
}
catch (UnauthorizedAccessException)
{
Console.WriteLine("Cannot open {0} - Access Denied", outputPath);
return;
}
Console.WriteLine("Output: {0}", outputPath);
foreach (string sparsePath in sparseList)
{
FileStream input;
try
{
input = File.Open(sparsePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
catch (IOException)
{
Console.WriteLine("Cannot open " + sparsePath);
return;
}
catch (UnauthorizedAccessException)
{
Console.WriteLine("Cannot open {0} - Access Denied", sparsePath);
return;
}
Console.WriteLine("Processing: {0}", sparsePath);
try
{
SparseDecompressionHelper.DecompressSparse(input, output);
}
catch(ArgumentException)
{
Console.WriteLine("Invalid Sparse Image Format");
return;
}
}
output.Close();
}
private static void PrintSparseImageStatistics(string path)
{
FileStream stream;
try
{
stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
catch (IOException)
{
Console.WriteLine("Cannot open " + path);
return;
}
catch (UnauthorizedAccessException)
{
Console.WriteLine("Cannot open {0} - Access Denied", path);
return;
}
SparseHeader sparseHeader = SparseHeader.Read(stream);
if (sparseHeader == null)
{
Console.WriteLine("Invalid Sparse Image Format");
return;
}
Console.WriteLine("Total Blocks: " + sparseHeader.TotalBlocks);
Console.WriteLine("Total Chunks: " + sparseHeader.TotalChunks);
long outputSize = 0;
for(uint index = 0; index < sparseHeader.TotalChunks; index++)
{
ChunkHeader chunkHeader = ChunkHeader.Read(stream);
Console.Write("Chunk type: {0}, size: {1}, total size: {2}", chunkHeader.ChunkType.ToString(), chunkHeader.ChunkSize, chunkHeader.TotalSize);
int dataLength = (int)(chunkHeader.ChunkSize * sparseHeader.BlockSize);
switch(chunkHeader.ChunkType)
{
case ChunkType.Raw:
{
SparseDecompressionHelper.ReadBytes(stream, dataLength);
Console.WriteLine();
outputSize += dataLength;
break;
}
case ChunkType.Fill:
{
byte[] fillBytes = SparseDecompressionHelper.ReadBytes(stream, 4);
uint fill = LittleEndianConverter.ToUInt32(fillBytes, 0);
Console.WriteLine(", value: 0x{0}", fill.ToString("X8"));
outputSize += dataLength;
break;
}
case ChunkType.DontCare:
{
Console.WriteLine();
break;
}
case ChunkType.CRC:
{
byte[] crcBytes = SparseDecompressionHelper.ReadBytes(stream, 4);
uint crc = LittleEndianConverter.ToUInt32(crcBytes, 0);
Console.WriteLine(", value: 0x{0}", crc.ToString("X8"));
break;
}
default:
{
Console.WriteLine();
Console.WriteLine("Error: Invalid Chunk Type");
return;
}
}
}
stream.Close();
Console.WriteLine("Output size: {0}", outputSize);
}
public static long ParseStandardSizeString(string value)
{
if (value.ToUpper().EndsWith("TB"))
{
return (long)1024 * 1024 * 1024 * 1024 * Conversion.ToInt64(value.Substring(0, value.Length - 2), -1);
}
else if (value.ToUpper().EndsWith("GB"))
{
return 1024 * 1024 * 1024 * Conversion.ToInt64(value.Substring(0, value.Length - 2), -1);
}
else if (value.ToUpper().EndsWith("MB"))
{
return 1024 * 1024 * Conversion.ToInt64(value.Substring(0, value.Length - 2), -1);
}
else if (value.ToUpper().EndsWith("KB"))
{
return 1024 * Conversion.ToInt64(value.Substring(0, value.Length - 2), -1);
}
if (value.ToUpper().EndsWith("B"))
{
return Conversion.ToInt64(value.Substring(0, value.Length - 1), -1);
}
else
{
return Conversion.ToInt64(value, -1);
}
}
}
}