-
Notifications
You must be signed in to change notification settings - Fork 26
/
Block.py
134 lines (118 loc) · 4.59 KB
/
Block.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
from PIL import Image
from hashlib import md5
from zlib import decompress
from LVmisc import getVersion
from StringIO import StringIO
from binstreamer import BinaryStream
from LVmisc import LABVIEW_COLOR_PALETTE
class Block(object):
def __init__(self, vi, header):
self.vi = vi
self.header = header
self.name = header['Name']
self.count = header['Count']
self.vi.reader.seek(header['Offset'])
self.ints = []
self.ints.append(self.vi.reader.readInt32())
self.ints.append(self.vi.reader.readInt32())
self.ints.append(self.vi.reader.readInt32())
self.offset = \
self.vi.header['DataSet']['Offset'] + self.vi.reader.readUInt32()
self.ints.append(self.vi.reader.readInt32())
self.size = None
def getData(self, blockNr=0, useCompression=False):
if self.size is None:
minSize = self.vi.header['DataSet']['Size']
for i in self.vi.blocks_arr:
if self != i and i.offset > self.offset:
minSize = min(minSize, i.offset - self.offset)
self.size = minSize
size = sumSize = 0
self.raw_data = []
for i in range(0, blockNr + 1):
if size % 4 > 0:
size += 4 - (size % 4)
sumSize += size
self.vi.reader.seek(self.offset + sumSize)
size = self.vi.reader.readUInt32()
sumSize += 4
if (sumSize + size) > self.size:
raise IOError("Out of block/container data (%d + %d) %d"
% (sumSize, size, self.size))
self.vi.reader.seek(self.offset + sumSize)
data = self.vi.reader.read(size)
self.raw_data.append(data)
data = BinaryStream(StringIO(self.raw_data[blockNr]))
if useCompression:
size = len(data) - 4
if size < 2:
raise IOError("Unable to decompress section [%s:%d]: \
block-size-error - size: %d" % (self.name, blockNr, size))
usize = data.readInt32("BE")
if (usize < size) or usize > size * 10:
raise IOError("Unable to decompress section [%s:%d]: \
uncompress-size-error - size: %d - uncompress-size: %d"
% (self.name, blockNr, size, usize))
data = BinaryStream(StringIO(decompress(data.read(size))))
return data
class LVSR(Block):
def __init__(self, *args):
return Block.__init__(self, *args)
def getData(self, *args):
block = Block.getData(self, *args)
self.version = getVersion(block.readUInt32())
self.INT1 = block.readInt16()
self.flags = block.readUInt16()
self.protected = ((self.flags & 0x2000) > 0)
self.flags = self.flags & 0xDFFF
return block
class vers(Block):
def __init__(self, *args):
return Block.__init__(self, *args)
def getData(self, *args):
block = Block.getData(self, *args)
self.version = getVersion(block.readUInt32())
self.version_text = block.read("s1")
self.version_info = block.read("s1")
return block
class icl8(Block):
def __init__(self, *args):
return Block.__init__(self, *args)
def getData(self, *args):
return Block.getData(self, *args)
def loadIcon(self, bitsPerPixel=8):
icon = Image.new("RGB", (32, 32))
block = self.getData()
for y in range(0, 32):
for x in range(0, 32):
idx = block.readByte()
icon.putpixel((x, y), LABVIEW_COLOR_PALETTE[idx])
self.icon = icon
return icon
class BDPW(Block):
def __init__(self, *args):
return Block.__init__(self, *args)
def getData(self, *args):
block = Block.getData(self, *args)
self.password_md5 = block.read(16)
self.hash_1 = block.read(16)
self.hash_2 = block.read(16)
return block
class LIBN(Block):
def __init__(self, *args):
return Block.__init__(self, *args)
def getData(self, *args):
block = Block.getData(self, *args)
self.count = block.readUInt32()
self.content = block.read("s1")
return block
class BDH(Block):
def __init__(self, *args):
return Block.__init__(self, *args)
def getData(self, *args):
Block.getData(self, *args)
block = Block.getData(self, useCompression=True)
self.content = block.read("s4", "BE")
self.hash = md5(self.content).digest()
return block
BDHc = BDHb = BDH