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")
def parse(self, path): buf = open(path, "rb").read() eof = len(buf) logging.debug("path: %s", path) logging.debug("parsing INX of %s(%s) bytes", eof, hex(eof)) self._set_pos(0) num_entries = read_int(buf, self._pos, 1) logging.debug("number of entries: %s", num_entries) self._add_pos(1) result = INXParseResult(buf) for i in xrange(num_entries): logging.debug("entry %s of %s", i+1, num_entries) content_start = read_int(buf, self._pos, 8) logging.debug("content_start: %s (%s)", content_start, hex(content_start)) self._add_pos(8) name_length = read_int(buf, self._pos, 1) logging.debug("name_length: %s", name_length) self._add_pos(1) name = read_str(buf, self._pos, name_length) logging.debug("name: %s", name) self._add_pos(name_length) result.append(INXEntry(result, name, content_start)) return result
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