-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidate_pbpack.py
65 lines (50 loc) · 2.22 KB
/
validate_pbpack.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
#!/usr/bin/env python
from zipfile import ZipFile
from glob import glob
import sys, logging, os.path, os, struct, json
from libpebble.stm32_crc import crc32
log = logging.getLogger()
logging.basicConfig(format='%(message)s')
log.setLevel(logging.DEBUG)
class BMPResource:
def __init__(self, data):
(self.depth,self.unknown_1,self.unknown_2,self.width,self.height) = struct.unpack("<hhlhh", data[:12])
assert(self.unknown_1 == 4096)
assert(self.unknown_2 == 0)
self.data = data[12:]
#log.debug(repr(self))
def __repr__(self):
return "<BMP %dx%d %2dB/scanline size %d>" % (self.width,self.height,self.depth,len(self.data))
class Resource:
def __init__(self, raw, file):
(self.index, self.offset, self.size, self.crc) = struct.unpack("<LLLL", raw)
#Resources are loaded sequentially, so...
self.raw_data = file.read(self.size)
self.png = BMPResource(self.raw_data)
log.debug("%08X | %08X" % (self.crc, crc32(self.raw_data)))
log.debug(repr(self))
assert(self.crc == crc32(self.raw_data))
def __str__(self):
return self.png.data
def __repr__(self):
return "<Resource %2d @ %04x:%04x CRC:%08x %s>" % (self.index, self.offset, self.offset+self.size, self.crc, repr(self.png))
class PBPack:
def __init__(self, pack):
(self.resource_count, self.crc, self.timestamp, self.name) = struct.unpack("<LLL16s", pack.read(12+16))
resource_block = pack.read(4096)
offset = 0
self.resources = []
for i in xrange(self.resource_count):
offset = 16*i
self.resources.append(Resource(resource_block[offset:offset+16], pack))
def pack(self):
return struct.pack("<LLL16s", self.resource_count, self.crc, self.timestamp, self.name)
def __repr__(self):
return "<PBPack %08X \"%s\" [%d]>" % (self.crc, self.name, self.resource_count)
if __name__=="__main__":
fn = "app_resources.pbpack"
if len(sys.argv) > 1:
fn = sys.argv[1]
log.info('Processing "%s"' % "app_resources.pbpack")
p = PBPack(open(fn, 'rb'))
log.info(p)