This repository was archived by the owner on Jun 23, 2020. It is now read-only.
forked from TeamMsgExtractor/msg-extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
193 lines (169 loc) · 5.61 KB
/
Copy pathutils.py
File metadata and controls
193 lines (169 loc) · 5.61 KB
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
"""
Utility functions of extract_msg.
"""
import datetime
import os
import sys
import tzlocal
from extract_msg import constants
if sys.version_info[0] >= 3: # Python 3
stri = (str,)
def encode(inp):
return inp
def properHex(inp):
"""
Taken (with permission) from https://github.com/TheElementalOfCreation/creatorUtils
"""
a = ''
if isinstance(inp, stri):
a = ''.join([hex(ord(inp[x]))[2:].rjust(2, '0') for x in range(len(inp))])
if isinstance(inp, bytes):
a = inp.hex()
elif isinstance(inp, int):
a = hex(inp)[2:]
if len(a) % 2 != 0:
a = '0' + a
return a
def windowsUnicode(string):
if string is None:
return None
return str(string, 'utf_16_le')
def xstr(s):
return '' if s is None else str(s)
else: # Python 2
stri = (str, unicode)
def encode(inp):
return inp.encode('utf-8')
def properHex(inp):
"""
Taken (with permission) from https://github.com/TheElementalOfCreation/creatorUtils
"""
a = ''
if isinstance(inp, stri):
a = ''.join([hex(ord(inp[x]))[2:].rjust(2, '0') for x in range(len(inp))])
elif isinstance(inp, int):
a = hex(inp)[2:]
elif isinstance(inp, long):
a = hex(inp)[2:-1]
if len(a) % 2 != 0:
a = '0' + a
return a
def windowsUnicode(string):
if string is None:
return None
return unicode(string, 'utf_16_le')
def xstr(s):
if isinstance(s, unicode):
return s.encode('utf-8')
else:
return '' if s is None else str(s)
def addNumToDir(dirName):
"""
Attempt to create the directory with a '(n)' appended.
"""
for i in range(2, 100):
try:
newDirName = dirName + ' (' + str(i) + ')'
os.makedirs(newDirName)
return newDirName
except Exception as e:
pass
return None
def divide(string, length):
"""
Taken (with permission) from https://github.com/TheElementalOfCreation/creatorUtils
Divides a string into multiple substrings of equal length
:param string: string to be divided.
:param length: length of each division.
:returns: list containing the divided strings.
Example:
>>>> a = divide('Hello World!', 2)
>>>> print(a)
['He', 'll', 'o ', 'Wo', 'rl', 'd!']
"""
return [string[length * x:length * (x + 1)] for x in range(int(len(string) / length))]
def fromTimeStamp(stamp):
return datetime.datetime.fromtimestamp(stamp, tzlocal.get_localzone())
def has_len(obj):
"""
Checks if :param obj: has a __len__ attribute.
"""
try:
obj.__len__
return True
except AttributeError:
return False
def msgEpoch(inp):
"""
Taken (with permission) from https://github.com/TheElementalOfCreation/creatorUtils
"""
return (inp - 116444736000000000) / 10000000.0
def parse_type(_type, stream):
"""
Converts the data in :param stream: to a
much more accurate type, specified by
:param _type:, if possible.
Some types require that :param prop_value: be specified. This can be retrieved from the Properties instance.
WARNING: Not done. Do not try to implement anywhere where it is not already implemented
"""
# WARNING Not done. Do not try to implement anywhere where it is not already implemented
value = stream
if _type == 0x0000: # PtypUnspecified
pass;
elif _type == 0x0001: # PtypNull
if value != b'\x00\x00\x00\x00\x00\x00\x00\x00':
# DEBUG
print('Warning: Property type is PtypNull, but is not equal to 0.')
value = None
elif _type == 0x0002: # PtypInteger16
value = constants.STI16.unpack(value)[0]
elif _type == 0x0003: # PtypInteger32
value = constants.STI32.unpack(value)[0]
elif _type == 0x0004: # PtypFloating32
value = constants.STF32.unpack(value)[0]
elif _type == 0x0005: # PtypFloating64
value = constants.STF64.unpack(value)[0]
elif _type == 0x0006: # PtypCurrency
value = (constants.STI64.unpack(value)[0]) / 10000.0
elif _type == 0x0007: # PtypFloatingTime
value = constants.STF64.unpack(value)[0]
# TODO parsing for this
pass;
elif _type == 0x000A: # PtypErrorCode
value = constants.STI32.unpack(value)[0]
# TODO parsing for this
pass;
elif _type == 0x000B: # PtypBoolean
value = bool(constants.ST3.unpack(value)[0])
elif _type == 0x000D: # PtypObject/PtypEmbeddedTable
# TODO parsing for this
pass;
elif _type == 0x0014: # PtypInteger64
value = constants.STI64.unpack(value)[0]
elif _type == 0x001E: # PtypString8
# TODO parsing for this
pass;
elif _type == 0x001F: # PtypString
value = value.decode('utf_16_le')
elif _type == 0x0040: # PtypTime
value = constants.ST3.unpack(value)[0]
elif _type == 0x0048: # PtypGuid
# TODO parsing for this
pass;
elif _type == 0x00FB: # PtypServerId
# TODO parsing for this
pass;
elif _type == 0x00FD: # PtypRestriction
# TODO parsing for this
pass;
elif _type == 0x00FE: # PtypRuleAction
# TODO parsing for this
pass;
elif _type == 0x0102: # PtypBinary
# TODO parsing for this
# Smh, how on earth am I going to code this???
pass;
elif _type & 0x1000 == 0x1000: # PtypMultiple
# TODO parsing for `multiple` types
pass;
return value;