forked from RPGLogs/mplus.subcreation.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExportUtil.py
74 lines (55 loc) · 2.42 KB
/
ExportUtil.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
# special thanks to Solian <3
BitsPerChar = 6
def MakeBase64ConversionTable():
base64ConversionTable = {0: 'A'}
i = 0
for num in range(0, 26):
base64ConversionTable[i] = chr(65 + num)
i += 1
for num in range(0, 26):
base64ConversionTable[i] = chr(97 + num)
i += 1
for num in range(0, 10):
base64ConversionTable[i] = str(num)
i += 1
base64ConversionTable[i] = '+'
i += 1
base64ConversionTable[i] = '/'
return base64ConversionTable
NumberToBase64CharConversionTable = MakeBase64ConversionTable()
Base64CharToNumberConversionTable = {v: k for k, v in MakeBase64ConversionTable().items()}
def ConvertToBase64(dataEntries):
exportString = ""
currentValue = 0
currentReservedBits = 0
totalBits = 0
for i, dataEntry in enumerate(dataEntries):
remainingValue = dataEntry.value
remainingRequiredBits = dataEntry.bitWidth
maxValue = 1 << remainingRequiredBits
if remainingValue >= maxValue:
print("Data entry has higher value than storable in bitWidth. (%d in %d bits)".format(remainingValue, remainingRequiredBits))
return ""
totalBits = totalBits + remainingRequiredBits
while remainingRequiredBits > 0:
spaceInCurrentValue = (BitsPerChar - currentReservedBits)
maxStorableValue = 1 << spaceInCurrentValue
remainder = remainingValue % maxStorableValue
remainingValue = remainingValue >> spaceInCurrentValue
currentValue = currentValue + (remainder << currentReservedBits)
if spaceInCurrentValue > remainingRequiredBits:
currentReservedBits = (currentReservedBits + remainingRequiredBits) % BitsPerChar
remainingRequiredBits = 0
else:
exportString += NumberToBase64CharConversionTable[currentValue]
currentValue = 0
currentReservedBits = 0
remainingRequiredBits = remainingRequiredBits - spaceInCurrentValue
if currentReservedBits > 0:
exportString += NumberToBase64CharConversionTable[currentValue]
return exportString
def ConvertFromBase64(exportString):
dataValues = {}
for i in range (0, len(exportString)):
dataValues[i] = Base64CharToNumberConversionTable[exportString[i:i+1]]
return dataValues