Skip to content

Commit 019ea1a

Browse files
authored
Sector list (#7)
* added FAT sectors
1 parent 438f5ed commit 019ea1a

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

tests/test_vbaProjectCompiler.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,25 @@ def test_header():
1818
result = project.header()
1919
expected = b'\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x3E\x00\x03\x00\xFE\xFF\x09\x00\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\xFE\xFF\xFF\xFF\x00\x00\x00\x00'
2020
assert result == expected
21+
22+
def test_streamSectors():
23+
project = VbaProject('.')
24+
list1 = [7]
25+
list2 = [4, 5, 6, 8, 16, 9, 10, 11, 12, 13, 14, 15, 17]
26+
project.addStreamSectorList(list1)
27+
assert project.countStreams() == 1
28+
project.addStreamSectorList(list2)
29+
assert project.countStreams() == 2
30+
result = project.streamSectors
31+
assert result == [list1, list2]
32+
assert project.getFatChainLength() == 17
33+
assert project.countFatChainSectors() == 1
34+
35+
result = project.writeFatSectorList()
36+
empty = b'\xff\xff\xff\xff'
37+
emptyLine = empty * 4
38+
padding = emptyLine * 27
39+
expected = b'\x00\x00\x00\x00' + padding
40+
assert result == expected
41+
42+

vbaProjectCompiler/main.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ class VbaProject:
2222
#data members of class
2323
path = "." #path to the project root
2424

25+
#Each element is a chain of sector numbers for a particular stream.
26+
streamSectors = []
27+
28+
#A list of sectors that contain FAT chain information.
29+
fatSectors = []
30+
2531
#class default constructor
2632
def __init__(self, path):
2733
self.path = path
@@ -96,17 +102,24 @@ def header(self):
96102
csectDif = LONG_ZERO
97103
header += csectDif
98104

99-
#sectFat = getFirst109FatSectors()
105+
#sectFat = writeFatSectorList()
100106
#header += sectFat
101107
return header
102108

109+
def addStreamSectorList(self, list):
110+
"""Add a list of sector numbers to the FAT table."""
111+
self.streamSectors.append(list)
112+
113+
def countStreams(self):
114+
"""Return the number of streams defined in the FAT table."""
115+
return len(self.streamSectors)
116+
103117
def writeFat(i):
104118
return 1
105119

106120
def countFatChainSectors(self):
107121
"""Calculate the number of sectors needed to express the FAT chain."""
108-
#return self.getFatChainLength() / 512 + 1 #intdiv, roundup.
109-
return 1
122+
return self.getFatChainLength() // 511 + 1
110123

111124
def getFirstDirectoryChainSector(self):
112125
return 1
@@ -120,22 +133,28 @@ def countMiniFatChainSectors(self):
120133
def getFirstMiniChainSector(self):
121134
return 2
122135

123-
def getFirst109FatSectors(self):
124-
#return an array of 109 4-byte numbers
125-
#00000000 followed by FFFFFFFF 108 times
126-
return "00000000";
136+
def writeFatSectorList(self):
137+
"""Create a 436 byte stream of the first 109 FAT sectors, padded with \\xFF"""
138+
list = bytearray(b'\x00\x00\x00\x00')
139+
if self.countFatChainSectors() > 1:
140+
#the second FAT sector is number 128.
141+
pass
142+
list = list.ljust(436, b'\xff')
143+
return list
144+
145+
def getFatSectors(self):
146+
sectorList = [0]
147+
# add
148+
return sectorList
127149

128150
def writeFatSector(self, i):
129151
"""return a 512 byte sector"""
130152
return "FE FF FF FF"
131153
# followed by 511 bytes
132154

133155
def getFatChainLength(self):
134-
total = 0
135-
#get the length of the fat chain including termination and beginning codes
136-
#Total = getDirectoryChainLength() + 1
137-
#Total += getMiniFatChainLength() + 1
138-
#for stream in streams:
139-
# total += stream.getChainLength() + 1
140-
#total += total % 511 #add one double for each sector
156+
"""Count the number of entries in the complete FAT chain."""
157+
total = 1
158+
for stream in self.streamSectors:
159+
total += len(stream) + 1
141160
return total

0 commit comments

Comments
 (0)