-
Notifications
You must be signed in to change notification settings - Fork 0
/
sisfile.py
376 lines (276 loc) · 8.23 KB
/
sisfile.py
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/env python3
import os
from enum import IntEnum
from util.binfile import (
Structure,
Attribute,
StructurePayloadLength,
CanBeLast,
SkipNextIfByte,
EfficientUInt63,
BuildEnum,
Zlib,
Int32,
UInt64,
UInt32,
UInt16,
UInt8,
UTF16String,
Array,
UnknownPayload,
)
# based on format documentation from:
# https://web.archive.org/web/20101011053920/http://developer.symbian.org/wiki/images/b/b7/SymbianOSv9.x_SIS_File_Format_Specification.pdf
TInt32 = Int32
TUint64 = UInt64
TUint32 = UInt32
TUint16 = UInt16
TUint8 = UInt8
class SymbianFileHeader(Structure):
UID1 : TInt32 = 0x10201A7A
UID2 : TInt32 = 0
UID3 : TInt32
UIDChecksum : TInt32
class TField(IntEnum):
(
INVALID,
SISString,
SISArray,
SISCompressed,
SISVersion,
SISVersionRange,
SISDate,
SISTime,
SISDateTime,
SISUid,
UNUSED,
SISLanguage,
SISContents,
SISController,
SISInfo,
SISSupportedLanguages,
SISSupportedOptions,
SISPrerequisites,
SISDependency,
SISProperties,
SISProperty,
SISSignatures,
SISCertificateChain,
SISLogo,
SISFileDescription,
SISHash,
SISIf,
SISElseIf,
SISInstallBlock,
SISExpression,
SISData,
SISDataUnit,
SISFileData,
SISSupportedOption,
SISControllerChecksum,
SISDataChecksum,
SISSignature,
SISBlob,
SISSignatureAlgorithm,
SISSignatureCertificateChain,
SISDataIndex,
SISCapabilities,
) = range(42)
class TCompressionAlgorithm(IntEnum):
SISCompressedNone, SISCompressedDeflate = range(2)
class TLanguage(IntEnum):
C, EN = range(2) # made-up names
class SISField(Structure):
_subclassfield = 'Type'
ALIGNMENT = 4
Type : BuildEnum(TInt32, TField)
Length : EfficientUInt63
Length : StructurePayloadLength
class SISString(SISField):
# UCS-2 encoded unicode string
String : UTF16String # made-up name
class SISArray(SISField):
_template = '_tp',
SISFieldType : BuildEnum(TUint32, TField)
Contents : Array['_tp'] # made-up name
def init_common(self, obj):
obj.Type = self.SISFieldType
class SISCompressed(SISField):
_subclassfield = 'Algorithm'
_template = '_tp',
Algorithm : BuildEnum(TUint32, TCompressionAlgorithm)
UncompressedDataSize : TUint64
class SISCompressedNone(SISCompressed):
CompressedData : '_tp'
class SISCompressedDeflate(SISCompressed):
CompressedData : Zlib['_tp']
def init_common(self, obj):
obj._maxfin = self.UncompressedDataSize
class SISVersion(SISField):
Length = 12
# -1 means Any
Major : TInt32
Minor : TInt32
Build : TInt32
class SISVersionRange(SISField):
# Length = 40 # can be 20 as well
FromVersion : SISVersion
FromVersion : CanBeLast
ToVersion : SISVersion
class SISDate(SISField):
Length = 4
Year : TUint16
Month : TUint8 # zero-based
Day : TUint8 # one-based
class SISTime(SISField):
Hours : TUint8
Minutes : TUint8
Seconds : TUint8
class SISDateTime(SISField):
Date : SISDate
Time : SISTime
# UNUSED here
class SISUid(SISField):
# is to match UID3 from file header
Length = 4
UID1 : TInt32
class SISLanguage(SISField):
Length = 4
Language : BuildEnum(TUint32, TLanguage)
class SISBlob(SISField):
Blob : UnknownPayload
class SISDataIndex(SISField):
Length = 4
DataIndex : TUint32
# reordered before SISContents:
class SISControllerChecksum(SISField):
Length = 2
Checksum : TUint16 # CRC-16
class SISDataChecksum(SISField):
Length = 2
Checksum : TUint16 # CRC-16
# reordered before SISController:
class SISInfo(SISField):
UID : SISUid
VendorUniqueName : SISString
Names : SISArray[SISString]
VendorNames : SISArray[SISString]
Version : SISVersion
CreationTime : SISDateTime
InstallType : TUint8 # TInstallType
InstallFlags : TUint8 # TInstallFlags
class SISSupportedLanguages(SISField):
Languages : SISArray[SISLanguage]
# reordered before SISSupportedOptions:
class SISSupportedOption(SISField):
Names : SISArray[SISString]
class SISSupportedOptions(SISField):
Options : SISArray[SISSupportedOption]
# reordered before SISPrerequisites
class SISDependency(SISField):
UID : SISUid
VersionRange : SISVersionRange
DependencyNames : SISArray[SISString]
class SISPrerequisites(SISField):
TargetDevices : SISArray[SISDependency]
Dependencies : SISArray[SISDependency]
# reordered before SISProperties
class SISProperty(SISField):
Length = 8
Key : TInt32
Value : TInt32
class SISProperties(SISField):
Properties : SISArray[SISProperty]
# reordered before SISFileDescription
class SISCapabilities(SISField):
Capabilities : Array[TUint32] # bitfield
class SISHash(SISField):
HashAlgorithm : TUint32 # TSISHashAlgorithm
HashData : SISBlob
# reordered before SISLogo
class SISFileDescription(SISField):
Target : SISString
MIMEType : SISString
MIMEType : SkipNextIfByte('Capabilities', TField.SISHash & 255)
# "This field is optional"
Capabilities : SISCapabilities
Hash : SISHash
Operation : TUint32
OperationOptions : TUint32
FileLength : TUint64 # originally called Length, but clashes with SISField.Length
UncompressedLength : TUint64
FileIndex : TUint32
# reordered before SISController
class SISLogo(SISField):
LogoFile : SISFileDescription
class SISInstallBlock(SISField):
Files : SISArray[SISFileDescription]
EmbeddedSISFiles : SISArray[SISField] # should be SISControllers
IfBlocks : SISArray[SISField] # should be SISIf
# reordered before SISSignatureCertificateChain
class SISCertificateChain(SISField):
CertificateData : SISBlob
# reordered before SISSignature
class SISSignatureAlgorithm(SISField):
# Supported values:
# * “1.2.840.113549.1.1.5” - SHA-1 with RSA signature
# * “1.2.840.10040.4.3” - SHA-1 with DSA signature
AlgorithmIdentifier : SISString
class SISSignature(SISField):
SignatureAlgorithm : SISSignatureAlgorithm
SignatureData : SISBlob
class SISSignatureCertificateChain(SISField):
Signatures : SISArray[SISSignature]
CertificateChain : SISCertificateChain
class SISController(SISField):
Info : SISInfo
Options : SISSupportedOptions
Languages : SISSupportedLanguages
Prerequisites : SISPrerequisites
Properties : SISProperties
Properties : SkipNextIfByte('Logo', TField.SISInstallBlock & 255)
# "This field is optional"
Logo : SISLogo
InstallBlock : SISInstallBlock
Signature0 : SISSignatureCertificateChain
DataIndex : SISDataIndex
# reordered before SISDataUnit
class SISFileData(SISField):
FileData : SISCompressed[UnknownPayload]
# reordered before SISData
class SISDataUnit(SISField):
FileData : SISArray[SISFileData]
class SISData(SISField):
DataUnits : SISArray[SISDataUnit]
class SISContents(SISField):
ControllerChecksum : SISControllerChecksum
DataChecksum : SISDataChecksum
Controller : SISCompressed[SISController]
Data : SISData
# reordered before SISIf
class SISExpression(SISField):
Operator : TUint32 # TOperator
IntegerValue : TInt32
StringValue : SISString
StringValue : CanBeLast
LeftExpression : SISField # should be SISExpression
LeftExpression : CanBeLast
RightExpression : SISField # should be SISExpression
class SISElseIf(SISField):
Expression : SISExpression
InstallBlock : SISInstallBlock
class SISIf(SISField):
Expression : SISExpression
InstallBlock : SISInstallBlock
ElseIfs : SISArray[SISElseIf]
def extract_files(fp, header, target_dir):
ff = SISField(fp)
for f in ff.Controller.CompressedData.InstallBlock.Files.Contents:
fd = ff.Data.DataUnits.Contents[0].FileData.Contents[f.FileIndex]
print(fd.FileData.CompressedData)
print(f.Target)
print(f.MIMEType)
name = os.path.join(target_dir, f.Target.String.split('\\')[-1] or "%d"%f.FileIndex)
with open(name, 'wb') as ofp:
ofp.write(fd.FileData.CompressedData)
return ff