-
Notifications
You must be signed in to change notification settings - Fork 0
/
fdt.py
228 lines (175 loc) · 6.55 KB
/
fdt.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
# -*- coding: utf-8 -*-
'''
Created on 2013/02/27
@author: user
'''
import logging
logging.basicConfig(
level=logging.DEBUG,
format="%(levelname)-8s %(module)-16s %(funcName)-16s@%(lineno)d - %(message)s"
)
import unittest
from unpack import read_int, read_str, find
def read_utf8(f, p, n):
length = 0
eof = len(f)
buf = ""
while True:
if p == eof:
raise EOFError()
buf += read_str(f, p, 1)
p += 1
length += 1
if n <= length:
try:
text = unicode(buf, "utf-8")
if len(text) == n:
return UTF8String(length, text)
except UnicodeDecodeError:
pass
def read_vint(f, p):
"""
Decodes variable-length format for position integers.
http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/fileformats.html#VInt
"""
eof = len(f)
length = 0
total = 0
while True:
if p == eof:
raise EOFError()
i = read_int(f, p, 1)
has_remain = False
if 0b10000000 <= i: #high-order bit is 1
has_remain = True
i = i ^ 0b10000000 #high-order bit to 0
else:
has_remain = False
total = total | i << 7 * length #append low-order 7 bits to the result
length += 1
p += 1
if not has_remain:
break
return VInt(length, total)
class LuceneFDTDataType(object):
pass
class VInt(LuceneFDTDataType):
def __init__(self, length, value):
self.length = length
self.value = value
def __str__(self):
return "VInt: length=%s, value=%s" % (self.length, self.value)
def __int__(self):
return self.value
class UTF8String(LuceneFDTDataType):
def __init__(self, length, value):
self.length = length
self.value = value
def __str__(self):
return "UTF8String: length=%s, value=%s" % (self.length, self.value)
def __int__(self):
return self.value
class FDTParser(object):
def _set_pos(self, n):
self._pos = n
logging.debug("position moved to %s", hex(self._pos))
def _add_pos(self, n):
self._pos += n
logging.debug("position moved to %s", hex(self._pos))
def _read_vstr(self, buf):
vint = read_vint(buf, self._pos)
logging.debug(vint)
self._add_pos(vint.length)
vstr = read_utf8(buf, self._pos, vint.value)
logging.debug(vstr)
self._add_pos(vstr.length)
return vstr
def accept(self, buf, visitor):
"""
[0x0B 0x09 0x00] ... 2 bytes ...
[main id: 11 byte] ... 3 bytes ...
[sub id: 3byte] ... ? ...
[0x00 0x00 0x02 0x01]
[VInt A] [VStr(heading): A.length] ... ? ...
[0x0B 0x00]
[VInt B] [VSTr(body): B.length]
...
OR
[0x00 0x00 0x03 0x01]
[Vint C] [VStr(heading): A.length]
...
"""
logging.debug("parsing FDT of %s(%s) bytes", len(buf), hex(len(buf)))
self._set_pos(0)
try:
while True:
i = find(buf, self._pos, 0x0B, 0x09, 0x00)
logging.debug("[0x0B 0x09 0x00] found at %s", hex(i))
self._set_pos(i)
main_id = read_str(buf, self._pos+4, 11)
sub_id = read_str(buf, self._pos+18, 3)
logging.debug("main id: %s", main_id)
logging.debug("sub id: %s", sub_id)
if int(sub_id) == 0:
i = find(buf, self._pos, 0x00, 0x00, 0x02, 0x01)
logging.debug("[0x00 0x00 0x02 0x01] found at %s", hex(i))
else:
i = find(buf, self._pos, 0x00, 0x00, 0x03, 0x01)
logging.debug("[0x00 0x00 0x03 0x01] found at %s", hex(i))
self._set_pos(i+4)
heading = self._read_vstr(buf)
if int(sub_id) > 0:
visitor.example(FDTParserExample(main_id, sub_id, heading.value))
continue
i = find(buf, self._pos, 0x0B, 0x00)
logging.debug("[0x0B 0x00 found] at %s", hex(i))
if i - self._pos > 5:
logging.warn("[0x0B 0x00](%s) found long after [0x00 0x00 0x02 0x01](%s)",
hex(i), hex(self._pos))
self._set_pos(i+2)
body = self._read_vstr(buf)
visitor.document(FDTParserDocument(main_id, sub_id, heading.value, body.value))
except EOFError:
logging.debug("rearch EOF")
logging.debug("parse finishied")
class TestFDTParser(unittest.TestCase):
def test_parse_header(self):
parser = FDTParser()
visitor = FDTParserVisitorExample()
parser.accept(open("test/_ozh.fdt", "rb").read(), visitor)
visitor.show()
class FDTParserFragment(object):
pass
class FDTParserExample(FDTParserFragment):
def __init__(self, main_id, sub_id, heading):
self.main_id = main_id
self.sub_id = sub_id
self.heading = heading
def __str__(self):
return "%s" % self.heading
class FDTParserDocument(FDTParserFragment):
def __init__(self, main_id, sub_id, heading, body):
self.main_id = main_id
self.sub_id = sub_id
self.heading = heading
self.body = body
def __str__(self):
return u"%s %s" % (self.heading, self.body)
class FDTParserVisitor(object):
def example(self, example):
pass
def document(self, document):
pass
class FDTParserVisitorExample(FDTParserVisitor):
def __init__(self):
self.examples = 0
self.documents = 0
def example(self, example):
logging.info(example)
self.examples += 1
def document(self, document):
logging.info(document)
self.documents += 1
def show(self):
logging.info("documents: %s, examples: %s, total: %s",
self.documents, self.examples, self.documents+self.examples)