-
Notifications
You must be signed in to change notification settings - Fork 20
/
output_writers.py
280 lines (213 loc) · 6.88 KB
/
output_writers.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# -*- coding: utf-8 -*-
"""Output writer."""
import abc
from dfdatetime import fat_date_time as dfdatetime_fat_date_time
from dfdatetime import filetime as dfdatetime_filetime
from winregrc import hexdump
class OutputWriter(object):
"""Output writer interface."""
# Note that redundant-returns-doc is broken for pylint 1.7.x
# pylint: disable=redundant-returns-doc
_HEXDUMP_CHARACTER_MAP = [
'.' if byte < 0x20 or byte > 0x7e else chr(byte) for byte in range(256)]
def _FormatDataInHexadecimal(self, data):
"""Formats data in a hexadecimal representation.
Args:
data (bytes): data.
Returns:
str: hexadecimal representation of the data.
"""
in_group = False
previous_hexadecimal_string = None
lines = []
data_size = len(data)
for block_index in range(0, data_size, 16):
data_string = data[block_index:block_index + 16]
hexadecimal_byte_values = []
printable_values = []
for byte_value in data_string:
if isinstance(byte_value, str):
byte_value = ord(byte_value)
hexadecimal_byte_values.append(f'{byte_value:02x}')
printable_value = self._HEXDUMP_CHARACTER_MAP[byte_value]
printable_values.append(printable_value)
remaining_size = 16 - len(data_string)
if remaining_size == 0:
whitespace = ''
elif remaining_size >= 8:
whitespace = ' ' * ((3 * remaining_size) - 1)
else:
whitespace = ' ' * (3 * remaining_size)
hexadecimal_string_part1 = ' '.join(hexadecimal_byte_values[0:8])
hexadecimal_string_part2 = ' '.join(hexadecimal_byte_values[8:16])
hexadecimal_string = (
f'{hexadecimal_string_part1:s} {hexadecimal_string_part2:s}'
f'{whitespace:s}')
if (previous_hexadecimal_string is not None and
previous_hexadecimal_string == hexadecimal_string and
block_index + 16 < data_size):
if not in_group:
in_group = True
lines.append('...')
else:
printable_string = ''.join(printable_values)
lines.append(
f'0x{block_index:08x} {hexadecimal_string:s} '
f'{printable_string:s}')
in_group = False
previous_hexadecimal_string = hexadecimal_string
lines.extend(['', ''])
return '\n'.join(lines)
def _FormatFATDateTimeValue(self, value):
"""Formats a FAT date time value.
Args:
value (int): FAT date time value.
Returns:
str: date time string.
"""
if not value:
date_time_string = 'Not set (0)'
else:
date_time = dfdatetime_fat_date_time.FATDateTime(fat_date_time=value)
date_time_string = date_time.CopyToDateTimeString()
if not date_time_string:
date_time_string = f'0x{value:04x}'
return date_time_string
def _FormatFiletimeValue(self, value):
"""Formats a FILETIME timestamp value.
Args:
value (int): FILETIME timestamp value.
Returns:
str: date time string.
"""
if value == 0:
date_time_string = 'Not set (0)'
elif value == 0x7fffffffffffffff:
date_time_string = 'Never (0x7fffffffffffffff)'
else:
date_time = dfdatetime_filetime.Filetime(timestamp=value)
date_time_string = date_time.CopyToDateTimeString()
if date_time_string:
date_time_string = f'{date_time_string:s} UTC'
else:
date_time_string = f'0x{value:08x}'
return date_time_string
@abc.abstractmethod
def Close(self):
"""Closes the output writer."""
def DebugPrintData(self, description, data):
"""Prints data for debugging.
Args:
description (str): description.
data (bytes): data.
"""
self.WriteText(f'{description:s}:\n')
value_string = self._FormatDataInHexadecimal(data)
self.WriteText(value_string)
def DebugPrintValue(self, description, value):
"""Prints a value for debugging.
Args:
description (str): description.
value (object): value.
"""
alignment, _ = divmod(len(description), 8)
alignment_string = '\t' * (8 - alignment + 1)
self.WriteText(f'{description:s}{alignment_string:s}: {value!s}\n')
def DebugPrintText(self, text):
"""Prints text for debugging.
Args:
text (str): text.
"""
self.WriteText(text)
@abc.abstractmethod
def Open(self):
"""Opens the output writer.
Returns:
bool: True if successful or False if not.
"""
@abc.abstractmethod
def WriteDebugData(self, description, data):
"""Writes data for debugging.
Args:
description (str): description.
data (bytes): data to write.
"""
@abc.abstractmethod
def WriteIntegerValueAsDecimal(self, description, value):
"""Writes an integer value as decimal.
Args:
description (str): description.
value (int): value to write.
"""
@abc.abstractmethod
def WriteFiletimeValue(self, description, value):
"""Writes a FILETIME timestamp value.
Args:
description (str): description.
value (str): value to write.
"""
@abc.abstractmethod
def WriteText(self, text):
"""Writes text.
Args:
text (str): text to write.
"""
@abc.abstractmethod
def WriteValue(self, description, value):
"""Writes a value.
Args:
description (str): description.
value (str): value to write.
"""
class StdoutOutputWriter(OutputWriter):
"""Stdout output writer."""
def Close(self):
"""Closes the output writer."""
return
def Open(self):
"""Opens the output writer.
Returns:
bool: True if successful or False if not.
"""
return True
def WriteDebugData(self, description, data):
"""Writes data for debugging.
Args:
description (str): description.
data (bytes): data.
"""
self.WriteText(description)
self.WriteText('\n')
hexdump_text = hexdump.Hexdump(data)
self.WriteText(hexdump_text)
def WriteFiletimeValue(self, description, value):
"""Writes a FILETIME timestamp value.
Args:
description (str): description.
value (int): FILETIME timestamp value.
"""
date_time_string = self._FormatFiletimeValue(value)
self.WriteValue(description, date_time_string)
def WriteIntegerValueAsDecimal(self, description, value):
"""Writes an integer value as decimal.
Args:
description (str): description.
value (int): integer value.
"""
self.WriteValue(description, f'{value:d}')
def WriteText(self, text):
"""Writes text.
Args:
text (str): text to write.
"""
print(text, end='')
def WriteValue(self, description, value):
"""Writes a value.
Args:
description (str): description.
value (object): value.
"""
description_no_tabs = description.replace('\t', ' ' * 8)
alignment, _ = divmod(len(description_no_tabs), 8)
alignment_string = '\t' * (8 - alignment + 1)
self.WriteText(f'{description:s}{alignment_string:s}: {value!s}\n')